]> code.delx.au - gnu-emacs/blob - lisp/imenu.el
(imenu-generic-lisp-expression)
[gnu-emacs] / lisp / imenu.el
1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
2
3 ;; Copyright (C) 1994, 1995 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
8 ;; Keywords: tools
9 ;;
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, write to the Free Software
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25 ;;
26 ;; Purpose of this package:
27 ;; To present a framework for mode-specific buffer indexes.
28 ;; A buffer index is an alist of names and buffer positions.
29 ;; For instance all functions in a C-file and their positions.
30 ;;
31 ;; How it works:
32
33 ;; A mode-specific function is called to generate the index. It is
34 ;; then presented to the user, who can choose from this index.
35 ;;
36 ;; The package comes with a set of example functions for how to
37 ;; utilize this package.
38
39 ;; There are *examples* for index gathering functions/regular
40 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
41 ;; customize for other modes. A function for jumping to the chosen
42 ;; index position is also supplied.
43
44 ;;; Thanks goes to
45 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
46 ;; [dean] - Dean Andrews ada@unison.com
47 ;; [alon] - Alon Albert al@mercury.co.il
48 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
49 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
50 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
51 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
52 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
53 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
54
55 ;;; Code
56 (eval-when-compile (require 'cl))
57
58 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 ;;;
60 ;;; Customizable variables
61 ;;;
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 (defvar imenu-use-keymap-menu nil
64 "*Non-nil means use a keymap when making the mouse menu.")
65
66 (defvar imenu-auto-rescan nil
67 "*Non-nil means Imenu should always rescan the buffers.")
68
69 (defvar imenu-auto-rescan-maxout 60000
70 "* auto-rescan is disabled in buffers larger than this.
71 This variable is buffer-local.")
72
73 (defvar imenu-always-use-completion-buffer-p nil
74 "*Set this to non-nil for displaying the index in a completion buffer.
75
76 Non-nil means always display the index in a completion buffer.
77 Nil means display the index as a mouse menu when the mouse was
78 used to invoke `imenu'.
79 `never' means never automatically display a listing of any kind.")
80
81 (defvar imenu-sort-function nil
82 "*The function to use for sorting the index mouse-menu.
83
84 Affects only the mouse index menu.
85
86 Set this to nil if you don't want any sorting (faster).
87 The items in the menu are then presented in the order they were found
88 in the buffer.
89
90 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
91
92 The function should take two arguments and return T if the first
93 element should come before the second. The arguments are cons cells;
94 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example.")
95
96 (defvar imenu-max-items 25
97 "*Maximum number of elements in an index mouse-menu.")
98
99 (defvar imenu-scanning-message "Scanning buffer for index (%3d%%)"
100 "*Progress message during the index scanning of the buffer.
101 If non-nil, user gets a message during the scanning of the buffer
102
103 Relevant only if the mode-specific function that creates the buffer
104 index use `imenu-progress-message'.")
105
106 (defvar imenu-space-replacement "^"
107 "*The replacement string for spaces in index names.
108 Used when presenting the index in a completion-buffer to make the
109 names work as tokens.")
110
111 (defvar imenu-level-separator ":"
112 "*The separator between index names of different levels.
113 Used for making mouse-menu titles and for flattening nested indexes
114 with name concatenation.")
115
116 (defvar imenu-submenu-name-format "%s..."
117 "*The format for making a submenu name.")
118
119 ;;;###autoload
120 (defvar imenu-generic-expression nil
121 "The regex pattern to use for creating a buffer index.
122
123 If non-nil this pattern is passed to `imenu-create-index-with-pattern'
124 to create a buffer index.
125
126 It is an alist with elements that look like this: (MENU-TITLE
127 REGEXP INDEX).
128
129 MENU-TITLE is a string used as the title for the submenu or nil if the
130 entries are not nested.
131
132 REGEXP is a regexp that should match a construct in the buffer that is
133 to be displayed in the menu; i.e., function or variable definitions,
134 etc. It contains a substring which is the name to appear in the
135 menu. See the info section on Regexps for more information.
136
137 INDEX points to the substring in REGEXP that contains the name (of the
138 function, variable or type) that is to appear in the menu.
139
140 For emacs-lisp-mode for example PATTERN would look like:
141
142 '((nil \"^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+]+\\)\" 2)
143 (\"*Vars*\" \"^\\s-*(def\\(var\\|const\\)\\s-+\\([-A-Za-z0-9+]+\\)\" 2)
144 (\"*Types*\" \"^\\s-*(def\\(type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+]+\\)\" 2))
145
146 The variable is buffer-local.")
147
148 (make-variable-buffer-local 'imenu-generic-expression)
149
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
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
158 \(INDEX-NAME . INDEX-ALIST).
159
160 This function is called within a `save-excursion'.
161
162 The variable is buffer-local.")
163 (make-variable-buffer-local 'imenu-create-index-function)
164
165 (defvar imenu-prev-index-position-function 'beginning-of-defun
166 "Function for finding the next index position.
167
168 If `imenu-create-index-function' is set to
169 `imenu-default-create-index-function', then you must set this variable
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
174 index and it should return nil when it doesn't find another index.")
175 (make-variable-buffer-local 'imenu-prev-index-position-function)
176
177 (defvar imenu-extract-index-name-function nil
178 "Function for extracting the index name.
179
180 This function is called after the function pointed out by
181 `imenu-prev-index-position-function'.")
182 (make-variable-buffer-local 'imenu-extract-index-name-function)
183
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
210 ;; Return the current/previous sexp and the location of the sexp (its
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
281 (push (cons (imenu-create-submenu-name "Variables") index-var-alist)
282 index-alist))
283 (and index-type-alist
284 (push (cons (imenu-create-submenu-name "Types") index-type-alist)
285 index-alist))
286 (and index-unknown-alist
287 (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist)
288 index-alist))
289 index-alist))
290
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
322
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
337 ;; History list for 'jump-to-function-in-buffer'.
338 ;; Making this buffer local caused it not to work!
339 (defvar imenu--history-list nil)
340
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;;
343 ;;; Internal support functions
344 ;;;
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346
347 ;;;
348 ;;; Sort function
349 ;;; Sorts the items depending on their index name.
350 ;;; An item look like (NAME . POSITION).
351 ;;;
352 (defun imenu--sort-by-name (item1 item2)
353 (string-lessp (car item1) (car item2)))
354
355 (defun imenu--relative-position (&optional reverse)
356 ;; Support function to calculate relative position in buffer
357 ;; Beginning of buffer is 0 and end of buffer is 100
358 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
359 (let ((pos (point))
360 (total (buffer-size)))
361 (and reverse (setq pos (- total pos)))
362 (if (> total 50000)
363 ;; Avoid overflow from multiplying by 100!
364 (/ (1- pos) (max (/ total 100) 1))
365 (/ (* 100 (1- pos)) (max total 1)))))
366
367 ;;;
368 ;;; Function for suporting general looking submenu names.
369 ;;; Uses `imenu-submenu-name-format' for creating the name.
370 ;;; NAME is the base of the new submenu name.
371 ;;;
372 (defun imenu-create-submenu-name (name)
373 (format imenu-submenu-name-format name))
374
375 ;; Split LIST into sublists of max length N.
376 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
377 (defun imenu--split (list n)
378 (let ((remain list)
379 (result '())
380 (sublist '())
381 (i 0))
382 (while remain
383 (push (pop remain) sublist)
384 (incf i)
385 (and (= i n)
386 ;; We have finished a sublist
387 (progn (push (nreverse sublist) result)
388 (setq i 0)
389 (setq sublist '()))))
390 ;; There might be a sublist (if the length of LIST mod n is != 0)
391 ;; that has to be added to the result list.
392 (and sublist
393 (push (nreverse sublist) result))
394 (nreverse result)))
395
396 ;;;
397 ;;; Split a menu in to several menus.
398 ;;;
399 (defun imenu--split-menu (menulist title)
400 (cons "Index menu"
401 (mapcar
402 (function
403 (lambda (menu)
404 (cons (format "(%s)" title) menu)))
405 (imenu--split menulist imenu-max-items))))
406
407 ;;;
408 ;;; Find all items in this buffer that should be in the index.
409 ;;; Returns an alist on the form
410 ;;; ((NAME . POSITION) (NAME . POSITION) ...)
411 ;;;
412
413 (defun imenu--make-index-alist ()
414 ;; Create a list for this buffer only when needed.
415 (or (and imenu--index-alist
416 (or (not imenu-auto-rescan)
417 (and imenu-auto-rescan
418 (> (buffer-size) imenu-auto-rescan-maxout))))
419 ;; Get the index
420 (setq imenu--index-alist
421 (save-excursion
422 (funcall imenu-create-index-function))))
423 (or imenu--index-alist
424 (error "No items suitable for an index found in this buffer"))
425 ;; Add a rescan option to the index.
426 (cons imenu--rescan-item imenu--index-alist))
427 ;;;
428 ;;; Find all markers in alist and makes
429 ;;; them point nowhere.
430 ;;;
431 (defun imenu--cleanup (&optional alist)
432 ;; Sets the markers in imenu--index-alist
433 ;; point nowhere.
434 ;; if alist is provided use that list.
435 (or alist
436 (setq alist imenu--index-alist))
437 (and alist
438 (mapcar
439 (function
440 (lambda (item)
441 (cond
442 ((markerp (cdr item))
443 (set-marker (cdr item) nil))
444 ((consp (cdr item))
445 (imenu--cleanup (cdr item))))))
446 alist)
447 t))
448
449 (defun imenu--create-keymap-2 (alist counter)
450 (let ((map nil))
451 (mapcar
452 (function
453 (lambda (item)
454 (cond
455 ((listp (cdr item))
456 (append (list (incf counter) (car item) 'keymap (car item))
457 (imenu--create-keymap-2 (cdr item) (+ counter 10))))
458 (t
459 (let ((end (cons '(nil) t)))
460 (cons (car item)
461 (cons (car item) end))))
462 )))
463 alist)))
464
465 (defun imenu--create-keymap-1 (title alist)
466 (append (list 'keymap title) (imenu--create-keymap-2 alist 0)))
467
468
469 (defun imenu--in-alist (str alist)
470 "Check whether the string STR is contained in multi-level ALIST."
471 (let (elt head tail res)
472 (setq res nil)
473 (while alist
474 (setq elt (car alist)
475 tail (cdr elt)
476 alist (cdr alist)
477 head (car elt))
478 (if (string= str head)
479 (setq alist nil res elt)
480 (if (and (listp tail)
481 (setq res (imenu--in-alist str tail)))
482 (setq alist nil))))
483 res))
484
485 (defun imenu-default-create-index-function ()
486 "*Wrapper for index searching functions.
487
488 Moves point to end of buffer and then repeatedly calls
489 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
490 Their results are gathered into an index alist."
491 ;; These should really be done by setting imenu-create-index-function
492 ;; in these major modes. But save that change for later.
493 (cond ((and (fboundp imenu-prev-index-position-function)
494 (fboundp imenu-extract-index-name-function))
495 (let ((index-alist '())
496 prev-pos name)
497 (goto-char (point-max))
498 (imenu-progress-message prev-pos 0 t)
499 ;; Search for the function
500 (while (funcall imenu-prev-index-position-function)
501 (imenu-progress-message prev-pos nil t)
502 (save-excursion
503 (setq name (funcall imenu-extract-index-name-function)))
504 (and (stringp name)
505 (push (cons name (point)) index-alist)))
506 (imenu-progress-message prev-pos 100 t)
507 index-alist))
508 ;; Use generic expression if possible.
509 ((and imenu-generic-expression)
510 (imenu--generic-function imenu-generic-expression))
511 (t
512 (error "The mode \"%s\" does not take full advantage of imenu.el yet."
513 mode-name))))
514
515 (defun imenu--replace-spaces (name replacement)
516 ;; Replace all spaces in NAME with REPLACEMENT.
517 ;; That second argument should be a string.
518 (mapconcat
519 (function
520 (lambda (ch)
521 (if (char-equal ch ?\ )
522 replacement
523 (char-to-string ch))))
524 name
525 ""))
526
527 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
528 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
529 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
530 ;; name and a space concatenated to the names of the children.
531 ;; Third argument PREFIX is for internal use only.
532 (mapcan
533 (function
534 (lambda (item)
535 (let* ((name (car item))
536 (pos (cdr item))
537 (new-prefix (and concat-names
538 (if prefix
539 (concat prefix imenu-level-separator name)
540 name))))
541 (cond
542 ((or (markerp pos) (numberp pos))
543 (list (cons new-prefix pos)))
544 (t
545 (imenu--flatten-index-alist pos new-prefix))))))
546 index-alist))
547
548 ;;;
549 ;;; Generic index gathering function.
550 ;;;
551
552 (defun imenu--generic-function (patterns)
553 ;; Built on some ideas that Erik Naggum <erik@naggum.no> once posted
554 ;; to comp.emacs
555 "Return an index of the current buffer as an alist.
556
557 PATTERN is an alist with elements that look like this: (MENU-TITLE
558 REGEXP INDEX).
559
560 MENU-TITLE is a string used as the title for the submenu or nil if the
561 entries are not nested.
562
563 REGEXP is a regexp that should match a construct in the buffer that is
564 to be displayed in the menu; i.e., function or variable definitions,
565 etc. It contains a substring which is the name to appear in the
566 menu. See the info section on Regexps for more information.
567
568 INDEX points to the substring in REGEXP that contains the name (of the
569 function, variable or type) that is to appear in the menu.
570
571 For emacs-lisp-mode for example PATTERN would look like:
572
573 '((nil \"^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9]+\\)\" 2)
574 (\"*Vars*\" \"^\\s-*(def\\(var\\|const\\)\\s-+\\([-A-Za-z0-9]+\\)\" 2)
575 (\"*Types*\" \"^\\s-*(def\\(type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9]+\\)\" 2))'
576
577 Returns an index of the current buffer as an alist. The elements in
578 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
579 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
580 pattern.
581
582 \(imenu--generic-function PATTERN\)."
583
584 (let ((index-alist (list 'dummy))
585 (found nil)
586 (global-regexp
587 (concat "\\("
588 (mapconcat
589 (function (lambda (pattern) (identity (cadr pattern))))
590 patterns "\\)\\|\\(")
591 "\\)"))
592 prev-pos)
593
594 (goto-char (point-max))
595 (imenu-progress-message prev-pos 0 t)
596 (save-match-data
597 (while (re-search-backward global-regexp nil t)
598 (imenu-progress-message prev-pos nil t)
599 (setq found nil)
600 (save-excursion
601 (goto-char (match-beginning 0))
602 (mapcar
603 (function
604 (lambda (pat)
605 (let ((menu-title (car pat))
606 (regexp (cadr pat))
607 (index (caddr pat)))
608 (if (and (not found) ; Only allow one entry;
609 (looking-at regexp))
610 (let ((beg (match-beginning index))
611 (end (match-end index)))
612 (setq found t)
613 (push
614 (cons (buffer-substring beg end) beg)
615 (cdr
616 (or (if (not (stringp menu-title)) index-alist)
617 (assoc
618 (imenu-create-submenu-name menu-title)
619 index-alist)
620 (car (push
621 (cons
622 (imenu-create-submenu-name menu-title)
623 '())
624 index-alist))))))))))
625 patterns))))
626 (imenu-progress-message prev-pos 100 t)
627 (delete 'dummy index-alist)))
628
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;;;
631 ;;; The main functions for this package!
632 ;;;
633 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
634
635 (defun imenu--completion-buffer (index-alist &optional prompt)
636 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
637
638 Returns t for rescan and otherwise a position number."
639 ;; Create a list for this buffer only when needed.
640 (let (name choice
641 (prepared-index-alist
642 (mapcar
643 (function
644 (lambda (item)
645 (cons (imenu--replace-spaces (car item) imenu-space-replacement)
646 (cdr item))))
647 index-alist)))
648 (if (eq imenu-always-use-completion-buffer-p 'never)
649 (setq name (completing-read (or prompt "Index item: ")
650 prepared-index-alist
651 nil t nil 'imenu--history-list))
652 (save-window-excursion
653 ;; Display the completion buffer
654 (with-output-to-temp-buffer "*Completions*"
655 (display-completion-list
656 (all-completions "" prepared-index-alist )))
657 (let ((minibuffer-setup-hook
658 (function (lambda ()
659 (let ((buffer (current-buffer)))
660 (save-excursion
661 (set-buffer "*Completions*")
662 (setq completion-reference-buffer buffer)))))))
663 ;; Make a completion question
664 (setq name (completing-read (or prompt "Index item: ")
665 prepared-index-alist
666 nil t nil 'imenu--history-list)))))
667 (cond ((not (stringp name))
668 nil)
669 ((string= name (car imenu--rescan-item))
670 t)
671 (t
672 (setq choice (assoc name prepared-index-alist))
673 (if (listp (cdr choice))
674 (imenu--completion-buffer (cdr choice) prompt)
675 choice)))))
676
677 (defun imenu--mouse-menu (index-alist event &optional title)
678 "Let the user select from a buffer index from a mouse menu.
679
680 INDEX-ALIST is the buffer index and EVENT is a mouse event.
681
682 Returns t for rescan and otherwise a position number."
683 (let* ((menu (imenu--split-menu
684 (if imenu-sort-function
685 (sort
686 (let ((res nil)
687 (oldlist index-alist))
688 ;; Copy list method from the cl package `copy-list'
689 (while (consp oldlist) (push (pop oldlist) res))
690 (prog1 (nreverse res) (setcdr res oldlist)))
691 imenu-sort-function)
692 index-alist)
693 (or title (buffer-name))))
694 position)
695 (and imenu-use-keymap-menu
696 (setq menu (imenu--create-keymap-1 (car menu)
697 (if (< 1 (length (cdr menu)))
698 (cdr menu)
699 (cdr (cadr menu))))))
700 (setq position (x-popup-menu event menu))
701 (if imenu-use-keymap-menu
702 (progn
703 (cond
704 ((and (listp position)
705 (numberp (car position))
706 (stringp (nth (1- (length position)) position)))
707 (setq position (nth (1- (length position)) position)))
708 ((and (stringp (car position))
709 (null (cdr position)))
710 (setq position (car position))))))
711 (cond
712 ((eq position nil)
713 position)
714 ((listp position)
715 (imenu--mouse-menu position event
716 (if title
717 (concat title imenu-level-separator
718 (car (rassq position index-alist)))
719 (car (rassq position index-alist)))))
720 ((stringp position)
721 (or (string= position (car imenu--rescan-item))
722 (imenu--in-alist position index-alist)))
723 ((or (= position (cdr imenu--rescan-item))
724 (and (stringp position)
725 (string= position (car imenu--rescan-item))))
726 t)
727 (t
728 (rassq position index-alist)))))
729
730 (defun imenu-choose-buffer-index (&optional prompt alist)
731 "Let the user select from a buffer index and return the chosen index.
732
733 If the user originally activated this function with the mouse, a mouse
734 menu is used. Otherwise a completion buffer is used and the user is
735 prompted with PROMPT.
736
737 If you call this function with index alist ALIST, then it lets the user
738 select from ALIST.
739
740 With no index alist ALIST, it calls `imenu--make-index-alist' to
741 create the index alist.
742
743 If `imenu-always-use-completion-buffer-p' is non-nil, then the
744 completion buffer is always used, no matter if the mouse was used or
745 not.
746
747 The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
748 (let (index-alist
749 (mouse-triggered (listp last-nonmenu-event))
750 (result t) )
751 ;; If selected by mouse, see to that the window where the mouse is
752 ;; really is selected.
753 (and mouse-triggered
754 (not (equal last-nonmenu-event '(menu-bar)))
755 (let ((window (posn-window (event-start last-nonmenu-event))))
756 (or (framep window) (null window) (select-window window))))
757 ;; Create a list for this buffer only when needed.
758 (while (eq result t)
759 (setq index-alist (if alist alist (imenu--make-index-alist)))
760 (setq result
761 (if (and mouse-triggered
762 (not imenu-always-use-completion-buffer-p))
763 (imenu--mouse-menu index-alist last-nonmenu-event)
764 (imenu--completion-buffer index-alist prompt)))
765 (and (eq result t)
766 (imenu--cleanup)
767 (setq imenu--index-alist nil)))
768 result))
769
770 ;;;###autoload
771 (defun imenu-add-to-menubar (name)
772 "Adds an \"imenu\" entry to the menubar for the current local keymap.
773 NAME is the string naming the menu to be added.
774 See 'imenu' for more information."
775 (interactive "sMenu name: ")
776 (and window-system
777 (define-key (current-local-map) [menu-bar index]
778 (cons name 'imenu))))
779
780 ;;;###autoload
781 (defun imenu (index-item)
782 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
783 See `imenu-choose-buffer-index' for more information."
784 (interactive
785 (list (save-restriction
786 (widen)
787 (imenu-choose-buffer-index))))
788 (and index-item
789 (progn
790 (push-mark)
791 (cond
792 ((markerp (cdr index-item))
793 (if (or ( > (marker-position (cdr index-item)) (point-min))
794 ( < (marker-position (cdr index-item)) (point-max)))
795 ;; widen if outside narrowing
796 (widen))
797 (goto-char (marker-position (cdr index-item))))
798 (t
799 (if (or ( > (cdr index-item) (point-min))
800 ( < (cdr index-item) (point-max)))
801 ;; widen if outside narrowing
802 (widen))
803 (goto-char (cdr index-item)))))))
804
805 (provide 'imenu)
806
807 ;;; imenu.el ends here