diff --git a/doc/org-manual.org b/doc/org-manual.org index b26f152f7..985c9d4f6 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -6886,6 +6886,21 @@ If you only want this from time to time, use three universal prefix arguments with ~org-clock-in~ and two {{{kbd(C-u C-u)}}} with ~org-clock-in-last~. +**** Clocking out automatically after some idle time +:PROPERTIES: +:UNNUMBERED: notoc +:END: +#+cindex: auto clocking out after idle time + +#+vindex: org-clock-auto-clockout-timer +When you often forget to clock out before being idle and you don't +want to manually set the clocking time to take into account, you can +set ~org-clock-auto-clockout-timer~ to a number of seconds and add +=(org-clock-auto-clockout-insinuate)= to your =.emacs= file. + +When the clock is running and Emacs is idle for more than this number +of seconds, the clock will be clocked out automatically. + ** Effort Estimates :PROPERTIES: :DESCRIPTION: Planning work effort in advance. diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index bebdee267..359853eb6 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -79,6 +79,15 @@ Babel Java blocks recognize header argument =:cmdargs= and pass its value in call to =java=. ** New options +*** New option ~org-clock-auto-clockout-timer~ + +When this option is set to a number and the user configuration +contains =(org-clock-auto-clockout-insinuate)=, Org will clock out the +currently clocked in task after that number of seconds of idle time. + +This is useful when you often forget to clock out before being idle +and don't want to have to manually set the clocking time to take into +account. *** New option ~org-table-header-line-p~ diff --git a/lisp/org-clock.el b/lisp/org-clock.el index ec9e02291..b648ca289 100644 --- a/lisp/org-clock.el +++ b/lisp/org-clock.el @@ -468,6 +468,19 @@ Valid values are: `today', `yesterday', `thisweek', `lastweek', (const :tag "Select range interactively" interactive)) :safe #'symbolp) +(defcustom org-clock-auto-clockout-timer nil + "Timer for auto clocking out when Emacs is idle. +When set to a number, auto clock out the currently clocked in +task after this number of seconds of idle time. + +This is only effective when `org-clock-auto-clockout-insinuate' +is added to the user configuration." + :group 'org-clock + :package-version '(Org . "9.4") + :type '(choice + (integer :tag "Clock out after Emacs is idle for X seconds") + (const :tag "Never auto clock out" nil))) + (defvar org-clock-in-prepare-hook nil "Hook run when preparing the clock. This hook is run before anything happens to the task that @@ -1397,6 +1410,17 @@ the default behavior." (message "Clock starts at %s - %s" ts org--msg-extra) (run-hooks 'org-clock-in-hook)))))) +(defun org-clock-auto-clockout () + "Clock out the currently clocked in task if Emacs is idle. +See `org-clock-auto-clockout-timer' to set the idle time span. + +Thie is only effective when `org-clock-auto-clockout-insinuate' +is present in the user configuration." + (when (and (numberp org-clock-auto-clockout-timer) + org-clock-current-task) + (run-with-idle-timer + org-clock-auto-clockout-timer nil #'org-clock-out))) + ;;;###autoload (defun org-clock-in-last (&optional arg) "Clock in the last closed clocked item. diff --git a/lisp/org.el b/lisp/org.el index d2c2691cc..a9109f000 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3766,6 +3766,14 @@ If yes, offer to stop it and to save the buffer with the changes." (add-hook 'org-mode-hook 'org-clock-load) (add-hook 'kill-emacs-hook 'org-clock-save)) +(defun org-clock-auto-clockout-insinuate () + "Set up hook for auto clocking out when Emacs is idle. +See `org-clock-auto-clockout-timer'. + +This function is meant to be added to the user configuration." + (require 'org-clock) + (add-hook 'org-clock-in-hook #'org-clock-auto-clockout t)) + (defgroup org-archive nil "Options concerning archiving in Org mode." :tag "Org Archive"