[Org] Refactor ‘db/org-clocking-time-in-range’

This function now uses the more general ‘db/org-map-clock-lines-and-entries’,
which is also going to be used by the future ‘db/org-add-clock-line-to-file’ (or
similar).
This commit is contained in:
Daniel - 2017-12-16 10:42:23 +01:00
parent 1f00baadde
commit 34e639ac98
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
1 changed files with 49 additions and 41 deletions

View File

@ -939,10 +939,32 @@ Current Task: %`org-clock-current-task; "
(require 'dash) (require 'dash)
(defun db/org-map-clock-lines-and-entries (clockline-fn headline-fn)
"Iterate point over all clocklines and headlines of the current buffer.
For each clockline, call CLOCKLINE-FN with the starting and
ending time as arguments and point on the beginning of the line.
For each headline, call HEADLINE-FN with no arguments and point
on the start of the headline. Traversal will be done from the
end of the file upwards."
(when (eq major-mode 'org-mode)
(let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
org-clock-string
"[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)")))
(save-excursion
(goto-char (point-max))
(while (re-search-backward re nil t)
(cond
((match-end 2)
;; Two time stamps.
(funcall clockline-fn (match-string 2) (match-string 3)))
(t
;; A headline
(funcall headline-fn))))))))
(defun db/org-clocking-time-in-range (tstart tend) (defun db/org-clocking-time-in-range (tstart tend)
"Return list of all tasks in the current buffer together with "Return list of all tasks in the current buffer together with
their clocking times that are between TSTART and TEND. The their clocking times that are between TSTART and TEND. The
resulting list conists of elements of the form resulting list consists of elements of the form
(MARKER . CLOCK-TIMES) (MARKER . CLOCK-TIMES)
@ -956,52 +978,38 @@ or END may occassionally lie outside of these limits, but it is
always true that TSTART END TEND or TSTART START TEND." always true that TSTART END TEND or TSTART START TEND."
;; adapted from `org-clock-sum ;; adapted from `org-clock-sum
(when (eq major-mode 'org-mode) (when (eq major-mode 'org-mode)
(let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*" (let* ((tstart (cond ((stringp tstart) (org-time-string-to-seconds tstart))
org-clock-string
"[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
(level 0)
(tstart (cond ((stringp tstart) (org-time-string-to-seconds tstart))
((consp tstart) (float-time tstart)) ((consp tstart) (float-time tstart))
(t tstart))) (t tstart)))
(tend (cond ((stringp tend) (org-time-string-to-seconds tend)) (tend (cond ((stringp tend) (org-time-string-to-seconds tend))
((consp tend) (float-time tend)) ((consp tend) (float-time tend))
(t tend))) (t tend)))
(t1 0)
task-clock-times headline times) task-clock-times headline times)
(save-excursion (db/org-map-clock-lines-and-entries
(goto-char (point-max)) ;; when on clock line, collect times
(while (re-search-backward re nil t) #'(lambda (start end)
(cond (let* ((ts (float-time
((match-end 2) (apply #'encode-time (org-parse-time-string start))))
;; Two time stamps. (te (float-time
(let* ((ts (float-time (apply #'encode-time (org-parse-time-string end))))
(apply #'encode-time (dt (- (if tend (min te tend) te)
(save-match-data (if tstart (max ts tstart) ts))))
(org-parse-time-string (when (> dt 0)
(match-string 2)))))) (push (cons ts te) times))))
(te (float-time ;; when on headlines, store away collected clocklines
(apply #'encode-time #'(lambda ()
(save-match-data (when (and org-clock-report-include-clocking-task
(org-parse-time-string (eq (org-clocking-buffer) (current-buffer))
(match-string 3)))))) (eq (marker-position org-clock-hd-marker) (point))
(dt (- (if tend (min te tend) te) (or (and tstart
(if tstart (max ts tstart) ts)))) (<= tstart (float-time org-clock-start-time) tend))
(when (> dt 0) (and tend
(push (cons ts te) times)))) (<= tstart (float-time) tend))))
(t (push (cons (float-time org-clock-start-time) (float-time))
;; A headline times))
(when (and org-clock-report-include-clocking-task (when (not (null times))
(eq (org-clocking-buffer) (current-buffer)) (push (cons (point-marker) times) task-clock-times)
(eq (marker-position org-clock-hd-marker) (point)) (setq times nil))))
(or (and tstart
(<= tstart (float-time org-clock-start-time) tend))
(and tend
(<= tstart (float-time) tend))))
(push (cons (float-time org-clock-start-time) (float-time))
times))
(when (not (null times))
(push (cons (point-marker) times) task-clock-times)
(setq times nil))))))
task-clock-times))) task-clock-times)))
(defun db/org-timeline-in-range (tstart tend &optional files) (defun db/org-timeline-in-range (tstart tend &optional files)