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-after-jump-hook nil
120 "*Hooks called after jumping to a place in the buffer.
122 Useful things to use here include `reposition-window', `recenter', and
123 \(lambda () (recenter 0)) to show at top of screen."
128 (defcustom imenu-sort-function nil
129 "*The function to use for sorting the index mouse-menu.
131 Affects only the mouse index menu.
133 Set this to nil if you don't want any sorting (faster).
134 The items in the menu are then presented in the order they were found
137 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
139 The function should take two arguments and return t if the first
140 element should come before the second. The arguments are cons cells;
141 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
142 :type
'(choice (const :tag
"No sorting" nil
)
143 (const :tag
"Sort by name" imenu--sort-by-name
)
144 (function :tag
"Another function"))
147 (defcustom imenu-max-items
25
148 "*Maximum number of elements in a mouse menu for Imenu."
152 (defcustom imenu-scanning-message
"Scanning buffer for index (%3d%%)"
153 "*Progress message during the index scanning of the buffer.
154 If non-nil, user gets a message during the scanning of the buffer.
156 Relevant only if the mode-specific function that creates the buffer
157 index use `imenu-progress-message', and not useful if that is fast, in
158 which case you might as well set this to nil."
159 :type
'(choice string
160 (const :tag
"None" nil
))
163 (defcustom imenu-space-replacement
"."
164 "*The replacement string for spaces in index names.
165 Used when presenting the index in a completion buffer to make the
166 names work as tokens."
170 (defcustom imenu-level-separator
":"
171 "*The separator between index names of different levels.
172 Used for making mouse-menu titles and for flattening nested indexes
173 with name concatenation."
178 (defvar imenu-generic-expression nil
179 "The regex pattern to use for creating a buffer index.
181 If non-nil this pattern is passed to `imenu--generic-function'
182 to create a buffer index.
184 The value should be an alist with elements that look like this:
185 (MENU-TITLE REGEXP INDEX)
187 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
188 with zero or more ARGUMENTS. The former format creates a simple element in
189 the index alist when it matches; the latter creates a special element
190 of the form (NAME FUNCTION POSITION-MARKER ARGUMENTS...)
191 with FUNCTION and ARGUMENTS beiong copied from `imenu-generic-expression'.
193 MENU-TITLE is a string used as the title for the submenu or nil if the
194 entries are not nested.
196 REGEXP is a regexp that should match a construct in the buffer that is
197 to be displayed in the menu; i.e., function or variable definitions,
198 etc. It contains a substring which is the name to appear in the
199 menu. See the info section on Regexps for more information.
201 INDEX points to the substring in REGEXP that contains the name (of the
202 function, variable or type) that is to appear in the menu.
204 The variable is buffer-local.
206 The variable `imenu-case-fold-search' determines whether or not the
207 regexp matches are case sensitive. and `imenu-syntax-alist' can be
208 used to alter the syntax table for the search.
210 For example, see the value of `lisp-imenu-generic-expression' used by
211 `lisp-mode' and `emacs-lisp-mode' with `imenu-syntax-alist' set
212 locally to give the characters which normally have \"punctuation\"
213 syntax \"word\" syntax during matching.")
216 (make-variable-buffer-local 'imenu-generic-expression
)
221 (defvar imenu-create-index-function
'imenu-default-create-index-function
222 "The function to use for creating a buffer index.
224 It should be a function that takes no arguments and returns an index
225 of the current buffer as an alist.
227 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
228 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
229 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
230 The function `imenu--subalist-p' tests an element and returns t
231 if it is a sub-alist.
233 This function is called within a `save-excursion'.
235 The variable is buffer-local.")
237 (make-variable-buffer-local 'imenu-create-index-function
)
240 (defvar imenu-prev-index-position-function
'beginning-of-defun
241 "Function for finding the next index position.
243 If `imenu-create-index-function' is set to
244 `imenu-default-create-index-function', then you must set this variable
245 to a function that will find the next index, looking backwards in the
248 The function should leave point at the place to be connected to the
249 index and it should return nil when it doesn't find another index.
251 This variable is local in all buffers.")
253 (make-variable-buffer-local 'imenu-prev-index-position-function
)
256 (defvar imenu-extract-index-name-function nil
257 "Function for extracting the index item name, given a position.
259 This function is called after `imenu-prev-index-position-function'
260 finds a position for an index item, with point at that position.
261 It should return the name for that index item.
263 This variable is local in all buffers.")
265 (make-variable-buffer-local 'imenu-extract-index-name-function
)
268 (defvar imenu-name-lookup-function nil
269 "Function to compare string with index item.
271 This function will be called with two strings, and should return
272 non-nil if they match.
274 If nil, comparison is done with `string='.
275 Set this to some other function for more advanced comparisons,
276 such as \"begins with\" or \"name matches and number of
279 This variable is local in all buffers.")
281 (make-variable-buffer-local 'imenu-name-lookup-function
)
284 (defvar imenu-default-goto-function
'imenu-default-goto-function
285 "The default function called when selecting an Imenu item.
286 The function in this variable is called when selecting a normal index-item.")
288 (make-variable-buffer-local 'imenu-default-goto-function
)
291 (defun imenu--subalist-p (item)
292 (and (consp (cdr item
)) (listp (cadr item
))
293 (not (eq (car (cadr item
)) 'lambda
))))
295 ;; Macro to display a progress message.
296 ;; RELPOS is the relative position to display.
297 ;; If RELPOS is nil, then the relative position in the buffer
299 ;; PREVPOS is the variable in which we store the last position displayed.
300 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
302 imenu-scanning-message
303 (let ((pos ,(if relpos
305 `(imenu--relative-position ,reverse
))))
307 `(> pos
(+ 5 ,prevpos
)))
309 (message imenu-scanning-message pos
)
310 (setq ,prevpos pos
))))))
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
315 ;;;; Some examples of functions utilizing the framework of this
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;; Return the current/previous sexp and the location of the sexp (its
321 ;; beginning) without moving the point.
322 (defun imenu-example--name-and-position ()
325 ;; [ydi] modified for imenu-use-markers
326 (let ((beg (if imenu-use-markers
(point-marker) (point)))
327 (end (progn (forward-sexp) (point))))
328 (cons (buffer-substring beg end
)
335 (defun imenu-example--lisp-extract-index-name ()
336 ;; Example of a candidate for `imenu-extract-index-name-function'.
337 ;; This will generate a flat index of definitions in a lisp file.
339 (and (looking-at "(def")
345 (end (progn (forward-sexp -
1) (point))))
346 (buffer-substring beg end
)))
349 (defun imenu-example--create-lisp-index ()
350 ;; Example of a candidate for `imenu-create-index-function'.
351 ;; It will generate a nested index of definitions.
352 (let ((index-alist '())
353 (index-var-alist '())
354 (index-type-alist '())
355 (index-unknown-alist '())
357 (goto-char (point-max))
358 (imenu-progress-message prev-pos
0)
359 ;; Search for the function
360 (while (beginning-of-defun)
361 (imenu-progress-message prev-pos nil t
)
363 (and (looking-at "(def")
367 ((looking-at "def\\(var\\|const\\)")
369 (push (imenu-example--name-and-position)
371 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
373 (push (imenu-example--name-and-position)
375 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
377 (if (= (char-after (1- (point))) ?\
))
382 (push (imenu-example--name-and-position)
386 (push (imenu-example--name-and-position)
387 index-unknown-alist
)))))))
388 (imenu-progress-message prev-pos
100)
390 (push (cons "Variables" index-var-alist
)
392 (and index-type-alist
393 (push (cons "Types" index-type-alist
)
395 (and index-unknown-alist
396 (push (cons "Syntax-unknown" index-unknown-alist
)
400 ;; Regular expression to find C functions
401 (defvar imenu-example--function-name-regexp-c
403 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
404 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
405 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
406 "\\([*&]+[ \t]*\\)?" ; pointer
407 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
410 (defun imenu-example--create-c-index (&optional regexp
)
411 (let ((index-alist '())
413 (goto-char (point-min))
414 (imenu-progress-message prev-pos
0)
415 ;; Search for the function
417 (while (re-search-forward
418 (or regexp imenu-example--function-name-regexp-c
)
420 (imenu-progress-message prev-pos
)
423 (goto-char (scan-sexps (point) 1))
424 (setq char
(following-char)))
425 ;; Skip this function name if it is a prototype declaration.
426 (if (not (eq char ?\
;))
427 (push (imenu-example--name-and-position) index-alist
))))
428 (imenu-progress-message prev-pos
100)
429 (nreverse index-alist
)))
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 ;;; Internal variables
436 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438 ;; The item to use in the index for rescanning the buffer.
439 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
441 ;; The latest buffer index.
443 (defvar imenu--index-alist nil
444 "The buffer index computed for this buffer in Imenu.
445 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
446 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
447 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
449 This variable is local in all buffers, once set.")
451 (make-variable-buffer-local 'imenu--index-alist
)
453 (defvar imenu--last-menubar-index-alist nil
454 "The latest buffer index used to update the menu bar menu.")
456 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
458 ;; History list for 'jump-to-function-in-buffer'.
459 ;; Making this buffer local caused it not to work!
460 (defvar imenu--history-list nil
)
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 ;;; Internal support functions
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;;; Sorts the items depending on their index name.
471 ;;; An item looks like (NAME . POSITION).
473 (defun imenu--sort-by-name (item1 item2
)
474 (string-lessp (car item1
) (car item2
)))
476 (defun imenu--sort-by-position (item1 item2
)
477 (< (cdr item1
) (cdr item2
)))
479 (defun imenu--relative-position (&optional reverse
)
480 ;; Support function to calculate relative position in buffer
481 ;; Beginning of buffer is 0 and end of buffer is 100
482 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
484 (total (buffer-size)))
485 (and reverse
(setq pos
(- total pos
)))
487 ;; Avoid overflow from multiplying by 100!
488 (/ (1- pos
) (max (/ total
100) 1))
489 (/ (* 100 (1- pos
)) (max total
1)))))
491 ;; Split LIST into sublists of max length N.
492 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
493 (defun imenu--split (list n
)
499 (push (pop remain
) sublist
)
502 ;; We have finished a sublist
503 (progn (push (nreverse sublist
) result
)
505 (setq sublist
'()))))
506 ;; There might be a sublist (if the length of LIST mod n is != 0)
507 ;; that has to be added to the result list.
509 (push (nreverse sublist
) result
))
512 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
513 ;;; In any case, add TITLE to the front of the alist.
514 (defun imenu--split-menu (menulist title
)
515 (let (keep-at-top tail
)
516 (if (memq imenu--rescan-item menulist
)
517 (setq keep-at-top
(cons imenu--rescan-item nil
)
518 menulist
(delq imenu--rescan-item menulist
)))
521 (if (imenu--subalist-p (car tail
))
522 (setq keep-at-top
(cons (car tail
) keep-at-top
)
523 menulist
(delq (car tail
) menulist
)))
524 (setq tail
(cdr tail
)))
525 (if imenu-sort-function
530 ;; Copy list method from the cl package `copy-list'
531 (while (consp oldlist
) (push (pop oldlist
) res
))
532 (if res
; in case, e.g. no functions defined
533 (prog1 (nreverse res
) (setcdr res oldlist
))))
534 imenu-sort-function
)))
535 (if (> (length menulist
) imenu-max-items
)
541 (cons (format "From: %s" (caar menu
)) menu
)))
542 (imenu--split menulist imenu-max-items
)))))
544 (nconc (nreverse keep-at-top
) menulist
))))
546 ;;; Split up each long alist that are nested within ALIST
547 ;;; into nested alists.
548 (defun imenu--split-submenus (alist)
554 (imenu--split-menu (cdr elt
) (car elt
))
558 ;;; Truncate all strings in MENULIST to imenu-max-item-length
559 (defun imenu--truncate-items (menulist)
564 (imenu--truncate-items (cdr item
)))
566 ;; truncate if necessary
567 (if (and (numberp imenu-max-item-length
)
568 (> (length (car item
)) imenu-max-item-length
))
569 (setcar item
(substring (car item
) 0 imenu-max-item-length
)))))))
573 (defun imenu--make-index-alist (&optional noerror
)
574 "Create an index-alist for the definitions in the current buffer.
576 Report an error if the list is empty unless NOERROR is supplied and
579 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
580 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
581 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
582 The function `imenu--subalist-p' tests an element and returns t
583 if it is a sub-alist.
585 There is one simple element with negative POSITION; that's intended
586 as a way for the user to ask to recalculate the buffer's index alist."
587 (or (and imenu--index-alist
588 (or (not imenu-auto-rescan
)
589 (and imenu-auto-rescan
590 (> (buffer-size) imenu-auto-rescan-maxout
))))
591 ;; Get the index; truncate if necessary
593 (setq imenu--index-alist
597 (funcall imenu-create-index-function
))))
598 (imenu--truncate-items imenu--index-alist
)))
599 (or imenu--index-alist noerror
600 (error "No items suitable for an index found in this buffer"))
601 (or imenu--index-alist
602 (setq imenu--index-alist
(list nil
)))
603 ;; Add a rescan option to the index.
604 (cons imenu--rescan-item imenu--index-alist
))
606 ;;; Find all markers in alist and makes
607 ;;; them point nowhere.
608 ;;; The top-level call uses nil as the argument;
609 ;;; non-nil arguments are in recursivecalls.
610 (defvar imenu--cleanup-seen
)
612 (defun imenu--cleanup (&optional alist
)
613 ;; If alist is provided use that list.
614 ;; If not, empty the table of lists already seen
615 ;; and use imenu--index-alist.
617 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
618 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
625 ((markerp (cdr item
))
626 (set-marker (cdr item
) nil
))
627 ;; Don't process one alist twice.
628 ((memq (cdr item
) imenu--cleanup-seen
))
629 ((imenu--subalist-p item
)
630 (imenu--cleanup (cdr item
))))))
634 (defun imenu--create-keymap-2 (alist counter
&optional commands
)
640 ((imenu--subalist-p item
)
641 (append (list (setq counter
(1+ counter
))
642 (car item
) 'keymap
(car item
))
643 (imenu--create-keymap-2 (cdr item
) (+ counter
10) commands
)))
645 (let ((end (if commands
`(lambda () (interactive)
646 (imenu--menubar-select ',item
))
647 (cons '(nil) item
))))
649 (cons (car item
) end
)))))))
652 ;; If COMMANDS is non-nil, make a real keymap
653 ;; with a real command used as the definition.
654 ;; If it is nil, make something suitable for x-popup-menu.
655 (defun imenu--create-keymap-1 (title alist
&optional commands
)
656 (append (list 'keymap title
) (imenu--create-keymap-2 alist
0 commands
)))
658 (defun imenu--in-alist (str alist
)
659 "Check whether the string STR is contained in multi-level ALIST."
660 (let (elt head tail res
)
663 (setq elt
(car alist
)
667 ;; A nested ALIST element looks like
668 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
669 ;; while a bottom-level element looks like
670 ;; (INDEX-NAME . INDEX-POSITION)
671 ;; We are only interested in the bottom-level elements, so we need to
672 ;; recurse if TAIL is a list.
674 (if (setq res
(imenu--in-alist str tail
))
676 ((if imenu-name-lookup-function
677 (funcall imenu-name-lookup-function str head
)
679 (setq alist nil res elt
))))
682 (defvar imenu-syntax-alist nil
683 "Alist of syntax table modifiers to use while executing `imenu--generic-function'.
685 The car of the assocs may be either a character or a string and the
686 cdr is a syntax description appropriate fo `modify-syntax-entry'. For
687 a string, all the characters in the string get the specified syntax.
689 This is typically used to give word syntax to characters which
690 normally have symbol syntax to simplify `imenu-expression'
691 and speed-up matching.")
692 (make-variable-buffer-local 'imenu-syntax-alist
)
694 (defun imenu-default-create-index-function ()
695 "*Wrapper for index searching functions.
697 Moves point to end of buffer and then repeatedly calls
698 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
699 Their results are gathered into an index alist."
700 ;; These should really be done by setting imenu-create-index-function
701 ;; in these major modes. But save that change for later.
702 (cond ((and imenu-prev-index-position-function
703 imenu-extract-index-name-function
)
704 (let ((index-alist '())
706 (goto-char (point-max))
707 (imenu-progress-message prev-pos
0 t
)
708 ;; Search for the function
709 (while (funcall imenu-prev-index-position-function
)
710 (imenu-progress-message prev-pos nil t
)
712 (setq name
(funcall imenu-extract-index-name-function
)))
714 ;; [ydi] updated for imenu-use-markers
715 (push (cons name
(if imenu-use-markers
(point-marker) (point)))
717 (imenu-progress-message prev-pos
100 t
)
719 ;; Use generic expression if possible.
720 ((and imenu-generic-expression
)
721 (imenu--generic-function imenu-generic-expression
))
723 (error "This buffer cannot use `imenu-default-create-index-function'"))))
725 (defun imenu--replace-spaces (name replacement
)
726 ;; Replace all spaces in NAME with REPLACEMENT.
727 ;; That second argument should be a string.
731 (if (char-equal ch ?\
)
733 (char-to-string ch
))))
737 ;; Not used and would require cl at run time
738 ;;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
739 ;;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
740 ;;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
741 ;;; ;; name and a space concatenated to the names of the children.
742 ;;; ;; Third argument PREFIX is for internal use only.
745 ;;; (let* ((name (car item))
747 ;;; (new-prefix (and concat-names
749 ;;; (concat prefix imenu-level-separator name)
752 ;;; ((or (markerp pos) (numberp pos))
753 ;;; (list (cons new-prefix pos)))
755 ;;; (imenu--flatten-index-alist pos new-prefix)))))
759 ;;; Generic index gathering function.
762 (defvar imenu-case-fold-search t
763 "Defines whether `imenu--generic-function' should fold case when matching.
765 This buffer-local variable should be set (only) by initialization code
766 for modes which use `imenu--generic-function'. If it is not set, that
767 function will use the current value of `case-fold-search' to match
770 (make-variable-buffer-local 'imenu-case-fold-search
)
772 ;; Originally "Built on some ideas that Erik Naggum <erik@naggum.no>
773 ;; once posted to comp.emacs" but since substantially re-written.
774 (defun imenu--generic-function (patterns)
775 "Return an index of the current buffer as an alist.
777 PATTERNS is an alist with elements that look like this:
778 (MENU-TITLE REGEXP INDEX).
780 MENU-TITLE is a string used as the title for the submenu or nil if the
781 entries are not nested.
783 REGEXP is a regexp that should match a construct in the buffer that is
784 to be displayed in the menu; i.e., function or variable definitions,
785 etc. It contains a substring which is the name to appear in the
786 menu. See the info section on Regexps for more information.
788 INDEX points to the substring in REGEXP that contains the name (of the
789 function, variable or type) that is to appear in the menu.
791 See `lisp-imenu-generic-expression' for an example of PATTERNS.
793 Returns an index of the current buffer as an alist. The elements in
794 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
795 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
798 (let ((index-alist (list 'dummy
))
800 (case-fold-search imenu-case-fold-search
)
801 (old-table (syntax-table))
802 (table (copy-syntax-table (syntax-table)))
803 (slist imenu-syntax-alist
))
804 ;; Modify the syntax table used while matching regexps.
806 ;; The character(s) to modify may be a single char or a string.
807 (if (numberp (caar slist
))
808 (modify-syntax-entry (caar slist
) (cdar slist
) table
)
811 (modify-syntax-entry c
(cdar slist
) table
)))
813 (setq slist
(cdr slist
)))
814 (goto-char (point-max))
815 (imenu-progress-message prev-pos
0 t
)
816 (unwind-protect ; for syntax table
818 (set-syntax-table table
)
819 ;; map over the elements of imenu-generic-expression
820 ;; (typically functions, variables ...)
824 (let ((menu-title (car pat
))
827 (function (nth 3 pat
))
828 (rest (nthcdr 4 pat
)))
829 ;; Go backwards for convenience of adding items in order.
830 (goto-char (point-max))
831 (while (re-search-backward regexp nil t
)
832 (imenu-progress-message prev-pos nil t
)
833 (setq beg
(match-beginning index
))
834 ;; Add this sort of submenu only when we've found an
835 ;; item for it, avoiding empty, duff menus.
836 (unless (assoc menu-title index-alist
)
837 (push (list menu-title
) index-alist
))
838 (if imenu-use-markers
839 (setq beg
(set-marker (make-marker) beg
)))
842 (nconc (list (match-string-no-properties index
)
845 (cons (match-string-no-properties index
)
847 ;; This is the desired submenu,
848 ;; starting with its title (or nil).
849 (menu (assoc menu-title index-alist
)))
850 ;; Insert the item unless it is already present.
851 (unless (member item
(cdr menu
))
853 (cons item
(cdr menu
)))))))))
855 (set-syntax-table old-table
)))
856 (imenu-progress-message prev-pos
100 t
)
857 ;; Sort each submenu by position.
858 ;; This is in case one submenu gets items from two different regexps.
859 (let ((tail index-alist
))
861 (if (listp (car tail
))
863 (sort (cdr (car tail
)) 'imenu--sort-by-position
)))
864 (setq tail
(cdr tail
))))
865 (let ((main-element (assq nil index-alist
)))
866 (nconc (delq main-element
(delq 'dummy index-alist
))
867 (cdr main-element
)))))
869 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
871 ;;; The main functions for this package!
873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
875 (defun imenu--completion-buffer (index-alist &optional prompt
)
876 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
878 Returns t for rescan and otherwise a position number."
879 ;; Create a list for this buffer only when needed.
880 (let ((name (thing-at-point 'symbol
))
882 (prepared-index-alist
886 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
890 ((and name
(imenu--in-alist name prepared-index-alist
))
891 (setq prompt
(format "Index item (default %s): " name
)))
892 (t (setq prompt
"Index item: ")))
893 (if (eq imenu-always-use-completion-buffer-p
'never
)
894 (setq name
(completing-read prompt
896 nil t nil
'imenu--history-list name
))
897 (save-window-excursion
898 ;; Display the completion buffer
899 (with-output-to-temp-buffer "*Completions*"
900 (display-completion-list
901 (all-completions "" prepared-index-alist
)))
902 (let ((minibuffer-setup-hook
905 (let ((buffer (current-buffer)))
907 (set-buffer "*Completions*")
908 (setq completion-reference-buffer buffer
)))))))
909 ;; Make a completion question
910 (setq name
(completing-read prompt
912 nil t nil
'imenu--history-list name
)))))
913 (cond ((not (stringp name
))
915 ((string= name
(car imenu--rescan-item
))
918 (setq choice
(assoc name prepared-index-alist
))
919 (if (imenu--subalist-p choice
)
920 (imenu--completion-buffer (cdr choice
) prompt
)
923 (defun imenu--mouse-menu (index-alist event
&optional title
)
924 "Let the user select from a buffer index from a mouse menu.
926 INDEX-ALIST is the buffer index and EVENT is a mouse event.
928 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
929 (setq index-alist
(imenu--split-submenus index-alist
))
930 (let* ((menu (imenu--split-menu index-alist
931 (or title
(buffer-name))))
933 (setq menu
(imenu--create-keymap-1 (car menu
)
934 (if (< 1 (length (cdr menu
)))
936 (cdr (car (cdr menu
))))))
937 (setq position
(x-popup-menu event menu
))
938 (cond ((eq position nil
)
940 ;; If one call to x-popup-menu handled the nested menus,
941 ;; find the result by looking down the menus here.
942 ((and (listp position
)
943 (numberp (car position
))
944 (stringp (nth (1- (length position
)) position
)))
947 (setq final
(assq (car position
) final
))
948 (setq position
(cdr position
)))
949 (or (string= (car final
) (car imenu--rescan-item
))
951 ;; If x-popup-menu went just one level and found a leaf item,
952 ;; return the INDEX-ALIST element for that.
953 ((and (consp position
)
954 (stringp (car position
))
955 (null (cdr position
)))
956 (or (string= (car position
) (car imenu--rescan-item
))
957 (assq (car position
) index-alist
)))
958 ;; If x-popup-menu went just one level
959 ;; and found a non-leaf item (a submenu),
960 ;; recurse to handle the rest.
962 (imenu--mouse-menu position event
964 (concat title imenu-level-separator
965 (car (rassq position index-alist
)))
966 (car (rassq position index-alist
))))))))
968 (defun imenu-choose-buffer-index (&optional prompt alist
)
969 "Let the user select from a buffer index and return the chosen index.
971 If the user originally activated this function with the mouse, a mouse
972 menu is used. Otherwise a completion buffer is used and the user is
973 prompted with PROMPT.
975 If you call this function with index alist ALIST, then it lets the user
978 With no index alist ALIST, it calls `imenu--make-index-alist' to
979 create the index alist.
981 If `imenu-always-use-completion-buffer-p' is non-nil, then the
982 completion buffer is always used, no matter if the mouse was used or
985 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
987 (mouse-triggered (listp last-nonmenu-event
))
989 ;; If selected by mouse, see to that the window where the mouse is
990 ;; really is selected.
992 (not (equal last-nonmenu-event
'(menu-bar)))
993 (let ((window (posn-window (event-start last-nonmenu-event
))))
994 (or (framep window
) (null window
) (select-window window
))))
995 ;; Create a list for this buffer only when needed.
997 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
999 (if (and mouse-triggered
1000 (not imenu-always-use-completion-buffer-p
))
1001 (imenu--mouse-menu index-alist last-nonmenu-event
)
1002 (imenu--completion-buffer index-alist prompt
)))
1005 (setq imenu--index-alist nil
)))
1009 (defun imenu-add-to-menubar (name)
1010 "Add an `imenu' entry to the menu bar for the current buffer.
1011 NAME is a string used to name the menu bar item.
1012 See the command `imenu' for more information."
1013 (interactive "sImenu menu item name: ")
1014 (if (or (and imenu-prev-index-position-function
1015 imenu-extract-index-name-function
)
1016 imenu-generic-expression
1017 (not (eq imenu-create-index-function
1018 'imenu-default-create-index-function
)))
1019 (let ((newmap (make-sparse-keymap))
1020 (menu-bar (lookup-key (current-local-map) [menu-bar
])))
1021 (setq imenu--last-menubar-index-alist nil
)
1022 (define-key newmap
[menu-bar
]
1023 (append (make-sparse-keymap) menu-bar
))
1024 (define-key newmap
[menu-bar index
]
1025 (cons name
(nconc (make-sparse-keymap "Imenu")
1026 (make-sparse-keymap))))
1027 (use-local-map (append newmap
(current-local-map)))
1028 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
1029 (error "The mode `%s' does not support Imenu" mode-name
)))
1032 (defun imenu-add-menubar-index ()
1033 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
1035 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
1037 (imenu-add-to-menubar "Index"))
1039 (defvar imenu-buffer-menubar nil
)
1041 (defun imenu-update-menubar ()
1042 (and (current-local-map)
1043 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
1044 (let ((index-alist (imenu--make-index-alist t
)))
1045 ;; Don't bother updating if the index-alist has not changed
1046 ;; since the last time we did it.
1047 (or (equal index-alist imenu--last-menubar-index-alist
)
1048 (let (menu menu1 old
)
1049 (setq imenu--last-menubar-index-alist index-alist
)
1050 (setq index-alist
(imenu--split-submenus index-alist
))
1051 (setq menu
(imenu--split-menu index-alist
1053 (setq menu1
(imenu--create-keymap-1 (car menu
)
1054 (if (< 1 (length (cdr menu
)))
1056 (cdr (car (cdr menu
))))
1058 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
1059 (setcdr old
(cdr menu1
)))))))
1061 (defun imenu--menubar-select (item)
1062 "Use Imenu to select the function or variable named in this menu item."
1063 (if (equal item imenu--rescan-item
)
1066 (setq imenu--index-alist nil
)
1067 (imenu-update-menubar))
1070 (defun imenu-default-goto-function (name position
&optional rest
)
1071 "Move the point to the given position.
1073 NAME is ignored. POSITION is where to move. REST is also ignored.
1074 The ignored args just make this function have the same interface as a
1075 function placed in a special index-item."
1076 (if (or (< position
(point-min))
1077 (> position
(point-max)))
1078 ;; widen if outside narrowing
1080 (goto-char position
))
1083 (defun imenu (index-item)
1084 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1085 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1086 for more information."
1087 (interactive (list (imenu-choose-buffer-index)))
1088 ;; Convert a string to an alist element.
1089 (if (stringp index-item
)
1090 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1094 (let* ((is-special-item (listp (cdr index-item
)))
1097 (nth 2 index-item
) imenu-default-goto-function
))
1098 (position (if is-special-item
1099 (cadr index-item
) (cdr index-item
)))
1100 (rest (if is-special-item
(cddr index-item
))))
1101 (apply function
(car index-item
) position rest
))))
1102 (run-hooks 'imenu-after-jump-hook
))
1106 ;;; imenu.el ends here