Difference between revisions of "Imenu"
(→Customisation: add to menu bar) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Package | {{Package | ||
|name=Imenu | |name=Imenu | ||
− | |description= | + | |description=https://www.gnu.org/software/emacs/manual/html_node/elisp/Imenu.html |
|author= | |author= | ||
|source= | |source= | ||
Line 17: | Line 17: | ||
= Customisation = | = Customisation = | ||
+ | |||
+ | == Add an entry menu == | ||
+ | |||
+ | You can add the imenu listing in the main menu bar. You have to add a hook to each mode: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (add-hook 'markdown-mode-hook #'imenu-add-menubar-index) | ||
+ | (add-hook 'makefile-mode-hook #'imenu-add-menubar-index) | ||
+ | (add-hook 'prog-mode-hook #'imenu-add-menubar-index) | ||
+ | </source> | ||
+ | |||
+ | The third line about prog-mode-hook adds imenu for all programming languages modes. | ||
+ | |||
+ | However, by default, changes to the file will not automatically update the index menu. Set imenu-auto-rescan to t in local fashion as shown below: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (add-hook 'markdown-mode-hook (lambda () (setq-local imenu-auto-rescan t))) | ||
+ | (add-hook 'makefile-mode-hook (lambda () (setq-local imenu-auto-rescan t))) | ||
+ | (add-hook 'prog-mode-hook | ||
+ | (lambda () | ||
+ | (setq-local imenu-auto-rescan t) | ||
+ | (setq-local imenu-sort-function #'imenu--sort-by-name))) | ||
+ | </source> | ||
+ | |||
+ | == Add an entry to the context menu == | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (add-hook 'prog-mode-hook 'context-menu-mode) | ||
+ | </source> | ||
== Using ido == | == Using ido == | ||
Line 76: | Line 105: | ||
Now, '''M-x eval-current-buffer''', hit '''C-c i''' and enjoy quick navigation through your code. | Now, '''M-x eval-current-buffer''', hit '''C-c i''' and enjoy quick navigation through your code. | ||
− | = | + | == imenu-list: show the menu list in a thin sidebar buffer == |
− | + | ||
+ | [https://github.com/bmag/imenu-list imenu-list] is a plugin (in melpa) to show the current buffer's imenu entries in a separate (and thin) buffer. | ||
+ | |||
+ | [[File:imenu-list-light.png]] | ||
+ | |||
+ | == imenu-anywhere: get a menu list across several buffers == | ||
+ | |||
+ | [https://github.com/vspinu/imenu-anywhere imenu-anywhere] (on [[MELPA]] and MELPA stable) allows to construct the menu list by looking into several buffers. By default it looks into buffers of the same mode, the same project and "friendly modes" (defined in <code>imenu-anywhere-friendly-modes</code>). | ||
+ | |||
+ | To bind it to a key of your choice do something like: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (global-set-key (kbd "C-.") #'imenu-anywhere) | ||
+ | </source> | ||
+ | |||
+ | = See also = | ||
+ | |||
+ | == helm-imenu == | ||
+ | |||
+ | [[Helm]] is a nice package that provides interactivity for lots of commands. Here '''helm-imenu''' (provided by the helm package) offers to interactively select a variable declaration, a method, a class (or something else depending on the language support). | ||
+ | |||
+ | [[File:helm-imenu.gif]] | ||
+ | |||
+ | |||
+ | == ace-jump, quick cursor movements == | ||
− | + | [[ace-jump]], for quick and direct cursor movement on current view. | |
[[Category:Intermediate]] | [[Category:Intermediate]] | ||
[[Category:Navigation]] | [[Category:Navigation]] | ||
+ | [[Category:Buffer Navigation]] |
Latest revision as of 00:45, 4 July 2024
Description | https://www.gnu.org/software/emacs/manual/html_node/elisp/Imenu.html |
---|---|
Maintainer | name of maintainer |
Part of Emacs | yes |
Imenu produces menus for accessing locations in documents, typically in the current buffer. You can access the locations using an ordinary menu (menu bar or other) or using minibuffer completion. A typical use of Imenu shows a menu-bar menu that is an index or table of contents for the current buffer. For a source-code buffer it is typical to index definitions of functions, variables, and so on.
You can use Imenu with any major mode and any programming language or document type. If there is no Imenu support for your context, you can add it using EmacsLisp and the constructs provided in library 'imenu.el'.
Library imenu+.el (Imenu+) extends standard library imenu.el.
Customisation
You can add the imenu listing in the main menu bar. You have to add a hook to each mode:
(add-hook 'markdown-mode-hook #'imenu-add-menubar-index)
(add-hook 'makefile-mode-hook #'imenu-add-menubar-index)
(add-hook 'prog-mode-hook #'imenu-add-menubar-index)
The third line about prog-mode-hook adds imenu for all programming languages modes.
However, by default, changes to the file will not automatically update the index menu. Set imenu-auto-rescan to t in local fashion as shown below:
(add-hook 'markdown-mode-hook (lambda () (setq-local imenu-auto-rescan t)))
(add-hook 'makefile-mode-hook (lambda () (setq-local imenu-auto-rescan t)))
(add-hook 'prog-mode-hook
(lambda ()
(setq-local imenu-auto-rescan t)
(setq-local imenu-sort-function #'imenu--sort-by-name)))
(add-hook 'prog-mode-hook 'context-menu-mode)
Using ido
Using ido in conjunction with imenu makes it very easy and quick to use. Add the following function into your ~/.emacs :
(defun ido-goto-symbol (&optional symbol-list)
"Refresh imenu and jump to a place in the buffer using Ido."
(interactive)
(unless (featurep 'imenu)
(require 'imenu nil t))
(cond
((not symbol-list)
(let ((ido-mode ido-mode)
(ido-enable-flex-matching
(if (boundp 'ido-enable-flex-matching)
ido-enable-flex-matching t))
name-and-pos symbol-names position)
(unless ido-mode
(ido-mode 1)
(setq ido-enable-flex-matching t))
(while (progn
(imenu--cleanup)
(setq imenu--index-alist nil)
(ido-goto-symbol (imenu--make-index-alist))
(setq selected-symbol
(ido-completing-read "Symbol? " symbol-names))
(string= (car imenu--rescan-item) selected-symbol)))
(unless (and (boundp 'mark-active) mark-active)
(push-mark nil t nil))
(setq position (cdr (assoc selected-symbol name-and-pos)))
(cond
((overlayp position)
(goto-char (overlay-start position)))
(t
(goto-char position)))))
((listp symbol-list)
(dolist (symbol symbol-list)
(let (name position)
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(ido-goto-symbol symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position
(get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name)
(string= (car imenu--rescan-item) name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(global-set-key (kbd "C-c i") 'ido-goto-symbol) ; or any key you see fit
Now, M-x eval-current-buffer, hit C-c i and enjoy quick navigation through your code.
imenu-list is a plugin (in melpa) to show the current buffer's imenu entries in a separate (and thin) buffer.
imenu-anywhere (on MELPA and MELPA stable) allows to construct the menu list by looking into several buffers. By default it looks into buffers of the same mode, the same project and "friendly modes" (defined in imenu-anywhere-friendly-modes
).
To bind it to a key of your choice do something like:
(global-set-key (kbd "C-.") #'imenu-anywhere)
See also
Helm is a nice package that provides interactivity for lots of commands. Here helm-imenu (provided by the helm package) offers to interactively select a variable declaration, a method, a class (or something else depending on the language support).
ace-jump, quick cursor movements
ace-jump, for quick and direct cursor movement on current view.