org-clock.el (org-clock-select-task): Throw a user error when the clock history is empty

* org-clock.el (org-clock-select-task): Throw a user error
when the clock history is empty.
This commit is contained in:
Bastien Guerry 2013-04-10 10:08:45 +02:00
parent 0480d0a0d0
commit ff4b7a4784
1 changed files with 44 additions and 40 deletions

View File

@ -514,47 +514,51 @@ of a different task.")
"Hook called in task selection just before prompting the user.") "Hook called in task selection just before prompting the user.")
(defun org-clock-select-task (&optional prompt) (defun org-clock-select-task (&optional prompt)
"Select a task that recently was associated with clocking." "Select a task that was recently associated with clocking."
(interactive) (interactive)
(let (sel-list rpl (i 0) s) (let ((chl (length org-clock-history)) sel-list rpl (i 0) s)
(save-window-excursion (if (zerop chl)
(org-switch-to-buffer-other-window (user-error "No recent clock")
(get-buffer-create "*Clock Task Select*")) (save-window-excursion
(erase-buffer) (org-switch-to-buffer-other-window
(when (marker-buffer org-clock-default-task) (get-buffer-create "*Clock Task Select*"))
(insert (org-add-props "Default Task\n" nil 'face 'bold)) (erase-buffer)
(setq s (org-clock-insert-selection-line ?d org-clock-default-task)) (when (marker-buffer org-clock-default-task)
(push s sel-list)) (insert (org-add-props "Default Task\n" nil 'face 'bold))
(when (marker-buffer org-clock-interrupted-task) (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
(insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold)) (push s sel-list))
(setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task)) (when (marker-buffer org-clock-interrupted-task)
(push s sel-list)) (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
(when (org-clocking-p) (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
(insert (org-add-props "Current Clocking Task\n" nil 'face 'bold)) (push s sel-list))
(setq s (org-clock-insert-selection-line ?c org-clock-marker)) (when (org-clocking-p)
(push s sel-list)) (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
(insert (org-add-props "Recent Tasks\n" nil 'face 'bold)) (setq s (org-clock-insert-selection-line ?c org-clock-marker))
(mapc (push s sel-list))
(lambda (m) (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
(when (marker-buffer m) (mapc
(setq i (1+ i) (lambda (m)
s (org-clock-insert-selection-line (when (marker-buffer m)
(if (< i 10) (setq i (1+ i)
(+ i ?0) s (org-clock-insert-selection-line
(+ i (- ?A 10))) m)) (if (< i 10)
(if (fboundp 'int-to-char) (setf (car s) (int-to-char (car s)))) (+ i ?0)
(push s sel-list))) (+ i (- ?A 10))) m))
org-clock-history) (if (fboundp 'int-to-char) (setf (car s) (int-to-char (car s))))
(run-hooks 'org-clock-before-select-task-hook) (push s sel-list)))
(goto-char (point-min)) org-clock-history)
(org-fit-window-to-buffer nil nil (+ 6 (length org-clock-history))) (run-hooks 'org-clock-before-select-task-hook)
(message (or prompt "Select task for clocking:")) (goto-char (point-min))
(setq cursor-type nil rpl (read-char-exclusive)) ;; Set min-height relatively to circumvent a possible but in
(cond ;; `fit-window-to-buffer'
((eq rpl ?q) nil) (fit-window-to-buffer nil nil (if (< chl 10) chl (+ 5 chl)))
((eq rpl ?x) nil) (message (or prompt "Select task for clocking:"))
((assoc rpl sel-list) (cdr (assoc rpl sel-list))) (setq cursor-type nil rpl (read-char-exclusive))
(t (error "Invalid task choice %c" rpl)))))) (cond
((eq rpl ?q) nil)
((eq rpl ?x) nil)
((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
(t (user-error "Invalid task choice %c" rpl)))))))
(defun org-clock-insert-selection-line (i marker) (defun org-clock-insert-selection-line (i marker)
"Insert a line for the clock selection menu. "Insert a line for the clock selection menu.