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