Awesome, it works! Thank you for the quick reply and fix.
On Fri, Jul 29, 2016 at 1:03 AM Joe Schafer wrote:
>
> I have a function to save the currently clocked item into a file so I
> can display it in my terminal. The hook seems to interfere with
> org-capture. If I capture something the following
> happens:
>
> 1. org-capture dialog appears
> 2. Fill-in capture. The capture is clocked-in.
> 3. Finish capture with C-c C-c
> 4. The capture is still clocked in. The clock should go back to the
> previous clocked-in entry.
>
> Trying to bisect the issue was difficult. Sometimes the new capture would
> clock-out, but the original entry would not clock back in. Other times
> the clock would be left running in the new capture entry.
>
> The smallest example I could get is:
>
> (defun my:org-get-clocked-in-headline ()
> "Get the headline of the currently clocked in headline.
> If no headline is clocked in, then return an empty string."
> (interactive)
> (with-current-buffer (marker-buffer org-clock-marker)
> (goto-char org-clock-marker)))
>
> (defun my:org-save-clocked-in-entry-to-file ()
> "Save currently clocked-in task to a file."
> (with-temp-buffer
> (insert (my:org-get-clocked-in-headline
>
> (add-hook 'org-clock-in-hook #'my:org-save-clocked-in-entry-to-file)
>
> For reference, here's the full code I'm using
>
> (defun my:org-get-clocked-in-headline ()
> "Get the headline of the currently clocked in headline.
> If no headline is clocked in, then return an empty string."
> (interactive)
> (if (not (org-clocking-p))
> ""
> (with-current-buffer (marker-buffer org-clock-marker)
> (save-excursion
> (save-restriction
> (when (or (< org-clock-marker (point-min)) (>
> org-clock-marker (point-max)))
> (widen))
> (goto-char org-clock-marker)
> (org-no-properties (org-get-heading 'no-tags 'no-todo)))
>
> (defvar my:org-clocked-in-file-path
> "/tmp/org-currently-clocked-in-task"
> "Where to save the currently clocked in task for all to see.")
>
> (defun my:org-save-clocked-in-entry-to-file ()
> "Save currently clocked-in task to a file."
> (let ((last-message (current-message))
> ;; Suppress echo area to see clock out information. Doesn't
> seem to
> ;; work, so we'll just re-display last-message.
> (inhibit-message nil))
> (with-temp-buffer
> (insert (my:org-get-clocked-in-headline))
> (write-region (point-min) (point-max)
> my:org-clocked-in-file-path))
> (message last-message)))
>
> (add-hook 'org-clock-in-hook #'my:org-save-clocked-in-entry-to-file)
> (add-hook 'org-clock-out-hook #'my:org-save-clocked-in-entry-to-file)
> (add-hook 'org-clock-cancel-hook
> #'my:org-save-clocked-in-entry-to-file)
>
>
> Emacs : GNU Emacs 25.1.50.2 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9)
> of 2016-07-14
> Package: Org-mode version 8.3.4 (8.3.4-93-g0d72c3-elpaplus @
> /home/joe/.emacs.d/elpa/org-20160627/)
>
> current state:
> ==
> (setq
> org-id-locations-file "/home/joe/.emacs.d/.cache/.org-id-locations"
> org-tab-first-hook '(org-hide-block-toggle-maybe
> org-babel-hide-result-toggle-maybe org-babel-header-arg-expand)
> org-drill-sm5-initial-interval 1.5
> org-clock-persist-file "/home/joe/.emacs.d/.cache/org-clock-save.el"
> org-habit-preceding-days 10
> org-speed-command-hook '(org-speed-command-default-hook
> org-babel-speed-command-hook)
> org-reverse-note-order t
> org-clock-history-length 23
> org-occur-hook '(org-first-headline-recenter)
> org-metaup-hook '(org-babel-load-in-session-maybe)
> org-html-format-drawer-function '(lambda (name contents) contents)
> org-capture-after-finalize-hook '(org-save-all-org-buffers)
> org-log-done t
> org-latex-format-inlinetask-function
> 'org-latex-format-inlinetask-default-function
> org-confirm-shell-link-function 'yes-or-no-p
> org-ascii-format-inlinetask-function 'org-ascii-format-inlinetask-default
> org-link-translation-function 'toc-org-unhrefify
> org-present-mode-hook '(spacemacs//org-present-start)
> org-agenda-restore-windows-after-quit t
> org-latex-format-headline-function
> 'org-latex-format-headline-default-function
> org-default-notes-file "~/gdrive/org/refile.org"
> org-clock-in-resume t
> org-clock-cancel-hook '(my:org-save-clocked-in-entry-to-file)
> org-capture-templates '(("t" "todo" entry (file "~/gdrive/org/refile.org")
> "* TODO %?\n%U\n%(my:org-pick-smart-context \"%x\")\n"
> :clock-in t :clock-resume t)
> ("r" "respond" entry (file "~/gdrive/org/
> refile.org")
> "* NEXT Respond to %:from on
> %:subject\nSCHEDULED: %t\n%U\n%a\n" :clock-in t :clock-resume t
> :immediate-finish t)
> ("n"