1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 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
28 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
37 (if (and (boundp 'byte-compile-dest-file)
38 (stringp byte-compile-dest-file))
39 (cons (file-name-directory byte-compile-dest-file) load-path)
41 (load "cc-bytecomp" nil t)))
45 ;; The things referenced in imenu, which we don't require.
46 (cc-bytecomp-defvar imenu-case-fold-search)
47 (cc-bytecomp-defvar imenu-generic-expression)
48 (cc-bytecomp-defvar imenu-create-index-function)
49 (cc-bytecomp-defun imenu-progress-message)
53 (defvar cc-imenu-c-prototype-macro-regexp nil
54 "RE matching macro names used to conditionally specify function prototypes.
61 #define _P(x) /*nothing*/
64 int main _P( (int argc, char *argv[]) )
66 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
68 (defvar cc-imenu-c++-generic-expression
70 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
71 ;; will be incorrectly recognised as function `new ()' because the regexps
72 ;; work by backtracking from the end of the definition.
76 "[^" c-alnum "_:<>~]" ; match any non-identifier char
77 ; (note: this can be `\n')
79 "\\([" c-alnum "_:<>~]*::\\)?" ; match an operator
81 "\\(()\\|[^(]*\\)" ; special case for `()' operator
84 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
85 ; require something other than
86 ; a `;' after the (...) to
87 ; avoid prototypes. Can't
88 ; catch cases with () inside
89 ; the parentheses surrounding
90 ; the parameters. e.g.:
91 ; `int foo(int a=bar()) {...}'
93 ;; Special case to match a line like `main() {}'
94 ;; e.g. no return type, not even on the previous line.
98 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
99 "[ \t]*(" ; see above, BUT
100 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
101 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
103 ;; General function name regexp
106 "^\\<" ; line MUST start with word char
107 "[^()]*" ; 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 that is either empty or contains at least
154 ;; two identifiers with only space between them. This avoids
155 ;; matching e.g. "else if (foo)".
156 (concat "([ \t\n\r]*"
157 "\\([\]\[.," c-alnum "_]+"
159 "[\]\[.," c-alnum "_]"
160 "[\]\[.," c-alnum "_ \t\n\r]*"
162 "[.," c-alnum "_ \t\n\r]*"
165 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
167 ;; *Warning for cc-mode developers*
169 ;; `cc-imenu-objc-generic-expression' elements depend on
170 ;; `cc-imenu-c++-generic-expression'. So if you change this
171 ;; expression, you need to change following variables,
172 ;; `cc-imenu-objc-generic-expression-*-index',
173 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
174 ;; order to know where the each regexp *group \\(foobar\\)* elements
177 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
178 ;; being initialized.
181 ;; Internal variables
182 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
183 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
184 (defvar cc-imenu-objc-generic-expression-proto-index nil)
185 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
187 (defvar cc-imenu-objc-generic-expression
192 ;; > Special case to match a line like `main() {}'
193 ;; > e.g. no return type, not even on the previous line.
194 ;; Pick a token by (match-string 1)
195 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
196 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
198 ;; > General function name regexp
199 ;; Pick a token by (match-string 3)
200 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 5
201 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
202 ;; > Special case for definitions using phony prototype macros like:
203 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
204 ;; Pick a token by (match-string 8)
205 (if cc-imenu-c-prototype-macro-regexp
208 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
209 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
211 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 8) "")
213 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 8) "")
216 ;; Pick a token by (match-string 8 or 9)
219 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
221 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
223 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
225 ;; For NSObject, NSProxy and Object... They don't have super class.
226 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
228 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
230 "^@implementation[\t ]+[" c-alnum "_]+"
232 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
233 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
236 ;; Imenu support for objective-c uses functions.
237 (defsubst cc-imenu-objc-method-to-selector (method)
238 "Return the objc selector style string of METHOD.
240 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
242 -perform:withObject:withObject:withObject: /* selector */"
243 ;; This function does not do any hidden buffer changes.
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."
284 ;; This function does not do any hidden buffer changes.
290 (setq char (aref str p))
292 (if (or (= char ?\ ) (= char ?\t))
294 (setq return (concat return (char-to-string char)))))
297 (defun cc-imenu-objc-function ()
298 "imenu supports for objc-mode."
299 ;; This function does not do any hidden buffer changes.
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 has 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 ;; This function does not do any hidden buffer changes.
419 (setq imenu-generic-expression mode-generic-expression
420 imenu-case-fold-search nil)
421 (when mode-create-index-function
422 (setq imenu-create-index-function mode-create-index-function)))
425 (cc-provide 'cc-menus)
427 ;;; arch-tag: f6b60933-91f0-4145-ab44-70ca6d1b919b
428 ;;; cc-menus.el ends here