Specify missing group (and type, if simple) in defcustom.
[emacs.git] / lisp / imenu.el
blob92e00282ea053463068b1905a86e7e4812d10113
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 "22.1")
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))
123 :group 'imenu)
125 (defcustom imenu-eager-completion-buffer
126 (not (eq imenu-always-use-completion-buffer-p 'never))
127 "If non-nil, eagerly popup the completion buffer."
128 :type 'boolean
129 :group 'imenu
130 :version "22.1")
132 (defcustom imenu-after-jump-hook nil
133 "*Hooks called after jumping to a place in the buffer.
135 Useful things to use here include `reposition-window', `recenter', and
136 \(lambda () (recenter 0)) to show at top of screen."
137 :type 'hook
138 :group 'imenu)
140 ;;;###autoload
141 (defcustom imenu-sort-function nil
142 "*The function to use for sorting the index mouse-menu.
144 Affects only the mouse index menu.
146 Set this to nil if you don't want any sorting (faster).
147 The items in the menu are then presented in the order they were found
148 in the buffer.
150 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
152 The function should take two arguments and return t if the first
153 element should come before the second. The arguments are cons cells;
154 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
155 :type '(choice (const :tag "No sorting" nil)
156 (const :tag "Sort by name" imenu--sort-by-name)
157 (function :tag "Another function"))
158 :group 'imenu)
160 (defcustom imenu-max-items 25
161 "*Maximum number of elements in a mouse menu for Imenu."
162 :type 'integer
163 :group 'imenu)
165 ;; No longer used. KFS 2004-10-27
166 ;; (defcustom imenu-scanning-message "Scanning buffer for index (%3d%%)"
167 ;; "*Progress message during the index scanning of the buffer.
168 ;; If non-nil, user gets a message during the scanning of the buffer.
170 ;; Relevant only if the mode-specific function that creates the buffer
171 ;; index use `imenu-progress-message', and not useful if that is fast, in
172 ;; which case you might as well set this to nil."
173 ;; :type '(choice string
174 ;; (const :tag "None" nil))
175 ;; :group 'imenu)
177 (defcustom imenu-space-replacement "."
178 "*The replacement string for spaces in index names.
179 Used when presenting the index in a completion buffer to make the
180 names work as tokens."
181 :type '(choice string (const nil))
182 :group 'imenu)
184 (defcustom imenu-level-separator ":"
185 "*The separator between index names of different levels.
186 Used for making mouse-menu titles and for flattening nested indexes
187 with name concatenation."
188 :type 'string
189 :group 'imenu)
191 ;;;###autoload
192 (defvar imenu-generic-expression nil
193 "The regex pattern to use for creating a buffer index.
195 If non-nil this pattern is passed to `imenu--generic-function'
196 to create a buffer index.
198 The value should be an alist with elements that look like this:
199 (MENU-TITLE REGEXP INDEX)
200 or like this:
201 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
202 with zero or more ARGUMENTS. The former format creates a simple element in
203 the index alist when it matches; the latter creates a special element
204 of the form (NAME POSITION-MARKER FUNCTION ARGUMENTS...)
205 with FUNCTION and ARGUMENTS copied from `imenu-generic-expression'.
207 MENU-TITLE is a string used as the title for the submenu or nil if the
208 entries are not nested.
210 REGEXP is a regexp that should match a construct in the buffer that is
211 to be displayed in the menu; i.e., function or variable definitions,
212 etc. It contains a substring which is the name to appear in the
213 menu. See the info section on Regexps for more information.
215 INDEX points to the substring in REGEXP that contains the name (of the
216 function, variable or type) that is to appear in the menu.
218 The variable `imenu-case-fold-search' determines whether or not the
219 regexp matches are case sensitive, and `imenu-syntax-alist' can be
220 used to alter the syntax table for the search.
222 For example, see the value of `fortran-imenu-generic-expression' used by
223 `fortran-mode' with `imenu-syntax-alist' set locally to give the
224 characters which normally have \"symbol\" syntax \"word\" syntax
225 during matching.")
227 ;;;###autoload
228 (make-variable-buffer-local 'imenu-generic-expression)
230 ;;;; Hooks
232 ;;;###autoload
233 (defvar imenu-create-index-function 'imenu-default-create-index-function
234 "The function to use for creating a buffer index.
236 It should be a function that takes no arguments and returns an index
237 of the current buffer as an alist.
239 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
240 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
241 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
242 The function `imenu--subalist-p' tests an element and returns t
243 if it is a sub-alist.
245 This function is called within a `save-excursion'.")
246 ;;;###autoload
247 (make-variable-buffer-local 'imenu-create-index-function)
249 ;;;###autoload
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
256 file.
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.")
260 ;;;###autoload
261 (make-variable-buffer-local 'imenu-prev-index-position-function)
263 ;;;###autoload
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.")
270 ;;;###autoload
271 (make-variable-buffer-local 'imenu-extract-index-name-function)
273 ;;;###autoload
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
283 arguments match\".")
284 ;;;###autoload
285 (make-variable-buffer-local 'imenu-name-lookup-function)
287 ;;;###autoload
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.")
291 ;;;###autoload
292 (make-variable-buffer-local 'imenu-default-goto-function)
295 (defun imenu--subalist-p (item)
296 (and (consp (cdr item)) (listp (cadr item))
297 (not (eq (car (cadr item)) 'lambda))))
299 ;; Macro to display a progress message.
300 ;; RELPOS is the relative position to display.
301 ;; If RELPOS is nil, then the relative position in the buffer
302 ;; is calculated.
303 ;; PREVPOS is the variable in which we store the last position displayed.
304 (defmacro imenu-progress-message (prevpos &optional relpos reverse)
306 ;; Made obsolete/empty, as computers are now faster than the eye, and
307 ;; it had problems updating the messages correctly, and could shadow
308 ;; more important messages/prompts in the minibuffer. KFS 2004-10-27.
310 ;; `(and
311 ;; imenu-scanning-message
312 ;; (let ((pos ,(if relpos
313 ;; relpos
314 ;; `(imenu--relative-position ,reverse))))
315 ;; (if ,(if relpos t
316 ;; `(> pos (+ 5 ,prevpos)))
317 ;; (progn
318 ;; (message imenu-scanning-message pos)
319 ;; (setq ,prevpos pos)))))
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;;;;
325 ;;;; Some examples of functions utilizing the framework of this
326 ;;;; package.
327 ;;;;
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; FIXME: This is the only imenu-example-* definition that's actually used,
331 ;; and it seems to only be used by cperl-mode.el. We should just move it to
332 ;; cperl-mode.el and remove the rest.
333 (defun imenu-example--name-and-position ()
334 "Return the current/previous sexp and its (beginning) location.
335 Don't move point."
336 (save-excursion
337 (forward-sexp -1)
338 ;; [ydi] modified for imenu-use-markers
339 (let ((beg (if imenu-use-markers (point-marker) (point)))
340 (end (progn (forward-sexp) (point))))
341 (cons (buffer-substring beg end)
342 beg))))
345 ;;; Lisp
348 (defun imenu-example--lisp-extract-index-name ()
349 ;; Example of a candidate for `imenu-extract-index-name-function'.
350 ;; This will generate a flat index of definitions in a lisp file.
351 (save-match-data
352 (and (looking-at "(def")
353 (condition-case nil
354 (progn
355 (down-list 1)
356 (forward-sexp 2)
357 (let ((beg (point))
358 (end (progn (forward-sexp -1) (point))))
359 (buffer-substring beg end)))
360 (error nil)))))
362 (defun imenu-example--create-lisp-index ()
363 ;; Example of a candidate for `imenu-create-index-function'.
364 ;; It will generate a nested index of definitions.
365 (let ((index-alist '())
366 (index-var-alist '())
367 (index-type-alist '())
368 (index-unknown-alist '())
369 prev-pos)
370 (goto-char (point-max))
371 (imenu-progress-message prev-pos 0)
372 ;; Search for the function
373 (while (beginning-of-defun)
374 (imenu-progress-message prev-pos nil t)
375 (save-match-data
376 (and (looking-at "(def")
377 (save-excursion
378 (down-list 1)
379 (cond
380 ((looking-at "def\\(var\\|const\\)")
381 (forward-sexp 2)
382 (push (imenu-example--name-and-position)
383 index-var-alist))
384 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
385 (forward-sexp 2)
386 (push (imenu-example--name-and-position)
387 index-alist))
388 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
389 (forward-sexp 2)
390 (if (= (char-after (1- (point))) ?\))
391 (progn
392 (forward-sexp -1)
393 (down-list 1)
394 (forward-sexp 1)))
395 (push (imenu-example--name-and-position)
396 index-type-alist))
398 (forward-sexp 2)
399 (push (imenu-example--name-and-position)
400 index-unknown-alist)))))))
401 (imenu-progress-message prev-pos 100)
402 (and index-var-alist
403 (push (cons "Variables" index-var-alist)
404 index-alist))
405 (and index-type-alist
406 (push (cons "Types" index-type-alist)
407 index-alist))
408 (and index-unknown-alist
409 (push (cons "Syntax-unknown" index-unknown-alist)
410 index-alist))
411 index-alist))
413 ;; Regular expression to find C functions
414 (defvar imenu-example--function-name-regexp-c
415 (concat
416 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
417 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
418 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
419 "\\([*&]+[ \t]*\\)?" ; pointer
420 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
423 (defun imenu-example--create-c-index (&optional regexp)
424 (let ((index-alist '())
425 prev-pos char)
426 (goto-char (point-min))
427 (imenu-progress-message prev-pos 0)
428 ;; Search for the function
429 (save-match-data
430 (while (re-search-forward
431 (or regexp imenu-example--function-name-regexp-c)
432 nil t)
433 (imenu-progress-message prev-pos)
434 (backward-up-list 1)
435 (save-excursion
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 (imenu-progress-message prev-pos 100)
442 (nreverse index-alist)))
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;;; Internal variables
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; The item to use in the index for rescanning the buffer.
452 (defconst imenu--rescan-item '("*Rescan*" . -99))
454 ;; The latest buffer index.
455 ;; Buffer local.
456 (defvar imenu--index-alist nil
457 "The buffer index computed for this buffer in Imenu.
458 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
459 Special elements look like (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...).
460 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).")
462 (make-variable-buffer-local 'imenu--index-alist)
464 (defvar imenu--last-menubar-index-alist nil
465 "The latest buffer index used to update the menu bar menu.")
467 (make-variable-buffer-local 'imenu--last-menubar-index-alist)
469 ;; History list for 'jump-to-function-in-buffer'.
470 ;; Making this buffer local caused it not to work!
471 (defvar imenu--history-list nil)
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;;; Internal support functions
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;;; Sort function
481 ;;; Sorts the items depending on their index name.
482 ;;; An item looks like (NAME . POSITION).
484 (defun imenu--sort-by-name (item1 item2)
485 (string-lessp (car item1) (car item2)))
487 (defun imenu--sort-by-position (item1 item2)
488 (< (cdr item1) (cdr item2)))
490 (defun imenu--relative-position (&optional reverse)
491 ;; Support function to calculate relative position in buffer
492 ;; Beginning of buffer is 0 and end of buffer is 100
493 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
494 (let ((pos (point))
495 (total (buffer-size)))
496 (and reverse (setq pos (- total pos)))
497 (if (> total 50000)
498 ;; Avoid overflow from multiplying by 100!
499 (/ (1- pos) (max (/ total 100) 1))
500 (/ (* 100 (1- pos)) (max total 1)))))
502 ;; Split LIST into sublists of max length N.
503 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
504 (defun imenu--split (list n)
505 (let ((remain list)
506 (result '())
507 (sublist '())
508 (i 0))
509 (while remain
510 (push (pop remain) sublist)
511 (incf i)
512 (and (= i n)
513 ;; We have finished a sublist
514 (progn (push (nreverse sublist) result)
515 (setq i 0)
516 (setq sublist '()))))
517 ;; There might be a sublist (if the length of LIST mod n is != 0)
518 ;; that has to be added to the result list.
519 (and sublist
520 (push (nreverse sublist) result))
521 (nreverse result)))
523 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
524 ;;; In any case, add TITLE to the front of the alist.
525 (defun imenu--split-menu (menulist title)
526 (let (keep-at-top tail)
527 (if (memq imenu--rescan-item menulist)
528 (setq keep-at-top (cons imenu--rescan-item nil)
529 menulist (delq imenu--rescan-item menulist)))
530 (setq tail menulist)
531 (dolist (item tail)
532 (when (imenu--subalist-p item)
533 (push item keep-at-top)
534 (setq menulist (delq item menulist))))
535 (if imenu-sort-function
536 (setq menulist (sort (copy-sequence menulist) imenu-sort-function)))
537 (if (> (length menulist) imenu-max-items)
538 (setq menulist
539 (mapcar
540 (lambda (menu)
541 (cons (format "From: %s" (caar menu)) menu))
542 (imenu--split menulist imenu-max-items))))
543 (cons title
544 (nconc (nreverse keep-at-top) menulist))))
546 ;;; Split up each long alist that are nested within ALIST
547 ;;; into nested alists.
548 (defun imenu--split-submenus (alist)
549 (mapcar (function
550 (lambda (elt)
551 (if (and (consp elt)
552 (stringp (car elt))
553 (listp (cdr elt)))
554 (imenu--split-menu (cdr elt) (car elt))
555 elt)))
556 alist))
558 ;;; Truncate all strings in MENULIST to imenu-max-item-length
559 (defun imenu--truncate-items (menulist)
560 (mapcar (function
561 (lambda (item)
562 (cond
563 ((consp (cdr item))
564 (imenu--truncate-items (cdr item)))
565 ;; truncate if necessary
566 ((and (numberp imenu-max-item-length)
567 (> (length (car item)) imenu-max-item-length))
568 (setcar item (substring (car item) 0 imenu-max-item-length))))))
569 menulist))
572 (defun imenu--make-index-alist (&optional noerror)
573 "Create an index-alist for the definitions in the current buffer.
575 Report an error if the list is empty unless NOERROR is supplied and
576 non-nil.
578 Simple elements in the alist look like (INDEX-NAME . INDEX-POSITION).
579 Special elements look like (INDEX-NAME FUNCTION ARGUMENTS...).
580 A nested sub-alist element looks like (INDEX-NAME SUB-ALIST).
581 The function `imenu--subalist-p' tests an element and returns t
582 if it is a sub-alist.
584 There is one simple element with negative POSITION; that's intended
585 as a way for the user to ask to recalculate the buffer's index alist."
586 (or (and imenu--index-alist
587 (or (not imenu-auto-rescan)
588 (and imenu-auto-rescan
589 (> (buffer-size) imenu-auto-rescan-maxout))))
590 ;; Get the index; truncate if necessary
591 (progn
592 (setq imenu--index-alist
593 (save-excursion
594 (save-restriction
595 (widen)
596 (funcall imenu-create-index-function))))
597 (imenu--truncate-items imenu--index-alist)))
598 (or imenu--index-alist noerror
599 (error "No items suitable for an index found in this buffer"))
600 (or imenu--index-alist
601 (setq imenu--index-alist (list nil)))
602 ;; Add a rescan option to the index.
603 (cons imenu--rescan-item imenu--index-alist))
605 ;;; Find all markers in alist and makes
606 ;;; them point nowhere.
607 ;;; The top-level call uses nil as the argument;
608 ;;; non-nil arguments are in recursivecalls.
609 (defvar imenu--cleanup-seen)
611 (defun imenu--cleanup (&optional alist)
612 ;; If alist is provided use that list.
613 ;; If not, empty the table of lists already seen
614 ;; and use imenu--index-alist.
615 (if alist
616 (setq imenu--cleanup-seen (cons alist imenu--cleanup-seen))
617 (setq alist imenu--index-alist imenu--cleanup-seen (list alist)))
619 (and alist
620 (mapc
621 (lambda (item)
622 (cond
623 ((markerp (cdr item))
624 (set-marker (cdr item) nil))
625 ;; Don't process one alist twice.
626 ((memq (cdr item) imenu--cleanup-seen))
627 ((imenu--subalist-p item)
628 (imenu--cleanup (cdr item)))))
629 alist)
632 (defun imenu--create-keymap (title alist &optional cmd)
633 (list* 'keymap title
634 (mapcar
635 (lambda (item)
636 (list* (car item) (car item)
637 (cond
638 ((imenu--subalist-p item)
639 (imenu--create-keymap (car item) (cdr item) cmd))
641 `(lambda () (interactive)
642 ,(if cmd `(,cmd ',item) (list 'quote item)))))))
643 alist)))
645 (defun imenu--in-alist (str alist)
646 "Check whether the string STR is contained in multi-level ALIST."
647 (let (elt head tail res)
648 (setq res nil)
649 (while alist
650 (setq elt (car alist)
651 tail (cdr elt)
652 alist (cdr alist)
653 head (car elt))
654 ;; A nested ALIST element looks like
655 ;; (INDEX-NAME (INDEX-NAME . INDEX-POSITION) ...)
656 ;; while a bottom-level element looks like
657 ;; (INDEX-NAME . INDEX-POSITION)
658 ;; We are only interested in the bottom-level elements, so we need to
659 ;; recurse if TAIL is a list.
660 (cond ((listp tail)
661 (if (setq res (imenu--in-alist str tail))
662 (setq alist nil)))
663 ((if imenu-name-lookup-function
664 (funcall imenu-name-lookup-function str head)
665 (string= str head))
666 (setq alist nil res elt))))
667 res))
669 (defvar imenu-syntax-alist nil
670 "Alist of syntax table modifiers to use while in `imenu--generic-function'.
672 The car of the assocs may be either a character or a string and the
673 cdr is a syntax description appropriate for `modify-syntax-entry'. For
674 a string, all the characters in the string get the specified syntax.
676 This is typically used to give word syntax to characters which
677 normally have symbol syntax to simplify `imenu-expression'
678 and speed-up matching.")
679 ;;;###autoload
680 (make-variable-buffer-local 'imenu-syntax-alist)
682 (defun imenu-default-create-index-function ()
683 "*Wrapper for index searching functions.
685 Moves point to end of buffer and then repeatedly calls
686 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
687 Their results are gathered into an index alist."
688 ;; These should really be done by setting imenu-create-index-function
689 ;; in these major modes. But save that change for later.
690 (cond ((and imenu-prev-index-position-function
691 imenu-extract-index-name-function)
692 (let ((index-alist '())
693 prev-pos name)
694 (goto-char (point-max))
695 (imenu-progress-message prev-pos 0 t)
696 ;; Search for the function
697 (while (funcall imenu-prev-index-position-function)
698 (imenu-progress-message prev-pos nil t)
699 (save-excursion
700 (setq name (funcall imenu-extract-index-name-function)))
701 (and (stringp name)
702 ;; [ydi] updated for imenu-use-markers
703 (push (cons name (if imenu-use-markers (point-marker) (point)))
704 index-alist)))
705 (imenu-progress-message prev-pos 100 t)
706 index-alist))
707 ;; Use generic expression if possible.
708 ((and imenu-generic-expression)
709 (imenu--generic-function imenu-generic-expression))
711 (error "This buffer cannot use `imenu-default-create-index-function'"))))
713 ;; Not used and would require cl at run time
714 ;; (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
715 ;; ;; Takes a nested INDEX-ALIST and returns a flat index alist.
716 ;; ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
717 ;; ;; name and a space concatenated to the names of the children.
718 ;; ;; Third argument PREFIX is for internal use only.
719 ;; (mapcan
720 ;; (lambda (item)
721 ;; (let* ((name (car item))
722 ;; (pos (cdr item))
723 ;; (new-prefix (and concat-names
724 ;; (if prefix
725 ;; (concat prefix imenu-level-separator name)
726 ;; name))))
727 ;; (cond
728 ;; ((or (markerp pos) (numberp pos))
729 ;; (list (cons new-prefix pos)))
730 ;; (t
731 ;; (imenu--flatten-index-alist pos new-prefix)))))
732 ;; index-alist))
735 ;;; Generic index gathering function.
738 (defvar imenu-case-fold-search t
739 "Defines whether `imenu--generic-function' should fold case when matching.
741 This variable should be set (only) by initialization code
742 for modes which use `imenu--generic-function'. If it is not set, but
743 `font-lock-defaults' is set, then font-lock's setting is used.")
744 ;;;###autoload
745 (make-variable-buffer-local 'imenu-case-fold-search)
747 ;; This function can be called with quitting disabled,
748 ;; so it needs to be careful never to loop!
749 (defun imenu--generic-function (patterns)
750 "Return an index of the current buffer as an alist.
752 PATTERNS is an alist with elements that look like this:
753 (MENU-TITLE REGEXP INDEX).
754 or like this:
755 (MENU-TITLE REGEXP INDEX FUNCTION ARGUMENTS...)
756 with zero or more ARGUMENTS.
758 MENU-TITLE is a string used as the title for the submenu or nil if the
759 entries are not nested.
761 REGEXP is a regexp that should match a construct in the buffer that is
762 to be displayed in the menu; i.e., function or variable definitions,
763 etc. It contains a substring which is the name to appear in the
764 menu. See the info section on Regexps for more information.
766 INDEX points to the substring in REGEXP that contains the name (of the
767 function, variable or type) that is to appear in the menu.
769 See `lisp-imenu-generic-expression' for an example of PATTERNS.
771 Returns an index of the current buffer as an alist. The elements in
772 the alist look like:
773 (INDEX-NAME . INDEX-POSITION)
774 or like:
775 (INDEX-NAME INDEX-POSITION FUNCTION ARGUMENTS...)
776 They may also be nested index alists like:
777 (INDEX-NAME . INDEX-ALIST)
778 depending on PATTERNS."
780 (let ((index-alist (list 'dummy))
781 prev-pos
782 (case-fold-search (if (or (local-variable-p 'imenu-case-fold-search)
783 (not (local-variable-p 'font-lock-defaults)))
784 imenu-case-fold-search
785 (nth 2 font-lock-defaults)))
786 (old-table (syntax-table))
787 (table (copy-syntax-table (syntax-table)))
788 (slist imenu-syntax-alist))
789 ;; Modify the syntax table used while matching regexps.
790 (dolist (syn slist)
791 ;; The character(s) to modify may be a single char or a string.
792 (if (numberp (car syn))
793 (modify-syntax-entry (car syn) (cdr syn) table)
794 (mapc (lambda (c)
795 (modify-syntax-entry c (cdr syn) table))
796 (car syn))))
797 (goto-char (point-max))
798 (imenu-progress-message prev-pos 0 t)
799 (unwind-protect ; for syntax table
800 (save-match-data
801 (set-syntax-table table)
803 ;; map over the elements of imenu-generic-expression
804 ;; (typically functions, variables ...)
805 (dolist (pat patterns)
806 (let ((menu-title (car pat))
807 (regexp (nth 1 pat))
808 (index (nth 2 pat))
809 (function (nth 3 pat))
810 (rest (nthcdr 4 pat))
811 start beg)
812 ;; Go backwards for convenience of adding items in order.
813 (goto-char (point-max))
814 (while (and (re-search-backward regexp nil t)
815 ;; Exit the loop if we get an empty match,
816 ;; because it means a bad regexp was specified.
817 (not (= (match-beginning 0) (match-end 0))))
818 (setq start (point))
819 ;; Record the start of the line in which the match starts.
820 ;; That's the official position of this definition.
821 (goto-char (match-beginning index))
822 (beginning-of-line)
823 (setq beg (point))
824 (imenu-progress-message prev-pos nil t)
825 ;; Add this sort of submenu only when we've found an
826 ;; item for it, avoiding empty, duff menus.
827 (unless (assoc menu-title index-alist)
828 (push (list menu-title) index-alist))
829 (if imenu-use-markers
830 (setq beg (copy-marker beg)))
831 (let ((item
832 (if function
833 (nconc (list (match-string-no-properties index)
834 beg function)
835 rest)
836 (cons (match-string-no-properties index)
837 beg)))
838 ;; This is the desired submenu,
839 ;; starting with its title (or nil).
840 (menu (assoc menu-title index-alist)))
841 ;; Insert the item unless it is already present.
842 (unless (member item (cdr menu))
843 (setcdr menu
844 (cons item (cdr menu)))))
845 ;; Go to the start of the match, to make sure we
846 ;; keep making progress backwards.
847 (goto-char start))))
848 (set-syntax-table old-table)))
849 (imenu-progress-message prev-pos 100 t)
850 ;; Sort each submenu by position.
851 ;; This is in case one submenu gets items from two different regexps.
852 (dolist (item index-alist)
853 (when (listp item)
854 (setcdr item (sort (cdr item) 'imenu--sort-by-position))))
855 (let ((main-element (assq nil index-alist)))
856 (nconc (delq main-element (delq 'dummy index-alist))
857 (cdr main-element)))))
859 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
861 ;;; The main functions for this package!
863 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
865 ;; See also info-lookup-find-item
866 (defun imenu-find-default (guess completions)
867 "Fuzzily find an item based on GUESS inside the alist COMPLETIONS."
868 (catch 'found
869 (let ((case-fold-search t))
870 (if (assoc guess completions) guess
871 (dolist (re (list (concat "\\`" (regexp-quote guess) "\\'")
872 (concat "\\`" (regexp-quote guess))
873 (concat (regexp-quote guess) "\\'")
874 (regexp-quote guess)))
875 (dolist (x completions)
876 (if (string-match re (car x)) (throw 'found (car x)))))))))
878 (defun imenu--completion-buffer (index-alist &optional prompt)
879 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
881 Return one of the entries in index-alist or nil."
882 ;; Create a list for this buffer only when needed.
883 (let ((name (thing-at-point 'symbol))
884 choice
885 (prepared-index-alist
886 (if (not imenu-space-replacement) index-alist
887 (mapcar
888 (lambda (item)
889 (cons (subst-char-in-string ?\ (aref imenu-space-replacement 0)
890 (car item))
891 (cdr item)))
892 index-alist))))
893 (when (stringp name)
894 (setq name (or (imenu-find-default name prepared-index-alist) name)))
895 (cond (prompt)
896 ((and name (imenu--in-alist name prepared-index-alist))
897 (setq prompt (format "Index item (default %s): " name)))
898 (t (setq prompt "Index item: ")))
899 (let ((minibuffer-setup-hook minibuffer-setup-hook))
900 ;; Display the completion buffer.
901 (if (not imenu-eager-completion-buffer)
902 (add-hook 'minibuffer-setup-hook 'minibuffer-completion-help))
903 (setq name (completing-read prompt
904 prepared-index-alist
905 nil t nil 'imenu--history-list name)))
907 (when (stringp name)
908 (setq choice (assoc name prepared-index-alist))
909 (if (imenu--subalist-p choice)
910 (imenu--completion-buffer (cdr choice) prompt)
911 choice))))
913 (defun imenu--mouse-menu (index-alist event &optional title)
914 "Let the user select from a buffer index from a mouse menu.
916 INDEX-ALIST is the buffer index and EVENT is a mouse event.
918 Returns t for rescan and otherwise an element or subelement of INDEX-ALIST."
919 (setq index-alist (imenu--split-submenus index-alist))
920 (let* ((menu (imenu--split-menu index-alist (or title (buffer-name))))
921 (map (imenu--create-keymap (car menu)
922 (cdr (if (< 1 (length (cdr menu)))
923 menu
924 (car (cdr menu)))))))
925 (popup-menu map event)))
927 (defun imenu-choose-buffer-index (&optional prompt alist)
928 "Let the user select from a buffer index and return the chosen index.
930 If the user originally activated this function with the mouse, a mouse
931 menu is used. Otherwise a completion buffer is used and the user is
932 prompted with PROMPT.
934 If you call this function with index alist ALIST, then it lets the user
935 select from ALIST.
937 With no index alist ALIST, it calls `imenu--make-index-alist' to
938 create the index alist.
940 If `imenu-use-popup-menu' is non-nil, then the
941 completion buffer is always used, no matter if the mouse was used or
942 not.
944 The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
945 (let (index-alist
946 (mouse-triggered (listp last-nonmenu-event))
947 (result t))
948 ;; If selected by mouse, see to that the window where the mouse is
949 ;; really is selected.
950 (and mouse-triggered
951 (not (equal last-nonmenu-event '(menu-bar)))
952 (let ((window (posn-window (event-start last-nonmenu-event))))
953 (or (framep window) (null window) (select-window window))))
954 ;; Create a list for this buffer only when needed.
955 (while (eq result t)
956 (setq index-alist (if alist alist (imenu--make-index-alist)))
957 (setq result
958 (if (and imenu-use-popup-menu
959 (or (eq imenu-use-popup-menu t) mouse-triggered))
960 (imenu--mouse-menu index-alist last-nonmenu-event)
961 (imenu--completion-buffer index-alist prompt)))
962 (and (equal result imenu--rescan-item)
963 (imenu--cleanup)
964 (setq result t imenu--index-alist nil)))
965 result))
967 ;;;###autoload
968 (defun imenu-add-to-menubar (name)
969 "Add an `imenu' entry to the menu bar for the current buffer.
970 NAME is a string used to name the menu bar item.
971 See the command `imenu' for more information."
972 (interactive "sImenu menu item name: ")
973 (if (or (and imenu-prev-index-position-function
974 imenu-extract-index-name-function)
975 imenu-generic-expression
976 (not (eq imenu-create-index-function
977 'imenu-default-create-index-function)))
978 (let ((newmap (make-sparse-keymap)))
979 (set-keymap-parent newmap (current-local-map))
980 (setq imenu--last-menubar-index-alist nil)
981 (define-key newmap [menu-bar index]
982 `(menu-item ,name ,(make-sparse-keymap "Imenu")))
983 (use-local-map newmap)
984 (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
985 (error "The mode `%s' does not support Imenu" mode-name)))
987 ;;;###autoload
988 (defun imenu-add-menubar-index ()
989 "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
991 A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
992 (interactive)
993 (imenu-add-to-menubar "Index"))
995 (defvar imenu-buffer-menubar nil)
997 (defvar imenu-menubar-modified-tick 0
998 "The value of (buffer-modified-tick) as of last call to `imenu-update-menubar'.")
999 (make-variable-buffer-local 'imenu-menubar-modified-tick)
1001 (defun imenu-update-menubar ()
1002 (when (and (current-local-map)
1003 (keymapp (lookup-key (current-local-map) [menu-bar index]))
1004 (not (eq (buffer-modified-tick)
1005 imenu-menubar-modified-tick)))
1006 (setq imenu-menubar-modified-tick (buffer-modified-tick))
1007 (let ((index-alist (imenu--make-index-alist t)))
1008 ;; Don't bother updating if the index-alist has not changed
1009 ;; since the last time we did it.
1010 (unless (equal index-alist imenu--last-menubar-index-alist)
1011 (let (menu menu1 old)
1012 (setq imenu--last-menubar-index-alist index-alist)
1013 (setq index-alist (imenu--split-submenus index-alist))
1014 (setq menu (imenu--split-menu index-alist
1015 (buffer-name)))
1016 (setq menu1 (imenu--create-keymap (car menu)
1017 (cdr (if (< 1 (length (cdr menu)))
1018 menu
1019 (car (cdr menu))))
1020 'imenu--menubar-select))
1021 (setq old (lookup-key (current-local-map) [menu-bar index]))
1022 (setcdr old (cdr menu1)))))))
1024 (defun imenu--menubar-select (item)
1025 "Use Imenu to select the function or variable named in this menu ITEM."
1026 (if (equal item imenu--rescan-item)
1027 (progn
1028 (imenu--cleanup)
1029 ;; Make sure imenu-update-menubar redoes everything.
1030 (setq imenu-menubar-modified-tick -1)
1031 (setq imenu--index-alist nil)
1032 (setq imenu--last-menubar-index-alist nil)
1033 (imenu-update-menubar)
1035 (imenu item)
1036 nil))
1038 (defun imenu-default-goto-function (name position &optional rest)
1039 "Move to the given position.
1041 NAME is ignored. POSITION is where to move. REST is also ignored.
1042 The ignored args just make this function have the same interface as a
1043 function placed in a special index-item."
1044 (if (or (< position (point-min))
1045 (> position (point-max)))
1046 ;; widen if outside narrowing
1047 (widen))
1048 (goto-char position))
1050 ;;;###autoload
1051 (defun imenu (index-item)
1052 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
1053 INDEX-ITEM specifies the position. See `imenu-choose-buffer-index'
1054 for more information."
1055 (interactive (list (imenu-choose-buffer-index)))
1056 ;; Convert a string to an alist element.
1057 (if (stringp index-item)
1058 (setq index-item (assoc index-item (imenu--make-index-alist))))
1059 (when index-item
1060 (push-mark)
1061 (let* ((is-special-item (listp (cdr index-item)))
1062 (function
1063 (if is-special-item
1064 (nth 2 index-item) imenu-default-goto-function))
1065 (position (if is-special-item
1066 (cadr index-item) (cdr index-item)))
1067 (rest (if is-special-item (cddr index-item))))
1068 (apply function (car index-item) position rest))
1069 (run-hooks 'imenu-after-jump-hook)))
1071 (dolist (mess
1072 '("^No items suitable for an index found in this buffer$"
1073 "^This buffer cannot use `imenu-default-create-index-function'$"
1074 "^The mode `.*' does not support Imenu$"))
1075 (add-to-list 'debug-ignored-errors mess))
1077 (provide 'imenu)
1079 ;; arch-tag: 98a2f5f5-4b91-4704-b18c-3aacf77d77a7
1080 ;;; imenu.el ends here