Subject: Re: bug#46151: 28.0.50; Set revert-buffer-function in shell command output buffers

From: Sean Whitton <[email-masked]>
Date: Fri, 29 Jan 2021 00:05:37 -0700

On Fri 29 Jan 2021 at 06:52AM +01, Lars Ingebrigtsen wrote:

> Sean Whitton <[email-masked]> writes:
>
>> Inspired by Stefan's comment at [1] I came up with this patch to set
>> revert-buffer-function in shell command buffers.
>
> Nice; I've applied your patch to Emacs 28.

Thanks for taking a look!

I realised that my patch needs to setq-local not just setq.  Indeed, my
previous patch breaks revert-buffer once you've run a shell command...

Attached is a fix.

>> I have a binding for revert-buffer which is nicer to press than M-! M-p
>> RET or M-& M-p RET when repeatedly editing and re-running programs and
>> scripts.
>
> Would it make sense to just define a tiny mode that just defines the `g'
> command to revert and use that in these buffers?

For M-!, yes, that would be useful and simple.

For M-&, the mode would need to be activated only once the command had
finished running, as otherwise 'g' should be sent to the inferior
process.  Not hard to do that either.

I take it a minor mode is more appropriate than a major mode for this?
If so, I can come up with a patch adding that to simple.el.

-- 
Sean Whitton
>From 21499b416759b77a5d0ef41cb4f68932d36b9b63 Mon Sep 17 00:00:00 2001
From: Sean Whitton <[email-masked]>
Date: Fri, 29 Jan 2021 00:02:08 -0700
Subject: [PATCH] ; Fix previous commit regarding revert-buffer-function

* simple.el (shell-command, shell-command-on-region): Set
revert-buffer-function buffer-locally, not globally.  Also, avoid an
unnecessary call to (current-buffer) by taking advantage of the
closure.
---
 lisp/simple.el | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index 64ee042135..742fc5004d 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3991,9 +3991,9 @@ impose the use of a shell (with its need to quote arguments)."
 			  (start-process-shell-command "Shell" buffer command)))
 		  (setq mode-line-process '(":%s"))
                   (shell-mode)
-                  (setq revert-buffer-function
-                        (lambda (&rest _)
-                          (async-shell-command command (current-buffer))))
+                  (setq-local revert-buffer-function
+                              (lambda (&rest _)
+                                (async-shell-command command buffer)))
                   (set-process-sentinel proc #'shell-command-sentinel)
 		  ;; Use the comint filter for proper handling of
 		  ;; carriage motion (see comint-inhibit-carriage-motion).
@@ -4260,9 +4260,9 @@ characters."
                                              buffer))))
             ;; Report the output.
             (with-current-buffer buffer
-              (setq revert-buffer-function
-                    (lambda (&rest _)
-                      (shell-command command)))
+              (setq-local revert-buffer-function
+                          (lambda (&rest _)
+                            (shell-command command)))
               (setq mode-line-process
                     (cond ((null exit-status)
                            " - Error")
-- 
2.29.2