* progmodes/cc-menus.el (cc-imenu-objc-function): Doc fix.
[bpt/emacs.git] / lisp / progmodes / cc-menus.el
1 ;;; cc-menus.el --- imenu support for CC Mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 1998- Martin Stjernholm
8 ;; 1992-1999 Barry A. Warsaw
9 ;; 1987 Dave Detlefs
10 ;; 1987 Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 ;; \n added to prevent overflow in regexp matcher.
109 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-02/msg00021.html
110 "[^()\n]*" ; no parentheses before
111 "[^" c-alnum "_:<>~]" ; match any non-identifier char
112 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
113 "\\([ \t\n]\\|\\\\\n\\)*(" ; see above, BUT the arg list
114 "\\([ \t\n]\\|\\\\\n\\)*\\([^ \t\n(*][^)]*\\)?)" ; must not start
115 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]" ; with an asterisk or parentheses
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'.")
145
146 (defvar cc-imenu-c-generic-expression
147 cc-imenu-c++-generic-expression
148 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
149
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 that is either empty or contains at least
157 ;; two identifiers with only space between them. This avoids
158 ;; matching e.g. "else if (foo)".
159 (concat "([ \t\n\r]*"
160 "\\([\]\[.," c-alnum "_]+"
161 "[ \t\n\r]+"
162 "[\]\[.," c-alnum "_]"
163 "[\]\[.," c-alnum "_ \t\n\r]*"
164 "\\)?)")
165 "[.," c-alnum "_ \t\n\r]*"
166 "{"
167 ) 1))
168 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
169
170 ;; *Warning for cc-mode developers*
171 ;;
172 ;; `cc-imenu-objc-generic-expression' elements depend on
173 ;; `cc-imenu-c++-generic-expression'. So if you change this
174 ;; expression, you need to change following variables,
175 ;; `cc-imenu-objc-generic-expression-*-index',
176 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
177 ;; order to know where the each regexp *group \\(foobar\\)* elements
178 ;; are started.
179 ;;
180 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
181 ;; being initialized.
182 ;;
183
184 ;; Internal variables
185 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
186 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
187 (defvar cc-imenu-objc-generic-expression-proto-index nil)
188 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
189
190 (defvar cc-imenu-objc-generic-expression
191 (concat
192 ;;
193 ;; For C
194 ;;
195 ;; > Special case to match a line like `main() {}'
196 ;; > e.g. no return type, not even on the previous line.
197 ;; Pick a token by (match-string 1)
198 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
199 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
200 "\\|"
201 ;; > General function name regexp
202 ;; Pick a token by (match-string 3)
203 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 5
204 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
205 ;; > Special case for definitions using phony prototype macros like:
206 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
207 ;; Pick a token by (match-string 8)
208 (if cc-imenu-c-prototype-macro-regexp
209 (concat
210 "\\|"
211 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
212 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
213 )
214 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 8) "")
215 "") ; -> index += 0
216 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 8) "")
217 ;;
218 ;; For Objective-C
219 ;; Pick a token by (match-string 8 or 9)
220 ;;
221 "\\|\\("
222 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
223 "\\|"
224 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
225 "\\|"
226 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
227 "\\|"
228 ;; For NSObject, NSProxy and Object... They don't have super class.
229 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
230 "\\|"
231 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
232 "\\|"
233 "^@implementation[\t ]+[" c-alnum "_]+"
234 "\\|"
235 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
236 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
237
238
239 ;; Imenu support for objective-c uses functions.
240 (defsubst cc-imenu-objc-method-to-selector (method)
241 "Return the objc selector style string of METHOD.
242 Example:
243 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
244 =>
245 -perform:withObject:withObject:withObject: /* selector */"
246 (let ((return "") ; String to be returned
247 (p 0) ; Current scanning position in METHOD
248 (pmax (length method)) ;
249 char ; Current scanning target
250 (betweenparen 0) ; CHAR is in parentheses.
251 argreq ; An argument is required.
252 inargvar) ; position of CHAR is in an argument variable.
253 (while (< p pmax)
254 (setq char (aref method p)
255 p (1+ p))
256 (cond
257 ;; Is CHAR part of a objc token?
258 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
259 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
260 (or (and (<= ?a char) (<= char ?z))
261 (and (<= ?A char) (<= char ?Z))
262 (and (<= ?0 char) (<= char ?9))
263 (= ?_ char)))
264 (if argreq
265 (setq inargvar t
266 argreq nil)
267 (setq return (concat return (char-to-string char)))))
268 ;; Or a white space?
269 ((and inargvar (or (eq ?\ char) (eq ?\n char))
270 (setq inargvar nil)))
271 ;; Or a method separator?
272 ;; If a method separator, the next token will be an argument variable.
273 ((eq ?: char)
274 (setq argreq t
275 return (concat return (char-to-string char))))
276 ;; Or an open parentheses?
277 ((eq ?\( char)
278 (setq betweenparen (1+ betweenparen)))
279 ;; Or a close parentheses?
280 ((eq ?\) char)
281 (setq betweenparen (1- betweenparen)))))
282 return))
283
284 (defun cc-imenu-objc-remove-white-space (str)
285 "Remove all spaces and tabs from STR."
286 (let ((return "")
287 (p 0)
288 (max (length str))
289 char)
290 (while (< p max)
291 (setq char (aref str p))
292 (setq p (1+ p))
293 (if (or (= char ?\ ) (= char ?\t))
294 ()
295 (setq return (concat return (char-to-string char)))))
296 return))
297
298 (defun cc-imenu-objc-function ()
299 "Imenu support for Objective C mode."
300 (let (methodlist
301 clist
302 ;;
303 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
304 ;;
305 ;; *Warning for developers*
306 ;; These constants depend on `cc-imenu-c++-generic-expression'.
307 ;;
308 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
309 ;; Special case to match a line like `main() {}'
310 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
311 ;; General function name regexp
312 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
313 ;; Special case for definitions using phony prototype macros like:
314 (Cproto cc-imenu-objc-generic-expression-proto-index)
315 langnum
316 ;;
317 (classcount 0)
318 toplist
319 stupid
320 str
321 str2
322 (intflen (length "@interface"))
323 (implen (length "@implementation"))
324 (prtlen (length "@protocol"))
325 (func
326 ;;
327 ;; Does this emacs have buffer-substring-no-properties?
328 ;;
329 (if (fboundp 'buffer-substring-no-properties)
330 'buffer-substring-no-properties
331 'buffer-substring)))
332 (goto-char (point-max))
333 (imenu-progress-message stupid 0)
334 ;;
335 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
336 (imenu-progress-message stupid)
337 (setq langnum (if (match-beginning OBJC)
338 OBJC
339 (cond
340 ((match-beginning Cproto) Cproto)
341 ((match-beginning Cgeneralfunc) Cgeneralfunc)
342 ((match-beginning Cnoreturn) Cnoreturn))))
343 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
344 ;;
345 (cond
346 ;;
347 ;; C
348 ;;
349 ((not (eq langnum OBJC))
350 (setq clist (cons (cons str (match-beginning langnum)) clist)))
351 ;;
352 ;; ObjC
353 ;;
354 ;; An instance Method
355 ((eq (aref str 0) ?-)
356 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
357 (setq methodlist (cons (cons str
358 (match-beginning langnum))
359 methodlist)))
360 ;; A factory Method
361 ((eq (aref str 0) ?+)
362 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
363 (setq methodlist (cons (cons str
364 (match-beginning langnum))
365 methodlist)))
366 ;; Interface or implementation or protocol
367 ((eq (aref str 0) ?@)
368 (setq classcount (1+ classcount))
369 (cond
370 ((and (> (length str) implen)
371 (string= (substring str 0 implen) "@implementation"))
372 (setq str (substring str implen)
373 str2 "@implementation"))
374 ((string= (substring str 0 intflen) "@interface")
375 (setq str (substring str intflen)
376 str2 "@interface"))
377 ((string= (substring str 0 prtlen) "@protocol")
378 (setq str (substring str prtlen)
379 str2 "@protocol")))
380 (setq str (cc-imenu-objc-remove-white-space str))
381 (setq methodlist (cons (cons str2
382 (match-beginning langnum))
383 methodlist))
384 (setq toplist (cons nil (cons (cons str
385 methodlist) toplist))
386 methodlist nil))))
387 ;;
388 (imenu-progress-message stupid 100)
389 (if (eq (car toplist) nil)
390 (setq toplist (cdr toplist)))
391
392 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
393 (if (< classcount 2)
394 (let ((classname (car (car toplist)))
395 (p (cdr (car (cdr (car toplist)))))
396 last)
397 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
398 ;; Add C lang token
399 (if clist
400 (progn
401 (setq last toplist)
402 (while (cdr last)
403 (setq last (cdr last)))
404 (setcdr last clist))))
405 ;; Add C lang tokens as a sub menu
406 (if clist
407 (setq toplist (cons (cons "C" clist) toplist))))
408 ;;
409 toplist
410 ))
411
412 ;(defvar cc-imenu-pike-generic-expression
413 ; ())
414 ; FIXME: Please contribute one!
415
416 (defun cc-imenu-init (mode-generic-expression
417 &optional mode-create-index-function)
418 (setq imenu-generic-expression mode-generic-expression
419 imenu-case-fold-search nil)
420 (when mode-create-index-function
421 (setq imenu-create-index-function mode-create-index-function)))
422
423 \f
424 (cc-provide 'cc-menus)
425
426 ;; arch-tag: f6b60933-91f0-4145-ab44-70ca6d1b919b
427 ;;; cc-menus.el ends here