1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2011 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 recognised 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\\)*\\([^ \t\n(*][^)]*\\)?)" ; must not start
112 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]" ; with an asterisk or parentheses
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
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
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
139 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
141 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
143 (defvar cc-imenu-c-generic-expression
144 cc-imenu-c++-generic-expression
145 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
147 (defvar cc-imenu-java-generic-expression
150 "[" c-alpha "_][\]\[." c-alnum "_<> ]+[ \t\n\r]+" ; type spec
151 "\\([" c-alpha "_][" c-alnum "_]*\\)" ; method name
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 [].
169 "[\]\[" c-alnum "_.]*"
174 "[\]\[.," c-alnum "_<> \t\n\r]*"
185 "[.," c-alnum " \t\n\r]*"
188 "Imenu generic expression for Java mode. See
189 `imenu-generic-expression'.")
191 ;; *Warning for cc-mode developers*
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
201 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
202 ;; being initialized.
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)
211 (defvar cc-imenu-objc-generic-expression
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) "")
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
232 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
233 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
235 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 8) "")
237 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 8) "")
240 ;; Pick a token by (match-string 8 or 9)
243 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
245 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
247 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
249 ;; For NSObject, NSProxy and Object... They don't have super class.
250 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
252 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
254 "^@implementation[\t ]+[" c-alnum "_]+"
256 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
257 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
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.
264 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
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.
275 (setq char (aref method p)
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))
288 (setq return (concat return (char-to-string char)))))
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.
296 return (concat return (char-to-string char))))
297 ;; Or an open parentheses?
299 (setq betweenparen (1+ betweenparen)))
300 ;; Or a close parentheses?
302 (setq betweenparen (1- betweenparen)))))
305 (defun cc-imenu-objc-remove-white-space (str)
306 "Remove all spaces and tabs from STR."
312 (setq char (aref str p))
314 (if (or (= char ?\ ) (= char ?\t))
316 (setq return (concat return (char-to-string char)))))
319 (defun cc-imenu-objc-function ()
320 "Imenu support for Objective C mode."
324 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
326 ;; *Warning for developers*
327 ;; These constants depend on `cc-imenu-c++-generic-expression'.
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)
342 (intflen (length "@interface"))
343 (implen (length "@implementation"))
344 (prtlen (length "@protocol"))
347 ;; Does this emacs have buffer-substring-no-properties?
349 (if (fboundp 'buffer-substring-no-properties)
350 'buffer-substring-no-properties
352 (goto-char (point-max))
354 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
355 (setq langnum (if (match-beginning OBJC)
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)))
367 ((not (eq langnum OBJC))
368 (setq clist (cons (cons str (match-beginning langnum)) clist)))
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))
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))
384 ;; Interface or implementation or protocol
385 ((eq (aref str 0) ?@)
386 (setq classcount (1+ classcount))
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)
395 ((string= (substring str 0 prtlen) "@protocol")
396 (setq str (substring str prtlen)
398 (setq str (cc-imenu-objc-remove-white-space str))
399 (setq methodlist (cons (cons str2
400 (match-beginning langnum))
402 (setq toplist (cons nil (cons (cons str
403 methodlist) toplist))
406 (if (eq (car toplist) nil)
407 (setq toplist (cdr toplist)))
409 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
411 (let ((classname (car (car toplist)))
412 (p (cdr (car (cdr (car toplist)))))
414 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
420 (setq last (cdr last)))
421 (setcdr last clist))))
422 ;; Add C lang tokens as a sub menu
424 (setq toplist (cons (cons "C" clist) toplist))))
429 ;(defvar cc-imenu-pike-generic-expression
431 ; FIXME: Please contribute one!
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)))
441 (cc-provide 'cc-menus)
443 ;;; cc-menus.el ends here