Subject: bug#38111: 27.0.50; globalized-minor-mode not enabled on get-buffer-create

From: Juri Linkov <[email-masked]>
Date: Sun, 08 Dec 2019 00:56:00 +0200

reopen 38111
tags 38111 - notabug
quit

>> Sounds like globalized-minor-mode machinery somehow doesn't support
>> the latter method of creating a buffer?
>
> Yep, globalized modes use `after-change-major-mode-hook' to call the
> buffer-local minor mode, and that hook is not triggered in these cases
> because, although new buffers are in `fundamental-mode' by default,
> the `fundamental-mode' function is not actually called, and so no mode
> change occurs.
>
> I believe this has cropped up in past discussion, but I don't recall
> where offhand.
>
> I agree that it's surprising when you are expecting the global mode(s)
> to be enabled (I first realised it when attempting to use a globalized
> mode for my custom key bindings, and discovering they were not being
> enabled in various buffers).
>
> If this were changed then there *might* be undesirable consequences to
> having lots of created-behind-the-scenes buffers suddenly doing things
> they never did before.

Actually that turned out to be a problem in practice, e.g. with this recipe:

0. emacs -Q
1. M-x global-display-fill-column-indicator-mode RET

2. M-& ls -la RET  ;; or any other command that produces enough lines
3. observe that the global mode is activated in the output buffer

4. M-! ls -la RET
5. the global mode is NOT activated in the output buffer

The difference between these two is that shell-command-on-region uses just

  (get-buffer-create "*Shell Command Output*")

and doesn't set its default fundamental-mode explicitly.

The following patch fixes this, but I'm still not sure if we need
to grep all occurrences of get-buffer-create and add set-buffer-major-mode
everywhere?

diff --git a/lisp/simple.el b/lisp/simple.el
index 67ddab3d34..1c4fdf9945 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3925,6 +3926,7 @@ shell-command-on-region
         ;; replacing its entire contents.
         (let ((buffer (get-buffer-create
                        (or output-buffer "*Shell Command Output*"))))
+          (set-buffer-major-mode buffer)
           (unwind-protect
               (if (and (eq buffer (current-buffer))
                        (or (not shell-command-dont-erase-buffer)