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