1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 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 GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, 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 (require 'cc-bytecomp
)))
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-defun imenu-progress-message)
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 recognised as function `new ()' because the regexps
70 ;; work by backtracking from the end of the definition.
74 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
75 ; (note: this can be `\n')
77 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; 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 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; 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 "[^()]*" ; no parentheses before
106 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
107 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
108 "\\([ \t\n]\\|\\\\\n\\)*(" ; see above, BUT the arg list
109 "\\([ \t\n]\\|\\\\\n\\)*\\([^ \t\n(*][^)]*\\)?)" ; must not start
110 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]" ; with an asterisk or parentheses
112 ;; Special case for definitions using phony prototype macros like:
113 ;; `int main _PROTO( (int argc,char *argv[]) )'.
114 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
115 ;; Only supported in c-code, so no `:<>~' chars in function name!
116 ,@(if cc-imenu-c-prototype-macro-regexp
119 "^\\<.*" ; line MUST start with word char
120 "[^a-zA-Z0-9_]" ; match any non-identifier char
121 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
122 "[ \t]*" ; whitespace before macro name
123 cc-imenu-c-prototype-macro-regexp
124 "[ \t]*(" ; ws followed by first paren.
125 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
130 "^" ; beginning of line is required
131 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
132 "\\(class\\|struct\\)[ \t]+"
133 "\\(" ; the string we want to get
134 "[a-zA-Z0-9_]+" ; class name
135 "\\(<[^>]+>\\)?" ; possibly explicitly specialized
137 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
139 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
141 (defvar cc-imenu-c-generic-expression
142 cc-imenu-c
++-generic-expression
143 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
145 (defvar cc-imenu-java-generic-expression
149 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
150 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
151 "\\([.A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
153 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
155 "[][a-zA-Z,_1-9\n \t]*" ; arguments
158 "[,a-zA-Z_1-9\n \t]*{"
160 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
162 ;; *Warning for cc-mode developers*
164 ;; `cc-imenu-objc-generic-expression' elements depend on
165 ;; `cc-imenu-c++-generic-expression'. So if you change this
166 ;; expression, you need to change following variables,
167 ;; `cc-imenu-objc-generic-expression-*-index',
168 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
169 ;; order to know where the each regexp *group \\(foobar\\)* elements
172 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
173 ;; being initialized.
176 ;; Internal variables
177 (defvar cc-imenu-objc-generic-expression-noreturn-index nil
)
178 (defvar cc-imenu-objc-generic-expression-general-func-index nil
)
179 (defvar cc-imenu-objc-generic-expression-proto-index nil
)
180 (defvar cc-imenu-objc-generic-expression-objc-base-index nil
)
182 (defvar cc-imenu-objc-generic-expression
187 ;; > Special case to match a line like `main() {}'
188 ;; > e.g. no return type, not even on the previous line.
189 ;; Pick a token by (match-string 1)
190 (car (cdr (nth 1 cc-imenu-c
++-generic-expression
))) ; -> index += 2
191 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index
1) "")
193 ;; > General function name regexp
194 ;; Pick a token by (match-string 3)
195 (car (cdr (nth 2 cc-imenu-c
++-generic-expression
))) ; -> index += 2
196 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index
3) "")
197 ;; > Special case for definitions using phony prototype macros like:
198 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
199 ;; Pick a token by (match-string 5)
200 (if cc-imenu-c-prototype-macro-regexp
203 (car (cdr (nth 3 cc-imenu-c
++-generic-expression
))) ; -> index += 1
204 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
6) "")
206 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index
5) "")
208 (prog2 (setq cc-imenu-objc-generic-expression-proto-index
5) "")
211 ;; Pick a token by (match-string 5 or 6)
214 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
216 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
218 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
220 ;; For NSObject, NSProxy and Object... They don't have super class.
221 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
223 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
225 "^@implementation[\t ]+[a-zA-Z0-9_]+"
227 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
228 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
231 ;; Imenu support for objective-c uses functions.
232 (defsubst cc-imenu-objc-method-to-selector
(method)
233 "Return the objc selector style string of METHOD.
235 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
237 -perform:withObject:withObject:withObject: /* selector */"
238 (let ((return "") ; String to be returned
239 (p 0) ; Current scanning position in METHOD
240 (pmax (length method
)) ;
241 char
; Current scanning target
242 (betweenparen 0) ; CHAR is in parentheses.
243 argreq
; An argument is required.
244 inargvar
) ; position of CHAR is in an argument variable.
246 (setq char
(aref method p
)
249 ;; Is CHAR part of a objc token?
250 ((and (not inargvar
) ; Ignore if CHAR is part of an argument variable.
251 (eq 0 betweenparen
) ; Ignore if CHAR is in parentheses.
252 (or (and (<= ?a char
) (<= char ?z
))
253 (and (<= ?A char
) (<= char ?Z
))
254 (and (<= ?
0 char
) (<= char ?
9))
259 (setq return
(concat return
(char-to-string char
)))))
261 ((and inargvar
(or (eq ?\ char
) (eq ?
\n char
))
262 (setq inargvar nil
)))
263 ;; Or a method separator?
264 ;; If a method separator, the next token will be an argument variable.
267 return
(concat return
(char-to-string char
))))
268 ;; Or an open parentheses?
270 (setq betweenparen
(1+ betweenparen
)))
271 ;; Or a close parentheses?
273 (setq betweenparen
(1- betweenparen
)))))
276 (defun cc-imenu-objc-remove-white-space (str)
277 "Remove all spaces and tabs from STR."
283 (setq char
(aref str p
))
285 (if (or (= char ?\
) (= char ?
\t))
287 (setq return
(concat return
(char-to-string char
)))))
290 (defun cc-imenu-objc-function ()
291 "imenu supports for objc-mode."
295 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
297 ;; *Warning for developers*
298 ;; These constants depend on `cc-imenu-c++-generic-expression'.
300 (OBJC cc-imenu-objc-generic-expression-objc-base-index
)
301 ;; Special case to match a line like `main() {}'
302 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index
)
303 ;; General function name regexp
304 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index
)
305 ;; Special case for definitions using phony prototype macros like:
306 (Cproto cc-imenu-objc-generic-expression-proto-index
)
314 (intflen (length "@interface"))
315 (implen (length "@implementation"))
316 (prtlen (length "@protocol"))
319 ;; Does this emacs has buffer-substring-no-properties?
321 (if (fboundp 'buffer-substring-no-properties
)
322 'buffer-substring-no-properties
324 (goto-char (point-max))
325 (imenu-progress-message stupid
0)
327 (while (re-search-backward cc-imenu-objc-generic-expression nil t
)
328 (imenu-progress-message stupid
)
329 (setq langnum
(if (match-beginning OBJC
)
332 ((match-beginning Cproto
) Cproto
)
333 ((match-beginning Cgeneralfunc
) Cgeneralfunc
)
334 ((match-beginning Cnoreturn
) Cnoreturn
))))
335 (setq str
(funcall func
(match-beginning langnum
) (match-end langnum
)))
341 ((not (eq langnum OBJC
))
342 (setq clist
(cons (cons str
(match-beginning langnum
)) clist
)))
346 ;; An instance Method
347 ((eq (aref str
0) ?-
)
348 (setq str
(concat "-" (cc-imenu-objc-method-to-selector str
)))
349 (setq methodlist
(cons (cons str
350 (match-beginning langnum
))
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
))
358 ;; Interface or implementation or protocol
359 ((eq (aref str
0) ?
@)
360 (setq classcount
(1+ classcount
))
362 ((and (> (length str
) implen
)
363 (string= (substring str
0 implen
) "@implementation"))
364 (setq str
(substring str implen
)
365 str2
"@implementation"))
366 ((string= (substring str
0 intflen
) "@interface")
367 (setq str
(substring str intflen
)
369 ((string= (substring str
0 prtlen
) "@protocol")
370 (setq str
(substring str prtlen
)
372 (setq str
(cc-imenu-objc-remove-white-space str
))
373 (setq methodlist
(cons (cons str2
374 (match-beginning langnum
))
376 (setq toplist
(cons nil
(cons (cons str
377 methodlist
) toplist
))
380 (imenu-progress-message stupid
100)
381 (if (eq (car toplist
) nil
)
382 (setq toplist
(cdr toplist
)))
384 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
386 (let ((classname (car (car toplist
)))
387 (p (cdr (car (cdr (car toplist
)))))
389 (setq toplist
(cons (cons classname p
) (cdr (cdr (car toplist
)))))
395 (setq last
(cdr last
)))
396 (setcdr last clist
))))
397 ;; Add C lang tokens as a sub menu
398 (setq toplist
(cons (cons "C" clist
) toplist
)))
403 ;(defvar cc-imenu-pike-generic-expression
405 ; FIXME: Please contribute one!
407 (defun cc-imenu-init (mode-generic-expression)
408 (setq imenu-generic-expression mode-generic-expression
409 imenu-case-fold-search nil
))
412 (cc-provide 'cc-menus
)
414 ;;; cc-menus.el ends here