1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
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.
37 ;; A mode-specific function is called to generate the index. It is
38 ;; then presented to the user, who can choose from this index.
40 ;; The package comes with a set of example functions for how to
41 ;; utilize this package.
43 ;; There are *examples* for index gathering functions/regular
44 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
45 ;; customize for other modes. A function for jumping to the chosen
46 ;; index position is also supplied.
49 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
50 ;; [dean] - Dean Andrews ada@unison.com
51 ;; [alon] - Alon Albert al@mercury.co.il
52 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
53 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
54 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
55 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
56 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
57 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
61 (eval-when-compile (require 'cl
))
63 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 ;;; Customizable variables
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 "Mode-specific buffer indexes."
74 (defcustom imenu-use-markers t
75 "*Non-nil means use markers instead of integers for Imenu buffer positions.
76 Setting this to nil makes Imenu work faster.
78 This might not yet be honored by all index-building functions."
83 (defcustom imenu-max-item-length
60
84 "*If a number, truncate Imenu entries to that length."
88 (defcustom imenu-auto-rescan nil
89 "*Non-nil means Imenu should always rescan the buffers."
93 (defcustom imenu-auto-rescan-maxout
60000
94 "*Imenu auto-rescan is disabled in buffers larger than this size.
95 This variable is buffer-local."
99 (defcustom imenu-always-use-completion-buffer-p nil
100 "*Set this to non-nil for displaying the index in a completion buffer.
102 `never' means never automatically display a listing of any kind.
103 A value of nil (the default) means display the index as a mouse menu
104 if the mouse was used to invoke `imenu'.
105 Another non-nil value means always display the index in a completion buffer."
106 :type
'(choice (const :tag
"On Mouse" nil
)
107 (const :tag
"Never" never
)
108 (sexp :tag
"Always" :format
"%t\n" t
))
111 (defcustom imenu-sort-function nil
112 "*The function to use for sorting the index mouse-menu.
114 Affects only the mouse index menu.
116 Set this to nil if you don't want any sorting (faster).
117 The items in the menu are then presented in the order they were found
120 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
122 The function should take two arguments and return T if the first
123 element should come before the second. The arguments are cons cells;
124 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
128 (defcustom imenu-max-items
25
129 "*Maximum number of elements in a mouse menu for Imenu."
133 (defcustom imenu-scanning-message
"Scanning buffer for index (%3d%%)"
134 "*Progress message during the index scanning of the buffer.
135 If non-nil, user gets a message during the scanning of the buffer.
137 Relevant only if the mode-specific function that creates the buffer
138 index use `imenu-progress-message'."
142 (defcustom imenu-space-replacement
"^"
143 "*The replacement string for spaces in index names.
144 Used when presenting the index in a completion-buffer to make the
145 names work as tokens."
149 (defcustom imenu-level-separator
":"
150 "*The separator between index names of different levels.
151 Used for making mouse-menu titles and for flattening nested indexes
152 with name concatenation."
157 (defvar imenu-generic-expression nil
158 "The regex pattern to use for creating a buffer index.
160 If non-nil this pattern is passed to `imenu--generic-function'
161 to create a buffer index.
163 The value should be an alist with elements that look like this:
164 (MENU-TITLE REGEXP INDEX)
166 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
167 with zero or more ARGUMENTS. The former format creates a simple element in
168 the index alist when it matches; the latter creates a special element
169 of the form (NAME FUNCTION NAME POSITION-MARKER ARGUMENTS...)
170 with FUNCTION and ARGUMENTS beiong copied from `imenu-generic-expression'.
172 MENU-TITLE is a string used as the title for the submenu or nil if the
173 entries are not nested.
175 REGEXP is a regexp that should match a construct in the buffer that is
176 to be displayed in the menu; i.e., function or variable definitions,
177 etc. It contains a substring which is the name to appear in the
178 menu. See the info section on Regexps for more information.
180 INDEX points to the substring in REGEXP that contains the name (of the
181 function, variable or type) that is to appear in the menu.
183 For emacs-lisp-mode for example PATTERN would look like:
185 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
186 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
187 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2))
189 The variable is buffer-local.
191 The variable `imenu-case-fold-search' determines whether or not the
192 regexp matches are case sensitive.")
195 (make-variable-buffer-local 'imenu-generic-expression
)
199 (defvar imenu-create-index-function
'imenu-default-create-index-function
200 "The function to use for creating a buffer index.
202 It should be a function that takes no arguments and returns an index
203 of the current buffer as an alist.
205 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
206 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
207 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
208 The function `imenu--subalist-p' tests an element and returns t
209 if it is a sub-alist.
211 This function is called within a `save-excursion'.
213 The variable is buffer-local.")
214 (make-variable-buffer-local 'imenu-create-index-function
)
216 (defvar imenu-prev-index-position-function
'beginning-of-defun
217 "Function for finding the next index position.
219 If `imenu-create-index-function' is set to
220 `imenu-default-create-index-function', then you must set this variable
221 to a function that will find the next index, looking backwards in the
224 The function should leave point at the place to be connected to the
225 index and it should return nil when it doesn't find another index.")
226 (make-variable-buffer-local 'imenu-prev-index-position-function
)
228 (defvar imenu-extract-index-name-function nil
229 "Function for extracting the index name.
231 This function is called after the function pointed out by
232 `imenu-prev-index-position-function'.")
233 (make-variable-buffer-local 'imenu-extract-index-name-function
)
235 (defvar imenu-default-goto-function
'imenu-default-goto-function
236 "The default function called when selecting an Imenu item.
237 The function in this variable is called when selecting a normal index-item.")
238 (make-variable-buffer-local 'imenu-default-goto-function
)
241 (defun imenu--subalist-p (item)
242 (and (consp (cdr item
)) (listp (cadr item
))
243 (not (eq (caadr item
) 'lambda
))))
246 ;;; Macro to display a progress message.
247 ;;; RELPOS is the relative position to display.
248 ;;; If RELPOS is nil, then the relative position in the buffer
250 ;;; PREVPOS is the variable in which we store the last position displayed.
251 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
253 imenu-scanning-message
254 (let ((pos (, (if relpos
256 (` (imenu--relative-position (, reverse
)))))))
258 (` (> pos
(+ 5 (, prevpos
))))))
260 (message imenu-scanning-message pos
)
261 (setq (, prevpos
) pos
)))))))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;;;; Some examples of functions utilizing the framework of this
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 ;; Return the current/previous sexp and the location of the sexp (its
272 ;; beginning) without moving the point.
273 (defun imenu-example--name-and-position ()
276 ;; [ydi] modified for imenu-use-markers
277 (let ((beg (if imenu-use-markers
(point-marker) (point)))
278 (end (progn (forward-sexp) (point))))
279 (cons (buffer-substring beg end
)
286 (defun imenu-example--lisp-extract-index-name ()
287 ;; Example of a candidate for `imenu-extract-index-name-function'.
288 ;; This will generate a flat index of definitions in a lisp file.
290 (and (looking-at "(def")
296 (end (progn (forward-sexp -
1) (point))))
297 (buffer-substring beg end
)))
300 (defun imenu-example--create-lisp-index ()
301 ;; Example of a candidate for `imenu-create-index-function'.
302 ;; It will generate a nested index of definitions.
303 (let ((index-alist '())
304 (index-var-alist '())
305 (index-type-alist '())
306 (index-unknown-alist '())
308 (goto-char (point-max))
309 (imenu-progress-message prev-pos
0)
310 ;; Search for the function
311 (while (beginning-of-defun)
312 (imenu-progress-message prev-pos nil t
)
314 (and (looking-at "(def")
318 ((looking-at "def\\(var\\|const\\)")
320 (push (imenu-example--name-and-position)
322 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
324 (push (imenu-example--name-and-position)
326 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
328 (if (= (char-after (1- (point))) ?\
))
333 (push (imenu-example--name-and-position)
337 (push (imenu-example--name-and-position)
338 index-unknown-alist
)))))))
339 (imenu-progress-message prev-pos
100)
341 (push (cons "Variables" index-var-alist
)
343 (and index-type-alist
344 (push (cons "Types" index-type-alist
)
346 (and index-unknown-alist
347 (push (cons "Syntax-unknown" index-unknown-alist
)
351 ;; Regular expression to find C functions
352 (defvar imenu-example--function-name-regexp-c
354 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
355 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
356 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
357 "\\([*&]+[ \t]*\\)?" ; pointer
358 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
361 (defun imenu-example--create-c-index (&optional regexp
)
362 (let ((index-alist '())
364 (goto-char (point-min))
365 (imenu-progress-message prev-pos
0)
366 ;; Search for the function
368 (while (re-search-forward
369 (or regexp imenu-example--function-name-regexp-c
)
371 (imenu-progress-message prev-pos
)
374 (goto-char (scan-sexps (point) 1))
375 (setq char
(following-char)))
376 ;; Skip this function name if it is a prototype declaration.
377 (if (not (eq char ?\
;))
378 (push (imenu-example--name-and-position) index-alist
))))
379 (imenu-progress-message prev-pos
100)
380 (nreverse index-alist
)))
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 ;;; Internal variables
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; The item to use in the index for rescanning the buffer.
390 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
392 ;; The latest buffer index.
394 (defvar imenu--index-alist nil
)
395 (make-variable-buffer-local 'imenu--index-alist
)
397 ;; The latest buffer index used to update the menu bar menu.
398 (defvar imenu--last-menubar-index-alist nil
)
399 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
401 ;; History list for 'jump-to-function-in-buffer'.
402 ;; Making this buffer local caused it not to work!
403 (defvar imenu--history-list nil
)
405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
407 ;;; Internal support functions
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;;; Sorts the items depending on their index name.
414 ;;; An item look like (NAME . POSITION).
416 (defun imenu--sort-by-name (item1 item2
)
417 (string-lessp (car item1
) (car item2
)))
419 (defun imenu--relative-position (&optional reverse
)
420 ;; Support function to calculate relative position in buffer
421 ;; Beginning of buffer is 0 and end of buffer is 100
422 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
424 (total (buffer-size)))
425 (and reverse
(setq pos
(- total pos
)))
427 ;; Avoid overflow from multiplying by 100!
428 (/ (1- pos
) (max (/ total
100) 1))
429 (/ (* 100 (1- pos
)) (max total
1)))))
431 ;; Split LIST into sublists of max length N.
432 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
433 (defun imenu--split (list n
)
439 (push (pop remain
) sublist
)
442 ;; We have finished a sublist
443 (progn (push (nreverse sublist
) result
)
445 (setq sublist
'()))))
446 ;; There might be a sublist (if the length of LIST mod n is != 0)
447 ;; that has to be added to the result list.
449 (push (nreverse sublist
) result
))
452 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
453 ;;; In any case, add TITLE to the front of the alist.
454 (defun imenu--split-menu (menulist title
)
455 (let (keep-at-top tail
)
456 (if (memq imenu--rescan-item menulist
)
457 (setq keep-at-top
(cons imenu--rescan-item nil
)
458 menulist
(delq imenu--rescan-item menulist
)))
461 (if (imenu--subalist-p (car tail
))
462 (setq keep-at-top
(cons (car tail
) keep-at-top
)
463 menulist
(delq (car tail
) menulist
)))
464 (setq tail
(cdr tail
)))
465 (if imenu-sort-function
470 ;; Copy list method from the cl package `copy-list'
471 (while (consp oldlist
) (push (pop oldlist
) res
))
472 (if res
; in case, e.g. no functions defined
473 (prog1 (nreverse res
) (setcdr res oldlist
))))
474 imenu-sort-function
)))
475 (if (> (length menulist
) imenu-max-items
)
481 (cons (format "From: %s" (caar menu
)) menu
)))
482 (imenu--split menulist imenu-max-items
)))))
484 (nconc (nreverse keep-at-top
) menulist
))))
486 ;;; Split up each long alist that are nested within ALIST
487 ;;; into nested alists.
488 (defun imenu--split-submenus (alist)
489 (mapcar (function (lambda (elt)
493 (imenu--split-menu (cdr elt
) (car elt
))
497 ;;; Truncate all strings in MENULIST to imenu-max-item-length
498 (defun imenu--truncate-items (menulist)
503 (imenu--truncate-items (cdr item
)))
505 ;; truncate if necessary
506 (if (and (numberp imenu-max-item-length
)
507 (> (length (car item
)) imenu-max-item-length
))
508 (setcar item
(substring (car item
) 0 imenu-max-item-length
)))))))
512 (defun imenu--make-index-alist (&optional noerror
)
513 "Create an index-alist for the definitions in the current buffer.
515 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
516 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
517 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
518 The function `imenu--subalist-p' tests an element and returns t
519 if it is a sub-alist.
521 There is one simple element with negative POSITION; that's intended
522 as a way for the user to ask to recalculate the buffer's index alist."
523 (or (and imenu--index-alist
524 (or (not imenu-auto-rescan
)
525 (and imenu-auto-rescan
526 (> (buffer-size) imenu-auto-rescan-maxout
))))
527 ;; Get the index; truncate if necessary
529 (setq imenu--index-alist
533 (funcall imenu-create-index-function
))))
534 (imenu--truncate-items imenu--index-alist
)))
535 (or imenu--index-alist noerror
536 (error "No items suitable for an index found in this buffer"))
537 (or imenu--index-alist
538 (setq imenu--index-alist
(list nil
)))
539 ;; Add a rescan option to the index.
540 (cons imenu--rescan-item imenu--index-alist
))
542 ;;; Find all markers in alist and makes
543 ;;; them point nowhere.
544 ;;; The top-level call uses nil as the argument;
545 ;;; non-nil arguments are in recursivecalls.
546 (defvar imenu--cleanup-seen
)
548 (defun imenu--cleanup (&optional alist
)
549 ;; If alist is provided use that list.
550 ;; If not, empty the table of lists already seen
551 ;; and use imenu--index-alist.
553 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
554 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
561 ((markerp (cdr item
))
562 (set-marker (cdr item
) nil
))
563 ;; Don't process one alist twice.
564 ((memq (cdr item
) imenu--cleanup-seen
))
565 ((imenu--subalist-p item
)
566 (imenu--cleanup (cdr item
))))))
570 (defun imenu--create-keymap-2 (alist counter
&optional commands
)
576 ((imenu--subalist-p item
)
577 (append (list (setq counter
(1+ counter
))
578 (car item
) 'keymap
(car item
))
579 (imenu--create-keymap-2 (cdr item
) (+ counter
10) commands
)))
581 (let ((end (if commands
`(lambda () (interactive)
582 (imenu--menubar-select ',item
))
583 (cons '(nil) item
))))
585 (cons (car item
) end
))))
589 ;; If COMMANDS is non-nil, make a real keymap
590 ;; with a real command used as the definition.
591 ;; If it is nil, make something suitable for x-popup-menu.
592 (defun imenu--create-keymap-1 (title alist
&optional commands
)
593 (append (list 'keymap title
) (imenu--create-keymap-2 alist
0 commands
)))
596 (defun imenu--in-alist (str alist
)
597 "Check whether the string STR is contained in multi-level ALIST."
598 (let (elt head tail res
)
601 (setq elt
(car alist
)
605 ;; A nested ALIST element looks like
606 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
607 ;; while a bottom-level element looks like
608 ;; (INDEX-NAME . INDEX-POSITION)
609 ;; We are only interested in the bottom-level elements, so we need to
610 ;; recurse if TAIL is a list.
612 (if (setq res
(imenu--in-alist str tail
))
615 (setq alist nil res elt
))))
618 (defvar imenu-syntax-alist nil
619 "Alist of syntax table modifiers to use while executing `imenu--generic-function'.
621 The car of the assocs may be either a character or a string and the
622 cdr is a syntax description appropriate fo `modify-syntax-entry'. For
623 a string, all the characters in the string get the specified syntax.
625 This is typically used to give word syntax to characters which
626 normallsymbol syntax to simplify `imenu-expression'
627 and speed-up matching.")
628 (make-variable-buffer-local 'imenu-syntax-alist
)
630 (defun imenu-default-create-index-function ()
631 "*Wrapper for index searching functions.
633 Moves point to end of buffer and then repeatedly calls
634 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
635 Their results are gathered into an index alist."
636 ;; These should really be done by setting imenu-create-index-function
637 ;; in these major modes. But save that change for later.
638 (cond ((and imenu-prev-index-position-function
639 imenu-extract-index-name-function
)
640 (let ((index-alist '())
642 (goto-char (point-max))
643 (imenu-progress-message prev-pos
0 t
)
644 ;; Search for the function
645 (while (funcall imenu-prev-index-position-function
)
646 (imenu-progress-message prev-pos nil t
)
648 (setq name
(funcall imenu-extract-index-name-function
)))
650 ;; [ydi] updated for imenu-use-markers
651 (push (cons name
(if imenu-use-markers
(point-marker) (point)))
653 (imenu-progress-message prev-pos
100 t
)
655 ;; Use generic expression if possible.
656 ((and imenu-generic-expression
)
657 (imenu--generic-function imenu-generic-expression
))
659 (error "This buffer cannot use `imenu-default-create-index-function'"))))
661 (defun imenu--replace-spaces (name replacement
)
662 ;; Replace all spaces in NAME with REPLACEMENT.
663 ;; That second argument should be a string.
667 (if (char-equal ch ?\
)
669 (char-to-string ch
))))
673 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix
)
674 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
675 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
676 ;; name and a space concatenated to the names of the children.
677 ;; Third argument PREFIX is for internal use only.
681 (let* ((name (car item
))
683 (new-prefix (and concat-names
685 (concat prefix imenu-level-separator name
)
688 ((or (markerp pos
) (numberp pos
))
689 (list (cons new-prefix pos
)))
691 (imenu--flatten-index-alist pos new-prefix
))))))
695 ;;; Generic index gathering function.
698 (defvar imenu-case-fold-search t
699 "Defines whether `imenu--generic-function' should fold case when matching.
701 This buffer-local variable should be set (only) by initialization code
702 for modes which use `imenu--generic-function'. If it is not set, that
703 function will use the current value of `case-fold-search' to match
705 (make-variable-buffer-local 'imenu-case-fold-search
)
707 (defun imenu--generic-function (patterns)
708 ;; Built on some ideas that Erik Naggum <erik@naggum.no> once posted
710 "Return an index of the current buffer as an alist.
712 PATTERN is an alist with elements that look like this: (MENU-TITLE
715 MENU-TITLE is a string used as the title for the submenu or nil if the
716 entries are not nested.
718 REGEXP is a regexp that should match a construct in the buffer that is
719 to be displayed in the menu; i.e., function or variable definitions,
720 etc. It contains a substring which is the name to appear in the
721 menu. See the info section on Regexps for more information.
723 INDEX points to the substring in REGEXP that contains the name (of the
724 function, variable or type) that is to appear in the menu.
726 For emacs-lisp-mode for example PATTERN would look like:
728 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
729 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
730 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2))'
732 Returns an index of the current buffer as an alist. The elements in
733 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
734 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
737 \(imenu--generic-function PATTERN\)."
739 (let ((index-alist (list 'dummy
))
744 (function (lambda (pattern) (identity (cadr pattern
))))
745 patterns
"\\)\\|\\(")
749 (case-fold-search imenu-case-fold-search
)
750 (old-table (syntax-table))
751 (table (copy-syntax-table (syntax-table)))
752 (slist imenu-syntax-alist
))
753 ;; Modify the syntax table used while matching regexps.
755 ;; The character to modify may be a single CHAR or a STRING.
756 (let ((chars (if (numberp (car (car slist
)))
757 (list (car (car slist
)))
758 (mapcar 'identity
(car (car slist
)))))
759 (syntax (cdr (car slist
))))
761 (modify-syntax-entry (car chars
) syntax table
)
762 (setq chars
(cdr chars
)))
763 (setq slist
(cdr slist
))))
764 (goto-char (point-max))
765 (imenu-progress-message prev-pos
0 t
)
768 (set-syntax-table table
)
770 (while (re-search-backward global-regexp nil t
)
771 (imenu-progress-message prev-pos nil t
)
774 (goto-char (match-beginning 0))
778 (let ((menu-title (car pat
))
781 (function (cadddr pat
))
783 (if (and (not found
) ; Only allow one entry;
785 (let ((beg (match-beginning index
))
786 (end (match-end index
)))
790 (buffer-substring-no-properties beg end
)))
791 ;; [ydi] updated for imenu-use-markers
792 (if imenu-use-markers
793 (setq beg
(set-marker (make-marker) beg
)))
795 (nconc (list name beg function
)
799 (or (assoc menu-title index-alist
)
801 (cons menu-title
'())
802 index-alist
))))))))))
804 (set-syntax-table old-table
))))
805 (imenu-progress-message prev-pos
100 t
)
806 (let ((main-element (assq nil index-alist
)))
807 (nconc (delq main-element
(delq 'dummy index-alist
))
808 (cdr main-element
)))))
810 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
812 ;;; The main functions for this package!
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
816 (defun imenu--completion-buffer (index-alist &optional prompt
)
817 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
819 Returns t for rescan and otherwise a position number."
820 ;; Create a list for this buffer only when needed.
822 (prepared-index-alist
826 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
829 (if (eq imenu-always-use-completion-buffer-p
'never
)
830 (setq name
(completing-read (or prompt
"Index item: ")
832 nil t nil
'imenu--history-list
))
833 (save-window-excursion
834 ;; Display the completion buffer
835 (with-output-to-temp-buffer "*Completions*"
836 (display-completion-list
837 (all-completions "" prepared-index-alist
)))
838 (let ((minibuffer-setup-hook
840 (let ((buffer (current-buffer)))
842 (set-buffer "*Completions*")
843 (setq completion-reference-buffer buffer
)))))))
844 ;; Make a completion question
845 (setq name
(completing-read (or prompt
"Index item: ")
847 nil t nil
'imenu--history-list
)))))
848 (cond ((not (stringp name
))
850 ((string= name
(car imenu--rescan-item
))
853 (setq choice
(assoc name prepared-index-alist
))
854 (if (imenu--subalist-p choice
)
855 (imenu--completion-buffer (cdr choice
) prompt
)
858 (defun imenu--mouse-menu (index-alist event
&optional title
)
859 "Let the user select from a buffer index from a mouse menu.
861 INDEX-ALIST is the buffer index and EVENT is a mouse event.
863 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
864 (setq index-alist
(imenu--split-submenus index-alist
))
865 (let* ((menu (imenu--split-menu index-alist
866 (or title
(buffer-name))))
868 (setq menu
(imenu--create-keymap-1 (car menu
)
869 (if (< 1 (length (cdr menu
)))
871 (cdr (car (cdr menu
))))))
872 (setq position
(x-popup-menu event menu
))
873 (cond ((eq position nil
)
875 ;; If one call to x-popup-menu handled the nested menus,
876 ;; find the result by looking down the menus here.
877 ((and (listp position
)
878 (numberp (car position
))
879 (stringp (nth (1- (length position
)) position
)))
882 (setq final
(assoc (car position
) final
))
883 (setq position
(cdr position
)))
884 (or (string= (car final
) (car imenu--rescan-item
))
885 (cdr (cdr (cdr final
))))))
886 ;; If x-popup-menu went just one level and found a leaf item,
887 ;; return the INDEX-ALIST element for that.
888 ((and (consp position
)
889 (stringp (car position
))
890 (null (cdr position
)))
891 (or (string= (car position
) (car imenu--rescan-item
))
892 (assq (car position
) index-alist
)))
893 ;; If x-popup-menu went just one level
894 ;; and found a non-leaf item (a submenu),
895 ;; recurse to handle the rest.
897 (imenu--mouse-menu position event
899 (concat title imenu-level-separator
900 (car (rassq position index-alist
)))
901 (car (rassq position index-alist
))))))))
903 (defun imenu-choose-buffer-index (&optional prompt alist
)
904 "Let the user select from a buffer index and return the chosen index.
906 If the user originally activated this function with the mouse, a mouse
907 menu is used. Otherwise a completion buffer is used and the user is
908 prompted with PROMPT.
910 If you call this function with index alist ALIST, then it lets the user
913 With no index alist ALIST, it calls `imenu--make-index-alist' to
914 create the index alist.
916 If `imenu-always-use-completion-buffer-p' is non-nil, then the
917 completion buffer is always used, no matter if the mouse was used or
920 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
922 (mouse-triggered (listp last-nonmenu-event
))
924 ;; If selected by mouse, see to that the window where the mouse is
925 ;; really is selected.
927 (not (equal last-nonmenu-event
'(menu-bar)))
928 (let ((window (posn-window (event-start last-nonmenu-event
))))
929 (or (framep window
) (null window
) (select-window window
))))
930 ;; Create a list for this buffer only when needed.
932 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
934 (if (and mouse-triggered
935 (not imenu-always-use-completion-buffer-p
))
936 (imenu--mouse-menu index-alist last-nonmenu-event
)
937 (imenu--completion-buffer index-alist prompt
)))
940 (setq imenu--index-alist nil
)))
944 (defun imenu-add-to-menubar (name)
945 "Adds an `imenu' entry to the menu bar for the current buffer.
946 NAME is a string used to name the menu bar item.
947 See the command `imenu' for more information."
948 (interactive "sImenu menu item name: ")
949 (if (or (and imenu-prev-index-position-function
950 imenu-extract-index-name-function
)
951 imenu-generic-expression
952 (not (eq imenu-create-index-function
953 'imenu-default-create-index-function
)))
954 (let ((newmap (make-sparse-keymap))
955 (menu-bar (lookup-key (current-local-map) [menu-bar
])))
956 (define-key newmap
[menu-bar
]
957 (append (make-sparse-keymap) menu-bar
))
958 (define-key newmap
[menu-bar index
]
959 (cons name
(nconc (make-sparse-keymap "Imenu")
960 (make-sparse-keymap))))
961 (use-local-map (append newmap
(current-local-map)))
962 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
963 (error "The mode `%s' does not support Imenu" mode-name
)))
965 (defvar imenu-buffer-menubar nil
)
967 (defun imenu-update-menubar ()
968 (and (current-local-map)
969 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
970 (let ((index-alist (imenu--make-index-alist t
)))
971 ;; Don't bother updating if the index-alist has not changed
972 ;; since the last time we did it.
973 (or (equal index-alist imenu--last-menubar-index-alist
)
974 (let (menu menu1 old
)
975 (setq imenu--last-menubar-index-alist index-alist
)
976 (setq index-alist
(imenu--split-submenus index-alist
))
977 (setq menu
(imenu--split-menu index-alist
979 (setq menu1
(imenu--create-keymap-1 (car menu
)
980 (if (< 1 (length (cdr menu
)))
982 (cdr (car (cdr menu
))))
984 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
985 (setcdr old
(cdr menu1
)))))))
987 (defun imenu--menubar-select (item)
988 "Use Imenu to select the function or variable named in this menu item."
989 (if (equal item imenu--rescan-item
)
992 (setq imenu--index-alist nil
)
993 (imenu-update-menubar))
996 (defun imenu-default-goto-function (name position
&optional rest
)
997 "This function is used for moving the point to POSITION.
998 The NAME and REST parameters are not used, they are here just to make
999 this function have the same interface as a function placed in a special
1001 (if (or (< position
(point-min))
1002 (> position
(point-max)))
1003 ;; widen if outside narrowing
1005 (goto-char position
))
1008 (defun imenu (index-item)
1009 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1010 See `imenu-choose-buffer-index' for more information."
1011 (interactive (list (imenu-choose-buffer-index)))
1012 ;; Convert a string to an alist element.
1013 (if (stringp index-item
)
1014 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1018 (let* ((is-special-item (listp (cdr index-item
)))
1021 (caddr index-item
) imenu-default-goto-function
))
1022 (position (if is-special-item
1023 (cadr index-item
) (cdr index-item
)))
1024 (rest (if is-special-item
(cddr index-item
))))
1025 (apply function
(car index-item
) position rest
)))))
1029 ;;; imenu.el ends here