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