Subject: bug#36974: 26.2.90; [PATCH] Add imenu support to xref

From: Damien Cassou <[email-masked]>
Date: Thu, 08 Aug 2019 13:53:03 +0200

* lisp/progmodes/xref.el (xref--imenu-prev-index-position)
(xref--imenu-extract-index-name): Add functions to get imenu support.
(xref--xref-buffer-mode): Set imenu variables to the new functions.
* etc/NEWS: Add corresponding entry.

-- 
Damien Cassou
Företagsplatsen AB
Phone/Fax: +46 (0)8 774 63 00
Mobile: +33 (0)6 80 50 18 91
Address: Skeppsbron 26, 4tr, SE-111 30 Stockholm
Web: www.foretagsplatsen.se
>From e7def47af8c5d8dfedadc5079d1f791c709b2be8 Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Thu, 8 Aug 2019 13:42:20 +0200
Subject: [PATCH] Add imenu support to xref

* lisp/progmodes/xref.el (xref--imenu-prev-index-position)
(xref--imenu-extract-index-name): Add functions to get imenu support.
(xref--xref-buffer-mode): Set imenu variables to the new functions.
* etc/NEWS: Add corresponding entry.
---
 etc/NEWS               |  5 +++++
 lisp/progmodes/xref.el | 23 +++++++++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 818875f7a0..50b2a0f76d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1810,6 +1810,11 @@ This results in the use of Gravatar's default size of 80 pixels.
 This is possible using the new user options 'gravatar-default-image'
 and 'gravatar-force-default'.
 
+** xref
+
+---
+*** Imenu support has been added to 'xref--xref-buffer-mode'.
+
 
 * New Modes and Packages in Emacs 27.1
 
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 57d803894c..c00622f747 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -728,7 +728,11 @@ xref--xref-buffer-mode
   "Mode for displaying cross-references."
   (setq buffer-read-only t)
   (setq next-error-function #'xref--next-error-function)
-  (setq next-error-last-buffer (current-buffer)))
+  (setq next-error-last-buffer (current-buffer))
+  (setq imenu-prev-index-position-function
+        #'xref--imenu-prev-index-position)
+  (setq imenu-extract-index-name-function
+        #'xref--imenu-extract-index-name))
 
 (defvar xref--transient-buffer-mode-map
   (let ((map (make-sparse-keymap)))
@@ -740,6 +744,21 @@ xref--transient-buffer-mode
   xref--xref-buffer-mode
   "XREF Transient")
 
+(defun xref--imenu-prev-index-position ()
+  "Move point to previous line in `xref' buffer.
+This function is used as a value for
+`imenu-prev-index-position-function'."
+  (if (bobp)
+      nil
+    (xref--search-property 'xref-group t)))
+
+(defun xref--imenu-extract-index-name ()
+  "Return imenu name for line at point.
+This function is used as a value for
+`imenu-extract-index-name-function'.  Point should be at the
+beginning of the line."
+  (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
+
 (defun xref--next-error-function (n reset?)
   (when reset?
     (goto-char (point-min)))
@@ -789,7 +808,7 @@ xref--insert-xrefs
            for line-format = (and max-line-width
                                   (format "%%%dd: " max-line-width))
            do
-           (xref--insert-propertized '(face xref-file-header) group "\n")
+           (xref--insert-propertized '(face xref-file-header 'xref-group t) group "\n")
            (cl-loop for (xref . more2) on xrefs do
                     (with-slots (summary location) xref
                       (let* ((line (xref-location-line location))
-- 
2.21.0