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