org-clock-sum: Do not err when have malformed clock lines

* lisp/org-clock.el: Only consider proper clock elements when
calculating clock sum.  Do not rely on crude regexp.

Reported-by: Gregor Zattler <grfz@gmx.de>
Link: https://orgmode.org/list/87y1yecmgb.fsf@localhost
This commit is contained in:
Ihor Radchenko 2022-10-13 14:12:36 +08:00
parent 0e4874f17c
commit 3790bf8ea1
No known key found for this signature in database
GPG key ID: 6470762A7DA11D8B

View file

@ -1914,62 +1914,66 @@ PROPNAME lets you set a custom text property instead of :org-clock-minutes."
(save-excursion (save-excursion
(goto-char (point-max)) (goto-char (point-max))
(while (re-search-backward re nil t) (while (re-search-backward re nil t)
(cond (let ((element-type
((match-end 2) (org-element-type
;; Two time stamps. (save-match-data
(let* ((ss (match-string 2)) (org-element-at-point)))))
(se (match-string 3)) (cond
(ts (org-time-string-to-seconds ss)) ((and (eq element-type 'clock) (match-end 2))
(te (org-time-string-to-seconds se)) ;; Two time stamps.
(dt (- (if tend (min te tend) te) (let* ((ss (match-string 2))
(if tstart (max ts tstart) ts)))) (se (match-string 3))
(when (> dt 0) (cl-incf t1 (floor dt 60))))) (ts (org-time-string-to-seconds ss))
((match-end 4) (te (org-time-string-to-seconds se))
;; A naked time. (dt (- (if tend (min te tend) te)
(setq t1 (+ t1 (string-to-number (match-string 5)) (if tstart (max ts tstart) ts))))
(* 60 (string-to-number (match-string 4)))))) (when (> dt 0) (cl-incf t1 (floor dt 60)))))
(t ;A headline ((match-end 4)
;; Add the currently clocking item time to the total. ;; A naked time.
(when (and org-clock-report-include-clocking-task (setq t1 (+ t1 (string-to-number (match-string 5))
(eq (org-clocking-buffer) (current-buffer)) (* 60 (string-to-number (match-string 4))))))
(eq (marker-position org-clock-hd-marker) (point)) ((memq element-type '(headline inlinetask)) ;A headline
tstart ;; Add the currently clocking item time to the total.
tend (when (and org-clock-report-include-clocking-task
(>= (float-time org-clock-start-time) tstart) (eq (org-clocking-buffer) (current-buffer))
(<= (float-time org-clock-start-time) tend)) (eq (marker-position org-clock-hd-marker) (point))
(let ((time (floor (org-time-convert-to-integer tstart
(time-since org-clock-start-time)) tend
60))) (>= (float-time org-clock-start-time) tstart)
(setq t1 (+ t1 time)))) (<= (float-time org-clock-start-time) tend))
(let* ((headline-forced (let ((time (floor (org-time-convert-to-integer
(get-text-property (point) (time-since org-clock-start-time))
:org-clock-force-headline-inclusion)) 60)))
(headline-included (setq t1 (+ t1 time))))
(or (null headline-filter) (let* ((headline-forced
(save-excursion (get-text-property (point)
(save-match-data (funcall headline-filter)))))) :org-clock-force-headline-inclusion))
(setq level (- (match-end 1) (match-beginning 1))) (headline-included
(when (>= level lmax) (or (null headline-filter)
(setq ltimes (vconcat ltimes (make-vector lmax 0)) lmax (* 2 lmax))) (save-excursion
(when (or (> t1 0) (> (aref ltimes level) 0)) (save-match-data (funcall headline-filter))))))
(when (or headline-included headline-forced) (setq level (- (match-end 1) (match-beginning 1)))
(if headline-included (when (>= level lmax)
(cl-loop for l from 0 to level do (setq ltimes (vconcat ltimes (make-vector lmax 0)) lmax (* 2 lmax)))
(aset ltimes l (+ (aref ltimes l) t1)))) (when (or (> t1 0) (> (aref ltimes level) 0))
(setq time (aref ltimes level)) (when (or headline-included headline-forced)
(goto-char (match-beginning 0)) (if headline-included
(put-text-property (point) (line-end-position) (cl-loop for l from 0 to level do
(or propname :org-clock-minutes) time) (aset ltimes l (+ (aref ltimes l) t1))))
(when headline-filter (setq time (aref ltimes level))
(save-excursion (goto-char (match-beginning 0))
(save-match-data (put-text-property (point) (line-end-position)
(while (org-up-heading-safe) (or propname :org-clock-minutes) time)
(put-text-property (when headline-filter
(point) (line-end-position) (save-excursion
:org-clock-force-headline-inclusion t)))))) (save-match-data
(setq t1 0) (while (org-up-heading-safe)
(cl-loop for l from level to (1- lmax) do (put-text-property
(aset ltimes l 0))))))) (point) (line-end-position)
:org-clock-force-headline-inclusion t))))))
(setq t1 0)
(cl-loop for l from level to (1- lmax) do
(aset ltimes l 0))))))))
(setq org-clock-file-total-minutes (aref ltimes 0)))))) (setq org-clock-file-total-minutes (aref ltimes 0))))))
(defun org-clock-sum-current-item (&optional tstart) (defun org-clock-sum-current-item (&optional tstart)