1 ;;; semantic/sb.el --- Semantic tag display for speedbar
3 ;; Copyright (C) 1999-2013 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 (defvar semantic-sb-filter-tags-of-class
'(code)
47 "Tags classes to not display in speedbar.
48 Make this buffer local for modes that have different types of tags
49 that should be ignored.")
51 (defcustom semantic-sb-button-format-tag-function
'semantic-format-tag-abbreviate
52 "*Function called to create the text for a but from a token."
54 :type semantic-format-tag-custom-list
)
56 (defcustom semantic-sb-info-format-tag-function
'semantic-format-tag-summarize
57 "*Function called to create the text for info display from a token."
59 :type semantic-format-tag-custom-list
)
64 ;;; Buffer setting for correct mode manipulation.
65 (defun semantic-sb-tag-set-buffer (tag)
66 "Set the current buffer to something associated with TAG.
67 use the `speedbar-line-file' to get this info if needed."
68 (if (semantic-tag-buffer tag
)
69 (set-buffer (semantic-tag-buffer tag
))
70 (let ((f (speedbar-line-file)))
71 (set-buffer (find-file-noselect f
)))))
73 (defmacro semantic-sb-with-tag-buffer
(tag &rest forms
)
74 "Set the current buffer to the origin of TAG and execute FORMS.
75 Restore the old current buffer when completed."
77 (semantic-sb-tag-set-buffer ,tag
)
79 (put 'semantic-sb-with-tag-buffer
'lisp-indent-function
1)
83 ;; Here are some button groups:
98 ;; +> -> click to see additional information
100 (define-overloadable-function semantic-sb-tag-children-to-expand
(tag)
101 "For TAG, return a list of children that TAG expands to.
102 If this returns a value, then a +> icon is created.
103 If it returns nil, then a => icon is created.")
105 (defun semantic-sb-tag-children-to-expand-default (tag)
106 "For TAG, the children for type, variable, and function classes."
107 (semantic-sb-with-tag-buffer tag
108 (semantic-tag-components tag
)))
110 (defun semantic-sb-one-button (tag depth
&optional prefix
)
111 "Insert TAG as a speedbar button at DEPTH.
112 Optional PREFIX is used to specify special marker characters."
113 (let* ((class (semantic-tag-class tag
))
114 (edata (semantic-sb-tag-children-to-expand tag
))
115 (type (semantic-tag-type tag
))
116 (abbrev (semantic-sb-with-tag-buffer tag
117 (funcall semantic-sb-button-format-tag-function tag
)))
120 (insert (int-to-string depth
) ":")
122 (insert-char ?
(1- depth
) nil
)
123 (put-text-property end
(point) 'invisible nil
)
124 ;; take care of edata = (nil) -- a yucky but hard to clean case
125 (if (and edata
(listp edata
) (and (<= (length edata
) 1) (not (car edata
))))
128 (member class
'(variable function
))
131 ;; types are a bit unique. Variable types can have special meaning.
133 (speedbar-insert-button (if prefix
(concat " +" prefix
) " +>")
134 'speedbar-button-face
135 'speedbar-highlight-face
136 'semantic-sb-show-extra
138 (speedbar-insert-button (if prefix
(concat " " prefix
) " =>")
140 (speedbar-insert-button abbrev
142 'speedbar-highlight-face
143 'semantic-sb-token-jump
145 ;; This is very bizarre. When this was just after the insertion
146 ;; of the depth: text, the : would get erased, but only for the
147 ;; auto-expanded short- buckets. Move back for a later version
148 ;; version of Emacs 21 CVS
149 (put-text-property start end
'invisible t
)
152 (defun semantic-sb-speedbar-data-line (depth button text
&optional
154 "Insert a semantic token data element.
155 DEPTH is the current depth. BUTTON is the text for the button.
156 TEXT is the actual info with TEXT-FUN to occur when it happens.
157 Argument TEXT-DATA is the token data to pass to TEXT-FUN."
158 (let ((start (point))
160 (insert (int-to-string depth
) ":")
162 (put-text-property start end
'invisible t
)
163 (insert-char ? depth nil
)
164 (put-text-property end
(point) 'invisible nil
)
165 (speedbar-insert-button button nil nil nil nil t
)
166 (speedbar-insert-button text
168 (if text-fun
'speedbar-highlight-face
)
169 text-fun text-data t
)
172 (defun semantic-sb-maybe-token-to-button (obj indent
&optional
174 "Convert OBJ, which was returned from the semantic parser, into a button.
175 This OBJ might be a plain string (simple type or untyped variable)
177 Argument INDENT is the indentation used when making the button.
178 Optional PREFIX is the character to use when marking the line.
179 Optional MODIFIERS is additional text needed for variables."
180 (let ((myprefix (or prefix
">")))
182 (semantic-sb-speedbar-data-line indent myprefix obj
)
185 (if (and (stringp (car obj
))
187 (semantic-sb-speedbar-data-line indent myprefix
191 (semantic-sb-one-button obj indent prefix
)))))))
193 (defun semantic-sb-insert-details (tag indent
)
194 "Insert details about TAG at level INDENT."
195 (let ((tt (semantic-tag-class tag
))
196 (type (semantic-tag-type tag
)))
198 (let ((parts (semantic-tag-type-members tag
))
200 ;; Lets expect PARTS to be a list of either strings,
201 ;; or variable tokens.
202 (when (semantic-tag-p (car parts
))
203 ;; Bucketize into groups
204 (semantic-sb-with-tag-buffer (car parts
)
205 (setq newparts
(semantic-bucketize parts
)))
206 (when (> (length newparts
) semantic-sb-autoexpand-length
)
207 ;; More than one bucket, insert inline
208 (semantic-sb-insert-tag-table (1- indent
) newparts
)
210 ;; Dump the strings in.
212 (semantic-sb-maybe-token-to-button (car parts
) indent
)
213 (setq parts
(cdr parts
))))))
216 (semantic-sb-maybe-token-to-button type indent
"@"))
217 (let ((default (semantic-tag-variable-default tag
)))
219 (semantic-sb-maybe-token-to-button default indent
"=")))
223 (semantic-sb-speedbar-data-line
225 (if (stringp type
) type
226 (semantic-tag-name type
))))
227 ;; Arguments to the function
228 (let ((args (semantic-tag-function-arguments tag
)))
229 (if (and args
(car args
))
231 (semantic-sb-maybe-token-to-button (car args
) indent
"(")
232 (setq args
(cdr args
))
233 (while (> (length args
) 1)
234 (semantic-sb-maybe-token-to-button (car args
)
237 (setq args
(cdr args
)))
239 (semantic-sb-maybe-token-to-button
240 (car args
) indent
")"))
245 (when (and (semantic-tag-overlay tag
)
246 (semantic-tag-buffer tag
))
247 (set-buffer (semantic-tag-buffer tag
)))
248 (semantic-sb-tag-children-to-expand tag
))))
249 ;; Well, it wasn't one of the many things we expect.
250 ;; Lets just insert them in with no decoration.
252 (semantic-sb-one-button (car components
) indent
)
253 (setq components
(cdr components
)))
258 (defun semantic-sb-detail-parent ()
259 "Return the first parent token of the current line that includes a location."
262 (let ((dep (if (looking-at "[0-9]+:")
263 (1- (string-to-number (match-string 0)))
265 (re-search-backward (concat "^"
270 (if (looking-at "[0-9]+: +[-+][>()@|] \\([^\n]+\\)$")
272 (goto-char (match-beginning 1))
273 (setq prop
(get-text-property (point) 'speedbar-token
))
274 (if (semantic-tag-with-position-p prop
)
276 (semantic-sb-detail-parent)))
279 (defun semantic-sb-show-extra (text token indent
)
280 "Display additional information about the token as an expansion.
281 TEXT TOKEN and INDENT are the details."
282 (cond ((string-match "+" text
) ;we have to expand this file
283 (speedbar-change-expand-button-char ?-
)
284 (speedbar-with-writable
286 (end-of-line) (forward-char 1)
288 (narrow-to-region (point) (point))
289 ;; Add in stuff specific to this type of token.
290 (semantic-sb-insert-details token
(1+ indent
))))))
291 ((string-match "-" text
) ;we have to contract this node
292 (speedbar-change-expand-button-char ?
+)
293 (speedbar-delete-subblock indent
))
294 (t (error "Ooops... not sure what to do")))
295 (speedbar-center-buffer-smartly))
297 (defun semantic-sb-token-jump (text token indent
)
298 "Jump to the location specified in token.
299 TEXT TOKEN and INDENT are the details."
302 (cond ((fboundp 'speedbar-line-path
)
303 (speedbar-line-directory indent
))
304 ((fboundp 'speedbar-line-directory
)
305 (speedbar-line-directory indent
)))
306 ;; If speedbar cannot figure this out, extract the filename from
307 ;; the token. True for Analysis mode.
308 (semantic-tag-file-name token
)))
309 (parent (semantic-sb-detail-parent)))
310 (let ((f (selected-frame)))
311 (dframe-select-attached-frame speedbar-frame
)
312 (run-hooks 'speedbar-before-visiting-tag-hook
)
314 ;; Sometimes FILE may be nil here. If you are debugging a problem
315 ;; when this happens, go back and figure out why FILE is nil and try
316 ;; and fix the source.
317 (speedbar-find-file-in-frame file
)
318 (save-excursion (speedbar-stealthy-updates))
319 (semantic-go-to-tag token parent
)
320 (switch-to-buffer (current-buffer))
321 ;; Reset the timer with a new timeout when clicking a file
322 ;; in case the user was navigating directories, we can cancel
324 ;; (speedbar-set-timer dframe-update-speed)
326 (dframe-maybee-jump-to-attached-frame)
327 (run-hooks 'speedbar-visiting-tag-hook
)))
329 (defun semantic-sb-expand-group (text token indent
)
330 "Expand a group which has semantic tokens.
331 TEXT TOKEN and INDENT are the details."
332 (cond ((string-match "+" text
) ;we have to expand this file
333 (speedbar-change-expand-button-char ?-
)
334 (speedbar-with-writable
336 (end-of-line) (forward-char 1)
338 (narrow-to-region (point-min) (point))
339 (semantic-sb-buttons-plain (1+ indent
) token
)))))
340 ((string-match "-" text
) ;we have to contract this node
341 (speedbar-change-expand-button-char ?
+)
342 (speedbar-delete-subblock indent
))
343 (t (error "Ooops... not sure what to do")))
344 (speedbar-center-buffer-smartly))
346 (defun semantic-sb-buttons-plain (level tokens
)
347 "Create buttons at LEVEL using TOKENS."
348 (let ((sordid (speedbar-create-tag-hierarchy tokens
)))
350 (cond ((null (car-safe sordid
)) nil
)
351 ((consp (car-safe (cdr-safe (car-safe sordid
))))
353 (speedbar-make-tag-line 'curly ?
+ 'semantic-sb-expand-group
356 nil nil
'speedbar-tag-face
358 (t ;; Assume that this is a token.
359 (semantic-sb-one-button (car sordid
) level
)))
360 (setq sordid
(cdr sordid
)))))
362 (defun semantic-sb-insert-tag-table (level table
)
363 "At LEVEL, insert the tag table TABLE.
364 Use arcane knowledge about the semantic tokens in the tagged elements
365 to create much wiser decisions about how to sort and group these items."
366 (semantic-sb-buttons level table
))
368 (defun semantic-sb-buttons (level lst
)
369 "Create buttons at LEVEL using LST sorting into type buckets."
371 (narrow-to-region (point-min) (point))
376 (if (<= (length (cdr tmp
)) semantic-sb-autoexpand-length
)
377 (semantic-sb-buttons-plain (1+ level
) (cdr tmp
))
378 (speedbar-make-tag-line 'curly ?
+ 'semantic-sb-expand-group
381 nil nil
'speedbar-tag-face
383 (setq lst
(cdr lst
))))))
385 (defun semantic-sb-fetch-tag-table (file)
386 "Load FILE into a buffer, and generate tags using the Semantic parser.
387 Returns the tag list, or t for an error."
389 (if (and (featurep 'semantic
/db
)
390 (semanticdb-minor-mode-p)
391 (not speedbar-power-click
)
392 ;; If the database is loaded and running, try to get
394 (setq out
(semanticdb-file-stream file
)))
395 ;; Successful DB query.
397 ;; No database, do it the old way.
398 (with-current-buffer (find-file-noselect file
)
399 (if (or (not (featurep 'semantic
))
400 (not semantic--parse-table
))
402 (if speedbar-power-click
(semantic-clear-toplevel-cache))
403 (setq out
(semantic-fetch-tags)))))
407 ;; This brings externally defined methods into
408 ;; their classes, and creates meta classes for
410 (setq out
(semantic-adopt-external-members out
))
411 ;; Dump all the tokens into buckets.
412 (semantic-sb-with-tag-buffer (car out
)
413 (semantic-bucketize out nil
415 ;; Remove all boring tags.
416 (semantic-filter-tags-by-class
417 semantic-sb-filter-tags-of-class
422 ;; Link ourselves into the tagging process.
423 (add-to-list 'speedbar-dynamic-tags-function-list
424 '(semantic-sb-fetch-tag-table . semantic-sb-insert-tag-table
))
426 (provide 'semantic
/sb
)
429 ;; generated-autoload-load-name: "semantic/sb"
432 ;;; semantic/sb.el ends here