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