emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Samuel W. Flint" <swflint+orgmode@flintfam.org>
To: "Org-Mode" <emacs-orgmode@gnu.org>
Subject: [PATCH v2 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier
Date: Wed, 26 Oct 2022 09:53:50 -0500	[thread overview]
Message-ID: <87eduud4ap.fsf@flintfam.org> (raw)

* 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


             reply	other threads:[~2022-10-26 14:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26 14:53 Samuel W. Flint [this message]
2022-10-27  4:11 ` [PATCH v2 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87eduud4ap.fsf@flintfam.org \
    --to=swflint+orgmode@flintfam.org \
    --cc=875yg6elmh.fsf@flintfam.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).