Imenu Indexing My emacs Init File

After implementing my simple occur indexing in my last post :

Indexing My Emacs Init File

A suggestion was made to put this into an imenu.

I thought that was rather a good idea and it would also give me the opportunity to explore imenu

I came up with the following to add to the emacs init file :

(add-hook 'emacs-lisp-mode-hook
  (lambda ()
    (setq imenu-sort-function 'imenu--sort-by-name)
    (setq imenu-generic-expression
      '(
         (nil "^;;[[:space:]]+-> \\(.*\\)$" 1)
         ("defun" "^.*([[:space:]]*defun[[:space:]]+\\([[:word:]-/]+\\)" 1)
         ("use-package" "^.*([[:space:]]*use-package[[:space:]]+\\([[:word:]-]+\\)" 1)
         )
      )
    (imenu-add-menubar-index)))

which produces the following:

I thought for good measure I would also add in some menus for defuns and use-package declarations (just for fun!) with my main defined sections forming the top level due to the MENU-TITLE being set to nil in the imenu-generic-expression variable.

This implementation has the added benefit of integrating nicely into the local completion system. For example I use vertico and running imenu calls up the defined sections in the mini-buffer and as an extra benefit running consult-imenu does its consult thing to quickly step through the sections or of course to complete. As I have defined section names for defun and use-package the completion search can search on these too to quickly narrow things down.

I had never looked into imenu before and it was quite an interesting learning experience and in fact I went all the way to defining my own imenu-create-index-function where I can create my own alist index in any way I want to, that was until I found a more simple method!

Comments

comments powered by Disqus