1 ;;; semantic/ia-sb.el --- Speedbar analysis display interactor
3 ;;; Copyright (C) 2002, 2003, 2004, 2006, 2008, 2009
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))
108 (set-buffer speedbar-buffer)
109 ;; If we have something, do something spiff with it.
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
115 nil nil 'speedbar-file-face 0)
118 (semantic-ia-sb-string-list cnt
120 'semantic-sb-token-jump))
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))
127 ;; Let different classes draw more buttons.
128 (semantic-ia-sb-more-buttons analysis)
130 (speedbar-insert-separator "Completions")
131 (semantic-ia-sb-completion-list completions
133 'semantic-ia-sb-complete))
136 ;; Show local variables
138 (semantic-ia-sb-show-scope scope))
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))))
146 (when (semantic-tag-p sym)
147 (setq doc (semantic-documentation-for-tag sym))
149 (speedbar-insert-separator "Documentation")
155 (defun semantic-ia-sb-show-scope (scope)
156 "Show SCOPE information."
157 (let ((localvars (when scope
158 (oref scope localvar)))
161 (speedbar-insert-separator "Local Variables")
162 (semantic-ia-sb-string-list localvars
164 ;; This is from semantic-sb
165 'semantic-sb-token-jump))))
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)))
171 (speedbar-insert-separator "Prefix")
172 (semantic-ia-sb-string-list prefix
174 'semantic-sb-token-jump))
177 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-assignment))
178 "Show a set of speedbar buttons specific to CONTEXT."
180 (let ((assignee (oref context assignee)))
182 (speedbar-insert-separator "Assignee")
183 (semantic-ia-sb-string-list assignee
185 'semantic-sb-token-jump))))
187 (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-functionarg))
188 "Show a set of speedbar buttons specific to CONTEXT."
190 (let ((func (oref context function)))
192 (speedbar-insert-separator "Function")
193 (semantic-ia-sb-string-list func
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)))
201 (speedbar-insert-separator
202 (format "Argument #%d" (oref context index)))
204 (semantic-ia-sb-string-list args
206 'semantic-sb-token-jump
208 'speedbar-selected-face)
209 ;; Else, no args list, so use what the context had.
210 (semantic-ia-sb-string-list arg
212 'semantic-sb-token-jump))
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."
222 (string (cond ((stringp (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)))
228 (localface (if (or (not idx) (/= idx count))
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
237 (speedbar-make-tag-line 'statictag ??
239 string (if usefn function) (car list) localface
241 (setq list (cdr list)
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."
249 (let* ((documentable nil)
250 (string (cond ((stringp (car list))
252 ((semantic-tag-p (car list))
253 (setq documentable t)
254 (semantic-format-tag-uml-concise-prototype (car list)))
257 (speedbar-make-tag-line 'angle ?i
258 'semantic-ia-sb-tag-info
260 string function (car list) face
262 (speedbar-make-tag-line 'statictag ? nil nil
263 string function (car list) face
265 (setq list (cdr list)))))
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."
276 (setq tok (get-text-property (point) 'speedbar-token)))
277 (semantic-ia-sb-tag-info nil tok 0)))
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)
287 (speedbar-select-attached-frame)
288 (setq ob (current-buffer))
289 (with-output-to-temp-buffer "*Tag Information*"
290 ;; Output something about this tag:
292 (set-buffer "*Tag Information*")
293 (goto-char (point-max))
295 (semantic-format-tag-prototype tag nil t)
301 ;; @todo - We need a context to derive a scope from.
302 (semantic-analyze-tag-type tag nil))
305 (insert (semantic-format-tag-prototype
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)))
315 (setq type (car type))))
316 (if (semantic-lex-keyword-p type)
318 (semantic-lex-keyword-get type 'summary))))
324 (shrink-window-if-larger-than-buffer
325 (get-buffer-window "*Tag Information*")))
326 (select-frame speedbar-frame))))
328 (defun semantic-ia-sb-line-path (&optional depth)
329 "Return the file name associated with DEPTH."
331 (let* ((tok (speedbar-line-token))
332 (buff (if (semantic-tag-buffer tok)
333 (semantic-tag-buffer tok)
335 (buffer-file-name buff))))
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)
343 (let* ((a (semantic-analyze-current-context (point)))
344 (bounds (oref a bounds))
348 (if (and (<= (point) (cdr bounds)) (>= (point) (car bounds)))
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
357 ;;(semantic-insert-foreign-tag tag ??))
360 (let ((cf (selected-frame)))
361 (speedbar-select-attached-frame)
362 (goto-char movepoint)
364 (select-frame speedbar-frame)))
366 (provide 'semantic/ia-sb)
369 ;; generated-autoload-file: "loaddefs.el"
370 ;; generated-autoload-feature: semantic/loaddefs
371 ;; generated-autoload-load-name: "semantic/ia-sb"
374 ;;; semantic/ia-sb.el ends here