Mercurial > emacs
annotate lisp/imenu.el @ 8240:363f719d47a5
(resize-minibuffer-setup): Copy post-command-hook when handling minibuffer
windows in other frames, not just minibuffer-exclusive frames.
Resize the minibuffer window/frame now, in case it has already been
initialized with text.
author | Noah Friedman <friedman@splode.com> |
---|---|
date | Wed, 13 Jul 1994 17:19:23 +0000 |
parents | 67b7d1ea7b2e |
children | da75bd851e9f |
rev | line source |
---|---|
6230 | 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 | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
8 ;; Version: 1.7 |
6230 | 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 ;;; Change Log: | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
46 ;; v1.7 Apr 12 1994 Ake Stenhoff |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
47 ;; Changed doc strings refering to symbols. |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
48 ;; Require 'cl' when compiling only. |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
49 ;; Only uses 'cl' macros. |
6241 | 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]. | |
6230 | 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 | |
6241 | 77 ;;; Thanks goes to |
78 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch | |
79 ;; [dean] - Dean Andrews ada@unison.com | |
80 ;; | |
81 | |
6230 | 82 ;;; Code |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
83 (eval-when-compile (require 'cl)) |
6230 | 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 | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
96 used to invoke `imenu'.") |
6230 | 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 | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
107 Set it to `imenu--sort-by-name' if you want alphabetic sorting. |
6230 | 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; | |
7639 | 111 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example.") |
6230 | 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. | |
118 If non NIL, user gets a message during the scanning of the buffer | |
119 | |
120 Relevant only if the mode-specific function that creates the buffer | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
121 index use `imenu-progress-message'.") |
6230 | 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 | |
7639 | 144 \(INDEX-NAME . INDEX-ALIST). |
6230 | 145 |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
146 This function is called within a `save-excursion'. |
6230 | 147 |
148 The variable is buffer-local.") | |
149 (make-variable-buffer-local 'imenu-create-index-function) | |
150 | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
151 (defvar imenu-prev-index-position-function 'beginning-of-defun |
6230 | 152 "Function for finding the next index position. |
153 | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
154 If `imenu-create-index-function' is set to |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
155 `imenu-default-create-index-function', then you must set this variable |
6230 | 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. ") | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
161 (make-variable-buffer-local 'imenu-prev-index-position-function) |
6230 | 162 |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
163 (defvar imenu-extract-index-name-function nil |
6230 | 164 "Function for extracting the index name. |
165 | |
166 This function is called after the function pointed out by | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
167 `imenu-prev-index-position-function'.") |
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
168 (make-variable-buffer-local 'imenu-extract-index-name-function) |
6230 | 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 ;;; | |
216 ;;; Macro to display a progress message. This will probably be used | |
217 ;;; in a tight loop, that is why we use a macro. | |
218 ;;; RELPOS is the relative position to display. | |
219 ;;; If RELPOS is nil, then the relative position in the buffer | |
220 ;;; is calculated. | |
221 (defmacro imenu-progress-message (&optional relpos reverse) | |
222 (` (and | |
223 imenu-scanning-message | |
224 (message imenu-scanning-message | |
225 (, (if relpos | |
226 relpos | |
227 (` (imenu--relative-position (, reverse))))))))) | |
228 | |
229 ;;; | |
230 ;;; Function for suporting general looking submenu names. | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
231 ;;; Uses `imenu-submenu-name-format' for creating the name. |
6230 | 232 ;;; NAME is the base of the new submenu name. |
233 ;;; | |
234 (defun imenu-create-submenu-name (name) | |
235 (format imenu-submenu-name-format name)) | |
236 | |
237 ;; Split LIST into sublists of max length N. | |
238 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8)) | |
239 (defun imenu--split (list n) | |
240 (let ((remain list) | |
241 (result '()) | |
242 (sublist '()) | |
243 (i 0)) | |
244 (while remain | |
245 (push (pop remain) sublist) | |
246 (incf i) | |
247 (and (= i n) | |
248 ;; We have finished a sublist | |
249 (progn (push (nreverse sublist) result) | |
250 (setq i 0) | |
251 (setq sublist '())))) | |
252 ;; There might be a sublist (if the length of LIST mod n is != 0) | |
253 ;; that has to be added to the result list. | |
254 (and sublist | |
255 (push (nreverse sublist) result)) | |
256 (nreverse result))) | |
257 | |
258 ;;; | |
259 ;;; Split a menu in to several menus. | |
260 ;;; | |
261 (defun imenu--split-menu (menulist title) | |
262 (cons "Function menus" | |
263 (mapcar | |
264 (function | |
265 (lambda (menu) | |
266 (cons (format "(%s)" title) menu))) | |
267 (imenu--split menulist imenu-max-items)))) | |
268 | |
269 ;;; | |
270 ;;; Find all items in this buffer that should be in the index. | |
271 ;;; Returns an alist on the form | |
272 ;;; ((NAME . POSITION) (NAME . POSITION) ...) | |
273 ;;; | |
274 | |
275 (defun imenu--make-index-alist () | |
276 ;; Create a list for this buffer only when needed. | |
277 (or imenu--index-alist | |
278 ;; Get the index | |
279 (setq imenu--index-alist | |
280 (save-excursion | |
281 (funcall imenu-create-index-function)))) | |
282 (or imenu--index-alist | |
283 (error "No items suitable for an index found in this buffer.")) | |
284 ;; Add a rescan option to the index. | |
285 (cons imenu--rescan-item imenu--index-alist)) | |
286 | |
287 (defun imenu-default-create-index-function () | |
288 "*Wrapper for index searching functions. | |
289 | |
290 Moves point to end of buffer and then repeatedly calls | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
291 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'. |
6230 | 292 Their results are gathered into an index alist." |
7350
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
293 ;; These should really be done by setting imenu-create-index-function |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
294 ;; in these major modes. But save that change for later. |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
295 (cond ((eq major-mode 'emacs-lisp-mode) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
296 (imenu-example--create-lisp-index)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
297 ((eq major-mode 'lisp-mode) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
298 (imenu-example--create-lisp-index)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
299 ((eq major-mode 'c++-mode) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
300 (imenu-example--create-c++-index)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
301 ((eq major-mode 'c-mode) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
302 (imenu-example--create-c-index)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
303 (t |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
304 (or (and (fboundp imenu-prev-index-position-function) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
305 (fboundp imenu-extract-index-name-function)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
306 (error "The mode \"%s\" does not take full advantage of imenu.el yet." |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
307 mode-name)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
308 (let ((index-alist '()) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
309 name) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
310 (goto-char (point-max)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
311 (imenu-progress-message 0 t) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
312 ;; Search for the function |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
313 (while (funcall imenu-prev-index-position-function) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
314 (imenu-progress-message nil t) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
315 (save-excursion |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
316 (setq name (funcall imenu-extract-index-name-function))) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
317 (and (stringp name) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
318 (push (cons name (point)) index-alist))) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
319 (imenu-progress-message 100 t) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
320 index-alist)))) |
6230 | 321 |
322 (defun imenu--replace-spaces (name replacement) | |
323 ;; Replace all spaces in NAME with REPLACEMENT. | |
324 ;; That second argument should be a string. | |
325 (mapconcat | |
326 (function | |
327 (lambda (ch) | |
328 (if (char-equal ch ?\ ) | |
329 replacement | |
330 (char-to-string ch)))) | |
331 name | |
332 "")) | |
333 | |
334 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix) | |
335 ;; Takes a nested INDEX-ALIST and returns a flat index alist. | |
336 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its | |
337 ;; name and a space concatenated to the names of the children. | |
338 ;; Third argument PREFIX is for internal use only. | |
339 (mapcan | |
340 (function | |
341 (lambda (item) | |
342 (let* ((name (car item)) | |
343 (pos (cdr item)) | |
344 (new-prefix (and concat-names | |
345 (if prefix | |
346 (concat prefix imenu-level-separator name) | |
347 name)))) | |
348 (cond | |
349 ((numberp pos) | |
350 (list (cons new-prefix pos))) | |
351 (t | |
352 (imenu--flatten-index-alist pos new-prefix)))))) | |
353 index-alist)) | |
354 | |
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
356 ;;; | |
357 ;;; The main functions for this package! | |
358 ;;; | |
359 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
360 | |
361 (defun imenu--completion-buffer (index-alist &optional prompt) | |
362 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT. | |
363 | |
364 Returns t for rescan and otherwise a position number." | |
365 ;; Create a list for this buffer only when needed. | |
366 (let (name choice | |
367 (prepared-index-alist | |
368 (mapcar | |
369 (function | |
370 (lambda (item) | |
371 (cons (imenu--replace-spaces (car item) imenu-space-replacement) | |
372 (cdr item)))) | |
373 index-alist))) | |
374 (save-window-excursion | |
375 ;; Display the completion buffer | |
376 (with-output-to-temp-buffer "*Completions*" | |
377 (display-completion-list | |
378 (all-completions "" prepared-index-alist ))) | |
7350
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
379 (let ((minibuffer-setup-hook |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
380 (function (lambda () |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
381 (let ((buffer (current-buffer))) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
382 (save-excursion |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
383 (set-buffer "*Completions*") |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
384 (setq completion-reference-buffer buffer))))))) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
385 ;; Make a completion question |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
386 (setq name (completing-read (or prompt "Index item: ") |
6230 | 387 prepared-index-alist |
7350
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
388 nil t nil 'imenu--history-list)))) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
389 (cond ((not (stringp name)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
390 nil) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
391 ((string= name (car imenu--rescan-item)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
392 t) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
393 (t |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
394 (setq choice (assoc name prepared-index-alist)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
395 (if (listp (cdr choice)) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
396 (imenu--completion-buffer (cdr choice) prompt) |
542ab48d0f18
(imenu-default-create-index-function):
Richard M. Stallman <rms@gnu.org>
parents:
7319
diff
changeset
|
397 choice))))) |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
398 |
6230 | 399 (defun imenu--mouse-menu (index-alist event &optional title) |
400 "Let the user select from a buffer index from a mouse menu. | |
401 | |
402 INDEX-ALIST is the buffer index and EVENT is a mouse event. | |
403 | |
404 Returns t for rescan and otherwise a position number." | |
405 (let* ((menu (imenu--split-menu | |
406 (if imenu-sort-function | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
407 (sort |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
408 (let ((res nil) |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
409 (oldlist index-alist)) |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
410 ;; Copy list method from the cl package `copy-list' |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
411 (while (consp oldlist) (push (pop oldlist) res)) |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
412 (prog1 (nreverse res) (setcdr res oldlist))) |
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
413 imenu-sort-function) |
6230 | 414 index-alist) |
415 (or title (buffer-name)))) | |
416 position) | |
417 (setq position (x-popup-menu event menu)) | |
418 (cond | |
419 ((eq position nil) | |
420 position) | |
6241 | 421 ((listp position) |
6230 | 422 (imenu--mouse-menu position event |
423 (if title | |
424 (concat title imenu-level-separator | |
425 (car (rassq position index-alist))) | |
426 (car (rassq position index-alist))))) | |
427 ((= position (cdr imenu--rescan-item)) | |
428 t) | |
429 (t | |
430 (rassq position index-alist))))) | |
431 | |
6241 | 432 (defun imenu-choose-buffer-index (&optional prompt alist) |
6230 | 433 "Let the user select from a buffer index and return the chosen index. |
434 | |
435 If the user originally activated this function with the mouse, a mouse | |
436 menu is used. Otherwise a completion buffer is used and the user is | |
437 prompted with PROMPT. | |
438 | |
6241 | 439 If you call this function with index alist ALIST, then it lets the user |
440 select from ALIST. | |
441 | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
442 With no index alist ALIST, it calls `imenu--make-index-alist' to |
6241 | 443 create the index alist. |
444 | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
445 If `imenu-always-use-completion-buffer-p' is non-nil, then the |
6230 | 446 completion buffer is always used, no matter if the mouse was used or |
447 not. | |
448 | |
449 The returned value is on the form (INDEX-NAME . INDEX-POSITION)." | |
450 (let (index-alist | |
451 (mouse-triggered (listp last-command-event)) | |
452 (result t) ) | |
453 ;; If selected by mouse, see to that the window where the mouse is | |
454 ;; really is selected. | |
455 (and mouse-triggered | |
456 (let ((window (posn-window (event-start last-command-event)))) | |
457 (or (framep window) (select-window window)))) | |
458 ;; Create a list for this buffer only when needed. | |
459 (while (eq result t) | |
6241 | 460 (setq index-alist (if alist alist (imenu--make-index-alist))) |
6230 | 461 (setq result |
462 (if (and mouse-triggered | |
463 (not imenu-always-use-completion-buffer-p)) | |
464 (imenu--mouse-menu index-alist last-command-event) | |
465 (imenu--completion-buffer index-alist prompt))) | |
466 (and (eq result t) | |
467 (setq imenu--index-alist nil))) | |
468 result)) | |
469 | |
7319
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
470 ;;;###autoload |
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
471 (defun imenu () |
cd73ba498964
(imenu): Renamed from goto-index-pos.
Richard M. Stallman <rms@gnu.org>
parents:
6909
diff
changeset
|
472 "Jump to a place in the buffer chosen using a buffer menu or mouse menu. |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
473 See `imenu-choose-buffer-index' for more information." |
6230 | 474 (interactive) |
475 (let ((index-item (imenu-choose-buffer-index))) | |
476 (and index-item | |
477 (progn | |
478 (push-mark) | |
479 (goto-char (cdr index-item)))))) | |
480 | |
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
482 ;;;; | |
483 ;;;; Some examples of functions utilizing the framework of this | |
484 ;;;; package. | |
485 ;;;; | |
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
487 | |
488 ;; Return the current/previous sexp and the location of the sexp (it's | |
489 ;; beginning) without moving the point. | |
490 (defun imenu-example--name-and-position () | |
491 (save-excursion | |
492 (forward-sexp -1) | |
493 (let ((beg (point)) | |
494 (end (progn (forward-sexp) (point)))) | |
495 (cons (buffer-substring beg end) | |
496 beg)))) | |
497 | |
498 ;;; | |
499 ;;; Lisp | |
500 ;;; | |
501 | |
502 (defun imenu-example--lisp-extract-index-name () | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
503 ;; Example of a candidate for `imenu-extract-index-name-function'. |
6230 | 504 ;; This will generate a flat index of definitions in a lisp file. |
505 (save-match-data | |
506 (and (looking-at "(def") | |
507 (condition-case nil | |
508 (progn | |
509 (down-list 1) | |
510 (forward-sexp 2) | |
511 (let ((beg (point)) | |
512 (end (progn (forward-sexp -1) (point)))) | |
513 (buffer-substring beg end))) | |
514 (error nil))))) | |
515 | |
516 (defun imenu-example--create-lisp-index () | |
6909
9e59751db725
Require cl only at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
6241
diff
changeset
|
517 ;; Example of a candidate for `imenu-create-index-function'. |
6230 | 518 ;; It will generate a nested index of definitions. |
519 (let ((index-alist '()) | |
520 (index-var-alist '()) | |
6241 | 521 (index-type-alist '()) |
6230 | 522 (index-unknown-alist '())) |
523 (goto-char (point-max)) | |
524 (imenu-progress-message 0) | |
525 ;; Search for the function | |
526 (while (beginning-of-defun) | |
527 (imenu-progress-message nil t) | |
528 (save-match-data | |
529 (and (looking-at "(def") | |
530 (save-excursion | |
531 (down-list 1) | |
532 (cond | |
533 ((looking-at "def\\(var\\|const\\)") | |
534 (forward-sexp 2) | |
535 (push (imenu-example--name-and-position) | |
536 index-var-alist)) | |
537 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)") | |
538 (forward-sexp 2) | |
539 (push (imenu-example--name-and-position) | |
540 index-alist)) | |
6241 | 541 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)") |
542 (forward-sexp 2) | |
543 (if (= (char-after (1- (point))) ?\)) | |
544 (progn | |
545 (forward-sexp -1) | |
546 (down-list 1) | |
547 (forward-sexp 1))) | |
548 (push (imenu-example--name-and-position) | |
549 index-type-alist)) | |
6230 | 550 (t |
551 (forward-sexp 2) | |
552 (push (imenu-example--name-and-position) | |
553 index-unknown-alist))))))) | |
554 (imenu-progress-message 100) | |
555 (and index-var-alist | |
556 (push (cons (imenu-create-submenu-name "Variables") index-var-alist) | |
557 index-alist)) | |
6241 | 558 (and index-type-alist |
559 (push (cons (imenu-create-submenu-name "Types") index-type-alist) | |
560 index-alist)) | |
6230 | 561 (and index-unknown-alist |
562 (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist) | |
563 index-alist)) | |
564 index-alist)) | |
565 | |
566 | |
567 ;;; | |
568 ;;; C | |
569 ;;; | |
570 ;; Regular expression to find C functions | |
571 (defvar imenu-example--function-name-regexp-c | |
572 (concat | |
573 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no | |
574 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right? | |
575 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" | |
576 "\\([*&]+[ \t]*\\)?" ; pointer | |
577 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name | |
578 )) | |
579 | |
580 (defun imenu-example--create-c-index (&optional regexp) | |
581 (let ((index-alist '()) | |
582 (char)) | |
583 (goto-char (point-min)) | |
584 (imenu-progress-message 0) | |
585 ;; Search for the function | |
586 (save-match-data | |
587 (while (re-search-forward | |
588 (or regexp imenu-example--function-name-regexp-c) | |
589 nil t) | |
590 (imenu-progress-message) | |
591 (backward-up-list 1) | |
592 (save-excursion | |
593 (goto-char (scan-sexps (point) 1)) | |
594 (setq char (following-char))) | |
595 ;; Skip this function name if it is a prototype declaration. | |
596 (if (not (eq char ?\;)) | |
597 (push (imenu-example--name-and-position) index-alist)))) | |
598 (imenu-progress-message 100) | |
599 (nreverse index-alist))) | |
600 | |
601 ;;; | |
602 ;;; C++ | |
603 ;;; | |
604 ;; Regular expression to find C++ functions | |
605 (defvar imenu-example--function-name-regexp-c++ | |
606 (concat | |
607 "^[a-zA-Z0-9:]+[ \t]?" ; type specs; there can be no | |
608 "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?" ; more than 3 tokens, right? | |
609 "\\([a-zA-Z0-9_:~*]+[ \t]+\\)?" | |
610 "\\([*&]+[ \t]*\\)?" ; pointer | |
611 "\\([a-zA-Z0-9_:*]+\\)[ \t]*(" ; name | |
612 )) | |
613 (defun imenu-example--create-c++-index () | |
614 (imenu-example--create-c-index imenu-example--function-name-regexp-c++)) | |
615 | |
616 (provide 'imenu) | |
617 | |
618 ;;; imenu.el ends here |