1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 (if (and (boundp 'byte-compile-current-file
)
34 (stringp byte-compile-current-file
))
35 (cons (file-name-directory byte-compile-current-file
)
38 (load "cc-defs" nil t
)))
40 ;; Dummy definitions to shut up the compiler in case imenu doesn't exist.
41 (defvar imenu-generic-expression
)
42 (defvar imenu-case-fold-search
)
43 (or (fboundp 'imenu-progress-message
)
44 (defun imenu-progress-message (&rest args
) nil
))
46 ;; Try to pull in imenu.
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 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
78 ; (note: this can be `\n')
80 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; 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 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; 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 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
110 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
111 "[ \t]*(" ; see above, BUT
112 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
113 "[ \t]*[^ \t;(]" ; 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 "[^a-zA-Z0-9_]" ; match any non-identifier char
124 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; 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 <...>'
136 "\\(" ; the string we want to get
137 "[a-zA-Z0-9_]+" ; class name
138 "\\(<[^>]+>\\)?" ; possibly explicitely specialized
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
152 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
153 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
154 "\\([.A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
156 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
158 "[][a-zA-Z,_1-9\n \t]*" ; arguments
161 "[,a-zA-Z_1-9\n \t]*{"
163 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
165 ;; *Warning for cc-mode developers*
167 ;; `cc-imenu-objc-generic-expression' elements depend on
168 ;; `cc-imenu-c++-generic-expression'. So if you change this
169 ;; expression, you need to change following variables,
170 ;; `cc-imenu-objc-generic-expression-*-index',
171 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
172 ;; order to know where the each regexp *group \\(foobar\\)* elements
175 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
176 ;; being initialized.
179 ;; Internal variables
180 (defvar cc-imenu-objc-generic-expression-noreturn-index nil
)
181 (defvar cc-imenu-objc-generic-expression-general-func-index nil
)
182 (defvar cc-imenu-objc-generic-expression-proto-index nil
)
183 (defvar cc-imenu-objc-generic-expression-objc-base-index nil
)
185 (defvar cc-imenu-objc-generic-expression
190 ;; > Special case to match a line like `main() {}'
191 ;; > e.g. no return type, not even on the previous line.
192 ;; Pick a token by (match-string 1)
193 (car (cdr (nth 1 cc-imenu-c
++-generic-expression
))) ; -> index += 2
194 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index
1) "")
196 ;; > General function name regexp
197 ;; Pick a token by (match-string 3)
198 (car (cdr (nth 2 cc-imenu-c
++-generic-expression
))) ; -> index += 2
199 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index
3) "")
200 ;; > Special case for definitions using phony prototype macros like:
201 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
202 ;; Pick a token by (match-string 5)
203 (if cc-imenu-c-prototype-macro-regexp
206 (car (cdr (nth 3 cc-imenu-c
++-generic-expression
))) ; -> index += 1
207 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
6) "")
209 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
5) "")
211 (prog2 (setq cc-imenu-objc-generic-expression-proto-index
5) "")
214 ;; Pick a token by (match-string 5 or 6)
217 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
219 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
221 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
223 ;; For NSObject, NSProxy and Object... They don't have super class.
224 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
226 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
228 "^@implementation[\t ]+[a-zA-Z0-9_]+"
230 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
231 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
234 ;; Imenu support for objective-c uses functions.
235 (defsubst cc-imenu-objc-method-to-selector
(method)
236 "Return the objc selector style string of METHOD.
238 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
240 -perform:withObject:withObject:withObject: /* selector */"
241 (let ((return "") ; String to be returned
242 (p 0) ; Current scanning position in METHOD
243 (pmax (length method
)) ;
244 char
; Current scanning target
245 (betweenparen 0) ; CHAR is in parentheses.
246 argreq
; An argument is required.
247 inargvar
) ; position of CHAR is in an argument variable.
249 (setq char
(aref method p
)
252 ;; Is CHAR part of a objc token?
253 ((and (not inargvar
) ; Ignore if CHAR is part of an argument variable.
254 (eq 0 betweenparen
) ; Ignore if CHAR is in parentheses.
255 (or (and (<= ?a char
) (<= char ?z
))
256 (and (<= ?A char
) (<= char ?Z
))
257 (and (<= ?
0 char
) (<= char ?
9))
262 (setq return
(concat return
(char-to-string char
)))))
264 ((and inargvar
(or (eq ?\ char
) (eq ?
\n char
))
265 (setq inargvar nil
)))
266 ;; Or a method separator?
267 ;; If a method separator, the next token will be an argument variable.
270 return
(concat return
(char-to-string char
))))
271 ;; Or an open parentheses?
273 (setq betweenparen
(1+ betweenparen
)))
274 ;; Or a close parentheses?
276 (setq betweenparen
(1- betweenparen
)))))
279 (defun cc-imenu-objc-remove-white-space (str)
280 "Remove all spaces and tabs from STR."
286 (setq char
(aref str p
))
288 (if (or (= char ?\
) (= char ?
\t))
290 (setq return
(concat return
(char-to-string char
)))))
293 (defun cc-imenu-objc-function ()
294 "imenu supports for objc-mode."
298 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
300 ;; *Warning for developers*
301 ;; These constants depend on `cc-imenu-c++-generic-expression'.
303 (OBJC cc-imenu-objc-generic-expression-objc-base-index
)
304 ;; Special case to match a line like `main() {}'
305 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index
)
306 ;; General function name regexp
307 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index
)
308 ;; Special case for definitions using phony prototype macros like:
309 (Cproto cc-imenu-objc-generic-expression-proto-index
)
317 (intflen (length "@interface"))
318 (implen (length "@implementation"))
319 (prtlen (length "@protocol"))
322 ;; Does this emacs has buffer-substring-no-properties?
324 (if (fboundp 'buffer-substring-no-properties
)
325 'buffer-substring-no-properties
327 (goto-char (point-max))
328 (imenu-progress-message stupid
0)
330 (while (re-search-backward cc-imenu-objc-generic-expression nil t
)
331 (imenu-progress-message stupid
)
332 (setq langnum
(if (match-beginning OBJC
)
335 ((match-beginning Cproto
) Cproto
)
336 ((match-beginning Cgeneralfunc
) Cgeneralfunc
)
337 ((match-beginning Cnoreturn
) Cnoreturn
))))
338 (setq str
(funcall func
(match-beginning langnum
) (match-end langnum
)))
344 ((not (eq langnum OBJC
))
345 (setq clist
(cons (cons str
(match-beginning langnum
)) clist
)))
349 ;; An instance Method
350 ((eq (aref str
0) ?-
)
351 (setq str
(concat "-" (cc-imenu-objc-method-to-selector str
)))
352 (setq methodlist
(cons (cons str
353 (match-beginning langnum
))
356 ((eq (aref str
0) ?
+)
357 (setq str
(concat "+" (cc-imenu-objc-method-to-selector str
)))
358 (setq methodlist
(cons (cons str
359 (match-beginning langnum
))
361 ;; Interface or implementation or protocol
362 ((eq (aref str
0) ?
@)
363 (setq classcount
(1+ classcount
))
365 ((and (> (length str
) implen
)
366 (string= (substring str
0 implen
) "@implementation"))
367 (setq str
(substring str implen
)
368 str2
"@implementation"))
369 ((string= (substring str
0 intflen
) "@interface")
370 (setq str
(substring str intflen
)
372 ((string= (substring str
0 prtlen
) "@protocol")
373 (setq str
(substring str prtlen
)
375 (setq str
(cc-imenu-objc-remove-white-space str
))
376 (setq methodlist
(cons (cons str2
377 (match-beginning langnum
))
379 (setq toplist
(cons nil
(cons (cons str
380 methodlist
) toplist
))
383 (imenu-progress-message stupid
100)
384 (if (eq (car toplist
) nil
)
385 (setq toplist
(cdr toplist
)))
387 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
389 (let ((classname (car (car toplist
)))
390 (p (cdr (car (cdr (car toplist
)))))
392 (setq toplist
(cons (cons classname p
) (cdr (cdr (car toplist
)))))
398 (setq last
(cdr last
)))
399 (setcdr last clist
))))
400 ;; Add C lang tokens as a sub menu
401 (setq toplist
(cons (cons "C" clist
) toplist
)))
406 ;(defvar cc-imenu-pike-generic-expression
408 ; FIXME: Please contribute one!
410 (defun cc-imenu-init (mode-generic-expression)
411 (setq imenu-generic-expression mode-generic-expression
412 imenu-case-fold-search nil
))
416 ;;; cc-menus.el ends here