From e2fa281ca3d5c58fd759011c5f93790f49cb669d Mon Sep 17 00:00:00 2001 From: Allen Li Date: Tue, 12 Jan 2021 00:33:32 -0800 Subject: [PATCH 2/2] org-clock: Query when exiting with running clock It's annoying to accidentally quit Emacs with a running clock, then resolve the clock the next time when Emacs is started. * lisp/org-clock.el (org-clock-kill-emacs-query): New function. (org-clock-ask-before-exiting): New user option. --- lisp/org-clock.el | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lisp/org-clock.el b/lisp/org-clock.el index 2a6a9af47..52b486cd8 100644 --- a/lisp/org-clock.el +++ b/lisp/org-clock.el @@ -485,6 +485,16 @@ is added to the user configuration." (integer :tag "Clock out after Emacs is idle for X seconds") (const :tag "Never auto clock out" nil))) +(defcustom org-clock-ask-before-exiting t + "If non-nil, ask if the user wants to clock out before exiting Emacs. + This variable only has effect if set with \\[customize]." + :set (lambda (symbol value) + (if value + (add-hook 'kill-emacs-query-functions #'org-clock-kill-emacs-query) + (remove-hook 'kill-emacs-query-functions #'org-clock-kill-emacs-query)) + (set symbol value)) + :type 'boolean) + (defvar org-clock-in-prepare-hook nil "Hook run when preparing the clock. This hook is run before anything happens to the task that @@ -3096,6 +3106,17 @@ The details of what will be saved are regulated by the variable (when (org-invisible-p) (org-show-context)))))) (_ nil))))) +(defun org-clock-kill-emacs-query () + "Query user when killing Emacs. +This function is added to `kill-emacs-query-functions'." + (let ((buf (org-clock-buffer))) + (when (and buf (yes-or-no-p "Clock out and save? ")) + (with-current-buffer buf + (org-clock-out) + (save-buffer)))) + ;; Unconditionally return t for `kill-emacs-query-functions'. + t) + ;; Suggested bindings (org-defkey org-mode-map "\C-c\C-x\C-e" 'org-clock-modify-effort-estimate) -- 2.30.1