[Timeline] Make clock line parser barf when not in Org mode

This commit is contained in:
Daniel - 2019-02-17 09:13:37 +01:00
parent 82e0967b9f
commit 07aad33448
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
2 changed files with 72 additions and 58 deletions

View File

@ -101,6 +101,19 @@ CLOCK: [2018-01-08 Mon 16:00]--[2018-01-08 Mon 16:15] => 0:15
((1515334380.0 . 1515338220.0) ((1515334380.0 . 1515338220.0)
(1515423600.0 . 1515424500.0))))))) (1515423600.0 . 1515424500.0)))))))
(ert-deftest timeline-tools-test-parse-clocklines-5 ()
"Test `timeline-tools-clocklines-in-range without org-mode."
(let ((result (should-error (with-temp-buffer
(insert "* Task 1\n")
(insert ":LOGBOOK:\n")
(insert "CLOCK: [2018-01-07 Sun 13:15]--[2018-01-07 Sun 14:00] => 0:45\n")
(insert ":END:\n")
(timeline-tools-clocklines-in-range 1515279600.0 1515366000.0))
:type 'user-error)))
(should (equal (cadr result)
"Not in Org mode buffer, cannot parse clocklines"))
(should (equal (car result)
'user-error))))
;; Conflict resolution tests ;; Conflict resolution tests

View File

@ -151,26 +151,28 @@ For each headline, call HEADLINE-FN with no arguments and point
on the start of the headline. Traversal will be done from the on the start of the headline. Traversal will be done from the
end of the file upwards. If the buffer is narrowed, only this end of the file upwards. If the buffer is narrowed, only this
region will be traversed." region will be traversed."
(when (eq major-mode 'org-mode) (unless (eq major-mode 'org-mode)
;; Make sure everything is visible, as otherwise editing may produce odd (user-error "Not in Org mode buffer, cannot parse clocklines"))
;; results
(org-cycle '(64))
(let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*" ;; Make sure everything is visible, as otherwise editing may produce odd
org-clock-string ;; results
"[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))) (org-cycle '(64))
(save-excursion
(goto-char (point-max)) (let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
(while (re-search-backward re nil t) org-clock-string
(cond "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)")))
((match-end 2) (save-excursion
;; Two time stamps. (goto-char (point-max))
(save-mark-and-excursion (while (re-search-backward re nil t)
(funcall clockline-fn (match-string 2) (match-string 3)))) (cond
(t ((match-end 2)
;; A headline ;; Two time stamps.
(save-mark-and-excursion (save-mark-and-excursion
(funcall headline-fn))))))))) (funcall clockline-fn (match-string 2) (match-string 3))))
(t
;; A headline
(save-mark-and-excursion
(funcall headline-fn))))))))
(defvar timeline-tools-org-inactive-timestamp-format (defvar timeline-tools-org-inactive-timestamp-format
(concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]") (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
@ -245,46 +247,45 @@ given as seconds since the epoch, as a floating point number.
Truncation with respect to TSTART and TEND is done, i.e., START Truncation with respect to TSTART and TEND is done, i.e., START
or END will always be in the interval [TSTART,TEND]." or END will always be in the interval [TSTART,TEND]."
;; adapted from `org-clock-sum ;; adapted from `org-clock-sum
(when (eq major-mode 'org-mode) (let* ((tstart (cond ((stringp tstart) (org-time-string-to-seconds tstart))
(let* ((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))) task-clock-times times)
task-clock-times times) (timeline-tools-map-clocklines
(timeline-tools-map-clocklines ;; when on clock line, collect times
;; when on clock line, collect times #'(lambda (start end)
#'(lambda (start end) (let* ((ts (org-time-string-to-seconds start))
(let* ((ts (org-time-string-to-seconds start)) (te (org-time-string-to-seconds end)))
(te (org-time-string-to-seconds end))) (when (or (<= tstart te tend)
(when (or (<= tstart te tend) (<= tstart ts tend)
(<= tstart ts tend) (<= ts tstart tend te))
(<= ts tstart tend te)) (push (cons (max ts tstart)
(push (cons (max ts tstart) (min te tend))
(min te tend)) times))))
;; when on headlines, store away collected clocklines
#'(lambda ()
;; add currently running clock if wanted
(when (and org-clock-report-include-clocking-task
(eq (org-clocking-buffer) (current-buffer))
(eq (marker-position org-clock-hd-marker) (point)))
(let ((current-clock-start (float-time org-clock-start-time))
(current-clock-end (float-time)))
(when (or (<= tstart current-clock-start tend)
(<= tstart current-clock-end tend)
(<= current-clock-start
tstart tend
current-clock-end))
(push (cons (max current-clock-start tstart)
(min current-clock-end tend))
times)))) times))))
;; when on headlines, store away collected clocklines ;; store away clocklines of current headline
#'(lambda () (when (not (null times))
;; add currently running clock if wanted (push (cons (point-marker) times) task-clock-times)
(when (and org-clock-report-include-clocking-task (setq times nil))))
(eq (org-clocking-buffer) (current-buffer)) task-clock-times))
(eq (marker-position org-clock-hd-marker) (point)))
(let ((current-clock-start (float-time org-clock-start-time))
(current-clock-end (float-time)))
(when (or (<= tstart current-clock-start tend)
(<= tstart current-clock-end tend)
(<= current-clock-start
tstart tend
current-clock-end))
(push (cons (max current-clock-start tstart)
(min current-clock-end tend))
times))))
;; store away clocklines of current headline
(when (not (null times))
(push (cons (point-marker) times) task-clock-times)
(setq times nil))))
task-clock-times)))
(defun timeline-tools-timeline (tstart tend &optional files) (defun timeline-tools-timeline (tstart tend &optional files)
"Return timeline between TSTART and TEND from FILES. "Return timeline between TSTART and TEND from FILES.