commands.texi small fix for bug#13393
[emacs.git] / lisp / progmodes / cc-menus.el
bloba06eaf566d81517d7577f54752d014561b666101
1 ;;; cc-menus.el --- imenu support for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2013 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs
8 ;; 1987 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 ;; Keywords: c languages
13 ;; Package: cc-mode
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 3 of the License, or
20 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;;; Code:
34 (eval-when-compile
35 (let ((load-path
36 (if (and (boundp 'byte-compile-dest-file)
37 (stringp byte-compile-dest-file))
38 (cons (file-name-directory byte-compile-dest-file) load-path)
39 load-path)))
40 (load "cc-bytecomp" nil t)))
42 (cc-require 'cc-defs)
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-defvar imenu-create-index-function)
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 ;; *Warning for cc-mode developers*
68 ;; `cc-imenu-objc-generic-expression' elements depend on
69 ;; `cc-imenu-c++-generic-expression'. So if you change this
70 ;; expression, you need to change following variables,
71 ;; `cc-imenu-objc-generic-expression-*-index',
72 ;; too. `cc-imenu-objc-function' uses these *-index variables, in
73 ;; order to know where the each regexp *group \\(foobar\\)* elements
74 ;; are started.
76 ;; *-index variables are initialized during `cc-imenu-objc-generic-expression'
77 ;; being initialized.
80 (defvar cc-imenu-c++-generic-expression
82 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
83 ;; will be incorrectly recognized as function `new ()' because the regexps
84 ;; work by backtracking from the end of the definition.
85 (nil
86 ,(concat
87 "^\\<.*"
88 "[^" c-alnum "_:<>~]" ; match any non-identifier char
89 ; (note: this can be `\n')
90 "\\("
91 "\\([" c-alnum "_:<>~]*::\\)?" ; match an operator
92 "operator\\>[ \t]*"
93 "\\(()\\|[^(]*\\)" ; special case for `()' operator
94 "\\)"
96 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
97 ; require something other than
98 ; a `;' after the (...) to
99 ; avoid prototypes. Can't
100 ; catch cases with () inside
101 ; the parentheses surrounding
102 ; the parameters. e.g.:
103 ; `int foo(int a=bar()) {...}'
104 ) 1)
105 ;; Special case to match a line like `main() {}'
106 ;; e.g. no return type, not even on the previous line.
107 (nil
108 ,(concat
110 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; 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 ;; General function name regexp
116 (nil
117 ,(concat
118 "^\\<" ; line MUST start with word char
119 ;; \n added to prevent overflow in regexp matcher.
120 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-02/msg00021.html
121 "[^()\n]*" ; no parentheses before
122 "[^" c-alnum "_:<>~]" ; match any non-identifier char
123 "\\([" c-alpha "_][" c-alnum "_:<>~]*\\)" ; match function name
124 "\\([ \t\n]\\|\\\\\n\\)*(" ; see above, BUT the arg list
125 "\\([ \t\n]\\|\\\\\n\\)*" ; must not start
126 "\\([^ \t\n(*]" ; with an asterisk or parentheses
127 "[^()]*\\(([^()]*)[^()]*\\)*" ; Maybe function pointer arguments
128 "\\)?)"
129 "\\([ \t\n]\\|\\\\\n\\)*[^ \t\n;(]"
130 ) 1)
131 ;; Special case for definitions using phony prototype macros like:
132 ;; `int main _PROTO( (int argc,char *argv[]) )'.
133 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
134 ;; Only supported in c-code, so no `:<>~' chars in function name!
135 ,@(if cc-imenu-c-prototype-macro-regexp
136 `((nil
137 ,(concat
138 "^\\<.*" ; line MUST start with word char
139 "[^" c-alnum "_]" ; match any non-identifier char
140 "\\([" c-alpha "_][" c-alnum "_]*\\)" ; match function name
141 "[ \t]*" ; whitespace before macro name
142 cc-imenu-c-prototype-macro-regexp
143 "[ \t]*(" ; ws followed by first paren.
144 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
145 ) 1)))
146 ;; Class definitions
147 ("Class"
148 ,(concat
149 "^" ; beginning of line is required
150 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
151 "\\(class\\|struct\\)[ \t]+"
152 "\\(" ; the string we want to get
153 "[" c-alnum "_]+" ; class name
154 "\\(<[^>]+>\\)?" ; possibly explicitly specialized
155 "\\)"
156 "\\([ \t\n]\\|\\\\\n\\)*[:{]"
157 ) 3))
158 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
160 (defvar cc-imenu-c-generic-expression
161 cc-imenu-c++-generic-expression
162 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
164 (defvar cc-imenu-java-generic-expression
165 `((nil
166 ,(concat
167 "[" c-alpha "_][\]\[." c-alnum "_<> ]+[ \t\n\r]+" ; type spec
168 "\\([" c-alpha "_][" c-alnum "_]*\\)" ; method name
169 "[ \t\n\r]*"
170 ;; An argument list htat is either empty or contains any number
171 ;; of arguments. An argument is any number of annotations
172 ;; followed by a type spec followed by a word. A word is an
173 ;; identifier. A type spec is an identifier, possibly followed
174 ;; by < typespec > possibly followed by [].
175 (concat "("
176 "\\("
177 "[ \t\n\r]*"
178 "\\("
180 "[" c-alpha "_]"
181 "[" c-alnum "._]""*"
182 "[ \t\n\r]+"
183 "\\)*"
184 "\\("
185 "[" c-alpha "_]"
186 "[\]\[" c-alnum "_.]*"
187 "\\("
190 "[ \t\n\r]*"
191 "[\]\[.," c-alnum "_<> \t\n\r]*"
193 "\\)?"
194 "\\(\\[\\]\\)?"
195 "[ \t\n\r]+"
196 "\\)"
197 "[" c-alpha "_]"
198 "[" c-alnum "_]*"
199 "[ \t\n\r,]*"
200 "\\)*"
202 "[.," c-alnum " \t\n\r]*"
204 )) 1))
205 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
207 ;; Internal variables
208 (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
209 (defvar cc-imenu-objc-generic-expression-general-func-index nil)
210 (defvar cc-imenu-objc-generic-expression-proto-index nil)
211 (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
213 (defvar cc-imenu-objc-generic-expression
214 (concat
216 ;; For C
218 ;; > Special case to match a line like `main() {}'
219 ;; > e.g. no return type, not even on the previous line.
220 ;; Pick a token by (match-string 1)
221 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
222 (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
223 "\\|"
224 ;; > General function name regexp
225 ;; Pick a token by (match-string 3)
226 (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 6
227 (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
228 ;; > Special case for definitions using phony prototype macros like:
229 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
230 ;; Pick a token by (match-string 8)
231 (if cc-imenu-c-prototype-macro-regexp
232 (concat
233 "\\|"
234 (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
235 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 10) "")
237 (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 9) "")
238 "") ; -> index += 0
239 (prog2 (setq cc-imenu-objc-generic-expression-proto-index 9) "")
241 ;; For Objective-C
242 ;; Pick a token by (match-string 8 or 9)
244 "\\|\\("
245 "^[-+][:" c-alnum "()*_<>\n\t ]*[;{]" ; Methods
246 "\\|"
247 "^@interface[\t ]+[" c-alnum "_]+[\t ]*:"
248 "\\|"
249 "^@interface[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
250 "\\|"
251 ;; For NSObject, NSProxy and Object... They don't have super class.
252 "^@interface[\t ]+[" c-alnum "_]+[\t ]*.*$"
253 "\\|"
254 "^@implementation[\t ]+[" c-alnum "_]+[\t ]*([" c-alnum "_]+)"
255 "\\|"
256 "^@implementation[\t ]+[" c-alnum "_]+"
257 "\\|"
258 "^@protocol[\t ]+[" c-alnum "_]+" "\\)")
259 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
262 ;; Imenu support for objective-c uses functions.
263 (defsubst cc-imenu-objc-method-to-selector (method)
264 "Return the objc selector style string of METHOD.
265 Example:
266 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
268 -perform:withObject:withObject:withObject: /* selector */"
269 (let ((return "") ; String to be returned
270 (p 0) ; Current scanning position in METHOD
271 (pmax (length method)) ;
272 char ; Current scanning target
273 (betweenparen 0) ; CHAR is in parentheses.
274 argreq ; An argument is required.
275 inargvar) ; position of CHAR is in an argument variable.
276 (while (< p pmax)
277 (setq char (aref method p)
278 p (1+ p))
279 (cond
280 ;; Is CHAR part of a objc token?
281 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
282 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
283 (or (and (<= ?a char) (<= char ?z))
284 (and (<= ?A char) (<= char ?Z))
285 (and (<= ?0 char) (<= char ?9))
286 (= ?_ char)))
287 (if argreq
288 (setq inargvar t
289 argreq nil)
290 (setq return (concat return (char-to-string char)))))
291 ;; Or a white space?
292 ((and inargvar (or (eq ?\ char) (eq ?\n char))
293 (setq inargvar nil)))
294 ;; Or a method separator?
295 ;; If a method separator, the next token will be an argument variable.
296 ((eq ?: char)
297 (setq argreq t
298 return (concat return (char-to-string char))))
299 ;; Or an open parentheses?
300 ((eq ?\( char)
301 (setq betweenparen (1+ betweenparen)))
302 ;; Or a close parentheses?
303 ((eq ?\) char)
304 (setq betweenparen (1- betweenparen)))))
305 return))
307 (defun cc-imenu-objc-remove-white-space (str)
308 "Remove all spaces and tabs from STR."
309 (let ((return "")
310 (p 0)
311 (max (length str))
312 char)
313 (while (< p max)
314 (setq char (aref str p))
315 (setq p (1+ p))
316 (if (or (= char ?\ ) (= char ?\t))
318 (setq return (concat return (char-to-string char)))))
319 return))
321 (defun cc-imenu-objc-function ()
322 "Imenu support for Objective C mode."
323 (let (methodlist
324 clist
326 ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
328 ;; *Warning for developers*
329 ;; These constants depend on `cc-imenu-c++-generic-expression'.
331 (OBJC cc-imenu-objc-generic-expression-objc-base-index)
332 ;; Special case to match a line like `main() {}'
333 (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
334 ;; General function name regexp
335 (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
336 ;; Special case for definitions using phony prototype macros like:
337 (Cproto cc-imenu-objc-generic-expression-proto-index)
338 langnum
340 (classcount 0)
341 toplist
343 str2
344 (intflen (length "@interface"))
345 (implen (length "@implementation"))
346 (prtlen (length "@protocol"))
347 (func
349 ;; Does this emacs have buffer-substring-no-properties?
351 (if (fboundp 'buffer-substring-no-properties)
352 'buffer-substring-no-properties
353 'buffer-substring)))
354 (goto-char (point-max))
356 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
357 (setq langnum (if (match-beginning OBJC)
358 OBJC
359 (cond
360 ((match-beginning Cproto) Cproto)
361 ((match-beginning Cgeneralfunc) Cgeneralfunc)
362 ((match-beginning Cnoreturn) Cnoreturn))))
363 (setq str (funcall func (match-beginning langnum) (match-end langnum)))
365 (cond
367 ;; C
369 ((not (eq langnum OBJC))
370 (setq clist (cons (cons str (match-beginning langnum)) clist)))
372 ;; ObjC
374 ;; An instance Method
375 ((eq (aref str 0) ?-)
376 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
377 (setq methodlist (cons (cons str
378 (match-beginning langnum))
379 methodlist)))
380 ;; A factory Method
381 ((eq (aref str 0) ?+)
382 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
383 (setq methodlist (cons (cons str
384 (match-beginning langnum))
385 methodlist)))
386 ;; Interface or implementation or protocol
387 ((eq (aref str 0) ?@)
388 (setq classcount (1+ classcount))
389 (cond
390 ((and (> (length str) implen)
391 (string= (substring str 0 implen) "@implementation"))
392 (setq str (substring str implen)
393 str2 "@implementation"))
394 ((string= (substring str 0 intflen) "@interface")
395 (setq str (substring str intflen)
396 str2 "@interface"))
397 ((string= (substring str 0 prtlen) "@protocol")
398 (setq str (substring str prtlen)
399 str2 "@protocol")))
400 (setq str (cc-imenu-objc-remove-white-space str))
401 (setq methodlist (cons (cons str2
402 (match-beginning langnum))
403 methodlist))
404 (setq toplist (cons (cons str methodlist) toplist)
405 methodlist nil))))
407 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
408 (if (< classcount 2)
409 (let ((classname (car (car toplist)))
410 (p (cdr (car (cdr (car toplist)))))
411 last)
412 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
413 ;; Add C lang token
414 (if clist
415 (progn
416 (setq last toplist)
417 (while (cdr last)
418 (setq last (cdr last)))
419 (setcdr last clist))))
420 ;; Add C lang tokens as a sub menu
421 (if clist
422 (setq toplist (cons (cons "C" clist) toplist))))
424 toplist
427 ;(defvar cc-imenu-pike-generic-expression
428 ; ())
429 ; FIXME: Please contribute one!
431 (defun cc-imenu-init (mode-generic-expression
432 &optional mode-create-index-function)
433 (setq imenu-generic-expression mode-generic-expression
434 imenu-case-fold-search nil)
435 (when mode-create-index-function
436 (setq imenu-create-index-function mode-create-index-function)))
439 (cc-provide 'cc-menus)
441 ;;; cc-menus.el ends here