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
))
120 (defcustom imenu-sort-function nil
121 "*The function to use for sorting the index mouse-menu.
123 Affects only the mouse index menu.
125 Set this to nil if you don't want any sorting (faster).
126 The items in the menu are then presented in the order they were found
129 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
131 The function should take two arguments and return t if the first
132 element should come before the second. The arguments are cons cells;
133 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
134 :type
'(choice (const :tag
"No sorting" nil
)
135 (const :tag
"Sort by name" 'imenu--sort-by-name
)
136 (function :tag
"Another function"))
139 (defcustom imenu-max-items
25
140 "*Maximum number of elements in a mouse menu for Imenu."
144 (defcustom imenu-scanning-message
"Scanning buffer for index (%3d%%)"
145 "*Progress message during the index scanning of the buffer.
146 If non-nil, user gets a message during the scanning of the buffer.
148 Relevant only if the mode-specific function that creates the buffer
149 index use `imenu-progress-message', and not useful if that is fast, in
150 which case you might as well set this to nil."
151 :type
'(choice string
152 (const :tag
"None" nil
))
155 (defcustom imenu-space-replacement
"."
156 "*The replacement string for spaces in index names.
157 Used when presenting the index in a completion buffer to make the
158 names work as tokens."
162 (defcustom imenu-level-separator
":"
163 "*The separator between index names of different levels.
164 Used for making mouse-menu titles and for flattening nested indexes
165 with name concatenation."
170 (defvar imenu-generic-expression nil
171 "The regex pattern to use for creating a buffer index.
173 If non-nil this pattern is passed to `imenu--generic-function'
174 to create a buffer index.
176 The value should be an alist with elements that look like this:
177 (MENU-TITLE REGEXP INDEX)
179 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
180 with zero or more ARGUMENTS. The former format creates a simple element in
181 the index alist when it matches; the latter creates a special element
182 of the form (NAME FUNCTION POSITION-MARKER ARGUMENTS...)
183 with FUNCTION and ARGUMENTS beiong copied from `imenu-generic-expression'.
185 MENU-TITLE is a string used as the title for the submenu or nil if the
186 entries are not nested.
188 REGEXP is a regexp that should match a construct in the buffer that is
189 to be displayed in the menu; i.e., function or variable definitions,
190 etc. It contains a substring which is the name to appear in the
191 menu. See the info section on Regexps for more information.
193 INDEX points to the substring in REGEXP that contains the name (of the
194 function, variable or type) that is to appear in the menu.
196 The variable is buffer-local.
198 The variable `imenu-case-fold-search' determines whether or not the
199 regexp matches are case sensitive. and `imenu-syntax-alist' can be
200 used to alter the syntax table for the search.
202 For example, see the value of `lisp-imenu-generic-expression' used by
203 `lisp-mode' and `emacs-lisp-mode' with `imenu-syntax-alist' set
204 locally to give the characters which normally have \"punctuation\"
205 syntax \"word\" syntax during matching.")
208 (make-variable-buffer-local 'imenu-generic-expression
)
213 (defvar imenu-create-index-function
'imenu-default-create-index-function
214 "The function to use for creating a buffer index.
216 It should be a function that takes no arguments and returns an index
217 of the current buffer as an alist.
219 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
220 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
221 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
222 The function `imenu--subalist-p' tests an element and returns t
223 if it is a sub-alist.
225 This function is called within a `save-excursion'.
227 The variable is buffer-local.")
229 (make-variable-buffer-local 'imenu-create-index-function
)
232 (defvar imenu-prev-index-position-function
'beginning-of-defun
233 "Function for finding the next index position.
235 If `imenu-create-index-function' is set to
236 `imenu-default-create-index-function', then you must set this variable
237 to a function that will find the next index, looking backwards in the
240 The function should leave point at the place to be connected to the
241 index and it should return nil when it doesn't find another index.
243 This variable is local in all buffers.")
245 (make-variable-buffer-local 'imenu-prev-index-position-function
)
248 (defvar imenu-extract-index-name-function nil
249 "Function for extracting the index item name, given a position.
251 This function is called after `imenu-prev-index-position-function'
252 finds a position for an index item, with point at that position.
253 It should return the name for that index item.
255 This variable is local in all buffers.")
257 (make-variable-buffer-local 'imenu-extract-index-name-function
)
260 (defvar imenu-default-goto-function
'imenu-default-goto-function
261 "The default function called when selecting an Imenu item.
262 The function in this variable is called when selecting a normal index-item.")
264 (make-variable-buffer-local 'imenu-default-goto-function
)
267 (defun imenu--subalist-p (item)
268 (and (consp (cdr item
)) (listp (cadr item
))
269 (not (eq (car (cadr item
)) 'lambda
))))
271 ;; Macro to display a progress message.
272 ;; RELPOS is the relative position to display.
273 ;; If RELPOS is nil, then the relative position in the buffer
275 ;; PREVPOS is the variable in which we store the last position displayed.
276 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
278 imenu-scanning-message
279 (let ((pos ,(if relpos
281 `(imenu--relative-position ,reverse
))))
283 `(> pos
(+ 5 ,prevpos
)))
285 (message imenu-scanning-message pos
)
286 (setq ,prevpos pos
))))))
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;;;; Some examples of functions utilizing the framework of this
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 ;; Return the current/previous sexp and the location of the sexp (its
297 ;; beginning) without moving the point.
298 (defun imenu-example--name-and-position ()
301 ;; [ydi] modified for imenu-use-markers
302 (let ((beg (if imenu-use-markers
(point-marker) (point)))
303 (end (progn (forward-sexp) (point))))
304 (cons (buffer-substring beg end
)
311 (defun imenu-example--lisp-extract-index-name ()
312 ;; Example of a candidate for `imenu-extract-index-name-function'.
313 ;; This will generate a flat index of definitions in a lisp file.
315 (and (looking-at "(def")
321 (end (progn (forward-sexp -
1) (point))))
322 (buffer-substring beg end
)))
325 (defun imenu-example--create-lisp-index ()
326 ;; Example of a candidate for `imenu-create-index-function'.
327 ;; It will generate a nested index of definitions.
328 (let ((index-alist '())
329 (index-var-alist '())
330 (index-type-alist '())
331 (index-unknown-alist '())
333 (goto-char (point-max))
334 (imenu-progress-message prev-pos
0)
335 ;; Search for the function
336 (while (beginning-of-defun)
337 (imenu-progress-message prev-pos nil t
)
339 (and (looking-at "(def")
343 ((looking-at "def\\(var\\|const\\)")
345 (push (imenu-example--name-and-position)
347 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
349 (push (imenu-example--name-and-position)
351 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
353 (if (= (char-after (1- (point))) ?\
))
358 (push (imenu-example--name-and-position)
362 (push (imenu-example--name-and-position)
363 index-unknown-alist
)))))))
364 (imenu-progress-message prev-pos
100)
366 (push (cons "Variables" index-var-alist
)
368 (and index-type-alist
369 (push (cons "Types" index-type-alist
)
371 (and index-unknown-alist
372 (push (cons "Syntax-unknown" index-unknown-alist
)
376 ;; Regular expression to find C functions
377 (defvar imenu-example--function-name-regexp-c
379 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
380 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
381 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
382 "\\([*&]+[ \t]*\\)?" ; pointer
383 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
386 (defun imenu-example--create-c-index (&optional regexp
)
387 (let ((index-alist '())
389 (goto-char (point-min))
390 (imenu-progress-message prev-pos
0)
391 ;; Search for the function
393 (while (re-search-forward
394 (or regexp imenu-example--function-name-regexp-c
)
396 (imenu-progress-message prev-pos
)
399 (goto-char (scan-sexps (point) 1))
400 (setq char
(following-char)))
401 ;; Skip this function name if it is a prototype declaration.
402 (if (not (eq char ?\
;))
403 (push (imenu-example--name-and-position) index-alist
))))
404 (imenu-progress-message prev-pos
100)
405 (nreverse index-alist
)))
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 ;;; Internal variables
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ;; The item to use in the index for rescanning the buffer.
415 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
417 ;; The latest buffer index.
419 (defvar imenu--index-alist nil
420 "The buffer index computed for this buffer in Imenu.
421 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
422 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
423 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
425 This variable is local in all buffers, once set.")
427 (make-variable-buffer-local 'imenu--index-alist
)
429 ;; The latest buffer index used to update the menu bar menu.
430 (defvar imenu--last-menubar-index-alist nil
)
431 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
433 ;; History list for 'jump-to-function-in-buffer'.
434 ;; Making this buffer local caused it not to work!
435 (defvar imenu--history-list nil
)
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
439 ;;; Internal support functions
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;;; Sorts the items depending on their index name.
446 ;;; An item looks like (NAME . POSITION).
448 (defun imenu--sort-by-name (item1 item2
)
449 (string-lessp (car item1
) (car item2
)))
451 (defun imenu--sort-by-position (item1 item2
)
452 (< (cdr item1
) (cdr item2
)))
454 (defun imenu--relative-position (&optional reverse
)
455 ;; Support function to calculate relative position in buffer
456 ;; Beginning of buffer is 0 and end of buffer is 100
457 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
459 (total (buffer-size)))
460 (and reverse
(setq pos
(- total pos
)))
462 ;; Avoid overflow from multiplying by 100!
463 (/ (1- pos
) (max (/ total
100) 1))
464 (/ (* 100 (1- pos
)) (max total
1)))))
466 ;; Split LIST into sublists of max length N.
467 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
468 (defun imenu--split (list n
)
474 (push (pop remain
) sublist
)
477 ;; We have finished a sublist
478 (progn (push (nreverse sublist
) result
)
480 (setq sublist
'()))))
481 ;; There might be a sublist (if the length of LIST mod n is != 0)
482 ;; that has to be added to the result list.
484 (push (nreverse sublist
) result
))
487 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
488 ;;; In any case, add TITLE to the front of the alist.
489 (defun imenu--split-menu (menulist title
)
490 (let (keep-at-top tail
)
491 (if (memq imenu--rescan-item menulist
)
492 (setq keep-at-top
(cons imenu--rescan-item nil
)
493 menulist
(delq imenu--rescan-item menulist
)))
496 (if (imenu--subalist-p (car tail
))
497 (setq keep-at-top
(cons (car tail
) keep-at-top
)
498 menulist
(delq (car tail
) menulist
)))
499 (setq tail
(cdr tail
)))
500 (if imenu-sort-function
505 ;; Copy list method from the cl package `copy-list'
506 (while (consp oldlist
) (push (pop oldlist
) res
))
507 (if res
; in case, e.g. no functions defined
508 (prog1 (nreverse res
) (setcdr res oldlist
))))
509 imenu-sort-function
)))
510 (if (> (length menulist
) imenu-max-items
)
516 (cons (format "From: %s" (caar menu
)) menu
)))
517 (imenu--split menulist imenu-max-items
)))))
519 (nconc (nreverse keep-at-top
) menulist
))))
521 ;;; Split up each long alist that are nested within ALIST
522 ;;; into nested alists.
523 (defun imenu--split-submenus (alist)
529 (imenu--split-menu (cdr elt
) (car elt
))
533 ;;; Truncate all strings in MENULIST to imenu-max-item-length
534 (defun imenu--truncate-items (menulist)
539 (imenu--truncate-items (cdr item
)))
541 ;; truncate if necessary
542 (if (and (numberp imenu-max-item-length
)
543 (> (length (car item
)) imenu-max-item-length
))
544 (setcar item
(substring (car item
) 0 imenu-max-item-length
)))))))
548 (defun imenu--make-index-alist (&optional noerror
)
549 "Create an index-alist for the definitions in the current buffer.
551 Report an error if the list is empty unless NOERROR is supplied and
554 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
555 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
556 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
557 The function `imenu--subalist-p' tests an element and returns t
558 if it is a sub-alist.
560 There is one simple element with negative POSITION; that's intended
561 as a way for the user to ask to recalculate the buffer's index alist."
562 (or (and imenu--index-alist
563 (or (not imenu-auto-rescan
)
564 (and imenu-auto-rescan
565 (> (buffer-size) imenu-auto-rescan-maxout
))))
566 ;; Get the index; truncate if necessary
568 (setq imenu--index-alist
572 (funcall imenu-create-index-function
))))
573 (imenu--truncate-items imenu--index-alist
)))
574 (or imenu--index-alist noerror
575 (error "No items suitable for an index found in this buffer"))
576 (or imenu--index-alist
577 (setq imenu--index-alist
(list nil
)))
578 ;; Add a rescan option to the index.
579 (cons imenu--rescan-item imenu--index-alist
))
581 ;;; Find all markers in alist and makes
582 ;;; them point nowhere.
583 ;;; The top-level call uses nil as the argument;
584 ;;; non-nil arguments are in recursivecalls.
585 (defvar imenu--cleanup-seen
)
587 (defun imenu--cleanup (&optional alist
)
588 ;; If alist is provided use that list.
589 ;; If not, empty the table of lists already seen
590 ;; and use imenu--index-alist.
592 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
593 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
600 ((markerp (cdr item
))
601 (set-marker (cdr item
) nil
))
602 ;; Don't process one alist twice.
603 ((memq (cdr item
) imenu--cleanup-seen
))
604 ((imenu--subalist-p item
)
605 (imenu--cleanup (cdr item
))))))
609 (defun imenu--create-keymap-2 (alist counter
&optional commands
)
615 ((imenu--subalist-p item
)
616 (append (list (setq counter
(1+ counter
))
617 (car item
) 'keymap
(car item
))
618 (imenu--create-keymap-2 (cdr item
) (+ counter
10) commands
)))
620 (let ((end (if commands
`(lambda () (interactive)
621 (imenu--menubar-select ',item
))
622 (cons '(nil) item
))))
624 (cons (car item
) end
)))))))
627 ;; If COMMANDS is non-nil, make a real keymap
628 ;; with a real command used as the definition.
629 ;; If it is nil, make something suitable for x-popup-menu.
630 (defun imenu--create-keymap-1 (title alist
&optional commands
)
631 (append (list 'keymap title
) (imenu--create-keymap-2 alist
0 commands
)))
633 (defun imenu--in-alist (str alist
)
634 "Check whether the string STR is contained in multi-level ALIST."
635 (let (elt head tail res
)
638 (setq elt
(car alist
)
642 ;; A nested ALIST element looks like
643 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
644 ;; while a bottom-level element looks like
645 ;; (INDEX-NAME . INDEX-POSITION)
646 ;; We are only interested in the bottom-level elements, so we need to
647 ;; recurse if TAIL is a list.
649 (if (setq res
(imenu--in-alist str tail
))
652 (setq alist nil res elt
))))
655 (defvar imenu-syntax-alist nil
656 "Alist of syntax table modifiers to use while executing `imenu--generic-function'.
658 The car of the assocs may be either a character or a string and the
659 cdr is a syntax description appropriate fo `modify-syntax-entry'. For
660 a string, all the characters in the string get the specified syntax.
662 This is typically used to give word syntax to characters which
663 normally have symbol syntax to simplify `imenu-expression'
664 and speed-up matching.")
665 (make-variable-buffer-local 'imenu-syntax-alist
)
667 (defun imenu-default-create-index-function ()
668 "*Wrapper for index searching functions.
670 Moves point to end of buffer and then repeatedly calls
671 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
672 Their results are gathered into an index alist."
673 ;; These should really be done by setting imenu-create-index-function
674 ;; in these major modes. But save that change for later.
675 (cond ((and imenu-prev-index-position-function
676 imenu-extract-index-name-function
)
677 (let ((index-alist '())
679 (goto-char (point-max))
680 (imenu-progress-message prev-pos
0 t
)
681 ;; Search for the function
682 (while (funcall imenu-prev-index-position-function
)
683 (imenu-progress-message prev-pos nil t
)
685 (setq name
(funcall imenu-extract-index-name-function
)))
687 ;; [ydi] updated for imenu-use-markers
688 (push (cons name
(if imenu-use-markers
(point-marker) (point)))
690 (imenu-progress-message prev-pos
100 t
)
692 ;; Use generic expression if possible.
693 ((and imenu-generic-expression
)
694 (imenu--generic-function imenu-generic-expression
))
696 (error "This buffer cannot use `imenu-default-create-index-function'"))))
698 (defun imenu--replace-spaces (name replacement
)
699 ;; Replace all spaces in NAME with REPLACEMENT.
700 ;; That second argument should be a string.
704 (if (char-equal ch ?\
)
706 (char-to-string ch
))))
710 ;; Not used and would require cl at run time
711 ;;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
712 ;;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
713 ;;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
714 ;;; ;; name and a space concatenated to the names of the children.
715 ;;; ;; Third argument PREFIX is for internal use only.
718 ;;; (let* ((name (car item))
720 ;;; (new-prefix (and concat-names
722 ;;; (concat prefix imenu-level-separator name)
725 ;;; ((or (markerp pos) (numberp pos))
726 ;;; (list (cons new-prefix pos)))
728 ;;; (imenu--flatten-index-alist pos new-prefix)))))
732 ;;; Generic index gathering function.
735 (defvar imenu-case-fold-search t
736 "Defines whether `imenu--generic-function' should fold case when matching.
738 This buffer-local variable should be set (only) by initialization code
739 for modes which use `imenu--generic-function'. If it is not set, that
740 function will use the current value of `case-fold-search' to match
743 (make-variable-buffer-local 'imenu-case-fold-search
)
745 ;; Originally "Built on some ideas that Erik Naggum <erik@naggum.no>
746 ;; once posted to comp.emacs" but since substantially re-written.
747 (defun imenu--generic-function (patterns)
748 "Return an index of the current buffer as an alist.
750 PATTERNS is an alist with elements that look like this:
751 (MENU-TITLE REGEXP INDEX).
753 MENU-TITLE is a string used as the title for the submenu or nil if the
754 entries are not nested.
756 REGEXP is a regexp that should match a construct in the buffer that is
757 to be displayed in the menu; i.e., function or variable definitions,
758 etc. It contains a substring which is the name to appear in the
759 menu. See the info section on Regexps for more information.
761 INDEX points to the substring in REGEXP that contains the name (of the
762 function, variable or type) that is to appear in the menu.
764 See `lisp-imenu-generic-expression' for an example of PATTERNS.
766 Returns an index of the current buffer as an alist. The elements in
767 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
768 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
771 (let ((index-alist (list 'dummy
))
773 (case-fold-search imenu-case-fold-search
)
774 (old-table (syntax-table))
775 (table (copy-syntax-table (syntax-table)))
776 (slist imenu-syntax-alist
))
777 ;; Modify the syntax table used while matching regexps.
779 ;; The character(s) to modify may be a single char or a string.
780 (if (numberp (caar slist
))
781 (modify-syntax-entry (caar slist
) (cdar slist
) table
)
784 (modify-syntax-entry c
(cdar slist
) table
)))
786 (setq slist
(cdr slist
)))
787 (goto-char (point-max))
788 (imenu-progress-message prev-pos
0 t
)
789 (unwind-protect ; for syntax table
791 (set-syntax-table table
)
792 ;; map over the elements of imenu-generic-expression
793 ;; (typically functions, variables ...)
797 (let ((menu-title (car pat
))
800 (function (nth 3 pat
))
801 (rest (nthcdr 4 pat
)))
802 ;; Go backwards for convenience of adding items in order.
803 (goto-char (point-max))
804 (while (re-search-backward regexp nil t
)
805 (imenu-progress-message prev-pos nil t
)
806 (setq beg
(match-beginning index
))
807 ;; Add this sort of submenu only when we've found an
808 ;; item for it, avoiding empty, duff menus.
809 (unless (assoc menu-title index-alist
)
810 (push (list menu-title
) index-alist
))
811 (if imenu-use-markers
812 (setq beg
(set-marker (make-marker) beg
)))
815 (nconc (list (match-string-no-properties index
)
818 (cons (match-string-no-properties index
)
820 ;; This is the desired submenu,
821 ;; starting with its title (or nil).
822 (menu (assoc menu-title index-alist
)))
823 ;; Insert the item unless it is already present.
824 (unless (member item
(cdr menu
))
826 (cons item
(cdr menu
)))))))))
828 (set-syntax-table old-table
)))
829 (imenu-progress-message prev-pos
100 t
)
830 ;; Sort each submenu by position.
831 ;; This is in case one submenu gets items from two different regexps.
832 (let ((tail index-alist
))
834 (if (listp (car tail
))
836 (sort (cdr (car tail
)) 'imenu--sort-by-position
)))
837 (setq tail
(cdr tail
))))
838 (let ((main-element (assq nil index-alist
)))
839 (nconc (delq main-element
(delq 'dummy index-alist
))
840 (cdr main-element
)))))
842 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
844 ;;; The main functions for this package!
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
848 (defun imenu--completion-buffer (index-alist &optional prompt
)
849 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
851 Returns t for rescan and otherwise a position number."
852 ;; Create a list for this buffer only when needed.
853 (let ((name (thing-at-point 'symbol
))
855 (prepared-index-alist
859 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
863 ((and name
(imenu--in-alist name prepared-index-alist
))
864 (setq prompt
(format "Index item (default %s): " name
)))
865 (t (setq prompt
"Index item: ")))
866 (if (eq imenu-always-use-completion-buffer-p
'never
)
867 (setq name
(completing-read prompt
869 nil t nil
'imenu--history-list name
))
870 (save-window-excursion
871 ;; Display the completion buffer
872 (with-output-to-temp-buffer "*Completions*"
873 (display-completion-list
874 (all-completions "" prepared-index-alist
)))
875 (let ((minibuffer-setup-hook
878 (let ((buffer (current-buffer)))
880 (set-buffer "*Completions*")
881 (setq completion-reference-buffer buffer
)))))))
882 ;; Make a completion question
883 (setq name
(completing-read prompt
885 nil t nil
'imenu--history-list name
)))))
886 (cond ((not (stringp name
))
888 ((string= name
(car imenu--rescan-item
))
891 (setq choice
(assoc name prepared-index-alist
))
892 (if (imenu--subalist-p choice
)
893 (imenu--completion-buffer (cdr choice
) prompt
)
896 (defun imenu--mouse-menu (index-alist event
&optional title
)
897 "Let the user select from a buffer index from a mouse menu.
899 INDEX-ALIST is the buffer index and EVENT is a mouse event.
901 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
902 (setq index-alist
(imenu--split-submenus index-alist
))
903 (let* ((menu (imenu--split-menu index-alist
904 (or title
(buffer-name))))
906 (setq menu
(imenu--create-keymap-1 (car menu
)
907 (if (< 1 (length (cdr menu
)))
909 (cdr (car (cdr menu
))))))
910 (setq position
(x-popup-menu event menu
))
911 (cond ((eq position nil
)
913 ;; If one call to x-popup-menu handled the nested menus,
914 ;; find the result by looking down the menus here.
915 ((and (listp position
)
916 (numberp (car position
))
917 (stringp (nth (1- (length position
)) position
)))
920 (setq final
(assoc (car position
) final
))
921 (setq position
(cdr position
)))
922 (or (string= (car final
) (car imenu--rescan-item
))
924 ;; If x-popup-menu went just one level and found a leaf item,
925 ;; return the INDEX-ALIST element for that.
926 ((and (consp position
)
927 (stringp (car position
))
928 (null (cdr position
)))
929 (or (string= (car position
) (car imenu--rescan-item
))
930 (assq (car position
) index-alist
)))
931 ;; If x-popup-menu went just one level
932 ;; and found a non-leaf item (a submenu),
933 ;; recurse to handle the rest.
935 (imenu--mouse-menu position event
937 (concat title imenu-level-separator
938 (car (rassq position index-alist
)))
939 (car (rassq position index-alist
))))))))
941 (defun imenu-choose-buffer-index (&optional prompt alist
)
942 "Let the user select from a buffer index and return the chosen index.
944 If the user originally activated this function with the mouse, a mouse
945 menu is used. Otherwise a completion buffer is used and the user is
946 prompted with PROMPT.
948 If you call this function with index alist ALIST, then it lets the user
951 With no index alist ALIST, it calls `imenu--make-index-alist' to
952 create the index alist.
954 If `imenu-always-use-completion-buffer-p' is non-nil, then the
955 completion buffer is always used, no matter if the mouse was used or
958 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
960 (mouse-triggered (listp last-nonmenu-event
))
962 ;; If selected by mouse, see to that the window where the mouse is
963 ;; really is selected.
965 (not (equal last-nonmenu-event
'(menu-bar)))
966 (let ((window (posn-window (event-start last-nonmenu-event
))))
967 (or (framep window
) (null window
) (select-window window
))))
968 ;; Create a list for this buffer only when needed.
970 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
972 (if (and mouse-triggered
973 (not imenu-always-use-completion-buffer-p
))
974 (imenu--mouse-menu index-alist last-nonmenu-event
)
975 (imenu--completion-buffer index-alist prompt
)))
978 (setq imenu--index-alist nil
)))
982 (defun imenu-add-to-menubar (name)
983 "Add an `imenu' entry to the menu bar for the current buffer.
984 NAME is a string used to name the menu bar item.
985 See the command `imenu' for more information."
986 (interactive "sImenu menu item name: ")
987 (if (or (and imenu-prev-index-position-function
988 imenu-extract-index-name-function
)
989 imenu-generic-expression
990 (not (eq imenu-create-index-function
991 'imenu-default-create-index-function
)))
992 (let ((newmap (make-sparse-keymap))
993 (menu-bar (lookup-key (current-local-map) [menu-bar
])))
994 (define-key newmap
[menu-bar
]
995 (append (make-sparse-keymap) menu-bar
))
996 (define-key newmap
[menu-bar index
]
997 (cons name
(nconc (make-sparse-keymap "Imenu")
998 (make-sparse-keymap))))
999 (use-local-map (append newmap
(current-local-map)))
1000 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
1001 (error "The mode `%s' does not support Imenu" mode-name
)))
1004 (defun imenu-add-menubar-index ()
1005 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
1007 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
1009 (imenu-add-to-menubar "Index"))
1011 (defvar imenu-buffer-menubar nil
)
1013 (defun imenu-update-menubar ()
1014 (and (current-local-map)
1015 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
1016 (let ((index-alist (imenu--make-index-alist t
)))
1017 ;; Don't bother updating if the index-alist has not changed
1018 ;; since the last time we did it.
1019 (or (equal index-alist imenu--last-menubar-index-alist
)
1020 (let (menu menu1 old
)
1021 (setq imenu--last-menubar-index-alist index-alist
)
1022 (setq index-alist
(imenu--split-submenus index-alist
))
1023 (setq menu
(imenu--split-menu index-alist
1025 (setq menu1
(imenu--create-keymap-1 (car menu
)
1026 (if (< 1 (length (cdr menu
)))
1028 (cdr (car (cdr menu
))))
1030 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
1031 (setcdr old
(cdr menu1
)))))))
1033 (defun imenu--menubar-select (item)
1034 "Use Imenu to select the function or variable named in this menu item."
1035 (if (equal item imenu--rescan-item
)
1038 (setq imenu--index-alist nil
)
1039 (imenu-update-menubar))
1042 (defun imenu-default-goto-function (name position
&optional rest
)
1043 "Move the point to the given position.
1045 NAME is ignored. POSITION is where to move. REST is also ignored.
1046 The ignored args just make this function have the same interface as a
1047 function placed in a special index-item."
1048 (if (or (< position
(point-min))
1049 (> position
(point-max)))
1050 ;; widen if outside narrowing
1052 (goto-char position
))
1055 (defun imenu (index-item)
1056 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1057 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1058 for more information."
1059 (interactive (list (imenu-choose-buffer-index)))
1060 ;; Convert a string to an alist element.
1061 (if (stringp index-item
)
1062 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1066 (let* ((is-special-item (listp (cdr index-item
)))
1069 (nth 2 index-item
) imenu-default-goto-function
))
1070 (position (if is-special-item
1071 (cadr index-item
) (cdr index-item
)))
1072 (rest (if is-special-item
(cddr index-item
))))
1073 (apply function
(car index-item
) position rest
)))))
1077 ;;; imenu.el ends here