* [PATCH] Query when exiting with running clock
@ 2021-01-12 8:55 Allen Li
2021-02-03 5:45 ` Kyle Meyer
0 siblings, 1 reply; 4+ messages in thread
From: Allen Li @ 2021-01-12 8:55 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1239 bytes --]
This is a patch adding a query function when exiting Emacs, warning the
user if there is a running clock. This is useful for preventing the
user from accidentally leaving dangling clocks. I have had success
using a modified personal version of this code.
My personal version instead prompts the user to clock out and save the
buffer. I didn't use it for the patch because it seems a little hacky
to me; e.g., kill-emacs-query-functions doesn't mention whether
functions can have side effects, and the UI is inconsistent with
save-buffers-kill-emacs. The code for my personal version:
(add-to-list 'kill-emacs-query-functions
(lambda ()
(if (not (org-clocking-p))
t
(if (y-or-n-p "Clock out and save?")
(with-current-buffer (marker-buffer org-clock-marker)
(org-clock-out)
(save-buffer)
t)
(message "Aborting")
nil))))
If there is great demand for this version, I might provide another patch.
I have also attached another patch which deduplicates two identical
functions that I noticed when preparing the first patch.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-clock-Replace-org-clocking-buffer-with-org-clock.patch --]
[-- Type: text/x-patch, Size: 4509 bytes --]
From f6145afd7d457cec533c44c8a3297d28f11d7255 Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
Date: Tue, 12 Jan 2021 00:26:47 -0800
Subject: [PATCH 1/2] org-clock: Replace org-clocking-buffer with
org-clock-is-active
org-clocking-buffer and org-clock-is-active have the same definition.
org-clocking-buffer is defined in org-clock.el while
org-clock-is-active is defined in org.el. org-clock.el requires
org.el.
org-clocking-buffer is kept as an alias to preserve backward
compatibility with any user code.
* lisp/org-clock.el (org-clocking-buffer): Changed to alias.
(org-clocking-p): Changed reference to org-clocking-buffer.
(org-clock-out): Changed reference to org-clocking-buffer.
(org-clock-cancel): Changed reference to org-clocking-buffer.
(org-clock-sum): Changed reference to org-clocking-buffer.
(org-clock-out-if-current): Changed reference to org-clocking-buffer.
(org-clock-save): Changed reference to org-clocking-buffer.
---
lisp/org-clock.el | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 892ae1810..37459580b 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -503,13 +503,11 @@ of a different task.")
(mapc (lambda (m) (org-check-and-save-marker m beg end))
org-clock-history))
-(defun org-clocking-buffer ()
- "Return the clocking buffer if we are currently clocking a task or nil."
- (marker-buffer org-clock-marker))
+(defalias 'org-clocking-buffer #'org-clock-is-active)
(defun org-clocking-p ()
"Return t when clocking a task."
- (not (equal (org-clocking-buffer) nil)))
+ (not (equal (org-clock-is-active) nil)))
(defvar org-clock-before-select-task-hook nil
"Hook called in task selection just before prompting the user.")
@@ -1501,7 +1499,7 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
ts te s h m remove)
(setq org-clock-out-time now)
(save-excursion ; Do not replace this with `with-current-buffer'.
- (org-no-warnings (set-buffer (org-clocking-buffer)))
+ (org-no-warnings (set-buffer (org-clock-is-active)))
(save-restriction
(widen)
(goto-char org-clock-marker)
@@ -1652,7 +1650,7 @@ Optional argument N tells to change by that many units."
(force-mode-line-update)
(error "No active clock"))
(save-excursion ; Do not replace this with `with-current-buffer'.
- (org-no-warnings (set-buffer (org-clocking-buffer)))
+ (org-no-warnings (set-buffer (org-clock-is-active)))
(goto-char org-clock-marker)
(if (org-looking-back (concat "^[ \t]*" org-clock-string ".*"))
(progn (delete-region (1- (point-at-bol)) (point-at-eol))
@@ -1763,7 +1761,7 @@ PROPNAME lets you set a custom text property instead of :org-clock-minutes."
(t ;; A headline
;; Add the currently clocking item time to the total
(when (and org-clock-report-include-clocking-task
- (equal (org-clocking-buffer) (current-buffer))
+ (equal (org-clock-is-active) (current-buffer))
(equal (marker-position org-clock-hd-marker) (point))
tstart
tend
@@ -1897,8 +1895,8 @@ and is only done if the variable `org-clock-out-when-done' is not nil."
(member org-state org-done-keywords))
(and (listp org-clock-out-when-done)
(member org-state org-clock-out-when-done)))
- (equal (or (buffer-base-buffer (org-clocking-buffer))
- (org-clocking-buffer))
+ (equal (or (buffer-base-buffer (org-clock-is-active))
+ (org-clock-is-active))
(or (buffer-base-buffer (current-buffer))
(current-buffer)))
(< (point) org-clock-marker)
@@ -2816,7 +2814,7 @@ The details of what will be saved are regulated by the variable
system-name (format-time-string
(cdr org-time-stamp-formats))))
(if (and (memq org-clock-persist '(t clock))
- (setq b (org-clocking-buffer))
+ (setq b (org-clock-is-active))
(setq b (or (buffer-base-buffer b) b))
(buffer-live-p b)
(buffer-file-name b)
@@ -2824,7 +2822,7 @@ The details of what will be saved are regulated by the variable
(y-or-n-p (concat "Save current clock ("
org-clock-heading ") "))))
(insert "(setq resume-clock '(\""
- (buffer-file-name (org-clocking-buffer))
+ (buffer-file-name (org-clock-is-active))
"\" . " (int-to-string (marker-position org-clock-marker))
"))\n"))
;; Store clocked task history. Tasks are stored reversed to make
--
2.30.0
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-org-clock-Query-when-exiting-with-running-clock.patch --]
[-- Type: text/x-patch, Size: 1178 bytes --]
From d12b01955773be64d04bd391d822cf7d1b67f637 Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
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.
---
lisp/org-clock.el | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 37459580b..bc1762f63 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -2886,6 +2886,15 @@ The details of what will be saved are regulated by the variable
(if (outline-invisible-p)
(org-show-context))))))))))
+(defun org-clock-kill-emacs-query ()
+ "Query user when killing Emacs.
+This function is added to `kill-emacs-query-functions'."
+ (if (org-clocking-buffer)
+ (y-or-n-p "Org clock is running; exit anyway? ")
+ t))
+
+(add-hook 'kill-emacs-query-functions #'org-clock-kill-emacs-query)
+
;; Suggested bindings
(org-defkey org-mode-map "\C-c\C-x\C-e" 'org-clock-modify-effort-estimate)
--
2.30.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] Query when exiting with running clock
2021-01-12 8:55 [PATCH] Query when exiting with running clock Allen Li
@ 2021-02-03 5:45 ` Kyle Meyer
2021-03-03 2:08 ` Allen Li
0 siblings, 1 reply; 4+ messages in thread
From: Kyle Meyer @ 2021-02-03 5:45 UTC (permalink / raw)
To: Allen Li; +Cc: emacs-orgmode
Allen Li writes:
> This is a patch adding a query function when exiting Emacs, warning the
> user if there is a running clock. This is useful for preventing the
> user from accidentally leaving dangling clocks. I have had success
> using a modified personal version of this code.
Thanks. I'd find this useful as well.
> My personal version instead prompts the user to clock out and save the
> buffer. I didn't use it for the patch because it seems a little hacky
> to me; e.g., kill-emacs-query-functions doesn't mention whether
> functions can have side effects, and the UI is inconsistent with
> save-buffers-kill-emacs. The code for my personal version:
Fwiw that's what Emacs's lisp/calendar/timeclock.el does:
(defun timeclock-query-out ()
"Ask the user whether to clock out.
This is a useful function for adding to `kill-emacs-query-functions'."
(and (equal (car timeclock-last-event) "i")
(y-or-n-p "You're currently clocking time, clock out? ")
(timeclock-out))
;; Unconditionally return t for `kill-emacs-query-functions'.
t)
> Subject: [PATCH 1/2] org-clock: Replace org-clocking-buffer with
> org-clock-is-active
>
> org-clocking-buffer and org-clock-is-active have the same definition.
> org-clocking-buffer is defined in org-clock.el while
> org-clock-is-active is defined in org.el. org-clock.el requires
> org.el.
>
> org-clocking-buffer is kept as an alias to preserve backward
> compatibility with any user code.
Dropping the duplicate definitions using an alias sounds good, though as
I mention below I'd prefer the spots that are specifically concerned
with a buffer to keep using the org-clocking-buffer name.
> * lisp/org-clock.el (org-clocking-buffer): Changed to alias.
> (org-clocking-p): Changed reference to org-clocking-buffer.
> (org-clock-out): Changed reference to org-clocking-buffer.
> (org-clock-cancel): Changed reference to org-clocking-buffer.
> (org-clock-sum): Changed reference to org-clocking-buffer.
> (org-clock-out-if-current): Changed reference to org-clocking-buffer.
> (org-clock-save): Changed reference to org-clocking-buffer.
> ---
> lisp/org-clock.el | 20 +++++++++-----------
> 1 file changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/lisp/org-clock.el b/lisp/org-clock.el
> index 892ae1810..37459580b 100644
Hmm, this patch doesn't apply to the current master (041459727). The
preimage blob is quite old:
$ git describe 892ae1810
release_8.2.7b-7-gbacfe5b4f:lisp/org-clock.el
$ git log --oneline --format="%h %cd" --find-object=892ae1810
ca21b7b86 Mon Jan 12 12:35:10 2015 +0100
bacfe5b4f Fri Jul 25 11:02:55 2014 +0200
> --- a/lisp/org-clock.el
> +++ b/lisp/org-clock.el
> @@ -503,13 +503,11 @@ of a different task.")
> (mapc (lambda (m) (org-check-and-save-marker m beg end))
> org-clock-history))
>
> -(defun org-clocking-buffer ()
> - "Return the clocking buffer if we are currently clocking a task or nil."
> - (marker-buffer org-clock-marker))
> +(defalias 'org-clocking-buffer #'org-clock-is-active)
>
> (defun org-clocking-p ()
> "Return t when clocking a task."
> - (not (equal (org-clocking-buffer) nil)))
> + (not (equal (org-clock-is-active) nil)))
Eh, so this too could just be an alias to org-clock-is-active, or, if it
looks like any callers really depend on this being t rather than just
non-nil (but why would they?), `(and (org-clock-is-active) t)' would be
better. Anyway, I know you're just doing the plain substitution here,
so I'm fine keeping it at that.
>
> (defvar org-clock-before-select-task-hook nil
> "Hook called in task selection just before prompting the user.")
> @@ -1501,7 +1499,7 @@ to, overriding the existing value of `org-clock-out-switch-to-state'."
> ts te s h m remove)
> (setq org-clock-out-time now)
> (save-excursion ; Do not replace this with `with-current-buffer'.
> - (org-no-warnings (set-buffer (org-clocking-buffer)))
> + (org-no-warnings (set-buffer (org-clock-is-active)))
This is an example of a spot that I think should continue using the
org-clocking-buffer variant for readability. And scanning the other
spots in this patch, I think they actually all fall into this category.
> 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.
> ---
> lisp/org-clock.el | 9 +++++++++
> 1 file changed, 9 insertions(+)
>
> diff --git a/lisp/org-clock.el b/lisp/org-clock.el
> index 37459580b..bc1762f63 100644
> --- a/lisp/org-clock.el
> +++ b/lisp/org-clock.el
> @@ -2886,6 +2886,15 @@ The details of what will be saved are regulated by the variable
> (if (outline-invisible-p)
> (org-show-context))))))))))
>
> +(defun org-clock-kill-emacs-query ()
> + "Query user when killing Emacs.
> +This function is added to `kill-emacs-query-functions'."
> + (if (org-clocking-buffer)
Hmm, why use org-clocking-buffer rather than org-clock-is-active? After
a patch that introduced org-clock-is-active and switched a bunch of
spots over to it, I would have expected you to use it here.
> + (y-or-n-p "Org clock is running; exit anyway? ")
> + t))
> +
> +(add-hook 'kill-emacs-query-functions #'org-clock-kill-emacs-query)
lisp/calendar/timeclock.el has the following option:
(defcustom timeclock-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 #'timeclock-query-out)
(remove-hook 'kill-emacs-query-functions #'timeclock-query-out))
(set symbol value))
:type 'boolean)
I wonder if we should do something similar.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Query when exiting with running clock
2021-02-03 5:45 ` Kyle Meyer
@ 2021-03-03 2:08 ` Allen Li
2021-03-04 3:54 ` Kyle Meyer
0 siblings, 1 reply; 4+ messages in thread
From: Allen Li @ 2021-03-03 2:08 UTC (permalink / raw)
To: Kyle Meyer; +Cc: Org Mode List
[-- Attachment #1: Type: text/plain, Size: 495 bytes --]
On Wed, Feb 3, 2021 at 5:45 AM Kyle Meyer <kyle@kyleam.com> wrote:
>
> Allen Li writes:
>
> > This is a patch adding a query function when exiting Emacs, warning the
> > user if there is a running clock. This is useful for preventing the
> > user from accidentally leaving dangling clocks. I have had success
> > using a modified personal version of this code.
>
> Thanks. I'd find this useful as well.
Thanks for your feedback. I have addressed your comments. Please see
the new patches.
[-- Attachment #2: 0002-org-clock-Query-when-exiting-with-running-clock.patch --]
[-- Type: text/x-patch, Size: 2083 bytes --]
From e2fa281ca3d5c58fd759011c5f93790f49cb669d Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
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
[-- Attachment #3: 0001-org-clock-Replace-org-clocking-buffer-with-org-clock.patch --]
[-- Type: text/x-patch, Size: 2943 bytes --]
From 429dd06ac281a5706c8ce2f3e35dab4aab6b5bfc Mon Sep 17 00:00:00 2001
From: Allen Li <darkfeline@felesatra.moe>
Date: Tue, 12 Jan 2021 00:26:47 -0800
Subject: [PATCH 1/2] org-clock: Replace org-clocking-buffer with
org-clock-is-active
org-clocking-buffer and org-clock-is-active have the same definition.
org-clocking-buffer is defined in org-clock.el while
org-clock-is-active is defined in org.el. org-clock.el requires
org.el.
org-clocking-buffer is kept as an alias to preserve backward
compatibility with any user code.
* lisp/org-clock.el (org-clocking-buffer): Moved to org.el.
* lisp/org.el (org-clocking-buffer): Moved function.
(org-clock-is-active): Made into an alias.
---
lisp/org-clock.el | 4 ----
lisp/org.el | 6 +++---
2 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index c94f60781..2a6a9af47 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -607,10 +607,6 @@ cannot be translated."
((stringp drawer) drawer)
(t nil))))
-(defun org-clocking-buffer ()
- "Return the clocking buffer if we are currently clocking a task or nil."
- (marker-buffer org-clock-marker))
-
(defun org-clocking-p ()
"Return t when clocking a task."
(not (equal (org-clocking-buffer) nil)))
diff --git a/lisp/org.el b/lisp/org.el
index fd6226702..e35c19a9e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -144,7 +144,6 @@ Stars are put in group 1 and the trimmed body in group 2.")
(declare-function org-clock-timestamps-down "org-clock" (&optional n))
(declare-function org-clock-timestamps-up "org-clock" (&optional n))
(declare-function org-clock-update-time-maybe "org-clock" ())
-(declare-function org-clocking-buffer "org-clock" ())
(declare-function org-clocktable-shift "org-clock" (dir n))
(declare-function org-columns-quit "org-colview" ())
(declare-function org-columns-insert-dblock "org-colview" ())
@@ -3828,10 +3827,11 @@ This is needed for font-lock setup.")
"Marker recording the last clock-in, but the headline position.")
(defvar org-clock-heading ""
"The heading of the current clock entry.")
-(defun org-clock-is-active ()
+(defun org-clocking-buffer ()
"Return the buffer where the clock is currently running.
Return nil if no clock is running."
(marker-buffer org-clock-marker))
+(defalias 'org-clock-is-active #'org-clocking-buffer)
(defun org-check-running-clock ()
"Check if the current buffer contains the running clock.
@@ -8254,7 +8254,7 @@ function is being called interactively."
;; The clock marker is lost when using `sort-subr'; mark
;; the clock with temporary `:org-clock-marker-backup'
;; text property.
- (when (and (eq (org-clock-is-active) (current-buffer))
+ (when (and (eq (org-clocking-buffer) (current-buffer))
(<= start (marker-position org-clock-marker))
(>= end (marker-position org-clock-marker)))
(with-silent-modifications
--
2.30.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] Query when exiting with running clock
2021-03-03 2:08 ` Allen Li
@ 2021-03-04 3:54 ` Kyle Meyer
0 siblings, 0 replies; 4+ messages in thread
From: Kyle Meyer @ 2021-03-04 3:54 UTC (permalink / raw)
To: Allen Li; +Cc: Org Mode List
Allen Li writes:
> Thanks for your feedback. I have addressed your comments. Please see
> the new patches.
Thanks. Pushed with a few modifications and a commit on top that adds a
NEWS entry.
1: 39422ba4a ! 1: 303e7c28e org-clock: Query when exiting with running clock
@@ Commit message
* lisp/org-clock.el (org-clock-kill-emacs-query): New function.
(org-clock-ask-before-exiting): New user option.
+ [km: added package-version keyword, fixed function name typo]
+
## Notes (amlog) ##
Message-Id: <CADbSrJzNAwgTaPD1zUFVP=Ntuak2ccGBJvSKzW0gg3XxiykkOA@mail.gmail.com>
@@ lisp/org-clock.el: (defcustom org-clock-auto-clockout-timer 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]."
++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)
++ :type 'boolean
++ :package-version '(Org . "9.5"))
+
(defvar org-clock-in-prepare-hook nil
"Hook run when preparing the clock.
@@ lisp/org-clock.el: (defun org-clock-load ()
+(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)))
++ (let ((buf (org-clocking-buffer)))
+ (when (and buf (yes-or-no-p "Clock out and save? "))
+ (with-current-buffer buf
+ (org-clock-out)
2: d612adc77 = 2: 16b5ee0ef org-clock: Replace org-clocking-buffer with org-clock-is-active
-: --------- > 3: 4d463ee4b ORG-NEWS: Add entry for org-clock-ask-before-exiting
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-03-04 3:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-01-12 8:55 [PATCH] Query when exiting with running clock Allen Li
2021-02-03 5:45 ` Kyle Meyer
2021-03-03 2:08 ` Allen Li
2021-03-04 3:54 ` Kyle Meyer
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).