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