1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman
8 ;; Maintainer: cc-mode-help@python.org
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Keywords: c languages oop
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 (defvar cc-imenu-c-prototype-macro-regexp nil
33 "RE matching macro names used to conditionally specify function prototypes.
40 #define _P(x) /*nothing*/
43 int main _P( (int argc, char *argv[]) )
45 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
47 (defvar cc-imenu-c++-generic-expression
50 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
51 ;; will be incorrectly recognised as function `new ()' because the regexps
52 ;; work by backtracking from the end of the definition.
57 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
58 ; (note: this can be `\n')
60 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
62 "\\(()\\|[^(]*\\)" ; special case for `()' operator
65 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
66 ; require something other than
67 ; a `;' after the (...) to
68 ; avoid prototypes. Can't
69 ; catch cases with () inside
70 ; the parentheses surrounding
71 ; the parameters. e.g.:
72 ; `int foo(int a=bar()) {...}'
74 ;; Special case to match a line like `main() {}'
75 ;; e.g. no return type, not even on the previous line.
80 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
81 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; see above
83 ;; Special case for definitions using phony prototype macros like:
84 ;; `int main _PROTO( (int argc,char *argv[]) )'.
85 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
86 ;; Only supported in c-code, so no `:<>~' chars in function name!
87 (,@ (if cc-imenu-c-prototype-macro-regexp
91 "^\\<.*" ; line MUST start with word char
92 "[^a-zA-Z0-9_]" ; match any non-identifier char
93 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
94 "[ \t]*" ; whitespace before macro name
95 cc-imenu-c-prototype-macro-regexp
96 "[ \t]*(" ; ws followed by first paren.
97 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; see above
99 ;; General function name regexp
103 "^\\<.*" ; line MUST start with word char
104 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
105 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
106 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; see above
111 "^" ; beginning of line is required
112 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
114 "\\([a-zA-Z0-9_]+\\)" ; the string we want to get
117 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
119 (defvar cc-imenu-c-generic-expression
120 cc-imenu-c++-generic-expression
121 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
123 (defvar cc-imenu-java-generic-expression
129 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
130 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
131 "\\([A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
133 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
135 "\\([a-zA-Z,_1-9\n \t]*[[]?[]]?\\)*" ; arguments
138 "[,a-zA-Z_1-9\n \t]*{"
140 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
142 (defvar cc-imenu-objc-generic-expression
145 ;; Pick a token by (match-string 6)
146 (car (cdr (car cc-imenu-c++-generic-expression)))
148 ;; Pick a token by (match-string 8)
150 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
152 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
154 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
156 ;; For NSObject, NSProxy and Object... They don't have super class.
157 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
159 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
161 "^@implementation[\t ]+[a-zA-Z0-9_]+"
163 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
164 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
167 ;; Imenu support for objective-c uses functions.
168 (defsubst cc-imenu-objc-method-to-selector (method)
169 "Return the objc selector style string of METHOD.
171 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
173 -perform:withObject:withObject:withObject: /* selector */"
174 (let ((return "") ; String to be returned
175 (p 0) ; Current scanning position in METHOD
176 (pmax (length method)) ;
177 char ; Current scanning target
178 (betweenparen 0) ; CHAR is in parentheses.
179 argreq ; An argument is required.
180 inargvar) ; position of CHAR is in an argument variable.
182 (setq char (aref method p)
185 ;; Is CHAR part of a objc token?
186 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
187 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
188 (or (and (<= ?a char) (<= char ?z))
189 (and (<= ?A char) (<= char ?Z))
190 (and (<= ?0 char) (<= char ?9))
195 (setq return (concat return (char-to-string char)))))
197 ((and inargvar (or (eq ?\ char) (eq ?\n char))
198 (setq inargvar nil)))
199 ;; Or a method separator?
200 ;; If a method separator, the next token will be an argument variable.
203 return (concat return (char-to-string char))))
204 ;; Or an open parentheses?
206 (setq betweenparen (1+ betweenparen)))
207 ;; Or a close parentheses?
209 (setq betweenparen (1- betweenparen)))))
212 (defun cc-imenu-objc-remove-white-space (str)
213 "Remove all spaces and tabs from STR."
219 (setq char (aref str p))
221 (if (or (= char ?\ ) (= char ?\t))
223 (setq return (concat return (char-to-string char)))))
226 (defun cc-imenu-objc-function ()
227 "imenu supports for objc-mode."
238 (intflen (length "@interface"))
239 (implen (length "@implementation"))
240 (prtlen (length "@protocol"))
243 ;; Does this emacs has buffer-substring-no-properties?
245 (fset 'bufsubst-fun (if (fboundp 'buffer-substring-no-properties)
246 (symbol-function 'buffer-substring-no-properties)
247 (symbol-function 'buffer-substring)))
248 (goto-char (point-max))
249 (imenu-progress-message stupid 0)
251 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
252 (imenu-progress-message stupid)
253 (setq langnum (if (match-beginning C) C OBJC))
254 (setq str (bufsubst-fun (match-beginning langnum) (match-end langnum)))
261 (setq clist (cons (cons str (match-beginning langnum)) clist)))
265 ;; An instance Method
266 ((eq (aref str 0) ?-)
267 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
268 (setq methodlist (cons (cons str
269 (match-beginning langnum))
272 ((eq (aref str 0) ?+)
273 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
274 (setq methodlist (cons (cons str
275 (match-beginning langnum))
277 ;; Interface or implementation or protocol
278 ((eq (aref str 0) ?@)
279 (setq classcount (1+ classcount))
281 ((and (> (length str) implen)
282 (string= (substring str 0 implen) "@implementation"))
283 (setq str (substring str implen)
284 str2 "@implementation"))
285 ((string= (substring str 0 intflen) "@interface")
286 (setq str (substring str intflen)
288 ((string= (substring str 0 prtlen) "@protocol")
289 (setq str (substring str prtlen)
291 (setq str (cc-imenu-objc-remove-white-space str))
292 (setq methodlist (cons (cons str2
293 (match-beginning langnum))
295 (setq toplist (cons nil (cons (cons str
296 methodlist) toplist))
299 (imenu-progress-message stupid 100)
300 (if (eq (car toplist) nil)
301 (setq toplist (cdr toplist)))
303 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
305 (let ((classname (car (car toplist)))
306 (p (cdr (car (cdr (car toplist)))))
308 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
314 (setq last (cdr last)))
315 (setcdr last clist))))
316 ;; Add C lang tokens as a sub menu
317 (setq toplist (cons (cons "C" clist) toplist)))
324 ;;; cc-menus.el ends here