annotate lisp/modes/imenu.el @ 16:0293115a14e9 r19-15b91

Import from CVS: tag r19-15b91
author cvs
date Mon, 13 Aug 2007 08:49:20 +0200
parents b82b59fe008d
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
1 ;;; imenu.el --- Framework for mode-specific buffer indexes.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
4
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
5 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
6 ;; Lars Lindberg <lli@sypro.cap.se>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
7 ;; Created: 8 Feb 1994
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
8 ;; Keywords: tools
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
9
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
11
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
15 ;; any later version.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
16
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
20 ;; General Public License for more details.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
21
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
26
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
27 ;;; Synched up with: FSF 19.34.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
28
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
29 ;;; Commentary:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
30
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
31 ;; Purpose of this package:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
32 ;; To present a framework for mode-specific buffer indexes.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
33 ;; A buffer index is an alist of names and buffer positions.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
34 ;; For instance all functions in a C-file and their positions.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
35 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
36 ;; How it works:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
37
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
38 ;; A mode-specific function is called to generate the index. It is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
39 ;; then presented to the user, who can choose from this index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
40 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
41 ;; The package comes with a set of example functions for how to
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
42 ;; utilize this package.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
43
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
44 ;; There are *examples* for index gathering functions/regular
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
45 ;; expressions for C/C++ and Lisp/Emacs Lisp but it is easy to
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
46 ;; customize for other modes. A function for jumping to the chosen
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
47 ;; index position is also supplied.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
48
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
49 ;;; Thanks goes to
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
50 ;; [simon] - Simon Leinen simon@lia.di.epfl.ch
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
51 ;; [dean] - Dean Andrews ada@unison.com
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
52 ;; [alon] - Alon Albert al@mercury.co.il
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
53 ;; [greg] - Greg Thompson gregt@porsche.visix.COM
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
54 ;; [wolfgang] - Wolfgang Bangerth zcg51122@rpool1.rus.uni-stuttgart.de
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
55 ;; [kai] - Kai Grossjohann grossjoh@linus.informatik.uni-dortmund.de
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
56 ;; [david] - David M. Smith dsmith@stats.adelaide.edu.au
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
57 ;; [christian] - Christian Egli Christian.Egli@hcsd.hac.com
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
58 ;; [karl] - Karl Fogel kfogel@floss.life.uiuc.edu
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
59
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
60 ;;; Code
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
61
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
62 (eval-when-compile (require 'cl))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
63
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
65 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
66 ;;; Customizable variables
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
67 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
69
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
70 (defvar imenu-auto-rescan nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
71 "*Non-nil means Imenu should always rescan the buffers.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
72
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
73 (defvar imenu-auto-rescan-maxout 60000
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
74 "* auto-rescan is disabled in buffers larger than this.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
75 This variable is buffer-local.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
76
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
77 (defvar imenu-always-use-completion-buffer-p nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
78 "*Set this to non-nil for displaying the index in a completion buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
79
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
80 Non-nil means always display the index in a completion buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
81 Nil means display the index as a mouse menu when the mouse was
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
82 used to invoke `imenu'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
83 `never' means never automatically display a listing of any kind.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
84
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
85 (defvar imenu-sort-function nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
86 "*The function to use for sorting the index mouse-menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
87
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
88 Affects only the mouse index menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
89
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
90 Set this to nil if you don't want any sorting (faster).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
91 The items in the menu are then presented in the order they were found
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
92 in the buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
93
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
94 Set it to `imenu--sort-by-name' if you want alphabetic sorting.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
95
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
96 The function should take two arguments and return T if the first
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
97 element should come before the second. The arguments are cons cells;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
98 \(NAME . POSITION). Look at `imenu--sort-by-name' for an example.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
99
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
100 (defvar imenu-max-items 25
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
101 "*Maximum number of elements in an mouse menu for Imenu.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
102
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
103 (defvar imenu-scanning-message "Scanning buffer for index (%3d%%)"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
104 "*Progress message during the index scanning of the buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
105 If non-nil, user gets a message during the scanning of the buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
106
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
107 Relevant only if the mode-specific function that creates the buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
108 index use `imenu-progress-message'.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
109
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
110 (defvar imenu-space-replacement "^"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
111 "*The replacement string for spaces in index names.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
112 Used when presenting the index in a completion-buffer to make the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
113 names work as tokens.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
114
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
115 (defvar imenu-level-separator ":"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
116 "*The separator between index names of different levels.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
117 Used for making mouse-menu titles and for flattening nested indexes
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
118 with name concatenation.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
119
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
120 ;;;###autoload
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
121 (defvar imenu-generic-expression nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
122 "The regex pattern to use for creating a buffer index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
123
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
124 If non-nil this pattern is passed to `imenu-create-index-with-pattern'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
125 to create a buffer index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
126
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
127 It is an alist with elements that look like this: (MENU-TITLE
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
128 REGEXP INDEX).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
129
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
130 MENU-TITLE is a string used as the title for the submenu or nil if the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
131 entries are not nested.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
132
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
133 REGEXP is a regexp that should match a construct in the buffer that is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
134 to be displayed in the menu; i.e., function or variable definitions,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
135 etc. It contains a substring which is the name to appear in the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
136 menu. See the info section on Regexps for more information.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
137
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
138 INDEX points to the substring in REGEXP that contains the name (of the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
139 function, variable or type) that is to appear in the menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
140
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
141 For emacs-lisp-mode for example PATTERN would look like:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
142
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
143 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
144 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
145 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9+]+\\\\)\" 2))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
146
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
147 The variable is buffer-local.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
148
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
149 ;;;###autoload
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
150 (make-variable-buffer-local 'imenu-generic-expression)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
151
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
152 ;;;; Hooks
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
153
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
154 (defvar imenu-create-index-function 'imenu-default-create-index-function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
155 "The function to use for creating a buffer index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
156
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
157 It should be a function that takes no arguments and returns an index
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
158 of the current buffer as an alist. The elements in the alist look
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
159 like: (INDEX-NAME . INDEX-POSITION). You may also nest index list like
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
160 \(INDEX-NAME . INDEX-ALIST).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
161
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
162 This function is called within a `save-excursion'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
163
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
164 The variable is buffer-local.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
165 (make-variable-buffer-local 'imenu-create-index-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
166
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
167 (defvar imenu-prev-index-position-function 'beginning-of-defun
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
168 "Function for finding the next index position.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
169
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
170 If `imenu-create-index-function' is set to
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
171 `imenu-default-create-index-function', then you must set this variable
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
172 to a function that will find the next index, looking backwards in the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
173 file.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
174
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
175 The function should leave point at the place to be connected to the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
176 index and it should return nil when it doesn't find another index.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
177 (make-variable-buffer-local 'imenu-prev-index-position-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
178
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
179 (defvar imenu-extract-index-name-function nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
180 "Function for extracting the index name.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
181
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
182 This function is called after the function pointed out by
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
183 `imenu-prev-index-position-function'.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
184 (make-variable-buffer-local 'imenu-extract-index-name-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
185
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
186 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
187 ;;; Macro to display a progress message.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
188 ;;; RELPOS is the relative position to display.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
189 ;;; If RELPOS is nil, then the relative position in the buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
190 ;;; is calculated.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
191 ;;; PREVPOS is the variable in which we store the last position displayed.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
192 (defmacro imenu-progress-message (prevpos &optional relpos reverse)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
193 (` (and
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
194 imenu-scanning-message
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
195 (let ((pos (, (if relpos
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
196 relpos
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
197 (` (imenu--relative-position (, reverse)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
198 (if (, (if relpos t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
199 (` (> pos (+ 5 (, prevpos))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
200 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
201 (message imenu-scanning-message pos)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
202 (setq (, prevpos) pos)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
203
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
204
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
206 ;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
207 ;;;; Some examples of functions utilizing the framework of this
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
208 ;;;; package.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
209 ;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
211
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
212 ;; Return the current/previous sexp and the location of the sexp (its
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
213 ;; beginning) without moving the point.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
214 (defun imenu-example--name-and-position ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
215 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
216 (forward-sexp -1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
217 (let ((beg (point))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
218 (end (progn (forward-sexp) (point)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
219 (marker (make-marker)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
220 (set-marker marker beg)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
221 (cons (buffer-substring beg end)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
222 marker))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
223
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
224 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
225 ;;; Lisp
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
226 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
227
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
228 (defun imenu-example--lisp-extract-index-name ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
229 ;; Example of a candidate for `imenu-extract-index-name-function'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
230 ;; This will generate a flat index of definitions in a lisp file.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
231 (save-match-data
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
232 (and (looking-at "(def")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
233 (condition-case nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
234 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
235 (down-list 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
236 (forward-sexp 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
237 (let ((beg (point))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
238 (end (progn (forward-sexp -1) (point))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
239 (buffer-substring beg end)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
240 (error nil)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
241
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
242 (defun imenu-example--create-lisp-index ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
243 ;; Example of a candidate for `imenu-create-index-function'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
244 ;; It will generate a nested index of definitions.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
245 (let ((index-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
246 (index-var-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
247 (index-type-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
248 (index-unknown-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
249 prev-pos)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
250 (goto-char (point-max))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
251 (imenu-progress-message prev-pos 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
252 ;; Search for the function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
253 (while (beginning-of-defun)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
254 (imenu-progress-message prev-pos nil t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
255 (save-match-data
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
256 (and (looking-at "(def")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
257 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
258 (down-list 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
259 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
260 ((looking-at "def\\(var\\|const\\)")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
261 (forward-sexp 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
262 (push (imenu-example--name-and-position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
263 index-var-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
264 ((looking-at "def\\(un\\|subst\\|macro\\|advice\\)")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
265 (forward-sexp 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
266 (push (imenu-example--name-and-position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
267 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
268 ((looking-at "def\\(type\\|struct\\|class\\|ine-condition\\)")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
269 (forward-sexp 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
270 (if (= (char-after (1- (point))) ?\))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
271 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
272 (forward-sexp -1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
273 (down-list 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
274 (forward-sexp 1)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
275 (push (imenu-example--name-and-position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
276 index-type-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
277 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
278 (forward-sexp 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
279 (push (imenu-example--name-and-position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
280 index-unknown-alist)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
281 (imenu-progress-message prev-pos 100)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
282 (and index-var-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
283 (push (cons "Variables" index-var-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
284 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
285 (and index-type-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
286 (push (cons "Types" index-type-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
287 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
288 (and index-unknown-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
289 (push (cons "Syntax-unknown" index-unknown-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
290 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
291 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
292
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
293 ;; Regular expression to find C functions
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
294 (defvar imenu-example--function-name-regexp-c
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
295 (concat
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
296 "^[a-zA-Z0-9]+[ \t]?" ; type specs; there can be no
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
297 "\\([a-zA-Z0-9_*]+[ \t]+\\)?" ; more than 3 tokens, right?
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
298 "\\([a-zA-Z0-9_*]+[ \t]+\\)?"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
299 "\\([*&]+[ \t]*\\)?" ; pointer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
300 "\\([a-zA-Z0-9_*]+\\)[ \t]*(" ; name
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
301 ))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
302
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
303 (defun imenu-example--create-c-index (&optional regexp)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
304 (let ((index-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
305 prev-pos char)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
306 (goto-char (point-min))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
307 (imenu-progress-message prev-pos 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
308 ;; Search for the function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
309 (save-match-data
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
310 (while (re-search-forward
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
311 (or regexp imenu-example--function-name-regexp-c)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
312 nil t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
313 (imenu-progress-message prev-pos)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
314 (backward-up-list 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
315 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
316 (goto-char (scan-sexps (point) 1))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
317 (setq char (following-char)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
318 ;; Skip this function name if it is a prototype declaration.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
319 (if (not (eq char ?\;))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
320 (push (imenu-example--name-and-position) index-alist))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
321 (imenu-progress-message prev-pos 100)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
322 (nreverse index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
323
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
324
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
326 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
327 ;;; Internal variables
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
328 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
330
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
331 ;; The item to use in the index for rescanning the buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
332 (defconst imenu--rescan-item '("*Rescan*" . -99))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
333
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
334 ;; The latest buffer index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
335 ;; Buffer local.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
336 (defvar imenu--index-alist nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
337 (make-variable-buffer-local 'imenu--index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
338
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
339 ;; The latest buffer index used to update the menu bar menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
340 (defvar imenu--last-menubar-index-alist nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
341 (make-variable-buffer-local 'imenu--last-menubar-index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
342
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
343 ;; History list for 'jump-to-function-in-buffer'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
344 ;; Making this buffer local caused it not to work!
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
345 (defvar imenu--history-list nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
346
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
348 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
349 ;;; Internal support functions
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
350 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
352
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
353 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
354 ;;; Sort function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
355 ;;; Sorts the items depending on their index name.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
356 ;;; An item look like (NAME . POSITION).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
357 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
358 (defun imenu--sort-by-name (item1 item2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
359 (string-lessp (car item1) (car item2)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
360
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
361 (defun imenu--relative-position (&optional reverse)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
362 ;; Support function to calculate relative position in buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
363 ;; Beginning of buffer is 0 and end of buffer is 100
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
364 ;; If REVERSE is non-nil then the beginning is 100 and the end is 0.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
365 (let ((pos (point))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
366 (total (buffer-size)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
367 (and reverse (setq pos (- total pos)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
368 (if (> total 50000)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
369 ;; Avoid overflow from multiplying by 100!
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
370 (/ (1- pos) (max (/ total 100) 1))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
371 (/ (* 100 (1- pos)) (max total 1)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
372
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
373 ;; Split LIST into sublists of max length N.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
374 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
375 (defun imenu--split (list n)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
376 (let ((remain list)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
377 (result '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
378 (sublist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
379 (i 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
380 (while remain
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
381 (push (pop remain) sublist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
382 (incf i)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
383 (and (= i n)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
384 ;; We have finished a sublist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
385 (progn (push (nreverse sublist) result)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
386 (setq i 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
387 (setq sublist '()))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
388 ;; There might be a sublist (if the length of LIST mod n is != 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
389 ;; that has to be added to the result list.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
390 (and sublist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
391 (push (nreverse sublist) result))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
392 (nreverse result)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
393
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
394 ;;; Split the alist MENULIST into a nested alist, if it is long enough.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
395 ;;; In any case, add TITLE to the front of the alist.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
396 (defun imenu--split-menu (menulist title)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
397 (if (> (length menulist) imenu-max-items)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
398 (let ((count 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
399 (cons title
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
400 (mapcar
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
401 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
402 (lambda (menu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
403 (cons (format "(%s-%d)" title (setq count (1+ count)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
404 menu)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
405 (imenu--split menulist imenu-max-items))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
406 (cons title menulist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
407
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
408 ;;; Split up each long alist that are nested within ALIST
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
409 ;;; into nested alists.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
410 (defun imenu--split-submenus (alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
411 (mapcar (function (lambda (elt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
412 (if (and (consp elt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
413 (stringp (car elt))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
414 (listp (cdr elt)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
415 (imenu--split-menu (cdr elt) (car elt))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
416 elt)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
417 alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
418
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
419 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
420 ;;; Find all items in this buffer that should be in the index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
421 ;;; Returns an alist on the form
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
422 ;;; ((NAME . POSITION) (NAME . POSITION) ...)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
423 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
424
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
425 (defun imenu--make-index-alist (&optional noerror)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
426 ;; Create a list for this buffer only when needed.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
427 (or (and imenu--index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
428 (or (not imenu-auto-rescan)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
429 (and imenu-auto-rescan
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
430 (> (buffer-size) imenu-auto-rescan-maxout))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
431 ;; Get the index
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
432 (setq imenu--index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
433 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
434 (funcall imenu-create-index-function))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
435 (or imenu--index-alist noerror
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
436 (error "No items suitable for an index found in this buffer"))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
437 (or imenu--index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
438 (setq imenu--index-alist (list nil)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
439 ;; Add a rescan option to the index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
440 (cons imenu--rescan-item imenu--index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
441 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
442 ;;; Find all markers in alist and makes
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
443 ;;; them point nowhere.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
444 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
445 (defun imenu--cleanup (&optional alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
446 ;; Sets the markers in imenu--index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
447 ;; point nowhere.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
448 ;; if alist is provided use that list.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
449 (or alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
450 (setq alist imenu--index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
451 (and alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
452 (mapcar
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
453 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
454 (lambda (item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
455 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
456 ((markerp (cdr item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
457 (set-marker (cdr item) nil))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
458 ((consp (cdr item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
459 (imenu--cleanup (cdr item))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
460 alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
461 t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
462
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
463 (defun imenu--create-keymap-2 (alist counter &optional commands)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
464 (let ((map nil))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
465 (mapcar
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
466 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
467 (lambda (item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
468 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
469 ((listp (cdr item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
470 (append (list (setq counter (1+ counter))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
471 (car item) 'keymap (car item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
472 (imenu--create-keymap-2 (cdr item) (+ counter 10) commands)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
473 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
474 (let ((end (if commands `(lambda () (interactive)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
475 (imenu--menubar-select ',item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
476 (cons '(nil) t))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
477 (cons (car item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
478 (cons (car item) end))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
479 )))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
480 alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
481
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
482 ;; If COMMANDS is non-nil, make a real keymap
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
483 ;; with a real command used as the definition.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
484 ;; If it is nil, make something suitable for x-popup-menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
485 (defun imenu--create-keymap-1 (title alist &optional commands)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
486 (append (list 'keymap title) (imenu--create-keymap-2 alist 0 commands)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
487
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
488
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
489 (defun imenu--in-alist (str alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
490 "Check whether the string STR is contained in multi-level ALIST."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
491 (let (elt head tail res)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
492 (setq res nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
493 (while alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
494 (setq elt (car alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
495 tail (cdr elt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
496 alist (cdr alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
497 head (car elt))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
498 (if (string= str head)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
499 (setq alist nil res elt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
500 (if (and (listp tail)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
501 (setq res (imenu--in-alist str tail)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
502 (setq alist nil))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
503 res))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
504
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
505 (defun imenu-default-create-index-function ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
506 "*Wrapper for index searching functions.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
507
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
508 Moves point to end of buffer and then repeatedly calls
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
509 `imenu-prev-index-position-function' and `imenu-extract-index-name-function'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
510 Their results are gathered into an index alist."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
511 ;; These should really be done by setting imenu-create-index-function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
512 ;; in these major modes. But save that change for later.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
513 (cond ((and (fboundp imenu-prev-index-position-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
514 (fboundp imenu-extract-index-name-function))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
515 (let ((index-alist '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
516 prev-pos name)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
517 (goto-char (point-max))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
518 (imenu-progress-message prev-pos 0 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
519 ;; Search for the function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
520 (while (funcall imenu-prev-index-position-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
521 (imenu-progress-message prev-pos nil t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
522 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
523 (setq name (funcall imenu-extract-index-name-function)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
524 (and (stringp name)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
525 (push (cons name (point)) index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
526 (imenu-progress-message prev-pos 100 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
527 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
528 ;; Use generic expression if possible.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
529 ((and imenu-generic-expression)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
530 (imenu--generic-function imenu-generic-expression))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
531 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
532 (error "The mode \"%s\" does not take full advantage of imenu.el yet."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
533 mode-name))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
534
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
535 (defun imenu--replace-spaces (name replacement)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
536 ;; Replace all spaces in NAME with REPLACEMENT.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
537 ;; That second argument should be a string.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
538 (mapconcat
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
539 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
540 (lambda (ch)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
541 (if (char-equal ch ?\ )
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
542 replacement
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
543 (char-to-string ch))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
544 name
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
545 ""))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
546
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
547 (defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
548 ;; Takes a nested INDEX-ALIST and returns a flat index alist.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
549 ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
550 ;; name and a space concatenated to the names of the children.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
551 ;; Third argument PREFIX is for internal use only.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
552 (mapcan
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
553 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
554 (lambda (item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
555 (let* ((name (car item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
556 (pos (cdr item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
557 (new-prefix (and concat-names
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
558 (if prefix
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
559 (concat prefix imenu-level-separator name)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
560 name))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
561 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
562 ((or (markerp pos) (numberp pos))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
563 (list (cons new-prefix pos)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
564 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
565 (imenu--flatten-index-alist pos new-prefix))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
566 index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
567
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
568 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
569 ;;; Generic index gathering function.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
570 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
571
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
572 (defun imenu--generic-function (patterns)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
573 ;; Built on some ideas that Erik Naggum <erik@naggum.no> once posted
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
574 ;; to comp.emacs
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
575 "Return an index of the current buffer as an alist.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
576
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
577 PATTERN is an alist with elements that look like this: (MENU-TITLE
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
578 REGEXP INDEX).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
579
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
580 MENU-TITLE is a string used as the title for the submenu or nil if the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
581 entries are not nested.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
582
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
583 REGEXP is a regexp that should match a construct in the buffer that is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
584 to be displayed in the menu; i.e., function or variable definitions,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
585 etc. It contains a substring which is the name to appear in the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
586 menu. See the info section on Regexps for more information.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
587
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
588 INDEX points to the substring in REGEXP that contains the name (of the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
589 function, variable or type) that is to appear in the menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
590
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
591 For emacs-lisp-mode for example PATTERN would look like:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
592
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
593 '((nil \"^\\\\s-*(def\\\\(un\\\\|subst\\\\|macro\\\\|advice\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
594 (\"*Vars*\" \"^\\\\s-*(def\\\\(var\\\\|const\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
595 (\"*Types*\" \"^\\\\s-*(def\\\\(type\\\\|struct\\\\|class\\\\|ine-condition\\\\)\\\\s-+\\\\([-A-Za-z0-9]+\\\\)\" 2))'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
596
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
597 Returns an index of the current buffer as an alist. The elements in
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
598 the alist look like: (INDEX-NAME . INDEX-POSITION). They may also be
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
599 nested index lists like (INDEX-NAME . INDEX-ALIST) depending on
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
600 pattern.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
601
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
602 \(imenu--generic-function PATTERN\)."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
603
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
604 (let ((index-alist (list 'dummy))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
605 (found nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
606 (global-regexp
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
607 (concat "\\("
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
608 (mapconcat
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
609 (function (lambda (pattern) (identity (cadr pattern))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
610 patterns "\\)\\|\\(")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
611 "\\)"))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
612 prev-pos)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
613
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
614 (goto-char (point-max))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
615 (imenu-progress-message prev-pos 0 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
616 (save-match-data
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
617 (while (re-search-backward global-regexp nil t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
618 (imenu-progress-message prev-pos nil t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
619 (setq found nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
620 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
621 (goto-char (match-beginning 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
622 (mapcar
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
623 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
624 (lambda (pat)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
625 (let ((menu-title (car pat))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
626 (regexp (cadr pat))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
627 (index (caddr pat)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
628 (if (and (not found) ; Only allow one entry;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
629 (looking-at regexp))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
630 (let ((beg (match-beginning index))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
631 (end (match-end index)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
632 (setq found t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
633 (push
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
634 (cons (buffer-substring-no-properties beg end) beg)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
635 (cdr
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
636 (or (assoc menu-title index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
637 (car (push
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
638 (cons menu-title '())
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
639 index-alist))))))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
640 patterns))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
641 (imenu-progress-message prev-pos 100 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
642 (let ((main-element (assq nil index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
643 (nconc (delq main-element (delq 'dummy index-alist)) main-element))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
644
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
645 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
646 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
647 ;;; The main functions for this package!
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
648 ;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
649 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
650
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
651 (defun imenu--completion-buffer (index-alist &optional prompt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
652 "Let the user select from INDEX-ALIST in a completion buffer with PROMPT.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
653
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
654 Returns t for rescan and otherwise a position number."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
655 ;; Create a list for this buffer only when needed.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
656 (let (name choice
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
657 (prepared-index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
658 (mapcar
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
659 (function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
660 (lambda (item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
661 (cons (imenu--replace-spaces (car item) imenu-space-replacement)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
662 (cdr item))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
663 index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
664 (if (eq imenu-always-use-completion-buffer-p 'never)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
665 (setq name (completing-read (or prompt "Index item: ")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
666 prepared-index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
667 nil t nil 'imenu--history-list))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
668 (save-window-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
669 ;; Display the completion buffer
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
670 (with-output-to-temp-buffer "*Completions*"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
671 (display-completion-list
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
672 (all-completions "" prepared-index-alist )))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
673 (let ((minibuffer-setup-hook
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
674 (function (lambda ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
675 (let ((buffer (current-buffer)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
676 (save-excursion
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
677 (set-buffer "*Completions*")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
678 (setq completion-reference-buffer buffer)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
679 ;; Make a completion question
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
680 (setq name (completing-read (or prompt "Index item: ")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
681 prepared-index-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
682 nil t nil 'imenu--history-list)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
683 (cond ((not (stringp name))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
684 nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
685 ((string= name (car imenu--rescan-item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
686 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
687 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
688 (setq choice (assoc name prepared-index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
689 (if (listp (cdr choice))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
690 (imenu--completion-buffer (cdr choice) prompt)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
691 choice)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
692
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
693 (defun imenu--mouse-menu (index-alist event &optional title)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
694 "Let the user select from a buffer index from a mouse menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
695
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
696 INDEX-ALIST is the buffer index and EVENT is a mouse event.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
697
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
698 Returns t for rescan and otherwise a position number."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
699 (setq index-alist (imenu--split-submenus index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
700 (let* ((menu (imenu--split-menu
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
701 (if imenu-sort-function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
702 (sort
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
703 (let ((res nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
704 (oldlist index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
705 ;; Copy list method from the cl package `copy-list'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
706 (while (consp oldlist) (push (pop oldlist) res))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
707 (prog1 (nreverse res) (setcdr res oldlist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
708 imenu-sort-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
709 index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
710 (or title (buffer-name))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
711 position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
712 (setq menu (imenu--create-keymap-1 (car menu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
713 (if (< 1 (length (cdr menu)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
714 (cdr menu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
715 (cdr (cadr menu)))))
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
716 (if (fboundp 'x-popup-menu)
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
717 (setq position (x-popup-menu event menu))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
718 (setq position (let ((val (get-popup-menu-response
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
719 (cons ""
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
720 (mapcar
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
721 (function
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
722 (lambda (x)
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
723 (vector (car x) (list (car x)) t)))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
724 menu)))))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
725 (setq val (and val
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
726 (listp (event-object val))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
727 (stringp (car-safe (event-object val)))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
728 (car (event-object val))))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
729 (cdr (assoc val choices)))))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
730 (cond ((and (listp position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
731 (numberp (car position))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
732 (stringp (nth (1- (length position)) position)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
733 (setq position (nth (1- (length position)) position)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
734 ((and (stringp (car position))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
735 (null (cdr position)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
736 (setq position (car position))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
737 (cond ((eq position nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
738 position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
739 ((listp position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
740 (imenu--mouse-menu position event
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
741 (if title
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
742 (concat title imenu-level-separator
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
743 (car (rassq position index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
744 (car (rassq position index-alist)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
745 ((stringp position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
746 (or (string= position (car imenu--rescan-item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
747 (imenu--in-alist position index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
748 ((or (= position (cdr imenu--rescan-item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
749 (and (stringp position)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
750 (string= position (car imenu--rescan-item))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
751 t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
752 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
753 (rassq position index-alist)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
754
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
755 (defun imenu-choose-buffer-index (&optional prompt alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
756 "Let the user select from a buffer index and return the chosen index.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
757
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
758 If the user originally activated this function with the mouse, a mouse
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
759 menu is used. Otherwise a completion buffer is used and the user is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
760 prompted with PROMPT.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
761
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
762 If you call this function with index alist ALIST, then it lets the user
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
763 select from ALIST.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
764
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
765 With no index alist ALIST, it calls `imenu--make-index-alist' to
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
766 create the index alist.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
767
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
768 If `imenu-always-use-completion-buffer-p' is non-nil, then the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
769 completion buffer is always used, no matter if the mouse was used or
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
770 not.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
771
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
772 The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
773 (let (index-alist
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
774 ;; XEmacs
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
775 (mouse-triggered (or (button-press-event-p last-command-event)
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
776 (button-release-event-p last-command-event)
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
777 (menu-event-p last-command-event)))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
778 ;;(mouse-triggered (listp last-nonmenu-event))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
779 (result t) )
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
780 ;; If selected by mouse, see to that the window where the mouse is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
781 ;; really is selected.
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
782 ;(and mouse-triggered
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
783 ;(not (equal last-nonmenu-event '(menu-bar)))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
784 ;(let ((window (posn-window (event-start last-nonmenu-event))))
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
785 ;(or (framep window) (null window) (select-window window))))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
786 ;; Create a list for this buffer only when needed.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
787 (while (eq result t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
788 (setq index-alist (if alist alist (imenu--make-index-alist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
789 (setq result
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
790 (if (and mouse-triggered
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
791 (not imenu-always-use-completion-buffer-p))
4
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
792 ;(imenu--mouse-menu index-alist last-nonmenu-event)
b82b59fe008d Import from CVS: tag r19-15b3
cvs
parents: 2
diff changeset
793 (imenu--mouse-menu index-alist last-command-event)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
794 (imenu--completion-buffer index-alist prompt)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
795 (and (eq result t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
796 (imenu--cleanup)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
797 (setq imenu--index-alist nil)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
798 result))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
799
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
800 ;;;###autoload
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
801 (defun imenu-add-to-menubar (name)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
802 "Adds an `imenu' entry to the menu bar for the current buffer.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
803 NAME is a string used to name the menu bar item.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
804 See the command `imenu' for more information."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
805 (interactive "sImenu menu item name: ")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
806 (let ((newmap (make-sparse-keymap))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
807 (menu-bar (lookup-key (current-local-map) [menu-bar])))
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
808 (when menu-bar
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
809 (define-key newmap [menu-bar]
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
810 (append (make-sparse-keymap) menu-bar))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
811 (define-key newmap [menu-bar index]
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
812 (cons name (nconc (make-sparse-keymap "Imenu")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
813 (make-sparse-keymap))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 4
diff changeset
814 (use-local-map (append newmap (current-local-map)))))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
815 (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
816
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
817 (defvar imenu-buffer-menubar nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
818
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
819 (defun imenu-update-menubar ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
820 (and (current-local-map)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
821 (keymapp (lookup-key (current-local-map) [menu-bar index]))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
822 (let ((index-alist (imenu--make-index-alist t)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
823 ;; Don't bother updating if the index-alist has not changed
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
824 ;; since the last time we did it.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
825 (or (equal index-alist imenu--last-menubar-index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
826 (let (menu menu1 old)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
827 (setq imenu--last-menubar-index-alist index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
828 (setq index-alist (imenu--split-submenus index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
829 (setq menu (imenu--split-menu
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
830 (if imenu-sort-function
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
831 (sort
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
832 (let ((res nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
833 (oldlist index-alist))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
834 ;; Copy list method from the cl package `copy-list'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
835 (while (consp oldlist) (push (pop oldlist) res))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
836 (prog1 (nreverse res) (setcdr res oldlist)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
837 imenu-sort-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
838 index-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
839 (buffer-name)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
840 (setq menu1 (imenu--create-keymap-1 (car menu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
841 (if (< 1 (length (cdr menu)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
842 (cdr menu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
843 (cdr (car (cdr menu))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
844 t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
845 (setq old (lookup-key (current-local-map) [menu-bar index]))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
846 (setcdr old (cdr menu1)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
847
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
848 (defun imenu--menubar-select (item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
849 "Use Imenu to select the function or variable named in this menu item."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
850 (if (equal item '("*Rescan*" . -99))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
851 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
852 (imenu--cleanup)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
853 (setq imenu--index-alist nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
854 (imenu-update-menubar))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
855 (imenu item)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
856
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
857 ;;;###autoload
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
858 (defun imenu (index-item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
859 "Jump to a place in the buffer chosen using a buffer menu or mouse menu.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
860 See `imenu-choose-buffer-index' for more information."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
861 (interactive
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
862 (list (save-restriction
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
863 (widen)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
864 (imenu-choose-buffer-index))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
865 ;; Convert a string to an alist element.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
866 (if (stringp index-item)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
867 (setq index-item (assoc index-item (imenu--make-index-alist))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
868 (and index-item
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
869 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
870 (push-mark)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
871 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
872 ((markerp (cdr index-item))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
873 (if (or ( > (marker-position (cdr index-item)) (point-min))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
874 ( < (marker-position (cdr index-item)) (point-max)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
875 ;; widen if outside narrowing
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
876 (widen))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
877 (goto-char (marker-position (cdr index-item))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
878 (t
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
879 (if (or ( > (cdr index-item) (point-min))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
880 ( < (cdr index-item) (point-max)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
881 ;; widen if outside narrowing
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
882 (widen))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
883 (goto-char (cdr index-item)))))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
884
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
885 (provide 'imenu)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
886
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
887 ;;; imenu.el ends here
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
888