From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id ECCXKTlk/V/GAQAA0tVLHw (envelope-from ) for ; Tue, 12 Jan 2021 08:56:25 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id UGZqJTlk/V8cGwAAB5/wlQ (envelope-from ) for ; Tue, 12 Jan 2021 08:56:25 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id EAB66940418 for ; Tue, 12 Jan 2021 08:56:24 +0000 (UTC) Received: from localhost ([::1]:59990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kzFTT-0002Go-Gn for larch@yhetil.org; Tue, 12 Jan 2021 03:56:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54090) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kzFSp-0002Ep-MB for emacs-orgmode@gnu.org; Tue, 12 Jan 2021 03:55:43 -0500 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]:43200) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kzFSn-0008WV-CB for emacs-orgmode@gnu.org; Tue, 12 Jan 2021 03:55:43 -0500 Received: by mail-pg1-x533.google.com with SMTP id n10so988390pgl.10 for ; Tue, 12 Jan 2021 00:55:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=felesatra-moe.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version; bh=ASeN3GsC23wM7PudSX/kjMSelAuukIOYELj43dMGRsA=; b=OSUvpqoiFam964K/UBwvio8EkdcT5Isd4jtnvf/oWYrECHo+zC/ouiLEy5/dvxqR0V JuFt/5QzXj7Qiv5UpSgvHliWU7zh9+Aiwnn30kxpd1W9xapDrzkPonHQEwrnKQ+xPBm9 xDEvfoC8l2VXa/NO94dl8c/BVD5VrwNuiXtY8yPzPCRtpfNkY+WTuBCZthA0727GhjDm ei3vZuS1ZgpIVeH+eHEI9KOiMKCx3qK5r7KvOnOBHaq7M4j1ROlEYkJmGq63TTVME11u 5H12KJRWT4MIQNgN9kdpEjjpby36MDN+smt9Mb2VNDl/r/jAA/b7bys8oNCP0oPutELn GWUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=ASeN3GsC23wM7PudSX/kjMSelAuukIOYELj43dMGRsA=; b=XwVTfF0y4tdiUdl/+iyGG0hF5R+h18098DFJporf4ugek8LJNPooEBAAKZyeXSRuLB HP7M2ViGqVo9bW/okWecdnCgdI9eMHja2kOo1YN/uSxhJFfl4I5ZHcCy/2XPDbaeCuuF QNh1PoGdgX5A/ZJQv0C7pWTeMcXz8SydhD6lF2bUP6AIFgFAmz8KTvFLwdpP2GG3tdu1 qEAs7tLKboy58uQNXzvcFEwa2PX5J9akqCUycdBJTiJKJZ3fUycFA4uK16vhsOJDce7M 1VGLVaiK1wak1Ohe4BBKARMAsxos/A0EZa2jBrxDRpDUxrBUkbtFw7NiTfpWGQ3WKSz4 lNLw== X-Gm-Message-State: AOAM530X75QkMGPoXl1qMFrRoI4JBVqzHPIW6rb4JWmbQYZa9o92L3SH aTzSjeCOE7oUlUM4Eil/DvqlAgZXVZfNrg== X-Google-Smtp-Source: ABdhPJx7obmDPfn5xFzPSlJI3x+DPQNrpM6n3TmkhiYA/uBkaXRNX5Z19L37Mw7LAxL6TglcqpomTg== X-Received: by 2002:a62:b410:0:b029:1a4:7868:7e4e with SMTP id h16-20020a62b4100000b02901a478687e4emr4011660pfn.62.1610441739175; Tue, 12 Jan 2021 00:55:39 -0800 (PST) Received: from localhost ([2600:1700:7270:7d1f:fa59:71ff:fe00:10cc]) by smtp.gmail.com with ESMTPSA id f7sm2257031pjs.25.2021.01.12.00.55.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jan 2021 00:55:38 -0800 (PST) From: Allen Li To: emacs-orgmode@gnu.org Subject: [PATCH] Query when exiting with running clock Date: Tue, 12 Jan 2021 00:55:37 -0800 Message-ID: <80o8hu356e.fsf@felesatra.moe> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=darkfeline@felesatra.moe; helo=mail-pg1-x533.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -2.56 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=felesatra-moe.20150623.gappssmtp.com header.s=20150623 header.b=OSUvpqoi; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: EAB66940418 X-Spam-Score: -2.56 X-Migadu-Scanner: scn0.migadu.com X-TUID: eLmYdP0fxKJo --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-org-clock-Replace-org-clocking-buffer-with-org-clock.patch >From f6145afd7d457cec533c44c8a3297d28f11d7255 Mon Sep 17 00:00:00 2001 From: Allen Li 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-org-clock-Query-when-exiting-with-running-clock.patch >From d12b01955773be64d04bd391d822cf7d1b67f637 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. --- 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 --=-=-=--