org-capture: Refresh clock mode line when finalizing capture

* lisp/org-capture.el (org-capture-finalize): Refresh clock mode line.
* lisp/org-clock.el (org-clock--mode-line-heading): New function.
(org-clock-get-clock-string): Small refactoring.
(org-clock-update-mode-line): Add optional argument. Use new function.
(org-clock-in): Use new function.
This commit is contained in:
Nicolas Goaziou 2018-02-21 15:02:33 +01:00
parent ec8590d5e2
commit 6655429b8d
2 changed files with 45 additions and 40 deletions

View File

@ -724,21 +724,24 @@ captured item after finalizing."
;; Did we start the clock in this capture buffer? ;; Did we start the clock in this capture buffer?
(when (and org-capture-clock-was-started (when (and org-capture-clock-was-started
org-clock-marker (marker-buffer org-clock-marker) org-clock-marker
(equal (marker-buffer org-clock-marker) (buffer-base-buffer)) (eq (marker-buffer org-clock-marker) (buffer-base-buffer))
(> org-clock-marker (point-min)) (>= org-clock-marker (point-min))
(< org-clock-marker (point-max))) (< org-clock-marker (point-max)))
;; Looks like the clock we started is still running. Clock out. ;; Looks like the clock we started is still running.
(when (not org-capture-clock-keep) (let (org-log-note-clock-out) (org-clock-out))) (if org-capture-clock-keep
(when (and (not org-capture-clock-keep) ;; User may have completed clocked heading from the template.
(org-capture-get :clock-resume 'local) ;; Refresh clock mode line.
(markerp (org-capture-get :interrupted-clock 'local)) (org-clock-update-mode-line t)
(buffer-live-p (marker-buffer ;; Clock out. Possibly resume interrupted clock.
(org-capture-get :interrupted-clock 'local)))) (let (org-log-note-clock-out) (org-clock-out))
(let ((clock-in-task (org-capture-get :interrupted-clock 'local))) (when (and (org-capture-get :clock-resume 'local)
(org-with-point-at clock-in-task (markerp (org-capture-get :interrupted-clock 'local))
(org-clock-in))) (buffer-live-p (marker-buffer
(message "Interrupted clock has been resumed"))) (org-capture-get :interrupted-clock 'local))))
(let ((clock-in-task (org-capture-get :interrupted-clock 'local)))
(org-with-point-at clock-in-task (org-clock-in)))
(message "Interrupted clock has been resumed"))))
(let ((beg (point-min)) (let ((beg (point-min))
(end (point-max)) (end (point-max))

View File

@ -513,6 +513,13 @@ cannot be translated."
(assoc-string language org-clock-clocktable-language-setup t)) (assoc-string language org-clock-clocktable-language-setup t))
s)) s))
(defun org-clock--mode-line-heading ()
"Return currently clocked heading, formatted for mode line."
(cond ((functionp org-clock-heading-function)
(funcall org-clock-heading-function))
((org-before-first-heading-p) "???")
(t (org-get-heading t t t t))))
(defun org-clock-menu () (defun org-clock-menu ()
(interactive) (interactive)
(popup-menu (popup-menu
@ -658,22 +665,22 @@ If an effort estimate was defined for the current item, use
01:30/01:50 format (clocked/estimated). 01:30/01:50 format (clocked/estimated).
If not, show simply the clocked time like 01:50." If not, show simply the clocked time like 01:50."
(let ((clocked-time (org-clock-get-clocked-time))) (let ((clocked-time (org-clock-get-clocked-time)))
(if org-clock-effort (propertize
(let* ((effort-in-minutes (org-duration-to-minutes org-clock-effort)) (if org-clock-effort
(work-done-str (let* ((effort-in-minutes (org-duration-to-minutes org-clock-effort))
(propertize (work-done-str
(org-duration-from-minutes clocked-time) (propertize (org-duration-from-minutes clocked-time)
'face (if (and org-clock-task-overrun (not org-clock-task-overrun-text)) 'face
'org-mode-line-clock-overrun 'org-mode-line-clock))) (if (and org-clock-task-overrun
(effort-str (org-duration-from-minutes effort-in-minutes)) (not org-clock-task-overrun-text))
(clockstr (propertize 'org-mode-line-clock-overrun
(concat " [%s/" effort-str 'org-mode-line-clock)))
"] (" (replace-regexp-in-string "%" "%%" org-clock-heading) ")") (effort-str (org-duration-from-minutes effort-in-minutes)))
'face 'org-mode-line-clock))) (format " [%s/%s] (%s)" work-done-str effort-str org-clock-heading))
(format clockstr work-done-str)) (format " [%s] (%s)"
(propertize (concat " [" (org-duration-from-minutes clocked-time) (org-duration-from-minutes clocked-time)
"]" (format " (%s)" org-clock-heading)) org-clock-heading))
'face 'org-mode-line-clock)))) 'face 'org-mode-line-clock)))
(defun org-clock-get-last-clock-out-time () (defun org-clock-get-last-clock-out-time ()
"Get the last clock-out time for the current subtree." "Get the last clock-out time for the current subtree."
@ -683,10 +690,13 @@ If not, show simply the clocked time like 01:50."
".*\\]--\\(\\[[^]]+\\]\\)") end t) ".*\\]--\\(\\[[^]]+\\]\\)") end t)
(org-time-string-to-time (match-string 1)))))) (org-time-string-to-time (match-string 1))))))
(defun org-clock-update-mode-line () (defun org-clock-update-mode-line (&optional refresh)
"Update mode line with clock information.
When optional argument is non-nil, refresh cached heading."
(if org-clock-effort (if org-clock-effort
(org-clock-notify-once-if-expired) (org-clock-notify-once-if-expired)
(setq org-clock-task-overrun nil)) (setq org-clock-task-overrun nil))
(when refresh (setq org-clock-heading (org-clock--mode-line-heading)))
(setq org-mode-line-string (setq org-mode-line-string
(propertize (propertize
(let ((clock-string (org-clock-get-clock-string)) (let ((clock-string (org-clock-get-clock-string))
@ -1270,15 +1280,7 @@ the default behavior."
org-clock-in-switch-to-state org-clock-in-switch-to-state
"\\>")))) "\\>"))))
(org-todo org-clock-in-switch-to-state))) (org-todo org-clock-in-switch-to-state)))
(setq org-clock-heading (setq org-clock-heading (org-clock--mode-line-heading))
(cond ((and org-clock-heading-function
(functionp org-clock-heading-function))
(funcall org-clock-heading-function))
((nth 4 (org-heading-components))
(replace-regexp-in-string
"\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
(match-string-no-properties 4)))
(t "???")))
(org-clock-find-position org-clock-in-resume) (org-clock-find-position org-clock-in-resume)
(cond (cond
((and org-clock-in-resume ((and org-clock-in-resume