1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2003, 2004, 2005 Free Software Foundation,
6 ;; Authors: 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and 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 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
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 2, or (at your option)
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 this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
38 (if (and (boundp 'byte-compile-dest-file
)
39 (stringp byte-compile-dest-file
))
40 (cons (file-name-directory byte-compile-dest-file
) load-path
)
42 (load "cc-bytecomp" nil t
)))
46 ;; The things referenced in imenu, which we don't require.
47 (cc-bytecomp-defvar imenu-case-fold-search
)
48 (cc-bytecomp-defvar imenu-generic-expression
)
49 (cc-bytecomp-defvar imenu-create-index-function
)
50 (cc-bytecomp-defun imenu-progress-message)
54 (defvar cc-imenu-c-prototype-macro-regexp nil
55 "RE matching macro names used to conditionally specify function prototypes.
62 #define _P(x) /*nothing*/
65 int main _P( (int argc, char *argv[]) )
67 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
69 (defvar cc-imenu-c
++-generic-expression
71 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
72 ;; will be incorrectly recognised as function `new ()' because the regexps
73 ;; work by backtracking from the end of the definition.
77 "[^" c-alnum
"_:<>~]" ; match any non-identifier char
78 ; (note: this can be `\n')
80 "\\([" c-alnum
"_:<>~]*::\\)?" ; match an operator
82 "\\(()\\|[^(]*\\)" ; special case for `()' operator
85 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
86 ; require something other than
87 ; a `;' after the (...) to
88 ; avoid prototypes. Can't
89 ; catch cases with () inside
90 ; the parentheses surrounding
91 ; the parameters. e.g.:
92 ; `int foo(int a=bar()) {...}'
94 ;; Special case to match a line like `main() {}'
95 ;; e.g. no return type, not even on the previous line.
99 "\\([" c-alpha
"_][" c-alnum
"_:<>~]*\\)" ; match function name
100 "[ \t]*(" ; see above, BUT
101 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
102 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
104 ;; General function name regexp
107 "^\\<" ; line MUST start with word char
108 "[^()]*" ; 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
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
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
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
140 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
142 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
144 (defvar cc-imenu-c-generic-expression
145 cc-imenu-c
++-generic-expression
146 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
148 (defvar cc-imenu-java-generic-expression
151 "[" c-alpha
"_][\]\[." c-alnum
"_]+[ \t\n\r]+" ; type spec
152 "\\([" c-alpha
"_][" c-alnum
"_]+\\)" ; method name
154 ;; An argument list that is either empty or contains at least
155 ;; two identifiers with only space between them. This avoids
156 ;; matching e.g. "else if (foo)".
157 (concat "([ \t\n\r]*"
158 "\\([\]\[.," c-alnum
"_]+"
160 "[\]\[.," c-alnum
"_]"
161 "[\]\[.," c-alnum
"_ \t\n\r]*"
163 "[.," c-alnum
"_ \t\n\r]*"
166 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
168 ;; *Warning for cc-mode developers*
170 ;; `cc-imenu-objc-generic-expression' elements depend on
171 ;; `cc-imenu-c++-generic-expression'. So if you change this
172 ;; expression, you need to change following variables,
173 ;; `cc-imenu-objc-generic-expression-*-index',
174 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
175 ;; order to know where the each regexp *group \\(foobar\\)* elements
178 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
179 ;; being initialized.
182 ;; Internal variables
183 (defvar cc-imenu-objc-generic-expression-noreturn-index nil
)
184 (defvar cc-imenu-objc-generic-expression-general-func-index nil
)
185 (defvar cc-imenu-objc-generic-expression-proto-index nil
)
186 (defvar cc-imenu-objc-generic-expression-objc-base-index nil
)
188 (defvar cc-imenu-objc-generic-expression
193 ;; > Special case to match a line like `main() {}'
194 ;; > e.g. no return type, not even on the previous line.
195 ;; Pick a token by (match-string 1)
196 (car (cdr (nth 1 cc-imenu-c
++-generic-expression
))) ; -> index += 2
197 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index
1) "")
199 ;; > General function name regexp
200 ;; Pick a token by (match-string 3)
201 (car (cdr (nth 2 cc-imenu-c
++-generic-expression
))) ; -> index += 5
202 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index
3) "")
203 ;; > Special case for definitions using phony prototype macros like:
204 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
205 ;; Pick a token by (match-string 8)
206 (if cc-imenu-c-prototype-macro-regexp
209 (car (cdr (nth 3 cc-imenu-c
++-generic-expression
))) ; -> index += 1
210 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
9) "")
212 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
8) "")
214 (prog2 (setq cc-imenu-objc-generic-expression-proto-index
8) "")
217 ;; Pick a token by (match-string 8 or 9)
220 "^[-+][:" c-alnum
"()*_<>\n\t ]*[;{]" ; Methods
222 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*:"
224 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*([" c-alnum
"_]+)"
226 ;; For NSObject, NSProxy and Object... They don't have super class.
227 "^@interface[\t ]+[" c-alnum
"_]+[\t ]*.*$"
229 "^@implementation[\t ]+[" c-alnum
"_]+[\t ]*([" c-alnum
"_]+)"
231 "^@implementation[\t ]+[" c-alnum
"_]+"
233 "^@protocol[\t ]+[" c-alnum
"_]+" "\\)")
234 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
237 ;; Imenu support for objective-c uses functions.
238 (defsubst cc-imenu-objc-method-to-selector
(method)
239 "Return the objc selector style string of METHOD.
241 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
243 -perform:withObject:withObject:withObject: /* selector */"
244 (let ((return "") ; String to be returned
245 (p 0) ; Current scanning position in METHOD
246 (pmax (length method
)) ;
247 char
; Current scanning target
248 (betweenparen 0) ; CHAR is in parentheses.
249 argreq
; An argument is required.
250 inargvar
) ; position of CHAR is in an argument variable.
252 (setq char
(aref method p
)
255 ;; Is CHAR part of a objc token?
256 ((and (not inargvar
) ; Ignore if CHAR is part of an argument variable.
257 (eq 0 betweenparen
) ; Ignore if CHAR is in parentheses.
258 (or (and (<= ?a char
) (<= char ?z
))
259 (and (<= ?A char
) (<= char ?Z
))
260 (and (<= ?
0 char
) (<= char ?
9))
265 (setq return
(concat return
(char-to-string char
)))))
267 ((and inargvar
(or (eq ?\ char
) (eq ?
\n char
))
268 (setq inargvar nil
)))
269 ;; Or a method separator?
270 ;; If a method separator, the next token will be an argument variable.
273 return
(concat return
(char-to-string char
))))
274 ;; Or an open parentheses?
276 (setq betweenparen
(1+ betweenparen
)))
277 ;; Or a close parentheses?
279 (setq betweenparen
(1- betweenparen
)))))
282 (defun cc-imenu-objc-remove-white-space (str)
283 "Remove all spaces and tabs from STR."
289 (setq char
(aref str p
))
291 (if (or (= char ?\
) (= char ?
\t))
293 (setq return
(concat return
(char-to-string char
)))))
296 (defun cc-imenu-objc-function ()
297 "imenu supports for objc-mode."
301 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
303 ;; *Warning for developers*
304 ;; These constants depend on `cc-imenu-c++-generic-expression'.
306 (OBJC cc-imenu-objc-generic-expression-objc-base-index
)
307 ;; Special case to match a line like `main() {}'
308 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index
)
309 ;; General function name regexp
310 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index
)
311 ;; Special case for definitions using phony prototype macros like:
312 (Cproto cc-imenu-objc-generic-expression-proto-index
)
320 (intflen (length "@interface"))
321 (implen (length "@implementation"))
322 (prtlen (length "@protocol"))
325 ;; Does this emacs has buffer-substring-no-properties?
327 (if (fboundp 'buffer-substring-no-properties
)
328 'buffer-substring-no-properties
330 (goto-char (point-max))
331 (imenu-progress-message stupid
0)
333 (while (re-search-backward cc-imenu-objc-generic-expression nil t
)
334 (imenu-progress-message stupid
)
335 (setq langnum
(if (match-beginning OBJC
)
338 ((match-beginning Cproto
) Cproto
)
339 ((match-beginning Cgeneralfunc
) Cgeneralfunc
)
340 ((match-beginning Cnoreturn
) Cnoreturn
))))
341 (setq str
(funcall func
(match-beginning langnum
) (match-end langnum
)))
347 ((not (eq langnum OBJC
))
348 (setq clist
(cons (cons str
(match-beginning langnum
)) clist
)))
352 ;; An instance Method
353 ((eq (aref str
0) ?-
)
354 (setq str
(concat "-" (cc-imenu-objc-method-to-selector str
)))
355 (setq methodlist
(cons (cons str
356 (match-beginning langnum
))
359 ((eq (aref str
0) ?
+)
360 (setq str
(concat "+" (cc-imenu-objc-method-to-selector str
)))
361 (setq methodlist
(cons (cons str
362 (match-beginning langnum
))
364 ;; Interface or implementation or protocol
365 ((eq (aref str
0) ?
@)
366 (setq classcount
(1+ classcount
))
368 ((and (> (length str
) implen
)
369 (string= (substring str
0 implen
) "@implementation"))
370 (setq str
(substring str implen
)
371 str2
"@implementation"))
372 ((string= (substring str
0 intflen
) "@interface")
373 (setq str
(substring str intflen
)
375 ((string= (substring str
0 prtlen
) "@protocol")
376 (setq str
(substring str prtlen
)
378 (setq str
(cc-imenu-objc-remove-white-space str
))
379 (setq methodlist
(cons (cons str2
380 (match-beginning langnum
))
382 (setq toplist
(cons nil
(cons (cons str
383 methodlist
) toplist
))
386 (imenu-progress-message stupid
100)
387 (if (eq (car toplist
) nil
)
388 (setq toplist
(cdr toplist
)))
390 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
392 (let ((classname (car (car toplist
)))
393 (p (cdr (car (cdr (car toplist
)))))
395 (setq toplist
(cons (cons classname p
) (cdr (cdr (car toplist
)))))
401 (setq last
(cdr last
)))
402 (setcdr last clist
))))
403 ;; Add C lang tokens as a sub menu
405 (setq toplist
(cons (cons "C" clist
) toplist
))))
410 ;(defvar cc-imenu-pike-generic-expression
412 ; FIXME: Please contribute one!
414 (defun cc-imenu-init (mode-generic-expression
415 &optional mode-create-index-function
)
416 (setq imenu-generic-expression mode-generic-expression
417 imenu-case-fold-search nil
)
418 (when mode-create-index-function
419 (setq imenu-create-index-function mode-create-index-function
)))
422 (cc-provide 'cc-menus
)
424 ;;; arch-tag: f6b60933-91f0-4145-ab44-70ca6d1b919b
425 ;;; cc-menus.el ends here