1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Purpose of this package:
30 ;; To present a framework for mode-specific buffer indexes.
31 ;; A buffer index is an alist of names and buffer positions.
32 ;; For instance all functions in a C-file and their positions.
36 ;; A mode-specific function is called to generate the index. It is
37 ;; then presented to the user, who can choose from this index.
39 ;; The package comes with a set of example functions for how to
40 ;; utilize this package.
42 ;; There are *examples* for index gathering functions/regular
43 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
44 ;; customize for other modes. A function for jumping to the chosen
45 ;; index position is also supplied.
48 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
49 ;; [dean] - Dean Andrews ada@unison.com
50 ;; [alon] - Alon Albert al@mercury.co.il
51 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
52 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
53 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
54 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
55 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
56 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
60 (eval-when-compile (require 'cl
))
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64 ;;; Customizable variables
66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 (defvar imenu-auto-rescan nil
69 "*Non-nil means Imenu should always rescan the buffers.")
71 (defvar imenu-auto-rescan-maxout
60000
72 "* auto-rescan is disabled in buffers larger than this.
73 This variable is buffer-local.")
75 (defvar imenu-always-use-completion-buffer-p nil
76 "*Set this to non-nil for displaying the index in a completion buffer.
78 Non-nil means always display the index in a completion buffer.
79 Nil means display the index as a mouse menu when the mouse was
80 used to invoke `imenu'.
81 `never' means never automatically display a listing of any kind.")
83 (defvar imenu-sort-function nil
84 "*The function to use for sorting the index mouse-menu.
86 Affects only the mouse index menu.
88 Set this to nil if you don't want any sorting (faster).
89 The items in the menu are then presented in the order they were found
92 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
94 The function should take two arguments and return T if the first
95 element should come before the second. The arguments are cons cells;
96 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example.")
98 (defvar imenu-max-items
25
99 "*Maximum number of elements in an mouse menu for Imenu.")
101 (defvar imenu-scanning-message
"Scanning buffer for index (%3d%%)"
102 "*Progress message during the index scanning of the buffer.
103 If non-nil, user gets a message during the scanning of the buffer
105 Relevant only if the mode-specific function that creates the buffer
106 index use `imenu-progress-message'.")
108 (defvar imenu-space-replacement
"^"
109 "*The replacement string for spaces in index names.
110 Used when presenting the index in a completion-buffer to make the
111 names work as tokens.")
113 (defvar imenu-level-separator
":"
114 "*The separator between index names of different levels.
115 Used for making mouse-menu titles and for flattening nested indexes
116 with name concatenation.")
119 (defvar imenu-generic-expression nil
120 "The regex pattern to use for creating a buffer index.
122 If non-nil this pattern is passed to `imenu-create-index-with-pattern'
123 to create a buffer index.
125 It is an alist with elements that look like this: (MENU-TITLE
128 MENU-TITLE is a string used as the title for the submenu or nil if the
129 entries are not nested.
131 REGEXP is a regexp that should match a construct in the buffer that is
132 to be displayed in the menu; i.e., function or variable definitions,
133 etc. It contains a substring which is the name to appear in the
134 menu. See the info section on Regexps for more information.
136 INDEX points to the substring in REGEXP that contains the name (of the
137 function, variable or type) that is to appear in the menu.
139 For emacs-lisp-mode for example PATTERN would look like:
141 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
142 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
143 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2))
145 The variable is buffer-local.")
148 (make-variable-buffer-local 'imenu-generic-expression
)
152 (defvar imenu-create-index-function
'imenu-default-create-index-function
153 "The function to use for creating a buffer index.
155 It should be a function that takes no arguments and returns an index
156 of the current buffer as an alist. The elements in the alist look
157 like: (INDEX-NAME . INDEX-POSITION). You may also nest index list like
158 \(INDEX-NAME . INDEX-ALIST).
160 This function is called within a `save-excursion'.
162 The variable is buffer-local.")
163 (make-variable-buffer-local 'imenu-create-index-function
)
165 (defvar imenu-prev-index-position-function
'beginning-of-defun
166 "Function for finding the next index position.
168 If `imenu-create-index-function' is set to
169 `imenu-default-create-index-function', then you must set this variable
170 to a function that will find the next index, looking backwards in the
173 The function should leave point at the place to be connected to the
174 index and it should return nil when it doesn't find another index.")
175 (make-variable-buffer-local 'imenu-prev-index-position-function
)
177 (defvar imenu-extract-index-name-function nil
178 "Function for extracting the index name.
180 This function is called after the function pointed out by
181 `imenu-prev-index-position-function'.")
182 (make-variable-buffer-local 'imenu-extract-index-name-function
)
185 ;;; Macro to display a progress message.
186 ;;; RELPOS is the relative position to display.
187 ;;; If RELPOS is nil, then the relative position in the buffer
189 ;;; PREVPOS is the variable in which we store the last position displayed.
190 (defmacro imenu-progress-message
(prevpos &optional relpos reverse
)
192 imenu-scanning-message
193 (let ((pos (, (if relpos
195 (` (imenu--relative-position (, reverse
)))))))
197 (` (> pos
(+ 5 (, prevpos
))))))
199 (message imenu-scanning-message pos
)
200 (setq (, prevpos
) pos
)))))))
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ;;;; Some examples of functions utilizing the framework of this
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;; Return the current/previous sexp and the location of the sexp (its
211 ;; beginning) without moving the point.
212 (defun imenu-example--name-and-position ()
216 (end (progn (forward-sexp) (point)))
217 (marker (make-marker)))
218 (set-marker marker beg
)
219 (cons (buffer-substring beg end
)
226 (defun imenu-example--lisp-extract-index-name ()
227 ;; Example of a candidate for `imenu-extract-index-name-function'.
228 ;; This will generate a flat index of definitions in a lisp file.
230 (and (looking-at "(def")
236 (end (progn (forward-sexp -
1) (point))))
237 (buffer-substring beg end
)))
240 (defun imenu-example--create-lisp-index ()
241 ;; Example of a candidate for `imenu-create-index-function'.
242 ;; It will generate a nested index of definitions.
243 (let ((index-alist '())
244 (index-var-alist '())
245 (index-type-alist '())
246 (index-unknown-alist '())
248 (goto-char (point-max))
249 (imenu-progress-message prev-pos
0)
250 ;; Search for the function
251 (while (beginning-of-defun)
252 (imenu-progress-message prev-pos nil t
)
254 (and (looking-at "(def")
258 ((looking-at "def\\(var\\|const\\)")
260 (push (imenu-example--name-and-position)
262 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
264 (push (imenu-example--name-and-position)
266 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
268 (if (= (char-after (1- (point))) ?\
))
273 (push (imenu-example--name-and-position)
277 (push (imenu-example--name-and-position)
278 index-unknown-alist
)))))))
279 (imenu-progress-message prev-pos
100)
281 (push (cons "Variables" index-var-alist
)
283 (and index-type-alist
284 (push (cons "Types" index-type-alist
)
286 (and index-unknown-alist
287 (push (cons "Syntax-unknown" index-unknown-alist
)
291 ;; Regular expression to find C functions
292 (defvar imenu-example--function-name-regexp-c
294 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
295 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
296 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
297 "\\([*&]+[ \t]*\\)?" ; pointer
298 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
301 (defun imenu-example--create-c-index (&optional regexp
)
302 (let ((index-alist '())
304 (goto-char (point-min))
305 (imenu-progress-message prev-pos
0)
306 ;; Search for the function
308 (while (re-search-forward
309 (or regexp imenu-example--function-name-regexp-c
)
311 (imenu-progress-message prev-pos
)
314 (goto-char (scan-sexps (point) 1))
315 (setq char
(following-char)))
316 ;; Skip this function name if it is a prototype declaration.
317 (if (not (eq char ?\
;))
318 (push (imenu-example--name-and-position) index-alist
))))
319 (imenu-progress-message prev-pos
100)
320 (nreverse index-alist
)))
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;;; Internal variables
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;; The item to use in the index for rescanning the buffer.
330 (defconst imenu--rescan-item
'("*Rescan*" . -
99))
332 ;; The latest buffer index.
334 (defvar imenu--index-alist nil
)
335 (make-variable-buffer-local 'imenu--index-alist
)
337 ;; The latest buffer index used to update the menu bar menu.
338 (defvar imenu--last-menubar-index-alist nil
)
339 (make-variable-buffer-local 'imenu--last-menubar-index-alist
)
341 ;; History list for 'jump-to-function-in-buffer'.
342 ;; Making this buffer local caused it not to work!
343 (defvar imenu--history-list nil
)
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; Internal support functions
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;;; Sorts the items depending on their index name.
354 ;;; An item look like (NAME . POSITION).
356 (defun imenu--sort-by-name (item1 item2
)
357 (string-lessp (car item1
) (car item2
)))
359 (defun imenu--relative-position (&optional reverse
)
360 ;; Support function to calculate relative position in buffer
361 ;; Beginning of buffer is 0 and end of buffer is 100
362 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
364 (total (buffer-size)))
365 (and reverse
(setq pos
(- total pos
)))
367 ;; Avoid overflow from multiplying by 100!
368 (/ (1- pos
) (max (/ total
100) 1))
369 (/ (* 100 (1- pos
)) (max total
1)))))
371 ;; Split LIST into sublists of max length N.
372 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
373 (defun imenu--split (list n
)
379 (push (pop remain
) sublist
)
382 ;; We have finished a sublist
383 (progn (push (nreverse sublist
) result
)
385 (setq sublist
'()))))
386 ;; There might be a sublist (if the length of LIST mod n is != 0)
387 ;; that has to be added to the result list.
389 (push (nreverse sublist
) result
))
392 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
393 ;;; In any case, add TITLE to the front of the alist.
394 (defun imenu--split-menu (menulist title
)
395 (if (> (length menulist
) imenu-max-items
)
401 (cons (format "(%s-%d)" title
(setq count
(1+ count
)))
403 (imenu--split menulist imenu-max-items
))))
404 (cons title menulist
)))
406 ;;; Split up each long alist that are nested within ALIST
407 ;;; into nested alists.
408 (defun imenu--split-submenus (alist)
409 (mapcar (function (lambda (elt)
413 (imenu--split-menu (cdr elt
) (car elt
))
418 ;;; Find all items in this buffer that should be in the index.
419 ;;; Returns an alist on the form
420 ;;; ((NAME . POSITION) (NAME . POSITION) ...)
423 (defun imenu--make-index-alist (&optional noerror
)
424 ;; Create a list for this buffer only when needed.
425 (or (and imenu--index-alist
426 (or (not imenu-auto-rescan
)
427 (and imenu-auto-rescan
428 (> (buffer-size) imenu-auto-rescan-maxout
))))
430 (setq imenu--index-alist
432 (funcall imenu-create-index-function
))))
433 (or imenu--index-alist noerror
434 (error "No items suitable for an index found in this buffer"))
435 (or imenu--index-alist
436 (setq imenu--index-alist
(list nil
)))
437 ;; Add a rescan option to the index.
438 (cons imenu--rescan-item imenu--index-alist
))
440 ;;; Find all markers in alist and makes
441 ;;; them point nowhere.
443 (defun imenu--cleanup (&optional alist
)
444 ;; Sets the markers in imenu--index-alist
446 ;; if alist is provided use that list.
448 (setq alist imenu--index-alist
))
454 ((markerp (cdr item
))
455 (set-marker (cdr item
) nil
))
457 (imenu--cleanup (cdr item
))))))
461 (defun imenu--create-keymap-2 (alist counter
&optional commands
)
468 (append (list (setq counter
(1+ counter
))
469 (car item
) 'keymap
(car item
))
470 (imenu--create-keymap-2 (cdr item
) (+ counter
10) commands
)))
472 (let ((end (if commands
`(lambda () (interactive)
473 (imenu--menubar-select ',item
))
476 (cons (car item
) end
))))
480 ;; If COMMANDS is non-nil, make a real keymap
481 ;; with a real command used as the definition.
482 ;; If it is nil, make something suitable for x-popup-menu.
483 (defun imenu--create-keymap-1 (title alist
&optional commands
)
484 (append (list 'keymap title
) (imenu--create-keymap-2 alist
0 commands
)))
487 (defun imenu--in-alist (str alist
)
488 "Check whether the string STR is contained in multi-level ALIST."
489 (let (elt head tail res
)
492 (setq elt
(car alist
)
496 ;; A nested ALIST element looks like
497 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
498 ;; while a bottom-level element looks like
499 ;; (INDEX-NAME . INDEX-POSITION)
500 ;; We are only interested in the bottom-level elements, so we need to
501 ;; recurse if TAIL is a list.
503 (if (setq res
(imenu--in-alist str tail
))
506 (setq alist nil res elt
))))
509 (defun imenu-default-create-index-function ()
510 "*Wrapper for index searching functions.
512 Moves point to end of buffer and then repeatedly calls
513 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
514 Their results are gathered into an index alist."
515 ;; These should really be done by setting imenu-create-index-function
516 ;; in these major modes. But save that change for later.
517 (cond ((and (fboundp imenu-prev-index-position-function
)
518 (fboundp imenu-extract-index-name-function
))
519 (let ((index-alist '())
521 (goto-char (point-max))
522 (imenu-progress-message prev-pos
0 t
)
523 ;; Search for the function
524 (while (funcall imenu-prev-index-position-function
)
525 (imenu-progress-message prev-pos nil t
)
527 (setq name
(funcall imenu-extract-index-name-function
)))
529 (push (cons name
(point)) index-alist
)))
530 (imenu-progress-message prev-pos
100 t
)
532 ;; Use generic expression if possible.
533 ((and imenu-generic-expression
)
534 (imenu--generic-function imenu-generic-expression
))
536 (error "The mode \"%s\" does not take full advantage of imenu.el yet."
539 (defun imenu--replace-spaces (name replacement
)
540 ;; Replace all spaces in NAME with REPLACEMENT.
541 ;; That second argument should be a string.
545 (if (char-equal ch ?\
)
547 (char-to-string ch
))))
551 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix
)
552 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
553 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
554 ;; name and a space concatenated to the names of the children.
555 ;; Third argument PREFIX is for internal use only.
559 (let* ((name (car item
))
561 (new-prefix (and concat-names
563 (concat prefix imenu-level-separator name
)
566 ((or (markerp pos
) (numberp pos
))
567 (list (cons new-prefix pos
)))
569 (imenu--flatten-index-alist pos new-prefix
))))))
573 ;;; Generic index gathering function.
576 (defun imenu--generic-function (patterns)
577 ;; Built on some ideas that Erik Naggum <erik@naggum.no> once posted
579 "Return an index of the current buffer as an alist.
581 PATTERN is an alist with elements that look like this: (MENU-TITLE
584 MENU-TITLE is a string used as the title for the submenu or nil if the
585 entries are not nested.
587 REGEXP is a regexp that should match a construct in the buffer that is
588 to be displayed in the menu; i.e., function or variable definitions,
589 etc. It contains a substring which is the name to appear in the
590 menu. See the info section on Regexps for more information.
592 INDEX points to the substring in REGEXP that contains the name (of the
593 function, variable or type) that is to appear in the menu.
595 For emacs-lisp-mode for example PATTERN would look like:
597 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
598 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
599 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2))'
601 Returns an index of the current buffer as an alist. The elements in
602 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
603 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
606 \(imenu--generic-function PATTERN\)."
608 (let ((index-alist (list 'dummy
))
613 (function (lambda (pattern) (identity (cadr pattern
))))
614 patterns
"\\)\\|\\(")
618 (goto-char (point-max))
619 (imenu-progress-message prev-pos
0 t
)
621 (while (re-search-backward global-regexp nil t
)
622 (imenu-progress-message prev-pos nil t
)
625 (goto-char (match-beginning 0))
629 (let ((menu-title (car pat
))
632 (if (and (not found
) ; Only allow one entry;
634 (let ((beg (match-beginning index
))
635 (end (match-end index
)))
638 (cons (buffer-substring-no-properties beg end
) beg
)
640 (or (assoc menu-title index-alist
)
642 (cons menu-title
'())
643 index-alist
))))))))))
645 (imenu-progress-message prev-pos
100 t
)
646 (let ((main-element (assq nil index-alist
)))
647 (nconc (delq main-element
(delq 'dummy index-alist
)) main-element
))))
649 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
651 ;;; The main functions for this package!
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
655 (defun imenu--completion-buffer (index-alist &optional prompt
)
656 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
658 Returns t for rescan and otherwise a position number."
659 ;; Create a list for this buffer only when needed.
661 (prepared-index-alist
665 (cons (imenu--replace-spaces (car item
) imenu-space-replacement
)
668 (if (eq imenu-always-use-completion-buffer-p
'never
)
669 (setq name
(completing-read (or prompt
"Index item: ")
671 nil t nil
'imenu--history-list
))
672 (save-window-excursion
673 ;; Display the completion buffer
674 (with-output-to-temp-buffer "*Completions*"
675 (display-completion-list
676 (all-completions "" prepared-index-alist
)))
677 (let ((minibuffer-setup-hook
679 (let ((buffer (current-buffer)))
681 (set-buffer "*Completions*")
682 (setq completion-reference-buffer buffer
)))))))
683 ;; Make a completion question
684 (setq name
(completing-read (or prompt
"Index item: ")
686 nil t nil
'imenu--history-list
)))))
687 (cond ((not (stringp name
))
689 ((string= name
(car imenu--rescan-item
))
692 (setq choice
(assoc name prepared-index-alist
))
693 (if (listp (cdr choice
))
694 (imenu--completion-buffer (cdr choice
) prompt
)
697 (defun imenu--mouse-menu (index-alist event
&optional title
)
698 "Let the user select from a buffer index from a mouse menu.
700 INDEX-ALIST is the buffer index and EVENT is a mouse event.
702 Returns t for rescan and otherwise a position number."
703 (setq index-alist
(imenu--split-submenus index-alist
))
704 (let* ((menu (imenu--split-menu
705 (if imenu-sort-function
708 (oldlist index-alist
))
709 ;; Copy list method from the cl package `copy-list'
710 (while (consp oldlist
) (push (pop oldlist
) res
))
711 (prog1 (nreverse res
) (setcdr res oldlist
)))
714 (or title
(buffer-name))))
716 (setq menu
(imenu--create-keymap-1 (car menu
)
717 (if (< 1 (length (cdr menu
)))
720 (setq position
(x-popup-menu event menu
))
721 (cond ((and (listp position
)
722 (numberp (car position
))
723 (stringp (nth (1- (length position
)) position
)))
724 (setq position
(nth (1- (length position
)) position
)))
725 ((and (stringp (car position
))
726 (null (cdr position
)))
727 (setq position
(car position
))))
728 (cond ((eq position nil
)
731 (imenu--mouse-menu position event
733 (concat title imenu-level-separator
734 (car (rassq position index-alist
)))
735 (car (rassq position index-alist
)))))
737 (or (string= position
(car imenu--rescan-item
))
738 (imenu--in-alist position index-alist
)))
739 ((or (= position
(cdr imenu--rescan-item
))
740 (and (stringp position
)
741 (string= position
(car imenu--rescan-item
))))
744 (rassq position index-alist
)))))
746 (defun imenu-choose-buffer-index (&optional prompt alist
)
747 "Let the user select from a buffer index and return the chosen index.
749 If the user originally activated this function with the mouse, a mouse
750 menu is used. Otherwise a completion buffer is used and the user is
751 prompted with PROMPT.
753 If you call this function with index alist ALIST, then it lets the user
756 With no index alist ALIST, it calls `imenu--make-index-alist' to
757 create the index alist.
759 If `imenu-always-use-completion-buffer-p' is non-nil, then the
760 completion buffer is always used, no matter if the mouse was used or
763 The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
765 (mouse-triggered (listp last-nonmenu-event
))
767 ;; If selected by mouse, see to that the window where the mouse is
768 ;; really is selected.
770 (not (equal last-nonmenu-event
'(menu-bar)))
771 (let ((window (posn-window (event-start last-nonmenu-event
))))
772 (or (framep window
) (null window
) (select-window window
))))
773 ;; Create a list for this buffer only when needed.
775 (setq index-alist
(if alist alist
(imenu--make-index-alist)))
777 (if (and mouse-triggered
778 (not imenu-always-use-completion-buffer-p
))
779 (imenu--mouse-menu index-alist last-nonmenu-event
)
780 (imenu--completion-buffer index-alist prompt
)))
783 (setq imenu--index-alist nil
)))
787 (defun imenu-add-to-menubar (name)
788 "Adds an `imenu' entry to the menu bar for the current buffer.
789 NAME is a string used to name the menu bar item.
790 See the command `imenu' for more information."
791 (interactive "sImenu menu item name: ")
792 (let ((newmap (make-sparse-keymap))
793 (menu-bar (lookup-key (current-local-map) [menu-bar
])))
794 (define-key newmap
[menu-bar
]
795 (append (make-sparse-keymap) menu-bar
))
796 (define-key newmap
[menu-bar index
]
797 (cons name
(nconc (make-sparse-keymap "Imenu")
798 (make-sparse-keymap))))
799 (use-local-map (append newmap
(current-local-map))))
800 (add-hook 'menu-bar-update-hook
'imenu-update-menubar
))
802 (defvar imenu-buffer-menubar nil
)
804 (defun imenu-update-menubar ()
805 (and (current-local-map)
806 (keymapp (lookup-key (current-local-map) [menu-bar index
]))
807 (let ((index-alist (imenu--make-index-alist t
)))
808 ;; Don't bother updating if the index-alist has not changed
809 ;; since the last time we did it.
810 (or (equal index-alist imenu--last-menubar-index-alist
)
811 (let (menu menu1 old
)
812 (setq imenu--last-menubar-index-alist index-alist
)
813 (setq index-alist
(imenu--split-submenus index-alist
))
814 (setq menu
(imenu--split-menu
815 (if imenu-sort-function
818 (oldlist index-alist
))
819 ;; Copy list method from the cl package `copy-list'
820 (while (consp oldlist
) (push (pop oldlist
) res
))
821 (prog1 (nreverse res
) (setcdr res oldlist
)))
825 (setq menu1
(imenu--create-keymap-1 (car menu
)
826 (if (< 1 (length (cdr menu
)))
828 (cdr (car (cdr menu
))))
830 (setq old
(lookup-key (current-local-map) [menu-bar index
]))
831 (setcdr old
(cdr menu1
)))))))
833 (defun imenu--menubar-select (item)
834 "Use Imenu to select the function or variable named in this menu item."
835 (if (equal item
'("*Rescan*" . -
99))
838 (setq imenu--index-alist nil
)
839 (imenu-update-menubar))
843 (defun imenu (index-item)
844 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
845 See `imenu-choose-buffer-index' for more information."
847 (list (save-restriction
849 (imenu-choose-buffer-index))))
850 ;; Convert a string to an alist element.
851 (if (stringp index-item
)
852 (setq index-item
(assoc index-item
(imenu--make-index-alist))))
857 ((markerp (cdr index-item
))
858 (if (or ( > (marker-position (cdr index-item
)) (point-min))
859 ( < (marker-position (cdr index-item
)) (point-max)))
860 ;; widen if outside narrowing
862 (goto-char (marker-position (cdr index-item
))))
864 (if (or ( > (cdr index-item
) (point-min))
865 ( < (cdr index-item
) (point-max)))
866 ;; widen if outside narrowing
868 (goto-char (cdr index-item
)))))))
872 ;;; imenu.el ends here