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