all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH v2 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier
@ 2022-10-26 14:53 Samuel W. Flint
  2022-10-27  4:11 ` Max Nikulin
  2022-10-27  4:45 ` Ihor Radchenko
  0 siblings, 2 replies; 10+ messages in thread
From: Samuel W. Flint @ 2022-10-26 14:53 UTC (permalink / raw)
  To: Org-Mode

* lisp/org-clock.el (org-clock-in-switch-to-state,
org-clock-out-switch-to-state): Allow
`org-clock-{in,out}-switch-to-state` to be an alist of
current/next-state pairs.

(org-clock-in, org-clock-out): Add logic to handle
`org-clock-{in,out}-switch-to-state` as alists.
---
 lisp/org-clock.el | 60 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 21 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 6332399bb..f5ffb515f 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -148,28 +148,34 @@ out time will be 14:50."
 (defcustom org-clock-in-switch-to-state nil
   "Set task to a special todo state while clocking it.
 The value should be the state to which the entry should be
-switched.  If the value is a function, it must take one
-parameter (the current TODO state of the item) and return the
-state to switch it to."
+switched.  It may also be an alist of `(CURRENT . NEXT)' pairs.
+If the value is a function, it must take one parameter (the
+current TODO state of the item) and return the state to switch it
+to."
   :group 'org-clock
   :group 'org-todo
   :type '(choice
 	  (const :tag "Don't force a state" nil)
 	  (string :tag "State")
-	  (symbol :tag "Function")))
+          (alist :key-value  (string :tag "Current State:")
+                 :value-type (string :tag "Next State   :"))
+          (symbol :tag "Function")))
 
 (defcustom org-clock-out-switch-to-state nil
   "Set task to a special todo state after clocking out.
 The value should be the state to which the entry should be
-switched.  If the value is a function, it must take one
-parameter (the current TODO state of the item) and return the
-state to switch it to."
+switched.  It may also be an alist of `(CURRENT . NEXT)' pairs.
+If the value is a function, it must take one parameter (the
+current TODO state of the item) and return the state to switch it
+to."
   :group 'org-clock
   :group 'org-todo
   :type '(choice
 	  (const :tag "Don't force a state" nil)
 	  (string :tag "State")
-	  (symbol :tag "Function")))
+          (alist :key-value  (string :tag "Current State:")
+                 :value-type (string :tag "Next State   :"))
+          (symbol :tag "Function")))
 
 (defcustom org-clock-history-length 5
   "Number of clock tasks to remember in history.
@@ -1348,17 +1354,23 @@ the default behavior."
 	 (run-hooks 'org-clock-in-prepare-hook)
 	 (org-clock-history-push)
 	 (setq org-clock-current-task (org-get-heading t t t t))
-	 (cond ((functionp org-clock-in-switch-to-state)
-		(let ((case-fold-search nil))
-		  (looking-at org-complex-heading-regexp))
-		(let ((newstate (funcall org-clock-in-switch-to-state
-					 (match-string 2))))
-		  (when newstate (org-todo newstate))))
+	 (cond ((listp org-clock-in-switch-to-state)
+                (let ((case-fold-search nil))
+                  (looking-at org-complex-heading-regexp))
+                (when-let ((state-pair (assoc (match-string 2) org-clock-in-switch-to-state))
+                           (new-state (cdr state-pair)))
+                  (org-todo new-state)))
+               ((functionp org-clock-in-switch-to-state)
+	        (let ((case-fold-search nil))
+	          (looking-at org-complex-heading-regexp))
+	        (let ((newstate (funcall org-clock-in-switch-to-state
+				         (match-string 2))))
+	          (when newstate (org-todo newstate))))
 	       ((and org-clock-in-switch-to-state
 		     (not (looking-at (concat org-outline-regexp "[ \t]*"
-					    org-clock-in-switch-to-state
-					    "\\>"))))
-		(org-todo org-clock-in-switch-to-state)))
+					      org-clock-in-switch-to-state
+					      "\\>"))))
+	        (org-todo org-clock-in-switch-to-state)))
 	 (setq org-clock-heading (org-clock--mode-line-heading))
 	 (org-clock-find-position org-clock-in-resume)
 	 (cond
@@ -1720,6 +1732,12 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
 	      (org-back-to-heading t)
 	      (let ((org-clock-out-when-done nil))
 		(cond
+                 ((listp org-clock-out-switch-to-state)
+                  (let ((case-fold-search nil))
+                    (looking-at org-complex-heading-regexp))
+                  (when-let ((state-pair (assoc (match-string 2) org-clock-out-switch-to-state))
+                             (new-state (cdr state-pair)))
+                    (org-todo new-state)))
 		 ((functionp org-clock-out-switch-to-state)
 		  (let ((case-fold-search nil))
 		    (looking-at org-complex-heading-regexp))
@@ -1728,10 +1746,10 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
 		    (when newstate (org-todo newstate))))
 		 ((and org-clock-out-switch-to-state
 		       (not (looking-at
-                           (concat
-                            org-outline-regexp "[ \t]*"
-			    org-clock-out-switch-to-state
-			    "\\>"))))
+                             (concat
+                              org-outline-regexp "[ \t]*"
+			      org-clock-out-switch-to-state
+			      "\\>"))))
 		  (org-todo org-clock-out-switch-to-state))))))
 	  (force-mode-line-update)
 	  (message (if remove
-- 
2.37.0



-- 
Samuel W. Flint
4096R/FA13D704
      (F50D 862B 4F65 5943 A8C2  EF0E 86C9 3E7A FA13 D704)
λs.(s s) λs.(s s)
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org


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

end of thread, other threads:[~2023-07-16  8:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-26 14:53 [PATCH v2 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier Samuel W. Flint
2022-10-27  4:11 ` Max Nikulin
2022-10-27  4:45 ` Ihor Radchenko
2022-10-28 15:03   ` Samuel W. Flint
2022-10-29  3:51     ` Ihor Radchenko
2022-11-21  9:06       ` Ihor Radchenko
2022-11-21 14:53         ` Samuel W. Flint
2022-11-22  3:36           ` Ihor Radchenko
2023-01-27 10:38             ` Ihor Radchenko
2023-07-16  8:22   ` Ihor Radchenko

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.