Bernt, thanks for your email. It helped solve the problem... I thought
my reply would not get routed to the mailing list. He is my problem
and solution, for all to see
I used Sacha's code to automatically clock in and out when the TODO state
swapped between STARTED and WAITING.
I then added code to note if my remember template had the TODO state of
STARTED. If so, I set it up to clock in that task on insertion (automatically
clocking out my old task)
My problem was the old task still had the STARTED state.
;; org-mode automatic clocking on TODO
;; ------------------------------------------------------------------
(defun sacha/org-clock-in-if-starting ()
"Clock in when the task is marked STARTED."
(when (and (string= state "STARTED")
(not (string= last-state state)))
(org-clock-in)))
(add-hook 'org-after-todo-state-change-hook
'sacha/org-clock-in-if-starting)
(defadvice org-clock-in (after sacha activate)
"Set this task's status to 'STARTED'."
(org-todo "STARTED"))
(defun sacha/org-clock-out-if-waiting ()
"Clock in when the task is marked STARTED."
(when (and (string= state "WAITING")
(not (string= last-state state)))
(org-clock-out)))
(add-hook 'org-after-todo-state-change-hook
'sacha/org-clock-out-if-waiting)
;; start the clock if there is a STARTED todo tag in template
;--------------------------------------------------------------------
(add-hook 'org-remember-before-finalize-hook 'my-start-clock-if-needed)
(defun my-start-clock-if-needed ()
(save-excursion
(goto-char (point-min))
(when (re-search-forward "* STARTED" nil t)
(change-todo-state-on-old-clock)
(org-clock-in))))
; change the state of the old clock
;---------------------------------------------------------------------------
(defun change-todo-state-on-old-clock ()
; old-clock needs state changed if STARTED
(save-excursion
(progn
(if (not (marker-buffer org-clock-marker))
(if select
(error "No task selected")
(error "No active clock")))
(set-buffer (marker-buffer org-clock-marker))
(goto-char (point-min))
(when (re-search-forward "^\** STARTED" nil t)
(org-todo "WAITING")))))
; example template
;----------------------------------------------------------------------------
(setq org-remember-templates
'(("Task" ?t "* %^{Task status|TODO|STARTED|SUBTASK|DONE} %^{Brief
Description} %^G\n %^{subject}p %^{other-subjects}p %^{sub-subjects}p
%^{keywords}p %?\n Added: %U \n" "~/notes/notes-log-090410.org" "Task")