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