Add arch tagline
[bpt/emacs.git] / lisp / cedet / semantic / ia-sb.el
1 ;;; semantic/ia-sb.el --- Speedbar analysis display interactor
2
3 ;;; Copyright (C) 2002, 2003, 2004, 2006, 2008, 2009
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Speedbar node for displaying derived context information.
27 ;;
28
29 (require 'semantic/analyze)
30 (require 'speedbar)
31
32 ;;; Code:
33 (defvar semantic-ia-sb-key-map nil
34 "Keymap used when in semantic analysis display mode.")
35
36 (if semantic-ia-sb-key-map
37 nil
38 (setq semantic-ia-sb-key-map (speedbar-make-specialized-keymap))
39
40 ;; Basic featuers.
41 (define-key semantic-ia-sb-key-map "\C-m" 'speedbar-edit-line)
42 (define-key semantic-ia-sb-key-map "I" 'semantic-ia-sb-show-tag-info)
43 )
44
45 (defvar semantic-ia-sb-easymenu-definition
46 '( "---"
47 ; [ "Expand" speedbar-expand-line nil ]
48 ; [ "Contract" speedbar-contract-line nil ]
49 [ "Tag Information" semantic-ia-sb-show-tag-info t ]
50 [ "Jump to Tag" speedbar-edit-line t ]
51 [ "Complete" speedbar-edit-line t ]
52 )
53 "Extra menu items Analysis mode.")
54
55 ;; Make sure our special speedbar major mode is loaded
56 (speedbar-add-expansion-list '("Analyze"
57 semantic-ia-sb-easymenu-definition
58 semantic-ia-sb-key-map
59 semantic-ia-speedbar))
60
61 (speedbar-add-mode-functions-list
62 (list "Analyze"
63 ;;'(speedbar-item-info . eieio-speedbar-item-info)
64 '(speedbar-line-directory . semantic-ia-sb-line-path)))
65
66 ;;;###autoload
67 (defun semantic-speedbar-analysis ()
68 "Start Speedbar in semantic analysis mode.
69 The analyzer displays information about the current context, plus a smart
70 list of possible completions."
71 (interactive)
72 ;; Make sure that speedbar is active
73 (speedbar-frame-mode 1)
74 ;; Now, throw us into Analyze mode on speedbar.
75 (speedbar-change-initial-expansion-list "Analyze")
76 )
77
78 (defun semantic-ia-speedbar (directory zero)
79 "Create buttons in speedbar which define the current analysis at POINT.
80 DIRECTORY is the current directory, which is ignored, and ZERO is 0."
81 (let ((analysis nil)
82 (scope nil)
83 (buffer nil)
84 (completions nil)
85 (cf (selected-frame))
86 (cnt nil)
87 (mode-local-active-mode nil)
88 )
89 ;; Try and get some sort of analysis
90 (condition-case nil
91 (progn
92 (speedbar-select-attached-frame)
93 (setq buffer (current-buffer))
94 (setq mode-local-active-mode major-mode)
95 (save-excursion
96 ;; Get the current scope
97 (setq scope (semantic-calculate-scope (point)))
98 ;; Get the analysis
99 (setq analysis (semantic-analyze-current-context (point)))
100 (setq cnt (semantic-find-tag-by-overlay))
101 (when analysis
102 (setq completions (semantic-analyze-possible-completions analysis))
103 )
104 ))
105 (error nil))
106 (select-frame cf)
107 (save-excursion
108 (set-buffer speedbar-buffer)
109 ;; If we have something, do something spiff with it.
110 (erase-buffer)
111 (speedbar-insert-separator "Buffer/Function")
112 ;; Note to self: Turn this into an expandable file name.
113 (speedbar-make-tag-line 'bracket ? nil nil
114 (buffer-name buffer)
115 nil nil 'speedbar-file-face 0)
116
117 (when cnt
118 (semantic-ia-sb-string-list cnt
119 'speedbar-tag-face
120 'semantic-sb-token-jump))
121 (when analysis
122 ;; If this analyzer happens to point at a complete symbol, then
123 ;; see if we can dig up some documentation for it.
124 (semantic-ia-sb-show-doc analysis))
125
126 (when analysis
127 ;; Let different classes draw more buttons.
128 (semantic-ia-sb-more-buttons analysis)
129 (when completions
130 (speedbar-insert-separator "Completions")
131 (semantic-ia-sb-completion-list completions
132 'speedbar-tag-face
133 'semantic-ia-sb-complete))
134 )
135
136 ;; Show local variables
137 (when scope
138 (semantic-ia-sb-show-scope scope))
139
140 )))
141
142 (defmethod semantic-ia-sb-show-doc ((context semantic-analyze-context))
143 "Show documentation about CONTEXT iff CONTEXT points at a complete symbol."
144 (let ((sym (car (reverse (oref context prefix))))
145 (doc nil))
146 (when (semantic-tag-p sym)
147 (setq doc (semantic-documentation-for-tag sym))
148 (when doc
149 (speedbar-insert-separator "Documentation")
150 (insert doc)
151 (insert "\n")
152 ))
153 ))
154
155 (defun semantic-ia-sb-show-scope (scope)
156 "Show SCOPE information."
157 (let ((localvars (when scope
158 (oref scope localvar)))
159 )
160 (when localvars
161 (speedbar-insert-separator "Local Variables")
162 (semantic-ia-sb-string-list localvars
163 'speedbar-tag-face
164 ;; This is from semantic-sb
165 'semantic-sb-token-jump))))
166
167 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context))
168 "Show a set of speedbar buttons specific to CONTEXT."
169 (let ((prefix (oref context prefix)))
170 (when prefix
171 (speedbar-insert-separator "Prefix")
172 (semantic-ia-sb-string-list prefix
173 'speedbar-tag-face
174 'semantic-sb-token-jump))
175 ))
176
177 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-assignment))
178 "Show a set of speedbar buttons specific to CONTEXT."
179 (call-next-method)
180 (let ((assignee (oref context assignee)))
181 (when assignee
182 (speedbar-insert-separator "Assignee")
183 (semantic-ia-sb-string-list assignee
184 'speedbar-tag-face
185 'semantic-sb-token-jump))))
186
187 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-functionarg))
188 "Show a set of speedbar buttons specific to CONTEXT."
189 (call-next-method)
190 (let ((func (oref context function)))
191 (when func
192 (speedbar-insert-separator "Function")
193 (semantic-ia-sb-string-list func
194 'speedbar-tag-face
195 'semantic-sb-token-jump)
196 ;; An index for the argument the prefix is in:
197 (let ((arg (oref context argument))
198 (args (semantic-tag-function-arguments (car func)))
199 (idx 0)
200 )
201 (speedbar-insert-separator
202 (format "Argument #%d" (oref context index)))
203 (if args
204 (semantic-ia-sb-string-list args
205 'speedbar-tag-face
206 'semantic-sb-token-jump
207 (oref context index)
208 'speedbar-selected-face)
209 ;; Else, no args list, so use what the context had.
210 (semantic-ia-sb-string-list arg
211 'speedbar-tag-face
212 'semantic-sb-token-jump))
213 ))))
214
215 (defun semantic-ia-sb-string-list (list face function &optional idx idxface)
216 "Create some speedbar buttons from LIST.
217 Each button will use FACE, and be activated with FUNCTION.
218 Optional IDX is an index into LIST to apply IDXFACE instead."
219 (let ((count 1))
220 (while list
221 (let* ((usefn nil)
222 (string (cond ((stringp (car list))
223 (car list))
224 ((semantic-tag-p (car list))
225 (setq usefn (semantic-tag-with-position-p (car list)))
226 (semantic-format-tag-uml-concise-prototype (car list)))
227 (t "<No Tag>")))
228 (localface (if (or (not idx) (/= idx count))
229 face
230 idxface))
231 )
232 (if (semantic-tag-p (car list))
233 (speedbar-make-tag-line 'angle ?i
234 'semantic-ia-sb-tag-info (car list)
235 string (if usefn function) (car list) localface
236 0)
237 (speedbar-make-tag-line 'statictag ??
238 nil nil
239 string (if usefn function) (car list) localface
240 0))
241 (setq list (cdr list)
242 count (1+ count)))
243 )))
244
245 (defun semantic-ia-sb-completion-list (list face function)
246 "Create some speedbar buttons from LIST.
247 Each button will use FACE, and be activated with FUNCTION."
248 (while list
249 (let* ((documentable nil)
250 (string (cond ((stringp (car list))
251 (car list))
252 ((semantic-tag-p (car list))
253 (setq documentable t)
254 (semantic-format-tag-uml-concise-prototype (car list)))
255 (t "foo"))))
256 (if documentable
257 (speedbar-make-tag-line 'angle ?i
258 'semantic-ia-sb-tag-info
259 (car list)
260 string function (car list) face
261 0)
262 (speedbar-make-tag-line 'statictag ? nil nil
263 string function (car list) face
264 0))
265 (setq list (cdr list)))))
266
267 (defun semantic-ia-sb-show-tag-info ()
268 "Display information about the tag on the current line.
269 Same as clicking on the <i> button.
270 See `semantic-ia-sb-tag-info' for more."
271 (interactive)
272 (let ((tok nil))
273 (save-excursion
274 (end-of-line)
275 (forward-char -1)
276 (setq tok (get-text-property (point) 'speedbar-token)))
277 (semantic-ia-sb-tag-info nil tok 0)))
278
279 (defun semantic-ia-sb-tag-info (text tag indent)
280 "Display as much information as we can about tag.
281 Show the information in a shrunk split-buffer and expand
282 out as many details as possible.
283 TEXT, TAG, and INDENT are speedbar function arguments."
284 (when (semantic-tag-p tag)
285 (unwind-protect
286 (let ((ob nil))
287 (speedbar-select-attached-frame)
288 (setq ob (current-buffer))
289 (with-output-to-temp-buffer "*Tag Information*"
290 ;; Output something about this tag:
291 (save-excursion
292 (set-buffer "*Tag Information*")
293 (goto-char (point-max))
294 (insert
295 (semantic-format-tag-prototype tag nil t)
296 "\n")
297 (let ((typetok
298 (condition-case nil
299 (save-excursion
300 (set-buffer ob)
301 ;; @todo - We need a context to derive a scope from.
302 (semantic-analyze-tag-type tag nil))
303 (error nil))))
304 (if typetok
305 (insert (semantic-format-tag-prototype
306 typetok nil t))
307 ;; No type found by the analyzer
308 ;; The below used to try and select the buffer from the last
309 ;; analysis, but since we are already in the correct buffer, I
310 ;; don't think that is needed.
311 (let ((type (semantic-tag-type tag)))
312 (cond ((semantic-tag-p type)
313 (setq type (semantic-tag-name type)))
314 ((listp type)
315 (setq type (car type))))
316 (if (semantic-lex-keyword-p type)
317 (setq typetok
318 (semantic-lex-keyword-get type 'summary))))
319 (if typetok
320 (insert typetok))
321 ))
322 ))
323 ;; Make it small
324 (shrink-window-if-larger-than-buffer
325 (get-buffer-window "*Tag Information*")))
326 (select-frame speedbar-frame))))
327
328 (defun semantic-ia-sb-line-path (&optional depth)
329 "Return the file name associated with DEPTH."
330 (save-match-data
331 (let* ((tok (speedbar-line-token))
332 (buff (if (semantic-tag-buffer tok)
333 (semantic-tag-buffer tok)
334 (current-buffer))))
335 (buffer-file-name buff))))
336
337 (defun semantic-ia-sb-complete (text tag indent)
338 "At point in the attached buffer, complete the symbol clicked on.
339 TEXT TAG and INDENT are the details."
340 ;; Find the specified bounds from the current analysis.
341 (speedbar-select-attached-frame)
342 (unwind-protect
343 (let* ((a (semantic-analyze-current-context (point)))
344 (bounds (oref a bounds))
345 (movepoint nil)
346 )
347 (save-excursion
348 (if (and (<= (point) (cdr bounds)) (>= (point) (car bounds)))
349 (setq movepoint t))
350 (goto-char (car bounds))
351 (delete-region (car bounds) (cdr bounds))
352 (insert (semantic-tag-name tag))
353 (if movepoint (setq movepoint (point)))
354 ;; I'd like to use this to add fancy () or what not at the end
355 ;; but we need the parent file whih requires an upgrade to the
356 ;; analysis tool.
357 ;;(semantic-insert-foreign-tag tag ??))
358 )
359 (if movepoint
360 (let ((cf (selected-frame)))
361 (speedbar-select-attached-frame)
362 (goto-char movepoint)
363 (select-frame cf))))
364 (select-frame speedbar-frame)))
365
366 (provide 'semantic/ia-sb)
367
368 ;; Local variables:
369 ;; generated-autoload-file: "loaddefs.el"
370 ;; generated-autoload-feature: semantic/loaddefs
371 ;; generated-autoload-load-name: "semantic/ia-sb"
372 ;; End:
373
374 ;; arch-tag: 4ab9f509-6978-415f-9938-9266edad9886
375 ;;; semantic/ia-sb.el ends here