1 ;;; imenu.el --- framework for mode-specific buffer indexes -*- lexical-binding: t -*-
3 ;; Copyright (C) 1994-1998, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
7 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 ;; Purpose of this package:
29 ;; To present a framework for mode-specific buffer indexes.
30 ;; A buffer index is an alist of names and buffer positions.
31 ;; For instance all functions in a C-file and their positions.
33 ;; It is documented in the Emacs Lisp manual.
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.
50 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
51 ;; [dean] - Dean Andrews ada@unison.com
52 ;; [alon] - Alon Albert al@mercury.co.il
53 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
54 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
55 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
56 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
57 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
58 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
62 (eval-when-compile (require 'cl-lib
))
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 ;;; Customizable variables
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71 "Mode-specific buffer indexes."
75 :link
'(custom-manual "(elisp)Imenu"))
77 (defcustom imenu-use-markers t
78 "Non-nil means use markers instead of integers for Imenu buffer positions.
80 Setting this to nil makes Imenu work a little faster but editing the
81 buffer will make the generated index positions wrong.
83 This might not yet be honored by all index-building functions."
88 (defcustom imenu-max-item-length
60
89 "If a number, truncate Imenu entries to that length."
90 :type
'(choice integer
91 (const :tag
"Unlimited"))
94 (defcustom imenu-auto-rescan nil
95 "Non-nil means Imenu should always rescan the buffers."
99 (defcustom imenu-auto-rescan-maxout
60000
100 "Imenu auto-rescan is disabled in buffers larger than this size (in bytes).
101 This variable is buffer-local."
105 (defvar imenu-always-use-completion-buffer-p nil
)
106 (make-obsolete-variable 'imenu-always-use-completion-buffer-p
107 'imenu-use-popup-menu
"22.1")
109 (defcustom imenu-use-popup-menu
110 (if imenu-always-use-completion-buffer-p
111 (not (eq imenu-always-use-completion-buffer-p
'never
))
113 "Use a popup menu rather than a minibuffer prompt.
114 If nil, always use a minibuffer prompt.
115 If t, always use a popup menu,
116 If `on-mouse' use a popup menu when `imenu' was invoked with the mouse."
117 :type
'(choice (const :tag
"On Mouse" on-mouse
)
118 (const :tag
"Never" nil
)
119 (other :tag
"Always" t
))
122 (defcustom imenu-eager-completion-buffer
123 (not (eq imenu-always-use-completion-buffer-p
'never
))
124 "If non-nil, eagerly popup the completion buffer."
129 (defcustom imenu-after-jump-hook nil
130 "Hooks called after jumping to a place in the buffer.
132 Useful things to use here include `reposition-window', `recenter', and
133 \(lambda () (recenter 0)) to show at top of screen."
138 (defcustom imenu-sort-function nil
139 "The function to use for sorting the index mouse-menu.
141 Affects only the mouse index menu.
143 Set this to nil if you don't want any sorting (faster).
144 The items in the menu are then presented in the order they were found
147 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
149 The function should take two arguments and return t if the first
150 element should come before the second. The arguments are cons cells;
151 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
152 :type
'(choice (const :tag
"No sorting" nil
)
153 (const :tag
"Sort by name" imenu--sort-by-name
)
154 (function :tag
"Another function"))
157 (defcustom imenu-max-items
25
158 "Maximum number of elements in a mouse menu for Imenu."
162 ;; No longer used. KFS 2004-10-27
163 ;; (defcustom imenu-scanning-message "Scanning buffer for index (%3d%%)"
164 ;; "Progress message during the index scanning of the buffer.
165 ;; If non-nil, user gets a message during the scanning of the buffer.
167 ;; Relevant only if the mode-specific function that creates the buffer
168 ;; index use `imenu-progress-message', and not useful if that is fast, in
169 ;; which case you might as well set this to nil."
170 ;; :type '(choice string
171 ;; (const :tag "None" nil))
174 (defcustom imenu-space-replacement
"."
175 "The replacement string for spaces in index names.
176 Used when presenting the index in a completion buffer to make the
177 names work as tokens."
178 :type
'(choice string
(const nil
))
181 (defcustom imenu-level-separator
":"
182 "The separator between index names of different levels.
183 Used for making mouse-menu titles and for flattening nested indexes
184 with name concatenation."
188 (defcustom imenu-generic-skip-comments-and-strings t
189 "When non-nil, ignore text inside comments and strings.
190 Only affects `imenu--generic-function'."
196 (defvar imenu-generic-expression nil
197 "List of definition matchers for creating an Imenu index.
198 Each element of this list should have the form
200 (MENU-TITLE REGEXP INDEX [FUNCTION] [ARGUMENTS...])
202 MENU-TITLE should be nil (in which case the matches for this
203 element are put in the top level of the buffer index) or a
204 string (which specifies the title of a submenu into which the
206 REGEXP is a regular expression matching a definition construct
207 which is to be displayed in the menu. REGEXP may also be a
208 function, called without arguments. It is expected to search
209 backwards. It must return true and set `match-data' if it finds
211 INDEX is an integer specifying which subexpression of REGEXP
212 matches the definition's name; this subexpression is displayed as
214 FUNCTION, if present, specifies a function to call when the index
215 item is selected by the user. This function is called with
216 arguments consisting of the item name, the buffer position, and
219 The variable `imenu-case-fold-search' determines whether or not
220 the regexp matches are case sensitive, and `imenu-syntax-alist'
221 can be used to alter the syntax table for the search.
223 If non-nil this pattern is passed to `imenu--generic-function' to
224 create a buffer index.
226 For example, see the value of `fortran-imenu-generic-expression'
227 used by `fortran-mode' with `imenu-syntax-alist' set locally to
228 give the characters which normally have \"symbol\" syntax
229 \"word\" syntax during matching.")
230 ;;;###autoload(put 'imenu-generic-expression 'risky-local-variable t)
233 (make-variable-buffer-local 'imenu-generic-expression
)
238 (defvar imenu-create-index-function
'imenu-default-create-index-function
239 "The function to use for creating an index alist of the current buffer.
241 It should be a function that takes no arguments and returns
242 an index alist of the current buffer. The function is
243 called within a `save-excursion'.
245 See `imenu--index-alist' for the format of the buffer index alist.")
247 (make-variable-buffer-local 'imenu-create-index-function
)
250 (defvar imenu-prev-index-position-function
'beginning-of-defun
251 "Function for finding the next index position.
253 If `imenu-create-index-function' is set to
254 `imenu-default-create-index-function', then you must set this variable
255 to a function that will find the next index, looking backwards in the
258 The function should leave point at the place to be connected to the
259 index and it should return nil when it doesn't find another index.")
261 (make-variable-buffer-local 'imenu-prev-index-position-function
)
264 (defvar imenu-extract-index-name-function nil
265 "Function for extracting the index item name, given a position.
267 This function is called after `imenu-prev-index-position-function'
268 finds a position for an index item, with point at that position.
269 It should return the name for that index item.")
271 (make-variable-buffer-local 'imenu-extract-index-name-function
)
274 (defvar imenu-name-lookup-function nil
275 "Function to compare string with index item.
277 This function will be called with two strings, and should return
278 non-nil if they match.
280 If nil, comparison is done with `string='.
281 Set this to some other function for more advanced comparisons,
282 such as \"begins with\" or \"name matches and number of
285 (make-variable-buffer-local 'imenu-name-lookup-function
)
288 (defvar imenu-default-goto-function
'imenu-default-goto-function
289 "The default function called when selecting an Imenu item.
290 The function in this variable is called when selecting a normal index-item.")
292 (make-variable-buffer-local 'imenu-default-goto-function
)
295 (defun imenu--subalist-p (item)
299 (not (functionp (cadr item
)))))
301 (defmacro imenu-progress-message
(_prevpos &optional _relpos _reverse
)
302 "Macro to display a progress message.
303 RELPOS is the relative position to display.
304 If RELPOS is nil, then the relative position in the buffer
306 PREVPOS is the variable in which we store the last position displayed."
308 ;; Made obsolete/empty, as computers are now faster than the eye, and
309 ;; it had problems updating the messages correctly, and could shadow
310 ;; more important messages/prompts in the minibuffer. KFS 2004-10-27.
313 ;; imenu-scanning-message
314 ;; (let ((pos ,(if relpos
316 ;; `(imenu--relative-position ,reverse))))
318 ;; `(> pos (+ 5 ,prevpos)))
320 ;; (message imenu-scanning-message pos)
321 ;; (setq ,prevpos pos)))))
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;;;; Some examples of functions utilizing the framework of this
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 ;; FIXME: This was the only imenu-example-* definition actually used,
333 ;; by cperl-mode.el. Now cperl-mode has its own copy, so these can
335 (defun imenu-example--name-and-position ()
336 "Return the current/previous sexp and its (beginning) location.
338 (declare (obsolete "use your own function instead." "23.2"))
341 ;; [ydi] modified for imenu-use-markers
342 (let ((beg (if imenu-use-markers
(point-marker) (point)))
343 (end (progn (forward-sexp) (point))))
344 (cons (buffer-substring beg end
)
351 (defun imenu-example--lisp-extract-index-name ()
352 ;; Example of a candidate for `imenu-extract-index-name-function'.
353 ;; This will generate a flat index of definitions in a lisp file.
354 (declare (obsolete nil
"23.2"))
356 (and (looking-at "(def")
362 (end (progn (forward-sexp -
1) (point))))
363 (buffer-substring beg end
)))
366 (defun imenu-example--create-lisp-index ()
367 ;; Example of a candidate for `imenu-create-index-function'.
368 ;; It will generate a nested index of definitions.
369 (declare (obsolete nil
"23.2"))
370 (let ((index-alist '())
371 (index-var-alist '())
372 (index-type-alist '())
373 (index-unknown-alist '()))
374 (goto-char (point-max))
375 ;; Search for the function
376 (while (beginning-of-defun)
378 (and (looking-at "(def")
382 ((looking-at "def\\(var\\|const\\)")
384 (push (imenu-example--name-and-position)
386 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
388 (push (imenu-example--name-and-position)
390 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
392 (if (= (char-after (1- (point))) ?\
))
397 (push (imenu-example--name-and-position)
401 (push (imenu-example--name-and-position)
402 index-unknown-alist
)))))))
404 (push (cons "Variables" index-var-alist
)
406 (and index-type-alist
407 (push (cons "Types" index-type-alist
)
409 (and index-unknown-alist
410 (push (cons "Syntax-unknown" index-unknown-alist
)
414 ;; Regular expression to find C functions
415 (defvar imenu-example--function-name-regexp-c
417 "^[a-zA-Z0-9]+[ \t]?" ; Type specs; there can be no
418 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
419 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
420 "\\([*&]+[ \t]*\\)?" ; Pointer.
421 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; Name.
424 (defun imenu-example--create-c-index (&optional regexp
)
425 (declare (obsolete nil
"23.2"))
426 (let ((index-alist '())
428 (goto-char (point-min))
429 ;; Search for the function
431 (while (re-search-forward
432 (or regexp imenu-example--function-name-regexp-c
)
436 (goto-char (scan-sexps (point) 1))
437 (setq char
(following-char)))
438 ;; Skip this function name if it is a prototype declaration.
439 (if (not (eq char ?\
;))
440 (push (imenu-example--name-and-position) index-alist
))))
441 (nreverse index-alist
)))
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;;; Internal variables
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; The item to use in the index for rescanning the buffer.
450 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
452 ;; The latest buffer index.
453 (defvar-local imenu--index-alist nil
454 "The buffer index alist computed for this buffer in Imenu.
456 Simple elements in the alist look like (INDEX-NAME . POSITION).
457 POSITION is the buffer position of the item; to go to the item
458 is simply to move point to that position.
459 POSITION is passed to `imenu-default-goto-function', so it can be a non-number
460 if that variable has been changed (e.g. Semantic uses overlays for POSITIONs).
462 Special elements look like (INDEX-NAME POSITION FUNCTION ARGUMENTS...).
463 To \"go to\" a special element means applying FUNCTION
464 to INDEX-NAME, POSITION, and the ARGUMENTS.
466 A nested sub-alist element looks like (INDEX-NAME . SUB-ALIST).
467 The function `imenu--subalist-p' tests an element and returns t
468 if it is a sub-alist.
470 There is one simple element with negative POSITION; selecting that
471 element recalculates the buffer's index alist.")
472 ;;;###autoload(put 'imenu--index-alist 'risky-local-variable t)
474 (defvar-local imenu--last-menubar-index-alist nil
475 "The latest buffer index alist used to update the menu bar menu.")
477 (defvar imenu--history-list nil
478 ;; Making this buffer local caused it not to work!
479 "History list for 'jump-to-function-in-buffer'.")
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
483 ;;; Internal support functions
485 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 (defun imenu--sort-by-name (item1 item2
)
488 "Comparison function to sort items depending on their index name.
489 An item looks like (NAME . POSITION)."
490 (string-lessp (car item1
) (car item2
)))
492 (defun imenu--sort-by-position (item1 item2
)
493 (< (cdr item1
) (cdr item2
)))
495 (defun imenu--relative-position (&optional reverse
)
496 "Support function to calculate relative position in buffer.
497 Beginning of buffer is 0 and end of buffer is 100
498 If REVERSE is non-nil then the beginning is 100 and the end is 0."
500 (total (buffer-size)))
501 (and reverse
(setq pos
(- total pos
)))
503 ;; Avoid overflow from multiplying by 100!
504 (/ (1- pos
) (max (/ total
100) 1))
505 (/ (* 100 (1- pos
)) (max total
1)))))
507 (defun imenu--split (list n
)
508 "Split LIST into sublists of max length N.
509 Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
510 The returned list DOES NOT share structure with LIST."
516 (push (pop remain
) sublist
)
519 ;; We have finished a sublist
520 (progn (push (nreverse sublist
) result
)
522 (setq sublist
'()))))
523 ;; There might be a sublist (if the length of LIST mod n is != 0)
524 ;; that has to be added to the result list.
526 (push (nreverse sublist
) result
))
529 (defun imenu--split-menu (menulist title
)
530 "Split the alist MENULIST into a nested alist, if it is long enough.
531 In any case, add TITLE to the front of the alist.
532 If IMENU--RESCAN-ITEM is present in MENULIST, it is moved to the
533 beginning of the returned alist.
534 The returned alist DOES NOT share structure with MENULIST."
535 (let ((menulist (copy-sequence menulist
))
537 (if (memq imenu--rescan-item menulist
)
538 (setq keep-at-top
(list imenu--rescan-item
)
539 menulist
(delq imenu--rescan-item menulist
)))
540 (dolist (item menulist
)
541 (when (imenu--subalist-p item
)
542 (push item keep-at-top
)
543 (setq menulist
(delq item menulist
))))
544 (if imenu-sort-function
545 (setq menulist
(sort menulist imenu-sort-function
)))
546 (if (> (length menulist
) imenu-max-items
)
550 (cons (format "From: %s" (caar menu
)) menu
))
551 (imenu--split menulist imenu-max-items
))))
553 (nconc (nreverse keep-at-top
) menulist
))))
555 (defun imenu--split-submenus (alist)
556 "Split up each long alist that are nested within ALIST into nested alists.
557 Return a split and sorted copy of ALIST. The returned alist DOES
558 NOT share structure with ALIST."
559 (mapcar (lambda (elt)
560 (if (imenu--subalist-p elt
)
561 (imenu--split-menu (cdr elt
) (car elt
))
565 (defun imenu--truncate-items (menulist)
566 "Truncate all strings in MENULIST to `imenu-max-item-length'."
568 ;; Truncate if necessary.
569 (when (and (numberp imenu-max-item-length
)
570 (> (length (car item
)) imenu-max-item-length
))
571 (setcar item
(substring (car item
) 0 imenu-max-item-length
)))
572 (when (imenu--subalist-p item
)
573 (imenu--truncate-items (cdr item
))))
576 (defun imenu--make-index-alist (&optional noerror
)
577 "Create an index alist for the definitions in the current buffer.
578 This works by using the hook function `imenu-create-index-function'.
579 Report an error if the list is empty unless NOERROR is supplied and
582 See `imenu--index-alist' for the format of the index alist."
583 (or (and imenu--index-alist
584 (or (not imenu-auto-rescan
)
585 (and imenu-auto-rescan
586 (> (buffer-size) imenu-auto-rescan-maxout
))))
587 ;; Get the index; truncate if necessary.
589 (setq imenu--index-alist
593 (funcall imenu-create-index-function
))))
594 (imenu--truncate-items imenu--index-alist
)))
595 (or imenu--index-alist noerror
596 (user-error "No items suitable for an index found in this buffer"))
597 (or imenu--index-alist
598 (setq imenu--index-alist
(list nil
)))
599 ;; Add a rescan option to the index.
600 (cons imenu--rescan-item imenu--index-alist
))
602 (defvar imenu--cleanup-seen nil
)
604 (defun imenu--cleanup (&optional alist
)
605 "Find all markers in ALIST and make them point nowhere.
606 If ALIST is nil (the normal case), use `imenu--index-alist'.
607 Non-nil arguments are in recursive calls."
608 ;; If alist is provided use that list.
609 ;; If not, empty the table of lists already seen
610 ;; and use imenu--index-alist.
612 (setq imenu--cleanup-seen
(cons alist imenu--cleanup-seen
))
613 (setq alist imenu--index-alist imenu--cleanup-seen
(list alist
)))
618 ((markerp (cdr item
)) (set-marker (cdr item
) nil
))
619 ;; Don't process one alist twice.
620 ((memq (cdr item
) imenu--cleanup-seen
))
621 ((imenu--subalist-p item
) (imenu--cleanup (cdr item
)))))
624 (defun imenu--create-keymap (title alist
&optional cmd
)
628 `(,(car item
) ,(car item
)
630 ((imenu--subalist-p item
)
631 (imenu--create-keymap (car item
) (cdr item
) cmd
))
633 `(lambda () (interactive)
634 ,(if cmd
`(,cmd
',item
) (list 'quote item
)))))))
637 (defun imenu--in-alist (str alist
)
638 "Check whether the string STR is contained in multi-level ALIST."
639 (let (elt head tail res
)
642 (setq elt
(car alist
)
646 ;; A nested ALIST element looks like
647 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
648 ;; while a bottom-level element looks like
649 ;; (INDEX-NAME . INDEX-POSITION)
651 ;; (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
652 ;; We are only interested in the bottom-level elements, so we need to
653 ;; recurse if TAIL is a nested ALIST.
654 (cond ((imenu--subalist-p elt
)
655 (if (setq res
(imenu--in-alist str tail
))
657 ((if imenu-name-lookup-function
658 (funcall imenu-name-lookup-function str head
)
660 (setq alist nil res elt
))))
663 (defvar imenu-syntax-alist nil
664 "Alist of syntax table modifiers to use while in `imenu--generic-function'.
666 The car of the assocs may be either a character or a string and the
667 cdr is a syntax description appropriate for `modify-syntax-entry'. For
668 a string, all the characters in the string get the specified syntax.
670 This is typically used to give word syntax to characters which
671 normally have symbol syntax to simplify `imenu-expression'
672 and speed-up matching.")
674 (make-variable-buffer-local 'imenu-syntax-alist
)
676 (defun imenu-default-create-index-function ()
677 "Default function to create an index alist of the current buffer.
679 The most general method is to move point to end of buffer, then repeatedly call
680 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
681 All the results returned by the latter are gathered into an index alist.
682 This method is used if those two variables are non-nil.
684 The alternate method, which is the one most often used, is to call
685 `imenu--generic-function' with `imenu-generic-expression' as argument."
686 ;; These should really be done by setting imenu-create-index-function
687 ;; in these major modes. But save that change for later.
688 (cond ((and imenu-prev-index-position-function
689 imenu-extract-index-name-function
)
690 (let ((index-alist '()) (pos (point-max))
693 ;; Search for the function
694 (while (funcall imenu-prev-index-position-function
)
695 (unless (< (point) pos
)
696 (error "Infinite loop at %s:%d: imenu-prev-index-position-function does not move point" (buffer-name) pos
))
699 (setq name
(funcall imenu-extract-index-name-function
)))
701 ;; [ydi] Updated for imenu-use-markers.
703 (if imenu-use-markers
(point-marker) (point)))
706 ;; Use generic expression if possible.
707 ((and imenu-generic-expression
)
708 (imenu--generic-function imenu-generic-expression
))
710 (user-error "This buffer cannot use `imenu-default-create-index-function'"))))
713 ;;; Generic index gathering function.
716 (defvar imenu-case-fold-search t
717 "Defines whether `imenu--generic-function' should fold case when matching.
719 This variable should be set (only) by initialization code
720 for modes which use `imenu--generic-function'. If it is not set, but
721 `font-lock-defaults' is set, then font-lock's setting is used.")
723 (make-variable-buffer-local 'imenu-case-fold-search
)
725 ;; This function can be called with quitting disabled,
726 ;; so it needs to be careful never to loop!
727 (defun imenu--generic-function (patterns)
728 "Return an index alist of the current buffer based on PATTERNS.
729 PATTERNS should be an alist with the same form as `imenu-generic-expression'.
731 If `imenu-generic-skip-comments-and-strings' is non-nil, this ignores
732 text inside comments and strings.
734 If `imenu-case-fold-search' is non-nil, this ignores case.
736 The return value is an alist of the form
737 (INDEX-NAME . INDEX-POSITION)
739 (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
740 The return value may also consist of nested index alists like:
741 (INDEX-NAME . INDEX-ALIST)
742 depending on PATTERNS."
743 (let ((index-alist (list 'dummy
))
744 (case-fold-search (if (or (local-variable-p 'imenu-case-fold-search
)
745 (not (local-variable-p 'font-lock-defaults
)))
746 imenu-case-fold-search
747 (nth 2 font-lock-defaults
)))
748 (old-table (syntax-table))
749 (table (copy-syntax-table (syntax-table)))
750 (slist imenu-syntax-alist
))
751 ;; Modify the syntax table used while matching regexps.
753 ;; The character(s) to modify may be a single char or a string.
754 (if (numberp (car syn
))
755 (modify-syntax-entry (car syn
) (cdr syn
) table
)
757 (modify-syntax-entry c
(cdr syn
) table
))
759 (goto-char (point-max))
760 (unwind-protect ; For syntax table.
762 (set-syntax-table table
)
764 ;; Map over the elements of imenu-generic-expression
765 ;; (typically functions, variables ...).
766 (dolist (pat patterns
)
767 (let ((menu-title (car pat
))
770 (function (nth 3 pat
))
771 (rest (nthcdr 4 pat
))
773 ;; Go backwards for convenience of adding items in order.
774 (goto-char (point-max))
775 (while (and (if (functionp regexp
)
778 (re-search-backward regexp nil t
)
779 ;; Do not count invisible definitions.
780 (let ((invis (invisible-p (point))))
785 (setq invis
(not (re-search-backward
788 ;; Exit the loop if we get an empty match,
789 ;; because it means a bad regexp was specified.
790 (not (= (match-beginning 0) (match-end 0))))
792 ;; Record the start of the line in which the match starts.
793 ;; That's the official position of this definition.
794 (goto-char (match-beginning index
))
797 ;; Add this sort of submenu only when we've found an
798 ;; item for it, avoiding empty, duff menus.
799 (unless (assoc menu-title index-alist
)
800 (push (list menu-title
) index-alist
))
801 (if imenu-use-markers
802 (setq beg
(copy-marker beg
)))
805 (nconc (list (match-string-no-properties index
)
808 (cons (match-string-no-properties index
)
810 ;; This is the desired submenu,
811 ;; starting with its title (or nil).
812 (menu (assoc menu-title index-alist
)))
813 ;; Insert the item unless it is already present.
814 (unless (or (member item
(cdr menu
))
815 (and imenu-generic-skip-comments-and-strings
816 (nth 8 (syntax-ppss))))
818 (cons item
(cdr menu
)))))
819 ;; Go to the start of the match, to make sure we
820 ;; keep making progress backwards.
822 (set-syntax-table old-table
)))
823 ;; Sort each submenu by position.
824 ;; This is in case one submenu gets items from two different regexps.
825 (dolist (item index-alist
)
827 (setcdr item
(sort (cdr item
) 'imenu--sort-by-position
))))
828 (let ((main-element (assq nil index-alist
)))
829 (nconc (delq main-element
(delq 'dummy index-alist
))
830 (cdr main-element
)))))
832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
834 ;;; The main functions for this package!
836 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
838 ;; See also info-lookup-find-item
839 (defun imenu-find-default (guess completions
)
840 "Fuzzily find an item based on GUESS inside the alist COMPLETIONS."
842 (let ((case-fold-search t
))
843 (if (assoc guess completions
) guess
844 (dolist (re (list (concat "\\`" (regexp-quote guess
) "\\'")
845 (concat "\\`" (regexp-quote guess
))
846 (concat (regexp-quote guess
) "\\'")
847 (regexp-quote guess
)))
848 (dolist (x completions
)
849 (if (string-match re
(car x
)) (throw 'found
(car x
)))))))))
851 (defun imenu--completion-buffer (index-alist &optional prompt
)
852 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
854 Return one of the entries in index-alist or nil."
855 ;; Create a list for this buffer only when needed.
856 (let ((name (thing-at-point 'symbol
))
858 (prepared-index-alist
859 (if (not imenu-space-replacement
) index-alist
862 (cons (subst-char-in-string ?\s
(aref imenu-space-replacement
0)
867 (setq name
(or (imenu-find-default name prepared-index-alist
) name
)))
869 ((and name
(imenu--in-alist name prepared-index-alist
))
870 (setq prompt
(format "Index item (default %s): " name
)))
871 (t (setq prompt
"Index item: ")))
872 (let ((minibuffer-setup-hook minibuffer-setup-hook
))
873 ;; Display the completion buffer.
874 (if (not imenu-eager-completion-buffer
)
875 (add-hook 'minibuffer-setup-hook
'minibuffer-completion-help
))
876 (setq name
(completing-read prompt
878 nil t nil
'imenu--history-list name
)))
881 (setq choice
(assoc name prepared-index-alist
))
882 (if (imenu--subalist-p choice
)
883 (imenu--completion-buffer (cdr choice
) prompt
)
886 (defun imenu--mouse-menu (index-alist event
&optional title
)
887 "Let the user select from a buffer index from a mouse menu.
889 INDEX-ALIST is the buffer index and EVENT is a mouse event.
891 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
892 (setq index-alist
(imenu--split-submenus index-alist
))
893 (let* ((menu (imenu--split-menu index-alist
(or title
(buffer-name))))
894 (map (imenu--create-keymap (car menu
)
895 (cdr (if (< 1 (length (cdr menu
)))
897 (car (cdr menu
)))))))
898 (popup-menu map event
)))
900 (defun imenu-choose-buffer-index (&optional prompt alist
)
901 "Let the user select from a buffer index and return the chosen index.
903 If the user originally activated this function with the mouse, a mouse
904 menu is used. Otherwise a completion buffer is used and the user is
905 prompted with PROMPT.
907 If you call this function with index alist ALIST, then it lets the user
910 With no index alist ALIST, it calls `imenu--make-index-alist' to
911 create the index alist.
913 If `imenu-use-popup-menu' is nil, then the completion buffer
914 is always used, no matter if the mouse was used or not.
916 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
918 (mouse-triggered (listp last-nonmenu-event
))
920 ;; If selected by mouse, see to that the window where the mouse is
921 ;; really is selected.
923 (not (equal last-nonmenu-event
'(menu-bar)))
924 (let ((window (posn-window (event-start last-nonmenu-event
))))
925 (or (framep window
) (null window
) (select-window window
))))
926 ;; Create a list for this buffer only when needed.
928 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
930 (if (and imenu-use-popup-menu
931 (or (eq imenu-use-popup-menu t
) mouse-triggered
))
932 (imenu--mouse-menu index-alist last-nonmenu-event
)
933 (imenu--completion-buffer index-alist prompt
)))
934 (and (equal result imenu--rescan-item
)
936 (setq result t imenu--index-alist nil
)))
940 (defun imenu-add-to-menubar (name)
941 "Add an `imenu' entry to the menu bar for the current buffer.
942 NAME is a string used to name the menu bar item.
943 See the command `imenu' for more information."
944 (interactive "sImenu menu item name: ")
945 (if (or (and imenu-prev-index-position-function
946 imenu-extract-index-name-function
)
947 imenu-generic-expression
948 (not (eq imenu-create-index-function
949 'imenu-default-create-index-function
)))
950 (unless (and (current-local-map)
951 (keymapp (lookup-key (current-local-map) [menu-bar index
])))
952 (let ((newmap (make-sparse-keymap)))
953 (set-keymap-parent newmap
(current-local-map))
954 (setq imenu--last-menubar-index-alist nil
)
955 (define-key newmap
[menu-bar index
]
956 `(menu-item ,name
,(make-sparse-keymap "Imenu")))
957 (use-local-map newmap
)
958 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
)))
959 (user-error "The mode `%s' does not support Imenu"
960 (format-mode-line mode-name
))))
963 (defun imenu-add-menubar-index ()
964 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
966 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
968 (imenu-add-to-menubar "Index"))
970 (defvar imenu-buffer-menubar nil
)
972 (defvar-local imenu-menubar-modified-tick
0
973 "The value of (buffer-chars-modified-tick) as of the last call
974 to `imenu-update-menubar'.")
976 (defun imenu-update-menubar ()
977 (when (and (current-local-map)
978 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
979 (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick
))
980 (setq imenu-menubar-modified-tick
(buffer-chars-modified-tick))
981 (let ((index-alist (imenu--make-index-alist t
)))
982 ;; Don't bother updating if the index-alist has not changed
983 ;; since the last time we did it.
984 (unless (equal index-alist imenu--last-menubar-index-alist
)
985 (let (menu menu1 old
)
986 (setq imenu--last-menubar-index-alist index-alist
)
987 (setq index-alist
(imenu--split-submenus index-alist
))
988 (setq menu
(imenu--split-menu index-alist
990 (setq menu1
(imenu--create-keymap (car menu
)
991 (cdr (if (< 1 (length (cdr menu
)))
994 'imenu--menubar-select
))
995 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
996 ;; This should never happen, but in some odd cases, potentially,
997 ;; lookup-key may return a dynamically composed keymap.
998 (if (keymapp (cadr old
)) (setq old
(cadr old
)))
999 (setcdr old
(cdr menu1
)))))))
1001 (defun imenu--menubar-select (item)
1002 "Use Imenu to select the function or variable named in this menu ITEM."
1003 (if (equal item imenu--rescan-item
)
1006 ;; Make sure imenu-update-menubar redoes everything.
1007 (setq imenu-menubar-modified-tick -
1)
1008 (setq imenu--index-alist nil
)
1009 (setq imenu--last-menubar-index-alist nil
)
1010 (imenu-update-menubar)
1015 (defun imenu-default-goto-function (_name position
&rest _rest
)
1016 "Move to the given position.
1018 NAME is ignored. POSITION is where to move. REST is also ignored.
1019 The ignored args just make this function have the same interface as a
1020 function placed in a special index-item."
1021 (if (or (< position
(point-min))
1022 (> position
(point-max)))
1023 ;; Widen if outside narrowing.
1025 (goto-char position
))
1028 (defun imenu (index-item)
1029 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1030 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1031 for more information."
1032 (interactive (list (imenu-choose-buffer-index)))
1033 ;; Convert a string to an alist element.
1034 (if (stringp index-item
)
1035 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
1038 (`(,name
,pos
,fn .
,args
)
1040 (apply fn name pos args
)
1041 (run-hooks 'imenu-after-jump-hook
))
1042 (`(,name .
,pos
) (imenu (list name pos imenu-default-goto-function
)))
1043 (_ (error "Unknown imenu item: %S" index-item
)))))
1047 ;;; imenu.el ends here