Subject: Re: bug#33870: 27.0.50; xref-goto-xref not configurable

From: Juri Linkov <[email-masked]>
Date: Thu, 03 Jan 2019 02:18:50 +0200

Hi João

> Any simplification to the implementation that keeps the
> "keep original window intent" behavior across xref
> intermediate buffers is very welcome.

Thanks for the explanation.  Now I understand better the intent in
xref--show-pos-in-buf.  Generally, I'd like to see the “keep original
window intent” behavior in more places, e.g. in *Occur*, *grep*, etc.
Based on your explanation, I've been able to write the patch that does
the following:

1. simplifies ‘xref--show-pos-in-buf’ while at the same time
   preserves the current behavior and respects user's customization
   of display actions;

2. makes the xref buffer non-obtrusive like *Completions*
   in xref--show-xref-buffer;

3. turns the existing arg QUIT of xref-goto-xref into a prefix arg,
   so a natural key sequence ‘C-u RET’ will quit the window.
   This is similar to the prefix arg of quit-window.

diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 87ce2299c5..a166f8299c 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -482,19 +482,9 @@ xref--show-pos-in-buf
                   (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
-        (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))
+                 `(,(lambda (buf alist)
+                      (window--display-buffer buf xref--original-window 'reuse alist)))))))
+    (with-selected-window (display-buffer buf action)
       (xref--goto-char pos)
       (run-hooks 'xref-after-jump-hook)
       (let ((buf (current-buffer)))
@@ -548,9 +538,8 @@ xref--item-at-point
 
 (defun xref-goto-xref (&optional quit)
   "Jump to the xref on the current line and select its window.
-Non-interactively, non-nil QUIT means to first quit the *xref*
-buffer."
-  (interactive)
+A prefix arg QUIT means to first quit the *xref* buffer."
+  (interactive "P")
   (let* ((buffer (current-buffer))
          (xref (or (xref--item-at-point)
                    (user-error "No reference at point")))
@@ -782,7 +771,17 @@ xref--show-xref-buffer
         (erase-buffer)
         (xref--insert-xrefs xref-alist)
         (xref--xref-buffer-mode)
-        (pop-to-buffer (current-buffer))
+        (pop-to-buffer
+         (current-buffer)
+         `((display-buffer--maybe-same-window
+            display-buffer-reuse-window
+            display-buffer--maybe-pop-up-frame
+            display-buffer-below-selected)
+	   ,(if temp-buffer-resize-mode
+		'(window-height . resize-temp-buffer-window)
+	      '(window-height . fit-window-to-buffer))
+	   ,(when temp-buffer-resize-mode
+	      '(preserve-size . (nil . t)))))
         (goto-char (point-min))
         (setq xref--original-window (assoc-default 'window alist)
               xref--original-window-intent (assoc-default 'display-action alist))