emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier
@ 2022-10-22 18:29 Samuel W. Flint
  2022-10-31  5:58 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Samuel W. Flint @ 2022-10-22 18:29 UTC (permalink / raw)
  To: Org-Mode

* lisp/org-clock.el (org-clock-in-next-state,
org-clock-out-next-state): Define and document customizeable variables
to describe the "next state" on clock-in/-out.

(org-clock-in-switch-to-state, org-clock-out-switch-to-state): Add
options to use the `org-clock-{in,out}-next-state' variables to change
on clock-in/-out.

(org-clock-in-next-state-function, org-clock-out-next-state-function):
Define functions which can be used to select the next TODO state on
clock-in/-out.
---
 lisp/org-clock.el | 48 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 6332399bb..18cd83988 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -145,6 +145,16 @@ out time will be 14:50."
   :group 'org-clock
   :type 'boolean)
 
+(defcustom org-clock-in-next-state nil
+  "Alist of next TODO states for clocking out.
+The symbol `error' may be used to prevent clocking out on tasks."
+  :group 'org-clock
+  :group 'org-todo
+  :type '(alist :key-value  (string :tag "Current State:")
+                :value-type (choice :tag "Next State   :"
+                                    (string :tag "TODO Keyword")
+                                    (const  :tag "Block Clock-In" 'error))))
+
 (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
@@ -156,7 +166,18 @@ state to switch it to."
   :type '(choice
 	  (const :tag "Don't force a state" nil)
 	  (string :tag "State")
-	  (symbol :tag "Function")))
+	  (const :tag "Use `org-clock-in-next-state'" 'org-clock-in-next-state-function)
+          (symbol :tag "Function")))
+
+(defcustom org-clock-out-next-state nil
+  "Alist of next TODO states for clocking out.
+The symbol `error' may be used to prevent clocking in on tasks."
+  :group 'org-clock
+  :group 'org-todo
+  :type '(alist :key-value  (string :tag "Current State:")
+                :value-type (choice :tag "Next State   :"
+                                    (string :tag "TODO Keyword")
+                                    (const  :tag "Block Clock-Out" 'error))))
 
 (defcustom org-clock-out-switch-to-state nil
   "Set task to a special todo state after clocking out.
@@ -169,7 +190,8 @@ state to switch it to."
   :type '(choice
 	  (const :tag "Don't force a state" nil)
 	  (string :tag "State")
-	  (symbol :tag "Function")))
+	  (const :tag "Use `org-clock-out-next-state'" 'org-clock-out-next-state-function)
+          (symbol :tag "Function")))
 
 (defcustom org-clock-history-length 5
   "Number of clock tasks to remember in history.
@@ -1256,6 +1278,17 @@ so long."
 (defvar org-clock-out-time nil) ; store the time of the last clock-out
 (defvar org--msg-extra)
 
+(defun org-clock-in-next-state-function (state)
+  "Change state to the next logical TODO STATE on clock-in.
+
+Use `org-clock-in-next-state'."
+  (when-let ((state-pair (assoc state org-clock-in-next-state))
+             (next-state (cdr state-pair)))
+    (if (and (symbolp next-state)
+             (eq 'error next-state))
+        (user-error (format "Cannot clock into %s task, change to a valid TODO state." state))
+      next-state)))
+
 ;;;###autoload
 (defun org-clock-in (&optional select start-time)
   "Start the clock on the current item.
@@ -1650,6 +1683,17 @@ and current `frame-title-format' is equal to `org-clock-frame-title-format'."
 	     (equal frame-title-format org-clock-frame-title-format))
     (setq frame-title-format org-frame-title-format-backup)))
 
+(defun org-clock-out-next-state-function (state)
+  "Change state to the next logical TODO STATE on clock-out.
+
+Use `org-clock-out-next-state'."
+  (when-let ((state-pair (assoc state org-clock-out-next-state))
+             (next-state (cdr state-pair)))
+    (if (and (symbolp next-state)
+             (eq 'error next-state))
+        (user-error (format "Cannot clock into %s task, change to a valid TODO state." state))
+      next-state)))
+
 ;;;###autoload
 (defun org-clock-out (&optional switch-to-state fail-quietly at-time)
   "Stop the currently running clock.
-- 
2.37.3



-- 
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] 2+ messages in thread

end of thread, other threads:[~2022-10-31  5:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 18:29 [PATCH 1/2] lisp/org-clock.el: Make switching states on clock-in/-out easier Samuel W. Flint
2022-10-31  5:58 ` Ihor Radchenko

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).