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