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