Last active
September 24, 2019 19:14
-
-
Save heikkil/76e8fbae3fbf6599870e6c0ca1f3c62b to your computer and use it in GitHub Desktop.
Merge the org CLOCK line with the next CLOCK line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
(progn | |
(forward-line) | |
(user-error "The first line must have a valid CLOCK range")))) | |
(first-line-t2 (match-string 9 first-line)) | |
(second-line (progn | |
(forward-line) | |
(buffer-substring | |
(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 | |
(org-cycle) | |
;; insert new time range | |
(insert (concat "CLOCK: [" second-line-t1 "]--[" first-line-t2 "]")) | |
;; generate duration | |
(org-ctrl-c-ctrl-c))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I missed this change and added code of my own. The main benefit is that I call this function automatically after clocking out, so I don't need to move point to the last clock-out line.