lisp/subr.el (display-delayed-warnings): Collapse identical adjacent messages.
[emacs.git] / lisp / progmodes / cc-menus.el
blobf57fcbff5ca600aa253fa3a530d23fcaca492926
1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs
8 ;; 1987 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 ;; Keywords: c languages
13 ;; Package: cc-mode
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 3 of the License, or
20 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;;; Code:
34 (eval-when-compile
35 (let ((load-path
36 (if (and (boundp 'byte-compile-dest-file)
37 (stringp byte-compile-dest-file))
38 (cons (file-name-directory byte-compile-dest-file) load-path)
39 load-path)))
40 (load "cc-bytecomp" nil t)))
42 (cc-require 'cc-defs)
44 ;; The things referenced in imenu, which we don't require.
45 (cc-bytecomp-defvar imenu-case-fold-search)
46 (cc-bytecomp-defvar imenu-generic-expression)
47 (cc-bytecomp-defvar imenu-create-index-function)
50 ;; imenu integration
51 (defvar cc-imenu-c-prototype-macro-regexp nil
52 "RE matching macro names used to conditionally specify function prototypes.
54 For example:
56 #ifdef __STDC__
57 #define _P(x) x
58 #else
59 #define _P(x) /*nothing*/
60 #endif
62 int main _P( (int argc, char *argv[]) )
64 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
66 (defvar cc-imenu-c++-generic-expression
68 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
69 ;; will be incorrectly recognized as function `new ()' because the regexps
70 ;; work by backtracking from the end of the definition.
71 (nil
72 ,(concat
73 "^\\<.*"
74 "[^" c-alnum "_:<>~]" ; match any non-identifier char
75 ; (note: this can be `\n')
76 "\\("
77 "\\([" c-alnum "_:<>~]*::\\)?" ; match an operator
78 "operator\\>[ \t]*"
79 "\\(()\\|[^(]*\\)" ; special case for `()' operator
80 "\\)"
82 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
83 ; require something other than
84 ; a `;' after the (...) to
85 ; avoid prototypes. Can't
86 ; catch cases with () inside
87 ; the parentheses surrounding
88 ; the parameters. e.g.:
89 ; `int foo(int a=bar()) {...}'
90 ) 1)
91 ;; Special case to match a line like `main() {}'
92 ;; e.g. no return type, not even on the previous line.
93 (nil
94 ,(concat
95 "^"
96 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
97 "[ \t]*(" ; see above, BUT
98 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
99 "[ \t]*[^ \t;(]" ; with an asterisk or parentheses
100 ) 1)
101 ;; General function name regexp
102 (nil
103 ,(concat
104 "^\\<" ; line MUST start with word char
105 ;; \n added to prevent overflow in regexp matcher.
106 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-02/msg00021.html
107 "[^()\n]*" ; 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\\)*" ; must not start
112 "\\([^ \t\n(*]" ; with an asterisk or parentheses
113 "[^()]*\\(([^()]*)[^()]*\\)*" ; Maybe function pointer arguments
114 "\\)?)"
115 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]"
116 ) 1)
117 ;; Special case for definitions using phony prototype macros like:
118 ;; `int main _PROTO( (int argc,char *argv[]) )'.
119 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
120 ;; Only supported in c-code, so no `:<>~' chars in function name!
121 ,@(if cc-imenu-c-prototype-macro-regexp
122 `((nil
123 ,(concat
124 "^\\<.*" ; line MUST start with word char
125 "[^" c-alnum "_]" ; match any non-identifier char
126 "\\([" c-alpha "_][" c-alnum "_]*\\)" ; match function name
127 "[ \t]*" ; whitespace before macro name
128 cc-imenu-c-prototype-macro-regexp
129 "[ \t]*(" ; ws followed by first paren.
130 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
131 ) 1)))
132 ;; Class definitions
133 ("Class"
134 ,(concat
135 "^" ; beginning of line is required
136 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
137 "\\(class\\|struct\\)[ \t]+"
138 "\\(" ; the string we want to get
139 "[" c-alnum "_]+" ; class name
140 "\\(<[^>]+>\\)?" ; possibly explicitly specialized
141 "\\)"
142 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
143 ) 3))
144 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
146 (defvar cc-imenu-c-generic-expression
147 cc-imenu-c++-generic-expression
148 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
150 (defvar cc-imenu-java-generic-expression
151 `((nil
152 ,(concat
153 "[" c-alpha "_][\]\[." c-alnum "_<> ]+[ \t\n\r]+" ; type spec
154 "\\([" c-alpha "_][" c-alnum "_]*\\)" ; method name
155 "[ \t\n\r]*"
156 ;; An argument list htat is either empty or contains any number
157 ;; of arguments. An argument is any number of annotations
158 ;; followed by a type spec followed by a word. A word is an
159 ;; identifier. A type spec is an identifier, possibly followed
160 ;; by < typespec > possibly followed by [].
161 (concat "("
162 "\\("
163 "[ \t\n\r]*"
164 "\\("
166 "[" c-alpha "_]"
167 "[" c-alnum "._]""*"
168 "[ \t\n\r]+"
169 "\\)*"
170 "\\("
171 "[" c-alpha "_]"
172 "[\]\[" c-alnum "_.]*"
173 "\\("
176 "[ \t\n\r]*"
177 "[\]\[.," c-alnum "_<> \t\n\r]*"
179 "\\)?"
180 "\\(\\[\\]\\)?"
181 "[ \t\n\r]+"
182 "\\)"
183 "[" c-alpha "_]"
184 "[" c-alnum "_]*"
185 "[ \t\n\r,]*"
186 "\\)*"
188 "[.," c-alnum " \t\n\r]*"
190 )) 1))
191 "Imenu generic expression for Java mode. See
192 `imenu-generic-expression'.")
194 ;; *Warning for cc-mode developers*
196 ;; `cc-imenu-objc-generic-expression' elements depend on
197 ;; `cc-imenu-c++-generic-expression'. So if you change this
198 ;; expression, you need to change following variables,
199 ;; `cc-imenu-objc-generic-expression-*-index',
200 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
201 ;; order to know where the each regexp *group \\(foobar\\)* elements
202 ;; are started.
204 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
205 ;; being initialized.
208 ;; Internal variables
209 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
210 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
211 (defvar cc-imenu-objc-generic-expression-proto-index nil)
212 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
214 (defvar cc-imenu-objc-generic-expression
215 (concat
217 ;; For C
219 ;; > Special case to match a line like `main() {}'
220 ;; > e.g. no return type, not even on the previous line.
221 ;; Pick a token by (match-string 1)
222 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
223 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
224 "\\|"
225 ;; > General function name regexp
226 ;; Pick a token by (match-string 3)
227 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 5
228 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
229 ;; > Special case for definitions using phony prototype macros like:
230 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
231 ;; Pick a token by (match-string 8)
232 (if cc-imenu-c-prototype-macro-regexp
233 (concat
234 "\\|"
235 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
236 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
238 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 8) "")
239 "") ; -> index += 0
240 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 8) "")
242 ;; For Objective-C
243 ;; Pick a token by (match-string 8 or 9)
245 "\\|\\("
246 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
247 "\\|"
248 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
249 "\\|"
250 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
251 "\\|"
252 ;; For NSObject, NSProxy and Object... They don't have super class.
253 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
254 "\\|"
255 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
256 "\\|"
257 "^@implementation[\t ]+[" c-alnum "_]+"
258 "\\|"
259 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
260 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
263 ;; Imenu support for objective-c uses functions.
264 (defsubst cc-imenu-objc-method-to-selector (method)
265 "Return the objc selector style string of METHOD.
266 Example:
267 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
269 -perform:withObject:withObject:withObject: /* selector */"
270 (let ((return "") ; String to be returned
271 (p 0) ; Current scanning position in METHOD
272 (pmax (length method)) ;
273 char ; Current scanning target
274 (betweenparen 0) ; CHAR is in parentheses.
275 argreq ; An argument is required.
276 inargvar) ; position of CHAR is in an argument variable.
277 (while (< p pmax)
278 (setq char (aref method p)
279 p (1+ p))
280 (cond
281 ;; Is CHAR part of a objc token?
282 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
283 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
284 (or (and (<= ?a char) (<= char ?z))
285 (and (<= ?A char) (<= char ?Z))
286 (and (<= ?0 char) (<= char ?9))
287 (= ?_ char)))
288 (if argreq
289 (setq inargvar t
290 argreq nil)
291 (setq return (concat return (char-to-string char)))))
292 ;; Or a white space?
293 ((and inargvar (or (eq ?\ char) (eq ?\n char))
294 (setq inargvar nil)))
295 ;; Or a method separator?
296 ;; If a method separator, the next token will be an argument variable.
297 ((eq ?: char)
298 (setq argreq t
299 return (concat return (char-to-string char))))
300 ;; Or an open parentheses?
301 ((eq ?\( char)
302 (setq betweenparen (1+ betweenparen)))
303 ;; Or a close parentheses?
304 ((eq ?\) char)
305 (setq betweenparen (1- betweenparen)))))
306 return))
308 (defun cc-imenu-objc-remove-white-space (str)
309 "Remove all spaces and tabs from STR."
310 (let ((return "")
311 (p 0)
312 (max (length str))
313 char)
314 (while (< p max)
315 (setq char (aref str p))
316 (setq p (1+ p))
317 (if (or (= char ?\ ) (= char ?\t))
319 (setq return (concat return (char-to-string char)))))
320 return))
322 (defun cc-imenu-objc-function ()
323 "Imenu support for Objective C mode."
324 (let (methodlist
325 clist
327 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
329 ;; *Warning for developers*
330 ;; These constants depend on `cc-imenu-c++-generic-expression'.
332 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
333 ;; Special case to match a line like `main() {}'
334 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
335 ;; General function name regexp
336 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
337 ;; Special case for definitions using phony prototype macros like:
338 (Cproto cc-imenu-objc-generic-expression-proto-index)
339 langnum
341 (classcount 0)
342 toplist
344 str2
345 (intflen (length "@interface"))
346 (implen (length "@implementation"))
347 (prtlen (length "@protocol"))
348 (func
350 ;; Does this emacs have buffer-substring-no-properties?
352 (if (fboundp 'buffer-substring-no-properties)
353 'buffer-substring-no-properties
354 'buffer-substring)))
355 (goto-char (point-max))
357 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
358 (setq langnum (if (match-beginning OBJC)
359 OBJC
360 (cond
361 ((match-beginning Cproto) Cproto)
362 ((match-beginning Cgeneralfunc) Cgeneralfunc)
363 ((match-beginning Cnoreturn) Cnoreturn))))
364 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
366 (cond
368 ;; C
370 ((not (eq langnum OBJC))
371 (setq clist (cons (cons str (match-beginning langnum)) clist)))
373 ;; ObjC
375 ;; An instance Method
376 ((eq (aref str 0) ?-)
377 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
378 (setq methodlist (cons (cons str
379 (match-beginning langnum))
380 methodlist)))
381 ;; A factory Method
382 ((eq (aref str 0) ?+)
383 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
384 (setq methodlist (cons (cons str
385 (match-beginning langnum))
386 methodlist)))
387 ;; Interface or implementation or protocol
388 ((eq (aref str 0) ?@)
389 (setq classcount (1+ classcount))
390 (cond
391 ((and (> (length str) implen)
392 (string= (substring str 0 implen) "@implementation"))
393 (setq str (substring str implen)
394 str2 "@implementation"))
395 ((string= (substring str 0 intflen) "@interface")
396 (setq str (substring str intflen)
397 str2 "@interface"))
398 ((string= (substring str 0 prtlen) "@protocol")
399 (setq str (substring str prtlen)
400 str2 "@protocol")))
401 (setq str (cc-imenu-objc-remove-white-space str))
402 (setq methodlist (cons (cons str2
403 (match-beginning langnum))
404 methodlist))
405 (setq toplist (cons nil (cons (cons str
406 methodlist) toplist))
407 methodlist nil))))
409 (if (eq (car toplist) nil)
410 (setq toplist (cdr toplist)))
412 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
413 (if (< classcount 2)
414 (let ((classname (car (car toplist)))
415 (p (cdr (car (cdr (car toplist)))))
416 last)
417 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
418 ;; Add C lang token
419 (if clist
420 (progn
421 (setq last toplist)
422 (while (cdr last)
423 (setq last (cdr last)))
424 (setcdr last clist))))
425 ;; Add C lang tokens as a sub menu
426 (if clist
427 (setq toplist (cons (cons "C" clist) toplist))))
429 toplist
432 ;(defvar cc-imenu-pike-generic-expression
433 ; ())
434 ; FIXME: Please contribute one!
436 (defun cc-imenu-init (mode-generic-expression
437 &optional mode-create-index-function)
438 (setq imenu-generic-expression mode-generic-expression
439 imenu-case-fold-search nil)
440 (when mode-create-index-function
441 (setq imenu-create-index-function mode-create-index-function)))
444 (cc-provide 'cc-menus)
446 ;;; cc-menus.el ends here