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