1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
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
15 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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
)
40 (load "cc-bytecomp" nil t
)))
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
)
51 (defvar cc-imenu-c-prototype-macro-regexp nil
52 "RE matching macro names used to conditionally specify function prototypes.
59 #define _P(x) /*nothing*/
62 int main _P( (int argc, char *argv[]) )
64 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
66 (defvar cc-imenu-c
++-generic-expression
68 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
69 ;; will be incorrectly recognized as function `new ()' because the regexps
70 ;; work by backtracking from the end of the definition.
74 "[^" c-alnum
"_:<>~]" ; match any non-identifier char
75 ; (note: this can be `\n')
77 "\\([" c-alnum
"_:<>~]*::\\)?" ; match an operator
79 "\\(()\\|[^(]*\\)" ; special case for `()' operator
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()) {...}'
91 ;; Special case to match a line like `main() {}'
92 ;; e.g. no return type, not even on the previous line.
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
101 ;; General function name regexp
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\\)*" ; must not start
112 "\\([^ \t\n(*]" ; with an asterisk or parentheses
113 "[^()]*\\(([^()]*)[^()]*\\)*" ; Maybe function pointer arguments
115 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]"
117 ;; Special case for definitions using phony prototype macros like:
118 ;; `int main _PROTO( (int argc,char *argv[]) )'.
119 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
120 ;; Only supported in c-code, so no `:<>~' chars in function name!
121 ,@(if cc-imenu-c-prototype-macro-regexp
124 "^\\<.*" ; line MUST start with word char
125 "[^" c-alnum
"_]" ; match any non-identifier char
126 "\\([" c-alpha
"_][" c-alnum
"_]*\\)" ; match function name
127 "[ \t]*" ; whitespace before macro name
128 cc-imenu-c-prototype-macro-regexp
129 "[ \t]*(" ; ws followed by first paren.
130 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
135 "^" ; beginning of line is required
136 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
137 "\\(class\\|struct\\)[ \t]+"
138 "\\(" ; the string we want to get
139 "[" c-alnum
"_]+" ; class name
140 "\\(<[^>]+>\\)?" ; possibly explicitly specialized
142 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
144 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
146 (defvar cc-imenu-c-generic-expression
147 cc-imenu-c
++-generic-expression
148 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
150 (defvar cc-imenu-java-generic-expression
153 "[" c-alpha
"_][\]\[." c-alnum
"_<> ]+[ \t\n\r]+" ; type spec
154 "\\([" c-alpha
"_][" c-alnum
"_]*\\)" ; method name
156 ;; An argument list htat is either empty or contains any number
157 ;; of arguments. An argument is any number of annotations
158 ;; followed by a type spec followed by a word. A word is an
159 ;; identifier. A type spec is an identifier, possibly followed
160 ;; by < typespec > possibly followed by [].
172 "[\]\[" c-alnum
"_.]*"
177 "[\]\[.," c-alnum
"_<> \t\n\r]*"
188 "[.," c-alnum
" \t\n\r]*"
191 "Imenu generic expression for Java mode. See
192 `imenu-generic-expression'.")
194 ;; *Warning for cc-mode developers*
196 ;; `cc-imenu-objc-generic-expression' elements depend on
197 ;; `cc-imenu-c++-generic-expression'. So if you change this
198 ;; expression, you need to change following variables,
199 ;; `cc-imenu-objc-generic-expression-*-index',
200 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
201 ;; order to know where the each regexp *group \\(foobar\\)* elements
204 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
205 ;; being initialized.
208 ;; Internal variables
209 (defvar cc-imenu-objc-generic-expression-noreturn-index nil
)
210 (defvar cc-imenu-objc-generic-expression-general-func-index nil
)
211 (defvar cc-imenu-objc-generic-expression-proto-index nil
)
212 (defvar cc-imenu-objc-generic-expression-objc-base-index nil
)
214 (defvar cc-imenu-objc-generic-expression
219 ;; > Special case to match a line like `main() {}'
220 ;; > e.g. no return type, not even on the previous line.
221 ;; Pick a token by (match-string 1)
222 (car (cdr (nth 1 cc-imenu-c
++-generic-expression
))) ; -> index += 2
223 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index
1) "")
225 ;; > General function name regexp
226 ;; Pick a token by (match-string 3)
227 (car (cdr (nth 2 cc-imenu-c
++-generic-expression
))) ; -> index += 5
228 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index
3) "")
229 ;; > Special case for definitions using phony prototype macros like:
230 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
231 ;; Pick a token by (match-string 8)
232 (if cc-imenu-c-prototype-macro-regexp
235 (car (cdr (nth 3 cc-imenu-c
++-generic-expression
))) ; -> index += 1
236 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
9) "")
238 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
8) "")
240 (prog2 (setq cc-imenu-objc-generic-expression-proto-index
8) "")
243 ;; Pick a token by (match-string 8 or 9)
246 "^[-+][:" c-alnum
"()*_<>\n\t ]*[;{]" ; Methods
248 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*:"
250 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*([" c-alnum
"_]+)"
252 ;; For NSObject, NSProxy and Object... They don't have super class.
253 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*.*$"
255 "^@implementation[\t ]+[" c-alnum
"_]+[\t ]*([" c-alnum
"_]+)"
257 "^@implementation[\t ]+[" c-alnum
"_]+"
259 "^@protocol[\t ]+[" c-alnum
"_]+" "\\)")
260 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
263 ;; Imenu support for objective-c uses functions.
264 (defsubst cc-imenu-objc-method-to-selector
(method)
265 "Return the objc selector style string of METHOD.
267 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
269 -perform:withObject:withObject:withObject: /* selector */"
270 (let ((return "") ; String to be returned
271 (p 0) ; Current scanning position in METHOD
272 (pmax (length method
)) ;
273 char
; Current scanning target
274 (betweenparen 0) ; CHAR is in parentheses.
275 argreq
; An argument is required.
276 inargvar
) ; position of CHAR is in an argument variable.
278 (setq char
(aref method p
)
281 ;; Is CHAR part of a objc token?
282 ((and (not inargvar
) ; Ignore if CHAR is part of an argument variable.
283 (eq 0 betweenparen
) ; Ignore if CHAR is in parentheses.
284 (or (and (<= ?a char
) (<= char ?z
))
285 (and (<= ?A char
) (<= char ?Z
))
286 (and (<= ?
0 char
) (<= char ?
9))
291 (setq return
(concat return
(char-to-string char
)))))
293 ((and inargvar
(or (eq ?\ char
) (eq ?
\n char
))
294 (setq inargvar nil
)))
295 ;; Or a method separator?
296 ;; If a method separator, the next token will be an argument variable.
299 return
(concat return
(char-to-string char
))))
300 ;; Or an open parentheses?
302 (setq betweenparen
(1+ betweenparen
)))
303 ;; Or a close parentheses?
305 (setq betweenparen
(1- betweenparen
)))))
308 (defun cc-imenu-objc-remove-white-space (str)
309 "Remove all spaces and tabs from STR."
315 (setq char
(aref str p
))
317 (if (or (= char ?\
) (= char ?
\t))
319 (setq return
(concat return
(char-to-string char
)))))
322 (defun cc-imenu-objc-function ()
323 "Imenu support for Objective C mode."
327 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
329 ;; *Warning for developers*
330 ;; These constants depend on `cc-imenu-c++-generic-expression'.
332 (OBJC cc-imenu-objc-generic-expression-objc-base-index
)
333 ;; Special case to match a line like `main() {}'
334 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index
)
335 ;; General function name regexp
336 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index
)
337 ;; Special case for definitions using phony prototype macros like:
338 (Cproto cc-imenu-objc-generic-expression-proto-index
)
345 (intflen (length "@interface"))
346 (implen (length "@implementation"))
347 (prtlen (length "@protocol"))
350 ;; Does this emacs have buffer-substring-no-properties?
352 (if (fboundp 'buffer-substring-no-properties
)
353 'buffer-substring-no-properties
355 (goto-char (point-max))
357 (while (re-search-backward cc-imenu-objc-generic-expression nil t
)
358 (setq langnum
(if (match-beginning OBJC
)
361 ((match-beginning Cproto
) Cproto
)
362 ((match-beginning Cgeneralfunc
) Cgeneralfunc
)
363 ((match-beginning Cnoreturn
) Cnoreturn
))))
364 (setq str
(funcall func
(match-beginning langnum
) (match-end langnum
)))
370 ((not (eq langnum OBJC
))
371 (setq clist
(cons (cons str
(match-beginning langnum
)) clist
)))
375 ;; An instance Method
376 ((eq (aref str
0) ?-
)
377 (setq str
(concat "-" (cc-imenu-objc-method-to-selector str
)))
378 (setq methodlist
(cons (cons str
379 (match-beginning langnum
))
382 ((eq (aref str
0) ?
+)
383 (setq str
(concat "+" (cc-imenu-objc-method-to-selector str
)))
384 (setq methodlist
(cons (cons str
385 (match-beginning langnum
))
387 ;; Interface or implementation or protocol
388 ((eq (aref str
0) ?
@)
389 (setq classcount
(1+ classcount
))
391 ((and (> (length str
) implen
)
392 (string= (substring str
0 implen
) "@implementation"))
393 (setq str
(substring str implen
)
394 str2
"@implementation"))
395 ((string= (substring str
0 intflen
) "@interface")
396 (setq str
(substring str intflen
)
398 ((string= (substring str
0 prtlen
) "@protocol")
399 (setq str
(substring str prtlen
)
401 (setq str
(cc-imenu-objc-remove-white-space str
))
402 (setq methodlist
(cons (cons str2
403 (match-beginning langnum
))
405 (setq toplist
(cons nil
(cons (cons str
406 methodlist
) toplist
))
409 (if (eq (car toplist
) nil
)
410 (setq toplist
(cdr toplist
)))
412 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
414 (let ((classname (car (car toplist
)))
415 (p (cdr (car (cdr (car toplist
)))))
417 (setq toplist
(cons (cons classname p
) (cdr (cdr (car toplist
)))))
423 (setq last
(cdr last
)))
424 (setcdr last clist
))))
425 ;; Add C lang tokens as a sub menu
427 (setq toplist
(cons (cons "C" clist
) toplist
))))
432 ;(defvar cc-imenu-pike-generic-expression
434 ; FIXME: Please contribute one!
436 (defun cc-imenu-init (mode-generic-expression
437 &optional mode-create-index-function
)
438 (setq imenu-generic-expression mode-generic-expression
439 imenu-case-fold-search nil
)
440 (when mode-create-index-function
441 (setq imenu-create-index-function mode-create-index-function
)))
444 (cc-provide 'cc-menus
)
446 ;;; cc-menus.el ends here