0
|
1 ;;!emacs
|
|
2 ;;
|
|
3 ;; FILE: br-objc-ft.el
|
|
4 ;; SUMMARY: Objective-C OO-Browser class and feature functions.
|
|
5 ;; USAGE: GNU Emacs Lisp Library
|
|
6 ;; KEYWORDS: c, oop, tools
|
|
7 ;;
|
|
8 ;; AUTHOR: Bob Weiner
|
100
|
9 ;; ORG: InfoDock Associates
|
0
|
10 ;;
|
|
11 ;; ORIG-DATE: 03-Oct-90
|
100
|
12 ;; LAST-MOD: 31-Oct-96 at 17:03:48 by Bob Weiner
|
0
|
13 ;;
|
100
|
14 ;; Copyright (C) 1990-1996 Free Software Foundation, Inc.
|
0
|
15 ;; See the file BR-COPY for license information.
|
|
16 ;;
|
|
17 ;; This file is part of the OO-Browser.
|
|
18 ;;
|
|
19 ;; DESCRIPTION:
|
|
20 ;; DESCRIP-END.
|
|
21
|
|
22 ;;; ************************************************************************
|
|
23 ;;; Other required Elisp libraries
|
|
24 ;;; ************************************************************************
|
|
25
|
|
26 (require 'br-objc)
|
|
27
|
|
28 ;;; ************************************************************************
|
|
29 ;;; Public variables
|
|
30 ;;; ************************************************************************
|
|
31
|
|
32 (defconst objc-default-category-class "[category]"
|
|
33 "Name of the default class whose instances are Objective-C categories.")
|
|
34
|
|
35 (defconst objc-default-protocol-class "[protocol]"
|
|
36 "Name of the default class whose instances are Objective-C protocols.")
|
|
37
|
|
38 (defconst objc-type-identifier
|
|
39 (concat "[a-zA-Z][" objc-identifier-chars "]*[ \t\n]*[*&]*"))
|
|
40
|
|
41 (defconst objc-type-tag-separator "@"
|
|
42 "String that separates a tag's type from its normalized definition form.")
|
|
43
|
|
44 (defconst objc-tag-fields-regexp
|
|
45 ;; The \\\\? below is necessary because we sometimes use this expression to
|
|
46 ;; test against a string that has ben regexp-quoted and some of the
|
|
47 ;; characters in br-feature-type-regexp will then be preceded by \\.
|
|
48 (format "\\`\\([^*& \n]+\\)%s\\\\?\\(%s \\)\\([^%s\n]+\\)\\(%s\\|\\'\\)"
|
|
49 objc-type-tag-separator br-feature-type-regexp
|
|
50 objc-type-tag-separator objc-type-tag-separator)
|
|
51 "Regexp matching the fields of an Objective-C feature tag line.
|
|
52 Group 1 is the class of the feature. Group 2 is the prefix preceding the
|
|
53 feature when displayed within a listing buffer. Group 3 is the feature name.
|
|
54 The feature definition signature begins at the end of the regexp match,
|
|
55 i.e. (match-end 0), and goes to the end of the string or line.")
|
|
56
|
|
57 (defvar objc-cpp-include-dirs '("/usr/include/")
|
|
58 "*Ordered list of include directories by default searched by C preprocessor.
|
|
59 Each directory must end with a directory separator. See also
|
|
60 'objc-include-dirs'.")
|
|
61
|
|
62 (defvar objc-include-dirs nil
|
|
63 "*Ordered list of directories to search for Objective-C include files.
|
|
64 Each directory must end with a directory separator. Directories normally
|
|
65 searched by the Objective-C pre-processor should be set instead in
|
|
66 'objc-cpp-include-dirs'.")
|
|
67
|
|
68 ;;; ************************************************************************
|
|
69 ;;; Public functions
|
|
70 ;;; ************************************************************************
|
|
71
|
|
72 (defun objc-add-default-classes ()
|
|
73 ;; Add to 'system' class table.
|
|
74 (mapcar (function (lambda (class) (br-add-class class br-null-path nil)))
|
|
75 (list objc-default-category-class objc-default-protocol-class))
|
|
76 (if br-c-tags-flag (c-add-default-classes)))
|
|
77
|
|
78 (defun objc-class-definition-name (class)
|
|
79 "Convert CLASS name to the way it appears in its source code definition.
|
|
80 Returns a regular expression."
|
|
81 (cond ((string-match "^<.+>$" class)
|
|
82 ;; Remove <> delimiters from protocol class.
|
|
83 (regexp-quote (substring class 1 -1)))
|
|
84 ((string-match "^\\([^ \(]+\\) *(\\([^\)]*\\)) *$" class)
|
|
85 ;; Allow for whitespace within class(category)
|
|
86 (format "%s[ \t\n\r]*([ \t\n\r]*%s[ \t\n\r]*)"
|
|
87 (regexp-quote
|
|
88 (substring class (match-beginning 1) (match-end 1)))
|
|
89 (regexp-quote
|
|
90 (substring class (match-beginning 2) (match-end 2)))))
|
|
91 ((string-match "^(\\([^\)]*\\)) *\\([^ ]+\\) *$" class)
|
|
92 ;; Allow for whitespace within (category)class
|
|
93 (format "%s[ \t\n\r]*([ \t\n\r]*%s[ \t\n\r]*)"
|
|
94 (regexp-quote
|
|
95 (substring class (match-beginning 2) (match-end 2)))
|
|
96 (regexp-quote
|
|
97 (substring class (match-beginning 1) (match-end 1)))))
|
|
98 (t (regexp-quote class))))
|
|
99
|
|
100 (defun objc-class-definition-regexp (class)
|
|
101 "Return regexp to uniquely match the definition of CLASS name."
|
|
102 (concat objc-class-name-before (objc-class-definition-name class)
|
|
103 objc-class-name-after))
|
|
104
|
|
105 (defun objc-feature-implementors (ftr-name)
|
|
106 "Return unsorted list of Objective-C feature tags which implement FTR-NAME."
|
100
|
107 (objc-feature-matches (regexp-quote ftr-name)))
|
0
|
108
|
|
109 (defun objc-feature-locate-p (feature-tag &optional regexp-flag)
|
|
110 "Leaves point at the start of FEATURE-TAG's definition in the current buffer.
|
|
111 Assumes caller has moved point to the beginning of the buffer or to the point
|
|
112 of desired search start.
|
|
113 Optional REGEXP-FLAG means FEATURE-TAG is a regular expression."
|
|
114 ;; Match to function definitions, not declarations.
|
|
115 (let ((found) (start))
|
|
116 ;; First move to the proper class implementation if this is not a
|
|
117 ;; [default-class], so that if two classes in the same file have the same
|
|
118 ;; feature signature, we still end up at the right one.
|
|
119 (if (and (not (string-match (if regexp-flag "\\`\\\\\\[" "\\`\\[")
|
|
120 feature-tag))
|
|
121 (string-match objc-tag-fields-regexp feature-tag))
|
|
122 (let ((class
|
|
123 (substring feature-tag (match-beginning 1) (match-end 1))))
|
|
124 ;; Protocols don't define methods, they only declare them, so we
|
|
125 ;; know we can't be searching for a protocol method definition
|
|
126 ;; here, and so there is no special case handling.
|
|
127 (re-search-forward (concat objc-implementation-before
|
|
128 ;; Assume regexp-quoted class is the
|
|
129 ;; same as non-regexp-quoted version
|
|
130 ;; since this call will regexp-quote it
|
|
131 ;; again; we have no way of
|
|
132 ;; un-regexp-quoting it.
|
|
133 (objc-class-definition-name class))
|
|
134 nil t)))
|
|
135 ;;
|
|
136 ;; Now search for feature.
|
|
137 (or regexp-flag (setq feature-tag
|
|
138 (objc-feature-signature-to-regexp feature-tag)))
|
|
139 (while (and (re-search-forward feature-tag nil t)
|
|
140 (setq start (match-beginning 0))
|
|
141 (not (setq found (not (if (c-within-comment-p)
|
|
142 (progn (search-forward "*/" nil t)
|
|
143 t)))))))
|
|
144 (if found
|
|
145 (progn (goto-char start)
|
|
146 (skip-chars-forward " \t\n")
|
|
147 (objc-to-comments-begin)
|
|
148 (recenter 0)
|
|
149 (goto-char start)
|
|
150 t))))
|
|
151
|
100
|
152 (defun objc-feature-map-class-tags (function class)
|
|
153 "Apply FUNCTION to all feature tags from CLASS and return a list of the results.
|
|
154 Feature tags come from the file named by br-feature-tags-file."
|
|
155 (let ((obuf (current-buffer))
|
|
156 (class-tag (concat "\n" class objc-type-tag-separator))
|
|
157 (results)
|
|
158 search-function)
|
|
159 (if (= (aref class 0) ?\[)
|
|
160 ;; Default class of protocols or categories. Use a string match
|
|
161 ;; for speed.
|
|
162 (setq search-function 'search-forward
|
|
163 class-tag (concat "\n" class objc-type-tag-separator))
|
|
164 (setq search-function 're-search-forward
|
|
165 class-tag
|
|
166 ;; Include methods defined in any of the class' categories.
|
|
167 (concat "^" class "\\(([^\)]*)\\)?" objc-type-tag-separator)))
|
|
168 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
169 (goto-char 1)
|
|
170 (while (funcall search-function class-tag nil t)
|
|
171 (setq results (cons (funcall function) results))
|
|
172 ;; Might have deleted current tag and would miss next tag unless point
|
|
173 ;; is moved backwards.
|
|
174 (backward-char))
|
|
175 (set-buffer obuf)
|
|
176 results))
|
|
177
|
0
|
178 (defun objc-feature-name-to-regexp (name)
|
|
179 "Converts feature NAME into a regular expression matching the feature's name tag."
|
|
180 (cond
|
|
181 ;;
|
|
182 ;; protocol name
|
|
183 ((= (aref name 0) ?\<) (regexp-quote name))
|
|
184 ;;
|
|
185 ;; category name
|
|
186 ((= (aref name 0) ?\()
|
|
187 ;; Drop any class name following the category.
|
|
188 (regexp-quote
|
|
189 (substring name 0 (1+ (string-match "\)" name)))))
|
|
190 ;;
|
|
191 ;; feature tag
|
|
192 ((string-match objc-tag-fields-regexp name)
|
|
193 (concat
|
|
194 "^" (regexp-quote (substring name (match-beginning 0) (match-end 0)))))
|
|
195 ;;
|
|
196 ;; unrecognized name format
|
|
197 (t (error "(objc-feature-name-to-regexp): Invalid name, '%s'" name))))
|
|
198
|
|
199 (defun objc-feature-signature-to-name (signature &optional with-class for-display)
|
|
200 "Extracts the feature name from SIGNATURE.
|
|
201 SIGNATURE may be a feature tag line or a signature extracted from source code.
|
|
202 The feature's class name is dropped from signature unless optional WITH-CLASS
|
|
203 is non-nil. The feature's type (class feature = +, instance feature = -)
|
|
204 is dropped unless FOR-DISPLAY is non-nil."
|
|
205
|
|
206 ;; feature tag
|
|
207 (if (string-match objc-tag-fields-regexp signature)
|
|
208 (cond ((and with-class for-display)
|
|
209 (substring signature (match-beginning 1) (match-end 3)))
|
|
210 (for-display
|
|
211 (substring signature (match-beginning 2) (match-end 3)))
|
|
212 (with-class
|
|
213 (concat
|
|
214 (substring signature (match-beginning 1) (1+ (match-end 1)))
|
|
215 (substring signature (match-beginning 3) (match-end 3))))
|
|
216 (t (substring signature (match-beginning 3) (match-end 3))))
|
|
217 ;;
|
|
218 ;; source code signature
|
|
219 (let ((loop-p t)
|
|
220 (name-part (concat "\\`" objc-name-part objc-name-sep))
|
|
221 (name-type (concat "\\`" objc-name-part objc-type-sep))
|
|
222 (name))
|
|
223 (cond ((or (= (aref signature 0) ?\<) (= (aref signature 0) ?\())
|
|
224 ;; protocol or category tags
|
|
225 (setq name
|
|
226 (if for-display
|
|
227 (concat "@ " signature)
|
|
228 signature)))
|
|
229 ((string-match (concat "\\`" br-feature-type-regexp) signature)
|
|
230 ;; regular feature signature
|
|
231 (if for-display
|
|
232 (setq name (concat (substring signature (match-beginning 0)
|
|
233 (match-end 0))
|
|
234 " ")))
|
|
235 (setq signature (concat (substring signature (match-end 0)) ";"))
|
|
236 (while (and loop-p (string-match name-part signature))
|
|
237 ;; Handles named or unnamed parameters.
|
|
238 (if (match-beginning objc-name-part-id)
|
|
239 (setq name (concat name
|
|
240 (substring
|
|
241 signature
|
|
242 (match-beginning objc-name-part-id)
|
|
243 (match-end objc-name-part-id)))))
|
|
244 (if (/= (aref signature (1- (match-end 0))) ?:)
|
|
245 (setq loop-p nil
|
|
246 signature (substring signature (match-end 0)))
|
|
247 (setq name (concat name ":")
|
|
248 signature (substring signature (match-end 0)))
|
|
249 (if (string-match name-type signature)
|
|
250 (setq signature (substring signature (match-end 0)))))))
|
|
251 (t (error
|
|
252 "(objc-feature-signature-to-name): Invalid signature, '%s'"
|
|
253 signature)))
|
|
254 name)))
|
|
255
|
|
256 (defun objc-feature-signature-to-regexp (signature)
|
|
257 "Return regexp to match the definition of an Objective-C element SIGNATURE.
|
|
258 SIGNATURE may be a feature tag line or a signature extracted from source code."
|
|
259 (cond ((string-match
|
|
260 (format
|
|
261 "^\\([^*& \n]+\\)%s%s [^%s\n]+%s\\([\<\(][^\>\)\n]+[\>\)]\\(%s\\)?\\)"
|
|
262 objc-type-tag-separator br-feature-type-regexp
|
|
263 objc-type-tag-separator objc-type-tag-separator
|
|
264 objc-identifier)
|
|
265 signature)
|
|
266 ;; protocol or category signature
|
|
267 (let ((class (substring signature (match-beginning 1) (match-end 1)))
|
|
268 (element (substring signature (match-beginning 2)
|
|
269 (match-end 2))))
|
|
270 (if (= (aref element 0) ?\<)
|
|
271 (if (string-equal class objc-default-protocol-class)
|
|
272 ;; find def of protocol
|
|
273 (concat objc-protocol-before
|
|
274 (objc-class-definition-name element)
|
|
275 "[\< \t\n\r]")
|
|
276 ;; find def of class which conforms to protocol
|
|
277 (concat objc-interface-before
|
|
278 (objc-class-definition-name class)
|
|
279 objc-class-name-after
|
|
280 "[^\>\{]*[\<,][ \t\n\r]*"
|
|
281 (objc-class-definition-name element)
|
|
282 "[ \t\n\r]*[\>,]"))
|
|
283 (if (string-equal class objc-default-category-class)
|
|
284 ;; find def of '[category]@(category-name)class-name'
|
|
285 (concat objc-interface-before
|
|
286 (objc-class-definition-name element))
|
|
287 ;; find def of 'class-name@(category-name)'
|
|
288 (concat objc-interface-before
|
|
289 (objc-class-definition-name (concat class element)))))))
|
|
290 ;;
|
|
291 (t
|
|
292 ;; regulare feature tag
|
|
293 (setq signature (regexp-quote signature))
|
|
294 (if (string-match objc-tag-fields-regexp signature)
|
|
295 ;;
|
|
296 ;; We must leave the class name as an optional component at the
|
|
297 ;; start of the signature so that functions that lookup feature
|
|
298 ;; definitions can use it to ensure that the definition is found
|
|
299 ;; within the right class.
|
|
300 (setq signature
|
|
301 (concat
|
|
302 "\\(" (substring signature (match-beginning 1)
|
|
303 (match-end 1))
|
|
304 objc-type-tag-separator "\\)?"
|
|
305 (substring signature (match-end 0)))))
|
|
306 (let ((pat) (i 0) (c) (len (length signature)))
|
|
307 (while (< i len)
|
|
308 (setq c (aref signature i)
|
|
309 pat (cond ((= c ? )
|
|
310 (concat pat "[ \t\n\^M]*"))
|
|
311 (t
|
|
312 (concat pat (char-to-string c))))
|
|
313 i (1+ i)))
|
|
314 (if (= ?{ (aref pat (1- (length pat))))
|
|
315 (setq pat (concat (substring pat 0 -1)
|
|
316 "\\([ \t\n]*//.*[\n]\\)*[ \t\n]*{"))
|
|
317 pat)))))
|
|
318
|
100
|
319 (defun objc-feature-tag-regexp (class feature-name)
|
|
320 "Return a regexp that matches to the feature tag entry for CLASS' FEATURE-NAME."
|
|
321 (concat "^" (regexp-quote class) objc-type-tag-separator
|
|
322 br-feature-type-regexp " "
|
|
323 (regexp-quote feature-name) "\\(" objc-type-tag-separator "\\|\\'\\)"))
|
|
324
|
0
|
325 (defun objc-feature-tree-command-p (class-or-signature)
|
|
326 "Display definition of CLASS-OR-SIGNATURE if a signature and return t, else return nil."
|
|
327 (if (br-in-browser) (br-to-view-window))
|
|
328 (br-feature-found-p (br-feature-file class-or-signature)
|
|
329 class-or-signature))
|
|
330
|
|
331 (defun objc-list-categories (class)
|
|
332 "Return sorted list of Objective-C CLASS categories."
|
|
333 (let ((obuf (current-buffer))
|
|
334 (categories)
|
|
335 class-tag)
|
|
336 (cond ((string-equal class objc-default-category-class)
|
|
337 (objc-list-features class))
|
|
338 ((= (aref class 0) ?\[)
|
|
339 ;; Any other default classes belong to no categories.
|
|
340 nil)
|
|
341 (t
|
|
342 (setq class-tag (concat "\n" class objc-type-tag-separator "\("))
|
|
343 (set-buffer
|
|
344 (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
345 (goto-char 1)
|
|
346 ;; Use a string match for speed.
|
|
347 (while (search-forward class-tag nil t)
|
|
348 (setq categories (cons (br-feature-current) categories)))
|
|
349 (set-buffer obuf)
|
|
350 (objc-sort-features (nreverse categories))))))
|
|
351
|
|
352 (defun objc-list-features (class &optional indent)
|
100
|
353 "Return sorted list of Objective-C feature tags lexically defined in CLASS.
|
|
354 Optional INDENT is unused but is required for multi-language OO-Browser conformance."
|
|
355 (objc-sort-features
|
|
356 (nreverse (objc-feature-map-class-tags 'br-feature-current class))))
|
0
|
357
|
|
358 (defun objc-list-protocols (class)
|
|
359 "Return sorted list of Objective-C CLASS protocols."
|
|
360 (let ((obuf (current-buffer))
|
|
361 (protocols)
|
|
362 class-tag)
|
|
363 (cond ((string-equal class objc-default-protocol-class)
|
|
364 (objc-list-features class))
|
|
365 ((= (aref class 0) ?\[)
|
|
366 ;; Any other default classes conform to no formal protocols.
|
|
367 nil)
|
|
368 (t
|
|
369 (setq class-tag (concat "\n" class objc-type-tag-separator "\<"))
|
|
370 (set-buffer
|
|
371 (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
372 (goto-char 1)
|
|
373 ;; Use a string match for speed.
|
|
374 (while (search-forward class-tag nil t)
|
|
375 (setq protocols (cons (br-feature-current) protocols)))
|
|
376 (set-buffer obuf)
|
|
377 (objc-sort-features (nreverse protocols))))))
|
|
378
|
|
379 (defun objc-routine-at-point-p ()
|
|
380 "Returns name of Objective-C routine signature at point or nil.
|
|
381 If called interactively, it prints the name in the minibuffer."
|
|
382 (interactive)
|
|
383 (save-excursion
|
|
384 (if (and (re-search-backward "[-+\n\^M]\\|\\`" nil t)
|
|
385 (looking-at "[ \t\n\^M]*[-+]"))
|
|
386 (let ((name "") (loop-p t)
|
|
387 (name-part (concat objc-name-part objc-name-sep))
|
|
388 (name-type (concat objc-name-part objc-type-sep)))
|
|
389 (goto-char (match-end 0))
|
|
390 (while (and loop-p (looking-at name-part))
|
|
391 ;; Handles named or unamed parameters.
|
|
392 (if (match-beginning objc-name-part-id)
|
|
393 (setq name (concat name
|
|
394 (buffer-substring
|
|
395 (match-beginning objc-name-part-id)
|
|
396 (match-end objc-name-part-id)))))
|
|
397 (goto-char (match-end 0))
|
|
398 (if (/= (preceding-char) ?:)
|
|
399 (setq loop-p nil)
|
|
400 (setq name (concat name ":"))
|
|
401 (if (looking-at name-type) (goto-char (match-end 0)))
|
|
402 ))
|
|
403 (if (interactive-p)
|
|
404 (message name)
|
|
405 name)))))
|
|
406
|
|
407 (defun objc-scan-features ()
|
|
408 "Return reverse ordered list of Objective-C routine definitions in current buffer.
|
|
409 Assume point is at beginning of widened buffer."
|
|
410 (save-excursion
|
|
411 (let ((routines) (rout) (class-end)
|
|
412 class category)
|
|
413 (while (re-search-forward
|
|
414 (concat "^@implementation[ \t\n\r]+" objc-identifier
|
|
415 "\\([ \t\n\r]*([ \t\n\r]*" objc-identifier
|
|
416 "[ \t\n\r]*)\\)?")
|
|
417 nil t)
|
|
418 (setq category (if (match-beginning 3)
|
|
419 (buffer-substring (match-beginning 3)
|
|
420 (match-end 3)))
|
|
421 class (buffer-substring (match-beginning 1) (match-end 1))
|
|
422 class (if category (format "%s(%s)" class category) class))
|
|
423 (save-excursion
|
|
424 (if (search-forward "\n@end" nil t)
|
|
425 (setq class-end (point))
|
|
426 (error "(objc-scan-features): %s, at char %d, @implementation without @end.")))
|
|
427 (while (re-search-forward objc-routine-def class-end t)
|
|
428 (setq rout (buffer-substring (match-beginning 0)
|
|
429 (match-end 0)))
|
|
430 (if (c-within-comment-p)
|
|
431 (search-forward "*/" nil t)
|
|
432 (backward-char) ;; Move point to precede feature opening brace.
|
|
433 (condition-case ()
|
|
434 ;; Move to end of feature but ignore any error if braces are
|
|
435 ;; unbalanced. Let the compiler tell the user about this.
|
|
436 (forward-sexp)
|
|
437 (error nil))
|
|
438 (setq rout (objc-feature-normalize rout class)
|
|
439 routines (cons rout routines)))))
|
|
440 routines)))
|
|
441
|
|
442 (defun objc-scan-protocol-list ()
|
|
443 "Return a list of protocol names following point, delimited by <> and separated by commas.
|
|
444 Point may be immediately before or after the '<' which begins the protocol
|
|
445 list. Leaves point afer the closing delimiter of the protocol list."
|
|
446 (cond ((= (preceding-char) ?\<))
|
|
447 ((= (following-char) ?\<)
|
|
448 (forward-char 1))
|
|
449 (t
|
|
450 (error "(objc-scan-protocol-list): Point must precede or follow a '<' delimiter.")))
|
|
451 (let ((end (save-excursion (search-forward "\>")))
|
|
452 (protocols))
|
|
453 (while (re-search-forward objc-identifier end t)
|
|
454 (setq protocols (cons (concat "<"
|
|
455 (buffer-substring (match-beginning 1)
|
|
456 (match-end 1))
|
|
457 ">")
|
|
458 protocols)))
|
|
459 (goto-char end)
|
|
460 (nreverse protocols)))
|
|
461
|
|
462 (defun objc-sort-features (routine-list)
|
|
463 (sort routine-list 'objc-feature-lessp))
|
|
464
|
|
465 (defun objc-to-definition (&optional other-win)
|
|
466 "If point is within a declaration, try to move to its definition.
|
|
467 With OTHER-WIN non-nil, show it in another window."
|
|
468 (interactive)
|
|
469 (let ((opoint (point)))
|
|
470 (cond
|
|
471 ((objc-include-file other-win))
|
|
472 ((br-check-for-class (objc-class-decl-p) other-win))
|
|
473 ((objc-feature other-win))
|
|
474 ((and (goto-char opoint)
|
|
475 (br-check-for-class (objc-find-class-name) other-win)))
|
|
476 (t (beep)
|
|
477 (message
|
|
478 "(OO-Browser): Select an Objective-C declaration to move to its definition.")
|
|
479 nil))))
|
|
480
|
|
481 (defun objc-view-protocol (protocol-name)
|
|
482 "Display definition of PROTOCOL-NAME for viewing.
|
|
483 PROTOCOL-NAME must be a string."
|
|
484 (or (string-match "<.*>" protocol-name)
|
|
485 (setq protocol-name (concat "<" protocol-name ">")))
|
|
486 (let* ((sig (concat objc-default-protocol-class objc-type-tag-separator
|
|
487 protocol-name))
|
|
488 (feature-path (br-feature-file sig)))
|
|
489 (br-to-view-window)
|
|
490 (if (br-feature-found-p feature-path sig)
|
|
491 (progn (br-major-mode)
|
|
492 (setq buffer-read-only t)
|
|
493 ;; Force mode-line redisplay
|
|
494 (set-buffer-modified-p (buffer-modified-p))
|
|
495 (br-to-from-viewer))
|
|
496 ;; Protocol not found. Return to original window and signal an error.
|
|
497 (br-to-from-viewer)
|
|
498 (error "(OO-Browser): No '%s' protocol defined in Environment."
|
|
499 protocol-name))))
|
|
500
|
|
501 ;;; ************************************************************************
|
|
502 ;;; Private functions
|
|
503 ;;; ************************************************************************
|
|
504
|
|
505 (defun objc-class-decl-p ()
|
|
506 "Return nil unless point is within a class declaration, referenced by another
|
|
507 class. Commented declarations also return nil. When value is non-nil, it is
|
|
508 the class name from the declaration. Leave point at start of statement for
|
|
509 visual clarity."
|
|
510 (objc-skip-to-statement)
|
|
511 (save-excursion
|
|
512 (let ((class))
|
|
513 (and (looking-at objc-class-decl)
|
|
514 (setq class (buffer-substring (match-beginning objc-class-name-grpn)
|
|
515 (match-end objc-class-name-grpn)))
|
|
516 (not (c-within-comment-p))
|
|
517 (progn (beginning-of-line)
|
|
518 (not (looking-at "[ \t]*//")))
|
|
519 class))))
|
|
520
|
|
521 (defun objc-feature (&optional other-win)
|
|
522 "Move point to definition of the element given by declaration at point.
|
|
523 Return nil if point is not within an element declaration."
|
|
524 ;; If '{' follows the feature declaration, then feature is defined right
|
|
525 ;; here, within the class definition.
|
|
526 (interactive)
|
|
527 (cond ((objc-feature-def-p)
|
|
528 (recenter 0)
|
|
529 t)
|
|
530 ;; Now look for feature definition in code (non-header) files.
|
|
531 ((objc-feature-decl-p)
|
|
532 (let ((class) feature-name signature)
|
|
533 (setq signature (buffer-substring (match-beginning 1)
|
|
534 (match-end 1)))
|
|
535 (save-excursion
|
|
536 (if (re-search-backward objc-class-def-regexp nil t)
|
|
537 (setq class (buffer-substring
|
|
538 (match-beginning objc-class-name-grpn)
|
|
539 (match-end objc-class-name-grpn)))))
|
|
540 (setq signature (objc-feature-normalize signature class)
|
|
541 feature-name (objc-feature-signature-to-name signature))
|
|
542 (if (objc-locate-feature feature-name class signature other-win)
|
|
543 t
|
|
544 (beep)
|
|
545 (message "(OO-Browser): No definition for '%s' in '%s'."
|
|
546 feature-name (or class "UNKNOWN-CLASS"))
|
|
547 t)))))
|
|
548
|
|
549 (defun objc-feature-decl-p ()
|
|
550 "Return t if point is within an Objective-C feature declaration."
|
|
551 (save-excursion
|
|
552 (beginning-of-line)
|
|
553 (looking-at objc-feature-declaration)))
|
|
554
|
|
555 (defun objc-feature-def-p ()
|
|
556 "Return nil unless point is within an element definition.
|
|
557 Commented element definitions also return nil."
|
|
558 (save-excursion
|
|
559 (objc-skip-to-statement)
|
|
560 (and (not (c-within-comment-p))
|
|
561 (save-excursion (beginning-of-line)
|
|
562 (not (looking-at "[ \t]*//")))
|
|
563 (not (looking-at objc-class-decl))
|
|
564 (looking-at (concat objc-feature-decl-or-def
|
|
565 objc-comment-regexp "[{;,]"))
|
|
566 (= ?\{ (save-excursion (goto-char (match-end 0))
|
|
567 (preceding-char))))))
|
|
568
|
100
|
569 (defun objc-feature-display (class-list signature ftr-regexp &optional other-win)
|
|
570 "Display routine definition derived from CLASS-LIST, matching SIGNATURE (string) and FTR-REGEXP (regexp matching SIGNATURE).
|
|
571 Use routine tags table to locate a match. Caller must use 'set-buffer'
|
|
572 to restore prior buffer when a match is not found."
|
|
573 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
574 (let ((classes class-list)
|
|
575 (found-ftr)
|
|
576 (class))
|
|
577 (if (null class-list)
|
|
578 nil
|
|
579 (while (and (not found-ftr) classes)
|
|
580 (setq class (car classes)
|
|
581 found-ftr (br-feature-found-p
|
|
582 (br-feature-file signature)
|
|
583 ftr-regexp nil other-win t)
|
|
584 classes (if found-ftr nil (cdr classes))))
|
|
585 (if found-ftr
|
|
586 (or class t)
|
|
587 (objc-feature-display
|
|
588 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
589 class-list))
|
|
590 signature
|
|
591 ftr-regexp
|
|
592 other-win)))))
|
0
|
593
|
|
594 (defun objc-feature-lessp (tag1 tag2)
|
|
595 (string-lessp (objc-feature-partial-name tag1)
|
|
596 (objc-feature-partial-name tag2)))
|
|
597
|
100
|
598 (defun objc-feature-map-tags (function regexp)
|
|
599 "Apply FUNCTION to all current feature tags that match REGEXP and return a list of the results.
|
|
600 Feature tags come from the file named by br-feature-tags-file."
|
|
601 (let ((identifier-chars (concat "[" objc-identifier-chars "]*"))
|
|
602 (results))
|
|
603 (setq regexp (format "^[^%s \n]+%s%s %s%s" objc-type-tag-separator
|
|
604 objc-type-tag-separator br-feature-type-regexp
|
|
605 regexp objc-type-tag-separator))
|
|
606 ;; Ensure match to feature names only; also handle "^" and "$" meta-chars
|
0
|
607 (save-excursion
|
100
|
608 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
0
|
609 (goto-char 1)
|
|
610 (while (re-search-forward regexp nil t)
|
100
|
611 (setq results (cons (funcall function) results))))
|
|
612 results))
|
|
613
|
|
614 (defun objc-feature-matches (regexp)
|
|
615 "Return an unsorted list of feature tags whose names match in part or whole to REGEXP.
|
|
616 ^ and $ characters may be used to match to the beginning and end of a feature name,
|
|
617 respectively."
|
|
618 (objc-feature-map-tags 'br-feature-current regexp))
|
0
|
619
|
|
620 (defun objc-feature-normalize (routine class)
|
|
621 (let* ((len (length routine))
|
|
622 (normal-feature (make-string len ?\ ))
|
|
623 (n 0) (i 0)
|
|
624 (space-list '(?\ ?\t ?\n ?\^M))
|
|
625 (space-regexp "[ \t\n\^M]+")
|
|
626 chr)
|
|
627 (while (< i len)
|
|
628 (setq chr (aref routine i))
|
|
629 (cond
|
|
630 ;; Convert sequences of space characters to a single space.
|
|
631 ((memq chr space-list)
|
|
632 (aset normal-feature n ?\ )
|
|
633 (if (string-match space-regexp routine i)
|
|
634 (setq i (match-end 0)
|
|
635 n (1+ n))
|
|
636 (setq i (1+ i)
|
|
637 n (1+ n))))
|
|
638 ;;
|
|
639 ;; Remove // style comments
|
|
640 ((and (= chr ?/)
|
|
641 (< (1+ i) len)
|
|
642 (= (aref routine (1+ i)) ?/))
|
|
643 (setq i (+ i 2))
|
|
644 (while (and (< i len) (/= (aref routine i) ?\n))
|
|
645 (setq i (1+ i))))
|
|
646 (t ;; Normal character
|
|
647 (aset normal-feature n chr)
|
|
648 (setq i (1+ i)
|
|
649 n (1+ n)))))
|
|
650 (setq normal-feature (substring normal-feature 0 n))
|
|
651 (concat class objc-type-tag-separator
|
|
652 (objc-feature-signature-to-name normal-feature nil t)
|
|
653 objc-type-tag-separator
|
|
654 normal-feature)))
|
|
655
|
100
|
656 (defun objc-feature-partial-name (feature-tag)
|
|
657 "Extract the feature name without its class name from FEATURE-TAG."
|
|
658 (objc-feature-signature-to-name feature-tag))
|
|
659
|
0
|
660 (defun objc-feature-tag-class (feature-signature)
|
|
661 "Extract the class name from FEATURE-SIGNATURE."
|
|
662 (if (string-match objc-type-tag-separator feature-signature)
|
|
663 (substring feature-signature 0 (match-beginning 0))
|
|
664 ""))
|
|
665
|
|
666 (defun objc-files-with-source (class)
|
|
667 "Use CLASS to compute set of files that match to an Objective-C source file regexp.
|
|
668 Return as a list."
|
|
669 (let ((file (if class (br-class-path class) buffer-file-name)))
|
|
670 (and file
|
|
671 (let* ((src-file-regexp (concat "^" (br-filename-head file)
|
|
672 objc-code-file-regexp))
|
|
673 (dir (file-name-directory file))
|
|
674 (files (directory-files dir nil src-file-regexp)))
|
|
675 (mapcar (function (lambda (f) (concat dir f)))
|
|
676 files)))))
|
|
677
|
|
678 (defun objc-find-ancestors-feature (class-list signature &optional other-win)
|
|
679 "Scan ancestors of CLASS-LIST and show routine definition matching SIGNATURE."
|
|
680 ;; If no class, search for non-element function.
|
|
681 (or class-list (setq class-list '(nil)))
|
|
682 (let ((obuf (current-buffer))
|
|
683 (ftr-regexp (objc-feature-signature-to-regexp signature)))
|
|
684 (prog1
|
|
685 (if (and br-feature-tags-file
|
|
686 (file-exists-p br-feature-tags-file)
|
|
687 (file-readable-p br-feature-tags-file))
|
100
|
688 (objc-feature-display
|
0
|
689 class-list signature ftr-regexp other-win)
|
|
690 ;; Only works if features are in same directory as class def.
|
|
691 (objc-scan-ancestors-feature class-list ftr-regexp other-win))
|
|
692 (set-buffer obuf))))
|
|
693
|
|
694 (defun objc-find-class-name ()
|
|
695 "Return current word as a potential class name."
|
|
696 (save-excursion
|
|
697 (let* ((start)
|
|
698 (ignore "-+ \t\n;,.<>{}*&\(\)")
|
|
699 (pat (concat "^" ignore)))
|
|
700 (forward-char 1)
|
|
701 (skip-chars-backward ignore)
|
|
702 (skip-chars-backward pat)
|
|
703 (setq start (point))
|
|
704 (skip-chars-forward (concat pat ":"))
|
|
705 (buffer-substring start (point)))))
|
|
706
|
|
707 (defun objc-get-class-name-from-source ()
|
|
708 "Return class name from closest class definition preceding point or nil."
|
|
709 (let ((opoint (point))
|
|
710 (class))
|
|
711 (save-excursion
|
|
712 (if (re-search-backward objc-class-def-regexp nil t)
|
|
713 (progn (setq class (buffer-substring
|
|
714 (match-beginning objc-class-name-grpn)
|
|
715 (match-end objc-class-name-grpn)))
|
|
716 ;; Ensure that declaration occurs within class definition.
|
|
717 (forward-list)
|
|
718 (and (> (point) opoint) class))))))
|
|
719
|
|
720 (defun objc-get-feature-tags (routine-file &optional routine-list)
|
|
721 "Scan Objective-C ROUTINE-FILE and hold routine tags in 'br-feature-tags-file'.
|
|
722 Assume ROUTINE-FILE has already been read into a buffer and that
|
|
723 'br-feature-tags-init' has been called. Optional ROUTINE-LIST can be
|
|
724 provided so that a non-standard scan function can be used before calling
|
|
725 this function."
|
|
726 (interactive)
|
|
727 (let ((obuf (current-buffer)))
|
|
728 (setq routine-list
|
|
729 (objc-sort-features
|
|
730 (or routine-list (objc-scan-features))))
|
|
731 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
732 (goto-char 1)
|
|
733 ;; Delete any prior routine tags associated with routine-file
|
|
734 (if (search-forward routine-file nil 'end)
|
|
735 (progn (forward-line -1)
|
|
736 (let ((start (point)))
|
|
737 (search-forward "\^L" nil 'end 2)
|
|
738 (backward-char 1)
|
|
739 (delete-region start (point))
|
|
740 )))
|
|
741 (if routine-list
|
|
742 (progn (insert "\^L\n" routine-file "\n")
|
|
743 (mapcar (function (lambda (tag) (insert tag "\n")))
|
|
744 routine-list)))
|
|
745 (set-buffer obuf)))
|
|
746
|
|
747 (defun objc-include-file (&optional other-win)
|
|
748 "If point is on an include file line, try to display file.
|
|
749 Return non-nil iff an include file line, even if file is not found.
|
|
750 Look for include file in 'objc-cpp-include-dirs' and in directory list
|
|
751 'objc-include-dirs'."
|
|
752 (let ((opoint (point)))
|
|
753 (beginning-of-line)
|
|
754 (if (looking-at objc-include-regexp)
|
|
755 (let ((incl-type (string-to-char
|
|
756 (buffer-substring
|
|
757 (match-beginning objc-include-type-grpn)
|
|
758 (1+ (match-beginning objc-include-type-grpn)))))
|
|
759 (file (buffer-substring
|
|
760 (match-beginning objc-include-file-grpn)
|
|
761 (match-end objc-include-file-grpn)))
|
|
762 (path)
|
|
763 (dir-list objc-include-dirs)
|
|
764 (found))
|
|
765 (goto-char opoint)
|
|
766 (setq dir-list (if (= incl-type ?\<)
|
|
767 (append dir-list objc-cpp-include-dirs)
|
|
768 (cons (file-name-directory buffer-file-name)
|
|
769 dir-list)))
|
|
770 (while dir-list
|
|
771 (setq path (concat (car dir-list) file)
|
|
772 dir-list (if (setq found (file-exists-p path))
|
|
773 nil
|
|
774 (cdr dir-list))))
|
|
775 ;;
|
|
776 ;; If not found in normal include dirs, check all Env paths also.
|
|
777 ;;
|
|
778 (if (not found)
|
|
779 (let ((paths (delq nil (hash-map 'cdr br-paths-htable))))
|
|
780 (while paths
|
|
781 (setq path (car paths))
|
|
782 (if (string-equal (file-name-nondirectory path) file)
|
|
783 (setq found t paths nil)
|
|
784 (setq paths (cdr paths))))))
|
|
785 ;;
|
|
786 ;; If found, display file
|
|
787 ;;
|
|
788 (if found
|
|
789 (if (file-readable-p path)
|
|
790 (progn
|
|
791 (funcall br-edit-file-function path other-win)
|
|
792 (if (not (fboundp 'br-lang-mode))
|
|
793 (objc-mode-setup))
|
|
794 (br-major-mode))
|
|
795 (beep)
|
|
796 (message "(OO-Browser): Include file '%s' unreadable." path))
|
|
797 (beep)
|
|
798 (message "(OO-Browser): Include file '%s' not found." file))
|
|
799 path)
|
|
800 (goto-char opoint)
|
|
801 nil)))
|
|
802
|
|
803 (defun objc-locate-feature (ftr class signature &optional other-win)
|
|
804 ;; 'class' may = nil, implying non-element function
|
|
805 (let ((def-class))
|
|
806 (if (and signature
|
|
807 (setq def-class
|
|
808 (objc-find-ancestors-feature (list class)
|
|
809 signature other-win)))
|
|
810 (progn (if (and class (not (equal class def-class)))
|
|
811 (message
|
|
812 "Element `%s` of class '%s' inherited from class '%s'."
|
|
813 ftr class def-class))
|
|
814 t))))
|
|
815
|
|
816 (defun objc-scan-ancestors-feature (class-list ftr-regexp &optional other-win)
|
|
817 "Display routine definition derived from CLASS-LIST, matching FTR-REGEXP.
|
|
818 Scan files with same base name as class file."
|
|
819 (let ((classes class-list)
|
|
820 (found-ftr)
|
|
821 (code-def-files)
|
|
822 (file)
|
|
823 (class))
|
|
824 (if (null class-list)
|
|
825 nil
|
|
826 (while (and (not found-ftr) classes)
|
|
827 (setq class (car classes)
|
|
828 code-def-files (objc-files-with-source class))
|
|
829 (while (and (setq file (car code-def-files))
|
|
830 (not (setq found-ftr
|
|
831 (br-feature-found-p file ftr-regexp
|
|
832 nil other-win t))))
|
|
833 (setq code-def-files (cdr code-def-files)))
|
|
834 (setq classes (if found-ftr nil (cdr classes))))
|
|
835 (if found-ftr
|
|
836 (or class t)
|
|
837 (objc-scan-ancestors-feature
|
|
838 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
839 class-list))
|
|
840 ftr-regexp)))))
|
|
841
|
|
842 (defun objc-skip-past-comments ()
|
|
843 "Skip over comments immediately following point."
|
|
844 (skip-chars-forward " \t\n")
|
|
845 (while
|
|
846 (cond ((looking-at "//")
|
|
847 (equal (forward-line 1) 0))
|
|
848 ((looking-at "/\\*")
|
|
849 (re-search-forward "\\*/" nil t))
|
|
850 (t nil))))
|
|
851
|
|
852 (defun objc-skip-to-statement ()
|
|
853 (if (re-search-backward "\\(^\\|[;{}]\\)[ \t]*" nil t)
|
|
854 (progn (goto-char (match-end 0))
|
|
855 (skip-chars-forward " \t")
|
|
856 t)))
|
|
857
|
|
858 ;;; ************************************************************************
|
|
859 ;;; Private variables
|
|
860 ;;; ************************************************************************
|
|
861
|
|
862 (defconst objc-code-file-regexp ".\\.[cmCM]$"
|
|
863 "Regular expression matching a unique part of Objective-C source (non-header) file name and no others.")
|
|
864
|
|
865 (defconst objc-include-regexp
|
|
866 "[ \t/*]*#[ \t]*\\(import\\|include\\)[ \t]+\\([\"<]\\)\\([^\">]+\\)[\">]"
|
|
867 "Regexp to match to Objective-C include file lines.
|
|
868 File name is grouping 'objc-include-file-grpn'. Type of include,
|
|
869 user-specified via double quote, or system-related starting with `<' is given
|
|
870 by grouping 'objc-include-type-grpn'.")
|
|
871
|
|
872 (defconst objc-include-type-grpn 2)
|
|
873 (defconst objc-include-file-grpn 3)
|
|
874
|
|
875 (defconst objc-type-def-modifier
|
|
876 "\\(auto\\|const\\|inline\\|register\\|static\\|typedef\\)")
|
|
877
|
|
878 (defconst objc-func-identifier (concat
|
|
879 "[_a-zA-Z][^][ \t:;.,~{}()]*")
|
|
880 "Regular expression matching an Objective-C function name.")
|
|
881
|
|
882 (defconst objc-feature-decl-or-def
|
|
883 "[-+]\\([^\]\[{};`'\"/|?,!.#$%^=+-]+\\)"
|
|
884 "Regexp matching an Objective-C feature declaration or definition.
|
|
885 Feature name is group 1.")
|
|
886
|
|
887 (defconst objc-feature-name-grpn 1)
|
|
888
|
|
889 (defconst objc-comment-regexp "\\([ \t\n]*//.*[\n]\\)*[ \t\n]*")
|
|
890
|
|
891 (defconst objc-routine-def (concat "^" objc-feature-decl-or-def
|
|
892 objc-comment-regexp "{"))
|
|
893
|
|
894 (defconst objc-feature-declaration
|
|
895 (concat "^[ \t]*\\(" objc-feature-decl-or-def "\\)" objc-comment-regexp ";"))
|
|
896
|
|
897 (defconst objc-class-decl
|
|
898 (concat objc-class-name-before objc-identifier "[ \t]*")
|
|
899 "Regexp matching an Objective-C class declaration.
|
|
900 Class name is grouping 'objc-class-name-grpn'.")
|
|
901
|
|
902 (defconst objc-class-name-grpn 2)
|
|
903
|
|
904 (defconst objc-arg-identifier (concat
|
|
905 "[_a-zA-Z][" objc-identifier-chars "]*")
|
|
906 "Regular expression matching an Objective-C function argument identifier.")
|
|
907
|
|
908 (defconst objc-name-part-prefix
|
|
909 "[ \t\n]*\\(([^\)]+)[ \t\n]*\\)?")
|
|
910
|
|
911 (defconst objc-name-part
|
|
912 (concat objc-name-part-prefix objc-identifier "?"))
|
|
913
|
|
914 (defconst objc-name-sep "[ \t\n]*\\([:;{]\\)")
|
|
915
|
|
916 (defconst objc-type-sep "\\([ \t\n;{]\\)")
|
|
917
|
|
918 (defconst objc-name-part-type 1)
|
|
919 (defconst objc-name-part-id 2)
|
|
920 (defconst objc-name-part-sep 3)
|
|
921
|
|
922 (provide 'br-objc-ft)
|