www.delorie.com/gnu/docs/elisp-manual-21/elisp_364.html   search  
 
Buy the book!


GNU Emacs Lisp Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

23.4 Imenu

Imenu is a feature that lets users select a definition or section in the buffer, from a menu which lists all of them, to go directly to that location in the buffer. Imenu works by constructing a buffer index which lists the names and buffer positions of the definitions, or other named portions of the buffer; then the user can choose one of them and move point to it. This section explains how to customize how Imenu finds the definitions or buffer portions for a particular major mode.

The usual and simplest way is to set the variable imenu-generic-expression:

Variable: imenu-generic-expression
This variable, if non-nil, specifies regular expressions for finding definitions for Imenu. In the simplest case, elements should look like this:

 
(menu-title regexp subexp)

Here, if menu-title is non-nil, it says that the matches for this element should go in a submenu of the buffer index; menu-title itself specifies the name for the submenu. If menu-title is nil, the matches for this element go directly in the top level of the buffer index.

The second item in the list, regexp, is a regular expression (see section 34.2 Regular Expressions); anything in the buffer that it matches is considered a definition, something to mention in the buffer index. The third item, subexp, indicates which subexpression in regexp matches the definition's name.

An element can also look like this:

 
(menu-title regexp index function arguments...)

Each match for this element creates a special index item which, if selected by the user, calls function with arguments consisting of the item name, the buffer position, and arguments.

For Emacs Lisp mode, pattern could look like this:

 
((nil "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\
\\s-+\\([-A-Za-z0-9+]+\\)" 2)
 ("*Vars*" "^\\s-*(def\\(var\\|const\\)\
\\s-+\\([-A-Za-z0-9+]+\\)" 2)
 ("*Types*"
  "^\\s-*\
(def\\(type\\|struct\\|class\\|ine-condition\\)\
\\s-+\\([-A-Za-z0-9+]+\\)" 2))

Setting this variable makes it buffer-local in the current buffer.

Variable: imenu-case-fold-search
This variable controls whether matching against imenu-generic-expression is case-sensitive: t, the default, means matching should ignore case.

Setting this variable makes it buffer-local in the current buffer.

Variable: imenu-syntax-alist
This variable is an alist of syntax table modifiers to use while processing imenu-generic-expression, to override the syntax table of the current buffer. Each element should have this form:

 
(characters . syntax-description)

The CAR, characters, can be either a character or a string. The element says to give that character or characters the syntax specified by syntax-description, which is passed to modify-syntax-entry (see section 35.3 Syntax Table Functions).

This feature is typically used to give word syntax to characters which normally have symbol syntax, and thus to simplify imenu-generic-expression and speed up matching. For example, Fortran mode uses it this way:

 
  (setq imenu-syntax-alist '(("_$" . "w")))

The imenu-generic-expression patterns can then use `\\sw+' instead of `\\(\\sw\\|\\s_\\)+'. Note that this technique may be inconvenient when the mode needs to limit the initial character of a name to a smaller set of characters than are allowed in the rest of a name.

Setting this variable makes it buffer-local in the current buffer.

Another way to customize Imenu for a major mode is to set the variables imenu-prev-index-position-function and imenu-extract-index-name-function:

Variable: imenu-prev-index-position-function
If this variable is non-nil, its value should be a function that finds the next "definition" to put in the buffer index, scanning backward in the buffer from point. It should return nil if it doesn't find another "definition" before point. Otherwise it shuould leave point at the place it finds a "definition," and return any non-nil value.

Setting this variable makes it buffer-local in the current buffer.

Variable: imenu-extract-index-name-function
If this variable is non-nil, its value should be a function to return the name for a definition, assuming point is in that definition as the imenu-prev-index-position-function function would leave it.

Setting this variable makes it buffer-local in the current buffer.

The last way to customize Imenu for a major mode is to set the variable imenu-create-index-function:

Variable: imenu-create-index-function
This variable specifies the function to use for creating a buffer index. The function should take no arguments, and return an index for the current buffer. It is called within save-excursion, so where it leaves point makes no difference.

The default value is a function that uses imenu-generic-expression to produce the index alist. If you specify a different function, then imenu-generic-expression is not used.

Setting this variable makes it buffer-local in the current buffer.

Variable: imenu-index-alist
This variable holds the index alist for the current buffer. Setting it makes it buffer-local in the current buffer.

Simple elements in the alist look like (index-name . index-position). Selecting a simple element has the effect of moving to position index-position in the buffer.

Special elements look like (index-name position function arguments...). Selecting a special element performs

 
(funcall function index-name position arguments...)

A nested sub-alist element looks like (index-name sub-alist).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003