emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Add org-clock-clocking-in for hooks
@ 2010-05-20 15:14 Bernt Hansen
  2010-05-20 15:43 ` Bernt Hansen
  0 siblings, 1 reply; 4+ messages in thread
From: Bernt Hansen @ 2010-05-20 15:14 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Bernt Hansen

org-clock-out-hook can now query org-clock-clocking-in to see if org-clock-out is
being called inside org-clock-in.  This allows the hook to selectively clock
in another task without leaving clocks open.
---
I've radically changed my clocking setup and needed the following patch to make it work.
This basically just wraps the entire org-clock-in function in a let statement that
sets org-clock-clocking-in to true for the duration of the org-clock-in call.

I'm testing a new org-clock-out-hook which automatically clocks in the default task when
clocking time in a context.

After things settle I'll be updating my http://doc.norang.ca/org-mode.html setup 
information to include these changes.

This patch is available at git://git.norang.ca/org-mode.git persistent-clocking


 lisp/org-clock.el |  311 +++++++++++++++++++++++++++--------------------------
 1 files changed, 157 insertions(+), 154 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 68a40ce..e24300d 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -911,6 +911,8 @@ so long."
 			 60.0))))
 	   org-clock-user-idle-start)))))
 
+(setq org-clock-clocking-in nil)
+
 (defun org-clock-in (&optional select start-time)
   "Start the clock on the current item.
 If necessary, clock-out of the currently active clock.
@@ -919,31 +921,32 @@ clock into.  When SELECT is `C-u C-u', clock into the current task and mark
 is as the default task, a special task that will always be offered in
 the clocking selection, associated with the letter `d'."
   (interactive "P")
-  (setq org-clock-notification-was-shown nil)
-  (catch 'abort
-    (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness)
-			     (org-clocking-p)))
-	  ts selected-task target-pos (msg-extra "")
-	  (leftover (and (not org-clock-resolving-clocks)
-			  org-clock-leftover-time)))
-      (when (and org-clock-auto-clock-resolution
-		 (or (not interrupting)
-		     (eq t org-clock-auto-clock-resolution))
-		 (not org-clock-clocking-in)
-		 (not org-clock-resolving-clocks))
-	(setq org-clock-leftover-time nil)
-	(let ((org-clock-clocking-in t))
-	  (org-resolve-clocks)))	; check if any clocks are dangling
-      (when (equal select '(4))
-	(setq selected-task (org-clock-select-task "Clock-in on task: "))
-	(if selected-task
-	    (setq selected-task (copy-marker selected-task))
-	  (error "Abort")))
-      (when interrupting
-	;; We are interrupting the clocking of a different task.
-	;; Save a marker to this task, so that we can go back.
-	;; First check if we are trying to clock into the same task!
-	(if (save-excursion
+  (let ((org-clock-clocking-in t))
+    (setq org-clock-notification-was-shown nil)
+    (catch 'abort
+      (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness)
+			       (org-clocking-p)))
+	    ts selected-task target-pos (msg-extra "")
+	    (leftover (and (not org-clock-resolving-clocks)
+			   org-clock-leftover-time)))
+	(when (and org-clock-auto-clock-resolution
+		   (or (not interrupting)
+		       (eq t org-clock-auto-clock-resolution))
+		   (not org-clock-clocking-in)
+		   (not org-clock-resolving-clocks))
+	  (setq org-clock-leftover-time nil)
+	  (let ((org-clock-clocking-in t))
+	    (org-resolve-clocks)))	; check if any clocks are dangling
+	(when (equal select '(4))
+	  (setq selected-task (org-clock-select-task "Clock-in on task: "))
+	  (if selected-task
+	      (setq selected-task (copy-marker selected-task))
+	    (error "Abort")))
+	(when interrupting
+	  ;; We are interrupting the clocking of a different task.
+	  ;; Save a marker to this task, so that we can go back.
+	  ;; First check if we are trying to clock into the same task!
+	  (if (save-excursion
 		(unless selected-task
 		  (org-back-to-heading t))
 		(and (equal (marker-buffer org-clock-hd-marker)
@@ -954,136 +957,136 @@ the clocking selection, associated with the letter `d'."
 			(if selected-task
 			    (marker-position selected-task)
 			  (point)))))
-	    (message "Clock continues in \"%s\"" org-clock-heading)
-	  (progn
-	    (move-marker org-clock-interrupted-task
-			 (marker-position org-clock-marker)
-			 (org-clocking-buffer))
-	    (org-clock-out t))))
-
-      (when (equal select '(16))
-	;; Mark as default clocking task
-	(org-clock-mark-default-task))
-
-      ;; Clock in at which position?
-      (setq target-pos
-	    (if (and (eobp) (not (org-on-heading-p)))
-		(point-at-bol 0)
-	      (point)))
-      (run-hooks 'org-clock-in-prepare-hook)
-      (save-excursion
-	(when (and selected-task (marker-buffer selected-task))
-	  ;; There is a selected task, move to the correct buffer
-	  ;; and set the new target position.
-	  (set-buffer (org-base-buffer (marker-buffer selected-task)))
-	  (setq target-pos (marker-position selected-task))
-	  (move-marker selected-task nil))
+	      (message "Clock continues in \"%s\"" org-clock-heading)
+	    (progn
+	      (move-marker org-clock-interrupted-task
+			   (marker-position org-clock-marker)
+			   (org-clocking-buffer))
+	      (org-clock-out t))))
+
+	(when (equal select '(16))
+	  ;; Mark as default clocking task
+	  (org-clock-mark-default-task))
+
+	;; Clock in at which position?
+	(setq target-pos
+	      (if (and (eobp) (not (org-on-heading-p)))
+		  (point-at-bol 0)
+		(point)))
+	(run-hooks 'org-clock-in-prepare-hook)
 	(save-excursion
-	  (save-restriction
-	    (widen)
-	    (goto-char target-pos)
-	    (org-back-to-heading t)
-	    (or interrupting (move-marker org-clock-interrupted-task nil))
-	    (org-clock-history-push)
-	    (org-clock-set-current)
-	    (cond ((functionp org-clock-in-switch-to-state)
-		   (looking-at org-complex-heading-regexp)
-		   (let ((newstate (funcall org-clock-in-switch-to-state
-					    (match-string 2))))
-		     (if newstate (org-todo newstate))))
-		  ((and org-clock-in-switch-to-state
-			(not (looking-at (concat outline-regexp "[ \t]*"
-						 org-clock-in-switch-to-state
-						 "\\>"))))
-		   (org-todo org-clock-in-switch-to-state)))
-	    (setq org-clock-heading-for-remember
-		  (and (looking-at org-complex-heading-regexp)
-		       (match-end 4)
-		       (org-trim (buffer-substring (match-end 1)
-						   (match-end 4)))))
-	    (setq org-clock-heading
-		  (cond ((and org-clock-heading-function
-			      (functionp org-clock-heading-function))
-			 (funcall org-clock-heading-function))
-			((looking-at org-complex-heading-regexp)
-			 (replace-regexp-in-string
-			  "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
-			  (match-string 4)))
-			(t "???")))
-	    (setq org-clock-heading (org-propertize org-clock-heading
-						    'face nil))
-	    (org-clock-find-position org-clock-in-resume)
-	    (cond
-	     ((and org-clock-in-resume
-		   (looking-at
-		    (concat "^[ \t]* " org-clock-string
-			    " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
-			    " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
-	      (message "Matched %s" (match-string 1))
-	      (setq ts (concat "[" (match-string 1) "]"))
-	      (goto-char (match-end 1))
-	      (setq org-clock-start-time
-		    (apply 'encode-time
-			   (org-parse-time-string (match-string 1))))
-	      (setq org-clock-effort (org-get-effort))
-	      (setq org-clock-total-time (org-clock-sum-current-item
-					  (org-clock-get-sum-start))))
-	     ((eq org-clock-in-resume 'auto-restart)
-	      ;; called from org-clock-load during startup,
-	      ;; do not interrupt, but warn!
-	      (message "Cannot restart clock because task does not contain unfinished clock")
-	      (ding)
-	      (sit-for 2)
-	      (throw 'abort nil))
-	     (t
-	      (insert-before-markers "\n")
-	      (backward-char 1)
-	      (org-indent-line-function)
-	      (when (and (save-excursion
-			   (end-of-line 0)
-			   (org-in-item-p)))
-		(beginning-of-line 1)
-		(org-indent-line-to (- (org-get-indentation) 2)))
-	      (insert org-clock-string " ")
-	      (setq org-clock-effort (org-get-effort))
-	      (setq org-clock-total-time (org-clock-sum-current-item
-					  (org-clock-get-sum-start)))
-	      (setq org-clock-start-time
-		    (or (and leftover
-			     (y-or-n-p
-			      (format
-			       "You stopped another clock %d mins ago; start this one from then? "
-			       (/ (- (org-float-time (current-time))
-				     (org-float-time leftover)) 60)))
-			     leftover)
-			start-time
-			(current-time)))
-	      (setq ts (org-insert-time-stamp org-clock-start-time
-					      'with-hm 'inactive))))
-	    (move-marker org-clock-marker (point) (buffer-base-buffer))
-	    (move-marker org-clock-hd-marker
-			 (save-excursion (org-back-to-heading t) (point))
-			 (buffer-base-buffer))
-	    (setq org-clock-has-been-used t)
-	    (or global-mode-string (setq global-mode-string '("")))
-	    (or (memq 'org-mode-line-string global-mode-string)
-		(setq global-mode-string
-		      (append global-mode-string '(org-mode-line-string))))
-	    (org-clock-update-mode-line)
-	    (when org-clock-mode-line-timer
-	      (cancel-timer org-clock-mode-line-timer)
-	      (setq org-clock-mode-line-timer nil))
-	    (setq org-clock-mode-line-timer
-		  (run-with-timer org-clock-update-period
-				  org-clock-update-period
-				  'org-clock-update-mode-line))
-	    (when org-clock-idle-timer
-	      (cancel-timer org-clock-idle-timer)
-	      (setq org-clock-idle-timer nil))
-	    (setq org-clock-idle-timer
-		  (run-with-timer 60 60 'org-resolve-clocks-if-idle))
-	    (message "Clock starts at %s - %s" ts msg-extra)
-	    (run-hooks 'org-clock-in-hook)))))))
+	  (when (and selected-task (marker-buffer selected-task))
+	    ;; There is a selected task, move to the correct buffer
+	    ;; and set the new target position.
+	    (set-buffer (org-base-buffer (marker-buffer selected-task)))
+	    (setq target-pos (marker-position selected-task))
+	    (move-marker selected-task nil))
+	  (save-excursion
+	    (save-restriction
+	      (widen)
+	      (goto-char target-pos)
+	      (org-back-to-heading t)
+	      (or interrupting (move-marker org-clock-interrupted-task nil))
+	      (org-clock-history-push)
+	      (org-clock-set-current)
+	      (cond ((functionp org-clock-in-switch-to-state)
+		     (looking-at org-complex-heading-regexp)
+		     (let ((newstate (funcall org-clock-in-switch-to-state
+					      (match-string 2))))
+		       (if newstate (org-todo newstate))))
+		    ((and org-clock-in-switch-to-state
+			  (not (looking-at (concat outline-regexp "[ \t]*"
+						   org-clock-in-switch-to-state
+						   "\\>"))))
+		     (org-todo org-clock-in-switch-to-state)))
+	      (setq org-clock-heading-for-remember
+		    (and (looking-at org-complex-heading-regexp)
+			 (match-end 4)
+			 (org-trim (buffer-substring (match-end 1)
+						     (match-end 4)))))
+	      (setq org-clock-heading
+		    (cond ((and org-clock-heading-function
+				(functionp org-clock-heading-function))
+			   (funcall org-clock-heading-function))
+			  ((looking-at org-complex-heading-regexp)
+			   (replace-regexp-in-string
+			    "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
+			    (match-string 4)))
+			  (t "???")))
+	      (setq org-clock-heading (org-propertize org-clock-heading
+						      'face nil))
+	      (org-clock-find-position org-clock-in-resume)
+	      (cond
+	       ((and org-clock-in-resume
+		     (looking-at
+		      (concat "^[ \t]* " org-clock-string
+			      " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
+			      " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
+		(message "Matched %s" (match-string 1))
+		(setq ts (concat "[" (match-string 1) "]"))
+		(goto-char (match-end 1))
+		(setq org-clock-start-time
+		      (apply 'encode-time
+			     (org-parse-time-string (match-string 1))))
+		(setq org-clock-effort (org-get-effort))
+		(setq org-clock-total-time (org-clock-sum-current-item
+					    (org-clock-get-sum-start))))
+	       ((eq org-clock-in-resume 'auto-restart)
+		;; called from org-clock-load during startup,
+		;; do not interrupt, but warn!
+		(message "Cannot restart clock because task does not contain unfinished clock")
+		(ding)
+		(sit-for 2)
+		(throw 'abort nil))
+	       (t
+		(insert-before-markers "\n")
+		(backward-char 1)
+		(org-indent-line-function)
+		(when (and (save-excursion
+			     (end-of-line 0)
+			     (org-in-item-p)))
+		  (beginning-of-line 1)
+		  (org-indent-line-to (- (org-get-indentation) 2)))
+		(insert org-clock-string " ")
+		(setq org-clock-effort (org-get-effort))
+		(setq org-clock-total-time (org-clock-sum-current-item
+					    (org-clock-get-sum-start)))
+		(setq org-clock-start-time
+		      (or (and leftover
+			       (y-or-n-p
+				(format
+				 "You stopped another clock %d mins ago; start this one from then? "
+				 (/ (- (org-float-time (current-time))
+				       (org-float-time leftover)) 60)))
+			       leftover)
+			  start-time
+			  (current-time)))
+		(setq ts (org-insert-time-stamp org-clock-start-time
+						'with-hm 'inactive))))
+	      (move-marker org-clock-marker (point) (buffer-base-buffer))
+	      (move-marker org-clock-hd-marker
+			   (save-excursion (org-back-to-heading t) (point))
+			   (buffer-base-buffer))
+	      (setq org-clock-has-been-used t)
+	      (or global-mode-string (setq global-mode-string '("")))
+	      (or (memq 'org-mode-line-string global-mode-string)
+		  (setq global-mode-string
+			(append global-mode-string '(org-mode-line-string))))
+	      (org-clock-update-mode-line)
+	      (when org-clock-mode-line-timer
+		(cancel-timer org-clock-mode-line-timer)
+		(setq org-clock-mode-line-timer nil))
+	      (setq org-clock-mode-line-timer
+		    (run-with-timer org-clock-update-period
+				    org-clock-update-period
+				    'org-clock-update-mode-line))
+	      (when org-clock-idle-timer
+		(cancel-timer org-clock-idle-timer)
+		(setq org-clock-idle-timer nil))
+	      (setq org-clock-idle-timer
+		    (run-with-timer 60 60 'org-resolve-clocks-if-idle))
+	      (message "Clock starts at %s - %s" ts msg-extra)
+	      (run-hooks 'org-clock-in-hook))))))))
 
 (defvar org-clock-current-task nil
   "Task currently clocked in.")
-- 
1.7.1.86.g0e460

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add org-clock-clocking-in for hooks
  2010-05-20 15:14 [PATCH] Add org-clock-clocking-in for hooks Bernt Hansen
@ 2010-05-20 15:43 ` Bernt Hansen
  2010-05-20 16:20   ` [PATCH] Set org-clock-clocking-in when calling org-clock-out from org-clock-in Bernt Hansen
  0 siblings, 1 reply; 4+ messages in thread
From: Bernt Hansen @ 2010-05-20 15:43 UTC (permalink / raw)
  To: emacs-orgmode

Bernt Hansen <bernt@norang.ca> writes:

> org-clock-out-hook can now query org-clock-clocking-in to see if org-clock-out is
> being called inside org-clock-in.  This allows the hook to selectively clock
> in another task without leaving clocks open.

OOPS.  Please don't apply this just yet.  I originally called my new var
org-clock-in-active but found there's a org-clock-is-active already and
thought it was too similar.  So I changed it to
org-clock-clocking-in... and it turns out we have one of those already.

New patch to follow.

Thanks,
Bernt

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] Set org-clock-clocking-in when calling org-clock-out from org-clock-in
  2010-05-20 15:43 ` Bernt Hansen
@ 2010-05-20 16:20   ` Bernt Hansen
  2010-05-20 21:23     ` John Wiegley
  0 siblings, 1 reply; 4+ messages in thread
From: Bernt Hansen @ 2010-05-20 16:20 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Bernt Hansen

org-clock-out-hook can now query org-clock-clocking-in to see if org-clock-out is
being called inside org-clock-in.  This allows the hook to selectively clock
in another task without leaving clocks open.
---
Here's the updated patch which seems to work just as well.

This patch is available at git://git.norang.ca/org-mode.git persistent-clocking

Most of the changes here are whitespace.  The very last change in the patch is the 
only interesting part.

-Bernt

 lisp/org-clock.el |   25 +++++++++++++------------
 1 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 68a40ce..dbcd032 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -925,7 +925,7 @@ the clocking selection, associated with the letter `d'."
 			     (org-clocking-p)))
 	  ts selected-task target-pos (msg-extra "")
 	  (leftover (and (not org-clock-resolving-clocks)
-			  org-clock-leftover-time)))
+			 org-clock-leftover-time)))
       (when (and org-clock-auto-clock-resolution
 		 (or (not interrupting)
 		     (eq t org-clock-auto-clock-resolution))
@@ -944,22 +944,23 @@ the clocking selection, associated with the letter `d'."
 	;; Save a marker to this task, so that we can go back.
 	;; First check if we are trying to clock into the same task!
 	(if (save-excursion
-		(unless selected-task
-		  (org-back-to-heading t))
-		(and (equal (marker-buffer org-clock-hd-marker)
-			    (if selected-task
-				(marker-buffer selected-task)
-			      (current-buffer)))
-		     (= (marker-position org-clock-hd-marker)
-			(if selected-task
-			    (marker-position selected-task)
-			  (point)))))
+	      (unless selected-task
+		(org-back-to-heading t))
+	      (and (equal (marker-buffer org-clock-hd-marker)
+			  (if selected-task
+			      (marker-buffer selected-task)
+			    (current-buffer)))
+		   (= (marker-position org-clock-hd-marker)
+		      (if selected-task
+			  (marker-position selected-task)
+			(point)))))
 	    (message "Clock continues in \"%s\"" org-clock-heading)
 	  (progn
 	    (move-marker org-clock-interrupted-task
 			 (marker-position org-clock-marker)
 			 (org-clocking-buffer))
-	    (org-clock-out t))))
+	    (let ((org-clock-clocking-in t))
+	      (org-clock-out t)))))
 
       (when (equal select '(16))
 	;; Mark as default clocking task
-- 
1.7.1.86.g0e460

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Set org-clock-clocking-in when calling org-clock-out from org-clock-in
  2010-05-20 16:20   ` [PATCH] Set org-clock-clocking-in when calling org-clock-out from org-clock-in Bernt Hansen
@ 2010-05-20 21:23     ` John Wiegley
  0 siblings, 0 replies; 4+ messages in thread
From: John Wiegley @ 2010-05-20 21:23 UTC (permalink / raw)
  To: Bernt Hansen; +Cc: emacs-orgmode

On May 20, 2010, at 12:20 PM, Bernt Hansen wrote:

> Here's the updated patch which seems to work just as well.

Thanks, Bernt, I'll try to get this in today.  Still working out kinks in our new patch acceptance system. :)

John

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-05-20 21:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-20 15:14 [PATCH] Add org-clock-clocking-in for hooks Bernt Hansen
2010-05-20 15:43 ` Bernt Hansen
2010-05-20 16:20   ` [PATCH] Set org-clock-clocking-in when calling org-clock-out from org-clock-in Bernt Hansen
2010-05-20 21:23     ` John Wiegley

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).