1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-2001 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 this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
34 (if (and (boundp 'byte-compile-dest-file)
35 (stringp byte-compile-dest-file))
36 (cons (file-name-directory byte-compile-dest-file) load-path)
38 (require 'cc-bytecomp)))
40 ;; Try to pull in imenu if it exists.
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-defun imenu-progress-message)
52 (defvar cc-imenu-c-prototype-macro-regexp nil
53 "RE matching macro names used to conditionally specify function prototypes.
60 #define _P(x) /*nothing*/
63 int main _P( (int argc, char *argv[]) )
65 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
67 (defvar cc-imenu-c++-generic-expression
69 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
70 ;; will be incorrectly recognised as function `new ()' because the regexps
71 ;; work by backtracking from the end of the definition.
75 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
76 ; (note: this can be `\n')
78 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
80 "\\(()\\|[^(]*\\)" ; special case for `()' operator
83 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
84 ; require something other than
85 ; a `;' after the (...) to
86 ; avoid prototypes. Can't
87 ; catch cases with () inside
88 ; the parentheses surrounding
89 ; the parameters. e.g.:
90 ; `int foo(int a=bar()) {...}'
92 ;; Special case to match a line like `main() {}'
93 ;; e.g. no return type, not even on the previous line.
97 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
98 "[ \t]*(" ; see above, BUT
99 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
100 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
102 ;; General function name regexp
105 "^\\<" ; line MUST start with word char
106 "[^()]*" ; no parentheses before
107 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
108 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
109 "[ \t]*(" ; see above, BUT
110 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
111 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
113 ;; Special case for definitions using phony prototype macros like:
114 ;; `int main _PROTO( (int argc,char *argv[]) )'.
115 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
116 ;; Only supported in c-code, so no `:<>~' chars in function name!
117 ,@(if cc-imenu-c-prototype-macro-regexp
120 "^\\<.*" ; line MUST start with word char
121 "[^a-zA-Z0-9_]" ; match any non-identifier char
122 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
123 "[ \t]*" ; whitespace before macro name
124 cc-imenu-c-prototype-macro-regexp
125 "[ \t]*(" ; ws followed by first paren.
126 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
131 "^" ; beginning of line is required
132 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
133 "\\(class\\|struct\\)[ \t]+"
134 "\\(" ; the string we want to get
135 "[a-zA-Z0-9_]+" ; class name
136 "\\(<[^>]+>\\)?" ; possibly explicitly specialized
140 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
142 (defvar cc-imenu-c-generic-expression
143 cc-imenu-c++-generic-expression
144 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
146 (defvar cc-imenu-java-generic-expression
150 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
151 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
152 "\\([.A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
154 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
156 "[][a-zA-Z,_1-9\n \t]*" ; arguments
159 "[,a-zA-Z_1-9\n \t]*{"
161 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
163 ;; *Warning for cc-mode developers*
165 ;; `cc-imenu-objc-generic-expression' elements depend on
166 ;; `cc-imenu-c++-generic-expression'. So if you change this
167 ;; expression, you need to change following variables,
168 ;; `cc-imenu-objc-generic-expression-*-index',
169 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
170 ;; order to know where the each regexp *group \\(foobar\\)* elements
173 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
174 ;; being initialized.
177 ;; Internal variables
178 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
179 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
180 (defvar cc-imenu-objc-generic-expression-proto-index nil)
181 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
183 (defvar cc-imenu-objc-generic-expression
188 ;; > Special case to match a line like `main() {}'
189 ;; > e.g. no return type, not even on the previous line.
190 ;; Pick a token by (match-string 1)
191 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
192 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
194 ;; > General function name regexp
195 ;; Pick a token by (match-string 3)
196 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 2
197 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
198 ;; > Special case for definitions using phony prototype macros like:
199 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
200 ;; Pick a token by (match-string 5)
201 (if cc-imenu-c-prototype-macro-regexp
204 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
205 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 6) "")
207 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 5) "")
209 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
212 ;; Pick a token by (match-string 5 or 6)
215 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
217 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
219 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
221 ;; For NSObject, NSProxy and Object... They don't have super class.
222 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
224 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
226 "^@implementation[\t ]+[a-zA-Z0-9_]+"
228 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
229 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
232 ;; Imenu support for objective-c uses functions.
233 (defsubst cc-imenu-objc-method-to-selector (method)
234 "Return the objc selector style string of METHOD.
236 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
238 -perform:withObject:withObject:withObject: /* selector */"
239 (let ((return "") ; String to be returned
240 (p 0) ; Current scanning position in METHOD
241 (pmax (length method)) ;
242 char ; Current scanning target
243 (betweenparen 0) ; CHAR is in parentheses.
244 argreq ; An argument is required.
245 inargvar) ; position of CHAR is in an argument variable.
247 (setq char (aref method p)
250 ;; Is CHAR part of a objc token?
251 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
252 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
253 (or (and (<= ?a char) (<= char ?z))
254 (and (<= ?A char) (<= char ?Z))
255 (and (<= ?0 char) (<= char ?9))
260 (setq return (concat return (char-to-string char)))))
262 ((and inargvar (or (eq ?\ char) (eq ?\n char))
263 (setq inargvar nil)))
264 ;; Or a method separator?
265 ;; If a method separator, the next token will be an argument variable.
268 return (concat return (char-to-string char))))
269 ;; Or an open parentheses?
271 (setq betweenparen (1+ betweenparen)))
272 ;; Or a close parentheses?
274 (setq betweenparen (1- betweenparen)))))
277 (defun cc-imenu-objc-remove-white-space (str)
278 "Remove all spaces and tabs from STR."
284 (setq char (aref str p))
286 (if (or (= char ?\ ) (= char ?\t))
288 (setq return (concat return (char-to-string char)))))
291 (defun cc-imenu-objc-function ()
292 "imenu supports for objc-mode."
296 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
298 ;; *Warning for developers*
299 ;; These constants depend on `cc-imenu-c++-generic-expression'.
301 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
302 ;; Special case to match a line like `main() {}'
303 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
304 ;; General function name regexp
305 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
306 ;; Special case for definitions using phony prototype macros like:
307 (Cproto cc-imenu-objc-generic-expression-proto-index)
315 (intflen (length "@interface"))
316 (implen (length "@implementation"))
317 (prtlen (length "@protocol"))
320 ;; Does this emacs has buffer-substring-no-properties?
322 (if (fboundp 'buffer-substring-no-properties)
323 'buffer-substring-no-properties
325 (goto-char (point-max))
326 (imenu-progress-message stupid 0)
328 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
329 (imenu-progress-message stupid)
330 (setq langnum (if (match-beginning OBJC)
333 ((match-beginning Cproto) Cproto)
334 ((match-beginning Cgeneralfunc) Cgeneralfunc)
335 ((match-beginning Cnoreturn) Cnoreturn))))
336 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
342 ((not (eq langnum OBJC))
343 (setq clist (cons (cons str (match-beginning langnum)) clist)))
347 ;; An instance Method
348 ((eq (aref str 0) ?-)
349 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
350 (setq methodlist (cons (cons str
351 (match-beginning langnum))
354 ((eq (aref str 0) ?+)
355 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
356 (setq methodlist (cons (cons str
357 (match-beginning langnum))
359 ;; Interface or implementation or protocol
360 ((eq (aref str 0) ?@)
361 (setq classcount (1+ classcount))
363 ((and (> (length str) implen)
364 (string= (substring str 0 implen) "@implementation"))
365 (setq str (substring str implen)
366 str2 "@implementation"))
367 ((string= (substring str 0 intflen) "@interface")
368 (setq str (substring str intflen)
370 ((string= (substring str 0 prtlen) "@protocol")
371 (setq str (substring str prtlen)
373 (setq str (cc-imenu-objc-remove-white-space str))
374 (setq methodlist (cons (cons str2
375 (match-beginning langnum))
377 (setq toplist (cons nil (cons (cons str
378 methodlist) toplist))
381 (imenu-progress-message stupid 100)
382 (if (eq (car toplist) nil)
383 (setq toplist (cdr toplist)))
385 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
387 (let ((classname (car (car toplist)))
388 (p (cdr (car (cdr (car toplist)))))
390 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
396 (setq last (cdr last)))
397 (setcdr last clist))))
398 ;; Add C lang tokens as a sub menu
399 (setq toplist (cons (cons "C" clist) toplist)))
404 ;(defvar cc-imenu-pike-generic-expression
406 ; FIXME: Please contribute one!
408 (defun cc-imenu-init (mode-generic-expression)
409 (setq imenu-generic-expression mode-generic-expression
410 imenu-case-fold-search nil))
413 (cc-provide 'cc-menus)
414 ;;; cc-menus.el ends here