1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 1998- Martin Stjernholm
8 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 ;; \n added to prevent overflow in regexp matcher.
109 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-02/msg00021.html
110 "[^()\n]*" ; no parentheses before
111 "[^" c-alnum "_:<>~]" ; match any non-identifier char
112 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
113 "\\([ \t\n]\\|\\\\\n\\)*(" ; see above, BUT the arg list
114 "\\([ \t\n]\\|\\\\\n\\)*\\([^ \t\n(*][^)]*\\)?)" ; must not start
115 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]" ; with an asterisk or parentheses
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 that is either empty or contains at least
157 ;; two identifiers with only space between them. This avoids
158 ;; matching e.g. "else if (foo)".
159 (concat "([ \t\n\r]*"
160 "\\([\]\[.," c-alnum "_]+"
162 "[\]\[.," c-alnum "_]"
163 "[\]\[.," c-alnum "_ \t\n\r]*"
165 "[.," c-alnum "_ \t\n\r]*"
168 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
170 ;; *Warning for cc-mode developers*
172 ;; `cc-imenu-objc-generic-expression' elements depend on
173 ;; `cc-imenu-c++-generic-expression'. So if you change this
174 ;; expression, you need to change following variables,
175 ;; `cc-imenu-objc-generic-expression-*-index',
176 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
177 ;; order to know where the each regexp *group \\(foobar\\)* elements
180 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
181 ;; being initialized.
184 ;; Internal variables
185 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
186 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
187 (defvar cc-imenu-objc-generic-expression-proto-index nil)
188 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
190 (defvar cc-imenu-objc-generic-expression
195 ;; > Special case to match a line like `main() {}'
196 ;; > e.g. no return type, not even on the previous line.
197 ;; Pick a token by (match-string 1)
198 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
199 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
201 ;; > General function name regexp
202 ;; Pick a token by (match-string 3)
203 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 5
204 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
205 ;; > Special case for definitions using phony prototype macros like:
206 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
207 ;; Pick a token by (match-string 8)
208 (if cc-imenu-c-prototype-macro-regexp
211 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
212 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
214 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 8) "")
216 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 8) "")
219 ;; Pick a token by (match-string 8 or 9)
222 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
224 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
226 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
228 ;; For NSObject, NSProxy and Object... They don't have super class.
229 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
231 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
233 "^@implementation[\t ]+[" c-alnum "_]+"
235 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
236 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
239 ;; Imenu support for objective-c uses functions.
240 (defsubst cc-imenu-objc-method-to-selector (method)
241 "Return the objc selector style string of METHOD.
243 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
245 -perform:withObject:withObject:withObject: /* selector */"
246 (let ((return "") ; String to be returned
247 (p 0) ; Current scanning position in METHOD
248 (pmax (length method)) ;
249 char ; Current scanning target
250 (betweenparen 0) ; CHAR is in parentheses.
251 argreq ; An argument is required.
252 inargvar) ; position of CHAR is in an argument variable.
254 (setq char (aref method p)
257 ;; Is CHAR part of a objc token?
258 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
259 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
260 (or (and (<= ?a char) (<= char ?z))
261 (and (<= ?A char) (<= char ?Z))
262 (and (<= ?0 char) (<= char ?9))
267 (setq return (concat return (char-to-string char)))))
269 ((and inargvar (or (eq ?\ char) (eq ?\n char))
270 (setq inargvar nil)))
271 ;; Or a method separator?
272 ;; If a method separator, the next token will be an argument variable.
275 return (concat return (char-to-string char))))
276 ;; Or an open parentheses?
278 (setq betweenparen (1+ betweenparen)))
279 ;; Or a close parentheses?
281 (setq betweenparen (1- betweenparen)))))
284 (defun cc-imenu-objc-remove-white-space (str)
285 "Remove all spaces and tabs from STR."
291 (setq char (aref str p))
293 (if (or (= char ?\ ) (= char ?\t))
295 (setq return (concat return (char-to-string char)))))
298 (defun cc-imenu-objc-function ()
299 "imenu supports for objc-mode."
303 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
305 ;; *Warning for developers*
306 ;; These constants depend on `cc-imenu-c++-generic-expression'.
308 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
309 ;; Special case to match a line like `main() {}'
310 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
311 ;; General function name regexp
312 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
313 ;; Special case for definitions using phony prototype macros like:
314 (Cproto cc-imenu-objc-generic-expression-proto-index)
322 (intflen (length "@interface"))
323 (implen (length "@implementation"))
324 (prtlen (length "@protocol"))
327 ;; Does this emacs have buffer-substring-no-properties?
329 (if (fboundp 'buffer-substring-no-properties)
330 'buffer-substring-no-properties
332 (goto-char (point-max))
333 (imenu-progress-message stupid 0)
335 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
336 (imenu-progress-message stupid)
337 (setq langnum (if (match-beginning OBJC)
340 ((match-beginning Cproto) Cproto)
341 ((match-beginning Cgeneralfunc) Cgeneralfunc)
342 ((match-beginning Cnoreturn) Cnoreturn))))
343 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
349 ((not (eq langnum OBJC))
350 (setq clist (cons (cons str (match-beginning langnum)) clist)))
354 ;; An instance Method
355 ((eq (aref str 0) ?-)
356 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
357 (setq methodlist (cons (cons str
358 (match-beginning langnum))
361 ((eq (aref str 0) ?+)
362 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
363 (setq methodlist (cons (cons str
364 (match-beginning langnum))
366 ;; Interface or implementation or protocol
367 ((eq (aref str 0) ?@)
368 (setq classcount (1+ classcount))
370 ((and (> (length str) implen)
371 (string= (substring str 0 implen) "@implementation"))
372 (setq str (substring str implen)
373 str2 "@implementation"))
374 ((string= (substring str 0 intflen) "@interface")
375 (setq str (substring str intflen)
377 ((string= (substring str 0 prtlen) "@protocol")
378 (setq str (substring str prtlen)
380 (setq str (cc-imenu-objc-remove-white-space str))
381 (setq methodlist (cons (cons str2
382 (match-beginning langnum))
384 (setq toplist (cons nil (cons (cons str
385 methodlist) toplist))
388 (imenu-progress-message stupid 100)
389 (if (eq (car toplist) nil)
390 (setq toplist (cdr toplist)))
392 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
394 (let ((classname (car (car toplist)))
395 (p (cdr (car (cdr (car toplist)))))
397 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
403 (setq last (cdr last)))
404 (setcdr last clist))))
405 ;; Add C lang tokens as a sub menu
407 (setq toplist (cons (cons "C" clist) toplist))))
412 ;(defvar cc-imenu-pike-generic-expression
414 ; FIXME: Please contribute one!
416 (defun cc-imenu-init (mode-generic-expression
417 &optional mode-create-index-function)
418 (setq imenu-generic-expression mode-generic-expression
419 imenu-case-fold-search nil)
420 (when mode-create-index-function
421 (setq imenu-create-index-function mode-create-index-function)))
424 (cc-provide 'cc-menus)
426 ;; arch-tag: f6b60933-91f0-4145-ab44-70ca6d1b919b
427 ;;; cc-menus.el ends here