Skip to content

Instantly share code, notes, and snippets.

@quodlibetor
Created April 19, 2012 18:44
Show Gist options
  • Save quodlibetor/2422928 to your computer and use it in GitHub Desktop.
Save quodlibetor/2422928 to your computer and use it in GitHub Desktop.
a scratch of a way to find the most-recent clocked in time in org-mode
(defun bwm/org-find-recent-clocks (buf)
"get the most recent clock-in or out time and position
returns a list that looks like (time position)"
(let (recent
this-un
match)
(with-current-buffer buf
(save-excursion
(goto-char (point-min))
(while (re-search-forward
"CLOCK: \\(\\[.*?\\]\\)--\\(\\[.*?\\]\\)" nil t)
;; try to compare clock-out times, but accept clock-in times if
;; there wasn't a clock-out
(if (match-string 2)
(setq match (match-string 2))
(setq match (match-string 1)))
(if recent
(progn
(when (string< (car recent) match)
(setq recent (list match (point)))))
(setq recent (list match (point)))))))
recent))
(defun bwm/org-goto-recent-clock-current-buffer ()
"Move point to the most recent clock-out or in time
Most of the logic is in `org-find-recent-clocks'."
(interactive)
(let ((marker (cadr (org-find-recent-clocks (current-buffer)))))
(push-mark)
(goto-char marker)
(show-subtree)
(hide-other)
(recenter)))
(defun bwm/org-goto-recent-clock-all-buffers ()
"Move point to the most recent clock-out or in time in any open buffer
Most of the logic is in `org-find-recent-clocks'."
(interactive)
(let (this-buf-result
most-recent-time)
(mapc
(lambda (b)
(when (with-current-buffer b (eq major-mode 'org-mode))
(setq this-buf-result (org-find-recent-clocks b))
(dbg this-buf-result)
(if (not most-recent-time)
(setq most-recent-time (append this-buf-result (list b)))
(when (and
(listp this-buf-result)
(car this-buf-result)
(string< (car most-recent-time) (car this-buf-result)))
(setq most-recent-time (append this-buf-result (list b)))))))
(buffer-list))
(switch-to-buffer (caddr most-recent-time))
(push-mark)
(goto-char (cadr most-recent-time))
(show-subtree)
(hide-other)
(recenter)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment