1 ;;; semantic/ia-sb.el --- Speedbar analysis display interactor
3 ;;; Copyright (C) 2002, 2003, 2004, 2006, 2008, 2009, 2010
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Speedbar node for displaying derived context information.
29 (require 'semantic/analyze)
33 (defvar semantic-ia-sb-key-map nil
34 "Keymap used when in semantic analysis display mode.")
36 (if semantic-ia-sb-key-map
38 (setq semantic-ia-sb-key-map (speedbar-make-specialized-keymap))
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)
45 (defvar semantic-ia-sb-easymenu-definition
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 ]
53 "Extra menu items Analysis mode.")
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))
61 (speedbar-add-mode-functions-list
63 ;;'(speedbar-item-info . eieio-speedbar-item-info)
64 '(speedbar-line-directory . semantic-ia-sb-line-path)))
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."
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")
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."
87 (mode-local-active-mode nil)
89 ;; Try and get some sort of analysis
92 (speedbar-select-attached-frame)
93 (setq buffer (current-buffer))
94 (setq mode-local-active-mode major-mode)
96 ;; Get the current scope
97 (setq scope (semantic-calculate-scope (point)))
99 (setq analysis (semantic-analyze-current-context (point)))
100 (setq cnt (semantic-find-tag-by-overlay))
102 (setq completions (semantic-analyze-possible-completions analysis))
107 (with-current-buffer speedbar-buffer
108 ;; If we have something, do something spiff with it.
110 (speedbar-insert-separator "Buffer/Function")
111 ;; Note to self: Turn this into an expandable file name.
112 (speedbar-make-tag-line 'bracket ? nil nil
114 nil nil 'speedbar-file-face 0)
117 (semantic-ia-sb-string-list cnt
119 'semantic-sb-token-jump))
121 ;; If this analyzer happens to point at a complete symbol, then
122 ;; see if we can dig up some documentation for it.
123 (semantic-ia-sb-show-doc analysis))
126 ;; Let different classes draw more buttons.
127 (semantic-ia-sb-more-buttons analysis)
129 (speedbar-insert-separator "Completions")
130 (semantic-ia-sb-completion-list completions
132 'semantic-ia-sb-complete))
135 ;; Show local variables
137 (semantic-ia-sb-show-scope scope))
141 (defmethod semantic-ia-sb-show-doc ((context semantic-analyze-context))
142 "Show documentation about CONTEXT iff CONTEXT points at a complete symbol."
143 (let ((sym (car (reverse (oref context prefix))))
145 (when (semantic-tag-p sym)
146 (setq doc (semantic-documentation-for-tag sym))
148 (speedbar-insert-separator "Documentation")
154 (defun semantic-ia-sb-show-scope (scope)
155 "Show SCOPE information."
156 (let ((localvars (when scope
157 (oref scope localvar)))
160 (speedbar-insert-separator "Local Variables")
161 (semantic-ia-sb-string-list localvars
163 ;; This is from semantic-sb
164 'semantic-sb-token-jump))))
166 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context))
167 "Show a set of speedbar buttons specific to CONTEXT."
168 (let ((prefix (oref context prefix)))
170 (speedbar-insert-separator "Prefix")
171 (semantic-ia-sb-string-list prefix
173 'semantic-sb-token-jump))
176 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-assignment))
177 "Show a set of speedbar buttons specific to CONTEXT."
179 (let ((assignee (oref context assignee)))
181 (speedbar-insert-separator "Assignee")
182 (semantic-ia-sb-string-list assignee
184 'semantic-sb-token-jump))))
186 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-functionarg))
187 "Show a set of speedbar buttons specific to CONTEXT."
189 (let ((func (oref context function)))
191 (speedbar-insert-separator "Function")
192 (semantic-ia-sb-string-list func
194 'semantic-sb-token-jump)
195 ;; An index for the argument the prefix is in:
196 (let ((arg (oref context argument))
197 (args (semantic-tag-function-arguments (car func)))
200 (speedbar-insert-separator
201 (format "Argument #%d" (oref context index)))
203 (semantic-ia-sb-string-list args
205 'semantic-sb-token-jump
207 'speedbar-selected-face)
208 ;; Else, no args list, so use what the context had.
209 (semantic-ia-sb-string-list arg
211 'semantic-sb-token-jump))
214 (defun semantic-ia-sb-string-list (list face function &optional idx idxface)
215 "Create some speedbar buttons from LIST.
216 Each button will use FACE, and be activated with FUNCTION.
217 Optional IDX is an index into LIST to apply IDXFACE instead."
221 (string (cond ((stringp (car list))
223 ((semantic-tag-p (car list))
224 (setq usefn (semantic-tag-with-position-p (car list)))
225 (semantic-format-tag-uml-concise-prototype (car list)))
227 (localface (if (or (not idx) (/= idx count))
231 (if (semantic-tag-p (car list))
232 (speedbar-make-tag-line 'angle ?i
233 'semantic-ia-sb-tag-info (car list)
234 string (if usefn function) (car list) localface
236 (speedbar-make-tag-line 'statictag ??
238 string (if usefn function) (car list) localface
240 (setq list (cdr list)
244 (defun semantic-ia-sb-completion-list (list face function)
245 "Create some speedbar buttons from LIST.
246 Each button will use FACE, and be activated with FUNCTION."
248 (let* ((documentable nil)
249 (string (cond ((stringp (car list))
251 ((semantic-tag-p (car list))
252 (setq documentable t)
253 (semantic-format-tag-uml-concise-prototype (car list)))
256 (speedbar-make-tag-line 'angle ?i
257 'semantic-ia-sb-tag-info
259 string function (car list) face
261 (speedbar-make-tag-line 'statictag ? nil nil
262 string function (car list) face
264 (setq list (cdr list)))))
266 (defun semantic-ia-sb-show-tag-info ()
267 "Display information about the tag on the current line.
268 Same as clicking on the <i> button.
269 See `semantic-ia-sb-tag-info' for more."
275 (setq tok (get-text-property (point) 'speedbar-token)))
276 (semantic-ia-sb-tag-info nil tok 0)))
278 (defun semantic-ia-sb-tag-info (text tag indent)
279 "Display as much information as we can about tag.
280 Show the information in a shrunk split-buffer and expand
281 out as many details as possible.
282 TEXT, TAG, and INDENT are speedbar function arguments."
283 (when (semantic-tag-p tag)
286 (speedbar-select-attached-frame)
287 (setq ob (current-buffer))
288 (with-output-to-temp-buffer "*Tag Information*"
289 ;; Output something about this tag:
290 (with-current-buffer "*Tag Information*"
291 (goto-char (point-max))
293 (semantic-format-tag-prototype tag nil t)
297 (with-current-buffer ob
298 ;; @todo - We need a context to derive a scope from.
299 (semantic-analyze-tag-type tag nil))
302 (insert (semantic-format-tag-prototype
304 ;; No type found by the analyzer
305 ;; The below used to try and select the buffer from the last
306 ;; analysis, but since we are already in the correct buffer, I
307 ;; don't think that is needed.
308 (let ((type (semantic-tag-type tag)))
309 (cond ((semantic-tag-p type)
310 (setq type (semantic-tag-name type)))
312 (setq type (car type))))
313 (if (semantic-lex-keyword-p type)
315 (semantic-lex-keyword-get type 'summary))))
321 (shrink-window-if-larger-than-buffer
322 (get-buffer-window "*Tag Information*")))
323 (select-frame speedbar-frame))))
325 (defun semantic-ia-sb-line-path (&optional depth)
326 "Return the file name associated with DEPTH."
328 (let* ((tok (speedbar-line-token))
329 (buff (if (semantic-tag-buffer tok)
330 (semantic-tag-buffer tok)
332 (buffer-file-name buff))))
334 (defun semantic-ia-sb-complete (text tag indent)
335 "At point in the attached buffer, complete the symbol clicked on.
336 TEXT TAG and INDENT are the details."
337 ;; Find the specified bounds from the current analysis.
338 (speedbar-select-attached-frame)
340 (let* ((a (semantic-analyze-current-context (point)))
341 (bounds (oref a bounds))
345 (if (and (<= (point) (cdr bounds)) (>= (point) (car bounds)))
347 (goto-char (car bounds))
348 (delete-region (car bounds) (cdr bounds))
349 (insert (semantic-tag-name tag))
350 (if movepoint (setq movepoint (point)))
351 ;; I'd like to use this to add fancy () or what not at the end
352 ;; but we need the parent file whih requires an upgrade to the
354 ;;(semantic-insert-foreign-tag tag ??))
357 (let ((cf (selected-frame)))
358 (speedbar-select-attached-frame)
359 (goto-char movepoint)
361 (select-frame speedbar-frame)))
363 (provide 'semantic/ia-sb)
366 ;; generated-autoload-file: "loaddefs.el"
367 ;; generated-autoload-load-name: "semantic/ia-sb"
370 ;; arch-tag: 4ab9f509-6978-415f-9938-9266edad9886
371 ;;; semantic/ia-sb.el ends here