Subject: Re: bug#35737: xref--original-command

From: Dmitry Gutov <[email-masked]>
Date: Tue, 28 May 2019 02:21:45 +0300

To be clear, here's the kind of change I was thinking of.

Though maybe instead of changing xref--show-defs-buffer, we'd add a new 
function named like xref--show-defs-buffer-below.
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 58c0119a54..80f80cdd75 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -704,6 +704,16 @@ xref--xref-buffer-mode
   (setq next-error-function #'xref--next-error-function)
   (setq next-error-last-buffer (current-buffer)))
 
+(defvar xref--definitions-buffer-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "RET") #'xref-quit-and-goto-xref)
+    (set-keymap-parent map xref--xref-buffer-mode-map)
+    map))
+
+(define-derived-mode xref--definitions-buffer-mode
+  xref--xref-buffer-mode
+  "XREF Defs")
+
 (defun xref--next-error-function (n reset?)
   (when reset?
     (goto-char (point-min)))
@@ -725,7 +735,6 @@ xref--next-error-function
 
 (defvar xref--button-map
   (let ((map (make-sparse-keymap)))
-    (define-key map [(control ?m)] #'xref-goto-xref)
     (define-key map [mouse-1] #'xref-goto-xref)
     (define-key map [mouse-2] #'xref--mouse-2)
     map))
@@ -789,18 +798,21 @@ xref--show-xref-buffer
            (funcall fetcher)))
          (xref-alist (xref--analyze xrefs)))
     (with-current-buffer (get-buffer-create xref-buffer-name)
-      (setq buffer-undo-list nil)
-      (let ((inhibit-read-only t)
-            (buffer-undo-list t))
-        (erase-buffer)
-        (xref--insert-xrefs xref-alist)
-        (xref--xref-buffer-mode)
-        (pop-to-buffer (current-buffer))
-        (goto-char (point-min))
-        (setq xref--original-window (assoc-default 'window alist)
-              xref--original-window-intent (assoc-default 'display-action alist))
-        (setq xref--fetcher fetcher)
-        (current-buffer)))))
+      (xref--show-common-initialize xref-alist fetcher alist)
+      (xref--xref-buffer-mode)
+      (pop-to-buffer (current-buffer))
+      (current-buffer))))
+
+(defun xref--show-common-initialize (xref-alist fetcher alist)
+  (setq buffer-undo-list nil)
+  (let ((inhibit-read-only t)
+        (buffer-undo-list t))
+    (erase-buffer)
+    (xref--insert-xrefs xref-alist)
+    (goto-char (point-min))
+    (setq xref--original-window (assoc-default 'window alist)
+          xref--original-window-intent (assoc-default 'display-action alist))
+    (setq xref--fetcher fetcher)))
 
 (defun xref-refresh-results ()
   "Refresh the search results in the current buffer."
@@ -826,9 +838,12 @@ xref--show-defs-buffer
       (xref--pop-to-location (car xrefs)
                              (assoc-default 'display-action alist)))
      (t
-      (xref--show-xref-buffer fetcher
-                              (cons (cons 'fetched-xrefs xrefs)
-                                    alist))))))
+      (with-current-buffer (get-buffer-create xref-buffer-name)
+        (xref--show-common-initialize (xref--analyze xrefs) fetcher alist)
+        (xref--definitions-buffer-mode)
+        (pop-to-buffer (current-buffer)
+                       '(display-buffer-in-direction . ((direction . below))))
+        (current-buffer))))))
 
 
 (defvar xref-show-xrefs-function 'xref--show-xref-buffer