24.17. Imenu

The Imenu facility is another way to find definitions or sections in a file. It is similar in spirit to Tags, but operates on a single buffer only, and works entirely within Emacs with no need for a separate tags table.

If you type M-x imenu, it reads the name of a section or definition in the current buffer, then goes to that section or definition. You can use completion to specify the name, and a complete list of possible names is always displayed.

Alternatively you can bind the command imenu to a mouse click. Then it displays mouse menus for you to select the section or definition you want. You can also add the buffer's index to the menu bar by calling imenu-add-menu-bar-index. If you want to have this menu bar item available for all buffers in a certain major mode, you can do this by adding imenu-add-menu-bar-index to its mode hook. But then you will have to wait for the buffer to be searched for sections and definitions, each time you visit a file which uses that mode.

When you change the contents of a buffer, if you add or delete definitions or sections, you can update the buffer's index to correspond to the new contents by invoking the *Rescan* item in the menu. Rescanning happens automatically if imenu-auto-rescan is non-nil. There is no need to rescan because of small changes in the text.

You can customize the way the menus are sorted via the variable imenu-sort-function. By default names are ordered as they occur in the buffer; alphabetic sorting is provided as an alternative.

Imenu provides the information to guide Which Function mode (Section 24.10). The Speedbar can also use it (Section 19.9).