Fix typo.
[emacs.git] / lisp / imenu.el
blob6859c0c74c7759b31eb0792216edf8fee89559cd
1 ;;; imenu.el --- framework for mode-specific buffer indexes
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
7 ;; Lars Lindberg <lli@sypro.cap.se>
8 ;; Maintainer: FSF
9 ;; Created: 8 Feb 1994
10 ;; Keywords: tools convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;;; Commentary:
31 ;; Purpose of this package:
32 ;; To present a framework for mode-specific buffer indexes.
33 ;; A buffer index is an alist of names and buffer positions.
34 ;; For instance all functions in a C-file and their positions.
36 ;; It is documented in the Emacs Lisp manual.
38 ;; How it works:
40 ;; A mode-specific function is called to generate the index. It is
41 ;; then presented to the user, who can choose from this index.
43 ;; The package comes with a set of example functions for how to
44 ;; utilize this package.
46 ;; There are *examples* for index gathering functions/regular
47 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
48 ;; customize for other modes. A function for jumping to the chosen
49 ;; index position is also supplied.
51 ;;; History:
52 ;; Thanks go to
53 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
54 ;; [dean] - Dean Andrews ada@unison.com
55 ;; [alon] - Alon Albert al@mercury.co.il
56 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
57 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
58 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
59 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
60 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
61 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
63 ;;; Code:
65 (eval-when-compile (require 'cl))
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 ;;;
69 ;;; Customizable variables
70 ;;;
71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 (defgroup imenu nil
74 "Mode-specific buffer indexes."
75 :group 'matching
76 :group 'frames
77 :group 'convenience
78 :link '(custom-manual "(elisp)Imenu"))
80 (defcustom imenu-use-markers t
81 "*Non-nil means use markers instead of integers for Imenu buffer positions.
83 Setting this to nil makes Imenu work a little faster but editing the
84 buffer will make the generated index positions wrong.
86 This might not yet be honored by all index-building functions."
87 :type 'boolean
88 :group 'imenu)
91 (defcustom imenu-max-item-length 60
92 "*If a number, truncate Imenu entries to that length."
93 :type '(choice integer
94 (const :tag "Unlimited"))
95 :group 'imenu)
97 (defcustom imenu-auto-rescan nil
98 "*Non-nil means Imenu should always rescan the buffers."
99 :type 'boolean
100 :group 'imenu)
102 (defcustom imenu-auto-rescan-maxout 60000
103 "*Imenu auto-rescan is disabled in buffers larger than this size (in bytes).
104 This variable is buffer-local."
105 :type 'integer
106 :group 'imenu)
108 (defvar imenu-always-use-completion-buffer-p nil)
109 (make-obsolete-variable 'imenu-always-use-completion-buffer-p
110 'imenu-use-popup-menu "21.4")
112 (defcustom imenu-use-popup-menu
113 (if imenu-always-use-completion-buffer-p
114 (not (eq imenu-always-use-completion-buffer-p 'never))
115 'on-mouse)
116 "Use a popup menu rather than a minibuffer prompt.
117 If nil, always use a minibuffer prompt.
118 If t, always use a popup menu,
119 If `on-mouse' use a popup menu when `imenu' was invoked with the mouse."
120 :type '(choice (const :tag "On Mouse" on-mouse)
121 (const :tag "Never" nil)
122 (other :tag "Always" t)))
124 (defcustom imenu-eager-completion-buffer
125 (not (eq imenu-always-use-completion-buffer-p 'never))
126 "If non-nil, eagerly popup the completion buffer."
127 :type 'boolean)
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."
134 :type 'hook
135 :group 'imenu)
137 ;;;###autoload
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
145 in the buffer.
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"))
155 :group 'imenu)
157 (defcustom imenu-max-items 25
158 "*Maximum number of elements in a mouse menu for Imenu."
159 :type 'integer
160 :group 'imenu)
162 (defcustom imenu-scanning-message "Scanning buffer for index (%3d%%)"
163 "*Progress message during the index scanning of the buffer.
164 If non-nil, user gets a message during the scanning of the buffer.
166 Relevant only if the mode-specific function that creates the buffer
167 index use `imenu-progress-message', and not useful if that is fast, in
168 which case you might as well set this to nil."
169 :type '(choice string
170 (const :tag "None" nil))
171 :group 'imenu)
173 (defcustom imenu-space-replacement "."
174 "*The replacement string for spaces in index names.
175 Used when presenting the index in a completion buffer to make the
176 names work as tokens."
177 :type '(choice string (const nil))
178 :group 'imenu)
180 (defcustom imenu-level-separator ":"
181 "*The separator between index names of different levels.
182 Used for making mouse-menu titles and for flattening nested indexes
183 with name concatenation."
184 :type 'string
185 :group 'imenu)
187 ;;;###autoload
188 (defvar imenu-generic-expression nil
189 "The regex pattern to use for creating a buffer index.
191 If non-nil this pattern is passed to `imenu--generic-function'
192 to create a buffer index.
194 The value should be an alist with elements that look like this:
195 (MENU-TITLE REGEXP INDEX)
196 or like this:
197 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
198 with zero or more ARGUMENTS. The former format creates a simple element in
199 the index alist when it matches; the latter creates a special element
200 of the form (NAME POSITION-MARKER FUNCTION ARGUMENTS...)
201 with FUNCTION and ARGUMENTS copied from `imenu-generic-expression'.
203 MENU-TITLE is a string used as the title for the submenu or nil if the
204 entries are not nested.
206 REGEXP is a regexp that should match a construct in the buffer that is
207 to be displayed in the menu; i.e., function or variable definitions,
208 etc. It contains a substring which is the name to appear in the
209 menu. See the info section on Regexps for more information.
211 INDEX points to the substring in REGEXP that contains the name (of the
212 function, variable or type) that is to appear in the menu.
214 The variable `imenu-case-fold-search' determines whether or not the
215 regexp matches are case sensitive, and `imenu-syntax-alist' can be
216 used to alter the syntax table for the search.
218 For example, see the value of `fortran-imenu-generic-expression' used by
219 `fortran-mode' with `imenu-syntax-alist' set locally to give the
220 characters which normally have \"symbol\" syntax \"word\" syntax
221 during matching.")
223 ;;;###autoload
224 (make-variable-buffer-local 'imenu-generic-expression)
226 ;;;; Hooks
228 ;;;###autoload
229 (defvar imenu-create-index-function 'imenu-default-create-index-function
230 "The function to use for creating a buffer index.
232 It should be a function that takes no arguments and returns an index
233 of the current buffer as an alist.
235 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
236 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
237 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
238 The function `imenu--subalist-p' tests an element and returns t
239 if it is a sub-alist.
241 This function is called within a `save-excursion'.")
242 ;;;###autoload
243 (make-variable-buffer-local 'imenu-create-index-function)
245 ;;;###autoload
246 (defvar imenu-prev-index-position-function 'beginning-of-defun
247 "Function for finding the next index position.
249 If `imenu-create-index-function' is set to
250 `imenu-default-create-index-function', then you must set this variable
251 to a function that will find the next index, looking backwards in the
252 file.
254 The function should leave point at the place to be connected to the
255 index and it should return nil when it doesn't find another index.")
256 ;;;###autoload
257 (make-variable-buffer-local 'imenu-prev-index-position-function)
259 ;;;###autoload
260 (defvar imenu-extract-index-name-function nil
261 "Function for extracting the index item name, given a position.
263 This function is called after `imenu-prev-index-position-function'
264 finds a position for an index item, with point at that position.
265 It should return the name for that index item.")
266 ;;;###autoload
267 (make-variable-buffer-local 'imenu-extract-index-name-function)
269 ;;;###autoload
270 (defvar imenu-name-lookup-function nil
271 "Function to compare string with index item.
273 This function will be called with two strings, and should return
274 non-nil if they match.
276 If nil, comparison is done with `string='.
277 Set this to some other function for more advanced comparisons,
278 such as \"begins with\" or \"name matches and number of
279 arguments match\".")
280 ;;;###autoload
281 (make-variable-buffer-local 'imenu-name-lookup-function)
283 ;;;###autoload
284 (defvar imenu-default-goto-function 'imenu-default-goto-function
285 "The default function called when selecting an Imenu item.
286 The function in this variable is called when selecting a normal index-item.")
287 ;;;###autoload
288 (make-variable-buffer-local 'imenu-default-goto-function)
291 (defun imenu--subalist-p (item)
292 (and (consp (cdr item)) (listp (cadr item))
293 (not (eq (car (cadr item)) 'lambda))))
295 ;; Macro to display a progress message.
296 ;; RELPOS is the relative position to display.
297 ;; If RELPOS is nil, then the relative position in the buffer
298 ;; is calculated.
299 ;; PREVPOS is the variable in which we store the last position displayed.
300 (defmacro imenu-progress-message (prevpos &optional relpos reverse)
301 `(and
302 imenu-scanning-message
303 (let ((pos ,(if relpos
304 relpos
305 `(imenu--relative-position ,reverse))))
306 (if ,(if relpos t
307 `(> pos (+ 5 ,prevpos)))
308 (progn
309 (message imenu-scanning-message pos)
310 (setq ,prevpos pos))))))
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 ;;;;
315 ;;;; Some examples of functions utilizing the framework of this
316 ;;;; package.
317 ;;;;
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;; FIXME: This is the only imenu-example-* definition that's actually used,
321 ;; and it seems to only be used by cperl-mode.el. We should just move it to
322 ;; cperl-mode.el and remove the rest.
323 (defun imenu-example--name-and-position ()
324 "Return the current/previous sexp and its (beginning) location.
325 Don't move point."
326 (save-excursion
327 (forward-sexp -1)
328 ;; [ydi] modified for imenu-use-markers
329 (let ((beg (if imenu-use-markers (point-marker) (point)))
330 (end (progn (forward-sexp) (point))))
331 (cons (buffer-substring beg end)
332 beg))))
335 ;;; Lisp
338 (defun imenu-example--lisp-extract-index-name ()
339 ;; Example of a candidate for `imenu-extract-index-name-function'.
340 ;; This will generate a flat index of definitions in a lisp file.
341 (save-match-data
342 (and (looking-at "(def")
343 (condition-case nil
344 (progn
345 (down-list 1)
346 (forward-sexp 2)
347 (let ((beg (point))
348 (end (progn (forward-sexp -1) (point))))
349 (buffer-substring beg end)))
350 (error nil)))))
352 (defun imenu-example--create-lisp-index ()
353 ;; Example of a candidate for `imenu-create-index-function'.
354 ;; It will generate a nested index of definitions.
355 (let ((index-alist '())
356 (index-var-alist '())
357 (index-type-alist '())
358 (index-unknown-alist '())
359 prev-pos)
360 (goto-char (point-max))
361 (imenu-progress-message prev-pos 0)
362 ;; Search for the function
363 (while (beginning-of-defun)
364 (imenu-progress-message prev-pos nil t)
365 (save-match-data
366 (and (looking-at "(def")
367 (save-excursion
368 (down-list 1)
369 (cond
370 ((looking-at "def\\(var\\|const\\)")
371 (forward-sexp 2)
372 (push (imenu-example--name-and-position)
373 index-var-alist))
374 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
375 (forward-sexp 2)
376 (push (imenu-example--name-and-position)
377 index-alist))
378 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
379 (forward-sexp 2)
380 (if (= (char-after (1- (point))) ?\))
381 (progn
382 (forward-sexp -1)
383 (down-list 1)
384 (forward-sexp 1)))
385 (push (imenu-example--name-and-position)
386 index-type-alist))
388 (forward-sexp 2)
389 (push (imenu-example--name-and-position)
390 index-unknown-alist)))))))
391 (imenu-progress-message prev-pos 100)
392 (and index-var-alist
393 (push (cons "Variables" index-var-alist)
394 index-alist))
395 (and index-type-alist
396 (push (cons "Types" index-type-alist)
397 index-alist))
398 (and index-unknown-alist
399 (push (cons "Syntax-unknown" index-unknown-alist)
400 index-alist))
401 index-alist))
403 ;; Regular expression to find C functions
404 (defvar imenu-example--function-name-regexp-c
405 (concat
406 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
407 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
408 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
409 "\\([*&]+[ \t]*\\)?" ; pointer
410 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
413 (defun imenu-example--create-c-index (&optional regexp)
414 (let ((index-alist '())
415 prev-pos char)
416 (goto-char (point-min))
417 (imenu-progress-message prev-pos 0)
418 ;; Search for the function
419 (save-match-data
420 (while (re-search-forward
421 (or regexp imenu-example--function-name-regexp-c)
422 nil t)
423 (imenu-progress-message prev-pos)
424 (backward-up-list 1)
425 (save-excursion
426 (goto-char (scan-sexps (point) 1))
427 (setq char (following-char)))
428 ;; Skip this function name if it is a prototype declaration.
429 (if (not (eq char ?\;))
430 (push (imenu-example--name-and-position) index-alist))))
431 (imenu-progress-message prev-pos 100)
432 (nreverse index-alist)))
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
437 ;;; Internal variables
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;; The item to use in the index for rescanning the buffer.
442 (defconst imenu--rescan-item '("*Rescan*" . -99))
444 ;; The latest buffer index.
445 ;; Buffer local.
446 (defvar imenu--index-alist nil
447 "The buffer index computed for this buffer in Imenu.
448 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
449 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
450 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).")
452 (make-variable-buffer-local 'imenu--index-alist)
454 (defvar imenu--last-menubar-index-alist nil
455 "The latest buffer index used to update the menu bar menu.")
457 (make-variable-buffer-local 'imenu--last-menubar-index-alist)
459 ;; History list for 'jump-to-function-in-buffer'.
460 ;; Making this buffer local caused it not to work!
461 (defvar imenu--history-list nil)
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;;; Internal support functions
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;;; Sort function
471 ;;; Sorts the items depending on their index name.
472 ;;; An item looks like (NAME . POSITION).
474 (defun imenu--sort-by-name (item1 item2)
475 (string-lessp (car item1) (car item2)))
477 (defun imenu--sort-by-position (item1 item2)
478 (< (cdr item1) (cdr item2)))
480 (defun imenu--relative-position (&optional reverse)
481 ;; Support function to calculate relative position in buffer
482 ;; Beginning of buffer is 0 and end of buffer is 100
483 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
484 (let ((pos (point))
485 (total (buffer-size)))
486 (and reverse (setq pos (- total pos)))
487 (if (> total 50000)
488 ;; Avoid overflow from multiplying by 100!
489 (/ (1- pos) (max (/ total 100) 1))
490 (/ (* 100 (1- pos)) (max total 1)))))
492 ;; Split LIST into sublists of max length N.
493 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
494 (defun imenu--split (list n)
495 (let ((remain list)
496 (result '())
497 (sublist '())
498 (i 0))
499 (while remain
500 (push (pop remain) sublist)
501 (incf i)
502 (and (= i n)
503 ;; We have finished a sublist
504 (progn (push (nreverse sublist) result)
505 (setq i 0)
506 (setq sublist '()))))
507 ;; There might be a sublist (if the length of LIST mod n is != 0)
508 ;; that has to be added to the result list.
509 (and sublist
510 (push (nreverse sublist) result))
511 (nreverse result)))
513 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
514 ;;; In any case, add TITLE to the front of the alist.
515 (defun imenu--split-menu (menulist title)
516 (let (keep-at-top tail)
517 (if (memq imenu--rescan-item menulist)
518 (setq keep-at-top (cons imenu--rescan-item nil)
519 menulist (delq imenu--rescan-item menulist)))
520 (setq tail menulist)
521 (dolist (item tail)
522 (when (imenu--subalist-p item)
523 (push item keep-at-top)
524 (setq menulist (delq item menulist))))
525 (if imenu-sort-function
526 (setq menulist (sort menulist imenu-sort-function)))
527 (if (> (length menulist) imenu-max-items)
528 (setq menulist
529 (mapcar
530 (lambda (menu)
531 (cons (format "From: %s" (caar menu)) menu))
532 (imenu--split menulist imenu-max-items))))
533 (cons title
534 (nconc (nreverse keep-at-top) menulist))))
536 ;;; Split up each long alist that are nested within ALIST
537 ;;; into nested alists.
538 (defun imenu--split-submenus (alist)
539 (mapcar (function
540 (lambda (elt)
541 (if (and (consp elt)
542 (stringp (car elt))
543 (listp (cdr elt)))
544 (imenu--split-menu (cdr elt) (car elt))
545 elt)))
546 alist))
548 ;;; Truncate all strings in MENULIST to imenu-max-item-length
549 (defun imenu--truncate-items (menulist)
550 (mapcar (function
551 (lambda (item)
552 (cond
553 ((consp (cdr item))
554 (imenu--truncate-items (cdr item)))
555 ;; truncate if necessary
556 ((and (numberp imenu-max-item-length)
557 (> (length (car item)) imenu-max-item-length))
558 (setcar item (substring (car item) 0 imenu-max-item-length))))))
559 menulist))
562 (defun imenu--make-index-alist (&optional noerror)
563 "Create an index-alist for the definitions in the current buffer.
565 Report an error if the list is empty unless NOERROR is supplied and
566 non-nil.
568 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
569 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
570 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
571 The function `imenu--subalist-p' tests an element and returns t
572 if it is a sub-alist.
574 There is one simple element with negative POSITION; that's intended
575 as a way for the user to ask to recalculate the buffer's index alist."
576 (or (and imenu--index-alist
577 (or (not imenu-auto-rescan)
578 (and imenu-auto-rescan
579 (> (buffer-size) imenu-auto-rescan-maxout))))
580 ;; Get the index; truncate if necessary
581 (progn
582 (setq imenu--index-alist
583 (save-excursion
584 (save-restriction
585 (widen)
586 (funcall imenu-create-index-function))))
587 (imenu--truncate-items imenu--index-alist)))
588 (or imenu--index-alist noerror
589 (error "No items suitable for an index found in this buffer"))
590 (or imenu--index-alist
591 (setq imenu--index-alist (list nil)))
592 ;; Add a rescan option to the index.
593 (cons imenu--rescan-item imenu--index-alist))
595 ;;; Find all markers in alist and makes
596 ;;; them point nowhere.
597 ;;; The top-level call uses nil as the argument;
598 ;;; non-nil arguments are in recursivecalls.
599 (defvar imenu--cleanup-seen)
601 (defun imenu--cleanup (&optional alist)
602 ;; If alist is provided use that list.
603 ;; If not, empty the table of lists already seen
604 ;; and use imenu--index-alist.
605 (if alist
606 (setq imenu--cleanup-seen (cons alist imenu--cleanup-seen))
607 (setq alist imenu--index-alist imenu--cleanup-seen (list alist)))
609 (and alist
610 (mapc
611 (lambda (item)
612 (cond
613 ((markerp (cdr item))
614 (set-marker (cdr item) nil))
615 ;; Don't process one alist twice.
616 ((memq (cdr item) imenu--cleanup-seen))
617 ((imenu--subalist-p item)
618 (imenu--cleanup (cdr item)))))
619 alist)
622 (defun imenu--create-keymap (title alist &optional cmd)
623 (list* 'keymap title
624 (mapcar
625 (lambda (item)
626 (list* (car item) (car item)
627 (cond
628 ((imenu--subalist-p item)
629 (imenu--create-keymap (car item) (cdr item) cmd))
631 `(lambda () (interactive)
632 ,(if cmd `(,cmd ',item) (list 'quote item)))))))
633 alist)))
635 (defun imenu--in-alist (str alist)
636 "Check whether the string STR is contained in multi-level ALIST."
637 (let (elt head tail res)
638 (setq res nil)
639 (while alist
640 (setq elt (car alist)
641 tail (cdr elt)
642 alist (cdr alist)
643 head (car elt))
644 ;; A nested ALIST element looks like
645 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
646 ;; while a bottom-level element looks like
647 ;; (INDEX-NAME . INDEX-POSITION)
648 ;; We are only interested in the bottom-level elements, so we need to
649 ;; recurse if TAIL is a list.
650 (cond ((listp tail)
651 (if (setq res (imenu--in-alist str tail))
652 (setq alist nil)))
653 ((if imenu-name-lookup-function
654 (funcall imenu-name-lookup-function str head)
655 (string= str head))
656 (setq alist nil res elt))))
657 res))
659 (defvar imenu-syntax-alist nil
660 "Alist of syntax table modifiers to use while in `imenu--generic-function'.
662 The car of the assocs may be either a character or a string and the
663 cdr is a syntax description appropriate for `modify-syntax-entry'. For
664 a string, all the characters in the string get the specified syntax.
666 This is typically used to give word syntax to characters which
667 normally have symbol syntax to simplify `imenu-expression'
668 and speed-up matching.")
669 ;;;###autoload
670 (make-variable-buffer-local 'imenu-syntax-alist)
672 (defun imenu-default-create-index-function ()
673 "*Wrapper for index searching functions.
675 Moves point to end of buffer and then repeatedly calls
676 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
677 Their results are gathered into an index alist."
678 ;; These should really be done by setting imenu-create-index-function
679 ;; in these major modes. But save that change for later.
680 (cond ((and imenu-prev-index-position-function
681 imenu-extract-index-name-function)
682 (let ((index-alist '())
683 prev-pos name)
684 (goto-char (point-max))
685 (imenu-progress-message prev-pos 0 t)
686 ;; Search for the function
687 (while (funcall imenu-prev-index-position-function)
688 (imenu-progress-message prev-pos nil t)
689 (save-excursion
690 (setq name (funcall imenu-extract-index-name-function)))
691 (and (stringp name)
692 ;; [ydi] updated for imenu-use-markers
693 (push (cons name (if imenu-use-markers (point-marker) (point)))
694 index-alist)))
695 (imenu-progress-message prev-pos 100 t)
696 index-alist))
697 ;; Use generic expression if possible.
698 ((and imenu-generic-expression)
699 (imenu--generic-function imenu-generic-expression))
701 (error "This buffer cannot use `imenu-default-create-index-function'"))))
703 ;; Not used and would require cl at run time
704 ;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
705 ;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
706 ;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
707 ;; ;; name and a space concatenated to the names of the children.
708 ;; ;; Third argument PREFIX is for internal use only.
709 ;; (mapcan
710 ;; (lambda (item)
711 ;; (let* ((name (car item))
712 ;; (pos (cdr item))
713 ;; (new-prefix (and concat-names
714 ;; (if prefix
715 ;; (concat prefix imenu-level-separator name)
716 ;; name))))
717 ;; (cond
718 ;; ((or (markerp pos) (numberp pos))
719 ;; (list (cons new-prefix pos)))
720 ;; (t
721 ;; (imenu--flatten-index-alist pos new-prefix)))))
722 ;; index-alist))
725 ;;; Generic index gathering function.
728 (defvar imenu-case-fold-search t
729 "Defines whether `imenu--generic-function' should fold case when matching.
731 This variable should be set (only) by initialization code
732 for modes which use `imenu--generic-function'. If it is not set, but
733 `font-lock-defaults' is set, then font-lock's setting is used.")
734 ;;;###autoload
735 (make-variable-buffer-local 'imenu-case-fold-search)
737 ;; Originally "Built on some ideas that Erik Naggum <erik@naggum.no>
738 ;; once posted to comp.emacs" but since substantially re-written.
739 (defun imenu--generic-function (patterns)
740 "Return an index of the current buffer as an alist.
742 PATTERNS is an alist with elements that look like this:
743 (MENU-TITLE REGEXP INDEX).
744 or like this:
745 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
746 with zero or more ARGUMENTS.
748 MENU-TITLE is a string used as the title for the submenu or nil if the
749 entries are not nested.
751 REGEXP is a regexp that should match a construct in the buffer that is
752 to be displayed in the menu; i.e., function or variable definitions,
753 etc. It contains a substring which is the name to appear in the
754 menu. See the info section on Regexps for more information.
756 INDEX points to the substring in REGEXP that contains the name (of the
757 function, variable or type) that is to appear in the menu.
759 See `lisp-imenu-generic-expression' for an example of PATTERNS.
761 Returns an index of the current buffer as an alist. The elements in
762 the alist look like:
763 (INDEX-NAME . INDEX-POSITION)
764 or like:
765 (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
766 They may also be nested index alists like:
767 (INDEX-NAME . INDEX-ALIST)
768 depending on PATTERNS."
770 (let ((index-alist (list 'dummy))
771 prev-pos beg
772 (case-fold-search (if (or (local-variable-p 'imenu-case-fold-search)
773 (not (local-variable-p 'font-lock-defaults)))
774 imenu-case-fold-search
775 (nth 2 font-lock-defaults)))
776 (old-table (syntax-table))
777 (table (copy-syntax-table (syntax-table)))
778 (slist imenu-syntax-alist))
779 ;; Modify the syntax table used while matching regexps.
780 (dolist (syn slist)
781 ;; The character(s) to modify may be a single char or a string.
782 (if (numberp (car syn))
783 (modify-syntax-entry (car syn) (cdr syn) table)
784 (mapc (lambda (c)
785 (modify-syntax-entry c (cdr syn) table))
786 (car syn))))
787 (goto-char (point-max))
788 (imenu-progress-message prev-pos 0 t)
789 (unwind-protect ; for syntax table
790 (save-match-data
791 (set-syntax-table table)
792 ;; map over the elements of imenu-generic-expression
793 ;; (typically functions, variables ...)
794 (dolist (pat patterns)
795 (let ((menu-title (car pat))
796 (regexp (nth 1 pat))
797 (index (nth 2 pat))
798 (function (nth 3 pat))
799 (rest (nthcdr 4 pat)))
800 ;; Go backwards for convenience of adding items in order.
801 (goto-char (point-max))
802 (while (re-search-backward regexp nil t)
803 (imenu-progress-message prev-pos nil t)
804 (setq beg (match-beginning index))
805 ;; Add this sort of submenu only when we've found an
806 ;; item for it, avoiding empty, duff menus.
807 (unless (assoc menu-title index-alist)
808 (push (list menu-title) index-alist))
809 (if imenu-use-markers
810 (setq beg (copy-marker beg)))
811 (let ((item
812 (if function
813 (nconc (list (match-string-no-properties index)
814 beg function)
815 rest)
816 (cons (match-string-no-properties index)
817 beg)))
818 ;; This is the desired submenu,
819 ;; starting with its title (or nil).
820 (menu (assoc menu-title index-alist)))
821 ;; Insert the item unless it is already present.
822 (unless (member item (cdr menu))
823 (setcdr menu
824 (cons item (cdr menu))))))))
825 (set-syntax-table old-table)))
826 (imenu-progress-message prev-pos 100 t)
827 ;; Sort each submenu by position.
828 ;; This is in case one submenu gets items from two different regexps.
829 (dolist (item index-alist)
830 (when (listp item)
831 (setcdr item (sort (cdr item) 'imenu--sort-by-position))))
832 (let ((main-element (assq nil index-alist)))
833 (nconc (delq main-element (delq 'dummy index-alist))
834 (cdr main-element)))))
836 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
838 ;;; The main functions for this package!
840 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
842 ;; See also info-lookup-find-item
843 (defun imenu-find-default (guess completions)
844 "Fuzzily find an item based on GUESS inside the alist COMPLETIONS."
845 (catch 'found
846 (let ((case-fold-search t))
847 (if (assoc guess completions) guess
848 (dolist (re (list (concat "\\`" (regexp-quote guess) "\\'")
849 (concat "\\`" (regexp-quote guess))
850 (concat (regexp-quote guess) "\\'")
851 (regexp-quote guess)))
852 (dolist (x completions)
853 (if (string-match re (car x)) (throw 'found (car x)))))))))
855 (defun imenu--completion-buffer (index-alist &optional prompt)
856 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
858 Return one of the entries in index-alist or nil."
859 ;; Create a list for this buffer only when needed.
860 (let ((name (thing-at-point 'symbol))
861 choice
862 (prepared-index-alist
863 (if (not imenu-space-replacement) index-alist
864 (mapcar
865 (lambda (item)
866 (cons (subst-char-in-string ?\ (aref imenu-space-replacement 0)
867 (car item))
868 (cdr item)))
869 index-alist))))
870 (when (stringp name)
871 (setq name (or (imenu-find-default name prepared-index-alist) name)))
872 (cond (prompt)
873 ((and name (imenu--in-alist name prepared-index-alist))
874 (setq prompt (format "Index item (default %s): " name)))
875 (t (setq prompt "Index item: ")))
876 (let ((minibuffer-setup-hook minibuffer-setup-hook))
877 ;; Display the completion buffer.
878 (if (not imenu-eager-completion-buffer)
879 (add-hook 'minibuffer-setup-hook 'minibuffer-completion-help))
880 (setq name (completing-read prompt
881 prepared-index-alist
882 nil t nil 'imenu--history-list name)))
884 (when (stringp name)
885 (setq choice (assoc name prepared-index-alist))
886 (if (imenu--subalist-p choice)
887 (imenu--completion-buffer (cdr choice) prompt)
888 choice))))
890 (defun imenu--mouse-menu (index-alist event &optional title)
891 "Let the user select from a buffer index from a mouse menu.
893 INDEX-ALIST is the buffer index and EVENT is a mouse event.
895 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
896 (setq index-alist (imenu--split-submenus index-alist))
897 (let* ((menu (imenu--split-menu index-alist (or title (buffer-name))))
898 (map (imenu--create-keymap (car menu)
899 (cdr (if (< 1 (length (cdr menu)))
900 menu
901 (car (cdr menu)))))))
902 (popup-menu map event)))
904 (defun imenu-choose-buffer-index (&optional prompt alist)
905 "Let the user select from a buffer index and return the chosen index.
907 If the user originally activated this function with the mouse, a mouse
908 menu is used. Otherwise a completion buffer is used and the user is
909 prompted with PROMPT.
911 If you call this function with index alist ALIST, then it lets the user
912 select from ALIST.
914 With no index alist ALIST, it calls `imenu--make-index-alist' to
915 create the index alist.
917 If `imenu-use-popup-menu' is non-nil, then the
918 completion buffer is always used, no matter if the mouse was used or
919 not.
921 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
922 (let (index-alist
923 (mouse-triggered (listp last-nonmenu-event))
924 (result t))
925 ;; If selected by mouse, see to that the window where the mouse is
926 ;; really is selected.
927 (and mouse-triggered
928 (not (equal last-nonmenu-event '(menu-bar)))
929 (let ((window (posn-window (event-start last-nonmenu-event))))
930 (or (framep window) (null window) (select-window window))))
931 ;; Create a list for this buffer only when needed.
932 (while (eq result t)
933 (setq index-alist (if alist alist (imenu--make-index-alist)))
934 (setq result
935 (if (and imenu-use-popup-menu
936 (or (eq imenu-use-popup-menu t) mouse-triggered))
937 (imenu--mouse-menu index-alist last-nonmenu-event)
938 (imenu--completion-buffer index-alist prompt)))
939 (and (equal result imenu--rescan-item)
940 (imenu--cleanup)
941 (setq result t imenu--index-alist nil)))
942 result))
944 ;;;###autoload
945 (defun imenu-add-to-menubar (name)
946 "Add an `imenu' entry to the menu bar for the current buffer.
947 NAME is a string used to name the menu bar item.
948 See the command `imenu' for more information."
949 (interactive "sImenu menu item name: ")
950 (if (or (and imenu-prev-index-position-function
951 imenu-extract-index-name-function)
952 imenu-generic-expression
953 (not (eq imenu-create-index-function
954 'imenu-default-create-index-function)))
955 (let ((newmap (make-sparse-keymap)))
956 (set-keymap-parent newmap (current-local-map))
957 (setq imenu--last-menubar-index-alist nil)
958 (define-key newmap [menu-bar index]
959 `(menu-item ,name ,(make-sparse-keymap "Imenu")))
960 (use-local-map newmap)
961 (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
962 (error "The mode `%s' does not support Imenu" mode-name)))
964 ;;;###autoload
965 (defun imenu-add-menubar-index ()
966 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
968 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
969 (interactive)
970 (imenu-add-to-menubar "Index"))
972 (defvar imenu-buffer-menubar nil)
974 (defvar imenu-menubar-modified-tick 0
975 "The value of (buffer-modified-tick) as of last call to `imenu-update-menubar'.")
976 (make-variable-buffer-local 'imenu-menubar-modified-tick)
978 (defun imenu-update-menubar ()
979 (when (and (current-local-map)
980 (keymapp (lookup-key (current-local-map) [menu-bar index]))
981 (not (eq (buffer-modified-tick)
982 imenu-menubar-modified-tick)))
983 (setq imenu-menubar-modified-tick (buffer-modified-tick))
984 (let ((index-alist (imenu--make-index-alist t)))
985 ;; Don't bother updating if the index-alist has not changed
986 ;; since the last time we did it.
987 (unless (equal index-alist imenu--last-menubar-index-alist)
988 (let (menu menu1 old)
989 (setq imenu--last-menubar-index-alist index-alist)
990 (setq index-alist (imenu--split-submenus index-alist))
991 (setq menu (imenu--split-menu index-alist
992 (buffer-name)))
993 (setq menu1 (imenu--create-keymap (car menu)
994 (cdr (if (< 1 (length (cdr menu)))
995 menu
996 (car (cdr menu))))
997 'imenu--menubar-select))
998 (setq old (lookup-key (current-local-map) [menu-bar index]))
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)
1004 (progn
1005 (imenu--cleanup)
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)
1012 (imenu item)
1013 nil))
1015 (defun imenu-default-goto-function (name position &optional 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
1024 (widen))
1025 (goto-char position))
1027 ;;;###autoload
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))))
1036 (when index-item
1037 (push-mark)
1038 (let* ((is-special-item (listp (cdr index-item)))
1039 (function
1040 (if is-special-item
1041 (nth 2 index-item) imenu-default-goto-function))
1042 (position (if is-special-item
1043 (cadr index-item) (cdr index-item)))
1044 (rest (if is-special-item (cddr index-item))))
1045 (apply function (car index-item) position rest))
1046 (run-hooks 'imenu-after-jump-hook)))
1048 (dolist (mess
1049 '("^No items suitable for an index found in this buffer$"
1050 "^This buffer cannot use `imenu-default-create-index-function'$"
1051 "^The mode `.*' does not support Imenu$"))
1052 (add-to-list 'debug-ignored-errors mess))
1054 (provide 'imenu)
1056 ;; arch-tag: 98a2f5f5-4b91-4704-b18c-3aacf77d77a7
1057 ;;; imenu.el ends here