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