Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Merge the org CLOCK line with the next CLOCK line
(defun org-clock-merge (arg)
"Merge the org CLOCK line with the next CLOCK line.
Requires that the time ranges in two lines overlap, i.e. the
start time of the first line and the second time of the second
line are identical.
If the testing fails, move the cursor one line down.
Universal argument ARG overrides the test and merges
the lines even if the ranges do not overlap."
(interactive "P")
(let* ((org-clock-regexp (concat "CLOCK: " org-ts-regexp3 "--" org-ts-regexp3))
(first-line-start (line-beginning-position))
(first-line (buffer-substring
(line-beginning-position) (line-end-position)))
(first-line-t1 (if (string-match org-clock-regexp first-line)
(match-string 1 first-line)
(user-error "The first line must have a valid CLOCK range"))))
(first-line-t2 (match-string 9 first-line))
(second-line (progn
(line-beginning-position) (line-end-position))))
(second-line-t1 (if (string-match org-clock-regexp second-line)
(match-string 1 second-line)
(user-error "The second line must have a valid CLOCK range")))
(second-line-t2 (match-string 9 second-line)))
;; check if lines should be merged
(unless (or arg (equal first-line-t1 second-line-t2))
(user-error "Clock ranges not continuous. Override with universal argument"))
;; remove the two lines
(delete-region first-line-start (line-end-position))
;; indent
;; insert new time range
(insert (concat "CLOCK: [" second-line-t1 "]--[" first-line-t2 "]"))
;; generate duration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.