(iso-transl-char-map): Add elements for mute-acute, etc.
[emacs.git] / lisp / imenu.el
blobaa4dd433dbe4cb41817fe462686a77f88c92686e
1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
6 ;; Lars Lindberg <lli@sypro.cap.se>
7 ;; Created: 8 Feb 1994
8 ;; Version: 1.15
9 ;; Keywords: tools
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.
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.
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.
25 ;;; Commentary:
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.
32 ;; How it works:
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.
37 ;; The package comes with a set of example functions for how to
38 ;; utilize this package.
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.
45 ;;; Thanks goes to
46 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
47 ;; [dean] - Dean Andrews ada@unison.com
48 ;; [alon] - Alon Albert al@mercury.co.il
49 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
50 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
51 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
52 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
53 ;;; Code
54 (eval-when-compile (require 'cl))
56 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 ;;;
58 ;;; Customizable variables
59 ;;;
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 (defvar imenu-always-use-completion-buffer-p nil
63 "*Set this to non-nil for displaying the index in a completion buffer.
65 Non-nil means always display the index in a completion buffer.
66 Nil means display the index as a mouse menu when the mouse was
67 used to invoke `imenu'.
68 `never' means never automatically display a listing of any kind.")
70 (defvar imenu-sort-function nil
71 "*The function to use for sorting the index mouse-menu.
73 Affects only the mouse index menu.
75 Set this to nil if you don't want any sorting (faster).
76 The items in the menu are then presented in the order they were found
77 in the buffer.
79 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
81 The function should take two arguments and return T if the first
82 element should come before the second. The arguments are cons cells;
83 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example.")
85 (defvar imenu-max-items 25
86 "*Maximum number of elements in an index mouse-menu.")
88 (defvar imenu-scanning-message "Scanning buffer for index. (%3d%%)"
89 "*Progress message during the index scanning of the buffer.
90 If non-nil, user gets a message during the scanning of the buffer
92 Relevant only if the mode-specific function that creates the buffer
93 index use `imenu-progress-message'.")
95 (defvar imenu-space-replacement "^"
96 "*The replacement string for spaces in index names.
97 Used when presenting the index in a completion-buffer to make the
98 names work as tokens.")
100 (defvar imenu-level-separator ":"
101 "*The separator between index names of different levels.
102 Used for making mouse-menu titles and for flattening nested indexes
103 with name concatenation.")
105 (defvar imenu-submenu-name-format "%s..."
106 "*The format for making a submenu name.")
108 (defvar imenu-generic-expression nil
109 "Generic regular expression for index gathering.
111 Can be either an regular expression or an alist in the form
112 \(REGEXP PAREN).")
113 (make-variable-buffer-local 'imenu-generic-expression)
115 ;;;; Hooks
117 (defvar imenu-create-index-function 'imenu-default-create-index-function
118 "The function to use for creating a buffer index.
120 It should be a function that takes no arguments and returns an index
121 of the current buffer as an alist. The elements in the alist look
122 like: (INDEX-NAME . INDEX-POSITION). You may also nest index list like
123 \(INDEX-NAME . INDEX-ALIST).
125 This function is called within a `save-excursion'.
127 The variable is buffer-local.")
128 (make-variable-buffer-local 'imenu-create-index-function)
130 (defvar imenu-prev-index-position-function 'beginning-of-defun
131 "Function for finding the next index position.
133 If `imenu-create-index-function' is set to
134 `imenu-default-create-index-function', then you must set this variable
135 to a function that will find the next index, looking backwards in the
136 file.
138 The function should leave point at the place to be connected to the
139 index and it should return nil when it doesn't find another index. ")
140 (make-variable-buffer-local 'imenu-prev-index-position-function)
142 (defvar imenu-extract-index-name-function nil
143 "Function for extracting the index name.
145 This function is called after the function pointed out by
146 `imenu-prev-index-position-function'.")
147 (make-variable-buffer-local 'imenu-extract-index-name-function)
150 ;;; Macro to display a progress message.
151 ;;; RELPOS is the relative position to display.
152 ;;; If RELPOS is nil, then the relative position in the buffer
153 ;;; is calculated.
154 ;;; PREVPOS is the variable in which we store the last position displayed.
155 (defmacro imenu-progress-message (prevpos &optional relpos reverse)
156 (` (and
157 imenu-scanning-message
158 (let ((pos (, (if relpos
159 relpos
160 (` (imenu--relative-position (, reverse)))))))
161 (if (, (if relpos t
162 (` (> pos (+ 5 (, prevpos))))))
163 (progn
164 (message imenu-scanning-message pos)
165 (setq (, prevpos) pos)))))))
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;;;;
170 ;;;; Some examples of functions utilizing the framework of this
171 ;;;; package.
172 ;;;;
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;; Return the current/previous sexp and the location of the sexp (it's
176 ;; beginning) without moving the point.
177 (defun imenu-example--name-and-position ()
178 (save-excursion
179 (forward-sexp -1)
180 (let ((beg (point))
181 (end (progn (forward-sexp) (point)))
182 (marker (make-marker)))
183 (set-marker marker beg)
184 (cons (buffer-substring beg end)
185 marker))))
188 ;;; Lisp
189 ;;;
191 (defun imenu-example--lisp-extract-index-name ()
192 ;; Example of a candidate for `imenu-extract-index-name-function'.
193 ;; This will generate a flat index of definitions in a lisp file.
194 (save-match-data
195 (and (looking-at "(def")
196 (condition-case nil
197 (progn
198 (down-list 1)
199 (forward-sexp 2)
200 (let ((beg (point))
201 (end (progn (forward-sexp -1) (point))))
202 (buffer-substring beg end)))
203 (error nil)))))
205 (defun imenu-example--create-lisp-index ()
206 ;; Example of a candidate for `imenu-create-index-function'.
207 ;; It will generate a nested index of definitions.
208 (let ((index-alist '())
209 (index-var-alist '())
210 (index-type-alist '())
211 (index-unknown-alist '())
212 prev-pos)
213 (goto-char (point-max))
214 (imenu-progress-message prev-pos 0)
215 ;; Search for the function
216 (while (beginning-of-defun)
217 (imenu-progress-message prev-pos nil t)
218 (save-match-data
219 (and (looking-at "(def")
220 (save-excursion
221 (down-list 1)
222 (cond
223 ((looking-at "def\\(var\\|const\\)")
224 (forward-sexp 2)
225 (push (imenu-example--name-and-position)
226 index-var-alist))
227 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
228 (forward-sexp 2)
229 (push (imenu-example--name-and-position)
230 index-alist))
231 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
232 (forward-sexp 2)
233 (if (= (char-after (1- (point))) ?\))
234 (progn
235 (forward-sexp -1)
236 (down-list 1)
237 (forward-sexp 1)))
238 (push (imenu-example--name-and-position)
239 index-type-alist))
241 (forward-sexp 2)
242 (push (imenu-example--name-and-position)
243 index-unknown-alist)))))))
244 (imenu-progress-message prev-pos 100)
245 (and index-var-alist
246 (push (cons (imenu-create-submenu-name "Variables") index-var-alist)
247 index-alist))
248 (and index-type-alist
249 (push (cons (imenu-create-submenu-name "Types") index-type-alist)
250 index-alist))
251 (and index-unknown-alist
252 (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist)
253 index-alist))
254 index-alist))
257 ;;; C
259 ;; Regular expression to find C functions
260 (defvar imenu-example--function-name-regexp-c
261 (concat
262 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
263 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
264 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
265 "\\([*&]+[ \t]*\\)?" ; pointer
266 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
269 (defun imenu-example--create-c-index (&optional regexp)
270 (let ((index-alist '())
271 prev-pos char)
272 (goto-char (point-min))
273 (imenu-progress-message prev-pos 0)
274 ;; Search for the function
275 (save-match-data
276 (while (re-search-forward
277 (or regexp imenu-example--function-name-regexp-c)
278 nil t)
279 (imenu-progress-message prev-pos)
280 (backward-up-list 1)
281 (save-excursion
282 (goto-char (scan-sexps (point) 1))
283 (setq char (following-char)))
284 ;; Skip this function name if it is a prototype declaration.
285 (if (not (eq char ?\;))
286 (push (imenu-example--name-and-position) index-alist))))
287 (imenu-progress-message prev-pos 100)
288 (nreverse index-alist)))
291 ;;; C++
293 ;; Example of an imenu-generic-expression
295 (defvar imenu-example--generic-c++-expression
296 (cons
297 ;; regular expression
298 (concat
299 "^" ; beginning of line is required
300 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a "template <...>"
301 "\\("
303 "\\(" ; >>looking for a function definition<<
304 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; type specs; there can be no
305 "\\([a-zA-Z0-9_:]+[ \t]+\\)?" ; more than 3 tokens, right?
307 "\\(" ; last type spec including */&
308 "[a-zA-Z0-9_:]+"
309 "\\([ \t]*[*&]+[ \t]*\\|[ \t]+\\)" ; either pointer/ref sign or whitespace
310 "\\)?" ; if there is a last type spec
312 "\\(" ; name; take that into the imenu entry
313 "[a-zA-Z0-9_:~]+" ; member function, ctor or dtor...
314 ; (may not contain * because then
315 ; "a::operator char*" would become "char*"!)
316 "\\|"
317 "\\([a-zA-Z0-9_:~]*::\\)?operator"
318 "[^a-zA-Z1-9_][^(]*" ; ...or operator
319 " \\)"
320 "[ \t]*([^)]*)[ \t\n]*[^ ;]" ; require something other than a ; after
321 ; the (...) to avoid prototypes. Can't
322 ; catch cases with () inside the parentheses
323 ; surrounding the parameters
324 ; (like "int foo(int a=bar()) {...}"
325 "\\)" ; <<looking for a function definition>>
327 "\\|"
329 "\\(" ; >>class decl<<
330 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; this is the string we want to get
331 "[ \t]*[:{]"
332 "\\)" ; <<class decl>>
334 "\\)")
335 ;; paren
336 (list 8 11))
337 "imenu generic expression for C++ mode in the form
338 \(REGEXP PAR).")
340 ;;;
341 ;;; TexInfo
342 ;;;
343 ;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
346 (defvar imenu-example--generic-texinfo-expression
347 (cons
348 (concat
349 "^@node[ \t]+"
350 "\\([^,\n]*\\)")
351 (list 1))
352 "imenu generic expression for TexInfo mode in the form
353 \(REGEXP PAR).
355 To overide this example, Either set 'imenu-generic-expression
356 or 'imenu-create-index-function")
358 ;;;
359 ;;; LaTex
360 ;;;
361 ;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
364 (defvar imenu-example--generic-latex-expression
365 (concat
366 "\\("
367 "%[ \t]*[0-9]+\\.[0-9]+[,;]?[ \t]?" ; i put numbers like 3.15 before my
368 ; \begin{equation}'s which tell me
369 ; the number the equation will get when
370 ; being printed.
371 "\\|"
372 "\\\\part{[^}]*}"
373 "\\|"
374 "\\\\chapter{[^}]*}"
375 "\\|"
376 "\\\\[a-zA-Z]*section{[^}]*}"
377 "\\)")
378 "imenu generic expression for LaTex mode in the form
379 \"REGEXP\".")
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;;; Internal variables
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; The item to use in the index for rescanning the buffer.
388 (defconst imenu--rescan-item '("*Rescan*" . -99))
390 ;; The latest buffer index.
391 ;; Buffer local.
392 (defvar imenu--index-alist nil)
393 (make-variable-buffer-local 'imenu--index-alist)
395 ;; History list for 'jump-to-function-in-buffer'.
396 ;; Buffer local.
397 (defvar imenu--history-list nil)
398 (make-variable-buffer-local 'imenu--history-list)
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 ;;; Internal support functions
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
407 ;;; Sort function
408 ;;; Sorts the items depending on their index name.
409 ;;; An item look like (NAME . POSITION).
411 (defun imenu--sort-by-name (item1 item2)
412 (string-lessp (car item1) (car item2)))
414 (defun imenu--relative-position (&optional reverse)
415 ;; Support function to calculate relative position in buffer
416 ;; Beginning of buffer is 0 and end of buffer is 100
417 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
418 (let ((pos (point))
419 (total (buffer-size)))
420 (and reverse (setq pos (- total pos)))
421 (if (> total 50000)
422 ;; Avoid overflow from multiplying by 100!
423 (/ (1- pos) (max (/ total 100) 1))
424 (/ (* 100 (1- pos)) (max total 1)))))
427 ;;; Function for suporting general looking submenu names.
428 ;;; Uses `imenu-submenu-name-format' for creating the name.
429 ;;; NAME is the base of the new submenu name.
431 (defun imenu-create-submenu-name (name)
432 (format imenu-submenu-name-format name))
434 ;; Split LIST into sublists of max length N.
435 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
436 (defun imenu--split (list n)
437 (let ((remain list)
438 (result '())
439 (sublist '())
440 (i 0))
441 (while remain
442 (push (pop remain) sublist)
443 (incf i)
444 (and (= i n)
445 ;; We have finished a sublist
446 (progn (push (nreverse sublist) result)
447 (setq i 0)
448 (setq sublist '()))))
449 ;; There might be a sublist (if the length of LIST mod n is != 0)
450 ;; that has to be added to the result list.
451 (and sublist
452 (push (nreverse sublist) result))
453 (nreverse result)))
456 ;;; Split a menu in to several menus.
458 (defun imenu--split-menu (menulist title)
459 (cons "Function menus"
460 (mapcar
461 (function
462 (lambda (menu)
463 (cons (format "(%s)" title) menu)))
464 (imenu--split menulist imenu-max-items))))
467 ;;; Find all items in this buffer that should be in the index.
468 ;;; Returns an alist on the form
469 ;;; ((NAME . POSITION) (NAME . POSITION) ...)
472 (defun imenu--make-index-alist ()
473 ;; Create a list for this buffer only when needed.
474 (or imenu--index-alist
475 ;; Get the index
476 (setq imenu--index-alist
477 (save-excursion
478 (funcall imenu-create-index-function))))
479 (or imenu--index-alist
480 (error "No items suitable for an index found in this buffer."))
481 ;; Add a rescan option to the index.
482 (cons imenu--rescan-item imenu--index-alist))
484 ;;; Find all markers in alist and makes
485 ;;; them point nowhere.
487 (defun imenu--cleanup (&optional alist)
488 ;; Sets the markers in imenu--index-alist
489 ;; point nowhere.
490 ;; if alist is provided use that list.
491 (and imenu--index-alist
492 (mapcar
493 (function
494 (lambda (item)
495 (cond
496 ((markerp (cdr item))
497 (set-marker (cdr item) nil))
498 ((listp (cdr item))
499 (imenu--cleanup (cdr item))))))
500 (if alist alist imenu--index-alist))
503 (defun imenu-default-create-index-function ()
504 "*Wrapper for index searching functions.
506 Moves point to end of buffer and then repeatedly calls
507 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
508 Their results are gathered into an index alist."
509 ;; These should really be done by setting imenu-create-index-function
510 ;; in these major modes. But save that change for later.
511 (cond ((and (fboundp imenu-prev-index-position-function)
512 (fboundp imenu-extract-index-name-function))
513 (let ((index-alist '())
514 prev-pos name)
515 (goto-char (point-max))
516 (imenu-progress-message prev-pos 0 t)
517 ;; Search for the function
518 (while (funcall imenu-prev-index-position-function)
519 (imenu-progress-message prev-pos nil t)
520 (save-excursion
521 (setq name (funcall imenu-extract-index-name-function)))
522 (and (stringp name)
523 (push (cons name (point)) index-alist)))
524 (imenu-progress-message prev-pos 100 t)
525 index-alist))
526 ;; Use generic expression if possible.
527 ((and imenu-generic-expression)
528 (imenu--generic-function imenu-generic-expression))
529 ;; Use supplied example functions
530 ((eq major-mode 'emacs-lisp-mode)
531 (imenu-example--create-lisp-index))
532 ((eq major-mode 'lisp-mode)
533 (imenu-example--create-lisp-index))
534 ((eq major-mode 'c++-mode)
535 (imenu--generic-function imenu-example--generic-c++-expression))
536 ((eq major-mode 'c-mode)
537 (imenu-example--create-c-index))
538 ((eq major-mode 'latex-mode)
539 (imenu--generic-function imenu-example--generic-latex-expression))
540 ((eq major-mode 'texinfo-mode)
541 (imenu--generic-function imenu-example--generic-texinfo-expression))
543 (error "The mode \"%s\" does not take full advantage of imenu.el yet."
544 mode-name))))
546 (defun imenu--replace-spaces (name replacement)
547 ;; Replace all spaces in NAME with REPLACEMENT.
548 ;; That second argument should be a string.
549 (mapconcat
550 (function
551 (lambda (ch)
552 (if (char-equal ch ?\ )
553 replacement
554 (char-to-string ch))))
555 name
556 ""))
558 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
559 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
560 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
561 ;; name and a space concatenated to the names of the children.
562 ;; Third argument PREFIX is for internal use only.
563 (mapcan
564 (function
565 (lambda (item)
566 (let* ((name (car item))
567 (pos (cdr item))
568 (new-prefix (and concat-names
569 (if prefix
570 (concat prefix imenu-level-separator name)
571 name))))
572 (cond
573 ((or (markerp pos) (numberp pos))
574 (list (cons new-prefix pos)))
576 (imenu--flatten-index-alist pos new-prefix))))))
577 index-alist))
580 ;;; Generic index gathering function.
582 (defun imenu--generic-extract-name (paren)
583 (let ((numofpar (1- (length paren)))
584 (parencount 0)
585 (par)
586 (index))
587 ;; Try until we get a match
588 (beginning-of-line)
589 (while (and (<= parencount numofpar)
590 (setq par (nth parencount paren))
591 (equal (match-beginning par) nil)
592 (equal (match-end par) nil))
593 (setq parencount (1+ parencount)))
594 (or (and
595 (<= parencount numofpar)
596 (setq index (buffer-substring (match-beginning par)
597 (match-end par))))
598 ;; take the whole match just in case.
599 (setq index (buffer-substring (match-beginning 0)
600 (match-end 0))))
601 index))
603 (defun imenu--generic-function (exp)
604 "Generic function for index gathering.
606 EXP can be either an regular expression or an alist in the form
607 \(REGEXP PAREN). "
609 (let ((index-alist '())
610 (regexp nil)
611 (paren nil)
612 prev-pos name)
613 (cond ((stringp exp)
614 (setq regexp exp)
615 (setq paren nil))
616 ((listp exp)
617 (setq regexp (car exp))
618 (setq paren (cdr exp)))
620 (error "Wrong type of argument.")))
621 (goto-char (point-max))
622 (imenu-progress-message prev-pos 0 t)
623 (while (re-search-backward regexp 1 t)
624 (imenu-progress-message prev-pos nil t)
625 (save-excursion
626 ;; If paren get sub expression
627 (or (and paren
628 (setq name (imenu--generic-extract-name paren)))
629 ;; get the whole expression
630 (beginning-of-line)
631 (setq name (buffer-substring (match-beginning 0)
632 (match-end 0)))))
633 (and (stringp name)
634 (push (cons name (point)) index-alist)))
635 (imenu-progress-message prev-pos 100 t)
636 index-alist))
638 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640 ;;; The main functions for this package!
642 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
644 (defun imenu--completion-buffer (index-alist &optional prompt)
645 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
647 Returns t for rescan and otherwise a position number."
648 ;; Create a list for this buffer only when needed.
649 (let (name choice
650 (prepared-index-alist
651 (mapcar
652 (function
653 (lambda (item)
654 (cons (imenu--replace-spaces (car item) imenu-space-replacement)
655 (cdr item))))
656 index-alist)))
657 (if (eq imenu-always-use-completion-buffer-p 'never)
658 (setq name (completing-read (or prompt "Index item: ")
659 prepared-index-alist
660 nil t nil 'imenu--history-list))
661 (save-window-excursion
662 ;; Display the completion buffer
663 (with-output-to-temp-buffer "*Completions*"
664 (display-completion-list
665 (all-completions "" prepared-index-alist )))
666 (let ((minibuffer-setup-hook
667 (function (lambda ()
668 (let ((buffer (current-buffer)))
669 (save-excursion
670 (set-buffer "*Completions*")
671 (setq completion-reference-buffer buffer)))))))
672 ;; Make a completion question
673 (setq name (completing-read (or prompt "Index item: ")
674 prepared-index-alist
675 nil t nil 'imenu--history-list)))))
676 (cond ((not (stringp name))
677 nil)
678 ((string= name (car imenu--rescan-item))
681 (setq choice (assoc name prepared-index-alist))
682 (if (listp (cdr choice))
683 (imenu--completion-buffer (cdr choice) prompt)
684 choice)))))
686 (defun imenu--mouse-menu (index-alist event &optional title)
687 "Let the user select from a buffer index from a mouse menu.
689 INDEX-ALIST is the buffer index and EVENT is a mouse event.
691 Returns t for rescan and otherwise a position number."
692 (let* ((menu (imenu--split-menu
693 (if imenu-sort-function
694 (sort
695 (let ((res nil)
696 (oldlist index-alist))
697 ;; Copy list method from the cl package `copy-list'
698 (while (consp oldlist) (push (pop oldlist) res))
699 (prog1 (nreverse res) (setcdr res oldlist)))
700 imenu-sort-function)
701 index-alist)
702 (or title (buffer-name))))
703 position)
704 (setq position (x-popup-menu event menu))
705 (cond
706 ((eq position nil)
707 position)
708 ((listp position)
709 (imenu--mouse-menu position event
710 (if title
711 (concat title imenu-level-separator
712 (car (rassq position index-alist)))
713 (car (rassq position index-alist)))))
714 ((= position (cdr imenu--rescan-item))
717 (rassq position index-alist)))))
719 (defun imenu-choose-buffer-index (&optional prompt alist)
720 "Let the user select from a buffer index and return the chosen index.
722 If the user originally activated this function with the mouse, a mouse
723 menu is used. Otherwise a completion buffer is used and the user is
724 prompted with PROMPT.
726 If you call this function with index alist ALIST, then it lets the user
727 select from ALIST.
729 With no index alist ALIST, it calls `imenu--make-index-alist' to
730 create the index alist.
732 If `imenu-always-use-completion-buffer-p' is non-nil, then the
733 completion buffer is always used, no matter if the mouse was used or
734 not.
736 The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
737 (let (index-alist
738 (mouse-triggered (listp last-nonmenu-event))
739 (result t) )
740 ;; If selected by mouse, see to that the window where the mouse is
741 ;; really is selected.
742 (and mouse-triggered
743 (let ((window (posn-window (event-start last-nonmenu-event))))
744 (or (framep window) (select-window window))))
745 ;; Create a list for this buffer only when needed.
746 (while (eq result t)
747 (setq index-alist (if alist alist (imenu--make-index-alist)))
748 (setq result
749 (if (and mouse-triggered
750 (not imenu-always-use-completion-buffer-p))
751 (imenu--mouse-menu index-alist last-nonmenu-event)
752 (imenu--completion-buffer index-alist prompt)))
753 (and (eq result t)
754 (imenu--cleanup)
755 (setq imenu--index-alist nil)))
756 result))
758 (defun imenu-add-to-menubar (name)
759 "Adds an \"imenu\" entry to the menubar for the
760 current local keymap.
761 NAME is the string naming the menu to be added.
762 See 'imenu' for more information."
763 (interactive "sMenu name: ")
764 (and window-system
765 (define-key (current-local-map) [menu-bar index]
766 (cons name 'imenu))))
768 ;;;###autoload
769 (defun imenu ()
770 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
771 See `imenu-choose-buffer-index' for more information."
772 (interactive)
773 (let ((index-item (save-restriction
774 (widen)
775 (imenu-choose-buffer-index))))
776 (and index-item
777 (progn
778 (push-mark)
779 (cond
780 ((markerp (cdr index-item))
781 (if (or ( > (marker-position (cdr index-item)) (point-min))
782 ( < (marker-position (cdr index-item)) (point-max)))
783 ;; widen if outside narrowing
784 (widen))
785 (goto-char (marker-position (cdr index-item))))
787 (if (or ( > (cdr index-item) (point-min))
788 ( < (cdr index-item) (point-max)))
789 ;; widen if outside narrowing
790 (widen))
791 (goto-char (cdr index-item))))))))
793 (provide 'imenu)
795 ;;; imenu.el ends here