unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#28814: 26.0.90; When *xref* window is needed, original window-switching intent is lost
@ 2017-10-13 16:07 João Távora
       [not found] ` <handler.28814.B.150791088020837.ack@debbugs.gnu.org>
  0 siblings, 1 reply; 38+ messages in thread
From: João Távora @ 2017-10-13 16:07 UTC (permalink / raw)
  To: 28814, dgutov

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

Hi Dmitry, maintainters,

Here are two patches to fix what I believe is a small but annoying bug
in xref.el.

I'll try to explain as clearly as possible: As you know, if the user
presses 'M-.' in a single-ref definition, he/she is transported to a new
buffer.

But there are other situations: when xref-find-definitions finds more
than one definition, a list is shown in an *xref* buffer, normally in a
new window. When the user selects an "xref" with xref-goto-xref, a
buffer and window switch happen.  Anyway, so far so good.

The problem is there is also 'C-x 4 .' and 'C-x 5 .' for
xref-find-definitions-other-window and xref-find-definitions-other-frame
respectively. These work just fine when an *xref* buffer isn't needed,
but when it is, the original intent of using another window or frame
will be lost when the user eventually selects a definition.

It shouldn't be so, in my opinion.

The first patch I attach (0001-Honor-window....patch) fixes this bug. I
hope it is readable enough but I can explain how it works in detail.

I also attach a second patch (0002-Quit-the....patch), that does not
really fix a bug, but changes the behavior of xref-goto-xref to
something much nicer: it quits the *xref* window before going to the
reference.

This brings a nice result: As always 'M-.' switches buffers if there is
only one definition.  Now, if there is more than one, the final state
after selecting one of these definitions is the same as if there had
only been one in the first place.  I think this makes sense because it
preserves the expectations of the user who probably wants M-. to behave
as predictably as possible.

Here's hoping you're not really confused by this report,
João


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Honor-window-switching-intents-in-xref-find-definiti.patch --]
[-- Type: text/x-diff, Size: 5872 bytes --]

From 1cba860d6a2c45e0fa690065b2bf4e6658e87628 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= <joaotavora@gmail.com>
Date: Fri, 13 Oct 2017 15:13:14 +0100
Subject: [PATCH 1/2] Honor window-switching intents in xref-find-definitions

When there is more than one xref to jump to, and an *xref* window
appears to help the user choose, the original intent to open a
definition another window or frame is remembered when the choice to go
to or show a reference is finally made.

* lisp/progmodes/xref.el (xref--show-pos-in-buf): Rewrite.
(xref--original-window-intent): New variable.
(xref--original-window): Rename from xref--window and move up
here for clarity.
(xref--show-pos-in-buf): Rewrite.  Don't take SELECT arg here.
(xref--show-location): Handle window selection decision here.
(xref--window): Rename to xref--original-window.
(xref-show-location-at-point): Don't attempt window management here.
(xref--show-xrefs): Ensure display-action intent is saved.
---
 lisp/progmodes/xref.el | 73 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 52 insertions(+), 21 deletions(-)

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 80cdcb3f18..768fa15a6b 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -449,43 +449,72 @@ xref--with-dedicated-window
        (when xref-w
          (set-window-dedicated-p xref-w xref-w-dedicated)))))
 
-(defun xref--show-pos-in-buf (pos buf select)
-  (let ((xref-buf (current-buffer))
-        win)
+(defvar-local xref--original-window-intent nil
+  "Original window-switching intent before xref buffer creation.")
+
+(defvar-local xref--original-window nil
+  "The original window this xref buffer was created from.")
+
+(defun xref--show-pos-in-buf (pos buf)
+  "Goto and display position POS of buffer BUF in a window.
+Honour `xref--original-window-intent', run `xref-after-jump-hook'
+and finally return the window."
+  (let* ((xref-buf (current-buffer))
+         (pop-up-frames
+          (or (eq xref--original-window-intent 'frame)
+              pop-up-frames))
+         (action
+          (cond ((memq
+                  xref--original-window-intent
+                  '(window frame))
+                 t)
+                ((and
+                  (window-live-p xref--original-window)
+                  (or (not (window-dedicated-p xref--original-window))
+                      (eq (window-buffer xref--original-window) buf)))
+                 `(,(lambda (buf _alist)
+                      (set-window-buffer xref--original-window buf)
+                      xref--original-window))))))
     (with-selected-window
-        (xref--with-dedicated-window
-         (display-buffer buf))
+        (with-selected-window
+            ;; Just before `display-buffer', place ourselves in the
+            ;; original window to suggest preserving it. Of course, if
+            ;; user has deleted the original window, all bets are off,
+            ;; just use the selected one.
+            (or (and (window-live-p xref--original-window)
+                     xref--original-window)
+                (selected-window))
+          (display-buffer buf action))
       (xref--goto-char pos)
       (run-hooks 'xref-after-jump-hook)
       (let ((buf (current-buffer)))
-        (setq win (selected-window))
         (with-current-buffer xref-buf
-          (setq-local other-window-scroll-buffer buf))))
-    (when select
-      (select-window win))))
+          (setq-local other-window-scroll-buffer buf)))
+      (selected-window))))
 
 (defun xref--show-location (location &optional select)
   (condition-case err
       (let* ((marker (xref-location-marker location))
              (buf (marker-buffer marker)))
-        (xref--show-pos-in-buf marker buf select))
+        (cond (select
+               (select-window (xref--show-pos-in-buf marker buf)))
+              (t
+               (save-selected-window
+                 (xref--with-dedicated-window
+                  (let (;; save-selected-window doesn't resist frame
+                        ;; raises
+                        (display-buffer-overriding-action
+                         '(nil . ((inhibit-switch-frame . t)))))
+                    (xref--show-pos-in-buf marker buf)))))))
     (user-error (message (error-message-string err)))))
 
-(defvar-local xref--window nil
-  "The original window this xref buffer was created from.")
-
 (defun xref-show-location-at-point ()
   "Display the source of xref at point in the appropriate window, if any."
   (interactive)
   (let* ((xref (xref--item-at-point))
          (xref--current-item xref))
     (when xref
-      ;; Try to avoid the window the current xref buffer was
-      ;; originally created from.
-      (if (window-live-p xref--window)
-          (with-selected-window xref--window
-            (xref--show-location (xref-item-location xref)))
-        (xref--show-location (xref-item-location xref))))))
+      (xref--show-location (xref-item-location xref)))))
 
 (defun xref-next-line ()
   "Move to the next xref and display its source in the appropriate window."
@@ -727,7 +756,8 @@ xref--show-xref-buffer
         (xref--xref-buffer-mode)
         (pop-to-buffer (current-buffer))
         (goto-char (point-min))
-        (setq xref--window (assoc-default 'window alist))
+        (setq xref--original-window (assoc-default 'window alist)
+              xref--original-window-intent (assoc-default 'display-action alist))
         (current-buffer)))))
 
 \f
@@ -754,7 +784,8 @@ xref--show-xrefs
    (t
     (xref-push-marker-stack)
     (funcall xref-show-xrefs-function xrefs
-             `((window . ,(selected-window)))))))
+             `((window . ,(selected-window))
+               (display-action . ,display-action))))))
 
 (defun xref--prompt-p (command)
   (or (eq xref-prompt-for-identifier t)
-- 
2.11.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Quit-the-xref-window-if-user-decides-to-go-to-a-ref.patch --]
[-- Type: text/x-diff, Size: 1248 bytes --]

From 228cb812197bd68b2fb6eccc60d7956675a728f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= <joaotavora@gmail.com>
Date: Fri, 13 Oct 2017 16:37:47 +0100
Subject: [PATCH 2/2] Quit the *xref* window if user decides to go to a ref

* lisp/progmodes/xref.el (xref--show-location): When SELECT is
t, quit window.
---
 lisp/progmodes/xref.el | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 768fa15a6b..3a5e9e53ed 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -495,9 +495,12 @@ xref--show-pos-in-buf
 (defun xref--show-location (location &optional select)
   (condition-case err
       (let* ((marker (xref-location-marker location))
-             (buf (marker-buffer marker)))
+             (buf (marker-buffer marker))
+             (xref-buffer (current-buffer)))
         (cond (select
-               (select-window (xref--show-pos-in-buf marker buf)))
+               (quit-window nil nil)
+               (with-current-buffer xref-buffer
+                 (select-window (xref--show-pos-in-buf marker buf))))
               (t
                (save-selected-window
                  (xref--with-dedicated-window
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2017-11-03 19:06 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-13 16:07 bug#28814: 26.0.90; When *xref* window is needed, original window-switching intent is lost João Távora
     [not found] ` <handler.28814.B.150791088020837.ack@debbugs.gnu.org>
2017-10-16 17:58   ` bug#28814: Acknowledgement (26.0.90; When *xref* window is needed, original window-switching intent is lost ) João Távora
2017-10-20  9:13   ` bug#28814: [BUMP, PATCH] " João Távora
2017-10-20 10:39     ` Dmitry Gutov
2017-10-23  2:06     ` Dmitry Gutov
2017-10-23  8:23       ` João Távora
2017-10-23 20:03         ` João Távora
2017-10-25  0:24           ` Dmitry Gutov
2017-10-25  7:43             ` João Távora
2017-10-25 10:24               ` Dmitry Gutov
2017-10-25 13:29                 ` João Távora
2017-10-25 14:49                   ` Dmitry Gutov
2017-10-25 15:35                     ` João Távora
2017-10-25 15:46                       ` Dmitry Gutov
2017-10-25 15:11             ` Eli Zaretskii
2017-10-25 15:27               ` João Távora
2017-10-25 15:39                 ` Eli Zaretskii
2017-10-25 20:56                   ` João Távora
2017-10-26  7:56                     ` martin rudalics
2017-10-26 16:42                     ` Eli Zaretskii
2017-10-26 22:40                       ` Dmitry Gutov
2017-10-27  0:05                         ` João Távora
2017-11-02 17:06                           ` Dmitry Gutov
2017-10-26 23:59                       ` João Távora
2017-10-28  9:41                         ` Eli Zaretskii
2017-10-28 19:19                           ` João Távora
2017-11-02 17:03                             ` João Távora
2017-11-02 17:07                               ` Eli Zaretskii
2017-11-02 19:07                                 ` João Távora
2017-11-02 19:32                                   ` Eli Zaretskii
2017-11-03 13:47                             ` Eli Zaretskii
2017-11-03 16:18                               ` João Távora
2017-11-03 19:06                                 ` Eli Zaretskii
2017-10-26 12:39                   ` Dmitry Gutov
2017-10-25  7:46           ` martin rudalics
2017-10-25 13:19             ` João Távora
2017-10-26  7:56               ` martin rudalics
2017-10-25  0:07         ` Dmitry Gutov

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).