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