unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: martin rudalics <rudalics@gmx.at>
Cc: 67993@debbugs.gnu.org
Subject: bug#67993: Selecting buffer automatically
Date: Sun, 14 Jan 2024 20:48:07 +0200	[thread overview]
Message-ID: <8634uzg6zc.fsf@mail.linkov.net> (raw)
In-Reply-To: <2edb97dc-bbf6-4020-b4ae-8188082c80c6@gmx.at> (martin rudalics's message of "Sat, 13 Jan 2024 11:39:10 +0100")

[-- Attachment #1: Type: text/plain, Size: 1123 bytes --]

>> But actually the customization for "1" doesn't affect popping to "2"
>> because pop-to-buffer still selects windows and creates such layout:
>>
>>    *scratch*
>>    Buffer "1"
>>    Buffer "2"
>>
>> Only post-command-hook selects the original window afterwards
>> instead of leaving the buffer "2" selected.
>
> And if the customization for "2" meant to select the window, the final
> outcome will depend on whichever (de-)selection comes last in the
> 'post-command-hook'.  You should at least document that in the manual.
> It looks pretty fragile to me.

A practical use of this feature is for commands that display a single buffer
that is an overwhelming majority of the commands.

However, the name 'select-window' would be too confusing for users
because based on such name they will expect that it will select
the window immediately after it's displayed.

Therefore I propose to rename it to the less ambiguous name
'post-command-select-window'.  Of course it should be documented in
any case, but this will help the users who never read the documentation.

Ok, here is a complete patch with the documentation:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: post-command-select-window.patch --]
[-- Type: text/x-diff, Size: 2742 bytes --]

diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi
index 93b25cbe67f..ecb1df82eba 100644
--- a/doc/lispref/windows.texi
+++ b/doc/lispref/windows.texi
@@ -3344,6 +3344,15 @@ Buffer Display Action Alists
 the entries @code{window-height}, @code{window-width} and
 @code{preserve-size} are applied that could resize the window to fit
 it to the inserted contents.
+
+@vindex post-command-select-window@r{, a buffer display action alist entry}
+@item post-command-select-window
+If the value is non-@code{nil}, the buffer displayed by @code{display-buffer}
+is selected after the current command is executed that runs the hook
+@code{post-command-hook} (@pxref{Command Overview}).
+If the value is @code{nil}, the buffer selected by such functions as
+@code{pop-to-buffer} is deselected, and the window that was previously
+selected before calling @code{display-buffer} remains selected.
 @end table
 
 By convention, the entries @code{window-height}, @code{window-width}
diff --git a/lisp/window.el b/lisp/window.el
index 23977691f50..b37502203b1 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -7798,6 +7803,13 @@ display-buffer
     and `preserve-size' are applied.  The function is supposed
     to fill the window body with some contents that might depend
     on dimensions of the displayed window.
+ `post-command-select-window' -- A non-nil value means that after the
+    current command is executed and the hook `post-command-hook' is called,
+    the window displayed by this function will be selected.  A nil value
+    means that if functions like `pop-to-buffer' selected another window,
+    at the end of this command that window will be deselected, and the
+    window that was selected before calling this function will remain
+    selected.
 
 The entries `window-height', `window-width', `window-size' and
 `preserve-size' are applied only when the window used for
@@ -7853,6 +7865,17 @@ display-buffer
       (while (and functions (not window))
         (setq window (funcall (car functions) buffer alist)
               functions (cdr functions)))
+      (when-let ((select (assq 'post-command-select-window alist)))
+        (letrec ((old-selected-window (selected-window))
+                 (postfun
+                  (lambda ()
+                    (if (cdr select)
+                        (when (window-live-p window)
+                          (select-window window))
+                      (when (window-live-p old-selected-window)
+                        (select-window old-selected-window)))
+                    (remove-hook 'post-command-hook postfun))))
+          (add-hook 'post-command-hook postfun)))
       (and (windowp window) window))))
 
 (defun display-buffer-other-frame (buffer)

  reply	other threads:[~2024-01-14 18:48 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-23 17:51 bug#67993: Selecting buffer automatically Juri Linkov
2024-01-04 17:12 ` Juri Linkov
2024-01-05  9:23   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-06 17:40     ` Juri Linkov
2024-01-07 14:56       ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-07 16:51         ` Juri Linkov
2024-01-08  8:55           ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-09 17:20             ` Juri Linkov
2024-01-10  8:37               ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-10 17:12                 ` Juri Linkov
2024-01-11  9:15                   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-12  7:46                     ` Juri Linkov
2024-01-13 10:39                       ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-14 18:48                         ` Juri Linkov [this message]
2024-01-15 10:23                           ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-15 17:56                             ` Juri Linkov
2024-01-16 10:18                               ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-16 16:54                                 ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8634uzg6zc.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=67993@debbugs.gnu.org \
    --cc=rudalics@gmx.at \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).