mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-08-26 01:32:53 +00:00
org-clock-update-time-maybe: Preserve point position better
* lisp/org-clock.el: Try harder to preserve point position upon updating the clock line. Even when `save-excursion' is not sufficient. Reported-by: Daniel Clemente <n142857@gmail.com> Link: https://orgmode.org/list/CAJKAhPDVs5GRKR-cmyeNd5cQFHyKpw_=JptBzi1f0FYNTqqjzA@mail.gmail.com
This commit is contained in:
parent
7e6b55b8fd
commit
4dddbc1436
|
@ -3049,53 +3049,58 @@ PROPERTIES: The list properties specified in the `:properties' parameter
|
|||
"If this is a CLOCK line, update it and return t.
|
||||
Otherwise, return nil."
|
||||
(interactive)
|
||||
(save-excursion
|
||||
(beginning-of-line 1)
|
||||
(skip-chars-forward " \t")
|
||||
(when (looking-at org-clock-string)
|
||||
(let ((re (concat "[ \t]*" org-clock-string
|
||||
" *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
|
||||
"\\([ \t]*=>.*\\)?\\)?"))
|
||||
ts te h m s neg)
|
||||
(cond
|
||||
((not (looking-at re))
|
||||
nil)
|
||||
((not (match-end 2))
|
||||
(when (and (equal (marker-buffer org-clock-marker) (current-buffer))
|
||||
(> org-clock-marker (point))
|
||||
(<= org-clock-marker (line-end-position)))
|
||||
;; The clock is running here
|
||||
(setq org-clock-start-time
|
||||
(org-time-string-to-time (match-string 1)))
|
||||
(org-clock-update-mode-line)))
|
||||
(t
|
||||
;; Prevent recursive call from `org-timestamp-change'.
|
||||
(cl-letf (((symbol-function 'org-clock-update-time-maybe) #'ignore))
|
||||
;; Update timestamps.
|
||||
(save-excursion
|
||||
(goto-char (match-beginning 1)) ; opening timestamp
|
||||
(save-match-data (org-timestamp-change 0 'day)))
|
||||
(let ((origin (point))) ;; `save-excursion' may not work when deleting.
|
||||
(save-excursion
|
||||
(beginning-of-line 1)
|
||||
(skip-chars-forward " \t")
|
||||
(when (looking-at org-clock-string)
|
||||
(let ((re (concat "[ \t]*" org-clock-string
|
||||
" *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
|
||||
"\\([ \t]*=>.*\\)?\\)?"))
|
||||
ts te h m s neg)
|
||||
(cond
|
||||
((not (looking-at re))
|
||||
nil)
|
||||
((not (match-end 2))
|
||||
(when (and (equal (marker-buffer org-clock-marker) (current-buffer))
|
||||
(> org-clock-marker (point))
|
||||
(<= org-clock-marker (line-end-position)))
|
||||
;; The clock is running here
|
||||
(setq org-clock-start-time
|
||||
(org-time-string-to-time (match-string 1)))
|
||||
(org-clock-update-mode-line)))
|
||||
(t
|
||||
;; Prevent recursive call from `org-timestamp-change'.
|
||||
(cl-letf (((symbol-function 'org-clock-update-time-maybe) #'ignore))
|
||||
;; Update timestamps.
|
||||
(save-excursion
|
||||
(goto-char (match-beginning 1)) ; opening timestamp
|
||||
(save-match-data (org-timestamp-change 0 'day)))
|
||||
;; Refresh match data.
|
||||
(looking-at re)
|
||||
(save-excursion
|
||||
(goto-char (match-beginning 3)) ; closing timestamp
|
||||
(save-match-data (org-timestamp-change 0 'day))))
|
||||
;; Refresh match data.
|
||||
(looking-at re)
|
||||
(save-excursion
|
||||
(goto-char (match-beginning 3)) ; closing timestamp
|
||||
(save-match-data (org-timestamp-change 0 'day))))
|
||||
;; Refresh match data.
|
||||
(looking-at re)
|
||||
(and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
|
||||
(end-of-line 1)
|
||||
(setq ts (match-string 1)
|
||||
te (match-string 3))
|
||||
(setq s (- (org-time-string-to-seconds te)
|
||||
(org-time-string-to-seconds ts))
|
||||
neg (< s 0)
|
||||
s (abs s)
|
||||
h (floor (/ s 3600))
|
||||
s (- s (* 3600 h))
|
||||
m (floor (/ s 60))
|
||||
s (- s (* 60 s)))
|
||||
(insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
|
||||
t))))))
|
||||
(and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
|
||||
(end-of-line 1)
|
||||
(setq ts (match-string 1)
|
||||
te (match-string 3))
|
||||
(setq s (- (org-time-string-to-seconds te)
|
||||
(org-time-string-to-seconds ts))
|
||||
neg (< s 0)
|
||||
s (abs s)
|
||||
h (floor (/ s 3600))
|
||||
s (- s (* 3600 h))
|
||||
m (floor (/ s 60))
|
||||
s (- s (* 60 s)))
|
||||
(insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
|
||||
t)))))
|
||||
;; Move back to initial position, but never beyond updated
|
||||
;; clock.
|
||||
(unless (< (point) origin)
|
||||
(goto-char origin))))
|
||||
|
||||
(defun org-clock-save ()
|
||||
"Persist various clock-related data to disk.
|
||||
|
|
Loading…
Reference in a new issue