[Org] Cleaing up code on manipulating clock lines

This commit is contained in:
Daniel - 2018-01-13 13:06:31 +01:00
parent 0fa88c141f
commit 2fcc4c4685
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
1 changed files with 35 additions and 46 deletions

View File

@ -1069,6 +1069,22 @@ ending at 23:61. When not given, FILES defaults to
(concat date " 23:61") (concat date " 23:61")
files)) files))
;;; Manipulating Clock Lines
(defun db/org-insert-clockline (time-1 time-2)
"Insert new clock line from TIME-1 to TIME-2 at the beginning
of the current line. TIME-1 and TIME-2 must be given in a
format understandable by `format-time-string, which see."
(let ((timestamp-format (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")))
(beginning-of-line)
(indent-according-to-mode)
(insert "CLOCK: ")
(insert (format-time-string timestamp-format time-1))
(insert "--")
(insert (format-time-string timestamp-format time-2))
(org-clock-update-time-maybe)))
(defun db/org-add-clocking-time (starting-time ending-time) (defun db/org-add-clocking-time (starting-time ending-time)
"Add \"CLOCK:\" line to the task under point in the current org-mode file." "Add \"CLOCK:\" line to the task under point in the current org-mode file."
(interactive (interactive
@ -1081,26 +1097,20 @@ ending at 23:61. When not given, FILES defaults to
(save-mark-and-excursion (save-mark-and-excursion
(org-clock-find-position nil) (org-clock-find-position nil)
(open-line 1) (open-line 1)
(indent-according-to-mode) (db/org-insert-clockline starting-time ending-time))))
(insert "CLOCK: ")
(org-insert-time-stamp starting-time t t)
(insert "--")
(org-insert-time-stamp ending-time t t)
(org-clock-update-time-maybe))))
(bind-key "C-c C-x C-a" #'db/org-add-clocking-time org-mode-map) (bind-key "C-c C-x C-a" #'db/org-add-clocking-time org-mode-map)
(defun db/org-add-clock-line-to-file (id start end) (defun db/org-add-clock-line-to-file (id start end)
"Add clock line with START and END time to task identified by ID. "Add clock line with START and END time to task identified by ID.
TODO: specify time format" START and END must be given as org-mode timestamp strings."
(let ((location (org-id-find id t))) (let ((location (org-id-find id t)))
(when (null location) (when (null location)
(user-error "ID %s cannot be found" id)) (user-error "ID %s cannot be found" id))
;; Update existing clock lines ;; Update existing clock lines
(let ((new-start (org-time-string-to-seconds start)) (let ((new-start (org-time-string-to-seconds start))
(new-end (org-time-string-to-seconds end)) (new-end (org-time-string-to-seconds end)))
(timestamp-format (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]"))) (with-current-buffer (marker-buffer location)
(org-with-point-at location
(db/org-map-clock-lines-and-entries (db/org-map-clock-lines-and-entries
(lambda (timestamp-1 timestamp-2) (lambda (timestamp-1 timestamp-2)
(let ((current-start (float-time (let ((current-start (float-time
@ -1108,7 +1118,9 @@ TODO: specify time format"
(org-parse-time-string timestamp-1)))) (org-parse-time-string timestamp-1))))
(current-end (float-time (current-end (float-time
(apply #'encode-time (apply #'encode-time
(org-parse-time-string timestamp-2))))) (org-parse-time-string timestamp-2))))
(kill-whole-line nil) ; dont delete newlines if not asked to
)
(cond (cond
;; if the current clock line is completely contained within the ;; if the current clock line is completely contained within the
;; given period, delete it ;; given period, delete it
@ -1116,50 +1128,27 @@ TODO: specify time format"
(kill-whole-line)) (kill-whole-line))
;; if the current clock line completely contains the given one, ;; if the current clock line completely contains the given one,
;; split it ;; split it
((and (<= current-start new-start) ((and (<= current-start new-start new-end current-end))
(<= new-end current-end))
(beginning-of-line) (beginning-of-line)
(let ((kill-whole-line nil)) (kill-line)
(kill-line)) (db/org-insert-clockline current-start new-start)
(indent-according-to-mode)
(insert "CLOCK: ")
(insert (format-time-string timestamp-format current-start))
(insert "--")
(insert (format-time-string timestamp-format new-start))
(org-clock-update-time-maybe)
(beginning-of-line) (beginning-of-line)
(open-line 1) (open-line 1)
(indent-according-to-mode) (db/org-insert-clockline new-end current-end))
(insert "CLOCK: ")
(insert (format-time-string timestamp-format new-end))
(insert "--")
(insert (format-time-string timestamp-format current-end))
(org-clock-update-time-maybe))
;; New interval overlaps beginning of current line ;; New interval overlaps beginning of current line
((<= new-start current-start new-end current-end) ((<= new-start current-start new-end current-end)
(beginning-of-line) (beginning-of-line)
(let ((kill-whole-line nil)) (kill-line)
(kill-line)) (db/org-insert-clockline new-end current-end))
(indent-according-to-mode)
(insert "CLOCK: ")
(insert (format-time-string timestamp-format new-end))
(insert "--")
(insert (format-time-string timestamp-format current-end))
(org-clock-update-time-maybe))
;; New interval overlaps at end of current line ;; New interval overlaps at end of current line
((<= current-start new-start current-end new-end) ((<= current-start new-start current-end new-end)
(beginning-of-line) (beginning-of-line)
(let ((kill-whole-line nil)) (kill-line)
(kill-line)) (db/org-insert-clockline current-start new-start)))))
(indent-according-to-mode)
(insert "CLOCK: ") ;; keep headline as they are, i.e., do nothing
(insert (format-time-string timestamp-format current-start)) (lambda ()))))
(insert "--")
(insert (format-time-string timestamp-format new-start))
(org-clock-update-time-maybe)))))
(lambda ()
;; keep headline as they are, i.e., do nothing
))))
;; Finally add the new clock line ;; Finally add the new clock line
(org-with-point-at location (org-with-point-at location
(db/org-add-clocking-time (apply #'encode-time (org-parse-time-string start)) (db/org-add-clocking-time (apply #'encode-time (org-parse-time-string start))