org-clock: Fix resolving clocks

* lisp/org-clock.el (org-clock-resolve-clock): Fix resolving clocks
  when the entry is empty barring the clock itself and possibly the
  clock drawer, and `org-clock-out-remove-zero-time-clocks' is
  non-nil.
This commit is contained in:
Nicolas Goaziou 2018-12-06 00:24:14 +01:00
parent 04641c4bbe
commit 503ede74bc
1 changed files with 36 additions and 33 deletions

View File

@ -917,39 +917,42 @@ If necessary, clock-out of the currently active clock."
(defvar org-clock-resolving-clocks nil) (defvar org-clock-resolving-clocks nil)
(defvar org-clock-resolving-clocks-due-to-idleness nil) (defvar org-clock-resolving-clocks-due-to-idleness nil)
(defun org-clock-resolve-clock (clock resolve-to clock-out-time (defun org-clock-resolve-clock
&optional close-p restart-p fail-quietly) (clock resolve-to clock-out-time close restart fail-quietly)
"Resolve `CLOCK' given the time `RESOLVE-TO', and the present. "Resolve CLOCK given the time RESOLVE-TO, and the present.
`CLOCK' is a cons cell of the form (MARKER START-TIME)." CLOCK is a cons cell of the form (MARKER START-TIME)."
(let ((org-clock-resolving-clocks t)) (let ((org-clock-resolving-clocks t)
(cond ;; If the clocked entry contained only a clock and possibly
((null resolve-to) ;; the associated drawer, and we either cancel it or clock it
(org-clock-clock-cancel clock) ;; out, `org-clock-out-remove-zero-time-clocks' may clear all
(if (and restart-p (not org-clock-clocking-in)) ;; contents, and leave point on the /next/ headline. We store
(org-clock-clock-in clock))) ;; the current entry location to be able to get back here when
;; we need to clock in again the previously clocked task.
((eq resolve-to 'now) (heading (org-with-point-at (car clock) (org-back-to-heading t))))
(if restart-p (pcase resolve-to
(error "RESTART-P is not valid here")) (`nil
(if (or close-p org-clock-clocking-in) (org-clock-clock-cancel clock)
(org-clock-clock-out clock fail-quietly) (when (and restart (not org-clock-clocking-in))
(unless (org-is-active-clock clock) (org-with-point-at heading (org-clock-in))))
(org-clock-clock-in clock t)))) (`now
(cond
((not (time-less-p resolve-to (current-time))) (restart (error "RESTART is not valid here"))
(error "RESOLVE-TO must refer to a time in the past")) ((or close org-clock-clocking-in)
(org-clock-clock-out clock fail-quietly))
(t ((org-is-active-clock clock) nil)
(if restart-p (t (org-clock-clock-in clock t))))
(error "RESTART-P is not valid here")) ((pred (time-less-p (current-time)))
(org-clock-clock-out clock fail-quietly (or clock-out-time (error "RESOLVE-TO must refer to a time in the past"))
resolve-to)) (_
(unless org-clock-clocking-in (when restart (error "RESTART is not valid here"))
(if close-p (org-clock-clock-out clock fail-quietly (or clock-out-time resolve-to))
(setq org-clock-leftover-time (and (null clock-out-time) (cond
resolve-to)) (org-clock-clocking-in nil)
(org-clock-clock-in clock nil (and clock-out-time (close
resolve-to)))))))) (setq org-clock-leftover-time (and (null clock-out-time) resolve-to)))
(t
(org-with-point-at heading
(org-clock-in nil (and clock-out-time resolve-to)))))))))
(defun org-clock-jump-to-current-clock (&optional effective-clock) (defun org-clock-jump-to-current-clock (&optional effective-clock)
"When an Org clock is running, jump to it." "When an Org clock is running, jump to it."