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