1 ;;; imenu.el --- framework for mode-specific buffer indexes
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
7 ;; Lars Lindberg <lli@sypro.cap.se>
10 ;; Keywords: tools convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Purpose of this package:
30 ;; To present a framework for mode-specific buffer indexes.
31 ;; A buffer index is an alist of names and buffer positions.
32 ;; For instance all functions in a C-file and their positions.
34 ;; It is documented in the Emacs Lisp manual.
38 ;; A mode-specific function is called to generate the index. It is
39 ;; then presented to the user, who can choose from this index.
41 ;; The package comes with a set of example functions for how to
42 ;; utilize this package.
44 ;; There are *examples* for index gathering functions/regular
45 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
46 ;; customize for other modes. A function for jumping to the chosen
47 ;; index position is also supplied.
51 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
52 ;; [dean] - Dean Andrews ada@unison.com
53 ;; [alon] - Alon Albert al@mercury.co.il
54 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
55 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
56 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
57 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
58 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
59 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
63 (eval-when-compile (require 'cl
))
65 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67 ;;; Customizable variables
69 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72 "Mode-specific buffer indexes."
76 :link
'(custom-manual "(elisp)Imenu"))
78 (defcustom imenu-use-markers t
79 "*Non-nil means use markers instead of integers for Imenu buffer positions.
81 Setting this to nil makes Imenu work a little faster but editing the
82 buffer will make the generated index positions wrong.
84 This might not yet be honored by all index-building functions."
89 (defcustom imenu-max-item-length
60
90 "*If a number, truncate Imenu entries to that length."
91 :type
'(choice integer
92 (const :tag
"Unlimited"))
95 (defcustom imenu-auto-rescan nil
96 "*Non-nil means Imenu should always rescan the buffers."
100 (defcustom imenu-auto-rescan-maxout
60000
101 "*Imenu auto-rescan is disabled in buffers larger than this size (in bytes).
102 This variable is buffer-local."
106 (defvar imenu-always-use-completion-buffer-p nil
)
107 (make-obsolete-variable 'imenu-always-use-completion-buffer-p
108 'imenu-use-popup-menu
"22.1")
110 (defcustom imenu-use-popup-menu
111 (if imenu-always-use-completion-buffer-p
112 (not (eq imenu-always-use-completion-buffer-p
'never
))
114 "Use a popup menu rather than a minibuffer prompt.
115 If nil, always use a minibuffer prompt.
116 If t, always use a popup menu,
117 If `on-mouse' use a popup menu when `imenu' was invoked with the mouse."
118 :type
'(choice (const :tag
"On Mouse" on-mouse
)
119 (const :tag
"Never" nil
)
120 (other :tag
"Always" t
))
123 (defcustom imenu-eager-completion-buffer
124 (not (eq imenu-always-use-completion-buffer-p
'never
))
125 "If non-nil, eagerly popup the completion buffer."
130 (defcustom imenu-after-jump-hook nil
131 "*Hooks called after jumping to a place in the buffer.
133 Useful things to use here include `reposition-window', `recenter', and
134 \(lambda () (recenter 0)) to show at top of screen."
139 (defcustom imenu-sort-function nil
140 "*The function to use for sorting the index mouse-menu.
142 Affects only the mouse index menu.
144 Set this to nil if you don't want any sorting (faster).
145 The items in the menu are then presented in the order they were found
148 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
150 The function should take two arguments and return t if the first
151 element should come before the second. The arguments are cons cells;
152 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
153 :type
'(choice (const :tag
"No sorting" nil
)
154 (const :tag
"Sort by name" imenu--sort-by-name
)
155 (function :tag
"Another function"))
158 (defcustom imenu-max-items
25
159 "*Maximum number of elements in a mouse menu for Imenu."
163 ;; No longer used. KFS 2004-10-27
164 ;; (defcustom imenu-scanning-message "Scanning buffer for index (%3d%%)"
165 ;; "*Progress message during the index scanning of the buffer.
166 ;; If non-nil, user gets a message during the scanning of the buffer.
168 ;; Relevant only if the mode-specific function that creates the buffer
169 ;; index use `imenu-progress-message', and not useful if that is fast, in
170 ;; which case you might as well set this to nil."
171 ;; :type '(choice string
172 ;; (const :tag "None" nil))
175 (defcustom imenu-space-replacement
"."
176 "*The replacement string for spaces in index names.
177 Used when presenting the index in a completion buffer to make the
178 names work as tokens."
179 :type
'(choice string
(const nil
))
182 (defcustom imenu-level-separator
":"
183 "*The separator between index names of different levels.
184 Used for making mouse-menu titles and for flattening nested indexes
185 with name concatenation."
190 (defvar imenu-generic-expression nil
191 "The regex pattern to use for creating a buffer index.
193 If non-nil this pattern is passed to `imenu--generic-function' to
194 create a buffer index. Look there for the documentation of this
197 For example, see the value of `fortran-imenu-generic-expression' used by
198 `fortran-mode' with `imenu-syntax-alist' set locally to give the
199 characters which normally have \"symbol\" syntax \"word\" syntax
203 (make-variable-buffer-local 'imenu-generic-expression
)
208 (defvar imenu-create-index-function
'imenu-default-create-index-function
209 "The function to use for creating an index alist of the current buffer.
211 It should be a function that takes no arguments and returns
212 an index alist of the current buffer. The function is
213 called within a `save-excursion'.
215 See `imenu--index-alist' for the format of the buffer index alist.")
217 (make-variable-buffer-local 'imenu-create-index-function
)
220 (defvar imenu-prev-index-position-function
'beginning-of-defun
221 "Function for finding the next index position.
223 If `imenu-create-index-function' is set to
224 `imenu-default-create-index-function', then you must set this variable
225 to a function that will find the next index, looking backwards in the
228 The function should leave point at the place to be connected to the
229 index and it should return nil when it doesn't find another index.")
231 (make-variable-buffer-local 'imenu-prev-index-position-function
)
234 (defvar imenu-extract-index-name-function nil
235 "Function for extracting the index item name, given a position.
237 This function is called after `imenu-prev-index-position-function'
238 finds a position for an index item, with point at that position.
239 It should return the name for that index item.")
241 (make-variable-buffer-local 'imenu-extract-index-name-function
)
244 (defvar imenu-name-lookup-function nil
245 "Function to compare string with index item.
247 This function will be called with two strings, and should return
248 non-nil if they match.
250 If nil, comparison is done with `string='.
251 Set this to some other function for more advanced comparisons,
252 such as \"begins with\" or \"name matches and number of
255 (make-variable-buffer-local 'imenu-name-lookup-function
)
258 (defvar imenu-default-goto-function
'imenu-default-goto-function
259 "The default function called when selecting an Imenu item.
260 The function in this variable is called when selecting a normal index-item.")
262 (make-variable-buffer-local 'imenu-default-goto-function
)
265 (defun imenu--subalist-p (item)
266 (and (consp (cdr item
)) (listp (cadr item
))
267 (not (eq (car (cadr item
)) 'lambda
))))
269 ;; Macro to display a progress message.
270 ;; RELPOS is the relative position to display.
271 ;; If RELPOS is nil, then the relative position in the buffer
273 ;; PREVPOS is the variable in which we store the last position displayed.
274 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
276 ;; Made obsolete/empty, as computers are now faster than the eye, and
277 ;; it had problems updating the messages correctly, and could shadow
278 ;; more important messages/prompts in the minibuffer. KFS 2004-10-27.
281 ;; imenu-scanning-message
282 ;; (let ((pos ,(if relpos
284 ;; `(imenu--relative-position ,reverse))))
286 ;; `(> pos (+ 5 ,prevpos)))
288 ;; (message imenu-scanning-message pos)
289 ;; (setq ,prevpos pos)))))
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ;;;; Some examples of functions utilizing the framework of this
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; FIXME: This is the only imenu-example-* definition that's actually used,
301 ;; and it seems to only be used by cperl-mode.el. We should just move it to
302 ;; cperl-mode.el and remove the rest.
303 (defun imenu-example--name-and-position ()
304 "Return the current/previous sexp and its (beginning) location.
308 ;; [ydi] modified for imenu-use-markers
309 (let ((beg (if imenu-use-markers
(point-marker) (point)))
310 (end (progn (forward-sexp) (point))))
311 (cons (buffer-substring beg end
)
318 (defun imenu-example--lisp-extract-index-name ()
319 ;; Example of a candidate for `imenu-extract-index-name-function'.
320 ;; This will generate a flat index of definitions in a lisp file.
322 (and (looking-at "(def")
328 (end (progn (forward-sexp -
1) (point))))
329 (buffer-substring beg end
)))
332 (defun imenu-example--create-lisp-index ()
333 ;; Example of a candidate for `imenu-create-index-function'.
334 ;; It will generate a nested index of definitions.
335 (let ((index-alist '())
336 (index-var-alist '())
337 (index-type-alist '())
338 (index-unknown-alist '())
340 (goto-char (point-max))
341 (imenu-progress-message prev-pos
0)
342 ;; Search for the function
343 (while (beginning-of-defun)
344 (imenu-progress-message prev-pos nil t
)
346 (and (looking-at "(def")
350 ((looking-at "def\\(var\\|const\\)")
352 (push (imenu-example--name-and-position)
354 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
356 (push (imenu-example--name-and-position)
358 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
360 (if (= (char-after (1- (point))) ?\
))
365 (push (imenu-example--name-and-position)
369 (push (imenu-example--name-and-position)
370 index-unknown-alist
)))))))
371 (imenu-progress-message prev-pos
100)
373 (push (cons "Variables" index-var-alist
)
375 (and index-type-alist
376 (push (cons "Types" index-type-alist
)
378 (and index-unknown-alist
379 (push (cons "Syntax-unknown" index-unknown-alist
)
383 ;; Regular expression to find C functions
384 (defvar imenu-example--function-name-regexp-c
386 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
387 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
388 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
389 "\\([*&]+[ \t]*\\)?" ; pointer
390 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
393 (defun imenu-example--create-c-index (&optional regexp
)
394 (let ((index-alist '())
396 (goto-char (point-min))
397 (imenu-progress-message prev-pos
0)
398 ;; Search for the function
400 (while (re-search-forward
401 (or regexp imenu-example--function-name-regexp-c
)
403 (imenu-progress-message prev-pos
)
406 (goto-char (scan-sexps (point) 1))
407 (setq char
(following-char)))
408 ;; Skip this function name if it is a prototype declaration.
409 (if (not (eq char ?\
;))
410 (push (imenu-example--name-and-position) index-alist
))))
411 (imenu-progress-message prev-pos
100)
412 (nreverse index-alist
)))
415 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
417 ;;; Internal variables
419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
421 ;; The item to use in the index for rescanning the buffer.
422 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
424 ;; The latest buffer index.
426 (defvar imenu--index-alist nil
427 "The buffer index alist computed for this buffer in Imenu.
429 Simple elements in the alist look like (INDEX-NAME . POSITION).
430 POSITION is the buffer position of the item; to go to the item
431 is simply to move point to that position.
433 Special elements look like (INDEX-NAME POSITION FUNCTION ARGUMENTS...).
434 To \"go to\" a special element means applying FUNCTION
435 to INDEX-NAME, POSITION, and the ARGUMENTS.
437 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
438 The function `imenu--subalist-p' tests an element and returns t
439 if it is a sub-alist.
441 There is one simple element with negative POSITION; selecting that
442 element recalculates the buffer's index alist.")
444 (make-variable-buffer-local 'imenu--index-alist
)
446 (defvar imenu--last-menubar-index-alist nil
447 "The latest buffer index alist used to update the menu bar menu.")
449 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
451 ;; History list for 'jump-to-function-in-buffer'.
452 ;; Making this buffer local caused it not to work!
453 (defvar imenu--history-list nil
)
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 ;;; Internal support functions
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
463 ;;; Sorts the items depending on their index name.
464 ;;; An item looks like (NAME . POSITION).
466 (defun imenu--sort-by-name (item1 item2
)
467 (string-lessp (car item1
) (car item2
)))
469 (defun imenu--sort-by-position (item1 item2
)
470 (< (cdr item1
) (cdr item2
)))
472 (defun imenu--relative-position (&optional reverse
)
473 ;; Support function to calculate relative position in buffer
474 ;; Beginning of buffer is 0 and end of buffer is 100
475 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
477 (total (buffer-size)))
478 (and reverse
(setq pos
(- total pos
)))
480 ;; Avoid overflow from multiplying by 100!
481 (/ (1- pos
) (max (/ total
100) 1))
482 (/ (* 100 (1- pos
)) (max total
1)))))
484 ;; Split LIST into sublists of max length N.
485 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
486 (defun imenu--split (list n
)
492 (push (pop remain
) sublist
)
495 ;; We have finished a sublist
496 (progn (push (nreverse sublist
) result
)
498 (setq sublist
'()))))
499 ;; There might be a sublist (if the length of LIST mod n is != 0)
500 ;; that has to be added to the result list.
502 (push (nreverse sublist
) result
))
505 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
506 ;;; In any case, add TITLE to the front of the alist.
507 (defun imenu--split-menu (menulist title
)
508 (let (keep-at-top tail
)
509 (if (memq imenu--rescan-item menulist
)
510 (setq keep-at-top
(cons imenu--rescan-item nil
)
511 menulist
(delq imenu--rescan-item menulist
)))
514 (when (imenu--subalist-p item
)
515 (push item keep-at-top
)
516 (setq menulist
(delq item menulist
))))
517 (if imenu-sort-function
518 (setq menulist
(sort (copy-sequence menulist
) imenu-sort-function
)))
519 (if (> (length menulist
) imenu-max-items
)
523 (cons (format "From: %s" (caar menu
)) menu
))
524 (imenu--split menulist imenu-max-items
))))
526 (nconc (nreverse keep-at-top
) menulist
))))
528 ;;; Split up each long alist that are nested within ALIST
529 ;;; into nested alists.
530 (defun imenu--split-submenus (alist)
536 (imenu--split-menu (cdr elt
) (car elt
))
540 ;;; Truncate all strings in MENULIST to imenu-max-item-length
541 (defun imenu--truncate-items (menulist)
546 (imenu--truncate-items (cdr item
)))
547 ;; truncate if necessary
548 ((and (numberp imenu-max-item-length
)
549 (> (length (car item
)) imenu-max-item-length
))
550 (setcar item
(substring (car item
) 0 imenu-max-item-length
))))))
554 (defun imenu--make-index-alist (&optional noerror
)
555 "Create an index alist for the definitions in the current buffer.
556 This works by using the hook function `imenu-create-index-function'.
557 Report an error if the list is empty unless NOERROR is supplied and
560 See `imenu--index-alist' for the format of the index alist."
561 (or (and imenu--index-alist
562 (or (not imenu-auto-rescan
)
563 (and imenu-auto-rescan
564 (> (buffer-size) imenu-auto-rescan-maxout
))))
565 ;; Get the index; truncate if necessary
567 (setq imenu--index-alist
571 (funcall imenu-create-index-function
))))
572 (imenu--truncate-items imenu--index-alist
)))
573 (or imenu--index-alist noerror
574 (error "No items suitable for an index found in this buffer"))
575 (or imenu--index-alist
576 (setq imenu--index-alist
(list nil
)))
577 ;; Add a rescan option to the index.
578 (cons imenu--rescan-item imenu--index-alist
))
580 ;;; Find all markers in alist and makes
581 ;;; them point nowhere.
582 ;;; The top-level call uses nil as the argument;
583 ;;; non-nil arguments are in recursivecalls.
584 (defvar imenu--cleanup-seen
)
586 (defun imenu--cleanup (&optional alist
)
587 ;; If alist is provided use that list.
588 ;; If not, empty the table of lists already seen
589 ;; and use imenu--index-alist.
591 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
592 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
598 ((markerp (cdr item
))
599 (set-marker (cdr item
) nil
))
600 ;; Don't process one alist twice.
601 ((memq (cdr item
) imenu--cleanup-seen
))
602 ((imenu--subalist-p item
)
603 (imenu--cleanup (cdr item
)))))
607 (defun imenu--create-keymap (title alist
&optional cmd
)
611 (list* (car item
) (car item
)
613 ((imenu--subalist-p item
)
614 (imenu--create-keymap (car item
) (cdr item
) cmd
))
616 `(lambda () (interactive)
617 ,(if cmd
`(,cmd
',item
) (list 'quote item
)))))))
620 (defun imenu--in-alist (str alist
)
621 "Check whether the string STR is contained in multi-level ALIST."
622 (let (elt head tail res
)
625 (setq elt
(car alist
)
629 ;; A nested ALIST element looks like
630 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
631 ;; while a bottom-level element looks like
632 ;; (INDEX-NAME . INDEX-POSITION)
633 ;; We are only interested in the bottom-level elements, so we need to
634 ;; recurse if TAIL is a list.
636 (if (setq res
(imenu--in-alist str tail
))
638 ((if imenu-name-lookup-function
639 (funcall imenu-name-lookup-function str head
)
641 (setq alist nil res elt
))))
644 (defvar imenu-syntax-alist nil
645 "Alist of syntax table modifiers to use while in `imenu--generic-function'.
647 The car of the assocs may be either a character or a string and the
648 cdr is a syntax description appropriate for `modify-syntax-entry'. For
649 a string, all the characters in the string get the specified syntax.
651 This is typically used to give word syntax to characters which
652 normally have symbol syntax to simplify `imenu-expression'
653 and speed-up matching.")
655 (make-variable-buffer-local 'imenu-syntax-alist
)
657 (defun imenu-default-create-index-function ()
658 "*Default function to create an index alist of the current buffer.
660 The most general method is to move point to end of buffer, then repeatedly call
661 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
662 All the results returned by the latter are gathered into an index alist.
663 This method is used if those two variables are non-nil.
665 The alternate method, which is the one most often used, is to call
666 `imenu--generic-function' with `imenu-generic-expression' as argument."
667 ;; These should really be done by setting imenu-create-index-function
668 ;; in these major modes. But save that change for later.
669 (cond ((and imenu-prev-index-position-function
670 imenu-extract-index-name-function
)
671 (let ((index-alist '())
673 (goto-char (point-max))
674 (imenu-progress-message prev-pos
0 t
)
675 ;; Search for the function
676 (while (funcall imenu-prev-index-position-function
)
677 (imenu-progress-message prev-pos nil t
)
679 (setq name
(funcall imenu-extract-index-name-function
)))
681 ;; [ydi] updated for imenu-use-markers
682 (push (cons name
(if imenu-use-markers
(point-marker) (point)))
684 (imenu-progress-message prev-pos
100 t
)
686 ;; Use generic expression if possible.
687 ((and imenu-generic-expression
)
688 (imenu--generic-function imenu-generic-expression
))
690 (error "This buffer cannot use `imenu-default-create-index-function'"))))
693 ;;; Generic index gathering function.
696 (defvar imenu-case-fold-search t
697 "Defines whether `imenu--generic-function' should fold case when matching.
699 This variable should be set (only) by initialization code
700 for modes which use `imenu--generic-function'. If it is not set, but
701 `font-lock-defaults' is set, then font-lock's setting is used.")
703 (make-variable-buffer-local 'imenu-case-fold-search
)
705 ;; This function can be called with quitting disabled,
706 ;; so it needs to be careful never to loop!
707 (defun imenu--generic-function (patterns)
708 "Return an index alist of the current buffer based on PATTERNS.
710 PATTERNS is an alist with elements that look like this:
711 (MENU-TITLE REGEXP INDEX)
713 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
714 with zero or more ARGUMENTS. The former format creates a simple
715 element in the index alist when it matches; the latter creates a
716 special element of the form (INDEX-NAME POSITION-MARKER FUNCTION
717 ARGUMENTS...) with FUNCTION and ARGUMENTS copied from PATTERNS.
719 MENU-TITLE is a string used as the title for the submenu or nil
720 if the entries are not nested.
722 REGEXP is a regexp that should match a construct in the buffer
723 that is to be displayed in the menu; i.e., function or variable
724 definitions, etc. It contains a substring which is the name to
725 appear in the menu. See the info section on Regexps for more
726 information. REGEXP may also be a function, called without
727 arguments. It is expected to search backwards. It shall return
728 true and set `match-data' if it finds another element.
730 INDEX points to the substring in REGEXP that contains the
731 name (of the function, variable or type) that is to appear in the
734 The variable `imenu-case-fold-search' determines whether or not the
735 regexp matches are case sensitive, and `imenu-syntax-alist' can be
736 used to alter the syntax table for the search.
738 See `lisp-imenu-generic-expression' for an example of PATTERNS.
740 Returns an index of the current buffer as an alist. The elements in
742 (INDEX-NAME . INDEX-POSITION)
744 (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
745 They may also be nested index alists like:
746 (INDEX-NAME . INDEX-ALIST)
747 depending on PATTERNS."
749 (let ((index-alist (list 'dummy
))
751 (case-fold-search (if (or (local-variable-p 'imenu-case-fold-search
)
752 (not (local-variable-p 'font-lock-defaults
)))
753 imenu-case-fold-search
754 (nth 2 font-lock-defaults
)))
755 (old-table (syntax-table))
756 (table (copy-syntax-table (syntax-table)))
757 (slist imenu-syntax-alist
))
758 ;; Modify the syntax table used while matching regexps.
760 ;; The character(s) to modify may be a single char or a string.
761 (if (numberp (car syn
))
762 (modify-syntax-entry (car syn
) (cdr syn
) table
)
764 (modify-syntax-entry c
(cdr syn
) table
))
766 (goto-char (point-max))
767 (imenu-progress-message prev-pos
0 t
)
768 (unwind-protect ; for syntax table
770 (set-syntax-table table
)
772 ;; map over the elements of imenu-generic-expression
773 ;; (typically functions, variables ...)
774 (dolist (pat patterns
)
775 (let ((menu-title (car pat
))
778 (function (nth 3 pat
))
779 (rest (nthcdr 4 pat
))
781 ;; Go backwards for convenience of adding items in order.
782 (goto-char (point-max))
783 (while (and (if (functionp regexp
)
785 (re-search-backward regexp nil t
))
786 ;; Exit the loop if we get an empty match,
787 ;; because it means a bad regexp was specified.
788 (not (= (match-beginning 0) (match-end 0))))
790 ;; Record the start of the line in which the match starts.
791 ;; That's the official position of this definition.
792 (goto-char (match-beginning index
))
795 (imenu-progress-message prev-pos nil t
)
796 ;; Add this sort of submenu only when we've found an
797 ;; item for it, avoiding empty, duff menus.
798 (unless (assoc menu-title index-alist
)
799 (push (list menu-title
) index-alist
))
800 (if imenu-use-markers
801 (setq beg
(copy-marker beg
)))
804 (nconc (list (match-string-no-properties index
)
807 (cons (match-string-no-properties index
)
809 ;; This is the desired submenu,
810 ;; starting with its title (or nil).
811 (menu (assoc menu-title index-alist
)))
812 ;; Insert the item unless it is already present.
813 (unless (member item
(cdr menu
))
815 (cons item
(cdr menu
)))))
816 ;; Go to the start of the match, to make sure we
817 ;; keep making progress backwards.
819 (set-syntax-table old-table
)))
820 (imenu-progress-message prev-pos
100 t
)
821 ;; Sort each submenu by position.
822 ;; This is in case one submenu gets items from two different regexps.
823 (dolist (item index-alist
)
825 (setcdr item
(sort (cdr item
) 'imenu--sort-by-position
))))
826 (let ((main-element (assq nil index-alist
)))
827 (nconc (delq main-element
(delq 'dummy index-alist
))
828 (cdr main-element
)))))
830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
832 ;;; The main functions for this package!
834 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
836 ;; See also info-lookup-find-item
837 (defun imenu-find-default (guess completions
)
838 "Fuzzily find an item based on GUESS inside the alist COMPLETIONS."
840 (let ((case-fold-search t
))
841 (if (assoc guess completions
) guess
842 (dolist (re (list (concat "\\`" (regexp-quote guess
) "\\'")
843 (concat "\\`" (regexp-quote guess
))
844 (concat (regexp-quote guess
) "\\'")
845 (regexp-quote guess
)))
846 (dolist (x completions
)
847 (if (string-match re
(car x
)) (throw 'found
(car x
)))))))))
849 (defun imenu--completion-buffer (index-alist &optional prompt
)
850 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
852 Return one of the entries in index-alist or nil."
853 ;; Create a list for this buffer only when needed.
854 (let ((name (thing-at-point 'symbol
))
856 (prepared-index-alist
857 (if (not imenu-space-replacement
) index-alist
860 (cons (subst-char-in-string ?\s
(aref imenu-space-replacement
0)
865 (setq name
(or (imenu-find-default name prepared-index-alist
) name
)))
867 ((and name
(imenu--in-alist name prepared-index-alist
))
868 (setq prompt
(format "Index item (default %s): " name
)))
869 (t (setq prompt
"Index item: ")))
870 (let ((minibuffer-setup-hook minibuffer-setup-hook
))
871 ;; Display the completion buffer.
872 (if (not imenu-eager-completion-buffer
)
873 (add-hook 'minibuffer-setup-hook
'minibuffer-completion-help
))
874 (setq name
(completing-read prompt
876 nil t nil
'imenu--history-list name
)))
879 (setq choice
(assoc name prepared-index-alist
))
880 (if (imenu--subalist-p choice
)
881 (imenu--completion-buffer (cdr choice
) prompt
)
884 (defun imenu--mouse-menu (index-alist event
&optional title
)
885 "Let the user select from a buffer index from a mouse menu.
887 INDEX-ALIST is the buffer index and EVENT is a mouse event.
889 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
890 (setq index-alist
(imenu--split-submenus index-alist
))
891 (let* ((menu (imenu--split-menu index-alist
(or title
(buffer-name))))
892 (map (imenu--create-keymap (car menu
)
893 (cdr (if (< 1 (length (cdr menu
)))
895 (car (cdr menu
)))))))
896 (popup-menu map event
)))
898 (defun imenu-choose-buffer-index (&optional prompt alist
)
899 "Let the user select from a buffer index and return the chosen index.
901 If the user originally activated this function with the mouse, a mouse
902 menu is used. Otherwise a completion buffer is used and the user is
903 prompted with PROMPT.
905 If you call this function with index alist ALIST, then it lets the user
908 With no index alist ALIST, it calls `imenu--make-index-alist' to
909 create the index alist.
911 If `imenu-use-popup-menu' is nil, then the completion buffer
912 is always used, no matter if the mouse was used or not.
914 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
916 (mouse-triggered (listp last-nonmenu-event
))
918 ;; If selected by mouse, see to that the window where the mouse is
919 ;; really is selected.
921 (not (equal last-nonmenu-event
'(menu-bar)))
922 (let ((window (posn-window (event-start last-nonmenu-event
))))
923 (or (framep window
) (null window
) (select-window window
))))
924 ;; Create a list for this buffer only when needed.
926 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
928 (if (and imenu-use-popup-menu
929 (or (eq imenu-use-popup-menu t
) mouse-triggered
))
930 (imenu--mouse-menu index-alist last-nonmenu-event
)
931 (imenu--completion-buffer index-alist prompt
)))
932 (and (equal result imenu--rescan-item
)
934 (setq result t imenu--index-alist nil
)))
938 (defun imenu-add-to-menubar (name)
939 "Add an `imenu' entry to the menu bar for the current buffer.
940 NAME is a string used to name the menu bar item.
941 See the command `imenu' for more information."
942 (interactive "sImenu menu item name: ")
943 (if (or (and imenu-prev-index-position-function
944 imenu-extract-index-name-function
)
945 imenu-generic-expression
946 (not (eq imenu-create-index-function
947 'imenu-default-create-index-function
)))
948 (let ((newmap (make-sparse-keymap)))
949 (set-keymap-parent newmap
(current-local-map))
950 (setq imenu--last-menubar-index-alist nil
)
951 (define-key newmap
[menu-bar index
]
952 `(menu-item ,name
,(make-sparse-keymap "Imenu")))
953 (use-local-map newmap
)
954 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
955 (error "The mode `%s' does not support Imenu"
956 (format-mode-line mode-name
))))
959 (defun imenu-add-menubar-index ()
960 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
962 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
964 (imenu-add-to-menubar "Index"))
966 (defvar imenu-buffer-menubar nil
)
968 (defvar imenu-menubar-modified-tick
0
969 "The value of (buffer-chars-modified-tick) as of the last call
970 to `imenu-update-menubar'.")
971 (make-variable-buffer-local 'imenu-menubar-modified-tick
)
973 (defun imenu-update-menubar ()
974 (when (and (current-local-map)
975 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
976 (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick
))
977 (setq imenu-menubar-modified-tick
(buffer-chars-modified-tick))
978 (let ((index-alist (imenu--make-index-alist t
)))
979 ;; Don't bother updating if the index-alist has not changed
980 ;; since the last time we did it.
981 (unless (equal index-alist imenu--last-menubar-index-alist
)
982 (let (menu menu1 old
)
983 (setq imenu--last-menubar-index-alist index-alist
)
984 (setq index-alist
(imenu--split-submenus index-alist
))
985 (setq menu
(imenu--split-menu index-alist
987 (setq menu1
(imenu--create-keymap (car menu
)
988 (cdr (if (< 1 (length (cdr menu
)))
991 'imenu--menubar-select
))
992 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
993 (setcdr old
(cdr menu1
)))))))
995 (defun imenu--menubar-select (item)
996 "Use Imenu to select the function or variable named in this menu ITEM."
997 (if (equal item imenu--rescan-item
)
1000 ;; Make sure imenu-update-menubar redoes everything.
1001 (setq imenu-menubar-modified-tick -
1)
1002 (setq imenu--index-alist nil
)
1003 (setq imenu--last-menubar-index-alist nil
)
1004 (imenu-update-menubar)
1009 (defun imenu-default-goto-function (name position
&optional rest
)
1010 "Move to the given position.
1012 NAME is ignored. POSITION is where to move. REST is also ignored.
1013 The ignored args just make this function have the same interface as a
1014 function placed in a special index-item."
1015 (if (or (< position
(point-min))
1016 (> position
(point-max)))
1017 ;; widen if outside narrowing
1019 (goto-char position
))
1022 (defun imenu (index-item)
1023 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1024 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1025 for more information."
1026 (interactive (list (imenu-choose-buffer-index)))
1027 ;; Convert a string to an alist element.
1028 (if (stringp index-item
)
1029 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1032 (let* ((is-special-item (listp (cdr index-item
)))
1035 (nth 2 index-item
) imenu-default-goto-function
))
1036 (position (if is-special-item
1037 (cadr index-item
) (cdr index-item
)))
1038 (rest (if is-special-item
(cddr index-item
))))
1039 (apply function
(car index-item
) position rest
))
1040 (run-hooks 'imenu-after-jump-hook
)))
1043 '("^No items suitable for an index found in this buffer$"
1044 "^This buffer cannot use `imenu-default-create-index-function'$"
1045 "^The mode `.*' does not support Imenu$"))
1046 (add-to-list 'debug-ignored-errors mess
))
1050 ;; arch-tag: 98a2f5f5-4b91-4704-b18c-3aacf77d77a7
1051 ;;; imenu.el ends here