1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; if not, write to the Free Software
23 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Purpose of this package:
28 ;; To present a framework for mode-specific buffer indexes.
29 ;; A buffer index is an alist of names and buffer positions.
30 ;; For instance all functions in a C-file and their positions.
34 ;; A mode-specific function is called to generate the index. It is
35 ;; then presented to the user, who can choose from this index.
37 ;; The package comes with a set of example functions for how to
38 ;; utilize this package.
40 ;; There are *examples* for index gathering functions for C/C++ and
41 ;; Lisp/Emacs Lisp but it is easy to customize for other modes. A
42 ;; function for jumping to the chosen index position is also
46 ;; Put this file in your load-path and insert the following in .emacs
48 ;; (autoload 'imenu-choose-buffer-index "imenu" "Menu of buffer index." t)
49 ;; (autoload 'goto-index-pos "imenu" "Goto buffer index position." t)
50 ;; (define-key global-map "\C-cj" 'goto-index-pos) ;; Or some other key
51 ;; (cond (window-system
52 ;; (define-key global-map [S-down-mouse-3] 'goto-index-pos)))
53 ;; Also run the 'add-hook' examples at the bottom of imenu.el.
56 ;; v1.6 Feb 28 1994 Ake Stenhoff
57 ;; Added alist as an optional argument to
58 ;; 'imenu-choose-buffer-index'.
60 ;; v1.5 Feb 25 1994 Ake Stenhoff
61 ;; Added code to parse DEFSTRUCT, DEFCLASS, DEFTYPE,
62 ;; DEFINE-CONDITION in the lisp example function.
64 ;; v1.4 Feb 18 1994 Ake Stenhoff
65 ;; Added 'imenu-create-submenu-name' for creating a submenu name.
66 ;; This is for getting a general look of submenu names.
67 ;; Added variable 'imenu-submenu-name-format' used by
68 ;; 'imenu-create-submenu-name'.
69 ;; v1.3 Feb 17 1994 Lars Lindberg
70 ;; Added 'imenu--flatten-index-alist' for flatten nexted index
72 ;; New examples for lisp mode that utilizes the features better.
73 ;; Added the variable 'imenu-space-replacement'.
74 ;; The completion-buffer version of the index menu now replaces
75 ;; spaces in the index-names to make tokens of them.
76 ;; v1.2 Feb 14 1994 Ake Stenhoff & Lars Lindberg
77 ;; Now handles nested index lists.
78 ;; v1.1 Feb 9 1994 Ake Stenhoff & Lars Lindberg
79 ;; Better comments (?).
80 ;; v1.0 Feb 8 1994 Ake Stenhoff & Lars Lindberg
81 ;; Based on func-menu.el 3.5.
84 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
85 ;; [dean] - Dean Andrews ada@unison.com
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;;; Customizable variables
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 (defvar imenu-always-use-completion-buffer-p nil
98 "*Set this to non-nil for displaying the index in a completion buffer.
100 Non-nil means always display the index in a completion buffer.
101 Nil means display the index as a mouse menu when the mouse was
102 used to trigger 'goto-index-pos'.")
104 (defvar imenu-sort-function nil
105 "*The function to use for sorting the index mouse-menu.
107 Affects only the mouse index menu.
109 Set this to nil if you don't want any sorting (faster).
110 The items in the menu are then presented in the order they were found
113 Set it to 'imenu--sort-by-name if you want alphabetic sorting.
115 The function should take two arguments and return T if the first
116 element should come before the second. The arguments are cons cells;
117 (NAME . POSITION). Look at 'imenu--sort-by-name' for an example.")
119 (defvar imenu-max-items
25
120 "*Maximum number of elements in an index mouse-menu.")
122 (defvar imenu-scanning-message
"Scanning buffer for index. (%3d%%)"
123 "*Progress message during the index scanning of the buffer.
124 If non NIL, user gets a message during the scanning of the buffer
126 Relevant only if the mode-specific function that creates the buffer
127 index use 'imenu-progress-message'.")
129 (defvar imenu-space-replacement
"^"
130 "*The replacement string for spaces in index names.
131 Used when presenting the index in a completion-buffer to make the
132 names work as tokens.")
134 (defvar imenu-level-separator
":"
135 "*The separator between index names of different levels.
136 Used for making mouse-menu titles and for flattening nested indexes
137 with name concatenation.")
139 (defvar imenu-submenu-name-format
"%s..."
140 "*The format for making a submenu name.")
144 (defvar imenu-create-index-function
'imenu-default-create-index-function
145 "The function to use for creating a buffer index.
147 It should be a function that takes no arguments and returns an index
148 of the current buffer as an alist. The elements in the alist look
149 like: (INDEX-NAME . INDEX-POSITION). You may also nest index list like
150 (INDEX-NAME . INDEX-ALIST).
152 This function is called within a 'save-excursion'.
154 The variable is buffer-local.")
155 (make-variable-buffer-local 'imenu-create-index-function
)
157 (defvar prev-index-position-function
'beginning-of-defun
158 "Function for finding the next index position.
160 If 'imenu-create-index-function' is set to
161 'imenu-default-create-index-function, then you must set this variable
162 to a function that will find the next index, looking backwards in the
165 The function should leave point at the place to be connected to the
166 index and it should return nil when it doesn't find another index. ")
167 (make-variable-buffer-local 'prev-index-position-function
)
169 (defvar extract-index-name-function nil
170 "Function for extracting the index name.
172 This function is called after the function pointed out by
173 'prev-index-position-function'.")
174 (make-variable-buffer-local 'extract-index-name-function
)
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;;; Internal variables
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;; The item to use in the index for rescanning the buffer.
183 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
185 ;; The latest buffer index.
187 (defvar imenu--index-alist nil
)
188 (make-variable-buffer-local 'imenu--index-alist
)
190 ;; History list for 'jump-to-function-in-buffer'.
192 (defvar imenu--history-list nil
)
193 (make-variable-buffer-local 'imenu--history-list
)
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;;; Internal support functions
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 ;;; Sorts the items depending on their index name.
204 ;;; An item look like (NAME . POSITION).
206 (defun imenu--sort-by-name (item1 item2
)
207 (string-lessp (car item1
) (car item2
)))
209 (defun imenu--relative-position (&optional reverse
)
210 ;; Support function to calculate relative position in buffer
211 ;; Beginning of buffer is 0 and end of buffer is 100
212 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
214 (total (buffer-size)))
215 (and reverse
(setq pos
(- total pos
)))
217 ;; Avoid overflow from multiplying by 100!
218 (/ (1- pos
) (max (/ total
100) 1))
219 (/ (* 100 (1- pos
)) (max total
1)))))
222 ;;; Macro to display a progress message. This will probably be used
223 ;;; in a tight loop, that is why we use a macro.
224 ;;; RELPOS is the relative position to display.
225 ;;; If RELPOS is nil, then the relative position in the buffer
227 (defmacro imenu-progress-message
(&optional relpos reverse
)
229 imenu-scanning-message
230 (message imenu-scanning-message
233 (` (imenu--relative-position (, reverse
)))))))))
236 ;;; Function for suporting general looking submenu names.
237 ;;; Uses 'imenu-submenu-name-format' for creating the name.
238 ;;; NAME is the base of the new submenu name.
240 (defun imenu-create-submenu-name (name)
241 (format imenu-submenu-name-format name
))
243 ;; Split LIST into sublists of max length N.
244 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
245 (defun imenu--split (list n
)
251 (push (pop remain
) sublist
)
254 ;; We have finished a sublist
255 (progn (push (nreverse sublist
) result
)
257 (setq sublist
'()))))
258 ;; There might be a sublist (if the length of LIST mod n is != 0)
259 ;; that has to be added to the result list.
261 (push (nreverse sublist
) result
))
265 ;;; Split a menu in to several menus.
267 (defun imenu--split-menu (menulist title
)
268 (cons "Function menus"
272 (cons (format "(%s)" title
) menu
)))
273 (imenu--split menulist imenu-max-items
))))
276 ;;; Find all items in this buffer that should be in the index.
277 ;;; Returns an alist on the form
278 ;;; ((NAME . POSITION) (NAME . POSITION) ...)
281 (defun imenu--make-index-alist ()
282 ;; Create a list for this buffer only when needed.
283 (or imenu--index-alist
285 (setq imenu--index-alist
287 (funcall imenu-create-index-function
))))
288 (or imenu--index-alist
289 (error "No items suitable for an index found in this buffer."))
290 ;; Add a rescan option to the index.
291 (cons imenu--rescan-item imenu--index-alist
))
293 (defun imenu-default-create-index-function ()
294 "*Wrapper for index searching functions.
296 Moves point to end of buffer and then repeatedly calls
297 'prev-index-position-function' and 'extract-index-name-function'.
298 Their results are gathered into an index alist."
300 (or (and (fboundp prev-index-position-function
)
301 (fboundp extract-index-name-function
))
302 (error "The mode \"%s\" does not take full advantage of imenu.el yet."
304 (let ((index-alist '())
306 (goto-char (point-max))
307 (imenu-progress-message 0 t
)
308 ;; Search for the function
309 (while (funcall prev-index-position-function
)
310 (imenu-progress-message nil t
)
312 (setq name
(funcall extract-index-name-function
)))
314 (push (cons name
(point)) index-alist
)))
315 (imenu-progress-message 100 t
)
318 (defun imenu--replace-spaces (name replacement
)
319 ;; Replace all spaces in NAME with REPLACEMENT.
320 ;; That second argument should be a string.
324 (if (char-equal ch ?\
)
326 (char-to-string ch
))))
330 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix
)
331 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
332 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
333 ;; name and a space concatenated to the names of the children.
334 ;; Third argument PREFIX is for internal use only.
338 (let* ((name (car item
))
340 (new-prefix (and concat-names
342 (concat prefix imenu-level-separator name
)
346 (list (cons new-prefix pos
)))
348 (imenu--flatten-index-alist pos new-prefix
))))))
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;;; The main functions for this package!
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 (defun imenu--completion-buffer (index-alist &optional prompt
)
358 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
360 Returns t for rescan and otherwise a position number."
361 ;; Create a list for this buffer only when needed.
363 (prepared-index-alist
367 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
370 (save-window-excursion
371 ;; Display the completion buffer
372 (with-output-to-temp-buffer "*Completions*"
373 (display-completion-list
374 (all-completions "" prepared-index-alist
)))
375 ;; Make a completion question
376 (setq name
(completing-read (or prompt
"Index item: ")
378 nil t nil
'imenu--history-list
)))
380 ((not (stringp name
))
382 ((string= name
(car imenu--rescan-item
))
385 (setq choice
(assoc name prepared-index-alist
))
387 ((listp (cdr choice
))
388 (imenu--completion-buffer (cdr choice
) prompt
))
392 (defun imenu--mouse-menu (index-alist event
&optional title
)
393 "Let the user select from a buffer index from a mouse menu.
395 INDEX-ALIST is the buffer index and EVENT is a mouse event.
397 Returns t for rescan and otherwise a position number."
398 (let* ((menu (imenu--split-menu
399 (if imenu-sort-function
400 (sort (copy-list index-alist
) imenu-sort-function
)
402 (or title
(buffer-name))))
404 (setq position
(x-popup-menu event menu
))
409 (imenu--mouse-menu position event
411 (concat title imenu-level-separator
412 (car (rassq position index-alist
)))
413 (car (rassq position index-alist
)))))
414 ((= position
(cdr imenu--rescan-item
))
417 (rassq position index-alist
)))))
419 (defun imenu-choose-buffer-index (&optional prompt alist
)
420 "Let the user select from a buffer index and return the chosen index.
422 If the user originally activated this function with the mouse, a mouse
423 menu is used. Otherwise a completion buffer is used and the user is
424 prompted with PROMPT.
426 If you call this function with index alist ALIST, then it lets the user
429 With no index alist ALIST, it calls 'imenu--make-index-alist' to
430 create the index alist.
432 If 'imenu-always-use-completion-buffer-p' is non-nil, then the
433 completion buffer is always used, no matter if the mouse was used or
436 The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
438 (mouse-triggered (listp last-command-event
))
440 ;; If selected by mouse, see to that the window where the mouse is
441 ;; really is selected.
443 (let ((window (posn-window (event-start last-command-event
))))
444 (or (framep window
) (select-window window
))))
445 ;; Create a list for this buffer only when needed.
447 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
449 (if (and mouse-triggered
450 (not imenu-always-use-completion-buffer-p
))
451 (imenu--mouse-menu index-alist last-command-event
)
452 (imenu--completion-buffer index-alist prompt
)))
454 (setq imenu--index-alist nil
)))
457 (defun goto-index-pos ()
458 "Jump to selected part of buffer, using a buffer menu or mouse menu.
460 See 'imenu-choose-buffer-index' for more information."
462 (let ((index-item (imenu-choose-buffer-index)))
466 (goto-char (cdr index-item
))))))
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;;;; Some examples of functions utilizing the framework of this
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;; Return the current/previous sexp and the location of the sexp (it's
476 ;; beginning) without moving the point.
477 (defun imenu-example--name-and-position ()
481 (end (progn (forward-sexp) (point))))
482 (cons (buffer-substring beg end
)
489 (defun imenu-example--lisp-extract-index-name ()
490 ;; Example of a candidate for 'imenu-extract-index-name-function'.
491 ;; This will generate a flat index of definitions in a lisp file.
493 (and (looking-at "(def")
499 (end (progn (forward-sexp -
1) (point))))
500 (buffer-substring beg end
)))
503 (defun imenu-example--create-lisp-index ()
504 ;; Example of a candidate for 'imenu-create-index-function'.
505 ;; It will generate a nested index of definitions.
506 (let ((index-alist '())
507 (index-var-alist '())
508 (index-type-alist '())
509 (index-unknown-alist '()))
510 (goto-char (point-max))
511 (imenu-progress-message 0)
512 ;; Search for the function
513 (while (beginning-of-defun)
514 (imenu-progress-message nil t
)
516 (and (looking-at "(def")
520 ((looking-at "def\\(var\\|const\\)")
522 (push (imenu-example--name-and-position)
524 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
526 (push (imenu-example--name-and-position)
528 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
530 (if (= (char-after (1- (point))) ?\
))
535 (push (imenu-example--name-and-position)
539 (push (imenu-example--name-and-position)
540 index-unknown-alist
)))))))
541 (imenu-progress-message 100)
543 (push (cons (imenu-create-submenu-name "Variables") index-var-alist
)
545 (and index-type-alist
546 (push (cons (imenu-create-submenu-name "Types") index-type-alist
)
548 (and index-unknown-alist
549 (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist
)
557 ;; Regular expression to find C functions
558 (defvar imenu-example--function-name-regexp-c
560 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
561 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
562 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
563 "\\([*&]+[ \t]*\\)?" ; pointer
564 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
567 (defun imenu-example--create-c-index (&optional regexp
)
568 (let ((index-alist '())
570 (goto-char (point-min))
571 (imenu-progress-message 0)
572 ;; Search for the function
574 (while (re-search-forward
575 (or regexp imenu-example--function-name-regexp-c
)
577 (imenu-progress-message)
580 (goto-char (scan-sexps (point) 1))
581 (setq char
(following-char)))
582 ;; Skip this function name if it is a prototype declaration.
583 (if (not (eq char ?\
;))
584 (push (imenu-example--name-and-position) index-alist
))))
585 (imenu-progress-message 100)
586 (nreverse index-alist
)))
591 ;; Regular expression to find C++ functions
592 (defvar imenu-example--function-name-regexp-c
++
594 "^[a-zA-Z0-9:]+[ \t]?" ; type specs; there can be no
595 "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?" ; more than 3 tokens, right?
596 "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?"
597 "\\([*&]+[ \t]*\\)?" ; pointer
598 "\\([a-zA-Z0-9_:*]+\\)[ \t]*(" ; name
600 (defun imenu-example--create-c++-index
()
601 (imenu-example--create-c-index imenu-example--function-name-regexp-c
++))
605 ;;; Example of hooks for the examples above
606 ;;; Put this in your .emacs.
608 ;; (add-hook 'emacs-lisp-mode-hook
611 ;; (setq imenu-create-index-function
612 ;; (function imenu-example--create-lisp-index)))))
614 ;; (add-hook 'lisp-mode-hook
617 ;; (setq imenu-create-index-function
618 ;; (function imenu-example--create-lisp-index)))))
620 ;; (add-hook 'c++-mode-hook
623 ;; (setq imenu-create-index-function
624 ;; (function imenu-example--create-c++-index)))))
626 ;; (add-hook 'c-mode-hook
629 ;; (setq imenu-create-index-function
630 ;; (function imenu-example--create-c-index)))))
634 ;;; imenu.el ends here