1 ;;; semantic/sb.el --- Semantic tag display for speedbar
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Convert a tag table into speedbar buttons.
29 ;; Use semanticdb to find which semanticdb-table is being used for each
30 ;; file/tag. Replace `semantic-sb-with-tag-buffer' to instead call
31 ;; children with the new `with-mode-local' instead.
34 (require 'semantic/format)
35 (require 'semantic/sort)
36 (require 'semantic/util)
38 (declare-function semanticdb-file-stream "semantic/db")
40 (defcustom semantic-sb-autoexpand-length 1
41 "*Length of a semantic bucket to autoexpand in place.
42 This will replace the named bucket that would have usually occurred here."
46 (defcustom semantic-sb-button-format-tag-function 'semantic-format-tag-abbreviate
47 "*Function called to create the text for a but from a token."
49 :type semantic-format-tag-custom-list)
51 (defcustom semantic-sb-info-format-tag-function 'semantic-format-tag-summarize
52 "*Function called to create the text for info display from a token."
54 :type semantic-format-tag-custom-list)
59 ;;; Buffer setting for correct mode manipulation.
60 (defun semantic-sb-tag-set-buffer (tag)
61 "Set the current buffer to something associated with TAG.
62 use the `speedbar-line-file' to get this info if needed."
63 (if (semantic-tag-buffer tag)
64 (set-buffer (semantic-tag-buffer tag))
65 (let ((f (speedbar-line-file)))
66 (set-buffer (find-file-noselect f)))))
68 (defmacro semantic-sb-with-tag-buffer (tag &rest forms)
69 "Set the current buffer to the origin of TAG and execute FORMS.
70 Restore the old current buffer when completed."
72 (semantic-sb-tag-set-buffer ,tag)
74 (put 'semantic-sb-with-tag-buffer 'lisp-indent-function 1)
78 ;; Here are some button groups:
93 ;; +> -> click to see additional information
95 (define-overloadable-function semantic-sb-tag-children-to-expand (tag)
96 "For TAG, return a list of children that TAG expands to.
97 If this returns a value, then a +> icon is created.
98 If it returns nil, then a => icon is created.")
100 (defun semantic-sb-tag-children-to-expand-default (tag)
101 "For TAG, the children for type, variable, and function classes."
102 (semantic-sb-with-tag-buffer tag
103 (semantic-tag-components tag)))
105 (defun semantic-sb-one-button (tag depth &optional prefix)
106 "Insert TAG as a speedbar button at DEPTH.
107 Optional PREFIX is used to specify special marker characters."
108 (let* ((class (semantic-tag-class tag))
109 (edata (semantic-sb-tag-children-to-expand tag))
110 (type (semantic-tag-type tag))
111 (abbrev (semantic-sb-with-tag-buffer tag
112 (funcall semantic-sb-button-format-tag-function tag)))
115 (insert (int-to-string depth) ":")
117 (insert-char ? (1- depth) nil)
118 (put-text-property end (point) 'invisible nil)
119 ;; take care of edata = (nil) -- a yucky but hard to clean case
120 (if (and edata (listp edata) (and (<= (length edata) 1) (not (car edata))))
123 (member class '(variable function))
126 ;; types are a bit unique. Variable types can have special meaning.
128 (speedbar-insert-button (if prefix (concat " +" prefix) " +>")
129 'speedbar-button-face
130 'speedbar-highlight-face
131 'semantic-sb-show-extra
133 (speedbar-insert-button (if prefix (concat " " prefix) " =>")
135 (speedbar-insert-button abbrev
137 'speedbar-highlight-face
138 'semantic-sb-token-jump
140 ;; This is very bizarre. When this was just after the insertion
141 ;; of the depth: text, the : would get erased, but only for the
142 ;; auto-expanded short- buckets. Move back for a later version
143 ;; version of Emacs 21 CVS
144 (put-text-property start end 'invisible t)
147 (defun semantic-sb-speedbar-data-line (depth button text &optional
149 "Insert a semantic token data element.
150 DEPTH is the current depth. BUTTON is the text for the button.
151 TEXT is the actual info with TEXT-FUN to occur when it happens.
152 Argument TEXT-DATA is the token data to pass to TEXT-FUN."
153 (let ((start (point))
155 (insert (int-to-string depth) ":")
157 (put-text-property start end 'invisible t)
158 (insert-char ? depth nil)
159 (put-text-property end (point) 'invisible nil)
160 (speedbar-insert-button button nil nil nil nil t)
161 (speedbar-insert-button text
163 (if text-fun 'speedbar-highlight-face)
164 text-fun text-data t)
167 (defun semantic-sb-maybe-token-to-button (obj indent &optional
169 "Convert OBJ, which was returned from the semantic parser, into a button.
170 This OBJ might be a plain string (simple type or untyped variable)
172 Argument INDENT is the indentation used when making the button.
173 Optional PREFIX is the character to use when marking the line.
174 Optional MODIFIERS is additional text needed for variables."
175 (let ((myprefix (or prefix ">")))
177 (semantic-sb-speedbar-data-line indent myprefix obj)
180 (if (and (stringp (car obj))
182 (semantic-sb-speedbar-data-line indent myprefix
186 (semantic-sb-one-button obj indent prefix)))))))
188 (defun semantic-sb-insert-details (tag indent)
189 "Insert details about TAG at level INDENT."
190 (let ((tt (semantic-tag-class tag))
191 (type (semantic-tag-type tag)))
193 (let ((parts (semantic-tag-type-members tag))
195 ;; Lets expect PARTS to be a list of either strings,
196 ;; or variable tokens.
197 (when (semantic-tag-p (car parts))
198 ;; Bucketize into groups
199 (semantic-sb-with-tag-buffer (car parts)
200 (setq newparts (semantic-bucketize parts)))
201 (when (> (length newparts) semantic-sb-autoexpand-length)
202 ;; More than one bucket, insert inline
203 (semantic-sb-insert-tag-table (1- indent) newparts)
205 ;; Dump the strings in.
207 (semantic-sb-maybe-token-to-button (car parts) indent)
208 (setq parts (cdr parts))))))
211 (semantic-sb-maybe-token-to-button type indent "@"))
212 (let ((default (semantic-tag-variable-default tag)))
214 (semantic-sb-maybe-token-to-button default indent "=")))
218 (semantic-sb-speedbar-data-line
220 (if (stringp type) type
221 (semantic-tag-name type))))
222 ;; Arguments to the function
223 (let ((args (semantic-tag-function-arguments tag)))
224 (if (and args (car args))
226 (semantic-sb-maybe-token-to-button (car args) indent "(")
227 (setq args (cdr args))
228 (while (> (length args) 1)
229 (semantic-sb-maybe-token-to-button (car args)
232 (setq args (cdr args)))
234 (semantic-sb-maybe-token-to-button
235 (car args) indent ")"))
240 (when (and (semantic-tag-overlay tag)
241 (semantic-tag-buffer tag))
242 (set-buffer (semantic-tag-buffer tag)))
243 (semantic-sb-tag-children-to-expand tag))))
244 ;; Well, it wasn't one of the many things we expect.
245 ;; Lets just insert them in with no decoration.
247 (semantic-sb-one-button (car components) indent)
248 (setq components (cdr components)))
253 (defun semantic-sb-detail-parent ()
254 "Return the first parent token of the current line that includes a location."
257 (let ((dep (if (looking-at "[0-9]+:")
258 (1- (string-to-number (match-string 0)))
260 (re-search-backward (concat "^"
265 (if (looking-at "[0-9]+: +[-+][>()@|] \\([^\n]+\\)$")
267 (goto-char (match-beginning 1))
268 (setq prop (get-text-property (point) 'speedbar-token))
269 (if (semantic-tag-with-position-p prop)
271 (semantic-sb-detail-parent)))
274 (defun semantic-sb-show-extra (text token indent)
275 "Display additional information about the token as an expansion.
276 TEXT TOKEN and INDENT are the details."
277 (cond ((string-match "+" text) ;we have to expand this file
278 (speedbar-change-expand-button-char ?-)
279 (speedbar-with-writable
281 (end-of-line) (forward-char 1)
283 (narrow-to-region (point) (point))
284 ;; Add in stuff specific to this type of token.
285 (semantic-sb-insert-details token (1+ indent))))))
286 ((string-match "-" text) ;we have to contract this node
287 (speedbar-change-expand-button-char ?+)
288 (speedbar-delete-subblock indent))
289 (t (error "Ooops... not sure what to do")))
290 (speedbar-center-buffer-smartly))
292 (defun semantic-sb-token-jump (text token indent)
293 "Jump to the location specified in token.
294 TEXT TOKEN and INDENT are the details."
297 (cond ((fboundp 'speedbar-line-path)
298 (speedbar-line-directory indent))
299 ((fboundp 'speedbar-line-directory)
300 (speedbar-line-directory indent)))
301 ;; If speedbar cannot figure this out, extract the filename from
302 ;; the token. True for Analysis mode.
303 (semantic-tag-file-name token)))
304 (parent (semantic-sb-detail-parent)))
305 (let ((f (selected-frame)))
306 (dframe-select-attached-frame speedbar-frame)
307 (run-hooks 'speedbar-before-visiting-tag-hook)
309 ;; Sometimes FILE may be nil here. If you are debugging a problem
310 ;; when this happens, go back and figure out why FILE is nil and try
311 ;; and fix the source.
312 (speedbar-find-file-in-frame file)
313 (save-excursion (speedbar-stealthy-updates))
314 (semantic-go-to-tag token parent)
315 (switch-to-buffer (current-buffer))
316 ;; Reset the timer with a new timeout when cliking a file
317 ;; in case the user was navigating directories, we can cancel
319 ;; (speedbar-set-timer dframe-update-speed)
321 (speedbar-maybee-jump-to-attached-frame)
322 (run-hooks 'speedbar-visiting-tag-hook)))
324 (defun semantic-sb-expand-group (text token indent)
325 "Expand a group which has semantic tokens.
326 TEXT TOKEN and INDENT are the details."
327 (cond ((string-match "+" text) ;we have to expand this file
328 (speedbar-change-expand-button-char ?-)
329 (speedbar-with-writable
331 (end-of-line) (forward-char 1)
333 (narrow-to-region (point-min) (point))
334 (semantic-sb-buttons-plain (1+ indent) token)))))
335 ((string-match "-" text) ;we have to contract this node
336 (speedbar-change-expand-button-char ?+)
337 (speedbar-delete-subblock indent))
338 (t (error "Ooops... not sure what to do")))
339 (speedbar-center-buffer-smartly))
341 (defun semantic-sb-buttons-plain (level tokens)
342 "Create buttons at LEVEL using TOKENS."
343 (let ((sordid (speedbar-create-tag-hierarchy tokens)))
345 (cond ((null (car-safe sordid)) nil)
346 ((consp (car-safe (cdr-safe (car-safe sordid))))
348 (speedbar-make-tag-line 'curly ?+ 'semantic-sb-expand-group
351 nil nil 'speedbar-tag-face
353 (t ;; Assume that this is a token.
354 (semantic-sb-one-button (car sordid) level)))
355 (setq sordid (cdr sordid)))))
357 (defun semantic-sb-insert-tag-table (level table)
358 "At LEVEL, insert the tag table TABLE.
359 Use arcane knowledge about the semantic tokens in the tagged elements
360 to create much wiser decisions about how to sort and group these items."
361 (semantic-sb-buttons level table))
363 (defun semantic-sb-buttons (level lst)
364 "Create buttons at LEVEL using LST sorting into type buckets."
366 (narrow-to-region (point-min) (point))
371 (if (<= (length (cdr tmp)) semantic-sb-autoexpand-length)
372 (semantic-sb-buttons-plain (1+ level) (cdr tmp))
373 (speedbar-make-tag-line 'curly ?+ 'semantic-sb-expand-group
376 nil nil 'speedbar-tag-face
378 (setq lst (cdr lst))))))
380 (defun semantic-sb-fetch-tag-table (file)
381 "Load FILE into a buffer, and generate tags using the Semantic parser.
382 Returns the tag list, or t for an error."
384 (if (and (featurep 'semantic/db)
385 (semanticdb-minor-mode-p)
386 (not speedbar-power-click)
387 ;; If the database is loaded and running, try to get
389 (setq out (semanticdb-file-stream file)))
390 ;; Successful DB query.
392 ;; No database, do it the old way.
393 (with-current-buffer (find-file-noselect file)
394 (if (or (not (featurep 'semantic))
395 (not semantic--parse-table))
397 (if speedbar-power-click (semantic-clear-toplevel-cache))
398 (setq out (semantic-fetch-tags)))))
402 ;; This brings externally defind methods into
403 ;; their classes, and creates meta classes for
405 (setq out (semantic-adopt-external-members out))
406 ;; Dump all the tokens into buckets.
407 (semantic-sb-with-tag-buffer (car out)
408 (semantic-bucketize out)))
412 ;; Link ourselves into the tagging process.
413 (add-to-list 'speedbar-dynamic-tags-function-list
414 '(semantic-sb-fetch-tag-table . semantic-sb-insert-tag-table))
416 (provide 'semantic/sb)
418 ;;; semantic/sb.el ends here