(custom-set-variables): Print message about errors in
[emacs.git] / lisp / progmodes / cc-menus.el
blob5504a4b04aa4ed22943e1d4a8d8526c72ebcbd3f
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)
20 ;; any later version.
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.
32 (eval-when-compile
33 (let ((load-path
34 (if (and (boundp 'byte-compile-current-file)
35 (stringp byte-compile-current-file))
36 (cons (file-name-directory byte-compile-current-file)
37 load-path)
38 load-path)))
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.
48 (eval-and-compile
49 (condition-case nil
50 (require 'imenu)
51 (error nil)))
54 ;; imenu integration
55 (defvar cc-imenu-c-prototype-macro-regexp nil
56 "RE matching macro names used to conditionally specify function prototypes.
58 For example:
60 #ifdef __STDC__
61 #define _P(x) x
62 #else
63 #define _P(x) /*nothing*/
64 #endif
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.
75 (nil
76 ,(concat
77 "^\\<.*"
78 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
79 ; (note: this can be `\n')
80 "\\("
81 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
82 "operator\\>[ \t]*"
83 "\\(()\\|[^(]*\\)" ; special case for `()' operator
84 "\\)"
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()) {...}'
94 ) 1)
95 ;; Special case to match a line like `main() {}'
96 ;; e.g. no return type, not even on the previous line.
97 (nil
98 ,(concat
99 "^"
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
104 ) 1)
105 ;; General function name regexp
106 (nil
107 ,(concat
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
115 ) 1)
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
121 `((nil
122 ,(concat
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
130 ) 1)))
131 ;; Class definitions
132 ("Class"
133 ,(concat
134 "^" ; beginning of line is required
135 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
136 "class[ \t]+"
137 "\\(" ; the string we want to get
138 "[a-zA-Z0-9_]+" ; class name
139 "\\(<[^>]+>\\)?" ; possibly explicitely specialized
140 "\\)"
141 "[ \t\n]*[:{]"
142 ) 2))
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
150 `((nil
151 ,(concat
152 "^\\([ \t]\\)*"
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]*[[]?[]]?\\)"
156 "\\([ \t]\\)"
157 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
158 "\\([ \t]*\\)+("
159 "[][a-zA-Z,_1-9\n \t]*" ; arguments
160 ")[ \t]*"
161 ; "[^;(]"
162 "[,a-zA-Z_1-9\n \t]*{"
163 ) 6))
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
174 ;; are started.
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
187 (concat
189 ;; For C
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) "")
196 "\\|"
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
205 (concat
206 "\\|"
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) "")
211 "") ; -> index += 0
212 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
214 ;; For Objective-C
215 ;; Pick a token by (match-string 5 or 6)
217 "\\|\\("
218 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
219 "\\|"
220 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
221 "\\|"
222 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
223 "\\|"
224 ;; For NSObject, NSProxy and Object... They don't have super class.
225 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
226 "\\|"
227 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
228 "\\|"
229 "^@implementation[\t ]+[a-zA-Z0-9_]+"
230 "\\|"
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.
238 Example:
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.
249 (while (< p pmax)
250 (setq char (aref method p)
251 p (1+ p))
252 (cond
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))
259 (= ?_ char)))
260 (if argreq
261 (setq inargvar t
262 argreq nil)
263 (setq return (concat return (char-to-string char)))))
264 ;; Or a white space?
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.
269 ((eq ?: char)
270 (setq argreq t
271 return (concat return (char-to-string char))))
272 ;; Or an open parentheses?
273 ((eq ?\( char)
274 (setq betweenparen (1+ betweenparen)))
275 ;; Or a close parentheses?
276 ((eq ?\) char)
277 (setq betweenparen (1- betweenparen)))))
278 return))
280 (defun cc-imenu-objc-remove-white-space (str)
281 "Remove all spaces and tabs from STR."
282 (let ((return "")
283 (p 0)
284 (max (length str))
285 char)
286 (while (< p max)
287 (setq char (aref str p))
288 (setq p (1+ p))
289 (if (or (= char ?\ ) (= char ?\t))
291 (setq return (concat return (char-to-string char)))))
292 return))
294 (defun cc-imenu-objc-function ()
295 "imenu supports for objc-mode."
296 (let (methodlist
297 clist
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)
311 langnum
313 (classcount 0)
314 toplist
315 stupid
317 str2
318 (intflen (length "@interface"))
319 (implen (length "@implementation"))
320 (prtlen (length "@protocol"))
321 (func
323 ;; Does this emacs has buffer-substring-no-properties?
325 (if (fboundp 'buffer-substring-no-properties)
326 'buffer-substring-no-properties
327 'buffer-substring)))
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)
334 OBJC
335 (cond
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)))
341 (cond
343 ;; C
345 ((not (eq langnum OBJC))
346 (setq clist (cons (cons str (match-beginning langnum)) clist)))
348 ;; ObjC
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))
355 methodlist)))
356 ;; A factory Method
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))
361 methodlist)))
362 ;; Interface or implementation or protocol
363 ((eq (aref str 0) ?@)
364 (setq classcount (1+ classcount))
365 (cond
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)
372 str2 "@interface"))
373 ((string= (substring str 0 prtlen) "@protocol")
374 (setq str (substring str prtlen)
375 str2 "@protocol")))
376 (setq str (cc-imenu-objc-remove-white-space str))
377 (setq methodlist (cons (cons str2
378 (match-beginning langnum))
379 methodlist))
380 (setq toplist (cons nil (cons (cons str
381 methodlist) toplist))
382 methodlist nil))))
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}.
389 (if (< classcount 2)
390 (let ((classname (car (car toplist)))
391 (p (cdr (car (cdr (car toplist)))))
392 last)
393 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
394 ;; Add C lang token
395 (if clist
396 (progn
397 (setq last toplist)
398 (while (cdr last)
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)))
404 toplist
407 ;(defvar cc-imenu-pike-generic-expression
408 ; ())
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))
416 (provide 'cc-menus)
417 ;;; cc-menus.el ends here