[Org] Change API of function to add clock lines to task

Instead of resolving an ID, this function now gets a marker designating the task
where the provided clock line should go to.
This commit is contained in:
Daniel - 2018-01-21 16:09:27 +01:00
parent fa0fb98291
commit 74a26b4d5a
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
1 changed files with 47 additions and 48 deletions

View File

@ -1104,58 +1104,57 @@ ending at 23:61. When not given, FILES defaults to
(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-marker (target-marker 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 TARGET-MARKER.
START and END must be given as time objects as returned by START and END must be given as time objects as returned by
`encode-time, or as an integer or float denoting seconds since `encode-time, or as an integer or float denoting seconds since
1970-01-01." 1970-01-01. TARGET-MARKER must be positioned on the task where
(let ((location (org-id-find id t))) the clock line is to be added to."
(when (null location) (when (not (markerp target-marker))
(user-error "ID %s cannot be found" id)) (user-error "Marker not valid."))
;; Update existing clock lines (let ((new-start (float-time start))
(let ((new-start (float-time start)) (new-end (float-time end)))
(new-end (float-time end))) (with-current-buffer (marker-buffer target-marker)
(with-current-buffer (marker-buffer 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 (apply #'encode-time
(apply #'encode-time (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
(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 ((and (<= new-start current-start current-end new-end))
((and (<= new-start current-start current-end new-end)) (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 new-end current-end))
((and (<= current-start new-start new-end current-end)) (beginning-of-line)
(beginning-of-line) (kill-line)
(kill-line) (db/org-insert-clockline current-start new-start)
(db/org-insert-clockline current-start new-start) (open-line 1)
(open-line 1) (db/org-insert-clockline new-end current-end))
(db/org-insert-clockline new-end current-end)) ;; 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) (kill-line)
(kill-line) (db/org-insert-clockline new-end current-end))
(db/org-insert-clockline new-end current-end)) ;; 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) (kill-line)
(kill-line) (db/org-insert-clockline current-start new-start)))))
(db/org-insert-clockline current-start new-start)))))
;; Keep headline as they are, i.e., do nothing ;; Keep headline as they are, i.e., do nothing
(lambda ()))) (lambda ())))
;; Finally add the new clock line ;; Finally add the new clock line
(org-with-point-at location (org-with-point-at target-marker
(db/org-add-clocking-time new-start new-end))))) (db/org-add-clocking-time new-start new-end))))
;;; End ;;; End