1 ;;; semantic-sb.el --- Semantic tag display for speedbar
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; X-RCS: $Id: semantic-sb.el,v 1.58 2007/02/19 02:52:50 zappo Exp $
9 ;; This file is not part of GNU Emacs.
11 ;; Semantic-sb 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 2, or (at your option)
16 ;; This software 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Convert a tag table into speedbar buttons.
32 ;; Use semanticdb to find which semanticdb-table is being used for each
33 ;; file/tag. Replace `semantic-sb-with-tag-buffer' to instead call
34 ;; children with the new `with-mode-local' instead.
37 (require 'semantic-util)
40 (inversion-require 'speedbar "0.15beta1"))
42 (defcustom semantic-sb-autoexpand-length 1
43 "*Length of a semantic bucket to autoexpand in place.
44 This will replace the named bucket that would have usually occured here."
48 (defcustom semantic-sb-button-format-tag-function 'semantic-format-tag-abbreviate
49 "*Function called to create the text for a but from a token."
51 :type semantic-format-tag-custom-list)
53 (defcustom semantic-sb-info-format-tag-function 'semantic-format-tag-summarize
54 "*Function called to create the text for info display from a token."
56 :type semantic-format-tag-custom-list)
61 ;;; Buffer setting for correct mode manipulation.
62 (defun semantic-sb-tag-set-buffer (tag)
63 "Set the current buffer to something associated with TAG.
64 use the `speedbar-line-file' to get this info if needed."
65 (if (semantic-tag-buffer tag)
66 (set-buffer (semantic-tag-buffer tag))
67 (let ((f (speedbar-line-file)))
68 (set-buffer (find-file-noselect f)))))
70 (defmacro semantic-sb-with-tag-buffer (tag &rest forms)
71 "Set the current buffer to the origin of TAG and execute FORMS.
72 Restore the old current buffer when completed."
74 (semantic-sb-tag-set-buffer ,tag)
76 (put 'semantic-sb-with-tag-buffer 'lisp-indent-function 1)
80 ;; Here are some button groups:
95 ;; +> -> click to see additional information
97 (define-overload semantic-sb-tag-children-to-expand (tag)
98 "For TAG, return a list of children that TAG expands to.
99 If this returns a value, then a +> icon is created.
100 If it returns nil, then a => icon is created.")
102 (defun semantic-sb-tag-children-to-expand-default (tag)
103 "For TAG, the children for type, variable, and function classes."
104 (semantic-sb-with-tag-buffer tag
105 (semantic-tag-components tag)))
107 (defun semantic-sb-one-button (tag depth &optional prefix)
108 "Insert TAG as a speedbar button at DEPTH.
109 Optional PREFIX is used to specify special marker characters."
110 (let* ((class (semantic-tag-class tag))
111 (edata (semantic-sb-tag-children-to-expand tag))
112 (type (semantic-tag-type tag))
113 (abbrev (semantic-sb-with-tag-buffer tag
114 (funcall semantic-sb-button-format-tag-function tag)))
117 (insert (int-to-string depth) ":")
119 (insert-char ? (1- depth) nil)
120 (put-text-property end (point) 'invisible nil)
121 ;; take care of edata = (nil) -- a yucky but hard to clean case
122 (if (and edata (listp edata) (and (<= (length edata) 1) (not (car edata))))
125 (member class '(variable function))
128 ;; types are a bit unique. Variable types can have special meaning.
130 (speedbar-insert-button (if prefix (concat " +" prefix) " +>")
131 'speedbar-button-face
132 'speedbar-highlight-face
133 'semantic-sb-show-extra
135 (speedbar-insert-button (if prefix (concat " " prefix) " =>")
137 (speedbar-insert-button abbrev
139 'speedbar-highlight-face
140 'semantic-sb-token-jump
142 ;; This is very bizarre. When this was just after the insertion
143 ;; of the depth: text, the : would get erased, but only for the
144 ;; auto-expanded short- buckets. Move back for a later version
145 ;; version of Emacs 21 CVS
146 (put-text-property start end 'invisible t)
149 (defun semantic-sb-speedbar-data-line (depth button text &optional
151 "Insert a semantic token data element.
152 DEPTH is the current depth. BUTTON is the text for the button.
153 TEXT is the actual info with TEXT-FUN to occur when it happens.
154 Argument TEXT-DATA is the token data to pass to TEXT-FUN."
155 (let ((start (point))
157 (insert (int-to-string depth) ":")
159 (put-text-property start end 'invisible t)
160 (insert-char ? depth nil)
161 (put-text-property end (point) 'invisible nil)
162 (speedbar-insert-button button nil nil nil nil t)
163 (speedbar-insert-button text
165 (if text-fun 'speedbar-highlight-face)
166 text-fun text-data t)
169 (defun semantic-sb-maybe-token-to-button (obj indent &optional
171 "Convert OBJ, which was returned from the semantic parser, into a button.
172 This OBJ might be a plain string (simple type or untyped variable)
174 Argument INDENT is the indentation used when making the button.
175 Optional PREFIX is the character to use when marking the line.
176 Optional MODIFIERS is additional text needed for variables."
177 (let ((myprefix (or prefix ">")))
179 (semantic-sb-speedbar-data-line indent myprefix obj)
182 (if (and (stringp (car obj))
184 (semantic-sb-speedbar-data-line indent myprefix
188 (semantic-sb-one-button obj indent prefix)))))))
190 (defun semantic-sb-insert-details (tag indent)
191 "Insert details about TAG at level INDENT."
192 (let ((tt (semantic-tag-class tag))
193 (type (semantic-tag-type tag)))
195 (let ((parts (semantic-tag-type-members tag))
197 ;; Lets expect PARTS to be a list of either strings,
198 ;; or variable tokens.
199 (when (semantic-tag-p (car parts))
200 ;; Bucketize into groups
201 (semantic-sb-with-tag-buffer (car parts)
202 (setq newparts (semantic-bucketize parts)))
203 (when (> (length newparts) semantic-sb-autoexpand-length)
204 ;; More than one bucket, insert inline
205 (semantic-sb-insert-tag-table (1- indent) newparts)
207 ;; Dump the strings in.
209 (semantic-sb-maybe-token-to-button (car parts) indent)
210 (setq parts (cdr parts))))))
213 (semantic-sb-maybe-token-to-button type indent "@"))
214 (let ((default (semantic-tag-variable-default tag)))
216 (semantic-sb-maybe-token-to-button default indent "=")))
220 (semantic-sb-speedbar-data-line
222 (if (stringp type) type
223 (semantic-tag-name type))))
224 ;; Arguments to the function
225 (let ((args (semantic-tag-function-arguments tag)))
226 (if (and args (car args))
228 (semantic-sb-maybe-token-to-button (car args) indent "(")
229 (setq args (cdr args))
230 (while (> (length args) 1)
231 (semantic-sb-maybe-token-to-button (car args)
234 (setq args (cdr args)))
236 (semantic-sb-maybe-token-to-button
237 (car args) indent ")"))
242 (when (and (semantic-tag-overlay tag)
243 (semantic-tag-buffer tag))
244 (set-buffer (semantic-tag-buffer tag)))
245 (semantic-sb-tag-children-to-expand tag))))
246 ;; Well, it wasn't one of the many things we expect.
247 ;; Lets just insert them in with no decoration.
249 (semantic-sb-one-button (car components) indent)
250 (setq components (cdr components)))
255 (defun semantic-sb-detail-parent ()
256 "Return the first parent token of the current line that includes a location."
259 (let ((dep (if (looking-at "[0-9]+:")
260 (1- (string-to-number (match-string 0)))
262 (re-search-backward (concat "^"
267 (if (looking-at "[0-9]+: +[-+][>()@|] \\([^\n]+\\)$")
269 (goto-char (match-beginning 1))
270 (setq prop (get-text-property (point) 'speedbar-token))
271 (if (semantic-tag-with-position-p prop)
273 (semantic-sb-detail-parent)))
276 (defun semantic-sb-show-extra (text token indent)
277 "Display additional information about the token as an expansion.
278 TEXT TOKEN and INDENT are the details."
279 (cond ((string-match "+" text) ;we have to expand this file
280 (speedbar-change-expand-button-char ?-)
281 (speedbar-with-writable
283 (end-of-line) (forward-char 1)
285 (narrow-to-region (point) (point))
286 ;; Add in stuff specific to this type of token.
287 (semantic-sb-insert-details token (1+ indent))))))
288 ((string-match "-" text) ;we have to contract this node
289 (speedbar-change-expand-button-char ?+)
290 (speedbar-delete-subblock indent))
291 (t (error "Ooops... not sure what to do")))
292 (speedbar-center-buffer-smartly))
294 (defun semantic-sb-token-jump (text token indent)
295 "Jump to the location specified in token.
296 TEXT TOKEN and INDENT are the details."
299 (cond ((fboundp 'speedbar-line-path)
300 (speedbar-line-path indent))
301 ((fboundp 'speedbar-line-directory)
302 (speedbar-line-directory indent)))
303 ;; If speedbar cannot figure this out, extract the filename from
304 ;; the token. True for Analysis mode.
305 (semantic-tag-file-name token)))
306 (parent (semantic-sb-detail-parent)))
307 (let ((f (selected-frame)))
308 (dframe-select-attached-frame speedbar-frame)
309 (run-hooks 'speedbar-before-visiting-tag-hook)
311 ;; Sometimes FILE may be nil here. If you are debugging a problem
312 ;; when this happens, go back and figure out why FILE is nil and try
313 ;; and fix the source.
314 (speedbar-find-file-in-frame file)
315 (save-excursion (speedbar-stealthy-updates))
316 (semantic-go-to-tag token parent)
317 (switch-to-buffer (current-buffer))
318 ;; Reset the timer with a new timeout when cliking a file
319 ;; in case the user was navigating directories, we can cancel
321 ;; (speedbar-set-timer dframe-update-speed)
323 (speedbar-maybee-jump-to-attached-frame)
324 (run-hooks 'speedbar-visiting-tag-hook)))
326 (defun semantic-sb-expand-group (text token indent)
327 "Expand a group which has semantic tokens.
328 TEXT TOKEN and INDENT are the details."
329 (cond ((string-match "+" text) ;we have to expand this file
330 (speedbar-change-expand-button-char ?-)
331 (speedbar-with-writable
333 (end-of-line) (forward-char 1)
335 (narrow-to-region (point-min) (point))
336 (semantic-sb-buttons-plain (1+ indent) token)))))
337 ((string-match "-" text) ;we have to contract this node
338 (speedbar-change-expand-button-char ?+)
339 (speedbar-delete-subblock indent))
340 (t (error "Ooops... not sure what to do")))
341 (speedbar-center-buffer-smartly))
343 (defun semantic-sb-buttons-plain (level tokens)
344 "Create buttons at LEVEL using TOKENS."
345 (let ((sordid (speedbar-create-tag-hierarchy tokens)))
347 (cond ((null (car-safe sordid)) nil)
348 ((consp (car-safe (cdr-safe (car-safe sordid))))
350 (speedbar-make-tag-line 'curly ?+ 'semantic-sb-expand-group
353 nil nil 'speedbar-tag-face
355 (t ;; Assume that this is a token.
356 (semantic-sb-one-button (car sordid) level)))
357 (setq sordid (cdr sordid)))))
359 (defun semantic-sb-insert-tag-table (level table)
360 "At LEVEL, insert the tag table TABLE.
361 Use arcane knowledge about the semantic tokens in the tagged elements
362 to create much wiser decisions about how to sort and group these items."
363 (semantic-sb-buttons level table))
365 (defun semantic-sb-buttons (level lst)
366 "Create buttons at LEVEL using LST sorting into type buckets."
368 (narrow-to-region (point-min) (point))
373 (if (<= (length (cdr tmp)) semantic-sb-autoexpand-length)
374 (semantic-sb-buttons-plain (1+ level) (cdr tmp))
375 (speedbar-make-tag-line 'curly ?+ 'semantic-sb-expand-group
378 nil nil 'speedbar-tag-face
380 (setq lst (cdr lst))))))
382 (defun semantic-sb-fetch-tag-table (file)
383 "Load FILE into a buffer, and generate tags using the Semantic parser.
384 Returns the tag list, or t for an error."
386 (if (and (featurep 'semanticdb) (semanticdb-minor-mode-p)
387 (not speedbar-power-click)
388 ;; If the database is loaded and running, try to get
390 (setq out (semanticdb-file-stream file)))
391 ;; Successful DB query.
393 ;; No database, do it the old way.
395 (set-buffer (find-file-noselect file))
396 (if (or (not (featurep 'semantic))
397 (not semantic--parse-table))
399 (if speedbar-power-click (semantic-clear-toplevel-cache))
400 (setq out (semantic-fetch-tags)))))
404 ;; This brings externally defind methods into
405 ;; their classes, and creates meta classes for
407 (setq out (semantic-adopt-external-members out))
408 ;; Dump all the tokens into buckets.
409 (semantic-sb-with-tag-buffer (car out)
410 (semantic-bucketize out)))
414 ;; Link ourselves into the tagging process.
415 (add-to-list 'speedbar-dynamic-tags-function-list
416 '(semantic-sb-fetch-tag-table . semantic-sb-insert-tag-table))
418 (provide 'semantic-sb)
420 ;;; semantic-sb.el ends here