]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-menus.el
(Info-default-directory-list): Use configdir twice.
[gnu-emacs] / lisp / progmodes / cc-menus.el
1 ;;; cc-menus.el --- imenu support for CC Mode
2
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
4
5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman
8 ;; Maintainer: cc-mode-help@python.org
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
10 ;; Version: See cc-mode.el
11 ;; Keywords: c languages oop
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 \f
31 ;; imenu integration
32 (eval-when-compile (require 'imenu))
33
34 (defvar cc-imenu-c-prototype-macro-regexp nil
35 "RE matching macro names used to conditionally specify function prototypes.
36
37 For example:
38
39 #ifdef __STDC__
40 #define _P(x) x
41 #else
42 #define _P(x) /*nothing*/
43 #endif
44
45 int main _P( (int argc, char *argv[]) )
46
47 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
48
49 (defvar cc-imenu-c++-generic-expression
50 (`
51 (
52 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
53 ;; will be incorrectly recognised as function `new ()' because the regexps
54 ;; work by backtracking from the end of the definition.
55 (nil
56 (,
57 (concat
58 "^\\<.*"
59 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
60 ; (note: this can be `\n')
61 "\\("
62 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
63 "operator\\>[ \t]*"
64 "\\(()\\|[^(]*\\)" ; special case for `()' operator
65 "\\)"
66
67 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
68 ; require something other than
69 ; a `;' after the (...) to
70 ; avoid prototypes. Can't
71 ; catch cases with () inside
72 ; the parentheses surrounding
73 ; the parameters. e.g.:
74 ; `int foo(int a=bar()) {...}'
75 )) 1)
76 ;; Special case to match a line like `main() {}'
77 ;; e.g. no return type, not even on the previous line.
78 (nil
79 (,
80 (concat
81 "^"
82 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
83 "[ \t]*(" ; see above, BUT
84 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
85 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
86 )) 1)
87 ;; General function name regexp
88 (nil
89 (,
90 (concat
91 "^\\<.*" ; line MUST start with word char
92 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
93 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
94 "[ \t]*(" ; see above, BUT
95 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
96 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
97 )) 1)
98 ;; Special case for definitions using phony prototype macros like:
99 ;; `int main _PROTO( (int argc,char *argv[]) )'.
100 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
101 ;; Only supported in c-code, so no `:<>~' chars in function name!
102 (,@ (if cc-imenu-c-prototype-macro-regexp
103 (` ((nil
104 (,
105 (concat
106 "^\\<.*" ; line MUST start with word char
107 "[^a-zA-Z0-9_]" ; match any non-identifier char
108 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
109 "[ \t]*" ; whitespace before macro name
110 cc-imenu-c-prototype-macro-regexp
111 "[ \t]*(" ; ws followed by first paren.
112 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
113 )) 1)))))
114 ;; Class definitions
115 ("Class"
116 (, (concat
117 "^" ; beginning of line is required
118 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
119 "class[ \t]+"
120 "\\([a-zA-Z0-9_]+\\)" ; the string we want to get
121 "[ \t]*[:{]"
122 )) 2)))
123 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
124
125 (defvar cc-imenu-c-generic-expression
126 cc-imenu-c++-generic-expression
127 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
128
129 (defvar cc-imenu-java-generic-expression
130 (`
131 ((nil
132 (,
133 (concat
134 "^\\([ \t]\\)*"
135 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
136 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
137 "\\([A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
138 "\\([ \t]\\)"
139 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
140 "\\([ \t]*\\)("
141 "\\([][a-zA-Z,_1-9\n \t]*\\)" ; arguments
142 ")[ \t]*"
143 ; "[^;(]"
144 "[,a-zA-Z_1-9\n \t]*{"
145 )) 6)))
146 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
147
148 ;; *Warning for cc-mode developers*
149 ;;
150 ;; `cc-imenu-objc-generic-expression' elements depend on
151 ;; `cc-imenu-c++-generic-expression'. So if you change this
152 ;; expression, you need to change following variables,
153 ;; `cc-imenu-objc-generic-expression-*-index',
154 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
155 ;; order to know where the each regexp *group \\(foobar\\)* elements
156 ;; are started.
157 ;;
158 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
159 ;; being initialized.
160 ;;
161
162 ;; Internal variables
163 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
164 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
165 (defvar cc-imenu-objc-generic-expression-proto-index nil)
166 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
167
168 (defvar cc-imenu-objc-generic-expression
169 (concat
170 ;;
171 ;; For C
172 ;;
173 ;; > Special case to match a line like `main() {}'
174 ;; > e.g. no return type, not even on the previous line.
175 ;; Pick a token by (match-string 1)
176 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
177 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
178 "\\|"
179 ;; > General function name regexp
180 ;; Pick a token by (match-string 3)
181 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 2
182 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
183 ;; > Special case for definitions using phony prototype macros like:
184 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
185 ;; Pick a token by (match-string 5)
186 (if cc-imenu-c-prototype-macro-regexp
187 (concat
188 "\\|"
189 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
190 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 6) "")
191 )
192 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 5) "")
193 "") ; -> index += 0
194 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
195 ;;
196 ;; For Objective-C
197 ;; Pick a token by (match-string 5 or 6)
198 ;;
199 "\\|\\("
200 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
201 "\\|"
202 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
203 "\\|"
204 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
205 "\\|"
206 ;; For NSObject, NSProxy and Object... They don't have super class.
207 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
208 "\\|"
209 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
210 "\\|"
211 "^@implementation[\t ]+[a-zA-Z0-9_]+"
212 "\\|"
213 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
214 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
215
216
217 ;; Imenu support for objective-c uses functions.
218 (defsubst cc-imenu-objc-method-to-selector (method)
219 "Return the objc selector style string of METHOD.
220 Example:
221 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
222 =>
223 -perform:withObject:withObject:withObject: /* selector */"
224 (let ((return "") ; String to be returned
225 (p 0) ; Current scanning position in METHOD
226 (pmax (length method)) ;
227 char ; Current scanning target
228 (betweenparen 0) ; CHAR is in parentheses.
229 argreq ; An argument is required.
230 inargvar) ; position of CHAR is in an argument variable.
231 (while (< p pmax)
232 (setq char (aref method p)
233 p (1+ p))
234 (cond
235 ;; Is CHAR part of a objc token?
236 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
237 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
238 (or (and (<= ?a char) (<= char ?z))
239 (and (<= ?A char) (<= char ?Z))
240 (and (<= ?0 char) (<= char ?9))
241 (= ?_ char)))
242 (if argreq
243 (setq inargvar t
244 argreq nil)
245 (setq return (concat return (char-to-string char)))))
246 ;; Or a white space?
247 ((and inargvar (or (eq ?\ char) (eq ?\n char))
248 (setq inargvar nil)))
249 ;; Or a method separator?
250 ;; If a method separator, the next token will be an argument variable.
251 ((eq ?: char)
252 (setq argreq t
253 return (concat return (char-to-string char))))
254 ;; Or an open parentheses?
255 ((eq ?\( char)
256 (setq betweenparen (1+ betweenparen)))
257 ;; Or a close parentheses?
258 ((eq ?\) char)
259 (setq betweenparen (1- betweenparen)))))
260 return))
261
262 (defun cc-imenu-objc-remove-white-space (str)
263 "Remove all spaces and tabs from STR."
264 (let ((return "")
265 (p 0)
266 (max (length str))
267 char)
268 (while (< p max)
269 (setq char (aref str p))
270 (setq p (1+ p))
271 (if (or (= char ?\ ) (= char ?\t))
272 ()
273 (setq return (concat return (char-to-string char)))))
274 return))
275
276 (defun cc-imenu-objc-function ()
277 "imenu supports for objc-mode."
278 (let (methodlist
279 clist
280 ;;
281 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
282 ;;
283 ;; *Warning for developers*
284 ;; These constants depend on `cc-imenu-c++-generic-expression'.
285 ;;
286 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
287 ;; Special case to match a line like `main() {}'
288 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
289 ;; General function name regexp
290 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
291 ;; Special case for definitions using phony prototype macros like:
292 (Cproto cc-imenu-objc-generic-expression-proto-index)
293 langnum
294 ;;
295 (classcount 0)
296 toplist
297 stupid
298 str
299 str2
300 (intflen (length "@interface"))
301 (implen (length "@implementation"))
302 (prtlen (length "@protocol"))
303 (func
304 ;;
305 ;; Does this emacs has buffer-substring-no-properties?
306 ;;
307 (if (fboundp 'buffer-substring-no-properties)
308 'buffer-substring-no-properties
309 'buffer-substring)))
310 (goto-char (point-max))
311 (imenu-progress-message stupid 0)
312 ;;
313 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
314 (imenu-progress-message stupid)
315 (setq langnum (if (match-beginning OBJC)
316 OBJC
317 (cond
318 ((match-beginning Cproto) Cproto)
319 ((match-beginning Cgeneralfunc) Cgeneralfunc)
320 ((match-beginning Cnoreturn) Cnoreturn))))
321 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
322 ;;
323 (cond
324 ;;
325 ;; C
326 ;;
327 ((not (eq langnum OBJC))
328 (setq clist (cons (cons str (match-beginning langnum)) clist)))
329 ;;
330 ;; ObjC
331 ;;
332 ;; An instance Method
333 ((eq (aref str 0) ?-)
334 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
335 (setq methodlist (cons (cons str
336 (match-beginning langnum))
337 methodlist)))
338 ;; A factory Method
339 ((eq (aref str 0) ?+)
340 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
341 (setq methodlist (cons (cons str
342 (match-beginning langnum))
343 methodlist)))
344 ;; Interface or implementation or protocol
345 ((eq (aref str 0) ?@)
346 (setq classcount (1+ classcount))
347 (cond
348 ((and (> (length str) implen)
349 (string= (substring str 0 implen) "@implementation"))
350 (setq str (substring str implen)
351 str2 "@implementation"))
352 ((string= (substring str 0 intflen) "@interface")
353 (setq str (substring str intflen)
354 str2 "@interface"))
355 ((string= (substring str 0 prtlen) "@protocol")
356 (setq str (substring str prtlen)
357 str2 "@protocol")))
358 (setq str (cc-imenu-objc-remove-white-space str))
359 (setq methodlist (cons (cons str2
360 (match-beginning langnum))
361 methodlist))
362 (setq toplist (cons nil (cons (cons str
363 methodlist) toplist))
364 methodlist nil))))
365 ;;
366 (imenu-progress-message stupid 100)
367 (if (eq (car toplist) nil)
368 (setq toplist (cdr toplist)))
369
370 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
371 (if (< classcount 2)
372 (let ((classname (car (car toplist)))
373 (p (cdr (car (cdr (car toplist)))))
374 last)
375 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
376 ;; Add C lang token
377 (if clist
378 (progn
379 (setq last toplist)
380 (while (cdr last)
381 (setq last (cdr last)))
382 (setcdr last clist))))
383 ;; Add C lang tokens as a sub menu
384 (setq toplist (cons (cons "C" clist) toplist)))
385 ;;
386 toplist
387 ))
388
389 \f
390 (provide 'cc-menus)
391 ;;; cc-menus.el ends here