Awesome, it works!  Thank you for the quick reply and fix.

On Fri, Jul 29, 2016 at 1:03 AM Joe Schafer <joesmo...@gmail.com> 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" "note" entry (file "~/gdrive/org/refile.org")
> "* %? :NOTE:\n%U\n%a\n" :clock-in t :clock-resume t)
>                          ("j" "Journal" entry (file+datetree "~/gdrive/org/
> journal.org") "* %?\n%U\n" :clock-in t :clock-resume t)
>                          ("w" "org-protocol" entry (file "~/gdrive/org/
> refile.org") "* TODO Review %c\n%U\n" :immediate-finish t)
>                          ("m" "Meeting" entry (file "~/gdrive/org/
> refile.org") "* MEETING with %? :MEETING:\n%U" :clock-in t :clock-resume
>                           t)
>                          ("p" "Phone call" entry (file "~/gdrive/org/
> refile.org") "* PHONE %? :PHONE:\n%U" :clock-in t :clock-resume t)
>                          ("h" "Habit" entry (file "~/gdrive/org/refile.org
> ")
>                           "* NEXT %?\n%U\n%a\nSCHEDULED:
> %(format-time-string \"%<<%Y-%m-%d %a .+1d/3d>>\")\n:PROPERTIES:\n:STYLE:
> habit\n:REPEAT_TO_STATE: NEXT\n:END:\n")
>                          )
>  org-startup-indented t
>  org-after-todo-state-change-hook '(org-clock-out-if-current)
>  org-latex-format-drawer-function '(lambda (name contents) contents)
>  org-drill-learn-fraction 0.3
>  org-from-is-user-regexp "\\<joe\\>"
>  org-src-mode-hook '(org-src-babel-configure-edit-buffer
> org-src-mode-configure-edit-buffer)
>  org-agenda-before-write-hook '(org-agenda-add-entry-text)
>  org-babel-pre-tangle-hook '(save-buffer)
>  org-font-lock-set-keywords-hook '(org-drill-add-cloze-fontification)
>  org-mode-hook '(spacemacs/load-yasnippet toc-org-enable org-bullets-mode
> org-autolist-mode org-clock-load
>                  #[nil "\300\301\302\303\304$\207" [org-add-hook
> change-major-mode-hook org-show-block-all append local] 5]
>                  #[nil "\300\301\302\303\304$\207" [org-add-hook
> change-major-mode-hook org-babel-show-result-all append local] 5]
>                  org-babel-result-hide-spec org-babel-hide-all-hashes
> spacemacs/add-org-surrounds evil-org-mode org-eldoc-load
>                  spacemacs//init-company-org-mode company-mode)
>  org-refile-targets '((nil :maxlevel . 9) (org-agenda-files :maxlevel . 9))
>  org-outline-path-complete-in-steps nil
>  org-todo-state-tags-triggers '(("CANCELLED" ("cancelled" . t)) ("WAITING"
> ("waiting" . t)) ("HOLD" ("waiting") ("hold" . t))
>                                 (done ("waiting") ("hold")) ("TODO"
> ("waiting") ("cancelled") ("hold"))
>                                 ("NEXT" ("waiting") ("cancelled")
> ("hold")) ("DONE" ("waiting") ("cancelled") ("hold")))
>  org-stuck-projects '("" nil nil "")
>  org-archive-hook '(org-attach-archive-delete-maybe)
>  org-agenda-compact-blocks t
>  org-ascii-format-drawer-function '(lambda (name contents width) contents)
>  org-clock-persist t
>  org-ctrl-c-ctrl-c-hook '(org-babel-hash-at-point
> org-babel-execute-safely-maybe)
>  org-refile-use-outline-path t
>  org-clock-in-switch-to-state 'bh/clock-in-to-next
>  org-directory "~/gdrive/org"
>  org-cycle-hook '(org-cycle-hide-archived-subtrees org-cycle-hide-drawers
> org-cycle-show-empty-lines
>                   org-optimize-window-after-visibility-change)
>  org-present-mode-quit-hook '(spacemacs//org-present-end)
>  org-export-async-init-file
> "/home/joe/.emacs.d/layers/org/org-async-init.el"
>  org-clock-persist-query-resume nil
>  org-todo-keywords '((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
> (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c)"))
>  org-modules '(org-bbdb org-bibtex org-docview org-gnus org-habit org-info
> org-irc org-mhe org-rmail org-w3m org-drill)
>  org-clock-report-include-clocking-task t
>  org-download-image-dir "~/gdrive/org/images"
>  org-confirm-elisp-link-function 'yes-or-no-p
>  org-startup-with-inline-images t
>  org-edit-src-content-indentation 0
>  org-metadown-hook '(org-babel-pop-to-session-maybe)
>  org-treat-S-cursor-todo-selection-as-state-change nil
>  org-html-format-headline-function
> 'org-html-format-headline-default-function
>  org-clock-in-hook '(my:org-save-clocked-in-entry-to-file)
>  org-html-format-inlinetask-function
> 'org-html-format-inlinetask-default-function
>  org-agenda-mode-hook '(org-save-all-org-buffers)
>  org-fontify-whole-heading-line t
>  org-agenda-files '("~/gdrive/org/")
>  org-clock-out-hook '(org-clock-remove-empty-clock-drawer
> bh/clock-out-maybe my:remove-empty-drawer-on-clock-out
>                       my:org-save-clocked-in-entry-to-file)
>  org-drill-add-random-noise-to-intervals-p t
>  org-clock-out-remove-zero-time-clocks t
>  org-publish-project-alist '(("swift-plaques" :author "Joe Schafer"
> :base-directory "~/prog/swift-plaques-business-plan"
>                               :publishing-directory
> "~/prog/swift-plaques-business-plan" :publishing-function
> org-latex-publish-to-pdf
>                               :base-extension "org")
>                              )
>  org-tag-alist '((:startgroup) ("work" . 119) ("home" . 104) ("comp" . 99)
> ("errand" . 101) (:endgroup) (:startgroup) ("start" . 115)
>                  ("mid" . 109) ("end" . 110) (:endgroup) (:startgroup)
> ("daily" . 100) ("weekly" . 107) (:endgroup) (:startgroup)
>                  ("waiting" . 116) ("hold" . 104) ("cancelled" . 99)
> (:endgroup))
>  )
>
-- 
Joe Schafer

Reply via email to