1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2000 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 the
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
34 (if (and (boundp 'byte-compile-current-file)
35 (stringp byte-compile-current-file))
36 (cons (file-name-directory byte-compile-current-file)
39 (load "cc-defs" nil t)))
41 ;; Dummy definitions to shut up the compiler in case imenu doesn't exist.
42 (defvar imenu-generic-expression)
43 (defvar imenu-case-fold-search)
44 (or (fboundp 'imenu-progress-message)
45 (defun imenu-progress-message (&rest args) nil))
47 ;; Try to pull in imenu.
55 (defvar cc-imenu-c-prototype-macro-regexp nil
56 "RE matching macro names used to conditionally specify function prototypes.
63 #define _P(x) /*nothing*/
66 int main _P( (int argc, char *argv[]) )
68 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
70 (defvar cc-imenu-c++-generic-expression
72 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
73 ;; will be incorrectly recognised as function `new ()' because the regexps
74 ;; work by backtracking from the end of the definition.
78 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
79 ; (note: this can be `\n')
81 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
83 "\\(()\\|[^(]*\\)" ; special case for `()' operator
86 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
87 ; require something other than
88 ; a `;' after the (...) to
89 ; avoid prototypes. Can't
90 ; catch cases with () inside
91 ; the parentheses surrounding
92 ; the parameters. e.g.:
93 ; `int foo(int a=bar()) {...}'
95 ;; Special case to match a line like `main() {}'
96 ;; e.g. no return type, not even on the previous line.
100 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
101 "[ \t]*(" ; see above, BUT
102 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
103 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
105 ;; General function name regexp
108 "^\\<" ; line MUST start with word char
109 "[^()]*" ; no parentheses before
110 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
111 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
112 "[ \t]*(" ; see above, BUT
113 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
114 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
116 ;; Special case for definitions using phony prototype macros like:
117 ;; `int main _PROTO( (int argc,char *argv[]) )'.
118 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
119 ;; Only supported in c-code, so no `:<>~' chars in function name!
120 ,@(if cc-imenu-c-prototype-macro-regexp
123 "^\\<.*" ; line MUST start with word char
124 "[^a-zA-Z0-9_]" ; match any non-identifier char
125 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
126 "[ \t]*" ; whitespace before macro name
127 cc-imenu-c-prototype-macro-regexp
128 "[ \t]*(" ; ws followed by first paren.
129 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
134 "^" ; beginning of line is required
135 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
137 "\\(" ; the string we want to get
138 "[a-zA-Z0-9_]+" ; class name
139 "\\(<[^>]+>\\)?" ; possibly explicitely specialized
143 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
145 (defvar cc-imenu-c-generic-expression
146 cc-imenu-c++-generic-expression
147 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
149 (defvar cc-imenu-java-generic-expression
153 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
154 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
155 "\\([.A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
157 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
159 "[][a-zA-Z,_1-9\n \t]*" ; arguments
162 "[,a-zA-Z_1-9\n \t]*{"
164 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
166 ;; *Warning for cc-mode developers*
168 ;; `cc-imenu-objc-generic-expression' elements depend on
169 ;; `cc-imenu-c++-generic-expression'. So if you change this
170 ;; expression, you need to change following variables,
171 ;; `cc-imenu-objc-generic-expression-*-index',
172 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
173 ;; order to know where the each regexp *group \\(foobar\\)* elements
176 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
177 ;; being initialized.
180 ;; Internal variables
181 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
182 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
183 (defvar cc-imenu-objc-generic-expression-proto-index nil)
184 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
186 (defvar cc-imenu-objc-generic-expression
191 ;; > Special case to match a line like `main() {}'
192 ;; > e.g. no return type, not even on the previous line.
193 ;; Pick a token by (match-string 1)
194 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
195 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
197 ;; > General function name regexp
198 ;; Pick a token by (match-string 3)
199 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 2
200 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
201 ;; > Special case for definitions using phony prototype macros like:
202 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
203 ;; Pick a token by (match-string 5)
204 (if cc-imenu-c-prototype-macro-regexp
207 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
208 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 6) "")
210 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 5) "")
212 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
215 ;; Pick a token by (match-string 5 or 6)
218 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
220 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
222 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
224 ;; For NSObject, NSProxy and Object... They don't have super class.
225 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
227 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
229 "^@implementation[\t ]+[a-zA-Z0-9_]+"
231 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
232 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
235 ;; Imenu support for objective-c uses functions.
236 (defsubst cc-imenu-objc-method-to-selector (method)
237 "Return the objc selector style string of METHOD.
239 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
241 -perform:withObject:withObject:withObject: /* selector */"
242 (let ((return "") ; String to be returned
243 (p 0) ; Current scanning position in METHOD
244 (pmax (length method)) ;
245 char ; Current scanning target
246 (betweenparen 0) ; CHAR is in parentheses.
247 argreq ; An argument is required.
248 inargvar) ; position of CHAR is in an argument variable.
250 (setq char (aref method p)
253 ;; Is CHAR part of a objc token?
254 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
255 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
256 (or (and (<= ?a char) (<= char ?z))
257 (and (<= ?A char) (<= char ?Z))
258 (and (<= ?0 char) (<= char ?9))
263 (setq return (concat return (char-to-string char)))))
265 ((and inargvar (or (eq ?\ char) (eq ?\n char))
266 (setq inargvar nil)))
267 ;; Or a method separator?
268 ;; If a method separator, the next token will be an argument variable.
271 return (concat return (char-to-string char))))
272 ;; Or an open parentheses?
274 (setq betweenparen (1+ betweenparen)))
275 ;; Or a close parentheses?
277 (setq betweenparen (1- betweenparen)))))
280 (defun cc-imenu-objc-remove-white-space (str)
281 "Remove all spaces and tabs from STR."
287 (setq char (aref str p))
289 (if (or (= char ?\ ) (= char ?\t))
291 (setq return (concat return (char-to-string char)))))
294 (defun cc-imenu-objc-function ()
295 "imenu supports for objc-mode."
299 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
301 ;; *Warning for developers*
302 ;; These constants depend on `cc-imenu-c++-generic-expression'.
304 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
305 ;; Special case to match a line like `main() {}'
306 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
307 ;; General function name regexp
308 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
309 ;; Special case for definitions using phony prototype macros like:
310 (Cproto cc-imenu-objc-generic-expression-proto-index)
318 (intflen (length "@interface"))
319 (implen (length "@implementation"))
320 (prtlen (length "@protocol"))
323 ;; Does this emacs has buffer-substring-no-properties?
325 (if (fboundp 'buffer-substring-no-properties)
326 'buffer-substring-no-properties
328 (goto-char (point-max))
329 (imenu-progress-message stupid 0)
331 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
332 (imenu-progress-message stupid)
333 (setq langnum (if (match-beginning OBJC)
336 ((match-beginning Cproto) Cproto)
337 ((match-beginning Cgeneralfunc) Cgeneralfunc)
338 ((match-beginning Cnoreturn) Cnoreturn))))
339 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
345 ((not (eq langnum OBJC))
346 (setq clist (cons (cons str (match-beginning langnum)) clist)))
350 ;; An instance Method
351 ((eq (aref str 0) ?-)
352 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
353 (setq methodlist (cons (cons str
354 (match-beginning langnum))
357 ((eq (aref str 0) ?+)
358 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
359 (setq methodlist (cons (cons str
360 (match-beginning langnum))
362 ;; Interface or implementation or protocol
363 ((eq (aref str 0) ?@)
364 (setq classcount (1+ classcount))
366 ((and (> (length str) implen)
367 (string= (substring str 0 implen) "@implementation"))
368 (setq str (substring str implen)
369 str2 "@implementation"))
370 ((string= (substring str 0 intflen) "@interface")
371 (setq str (substring str intflen)
373 ((string= (substring str 0 prtlen) "@protocol")
374 (setq str (substring str prtlen)
376 (setq str (cc-imenu-objc-remove-white-space str))
377 (setq methodlist (cons (cons str2
378 (match-beginning langnum))
380 (setq toplist (cons nil (cons (cons str
381 methodlist) toplist))
384 (imenu-progress-message stupid 100)
385 (if (eq (car toplist) nil)
386 (setq toplist (cdr toplist)))
388 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
390 (let ((classname (car (car toplist)))
391 (p (cdr (car (cdr (car toplist)))))
393 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
399 (setq last (cdr last)))
400 (setcdr last clist))))
401 ;; Add C lang tokens as a sub menu
402 (setq toplist (cons (cons "C" clist) toplist)))
407 ;(defvar cc-imenu-pike-generic-expression
409 ; FIXME: Please contribute one!
411 (defun cc-imenu-init (mode-generic-expression)
412 (setq imenu-generic-expression mode-generic-expression
413 imenu-case-fold-search nil))
417 ;;; cc-menus.el ends here