universal-ctags / citre

A superior code reading & auto-completion tool with pluggable backends.
GNU General Public License v3.0
337 stars 26 forks source link

Better Imenu integration (integrate with other imenu backend instead of overwriting it) #182

Open milanglacier opened 3 weeks ago

milanglacier commented 3 weeks ago

Citre mode will overwrite imenu-create-index-function when citre-enable-imenu-integration is t.

However, this might create problem if the user also has other imenu backend, e.g. eglot-imenu, or the imenu from the built-in xxxx-ts-mode since emacs 29. and want to use multiple imenu backends all at once.

Here is the piece of code of how eglot is handling the imenu:

    (unless (eglot--stay-out-of-p 'imenu)
      (add-function :before-until (local 'imenu-create-index-function)
                    #'eglot-imenu))

The takeaway is, instead of overwrite this function, it patches the imenu-create-index-function so that previous imenu backend does not remove.

This would be something like this and I think this way to use menu integration is a sensible default.

diff --git a/citre.el b/citre.el
index 4c3effb..0eed496 100644
--- a/citre.el
+++ b/citre.el
@@ -591,16 +591,15 @@ The returned value is a valid element of the return value of
       (add-hook 'completion-at-point-functions
                 #'citre-completion-at-point nil t))
     (when citre-enable-imenu-integration
-      (setq citre-imenu--create-index-function-orig
-            imenu-create-index-function)
-      (setq imenu-create-index-function #'citre-imenu-create-index-function)))
+      (add-function :before-until (local 'imenu-create-index-function)
+                    #'citre-imenu-create-index-function)
+      ))
    (t
     (remove-hook 'xref-backend-functions #'citre-xref-backend t)
     (remove-hook 'completion-at-point-functions
                  #'citre-completion-at-point t)
     (when citre-enable-imenu-integration
-      (setq imenu-create-index-function
-            citre-imenu--create-index-function-orig)))))
+      (remove-function (local 'imenu-create-index-function) #'citre-imenu-create-index-function)))))

 ;;;###autoload
 (defun citre-auto-enable-citre-mode ()
AmaiKinono commented 3 weeks ago

Thanks for the report! Yes, it's a pretty neat technique and we should do this instead. Would you like to open a PR for this?