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