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