171
|
1 ;;; cc-menus.el --- imenu support for CC Mode
|
165
|
2
|
|
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Authors: 1992-1997 Barry A. Warsaw
|
|
6 ;; 1987 Dave Detlefs and Stewart Clamen
|
|
7 ;; 1985 Richard M. Stallman
|
|
8 ;; Maintainer: cc-mode-help@python.org
|
|
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
|
203
|
10 ;; Version: See cc-mode.el
|
165
|
11 ;; Keywords: c languages oop
|
|
12
|
|
13 ;; This file is part of GNU Emacs.
|
|
14
|
|
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
16 ;; it under the terms of the GNU General Public License as published by
|
|
17 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
18 ;; any later version.
|
|
19
|
|
20 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23 ;; GNU General Public License for more details.
|
|
24
|
|
25 ;; You should have received a copy of the GNU General Public License
|
|
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
28 ;; Boston, MA 02111-1307, USA.
|
|
29
|
|
30
|
|
31 ;; imenu integration
|
189
|
32 (defvar cc-imenu-c-prototype-macro-regexp nil
|
|
33 "RE matching macro names used to conditionally specify function prototypes.
|
|
34
|
|
35 For example:
|
|
36
|
|
37 #ifdef __STDC__
|
|
38 #define _P(x) x
|
|
39 #else
|
|
40 #define _P(x) /*nothing*/
|
|
41 #endif
|
|
42
|
|
43 int main _P( (int argc, char *argv[]) )
|
|
44
|
|
45 A sample value might look like: `\\(_P\\|_PROTO\\)'.")
|
|
46
|
165
|
47 (defvar cc-imenu-c++-generic-expression
|
|
48 (`
|
189
|
49 (
|
|
50 ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
|
|
51 ;; will be incorrectly recognised as function `new ()' because the regexps
|
|
52 ;; work by backtracking from the end of the definition.
|
|
53 (nil
|
165
|
54 (,
|
|
55 (concat
|
189
|
56 "^\\<.*"
|
|
57 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
|
|
58 ; (note: this can be `\n')
|
|
59 "\\("
|
|
60 "\\([a-zA-Z0-9_:<>~]*::\\)?" ; match an operator
|
|
61 "operator\\>[ \t]*"
|
|
62 "\\(()\\|[^(]*\\)" ; special case for `()' operator
|
|
63 "\\)"
|
|
64
|
|
65 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; followed by ws, arg list,
|
|
66 ; require something other than
|
|
67 ; a `;' after the (...) to
|
|
68 ; avoid prototypes. Can't
|
|
69 ; catch cases with () inside
|
|
70 ; the parentheses surrounding
|
|
71 ; the parameters. e.g.:
|
|
72 ; `int foo(int a=bar()) {...}'
|
|
73 )) 1)
|
|
74 ;; Special case to match a line like `main() {}'
|
|
75 ;; e.g. no return type, not even on the previous line.
|
|
76 (nil
|
|
77 (,
|
|
78 (concat
|
|
79 "^"
|
|
80 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
|
|
81 "[ \t]*([^)]*)[ \t]*[^ \t;]" ; see above
|
|
82 )) 1)
|
203
|
83 ;; General function name regexp
|
|
84 (nil
|
|
85 (,
|
|
86 (concat
|
|
87 "^\\<.*" ; line MUST start with word char
|
|
88 "[^a-zA-Z0-9_:<>~]" ; match any non-identifier char
|
|
89 "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)" ; match function name
|
|
90 "[ \t]*(" ; see above, BUT
|
|
91 "[ \t]*[^ \t(][^)]*)[ \t]*[^ \t;]" ; the argument list must not start
|
|
92 ; with a parentheses
|
|
93 )) 1)
|
189
|
94 ;; Special case for definitions using phony prototype macros like:
|
|
95 ;; `int main _PROTO( (int argc,char *argv[]) )'.
|
|
96 ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
|
|
97 ;; Only supported in c-code, so no `:<>~' chars in function name!
|
|
98 (,@ (if cc-imenu-c-prototype-macro-regexp
|
|
99 (` ((nil
|
|
100 (,
|
|
101 (concat
|
|
102 "^\\<.*" ; line MUST start with word char
|
|
103 "[^a-zA-Z0-9_]" ; match any non-identifier char
|
|
104 "\\([a-zA-Z_][a-zA-Z0-9_]*\\)" ; match function name
|
|
105 "[ \t]*" ; whitespace before macro name
|
|
106 cc-imenu-c-prototype-macro-regexp
|
|
107 "[ \t]*(" ; ws followed by first paren.
|
203
|
108 "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
|
189
|
109 )) 1)))))
|
|
110 ;; Class definitions
|
165
|
111 ("Class"
|
|
112 (, (concat
|
189
|
113 "^" ; beginning of line is required
|
|
114 "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
|
|
115 "class[ \t]+"
|
|
116 "\\([a-zA-Z0-9_]+\\)" ; the string we want to get
|
|
117 "[ \t]*[:{]"
|
|
118 )) 2)))
|
165
|
119 "Imenu generic expression for C++ mode. See `imenu-generic-expression'.")
|
|
120
|
|
121 (defvar cc-imenu-c-generic-expression
|
|
122 cc-imenu-c++-generic-expression
|
|
123 "Imenu generic expression for C mode. See `imenu-generic-expression'.")
|
|
124
|
|
125 (defvar cc-imenu-java-generic-expression
|
|
126 (`
|
|
127 ((nil
|
|
128 (,
|
|
129 (concat
|
|
130 "^\\([ \t]\\)*"
|
|
131 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; type specs; there can be
|
|
132 "\\([A-Za-z0-9_-]+[ \t]+\\)?" ; more than 3 tokens, right?
|
|
133 "\\([A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
|
|
134 "\\([ \t]\\)"
|
|
135 "\\([A-Za-z0-9_-]+\\)" ; the string we want to get
|
|
136 "\\([ \t]*\\)+("
|
|
137 "\\([a-zA-Z,_1-9\n \t]*[[]?[]]?\\)*" ; arguments
|
|
138 ")[ \t]*"
|
189
|
139 ; "[^;(]"
|
165
|
140 "[,a-zA-Z_1-9\n \t]*{"
|
|
141 )) 6)))
|
|
142 "Imenu generic expression for Java mode. See `imenu-generic-expression'.")
|
|
143
|
189
|
144 (defvar cc-imenu-objc-generic-expression
|
|
145 (concat
|
203
|
146 ;;
|
189
|
147 ;; For C
|
203
|
148 ;; *Warning for developers*
|
|
149 ;; This expression elements depend on `cc-imenu-c++-generic-expression'.
|
|
150 ;;
|
|
151 ;; > Special case to match a line like `main() {}'
|
|
152 ;; > e.g. no return type, not even on the previous line.
|
|
153 ;; Pick a token by (match-string 1)
|
|
154 (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ;
|
|
155 "\\|"
|
|
156 ;; > General function name regexp
|
|
157 ;; Pick a token by (match-string 2)
|
|
158 (car (cdr (nth 2 cc-imenu-c++-generic-expression)))
|
|
159 ;; > Special case for definitions using phony prototype macros like:
|
|
160 ;; > `int main _PROTO( (int argc,char *argv[]) )'.
|
|
161 ;; Pick a token by (match-string 3)
|
|
162 (if cc-imenu-c-prototype-macro-regexp
|
|
163 (concat
|
|
164 "\\|"
|
|
165 (car (cdr (nth 3 cc-imenu-c++-generic-expression))))
|
|
166 "")
|
|
167 ;;
|
189
|
168 ;; For Objective-C
|
203
|
169 ;; Pick a token by (match-string 3 or 4)
|
|
170 ;;
|
189
|
171 "\\|\\("
|
|
172 "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]" ; Methods
|
|
173 "\\|"
|
|
174 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"
|
|
175 "\\|"
|
|
176 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
|
|
177 "\\|"
|
|
178 ;; For NSObject, NSProxy and Object... They don't have super class.
|
|
179 "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
|
|
180 "\\|"
|
|
181 "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
|
|
182 "\\|"
|
|
183 "^@implementation[\t ]+[a-zA-Z0-9_]+"
|
|
184 "\\|"
|
|
185 "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
|
|
186 "Imenu generic expression for ObjC mode. See `imenu-generic-expression'.")
|
|
187
|
|
188
|
|
189 ;; Imenu support for objective-c uses functions.
|
|
190 (defsubst cc-imenu-objc-method-to-selector (method)
|
|
191 "Return the objc selector style string of METHOD.
|
|
192 Example:
|
|
193 - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
|
|
194 =>
|
|
195 -perform:withObject:withObject:withObject: /* selector */"
|
|
196 (let ((return "") ; String to be returned
|
|
197 (p 0) ; Current scanning position in METHOD
|
|
198 (pmax (length method)) ;
|
|
199 char ; Current scanning target
|
|
200 (betweenparen 0) ; CHAR is in parentheses.
|
|
201 argreq ; An argument is required.
|
|
202 inargvar) ; position of CHAR is in an argument variable.
|
|
203 (while (< p pmax)
|
|
204 (setq char (aref method p)
|
|
205 p (1+ p))
|
|
206 (cond
|
|
207 ;; Is CHAR part of a objc token?
|
|
208 ((and (not inargvar) ; Ignore if CHAR is part of an argument variable.
|
|
209 (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
|
|
210 (or (and (<= ?a char) (<= char ?z))
|
|
211 (and (<= ?A char) (<= char ?Z))
|
|
212 (and (<= ?0 char) (<= char ?9))
|
|
213 (= ?_ char)))
|
|
214 (if argreq
|
|
215 (setq inargvar t
|
|
216 argreq nil)
|
|
217 (setq return (concat return (char-to-string char)))))
|
|
218 ;; Or a white space?
|
|
219 ((and inargvar (or (eq ?\ char) (eq ?\n char))
|
|
220 (setq inargvar nil)))
|
|
221 ;; Or a method separator?
|
|
222 ;; If a method separator, the next token will be an argument variable.
|
|
223 ((eq ?: char)
|
|
224 (setq argreq t
|
|
225 return (concat return (char-to-string char))))
|
|
226 ;; Or an open parentheses?
|
|
227 ((eq ?\( char)
|
|
228 (setq betweenparen (1+ betweenparen)))
|
|
229 ;; Or a close parentheses?
|
|
230 ((eq ?\) char)
|
|
231 (setq betweenparen (1- betweenparen)))))
|
|
232 return))
|
|
233
|
|
234 (defun cc-imenu-objc-remove-white-space (str)
|
|
235 "Remove all spaces and tabs from STR."
|
|
236 (let ((return "")
|
|
237 (p 0)
|
|
238 (max (length str))
|
|
239 char)
|
|
240 (while (< p max)
|
|
241 (setq char (aref str p))
|
|
242 (setq p (1+ p))
|
|
243 (if (or (= char ?\ ) (= char ?\t))
|
|
244 ()
|
|
245 (setq return (concat return (char-to-string char)))))
|
|
246 return))
|
|
247
|
|
248 (defun cc-imenu-objc-function ()
|
|
249 "imenu supports for objc-mode."
|
|
250 (let (methodlist
|
|
251 clist
|
203
|
252 ;;
|
|
253 ;; OBJC, C1, C2, C3 are constants.
|
|
254 ;;
|
|
255 ;; *Warning for developers*
|
|
256 ;; These constants depend on `cc-imenu-c++-generic-expression'.
|
|
257 ;;
|
|
258 (OBJC
|
|
259 (if cc-imenu-c-prototype-macro-regexp 4 3))
|
|
260 (C1 ; > Special case to match a line like `main() {}'
|
|
261 1)
|
|
262 (C2 ; > General function name regexp
|
|
263 2)
|
|
264 (C3 ; > Special case for definitions using phony prototype macros like:
|
|
265 3)
|
189
|
266 langnum
|
203
|
267 ;;
|
189
|
268 (classcount 0)
|
|
269 toplist
|
|
270 stupid
|
|
271 str
|
|
272 str2
|
|
273 (intflen (length "@interface"))
|
|
274 (implen (length "@implementation"))
|
|
275 (prtlen (length "@protocol"))
|
|
276 bufsubst-fun)
|
|
277 ;;
|
|
278 ;; Does this emacs has buffer-substring-no-properties?
|
|
279 ;;
|
|
280 (fset 'bufsubst-fun (if (fboundp 'buffer-substring-no-properties)
|
|
281 (symbol-function 'buffer-substring-no-properties)
|
|
282 (symbol-function 'buffer-substring)))
|
|
283 (goto-char (point-max))
|
|
284 (imenu-progress-message stupid 0)
|
|
285 ;;
|
|
286 (while (re-search-backward cc-imenu-objc-generic-expression nil t)
|
|
287 (imenu-progress-message stupid)
|
203
|
288 (setq langnum (if (match-beginning OBJC)
|
|
289 OBJC
|
|
290 (cond
|
|
291 ((match-beginning C3) C3)
|
|
292 ((match-beginning C2) C2)
|
|
293 ((match-beginning C1) C1))))
|
189
|
294 (setq str (bufsubst-fun (match-beginning langnum) (match-end langnum)))
|
|
295 ;;
|
|
296 (cond
|
|
297 ;;
|
|
298 ;; C
|
|
299 ;;
|
203
|
300 ((not (eq langnum OBJC))
|
189
|
301 (setq clist (cons (cons str (match-beginning langnum)) clist)))
|
|
302 ;;
|
|
303 ;; ObjC
|
|
304 ;;
|
|
305 ;; An instance Method
|
|
306 ((eq (aref str 0) ?-)
|
|
307 (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
|
|
308 (setq methodlist (cons (cons str
|
|
309 (match-beginning langnum))
|
|
310 methodlist)))
|
|
311 ;; A factory Method
|
|
312 ((eq (aref str 0) ?+)
|
|
313 (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
|
|
314 (setq methodlist (cons (cons str
|
|
315 (match-beginning langnum))
|
|
316 methodlist)))
|
|
317 ;; Interface or implementation or protocol
|
|
318 ((eq (aref str 0) ?@)
|
|
319 (setq classcount (1+ classcount))
|
|
320 (cond
|
|
321 ((and (> (length str) implen)
|
|
322 (string= (substring str 0 implen) "@implementation"))
|
|
323 (setq str (substring str implen)
|
|
324 str2 "@implementation"))
|
|
325 ((string= (substring str 0 intflen) "@interface")
|
|
326 (setq str (substring str intflen)
|
|
327 str2 "@interface"))
|
|
328 ((string= (substring str 0 prtlen) "@protocol")
|
|
329 (setq str (substring str prtlen)
|
|
330 str2 "@protocol")))
|
|
331 (setq str (cc-imenu-objc-remove-white-space str))
|
|
332 (setq methodlist (cons (cons str2
|
|
333 (match-beginning langnum))
|
|
334 methodlist))
|
|
335 (setq toplist (cons nil (cons (cons str
|
|
336 methodlist) toplist))
|
|
337 methodlist nil))))
|
|
338 ;;
|
|
339 (imenu-progress-message stupid 100)
|
|
340 (if (eq (car toplist) nil)
|
|
341 (setq toplist (cdr toplist)))
|
|
342
|
|
343 ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
|
|
344 (if (< classcount 2)
|
|
345 (let ((classname (car (car toplist)))
|
|
346 (p (cdr (car (cdr (car toplist)))))
|
|
347 last)
|
|
348 (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
|
|
349 ;; Add C lang token
|
|
350 (if clist
|
|
351 (progn
|
|
352 (setq last toplist)
|
|
353 (while (cdr last)
|
|
354 (setq last (cdr last)))
|
|
355 (setcdr last clist))))
|
|
356 ;; Add C lang tokens as a sub menu
|
|
357 (setq toplist (cons (cons "C" clist) toplist)))
|
|
358 ;;
|
|
359 toplist
|
|
360 ))
|
|
361
|
165
|
362
|
|
363 (provide 'cc-menus)
|
|
364 ;;; cc-menus.el ends here
|