(help-xref-go-back): Don't try to set position.
[emacs.git] / lisp / progmodes / cc-menus.el
blob8eb10050d015687359ca9bea11eec89710739abb
1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985,1987,1992-1999 Free Software Foundation, Inc.
5 ;; Authors: 1998-1999 Barry A. Warsaw and Martin Stjernholm
6 ;; 1992-1997 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile
32 (let ((load-path
33 (if (and (boundp 'byte-compile-current-file)
34 (stringp byte-compile-current-file))
35 (cons (file-name-directory byte-compile-current-file)
36 load-path)
37 load-path)))
38 (load "cc-defs" nil t)))
40 ;; Dummy definitions to shut up the compiler in case imenu doesn't exist.
41 (defvar imenu-generic-expression)
42 (defvar imenu-case-fold-search)
43 (or (fboundp 'imenu-progress-message)
44 (defun imenu-progress-message (&rest args) nil))
46 ;; Try to pull in imenu.
47 (eval-and-compile
48 (condition-case nil
49 (require 'imenu)
50 (error nil)))
53 ;; imenu integration
54 (defvar cc-imenu-c-prototype-macro-regexp nil
55 "RE matching macro names used to conditionally specify function prototypes.
57 For example:
59 #ifdef __STDC__
60 #define _P(x) x
61 #else
62 #define _P(x) /*nothing*/
63 #endif
65 int main _P( (int argc, char *argv[]) )
67 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
69 (defvar cc-imenu-c++-generic-expression
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 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
78 ; (note: this can be `\n')
79 "\\("
80 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
81 "operator\\>[ \t]*"
82 "\\(()\\|[^(]*\\)" ; special case for `()' operator
83 "\\)"
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 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; 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 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
110 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
111 "[ \t]*(" ; see above, BUT
112 "[ \t]*\\([^ \t(*][^)]*\\)?)" ; the arg list must not start
113 "[ \t]*[^ \t;(]" ; 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 "[^a-zA-Z0-9_]" ; match any non-identifier char
124 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; 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[ \t]+"
136 "\\(" ; the string we want to get
137 "[a-zA-Z0-9_]+" ; class name
138 "\\(<[^>]+>\\)?" ; possibly explicitely specialized
139 "\\)"
140 "[ \t\n]*[:{]"
141 ) 2))
142 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
144 (defvar cc-imenu-c-generic-expression
145 cc-imenu-c++-generic-expression
146 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
148 (defvar cc-imenu-java-generic-expression
149 `((nil
150 ,(concat
151 "^\\([ \t]\\)*"
152 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
153 "\\([.A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
154 "\\([.A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
155 "\\([ \t]\\)"
156 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
157 "\\([ \t]*\\)+("
158 "[][a-zA-Z,_1-9\n \t]*" ; arguments
159 ")[ \t]*"
160 ; "[^;(]"
161 "[,a-zA-Z_1-9\n \t]*{"
162 ) 6))
163 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
165 ;; *Warning for cc-mode developers*
167 ;; `cc-imenu-objc-generic-expression' elements depend on
168 ;; `cc-imenu-c++-generic-expression'. So if you change this
169 ;; expression, you need to change following variables,
170 ;; `cc-imenu-objc-generic-expression-*-index',
171 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
172 ;; order to know where the each regexp *group \\(foobar\\)* elements
173 ;; are started.
175 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
176 ;; being initialized.
179 ;; Internal variables
180 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
181 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
182 (defvar cc-imenu-objc-generic-expression-proto-index nil)
183 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
185 (defvar cc-imenu-objc-generic-expression
186 (concat
188 ;; For C
190 ;; > Special case to match a line like `main() {}'
191 ;; > e.g. no return type, not even on the previous line.
192 ;; Pick a token by (match-string 1)
193 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
194 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
195 "\\|"
196 ;; > General function name regexp
197 ;; Pick a token by (match-string 3)
198 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 2
199 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
200 ;; > Special case for definitions using phony prototype macros like:
201 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
202 ;; Pick a token by (match-string 5)
203 (if cc-imenu-c-prototype-macro-regexp
204 (concat
205 "\\|"
206 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
207 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 6) "")
209 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 5) "")
210 "") ; -> index += 0
211 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
213 ;; For Objective-C
214 ;; Pick a token by (match-string 5 or 6)
216 "\\|\\("
217 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
218 "\\|"
219 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
220 "\\|"
221 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
222 "\\|"
223 ;; For NSObject, NSProxy and Object... They don't have super class.
224 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
225 "\\|"
226 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
227 "\\|"
228 "^@implementation[\t ]+[a-zA-Z0-9_]+"
229 "\\|"
230 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
231 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
234 ;; Imenu support for objective-c uses functions.
235 (defsubst cc-imenu-objc-method-to-selector (method)
236 "Return the objc selector style string of METHOD.
237 Example:
238 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
240 -perform:withObject:withObject:withObject: /* selector */"
241 (let ((return "") ; String to be returned
242 (p 0) ; Current scanning position in METHOD
243 (pmax (length method)) ;
244 char ; Current scanning target
245 (betweenparen 0) ; CHAR is in parentheses.
246 argreq ; An argument is required.
247 inargvar) ; position of CHAR is in an argument variable.
248 (while (< p pmax)
249 (setq char (aref method p)
250 p (1+ p))
251 (cond
252 ;; Is CHAR part of a objc token?
253 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
254 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
255 (or (and (<= ?a char) (<= char ?z))
256 (and (<= ?A char) (<= char ?Z))
257 (and (<= ?0 char) (<= char ?9))
258 (= ?_ char)))
259 (if argreq
260 (setq inargvar t
261 argreq nil)
262 (setq return (concat return (char-to-string char)))))
263 ;; Or a white space?
264 ((and inargvar (or (eq ?\ char) (eq ?\n char))
265 (setq inargvar nil)))
266 ;; Or a method separator?
267 ;; If a method separator, the next token will be an argument variable.
268 ((eq ?: char)
269 (setq argreq t
270 return (concat return (char-to-string char))))
271 ;; Or an open parentheses?
272 ((eq ?\( char)
273 (setq betweenparen (1+ betweenparen)))
274 ;; Or a close parentheses?
275 ((eq ?\) char)
276 (setq betweenparen (1- betweenparen)))))
277 return))
279 (defun cc-imenu-objc-remove-white-space (str)
280 "Remove all spaces and tabs from STR."
281 (let ((return "")
282 (p 0)
283 (max (length str))
284 char)
285 (while (< p max)
286 (setq char (aref str p))
287 (setq p (1+ p))
288 (if (or (= char ?\ ) (= char ?\t))
290 (setq return (concat return (char-to-string char)))))
291 return))
293 (defun cc-imenu-objc-function ()
294 "imenu supports for objc-mode."
295 (let (methodlist
296 clist
298 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
300 ;; *Warning for developers*
301 ;; These constants depend on `cc-imenu-c++-generic-expression'.
303 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
304 ;; Special case to match a line like `main() {}'
305 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
306 ;; General function name regexp
307 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
308 ;; Special case for definitions using phony prototype macros like:
309 (Cproto cc-imenu-objc-generic-expression-proto-index)
310 langnum
312 (classcount 0)
313 toplist
314 stupid
316 str2
317 (intflen (length "@interface"))
318 (implen (length "@implementation"))
319 (prtlen (length "@protocol"))
320 (func
322 ;; Does this emacs has buffer-substring-no-properties?
324 (if (fboundp 'buffer-substring-no-properties)
325 'buffer-substring-no-properties
326 'buffer-substring)))
327 (goto-char (point-max))
328 (imenu-progress-message stupid 0)
330 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
331 (imenu-progress-message stupid)
332 (setq langnum (if (match-beginning OBJC)
333 OBJC
334 (cond
335 ((match-beginning Cproto) Cproto)
336 ((match-beginning Cgeneralfunc) Cgeneralfunc)
337 ((match-beginning Cnoreturn) Cnoreturn))))
338 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
340 (cond
342 ;; C
344 ((not (eq langnum OBJC))
345 (setq clist (cons (cons str (match-beginning langnum)) clist)))
347 ;; ObjC
349 ;; An instance Method
350 ((eq (aref str 0) ?-)
351 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
352 (setq methodlist (cons (cons str
353 (match-beginning langnum))
354 methodlist)))
355 ;; A factory Method
356 ((eq (aref str 0) ?+)
357 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
358 (setq methodlist (cons (cons str
359 (match-beginning langnum))
360 methodlist)))
361 ;; Interface or implementation or protocol
362 ((eq (aref str 0) ?@)
363 (setq classcount (1+ classcount))
364 (cond
365 ((and (> (length str) implen)
366 (string= (substring str 0 implen) "@implementation"))
367 (setq str (substring str implen)
368 str2 "@implementation"))
369 ((string= (substring str 0 intflen) "@interface")
370 (setq str (substring str intflen)
371 str2 "@interface"))
372 ((string= (substring str 0 prtlen) "@protocol")
373 (setq str (substring str prtlen)
374 str2 "@protocol")))
375 (setq str (cc-imenu-objc-remove-white-space str))
376 (setq methodlist (cons (cons str2
377 (match-beginning langnum))
378 methodlist))
379 (setq toplist (cons nil (cons (cons str
380 methodlist) toplist))
381 methodlist nil))))
383 (imenu-progress-message stupid 100)
384 (if (eq (car toplist) nil)
385 (setq toplist (cdr toplist)))
387 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
388 (if (< classcount 2)
389 (let ((classname (car (car toplist)))
390 (p (cdr (car (cdr (car toplist)))))
391 last)
392 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
393 ;; Add C lang token
394 (if clist
395 (progn
396 (setq last toplist)
397 (while (cdr last)
398 (setq last (cdr last)))
399 (setcdr last clist))))
400 ;; Add C lang tokens as a sub menu
401 (setq toplist (cons (cons "C" clist) toplist)))
403 toplist
406 ;(defvar cc-imenu-pike-generic-expression
407 ; ())
408 ; FIXME: Please contribute one!
410 (defun cc-imenu-init (mode-generic-expression)
411 (setq imenu-generic-expression mode-generic-expression
412 imenu-case-fold-search nil))
415 (provide 'cc-menus)
416 ;;; cc-menus.el ends here