]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve imenu indexing in ELisp mode
authorEshel Yaron <me@eshelyaron.com>
Tue, 25 Feb 2025 19:54:42 +0000 (20:54 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 25 Feb 2025 19:54:42 +0000 (20:54 +0100)
lisp/progmodes/elisp-mode.el

index a86683be2a5d00f1566c0453d17c619c9703f4cb..bce12f3576561356ab8c9cb3623dba20bd6d3199 100644 (file)
@@ -658,7 +658,7 @@ be used instead.
               #'elisp-flymake-byte-compile nil t)
   (add-hook 'refactor-backend-functions #'elisp-refactor-backend nil t)
   (add-hook 'context-menu-functions #'elisp-context-menu 10 t)
-  (setq-local imenu-create-index-function #'elisp-create-index)
+  (setq-local imenu-create-index-function #'elisp-create-imenu-index)
   (alist-set "compf" prettify-symbols-alist ?∘ #'equal))
 
 ;; Font-locking support.
@@ -2590,6 +2590,25 @@ of TARGET."
   (let ((bounds (bounds-of-thing-at-point 'symbol)))
     (delete-region (car bounds) (cdr bounds))))
 
+(defun elisp-create-imenu-index ()
+  (goto-char (point-min))
+  (let (index)
+    (condition-case nil
+        (while t
+          (scope (lambda (type beg len &rest _)
+                   (cl-case type
+                     ((defun)
+                      (push (cons (buffer-substring-no-properties beg (+ beg len)) beg)
+                            (alist-get "Function" index nil nil #'string=)))
+                     ((defvar)
+                      (push (cons (buffer-substring-no-properties beg (+ beg len)) beg)
+                            (alist-get "Var" index nil nil #'string=)))
+                     ((defface)
+                      (push (cons (buffer-substring-no-properties beg (+ beg len)) beg)
+                            (alist-get "Face" index nil nil #'string=)))))))
+      (end-of-file
+       (dolist (group index) (setcdr group (nreverse (cdr group))))
+       index))))
 \f
 (put 'read-symbol-shorthands 'safe-local-variable #'consp)