1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
9 ;; Keywords: tools convenience
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Purpose of this package:
31 ;; To present a framework for mode-specific buffer indexes.
32 ;; A buffer index is an alist of names and buffer positions.
33 ;; For instance all functions in a C-file and their positions.
35 ;; It is documented in the Emacs Lisp manual.
39 ;; A mode-specific function is called to generate the index. It is
40 ;; then presented to the user, who can choose from this index.
42 ;; The package comes with a set of example functions for how to
43 ;; utilize this package.
45 ;; There are *examples* for index gathering functions/regular
46 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
47 ;; customize for other modes. A function for jumping to the chosen
48 ;; index position is also supplied.
52 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
53 ;; [dean] - Dean Andrews ada@unison.com
54 ;; [alon] - Alon Albert al@mercury.co.il
55 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
56 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
57 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
58 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
59 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
60 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
64 (eval-when-compile (require 'cl
))
66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 ;;; Customizable variables
70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 "Mode-specific buffer indexes."
77 :link
'(custom-manual "(elisp)Imenu"))
79 (defcustom imenu-use-markers t
80 "*Non-nil means use markers instead of integers for Imenu buffer positions.
82 Setting this to nil makes Imenu work a little faster but editing the
83 buffer will make the generated index positions wrong.
85 This might not yet be honored by all index-building functions."
90 (defcustom imenu-max-item-length
60
91 "*If a number, truncate Imenu entries to that length."
92 :type
'(choice integer
93 (const :tag
"Unlimited"))
96 (defcustom imenu-auto-rescan nil
97 "*Non-nil means Imenu should always rescan the buffers."
101 (defcustom imenu-auto-rescan-maxout
60000
102 "*Imenu auto-rescan is disabled in buffers larger than this size (in bytes).
103 This variable is buffer-local."
107 (defcustom imenu-always-use-completion-buffer-p nil
108 "*Set this to non-nil for displaying the index in a completion buffer.
110 `never' means never automatically display a listing of any kind.
111 A value of nil (the default) means display the index as a mouse menu
112 if the mouse was used to invoke `imenu'.
113 Another non-nil value means always display the index in a completion buffer."
114 :type
'(choice (const :tag
"On Mouse" nil
)
115 (const :tag
"Never" never
)
116 (other :tag
"Always" t
))
119 (defcustom imenu-sort-function nil
120 "*The function to use for sorting the index mouse-menu.
122 Affects only the mouse index menu.
124 Set this to nil if you don't want any sorting (faster).
125 The items in the menu are then presented in the order they were found
128 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
130 The function should take two arguments and return T if the first
131 element should come before the second. The arguments are cons cells;
132 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
133 :type
'(choice (const :tag
"No sorting" nil
)
134 (const :tag
"Sort by name" 'imenu--sort-by-name
)
135 (function :tag
"Another function"))
138 (defcustom imenu-max-items
25
139 "*Maximum number of elements in a mouse menu for Imenu."
143 (defcustom imenu-scanning-message
"Scanning buffer for index (%3d%%)"
144 "*Progress message during the index scanning of the buffer.
145 If non-nil, user gets a message during the scanning of the buffer.
147 Relevant only if the mode-specific function that creates the buffer
148 index use `imenu-progress-message', and not useful if that is fast, in
149 which case you might as well set this to nil."
150 :type
'(choice string
151 (const :tag
"None" nil
))
154 (defcustom imenu-space-replacement
"."
155 "*The replacement string for spaces in index names.
156 Used when presenting the index in a completion buffer to make the
157 names work as tokens."
161 (defcustom imenu-level-separator
":"
162 "*The separator between index names of different levels.
163 Used for making mouse-menu titles and for flattening nested indexes
164 with name concatenation."
169 (defvar imenu-generic-expression nil
170 "The regex pattern to use for creating a buffer index.
172 If non-nil this pattern is passed to `imenu--generic-function'
173 to create a buffer index.
175 The value should be an alist with elements that look like this:
176 (MENU-TITLE REGEXP INDEX)
178 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
179 with zero or more ARGUMENTS. The former format creates a simple element in
180 the index alist when it matches; the latter creates a special element
181 of the form (NAME FUNCTION POSITION-MARKER ARGUMENTS...)
182 with FUNCTION and ARGUMENTS beiong copied from `imenu-generic-expression'.
184 MENU-TITLE is a string used as the title for the submenu or nil if the
185 entries are not nested.
187 REGEXP is a regexp that should match a construct in the buffer that is
188 to be displayed in the menu; i.e., function or variable definitions,
189 etc. It contains a substring which is the name to appear in the
190 menu. See the info section on Regexps for more information.
192 INDEX points to the substring in REGEXP that contains the name (of the
193 function, variable or type) that is to appear in the menu.
195 The variable is buffer-local.
197 The variable `imenu-case-fold-search' determines whether or not the
198 regexp matches are case sensitive. and `imenu-syntax-alist' can be
199 used to alter the syntax table for the search.
201 For example, see the value of `lisp-imenu-generic-expression' used by
202 `lisp-mode' and `emacs-lisp-mode' with `imenu-syntax-alist' set
203 locally to give the characters which normally have \"punctuation\"
204 syntax \"word\" syntax during matching.")
207 (make-variable-buffer-local 'imenu-generic-expression
)
211 (defvar imenu-create-index-function
'imenu-default-create-index-function
212 "The function to use for creating a buffer index.
214 It should be a function that takes no arguments and returns an index
215 of the current buffer as an alist.
217 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
218 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
219 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
220 The function `imenu--subalist-p' tests an element and returns t
221 if it is a sub-alist.
223 This function is called within a `save-excursion'.
225 The variable is buffer-local.")
226 (make-variable-buffer-local 'imenu-create-index-function
)
228 (defvar imenu-prev-index-position-function
'beginning-of-defun
229 "Function for finding the next index position.
231 If `imenu-create-index-function' is set to
232 `imenu-default-create-index-function', then you must set this variable
233 to a function that will find the next index, looking backwards in the
236 The function should leave point at the place to be connected to the
237 index and it should return nil when it doesn't find another index.
239 This variable is local in all buffers.")
241 (make-variable-buffer-local 'imenu-prev-index-position-function
)
243 (defvar imenu-extract-index-name-function nil
244 "Function for extracting the index item name, given a position.
246 This function is called after `imenu-prev-index-position-function'
247 finds a position for an index item, with point at that position.
248 It should return the name for that index item.
250 This variable is local in all buffers.")
252 (make-variable-buffer-local 'imenu-extract-index-name-function
)
254 (defvar imenu-default-goto-function
'imenu-default-goto-function
255 "The default function called when selecting an Imenu item.
256 The function in this variable is called when selecting a normal index-item.")
257 (make-variable-buffer-local 'imenu-default-goto-function
)
260 (defun imenu--subalist-p (item)
261 (and (consp (cdr item
)) (listp (cadr item
))
262 (not (eq (car (cadr item
)) 'lambda
))))
264 ;; Macro to display a progress message.
265 ;; RELPOS is the relative position to display.
266 ;; If RELPOS is nil, then the relative position in the buffer
268 ;; PREVPOS is the variable in which we store the last position displayed.
269 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
271 imenu-scanning-message
272 (let ((pos ,(if relpos
274 `(imenu--relative-position ,reverse
))))
276 `(> pos
(+ 5 ,prevpos
)))
278 (message imenu-scanning-message pos
)
279 (setq ,prevpos pos
))))))
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;;;; Some examples of functions utilizing the framework of this
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;; Return the current/previous sexp and the location of the sexp (its
290 ;; beginning) without moving the point.
291 (defun imenu-example--name-and-position ()
294 ;; [ydi] modified for imenu-use-markers
295 (let ((beg (if imenu-use-markers
(point-marker) (point)))
296 (end (progn (forward-sexp) (point))))
297 (cons (buffer-substring beg end
)
304 (defun imenu-example--lisp-extract-index-name ()
305 ;; Example of a candidate for `imenu-extract-index-name-function'.
306 ;; This will generate a flat index of definitions in a lisp file.
308 (and (looking-at "(def")
314 (end (progn (forward-sexp -
1) (point))))
315 (buffer-substring beg end
)))
318 (defun imenu-example--create-lisp-index ()
319 ;; Example of a candidate for `imenu-create-index-function'.
320 ;; It will generate a nested index of definitions.
321 (let ((index-alist '())
322 (index-var-alist '())
323 (index-type-alist '())
324 (index-unknown-alist '())
326 (goto-char (point-max))
327 (imenu-progress-message prev-pos
0)
328 ;; Search for the function
329 (while (beginning-of-defun)
330 (imenu-progress-message prev-pos nil t
)
332 (and (looking-at "(def")
336 ((looking-at "def\\(var\\|const\\)")
338 (push (imenu-example--name-and-position)
340 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
342 (push (imenu-example--name-and-position)
344 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
346 (if (= (char-after (1- (point))) ?\
))
351 (push (imenu-example--name-and-position)
355 (push (imenu-example--name-and-position)
356 index-unknown-alist
)))))))
357 (imenu-progress-message prev-pos
100)
359 (push (cons "Variables" index-var-alist
)
361 (and index-type-alist
362 (push (cons "Types" index-type-alist
)
364 (and index-unknown-alist
365 (push (cons "Syntax-unknown" index-unknown-alist
)
369 ;; Regular expression to find C functions
370 (defvar imenu-example--function-name-regexp-c
372 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
373 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
374 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
375 "\\([*&]+[ \t]*\\)?" ; pointer
376 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
379 (defun imenu-example--create-c-index (&optional regexp
)
380 (let ((index-alist '())
382 (goto-char (point-min))
383 (imenu-progress-message prev-pos
0)
384 ;; Search for the function
386 (while (re-search-forward
387 (or regexp imenu-example--function-name-regexp-c
)
389 (imenu-progress-message prev-pos
)
392 (goto-char (scan-sexps (point) 1))
393 (setq char
(following-char)))
394 ;; Skip this function name if it is a prototype declaration.
395 (if (not (eq char ?\
;))
396 (push (imenu-example--name-and-position) index-alist
))))
397 (imenu-progress-message prev-pos
100)
398 (nreverse index-alist
)))
401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403 ;;; Internal variables
405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
407 ;; The item to use in the index for rescanning the buffer.
408 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
410 ;; The latest buffer index.
412 (defvar imenu--index-alist nil
413 "The buffer index computed for this buffer in Imenu.
414 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
415 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
416 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
418 This variable is local in all buffers, once set.")
420 (make-variable-buffer-local 'imenu--index-alist
)
422 ;; The latest buffer index used to update the menu bar menu.
423 (defvar imenu--last-menubar-index-alist nil
)
424 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
426 ;; History list for 'jump-to-function-in-buffer'.
427 ;; Making this buffer local caused it not to work!
428 (defvar imenu--history-list nil
)
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 ;;; Internal support functions
434 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438 ;;; Sorts the items depending on their index name.
439 ;;; An item look like (NAME . POSITION).
441 (defun imenu--sort-by-name (item1 item2
)
442 (string-lessp (car item1
) (car item2
)))
444 (defun imenu--relative-position (&optional reverse
)
445 ;; Support function to calculate relative position in buffer
446 ;; Beginning of buffer is 0 and end of buffer is 100
447 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
449 (total (buffer-size)))
450 (and reverse
(setq pos
(- total pos
)))
452 ;; Avoid overflow from multiplying by 100!
453 (/ (1- pos
) (max (/ total
100) 1))
454 (/ (* 100 (1- pos
)) (max total
1)))))
456 ;; Split LIST into sublists of max length N.
457 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
458 (defun imenu--split (list n
)
464 (push (pop remain
) sublist
)
467 ;; We have finished a sublist
468 (progn (push (nreverse sublist
) result
)
470 (setq sublist
'()))))
471 ;; There might be a sublist (if the length of LIST mod n is != 0)
472 ;; that has to be added to the result list.
474 (push (nreverse sublist
) result
))
477 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
478 ;;; In any case, add TITLE to the front of the alist.
479 (defun imenu--split-menu (menulist title
)
480 (let (keep-at-top tail
)
481 (if (memq imenu--rescan-item menulist
)
482 (setq keep-at-top
(cons imenu--rescan-item nil
)
483 menulist
(delq imenu--rescan-item menulist
)))
486 (if (imenu--subalist-p (car tail
))
487 (setq keep-at-top
(cons (car tail
) keep-at-top
)
488 menulist
(delq (car tail
) menulist
)))
489 (setq tail
(cdr tail
)))
490 (if imenu-sort-function
495 ;; Copy list method from the cl package `copy-list'
496 (while (consp oldlist
) (push (pop oldlist
) res
))
497 (if res
; in case, e.g. no functions defined
498 (prog1 (nreverse res
) (setcdr res oldlist
))))
499 imenu-sort-function
)))
500 (if (> (length menulist
) imenu-max-items
)
506 (cons (format "From: %s" (caar menu
)) menu
)))
507 (imenu--split menulist imenu-max-items
)))))
509 (nconc (nreverse keep-at-top
) menulist
))))
511 ;;; Split up each long alist that are nested within ALIST
512 ;;; into nested alists.
513 (defun imenu--split-submenus (alist)
519 (imenu--split-menu (cdr elt
) (car elt
))
523 ;;; Truncate all strings in MENULIST to imenu-max-item-length
524 (defun imenu--truncate-items (menulist)
529 (imenu--truncate-items (cdr item
)))
531 ;; truncate if necessary
532 (if (and (numberp imenu-max-item-length
)
533 (> (length (car item
)) imenu-max-item-length
))
534 (setcar item
(substring (car item
) 0 imenu-max-item-length
)))))))
538 (defun imenu--make-index-alist (&optional noerror
)
539 "Create an index-alist for the definitions in the current buffer.
541 Report an error if the list is empty unless NOERROR is supplied and
544 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
545 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
546 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
547 The function `imenu--subalist-p' tests an element and returns t
548 if it is a sub-alist.
550 There is one simple element with negative POSITION; that's intended
551 as a way for the user to ask to recalculate the buffer's index alist."
552 (or (and imenu--index-alist
553 (or (not imenu-auto-rescan
)
554 (and imenu-auto-rescan
555 (> (buffer-size) imenu-auto-rescan-maxout
))))
556 ;; Get the index; truncate if necessary
558 (setq imenu--index-alist
562 (funcall imenu-create-index-function
))))
563 (imenu--truncate-items imenu--index-alist
)))
564 (or imenu--index-alist noerror
565 (error "No items suitable for an index found in this buffer"))
566 (or imenu--index-alist
567 (setq imenu--index-alist
(list nil
)))
568 ;; Add a rescan option to the index.
569 (cons imenu--rescan-item imenu--index-alist
))
571 ;;; Find all markers in alist and makes
572 ;;; them point nowhere.
573 ;;; The top-level call uses nil as the argument;
574 ;;; non-nil arguments are in recursivecalls.
575 (defvar imenu--cleanup-seen
)
577 (defun imenu--cleanup (&optional alist
)
578 ;; If alist is provided use that list.
579 ;; If not, empty the table of lists already seen
580 ;; and use imenu--index-alist.
582 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
583 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
590 ((markerp (cdr item
))
591 (set-marker (cdr item
) nil
))
592 ;; Don't process one alist twice.
593 ((memq (cdr item
) imenu--cleanup-seen
))
594 ((imenu--subalist-p item
)
595 (imenu--cleanup (cdr item
))))))
599 (defun imenu--create-keymap-2 (alist counter
&optional commands
)
605 ((imenu--subalist-p item
)
606 (append (list (setq counter
(1+ counter
))
607 (car item
) 'keymap
(car item
))
608 (imenu--create-keymap-2 (cdr item
) (+ counter
10) commands
)))
610 (let ((end (if commands
`(lambda () (interactive)
611 (imenu--menubar-select ',item
))
612 (cons '(nil) item
))))
614 (cons (car item
) end
)))))))
617 ;; If COMMANDS is non-nil, make a real keymap
618 ;; with a real command used as the definition.
619 ;; If it is nil, make something suitable for x-popup-menu.
620 (defun imenu--create-keymap-1 (title alist
&optional commands
)
621 (append (list 'keymap title
) (imenu--create-keymap-2 alist
0 commands
)))
623 (defun imenu--in-alist (str alist
)
624 "Check whether the string STR is contained in multi-level ALIST."
625 (let (elt head tail res
)
628 (setq elt
(car alist
)
632 ;; A nested ALIST element looks like
633 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
634 ;; while a bottom-level element looks like
635 ;; (INDEX-NAME . INDEX-POSITION)
636 ;; We are only interested in the bottom-level elements, so we need to
637 ;; recurse if TAIL is a list.
639 (if (setq res
(imenu--in-alist str tail
))
642 (setq alist nil res elt
))))
645 (defvar imenu-syntax-alist nil
646 "Alist of syntax table modifiers to use while executing `imenu--generic-function'.
648 The car of the assocs may be either a character or a string and the
649 cdr is a syntax description appropriate fo `modify-syntax-entry'. For
650 a string, all the characters in the string get the specified syntax.
652 This is typically used to give word syntax to characters which
653 normally have symbol syntax to simplify `imenu-expression'
654 and speed-up matching.")
655 (make-variable-buffer-local 'imenu-syntax-alist
)
657 (defun imenu-default-create-index-function ()
658 "*Wrapper for index searching functions.
660 Moves point to end of buffer and then repeatedly calls
661 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
662 Their results are gathered into an index alist."
663 ;; These should really be done by setting imenu-create-index-function
664 ;; in these major modes. But save that change for later.
665 (cond ((and imenu-prev-index-position-function
666 imenu-extract-index-name-function
)
667 (let ((index-alist '())
669 (goto-char (point-max))
670 (imenu-progress-message prev-pos
0 t
)
671 ;; Search for the function
672 (while (funcall imenu-prev-index-position-function
)
673 (imenu-progress-message prev-pos nil t
)
675 (setq name
(funcall imenu-extract-index-name-function
)))
677 ;; [ydi] updated for imenu-use-markers
678 (push (cons name
(if imenu-use-markers
(point-marker) (point)))
680 (imenu-progress-message prev-pos
100 t
)
682 ;; Use generic expression if possible.
683 ((and imenu-generic-expression
)
684 (imenu--generic-function imenu-generic-expression
))
686 (error "This buffer cannot use `imenu-default-create-index-function'"))))
688 (defun imenu--replace-spaces (name replacement
)
689 ;; Replace all spaces in NAME with REPLACEMENT.
690 ;; That second argument should be a string.
694 (if (char-equal ch ?\
)
696 (char-to-string ch
))))
700 ;; Not used and would require cl at run time
701 ;;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
702 ;;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
703 ;;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
704 ;;; ;; name and a space concatenated to the names of the children.
705 ;;; ;; Third argument PREFIX is for internal use only.
708 ;;; (let* ((name (car item))
710 ;;; (new-prefix (and concat-names
712 ;;; (concat prefix imenu-level-separator name)
715 ;;; ((or (markerp pos) (numberp pos))
716 ;;; (list (cons new-prefix pos)))
718 ;;; (imenu--flatten-index-alist pos new-prefix)))))
722 ;;; Generic index gathering function.
725 (defvar imenu-case-fold-search t
726 "Defines whether `imenu--generic-function' should fold case when matching.
728 This buffer-local variable should be set (only) by initialization code
729 for modes which use `imenu--generic-function'. If it is not set, that
730 function will use the current value of `case-fold-search' to match
733 (make-variable-buffer-local 'imenu-case-fold-search
)
735 ;; Originally "Built on some ideas that Erik Naggum <erik@naggum.no>
736 ;; once posted to comp.emacs" but since substantially re-written.
737 (defun imenu--generic-function (patterns)
738 "Return an index of the current buffer as an alist.
740 PATTERNS is an alist with elements that look like this:
741 (MENU-TITLE REGEXP INDEX).
743 MENU-TITLE is a string used as the title for the submenu or nil if the
744 entries are not nested.
746 REGEXP is a regexp that should match a construct in the buffer that is
747 to be displayed in the menu; i.e., function or variable definitions,
748 etc. It contains a substring which is the name to appear in the
749 menu. See the info section on Regexps for more information.
751 INDEX points to the substring in REGEXP that contains the name (of the
752 function, variable or type) that is to appear in the menu.
754 See `lisp-imenu-generic-expression' for an example of PATTERNS.
756 Returns an index of the current buffer as an alist. The elements in
757 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
758 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
761 (let ((index-alist (list 'dummy
))
763 (case-fold-search imenu-case-fold-search
)
764 (old-table (syntax-table))
765 (table (copy-syntax-table (syntax-table)))
766 (slist imenu-syntax-alist
))
767 ;; Modify the syntax table used while matching regexps.
769 ;; The character(s) to modify may be a single char or a string.
770 (if (numberp (caar slist
))
771 (modify-syntax-entry (caar slist
) (cdar slist
) table
)
774 (modify-syntax-entry c
(cdar slist
) table
)))
776 (setq slist
(cdr slist
)))
777 (goto-char (point-max))
778 (imenu-progress-message prev-pos
0 t
)
779 (unwind-protect ; for syntax table
781 (set-syntax-table table
)
782 ;; map over the elements of imenu-generic-expression
783 ;; (typically functions, variables ...)
787 (let ((menu-title (car pat
))
790 (function (nth 3 pat
))
791 (rest (nthcdr 4 pat
)))
792 ;; Go backwards for convenience of adding items in order.
793 (goto-char (point-max))
794 (while (re-search-backward regexp nil t
)
795 (imenu-progress-message prev-pos nil t
)
796 (setq beg
(match-beginning index
))
797 ;; Add this sort of submenu only when we've found an
798 ;; item for it, avoiding empty, duff menus.
799 (unless (assoc menu-title index-alist
)
800 (push (list menu-title
) index-alist
))
801 (if imenu-use-markers
802 (setq beg
(set-marker (make-marker) beg
)))
805 (nconc (list (match-string-no-properties index
)
808 (cons (match-string-no-properties index
)
810 (menu (cdr (assoc menu-title index-alist
))))
811 ;; avoid duplicates from, e.g. cc-mode patterns
812 (unless (member item menu
)
813 ;; insert the item after the (sub-)menu title
814 (setcdr (assoc menu-title index-alist
)
815 (cons item menu
))))))))
817 (set-syntax-table old-table
)))
818 (imenu-progress-message prev-pos
100 t
)
819 (let ((main-element (assq nil index-alist
)))
820 (nconc (delq main-element
(delq 'dummy index-alist
))
821 (cdr main-element
)))))
823 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
825 ;;; The main functions for this package!
827 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
829 (defun imenu--completion-buffer (index-alist &optional prompt
)
830 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
832 Returns t for rescan and otherwise a position number."
833 ;; Create a list for this buffer only when needed.
834 (let ((name (thing-at-point 'symbol
))
836 (prepared-index-alist
840 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
844 ((and name
(imenu--in-alist name prepared-index-alist
))
845 (setq prompt
(format "Index item (default %s): " name
)))
846 (t (setq prompt
"Index item: ")))
847 (if (eq imenu-always-use-completion-buffer-p
'never
)
848 (setq name
(completing-read prompt
850 nil t nil
'imenu--history-list name
))
851 (save-window-excursion
852 ;; Display the completion buffer
853 (with-output-to-temp-buffer "*Completions*"
854 (display-completion-list
855 (all-completions "" prepared-index-alist
)))
856 (let ((minibuffer-setup-hook
859 (let ((buffer (current-buffer)))
861 (set-buffer "*Completions*")
862 (setq completion-reference-buffer buffer
)))))))
863 ;; Make a completion question
864 (setq name
(completing-read prompt
866 nil t nil
'imenu--history-list name
)))))
867 (cond ((not (stringp name
))
869 ((string= name
(car imenu--rescan-item
))
872 (setq choice
(assoc name prepared-index-alist
))
873 (if (imenu--subalist-p choice
)
874 (imenu--completion-buffer (cdr choice
) prompt
)
877 (defun imenu--mouse-menu (index-alist event
&optional title
)
878 "Let the user select from a buffer index from a mouse menu.
880 INDEX-ALIST is the buffer index and EVENT is a mouse event.
882 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
883 (setq index-alist
(imenu--split-submenus index-alist
))
884 (let* ((menu (imenu--split-menu index-alist
885 (or title
(buffer-name))))
887 (setq menu
(imenu--create-keymap-1 (car menu
)
888 (if (< 1 (length (cdr menu
)))
890 (cdr (car (cdr menu
))))))
891 (setq position
(x-popup-menu event menu
))
892 (cond ((eq position nil
)
894 ;; If one call to x-popup-menu handled the nested menus,
895 ;; find the result by looking down the menus here.
896 ((and (listp position
)
897 (numberp (car position
))
898 (stringp (nth (1- (length position
)) position
)))
901 (setq final
(assoc (car position
) final
))
902 (setq position
(cdr position
)))
903 (or (string= (car final
) (car imenu--rescan-item
))
905 ;; If x-popup-menu went just one level and found a leaf item,
906 ;; return the INDEX-ALIST element for that.
907 ((and (consp position
)
908 (stringp (car position
))
909 (null (cdr position
)))
910 (or (string= (car position
) (car imenu--rescan-item
))
911 (assq (car position
) index-alist
)))
912 ;; If x-popup-menu went just one level
913 ;; and found a non-leaf item (a submenu),
914 ;; recurse to handle the rest.
916 (imenu--mouse-menu position event
918 (concat title imenu-level-separator
919 (car (rassq position index-alist
)))
920 (car (rassq position index-alist
))))))))
922 (defun imenu-choose-buffer-index (&optional prompt alist
)
923 "Let the user select from a buffer index and return the chosen index.
925 If the user originally activated this function with the mouse, a mouse
926 menu is used. Otherwise a completion buffer is used and the user is
927 prompted with PROMPT.
929 If you call this function with index alist ALIST, then it lets the user
932 With no index alist ALIST, it calls `imenu--make-index-alist' to
933 create the index alist.
935 If `imenu-always-use-completion-buffer-p' is non-nil, then the
936 completion buffer is always used, no matter if the mouse was used or
939 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
941 (mouse-triggered (listp last-nonmenu-event
))
943 ;; If selected by mouse, see to that the window where the mouse is
944 ;; really is selected.
946 (not (equal last-nonmenu-event
'(menu-bar)))
947 (let ((window (posn-window (event-start last-nonmenu-event
))))
948 (or (framep window
) (null window
) (select-window window
))))
949 ;; Create a list for this buffer only when needed.
951 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
953 (if (and mouse-triggered
954 (not imenu-always-use-completion-buffer-p
))
955 (imenu--mouse-menu index-alist last-nonmenu-event
)
956 (imenu--completion-buffer index-alist prompt
)))
959 (setq imenu--index-alist nil
)))
963 (defun imenu-add-to-menubar (name)
964 "Add an `imenu' entry to the menu bar for the current buffer.
965 NAME is a string used to name the menu bar item.
966 See the command `imenu' for more information."
967 (interactive "sImenu menu item name: ")
968 (if (or (and imenu-prev-index-position-function
969 imenu-extract-index-name-function
)
970 imenu-generic-expression
971 (not (eq imenu-create-index-function
972 'imenu-default-create-index-function
)))
973 (let ((newmap (make-sparse-keymap))
974 (menu-bar (lookup-key (current-local-map) [menu-bar
])))
975 (define-key newmap
[menu-bar
]
976 (append (make-sparse-keymap) menu-bar
))
977 (define-key newmap
[menu-bar index
]
978 (cons name
(nconc (make-sparse-keymap "Imenu")
979 (make-sparse-keymap))))
980 (use-local-map (append newmap
(current-local-map)))
981 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
982 (error "The mode `%s' does not support Imenu" mode-name
)))
985 (defun imenu-add-menubar-index ()
986 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
988 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
990 (imenu-add-to-menubar "Index"))
992 (defvar imenu-buffer-menubar nil
)
994 (defun imenu-update-menubar ()
995 (and (current-local-map)
996 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
997 (let ((index-alist (imenu--make-index-alist t
)))
998 ;; Don't bother updating if the index-alist has not changed
999 ;; since the last time we did it.
1000 (or (equal index-alist imenu--last-menubar-index-alist
)
1001 (let (menu menu1 old
)
1002 (setq imenu--last-menubar-index-alist index-alist
)
1003 (setq index-alist
(imenu--split-submenus index-alist
))
1004 (setq menu
(imenu--split-menu index-alist
1006 (setq menu1
(imenu--create-keymap-1 (car menu
)
1007 (if (< 1 (length (cdr menu
)))
1009 (cdr (car (cdr menu
))))
1011 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
1012 (setcdr old
(cdr menu1
)))))))
1014 (defun imenu--menubar-select (item)
1015 "Use Imenu to select the function or variable named in this menu item."
1016 (if (equal item imenu--rescan-item
)
1019 (setq imenu--index-alist nil
)
1020 (imenu-update-menubar))
1023 (defun imenu-default-goto-function (name position
&optional rest
)
1024 "Move the point to the given position.
1026 NAME is ignored. POSITION is where to move. REST is also ignored.
1027 The ignored args just make this function have the same interface as a
1028 function placed in a special index-item."
1029 (if (or (< position
(point-min))
1030 (> position
(point-max)))
1031 ;; widen if outside narrowing
1033 (goto-char position
))
1036 (defun imenu (index-item)
1037 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1038 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1039 for more information."
1040 (interactive (list (imenu-choose-buffer-index)))
1041 ;; Convert a string to an alist element.
1042 (if (stringp index-item
)
1043 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1047 (let* ((is-special-item (listp (cdr index-item
)))
1050 (nth 2 index-item
) imenu-default-goto-function
))
1051 (position (if is-special-item
1052 (cadr index-item
) (cdr index-item
)))
1053 (rest (if is-special-item
(cddr index-item
))))
1054 (apply function
(car index-item
) position rest
)))))
1058 ;;; imenu.el ends here