Commit | Line | Data |
---|---|---|
0a688fd0 RS |
1 | ;;; imenu.el --- Framework for mode-specific buffer indexes. |
2 | ||
3 | ;; Copyright (C) 1994 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se> | |
6 | ;; Lars Lindberg <lli@sypro.cap.se> | |
7 | ;; Created: 8 Feb 1994 | |
26d6bb60 | 8 | ;; Version: 1.6 |
0a688fd0 RS |
9 | ;; Keywords: tools |
10 | ;; | |
11 | ;; This program is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ;; | |
16 | ;; This program is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ;; | |
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with this program; if not, write to the Free Software | |
23 | ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ;; | |
27 | ;; Purpose of this package: | |
28 | ;; To present a framework for mode-specific buffer indexes. | |
29 | ;; A buffer index is an alist of names and buffer positions. | |
30 | ;; For instance all functions in a C-file and their positions. | |
31 | ;; | |
32 | ;; How it works: | |
33 | ||
34 | ;; A mode-specific function is called to generate the index. It is | |
35 | ;; then presented to the user, who can choose from this index. | |
36 | ;; | |
37 | ;; The package comes with a set of example functions for how to | |
38 | ;; utilize this package. | |
39 | ||
40 | ;; There are *examples* for index gathering functions for C/C++ and | |
41 | ;; Lisp/Emacs Lisp but it is easy to customize for other modes. A | |
42 | ;; function for jumping to the chosen index position is also | |
43 | ;; supplied. | |
44 | ;; | |
45 | ;; Installation: | |
46 | ;; Put this file in your load-path and insert the following in .emacs | |
47 | ;; | |
48 | ;; (autoload 'imenu-choose-buffer-index "imenu" "Menu of buffer index." t) | |
49 | ;; (autoload 'goto-index-pos "imenu" "Goto buffer index position." t) | |
50 | ;; (define-key global-map "\C-cj" 'goto-index-pos) ;; Or some other key | |
51 | ;; (cond (window-system | |
26d6bb60 RS |
52 | ;; (define-key global-map [S-down-mouse-3] 'goto-index-pos))) |
53 | ;; Also run the 'add-hook' examples at the bottom of imenu.el. | |
0a688fd0 RS |
54 | |
55 | ;;; Change Log: | |
26d6bb60 RS |
56 | ;; v1.6 Feb 28 1994 Ake Stenhoff |
57 | ;; Added alist as an optional argument to | |
58 | ;; 'imenu-choose-buffer-index'. | |
59 | ;; Thanks [dean]. | |
60 | ;; v1.5 Feb 25 1994 Ake Stenhoff | |
61 | ;; Added code to parse DEFSTRUCT, DEFCLASS, DEFTYPE, | |
62 | ;; DEFINE-CONDITION in the lisp example function. | |
63 | ;; Thanks [simon]. | |
0a688fd0 RS |
64 | ;; v1.4 Feb 18 1994 Ake Stenhoff |
65 | ;; Added 'imenu-create-submenu-name' for creating a submenu name. | |
66 | ;; This is for getting a general look of submenu names. | |
67 | ;; Added variable 'imenu-submenu-name-format' used by | |
68 | ;; 'imenu-create-submenu-name'. | |
69 | ;; v1.3 Feb 17 1994 Lars Lindberg | |
70 | ;; Added 'imenu--flatten-index-alist' for flatten nexted index | |
71 | ;; alists. | |
72 | ;; New examples for lisp mode that utilizes the features better. | |
73 | ;; Added the variable 'imenu-space-replacement'. | |
74 | ;; The completion-buffer version of the index menu now replaces | |
75 | ;; spaces in the index-names to make tokens of them. | |
76 | ;; v1.2 Feb 14 1994 Ake Stenhoff & Lars Lindberg | |
77 | ;; Now handles nested index lists. | |
78 | ;; v1.1 Feb 9 1994 Ake Stenhoff & Lars Lindberg | |
79 | ;; Better comments (?). | |
80 | ;; v1.0 Feb 8 1994 Ake Stenhoff & Lars Lindberg | |
81 | ;; Based on func-menu.el 3.5. | |
82 | ||
26d6bb60 RS |
83 | ;;; Thanks goes to |
84 | ;; [simon] - Simon Leinen simon@lia.di.epfl.ch | |
85 | ;; [dean] - Dean Andrews ada@unison.com | |
86 | ;; | |
87 | ||
0a688fd0 RS |
88 | ;;; Code |
89 | (require 'cl) | |
90 | ||
91 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
92 | ;;; | |
93 | ;;; Customizable variables | |
94 | ;;; | |
95 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
96 | ||
97 | (defvar imenu-always-use-completion-buffer-p nil | |
98 | "*Set this to non-nil for displaying the index in a completion buffer. | |
99 | ||
100 | Non-nil means always display the index in a completion buffer. | |
101 | Nil means display the index as a mouse menu when the mouse was | |
102 | used to trigger 'goto-index-pos'.") | |
103 | ||
104 | (defvar imenu-sort-function nil | |
105 | "*The function to use for sorting the index mouse-menu. | |
106 | ||
107 | Affects only the mouse index menu. | |
108 | ||
109 | Set this to nil if you don't want any sorting (faster). | |
110 | The items in the menu are then presented in the order they were found | |
111 | in the buffer. | |
112 | ||
113 | Set it to 'imenu--sort-by-name if you want alphabetic sorting. | |
114 | ||
115 | The function should take two arguments and return T if the first | |
116 | element should come before the second. The arguments are cons cells; | |
117 | (NAME . POSITION). Look at 'imenu--sort-by-name' for an example.") | |
118 | ||
119 | (defvar imenu-max-items 25 | |
120 | "*Maximum number of elements in an index mouse-menu.") | |
121 | ||
122 | (defvar imenu-scanning-message "Scanning buffer for index. (%3d%%)" | |
123 | "*Progress message during the index scanning of the buffer. | |
124 | If non NIL, user gets a message during the scanning of the buffer | |
125 | ||
126 | Relevant only if the mode-specific function that creates the buffer | |
127 | index use 'imenu-progress-message'.") | |
128 | ||
129 | (defvar imenu-space-replacement "^" | |
130 | "*The replacement string for spaces in index names. | |
131 | Used when presenting the index in a completion-buffer to make the | |
132 | names work as tokens.") | |
133 | ||
134 | (defvar imenu-level-separator ":" | |
135 | "*The separator between index names of different levels. | |
136 | Used for making mouse-menu titles and for flattening nested indexes | |
137 | with name concatenation.") | |
138 | ||
139 | (defvar imenu-submenu-name-format "%s..." | |
140 | "*The format for making a submenu name.") | |
141 | ||
142 | ;;;; Hooks | |
143 | ||
144 | (defvar imenu-create-index-function 'imenu-default-create-index-function | |
145 | "The function to use for creating a buffer index. | |
146 | ||
147 | It should be a function that takes no arguments and returns an index | |
148 | of the current buffer as an alist. The elements in the alist look | |
149 | like: (INDEX-NAME . INDEX-POSITION). You may also nest index list like | |
150 | (INDEX-NAME . INDEX-ALIST). | |
151 | ||
152 | This function is called within a 'save-excursion'. | |
153 | ||
154 | The variable is buffer-local.") | |
155 | (make-variable-buffer-local 'imenu-create-index-function) | |
156 | ||
157 | (defvar prev-index-position-function 'beginning-of-defun | |
158 | "Function for finding the next index position. | |
159 | ||
160 | If 'imenu-create-index-function' is set to | |
161 | 'imenu-default-create-index-function, then you must set this variable | |
162 | to a function that will find the next index, looking backwards in the | |
163 | file. | |
164 | ||
165 | The function should leave point at the place to be connected to the | |
166 | index and it should return nil when it doesn't find another index. ") | |
167 | (make-variable-buffer-local 'prev-index-position-function) | |
168 | ||
169 | (defvar extract-index-name-function nil | |
170 | "Function for extracting the index name. | |
171 | ||
0a688fd0 RS |
172 | This function is called after the function pointed out by |
173 | 'prev-index-position-function'.") | |
174 | (make-variable-buffer-local 'extract-index-name-function) | |
175 | ||
176 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
177 | ;;; | |
178 | ;;; Internal variables | |
179 | ;;; | |
180 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
181 | ||
182 | ;; The item to use in the index for rescanning the buffer. | |
183 | (defconst imenu--rescan-item '("*Rescan*" . -99)) | |
184 | ||
185 | ;; The latest buffer index. | |
186 | ;; Buffer local. | |
187 | (defvar imenu--index-alist nil) | |
188 | (make-variable-buffer-local 'imenu--index-alist) | |
189 | ||
190 | ;; History list for 'jump-to-function-in-buffer'. | |
191 | ;; Buffer local. | |
192 | (defvar imenu--history-list nil) | |
193 | (make-variable-buffer-local 'imenu--history-list) | |
194 | ||
195 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
196 | ;;; | |
197 | ;;; Internal support functions | |
198 | ;;; | |
199 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
200 | ||
201 | ;;; | |
202 | ;;; Sort function | |
203 | ;;; Sorts the items depending on their index name. | |
204 | ;;; An item look like (NAME . POSITION). | |
205 | ;;; | |
206 | (defun imenu--sort-by-name (item1 item2) | |
207 | (string-lessp (car item1) (car item2))) | |
208 | ||
209 | (defun imenu--relative-position (&optional reverse) | |
210 | ;; Support function to calculate relative position in buffer | |
211 | ;; Beginning of buffer is 0 and end of buffer is 100 | |
212 | ;; If REVERSE is non-nil then the beginning is 100 and the end is 0. | |
213 | (let ((pos (point)) | |
214 | (total (buffer-size))) | |
215 | (and reverse (setq pos (- total pos))) | |
216 | (if (> total 50000) | |
217 | ;; Avoid overflow from multiplying by 100! | |
218 | (/ (1- pos) (max (/ total 100) 1)) | |
219 | (/ (* 100 (1- pos)) (max total 1))))) | |
220 | ||
221 | ;;; | |
222 | ;;; Macro to display a progress message. This will probably be used | |
223 | ;;; in a tight loop, that is why we use a macro. | |
224 | ;;; RELPOS is the relative position to display. | |
225 | ;;; If RELPOS is nil, then the relative position in the buffer | |
226 | ;;; is calculated. | |
227 | (defmacro imenu-progress-message (&optional relpos reverse) | |
228 | (` (and | |
229 | imenu-scanning-message | |
230 | (message imenu-scanning-message | |
231 | (, (if relpos | |
232 | relpos | |
233 | (` (imenu--relative-position (, reverse))))))))) | |
234 | ||
235 | ;;; | |
236 | ;;; Function for suporting general looking submenu names. | |
237 | ;;; Uses 'imenu-submenu-name-format' for creating the name. | |
238 | ;;; NAME is the base of the new submenu name. | |
239 | ;;; | |
240 | (defun imenu-create-submenu-name (name) | |
241 | (format imenu-submenu-name-format name)) | |
242 | ||
243 | ;; Split LIST into sublists of max length N. | |
244 | ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8)) | |
245 | (defun imenu--split (list n) | |
246 | (let ((remain list) | |
247 | (result '()) | |
248 | (sublist '()) | |
249 | (i 0)) | |
250 | (while remain | |
251 | (push (pop remain) sublist) | |
252 | (incf i) | |
253 | (and (= i n) | |
254 | ;; We have finished a sublist | |
255 | (progn (push (nreverse sublist) result) | |
256 | (setq i 0) | |
257 | (setq sublist '())))) | |
258 | ;; There might be a sublist (if the length of LIST mod n is != 0) | |
259 | ;; that has to be added to the result list. | |
260 | (and sublist | |
261 | (push (nreverse sublist) result)) | |
262 | (nreverse result))) | |
263 | ||
264 | ;;; | |
265 | ;;; Split a menu in to several menus. | |
266 | ;;; | |
267 | (defun imenu--split-menu (menulist title) | |
268 | (cons "Function menus" | |
269 | (mapcar | |
270 | (function | |
271 | (lambda (menu) | |
272 | (cons (format "(%s)" title) menu))) | |
273 | (imenu--split menulist imenu-max-items)))) | |
274 | ||
275 | ;;; | |
276 | ;;; Find all items in this buffer that should be in the index. | |
277 | ;;; Returns an alist on the form | |
278 | ;;; ((NAME . POSITION) (NAME . POSITION) ...) | |
279 | ;;; | |
280 | ||
281 | (defun imenu--make-index-alist () | |
282 | ;; Create a list for this buffer only when needed. | |
283 | (or imenu--index-alist | |
284 | ;; Get the index | |
285 | (setq imenu--index-alist | |
286 | (save-excursion | |
287 | (funcall imenu-create-index-function)))) | |
288 | (or imenu--index-alist | |
289 | (error "No items suitable for an index found in this buffer.")) | |
290 | ;; Add a rescan option to the index. | |
291 | (cons imenu--rescan-item imenu--index-alist)) | |
292 | ||
293 | (defun imenu-default-create-index-function () | |
294 | "*Wrapper for index searching functions. | |
295 | ||
296 | Moves point to end of buffer and then repeatedly calls | |
297 | 'prev-index-position-function' and 'extract-index-name-function'. | |
0a688fd0 RS |
298 | Their results are gathered into an index alist." |
299 | ||
300 | (or (and (fboundp prev-index-position-function) | |
301 | (fboundp extract-index-name-function)) | |
302 | (error "The mode \"%s\" does not take full advantage of imenu.el yet." | |
303 | mode-name)) | |
304 | (let ((index-alist '()) | |
305 | name) | |
306 | (goto-char (point-max)) | |
307 | (imenu-progress-message 0 t) | |
308 | ;; Search for the function | |
309 | (while (funcall prev-index-position-function) | |
310 | (imenu-progress-message nil t) | |
311 | (save-excursion | |
312 | (setq name (funcall extract-index-name-function))) | |
313 | (and (stringp name) | |
314 | (push (cons name (point)) index-alist))) | |
315 | (imenu-progress-message 100 t) | |
316 | index-alist)) | |
317 | ||
318 | (defun imenu--replace-spaces (name replacement) | |
319 | ;; Replace all spaces in NAME with REPLACEMENT. | |
320 | ;; That second argument should be a string. | |
321 | (mapconcat | |
322 | (function | |
323 | (lambda (ch) | |
324 | (if (char-equal ch ?\ ) | |
325 | replacement | |
326 | (char-to-string ch)))) | |
327 | name | |
328 | "")) | |
329 | ||
330 | (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix) | |
331 | ;; Takes a nested INDEX-ALIST and returns a flat index alist. | |
332 | ;; If optional CONCAT-NAMES is non-nil, then a nested index has its | |
333 | ;; name and a space concatenated to the names of the children. | |
334 | ;; Third argument PREFIX is for internal use only. | |
335 | (mapcan | |
336 | (function | |
337 | (lambda (item) | |
338 | (let* ((name (car item)) | |
339 | (pos (cdr item)) | |
340 | (new-prefix (and concat-names | |
341 | (if prefix | |
342 | (concat prefix imenu-level-separator name) | |
343 | name)))) | |
344 | (cond | |
345 | ((numberp pos) | |
346 | (list (cons new-prefix pos))) | |
347 | (t | |
348 | (imenu--flatten-index-alist pos new-prefix)))))) | |
349 | index-alist)) | |
350 | ||
351 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
352 | ;;; | |
353 | ;;; The main functions for this package! | |
354 | ;;; | |
355 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
356 | ||
357 | (defun imenu--completion-buffer (index-alist &optional prompt) | |
358 | "Let the user select from INDEX-ALIST in a completion buffer with PROMPT. | |
359 | ||
360 | Returns t for rescan and otherwise a position number." | |
361 | ;; Create a list for this buffer only when needed. | |
362 | (let (name choice | |
363 | (prepared-index-alist | |
364 | (mapcar | |
365 | (function | |
366 | (lambda (item) | |
367 | (cons (imenu--replace-spaces (car item) imenu-space-replacement) | |
368 | (cdr item)))) | |
369 | index-alist))) | |
370 | (save-window-excursion | |
371 | ;; Display the completion buffer | |
372 | (with-output-to-temp-buffer "*Completions*" | |
373 | (display-completion-list | |
374 | (all-completions "" prepared-index-alist ))) | |
375 | ;; Make a completion question | |
376 | (setq name (completing-read (or prompt "Index item: ") | |
377 | prepared-index-alist | |
378 | nil t nil 'imenu--history-list))) | |
379 | (cond | |
380 | ((not (stringp name)) | |
381 | nil) | |
382 | ((string= name (car imenu--rescan-item)) | |
383 | t) | |
384 | (t | |
385 | (setq choice (assoc name prepared-index-alist)) | |
386 | (cond | |
387 | ((listp (cdr choice)) | |
388 | (imenu--completion-buffer (cdr choice) prompt)) | |
389 | (t | |
390 | choice)))))) | |
391 | ||
392 | (defun imenu--mouse-menu (index-alist event &optional title) | |
393 | "Let the user select from a buffer index from a mouse menu. | |
394 | ||
395 | INDEX-ALIST is the buffer index and EVENT is a mouse event. | |
396 | ||
397 | Returns t for rescan and otherwise a position number." | |
398 | (let* ((menu (imenu--split-menu | |
399 | (if imenu-sort-function | |
400 | (sort (copy-list index-alist) imenu-sort-function) | |
401 | index-alist) | |
402 | (or title (buffer-name)))) | |
403 | position) | |
404 | (setq position (x-popup-menu event menu)) | |
405 | (cond | |
406 | ((eq position nil) | |
407 | position) | |
26d6bb60 | 408 | ((listp position) |
0a688fd0 RS |
409 | (imenu--mouse-menu position event |
410 | (if title | |
411 | (concat title imenu-level-separator | |
412 | (car (rassq position index-alist))) | |
413 | (car (rassq position index-alist))))) | |
414 | ((= position (cdr imenu--rescan-item)) | |
415 | t) | |
416 | (t | |
417 | (rassq position index-alist))))) | |
418 | ||
26d6bb60 | 419 | (defun imenu-choose-buffer-index (&optional prompt alist) |
0a688fd0 RS |
420 | "Let the user select from a buffer index and return the chosen index. |
421 | ||
422 | If the user originally activated this function with the mouse, a mouse | |
0a688fd0 RS |
423 | menu is used. Otherwise a completion buffer is used and the user is |
424 | prompted with PROMPT. | |
425 | ||
26d6bb60 RS |
426 | If you call this function with index alist ALIST, then it lets the user |
427 | select from ALIST. | |
428 | ||
429 | With no index alist ALIST, it calls 'imenu--make-index-alist' to | |
430 | create the index alist. | |
431 | ||
0a688fd0 RS |
432 | If 'imenu-always-use-completion-buffer-p' is non-nil, then the |
433 | completion buffer is always used, no matter if the mouse was used or | |
434 | not. | |
435 | ||
436 | The returned value is on the form (INDEX-NAME . INDEX-POSITION)." | |
437 | (let (index-alist | |
438 | (mouse-triggered (listp last-command-event)) | |
439 | (result t) ) | |
440 | ;; If selected by mouse, see to that the window where the mouse is | |
441 | ;; really is selected. | |
442 | (and mouse-triggered | |
443 | (let ((window (posn-window (event-start last-command-event)))) | |
444 | (or (framep window) (select-window window)))) | |
445 | ;; Create a list for this buffer only when needed. | |
446 | (while (eq result t) | |
26d6bb60 | 447 | (setq index-alist (if alist alist (imenu--make-index-alist))) |
0a688fd0 RS |
448 | (setq result |
449 | (if (and mouse-triggered | |
450 | (not imenu-always-use-completion-buffer-p)) | |
451 | (imenu--mouse-menu index-alist last-command-event) | |
452 | (imenu--completion-buffer index-alist prompt))) | |
453 | (and (eq result t) | |
454 | (setq imenu--index-alist nil))) | |
455 | result)) | |
456 | ||
457 | (defun goto-index-pos () | |
458 | "Jump to selected part of buffer, using a buffer menu or mouse menu. | |
459 | ||
460 | See 'imenu-choose-buffer-index' for more information." | |
461 | (interactive) | |
462 | (let ((index-item (imenu-choose-buffer-index))) | |
463 | (and index-item | |
464 | (progn | |
465 | (push-mark) | |
466 | (goto-char (cdr index-item)))))) | |
467 | ||
468 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
469 | ;;;; | |
470 | ;;;; Some examples of functions utilizing the framework of this | |
471 | ;;;; package. | |
472 | ;;;; | |
473 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
474 | ||
475 | ;; Return the current/previous sexp and the location of the sexp (it's | |
476 | ;; beginning) without moving the point. | |
477 | (defun imenu-example--name-and-position () | |
478 | (save-excursion | |
479 | (forward-sexp -1) | |
480 | (let ((beg (point)) | |
481 | (end (progn (forward-sexp) (point)))) | |
482 | (cons (buffer-substring beg end) | |
483 | beg)))) | |
484 | ||
485 | ;;; | |
486 | ;;; Lisp | |
487 | ;;; | |
488 | ||
489 | (defun imenu-example--lisp-extract-index-name () | |
490 | ;; Example of a candidate for 'imenu-extract-index-name-function'. | |
491 | ;; This will generate a flat index of definitions in a lisp file. | |
492 | (save-match-data | |
493 | (and (looking-at "(def") | |
494 | (condition-case nil | |
495 | (progn | |
496 | (down-list 1) | |
497 | (forward-sexp 2) | |
498 | (let ((beg (point)) | |
499 | (end (progn (forward-sexp -1) (point)))) | |
500 | (buffer-substring beg end))) | |
501 | (error nil))))) | |
502 | ||
503 | (defun imenu-example--create-lisp-index () | |
504 | ;; Example of a candidate for 'imenu-create-index-function'. | |
505 | ;; It will generate a nested index of definitions. | |
506 | (let ((index-alist '()) | |
507 | (index-var-alist '()) | |
26d6bb60 | 508 | (index-type-alist '()) |
0a688fd0 RS |
509 | (index-unknown-alist '())) |
510 | (goto-char (point-max)) | |
511 | (imenu-progress-message 0) | |
512 | ;; Search for the function | |
513 | (while (beginning-of-defun) | |
514 | (imenu-progress-message nil t) | |
515 | (save-match-data | |
516 | (and (looking-at "(def") | |
517 | (save-excursion | |
518 | (down-list 1) | |
519 | (cond | |
520 | ((looking-at "def\\(var\\|const\\)") | |
521 | (forward-sexp 2) | |
522 | (push (imenu-example--name-and-position) | |
523 | index-var-alist)) | |
524 | ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)") | |
525 | (forward-sexp 2) | |
526 | (push (imenu-example--name-and-position) | |
527 | index-alist)) | |
26d6bb60 RS |
528 | ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)") |
529 | (forward-sexp 2) | |
530 | (if (= (char-after (1- (point))) ?\)) | |
531 | (progn | |
532 | (forward-sexp -1) | |
533 | (down-list 1) | |
534 | (forward-sexp 1))) | |
535 | (push (imenu-example--name-and-position) | |
536 | index-type-alist)) | |
0a688fd0 RS |
537 | (t |
538 | (forward-sexp 2) | |
539 | (push (imenu-example--name-and-position) | |
540 | index-unknown-alist))))))) | |
541 | (imenu-progress-message 100) | |
542 | (and index-var-alist | |
543 | (push (cons (imenu-create-submenu-name "Variables") index-var-alist) | |
544 | index-alist)) | |
26d6bb60 RS |
545 | (and index-type-alist |
546 | (push (cons (imenu-create-submenu-name "Types") index-type-alist) | |
547 | index-alist)) | |
0a688fd0 RS |
548 | (and index-unknown-alist |
549 | (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist) | |
550 | index-alist)) | |
551 | index-alist)) | |
552 | ||
553 | ||
554 | ;;; | |
555 | ;;; C | |
556 | ;;; | |
557 | ;; Regular expression to find C functions | |
558 | (defvar imenu-example--function-name-regexp-c | |
559 | (concat | |
560 | "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no | |
561 | "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right? | |
562 | "\\([a-zA-Z0-9_*]+[ \t]+\\)?" | |
563 | "\\([*&]+[ \t]*\\)?" ; pointer | |
564 | "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name | |
565 | )) | |
566 | ||
0a688fd0 RS |
567 | (defun imenu-example--create-c-index (&optional regexp) |
568 | (let ((index-alist '()) | |
569 | (char)) | |
570 | (goto-char (point-min)) | |
571 | (imenu-progress-message 0) | |
572 | ;; Search for the function | |
573 | (save-match-data | |
574 | (while (re-search-forward | |
575 | (or regexp imenu-example--function-name-regexp-c) | |
576 | nil t) | |
577 | (imenu-progress-message) | |
578 | (backward-up-list 1) | |
579 | (save-excursion | |
580 | (goto-char (scan-sexps (point) 1)) | |
581 | (setq char (following-char))) | |
582 | ;; Skip this function name if it is a prototype declaration. | |
583 | (if (not (eq char ?\;)) | |
584 | (push (imenu-example--name-and-position) index-alist)))) | |
585 | (imenu-progress-message 100) | |
586 | (nreverse index-alist))) | |
587 | ||
588 | ;;; | |
589 | ;;; C++ | |
590 | ;;; | |
591 | ;; Regular expression to find C++ functions | |
592 | (defvar imenu-example--function-name-regexp-c++ | |
593 | (concat | |
594 | "^[a-zA-Z0-9:]+[ \t]?" ; type specs; there can be no | |
595 | "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?" ; more than 3 tokens, right? | |
596 | "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?" | |
597 | "\\([*&]+[ \t]*\\)?" ; pointer | |
598 | "\\([a-zA-Z0-9_:*]+\\)[ \t]*(" ; name | |
599 | )) | |
600 | (defun imenu-example--create-c++-index () | |
601 | (imenu-example--create-c-index imenu-example--function-name-regexp-c++)) | |
602 | ||
603 | ||
604 | ;;; | |
605 | ;;; Example of hooks for the examples above | |
606 | ;;; Put this in your .emacs. | |
607 | ;;; | |
608 | ;; (add-hook 'emacs-lisp-mode-hook | |
609 | ;; (function | |
610 | ;; (lambda () | |
611 | ;; (setq imenu-create-index-function | |
612 | ;; (function imenu-example--create-lisp-index))))) | |
613 | ||
614 | ;; (add-hook 'lisp-mode-hook | |
615 | ;; (function | |
616 | ;; (lambda () | |
617 | ;; (setq imenu-create-index-function | |
618 | ;; (function imenu-example--create-lisp-index))))) | |
619 | ||
620 | ;; (add-hook 'c++-mode-hook | |
621 | ;; (function | |
622 | ;; (lambda () | |
623 | ;; (setq imenu-create-index-function | |
624 | ;; (function imenu-example--create-c++-index))))) | |
625 | ||
626 | ;; (add-hook 'c-mode-hook | |
627 | ;; (function | |
628 | ;; (lambda () | |
629 | ;; (setq imenu-create-index-function | |
630 | ;; (function imenu-example--create-c-index))))) | |
631 | ||
632 | (provide 'imenu) | |
633 | ||
634 | ;;; imenu.el ends here |