Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / semantic / semantic-sb.el
1 ;;; semantic-sb.el --- Semantic tag display for speedbar
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: syntax
7 ;; X-RCS: $Id: semantic-sb.el,v 1.58 2007/02/19 02:52:50 zappo Exp $
8
9 ;; This file is not part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27 ;;
28 ;; Convert a tag table into speedbar buttons.
29
30 ;;; TODO:
31 ;; 
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.
35
36 (require 'semantic)
37 (require 'semantic-util)
38 (require 'inversion)
39 (eval-and-compile
40   (inversion-require 'speedbar "0.15beta1"))
41
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."
45   :group 'speedbar
46   :type 'integer)
47
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."
50   :group 'speedbar
51   :type semantic-format-tag-custom-list)
52
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."
55   :group 'speedbar
56   :type semantic-format-tag-custom-list)
57
58 ;;; Code:
59 ;;
60
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)))))
69
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."
73   `(save-excursion
74      (semantic-sb-tag-set-buffer ,tag)
75      ,@forms))
76 (put 'semantic-sb-with-tag-buffer 'lisp-indent-function 1)
77
78 ;;; Button Generation
79 ;;
80 ;;  Here are some button groups:
81 ;;
82 ;;  +> Function ()
83 ;;     @ return_type
84 ;;    +( arg1
85 ;;    +| arg2
86 ;;    +) arg3
87 ;;
88 ;;  +> Variable[1] =
89 ;;    @ type
90 ;;    = default value
91 ;;
92 ;;  +> keywrd Type
93 ;;   +> type part
94 ;;
95 ;;  +>  -> click to see additional information
96
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.")
101
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)))
106
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)))
115          (start (point))
116          (end (progn
117                 (insert (int-to-string depth) ":")
118                 (point))))
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))))
123         (setq edata nil))
124     (if (and (not edata)
125              (member class '(variable function))
126              type)
127         (setq edata t))
128     ;; types are a bit unique.  Variable types can have special meaning.
129     (if edata
130         (speedbar-insert-button (if prefix (concat " +" prefix) " +>")
131                                 'speedbar-button-face
132                                 'speedbar-highlight-face
133                                 'semantic-sb-show-extra
134                                 tag t)
135       (speedbar-insert-button (if prefix (concat "  " prefix) " =>")
136                               nil nil nil nil t))
137     (speedbar-insert-button abbrev
138                             'speedbar-tag-face
139                             'speedbar-highlight-face
140                             'semantic-sb-token-jump
141                             tag t)
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)
147     ))
148   
149 (defun semantic-sb-speedbar-data-line (depth button text &optional
150                                              text-fun text-data)
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))
156         (end (progn
157                (insert (int-to-string depth) ":")
158                (point))))
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
164                             'speedbar-tag-face
165                             (if text-fun 'speedbar-highlight-face)
166                             text-fun text-data t)
167     ))
168
169 (defun semantic-sb-maybe-token-to-button (obj indent &optional
170                                               prefix modifiers)
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)
173 or a complete tag.
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 ">")))
178     (if (stringp obj)
179         (semantic-sb-speedbar-data-line indent myprefix obj)
180       (if (listp obj)
181           (progn
182             (if (and (stringp (car obj))
183                      (= (length obj) 1))
184                 (semantic-sb-speedbar-data-line indent myprefix
185                                                 (concat
186                                                  (car obj)
187                                                  (or modifiers "")))
188               (semantic-sb-one-button obj indent prefix)))))))
189
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)))
194     (cond ((eq tt 'type)
195            (let ((parts (semantic-tag-type-members tag))
196                  (newparts nil))
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)
206                  (setq parts nil))
207                ;; Dump the strings in.
208                (while parts
209                  (semantic-sb-maybe-token-to-button (car parts) indent)
210                  (setq parts (cdr parts))))))
211           ((eq tt 'variable)
212            (if type
213                (semantic-sb-maybe-token-to-button type indent "@"))
214            (let ((default (semantic-tag-variable-default tag)))
215              (if default
216                  (semantic-sb-maybe-token-to-button default indent "=")))
217            )
218           ((eq tt 'function)
219            (if type
220                (semantic-sb-speedbar-data-line
221                 indent "@"
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))
227                  (progn
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)
232                                                         indent
233                                                         "|")
234                      (setq args (cdr args)))
235                    (if args
236                        (semantic-sb-maybe-token-to-button
237                         (car args) indent ")"))
238                    ))))
239           (t
240            (let ((components
241                   (save-excursion
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.
248              (while components
249                (semantic-sb-one-button (car components) indent)
250                (setq components (cdr components)))
251              ))
252           )
253     ))
254
255 (defun semantic-sb-detail-parent ()
256   "Return the first parent token of the current line that includes a location."
257   (save-excursion
258     (beginning-of-line)
259     (let ((dep (if (looking-at "[0-9]+:")
260                    (1- (string-to-number (match-string 0)))
261                  0)))
262       (re-search-backward (concat "^"
263                                   (int-to-string dep)
264                                   ":")
265                           nil t))
266     (beginning-of-line)
267     (if (looking-at "[0-9]+: +[-+][>()@|] \\([^\n]+\\)$")
268         (let ((prop nil))
269           (goto-char (match-beginning 1))
270           (setq prop (get-text-property (point) 'speedbar-token))
271           (if (semantic-tag-with-position-p prop)
272               prop
273             (semantic-sb-detail-parent)))
274       nil)))
275
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
282            (save-excursion
283              (end-of-line) (forward-char 1)
284              (save-restriction
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))
293
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."
297   (let ((file
298          (or
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)
310       (select-frame f))
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
320     ;; that other timer.
321     ;; (speedbar-set-timer dframe-update-speed)
322     ;;(recenter)
323     (speedbar-maybee-jump-to-attached-frame)
324     (run-hooks 'speedbar-visiting-tag-hook)))
325
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
332            (save-excursion
333              (end-of-line) (forward-char 1)
334              (save-restriction
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))
342
343 (defun semantic-sb-buttons-plain (level tokens)
344   "Create buttons at LEVEL using TOKENS."
345   (let ((sordid (speedbar-create-tag-hierarchy tokens)))
346     (while sordid
347       (cond ((null (car-safe sordid)) nil)
348             ((consp (car-safe (cdr-safe (car-safe sordid))))
349              ;; A group!
350              (speedbar-make-tag-line 'curly ?+ 'semantic-sb-expand-group
351                                      (cdr (car sordid))
352                                      (car (car sordid))
353                                      nil nil 'speedbar-tag-face
354                                      level))
355             (t ;; Assume that this is a token.
356              (semantic-sb-one-button (car sordid) level)))
357       (setq sordid (cdr sordid)))))
358
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))
364
365 (defun semantic-sb-buttons (level lst)
366   "Create buttons at LEVEL using LST sorting into type buckets."
367   (save-restriction
368     (narrow-to-region (point-min) (point))
369     (let (tmp)
370       (while lst
371         (setq tmp (car lst))
372         (if (cdr tmp)
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
376                                       (cdr tmp)
377                                       (car (car lst))
378                                       nil nil 'speedbar-tag-face
379                                       (1+ level))))
380         (setq lst (cdr lst))))))
381
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."
385   (let ((out nil))
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
389              ;; tokens from it.
390              (setq out (semanticdb-file-stream file)))
391         ;; Successful DB query.
392         nil
393       ;; No database, do it the old way.
394       (save-excursion
395         (set-buffer (find-file-noselect file))
396         (if (or (not (featurep 'semantic))
397                 (not semantic--parse-table))
398             (setq out t)
399           (if speedbar-power-click (semantic-clear-toplevel-cache))
400           (setq out (semantic-fetch-tags)))))
401     (if (listp out)
402         (condition-case nil
403             (progn
404               ;; This brings externally defind methods into
405               ;; their classes, and creates meta classes for
406               ;; orphans.
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)))
411           (error t))
412       t)))
413
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))
417
418 (provide 'semantic-sb)
419
420 ;;; semantic-sb.el ends here