Subject: Re: bug#65719: [PATCH] Pass restart argument to 'kill-emacs-query-functions'

From: Ellis Kenyo <[email-masked]>
Date: Mon, 12 Feb 2024 15:29:50 +0000

One patch hot and fresh out the oven :)

Hope we can get this one in!

Thanks,

Ellis
From 7b18b01b434003e6f25fe8b71d510464b8ac0afd Mon Sep 17 00:00:00 2001
From: Ellis Kenyo <[email-masked]>
Date: Mon, 12 Feb 2024 15:25:25 +0000
Subject: [PATCH] Introduce 'restart-emacs-query-functions'

* etc/NEWS: Update NEWS file.

* lisp/files.el (save-buffers-kill-emacs): Add new option
'restart-emacs-query-functions' for functions that should be called
when 'restart' is passed.

* doc/lispref/os.texi: Document 'restart-emacs-query-functions' and
adjust 'restart-emacs' to reflect new change.
---
 doc/lispref/os.texi | 12 +++++++++++-
 etc/NEWS            |  7 +++++++
 lisp/files.el       | 12 ++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 60ae57d4c1d..5533edcddef 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -762,11 +762,21 @@ Killing Emacs
 directly does not run this hook.
 @end defopt
 
+@defopt restart-emacs-query-functions
+Similar to how @code{kill-emacs-query-functions} is called, when the
+@code{restart} flag is passed; these hooks trigger. Calling
+@code{restart-emacs} is one such way to ensure this flag is passed.
+
+They obey the same rules as @code{kill-emacs-query-functions}, and as
+such one should reference those rules above.
+@end defopt
+
 @deffn Command restart-emacs
 This command does the same as @code{save-buffers-kill-emacs}, but
 instead of just killing the current Emacs process at the end, it'll
 restart a new Emacs process, using the same command line arguments as
-the currently running Emacs process.
+the currently running Emacs process and also trigger all the functions
+defined in @code{restart-emacs-query-functions}.
 @end deffn
 
 @node Suspending Emacs
diff --git a/etc/NEWS b/etc/NEWS
index afc2c22e68b..da7ce45e87b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -538,6 +538,13 @@ you can add this to your init script:
 
     (setopt project-switch-commands #'project-prefix-or-any-command)
 
+** Files
+
+*** New user option 'restart-emacs-query-functions'.
+Equivalent to 'kill-emacs-query-functions', this hook is called when
+'save-buffers-kill-emacs' is called with the 'restart' flag; if
+invoked from say 'M-x restart-emacs'.
+
 ** VC
 
 ---
diff --git a/lisp/files.el b/lisp/files.el
index f67b650cb92..9704d818310 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -8252,6 +8252,15 @@ kill-emacs-query-functions
   :version "26.1"
   :group 'convenience)
 
+(defcustom restart-emacs-query-functions nil
+  "Functions to call with no arguments to query about restarting
+Emacs. If any of these functions returns nil, restarting Emacs is
+canceled. `save-buffers-kill-emacs' calls these functions when passed
+`restart'.  See also `kill-emacs-hook'."
+  :type 'hook
+  :version "30.1"
+  :group 'convenience)
+
 (defcustom confirm-kill-emacs nil
   "How to ask for confirmation when leaving Emacs.
 If nil, the default, don't ask at all.  If the value is non-nil, it should
@@ -8337,6 +8346,9 @@ save-buffers-kill-emacs
                             (yes-or-no-p "Active processes exist; kill them and exit anyway? "))
                         (when (window-live-p window)
                           (quit-restore-window window 'kill)))))))))
+     ;; Only call these when restarting
+     (when restart
+       (run-hook-with-args-until-failure 'restart-emacs-query-functions))
      ;; Query the user for other things, perhaps.
      (run-hook-with-args-until-failure 'kill-emacs-query-functions)
      (or (null confirm)
-- 
2.43.0