2

In org-mode, C-c C-x C-j executes org-clock-goto, jumping the cursor to the currently active or most recently used clock.

After using this, I almost always want to go back to the point where I was, often another buffer. I would expect running org-clock-goto a second time would get me there, toggling back to where I started. But unfortunately, that doesn't work. org-clock-goto does only what the name says.

In search of a solution, I came up with a pair of simple elisp functions:

(defun my/org-clock-goto ()
  (interactive)
  (bookmark-set "before goto clock")
  (org-clock-goto))

(defun my/org-clock-return ()
  (interactive)
  (bookmark-jump "before goto clock"))

This only works if I am starting off in a buffer which is a file (not *scratch* for instance). But more importantly, what I really want is a single function bound to C-c C-x C-j that will let me jump between the original point and the last clock.

Any hints?

  • 2
    Not sure if this is exactly what you want: (defadvice org-clock-goto (around bp/org-clock-goto--around) "Push mark to global-mark-ring for navigation purposes." (if (eq last-command 'org-clock-goto) (pop-global-mark) (push-mark (point)) ad-do-it)) (ad-activate 'org-clock-goto) Pushes a mark to the gobal-mark-ring when using org-clock-goto. Using org-clock-goto immediately afterwards will jump back to the starting position. When not just jumping back and forth, use pop-global-mark (C-x C-SPC) to go back. – mutbuerger Mar 25 '15 at 11:04
  • Getting warmer... If the point is not already at the most recent clock, then org-clock-goto should (push-mark (point)) first before going there. But otherwise (which is to say, if the mark is already at the active or most recent clock) then org-clock-goto should (pop-global-mark). If you can incorporate those conditions into this defadvice, then I expect it will work very nicely. – Brian Z Mar 26 '15 at 2:55
2

This advised org-clock-goto will push-mark to the global-mark-ring whenever you are not at the currently clocked item. Otherwise it will jump back to the starting position with pop-global-mark. (NOTE: org-clock-goto will no longer work correctly without an open clock.)

(defadvice org-clock-goto (around org-clock-goto--around)
  "If not at clocked item already, push mark to `global-mark-ring'."
  (if (and (org-clocking-p)
           (< (save-excursion (outline-back-to-heading) (point))
              org-clock-marker)
           (> (save-excursion (outline-next-heading) (point))
              org-clock-marker))
      (pop-global-mark)
    (push-mark (point)) ad-do-it))
(ad-activate 'org-clock-goto)
  • I accepted this because it works nicely if there is an active clock running. But with no active clock running, org-clock-goto is completely broken by this. I spent quite a bit of time trying to hack it into shape, but had no luck :( – Brian Z Mar 27 '15 at 6:19
  • What do you mean by "completely broken"? Besides pushing a mark it's default behavior (jumps to the most recently clocked item) for me. – mutbuerger Mar 27 '15 at 12:10
  • If I play around with this, moving around to different places, clocking in and out and trying to toggle between the clock and a mark, it frequently just behaves as if the defadvice were not in effect. But sometimes (I can't tell you how to reproduce it reliably, but this is the really broken part) the point jumps unexpectedly to another mark in the ring, before the most recent one. If it matters I'm on Emacs 24.3.1 and Org-mode 8.2.10. – Brian Z Mar 28 '15 at 8:44
  • You probably push some more marks to the ring before using org-clock-goto again. In that case, using (point-to-register 1) and (register-to-point 1) instead of push-mark and pop-global-mark would be the way to go. – mutbuerger Mar 28 '15 at 10:19
  • I should replace the line pop-global-mark with (point-to-register 1) and the following line, (push-mark (point)) ad-do-it)) with (register-to-point 1) ad-do-it))? I tried this just now ,but the point would not return to where I started from. – Brian Z Apr 5 '15 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.