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: Tue, 09 Jan 2024 19:20:13 +0200 Organization: LINKOV.NET Message-ID: <86frz6o832.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> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2661"; 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 Tue Jan 09 18:31:31 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 1rNFwp-0000Qh-08 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 09 Jan 2024 18:31:31 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNFwP-0006yn-71; Tue, 09 Jan 2024 12:31:05 -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 1rNFwM-0006uA-8V for bug-gnu-emacs@gnu.org; Tue, 09 Jan 2024 12:31:02 -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 1rNFwF-0002VR-NJ for bug-gnu-emacs@gnu.org; Tue, 09 Jan 2024 12:30:59 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rNFwM-0006TW-8n for bug-gnu-emacs@gnu.org; Tue, 09 Jan 2024 12:31: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: Tue, 09 Jan 2024 17:31: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.170482145624379 (code B ref 67993); Tue, 09 Jan 2024 17:31:02 +0000 Original-Received: (at 67993) by debbugs.gnu.org; 9 Jan 2024 17:30:56 +0000 Original-Received: from localhost ([127.0.0.1]:40848 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rNFwF-0006Kh-Vv for submit@debbugs.gnu.org; Tue, 09 Jan 2024 12:30:56 -0500 Original-Received: from relay7-d.mail.gandi.net ([2001:4b98:dc4:8::227]:39575) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rNFwD-00064b-Uv for 67993@debbugs.gnu.org; Tue, 09 Jan 2024 12:30:54 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id CCDBC20005; Tue, 9 Jan 2024 17:30:37 +0000 (UTC) In-Reply-To: <1ea06837-0d7e-46ba-849c-a4bf42929c40@gmx.at> (martin rudalics's message of "Mon, 8 Jan 2024 09:55:58 +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:277638 Archived-At: >>> In 'display-buffer' first save the selected window as >>> old-selected-window, display the buffer and in your code select >>> old-selected-window instead of (old-selected-window). >> >> Unfortunately, this is not so easy to do. 'old-selected-window' >> should be reinitialized before the next command is executed. > > Why? As I imagine it, it would be let-bound in 'display-buffer'. Ah, indeed, this is even better. This shows differences from the previous patch: diff --git a/lisp/window.el b/lisp/window.el index 5c3e68f04eb..f34b6d625c6 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -7859,13 +7859,14 @@ display-buffer (setq window (funcall (car functions) buffer alist) functions (cdr functions))) (when-let ((select-window (assq 'select-window alist))) - (letrec ((postfun + (letrec ((old-selected-window (selected-window)) + (postfun (lambda () (if (cdr select-window) (when (window-live-p window) (select-window window)) - (when (window-live-p (old-selected-window)) - (select-window (old-selected-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)))) >> So by definition 'old-selected-window' should contain >> the window that was selected before the current command >> was executed. I have no idea how to do this without hooks. > > Hmmm... I have problems to see what the "current command" is. One and > the same command may contain multiple invocations of 'display-buffer'. > I thought you wanted a 'select-window' entry to be handled separately by > each of them. Otherwise, IIUC the entry provided by the last invocation > would prevail any entries provided by previous invocations. How would > you explain that to a user? > > If you want 'old-selected-window' to denote "some" state before the > "current command", the function 'old-selected-window' might be a better > choice than a variable you bind in 'display-buffer' (but note that > redisplay may occur in the middle of executing a command). I guess the variable above should be fine. > But if you want to interpret "current command" more strictly, you need a > separate variable, say 'pre-command-selected-window' that you always set > in 'pre-command-hook' and consult (and maybe reset) in > 'post-command-hook'. This would be more troublesome. >> 'pop-to-buffer' can't be changed because (select-window . t) >> should be handled only at the end of the current command. > > Are your sure that you do not somewhat arbitrarily restrict the scope of > this feature? What if (select-window . t) were to be handled in a call > from within a timer? Would you ignore it? It would be a good design to ignore (select-window . t) from a hook. When windows pop up at random time while user is typing, this is too dangerous to allow. I remember such annoying popups as e.g. with a button "[D]elete" with the shortcut "D" while you are typing a text that contains "D" ;-) >> So this also need to run 'select-frame' in post-command-hook. > > 'select-frame-set-input-focus' maybe. Or, what's worse, undo the > consequences of a preceding 'select-frame-set-input-focus' call > triggered by 'pop-to-buffer'. Ok, I will try to save and restore 'old-selected-frame' as well. > That's why any such 'select-window' call (or its avoidance) you > propose would be better handled within 'display-buffer' and not later > in a 'post-command-hook'. No way to call 'select-window' immediately, because this will break too many functions that expect a window to be selected from the previous call of pop-to-buffer until the command is finished.