From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#67993: Selecting buffer automatically Date: Sun, 14 Jan 2024 20:48:07 +0200 Organization: LINKOV.NET Message-ID: <8634uzg6zc.fsf@mail.linkov.net> References: <86zfy0g641.fsf@mail.linkov.net> <865y09nmp0.fsf@mail.linkov.net> <4659812e-c023-492a-b810-d9d3cada1ade@gmx.at> <861qauxswd.fsf@mail.linkov.net> <86cyudjdmb.fsf@mail.linkov.net> <1ea06837-0d7e-46ba-849c-a4bf42929c40@gmx.at> <86frz6o832.fsf@mail.linkov.net> <942ced7b-53f8-4b33-9280-6cdba4b6ed6b@gmx.at> <867ckhqgqp.fsf@mail.linkov.net> <86y1cvkpm3.fsf@mail.linkov.net> <2edb97dc-bbf6-4020-b4ae-8188082c80c6@gmx.at> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24024"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 67993@debbugs.gnu.org To: martin rudalics Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jan 14 19:56:24 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rP5ei-00064Q-Af for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 14 Jan 2024 19:56:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rP5eR-0006a7-GQ; Sun, 14 Jan 2024 13:56:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rP5eP-0006Sq-7t for bug-gnu-emacs@gnu.org; Sun, 14 Jan 2024 13:56:05 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rP5eN-0000zI-5a for bug-gnu-emacs@gnu.org; Sun, 14 Jan 2024 13:56:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rP5eM-0007y9-8Z for bug-gnu-emacs@gnu.org; Sun, 14 Jan 2024 13:56:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 14 Jan 2024 18:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67993 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 67993-submit@debbugs.gnu.org id=B67993.170525853530586 (code B ref 67993); Sun, 14 Jan 2024 18:56:02 +0000 Original-Received: (at 67993) by debbugs.gnu.org; 14 Jan 2024 18:55:35 +0000 Original-Received: from localhost ([127.0.0.1]:44089 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rP5du-0007xG-I4 for submit@debbugs.gnu.org; Sun, 14 Jan 2024 13:55:34 -0500 Original-Received: from relay3-d.mail.gandi.net ([217.70.183.195]:44229) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rP5ds-0007wz-Vk for 67993@debbugs.gnu.org; Sun, 14 Jan 2024 13:55:33 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id A2CB560002; Sun, 14 Jan 2024 18:55:26 +0000 (UTC) In-Reply-To: <2edb97dc-bbf6-4020-b4ae-8188082c80c6@gmx.at> (martin rudalics's message of "Sat, 13 Jan 2024 11:39:10 +0100") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:278232 Archived-At: --=-=-= Content-Type: text/plain >> 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: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=post-command-select-window.patch 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) --=-=-=--