]> code.delx.au - gnu-emacs/blob - lisp/speedbar.el
(Abbrevs): A @node line without explicit Prev, Next, and Up links.
[gnu-emacs] / lisp / speedbar.el
1 ;;; speedbar --- quick access to files and tags in a frame
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: file, tags, tools
8
9 (defvar speedbar-version "1.0"
10 "The current version of speedbar.")
11 (defvar speedbar-incompatible-version "0.14beta4"
12 "This version of speedbar is incompatible with this version.
13 Due to massive API changes (removing the use of the word PATH)
14 this version is not backward compatible to 0.14 or earlier.")
15
16 ;; This file is part of GNU Emacs.
17
18 ;; GNU Emacs is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; any later version.
22
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
27
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
31 ;; Boston, MA 02110-1301, USA.
32
33 ;;; Commentary:
34 ;;
35 ;; The speedbar provides a frame in which files, and locations in
36 ;; files are displayed. These items can be clicked on with mouse-2 in
37 ;; to display that file location.
38 ;;
39 ;;; Customizing and Developing for speedbar
40 ;;
41 ;; Please see the speedbar manual for informaion.
42 ;;
43 ;;; Notes:
44 ;;
45 ;; Users of really old emacsen without the need timer functions
46 ;; will not have speedbar updating automatically. Use "g" to refresh
47 ;; the display after changing directories. Remember, do not interrupt
48 ;; the stealthy updates or your display may not be completely
49 ;; refreshed.
50 ;;
51 ;; AUC-TEX users: The imenu tags for AUC-TEX mode don't work very
52 ;; well. Use the imenu keywords from tex-mode.el for better results.
53 ;;
54 ;; This file requires the library package assoc (association lists)
55 ;; assoc should be available in all modern versions of Emacs.
56 ;; The custom package is optional (for easy configuration of speedbar)
57 ;; http://www.dina.kvl.dk/~abraham/custom/
58 ;; custom is available in all versions of Emacs version 20 or better.
59 ;;
60 ;;; Developing for speedbar
61 ;;
62 ;; Adding a speedbar specialized display mode:
63 ;;
64 ;; Speedbar can be configured to create a special display for certain
65 ;; modes that do not display traditional file/tag data. Rmail, Info,
66 ;; and the debugger are examples. These modes can, however, benefit
67 ;; from a speedbar style display in their own way.
68 ;;
69 ;; If your `major-mode' is `foo-mode', the only requirement is to
70 ;; create a function called `foo-speedbar-buttons' which takes one
71 ;; argument, BUFFER. BUFFER will be the buffer speedbar wants filled.
72 ;; In `foo-speedbar-buttons' there are several functions that make
73 ;; building a speedbar display easy. See the documentation for
74 ;; `speedbar-with-writable' (needed because the buffer is usually
75 ;; read-only) `speedbar-make-tag-line', `speedbar-insert-button', and
76 ;; `speedbar-insert-generic-list'. If you use
77 ;; `speedbar-insert-generic-list', also read the doc for
78 ;; `speedbar-tag-hierarchy-method' in case you wish to override it.
79 ;; The macro `speedbar-with-attached-buffer' brings you back to the
80 ;; buffer speedbar is displaying for.
81 ;;
82 ;; For those functions that make buttons, the "function" should be a
83 ;; symbol that is the function to call when clicked on. The "token"
84 ;; is extra data you can pass along. The "function" must take three
85 ;; parameters. They are (TEXT TOKEN INDENT). TEXT is the text of the
86 ;; button clicked on. TOKEN is the data passed in when you create the
87 ;; button. INDENT is an indentation level, or 0. You can store
88 ;; indentation levels with `speedbar-make-tag-line' which creates a
89 ;; line with an expander (eg. [+]) and a text button.
90 ;;
91 ;; Some useful functions when writing expand functions, and click
92 ;; functions are `speedbar-change-expand-button-char',
93 ;; `speedbar-delete-subblock', and `speedbar-center-buffer-smartly'.
94 ;; The variable `speedbar-power-click' is set to t in your functions
95 ;; when the user shift-clicks. This is an indication of anything from
96 ;; refreshing cached data to making a buffer appear in a new frame.
97 ;;
98 ;; If you wish to add to the default speedbar menu for the case of
99 ;; `foo-mode', create a variable `foo-speedbar-menu-items'. This
100 ;; should be a list compatible with the `easymenu' package. It will
101 ;; be spliced into the main menu. (Available with click-mouse-3). If
102 ;; you wish to have extra key bindings in your special mode, create a
103 ;; variable `foo-speedbar-key-map'. Instead of using `make-keymap',
104 ;; or `make-sparse-keymap', use the function
105 ;; `speedbar-make-specialized-keymap'. This lets you inherit all of
106 ;; speedbar's default bindings with low overhead.
107 ;;
108 ;; Adding a speedbar top-level display mode:
109 ;;
110 ;; Unlike the specialized modes, there are no name requirements,
111 ;; however the methods for writing a button display, menu, and keymap
112 ;; are the same. Once you create these items, you can call the
113 ;; function `speedbar-add-expansion-list'. It takes one parameter
114 ;; which is a list element of the form (NAME MENU KEYMAP &rest
115 ;; BUTTON-FUNCTIONS). NAME is a string that will show up in the
116 ;; Displays menu item. MENU is a symbol containing the menu items to
117 ;; splice in. KEYMAP is a symbol holding the keymap to use, and
118 ;; BUTTON-FUNCTIONS are the function names to call, in order, to create
119 ;; the display.
120 ;; Another tweakable variable is `speedbar-stealthy-function-list'
121 ;; which is of the form (NAME &rest FUNCTION ...). NAME is the string
122 ;; name matching `speedbar-add-expansion-list'. (It does not need to
123 ;; exist.). This provides additional display info which might be
124 ;; time-consuming to calculate.
125 ;; Lastly, `speedbar-mode-functions-list' allows you to set special
126 ;; function overrides.
127
128 ;;; TODO:
129 ;; - Timeout directories we haven't visited in a while.
130
131 (require 'assoc)
132 (require 'easymenu)
133 (require 'dframe)
134 (require 'sb-image)
135
136 ;; customization stuff
137 (defgroup speedbar nil
138 "File and tag browser frame."
139 :group 'etags
140 :group 'tools
141 :group 'convenience
142 ; :version "20.3"
143 )
144
145 (defgroup speedbar-faces nil
146 "Faces used in speedbar."
147 :prefix "speedbar-"
148 :group 'speedbar
149 :group 'faces)
150
151 (defgroup speedbar-vc nil
152 "Version control display in speedbar."
153 :prefix "speedbar-"
154 :group 'speedbar)
155
156 ;;; Code:
157
158 ;; Note: `inversion-test' requires parts of the CEDET package that are
159 ;; not included with Emacs.
160 ;;
161 ;; (defun speedbar-require-version (major minor &optional beta)
162 ;; "Non-nil if this version of SPEEDBAR does not satisfy a specific version.
163 ;; Arguments can be:
164 ;;
165 ;; (MAJOR MINOR &optional BETA)
166 ;;
167 ;; Values MAJOR and MINOR must be integers. BETA can be an integer, or
168 ;; excluded if a released version is required.
169 ;;
170 ;; It is assumed that if the current version is newer than that specified,
171 ;; everything passes. Exceptions occur when known incompatibilities are
172 ;; introduced."
173 ;; (inversion-test 'speedbar
174 ;; (concat major "." minor
175 ;; (when beta (concat "beta" beta)))))
176
177 (defvar speedbar-initial-expansion-mode-alist
178 '(("buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
179 speedbar-buffer-buttons)
180 ("quick buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
181 speedbar-buffer-buttons-temp)
182 ;; Files last, means first in the Displays menu
183 ("files" speedbar-easymenu-definition-special speedbar-file-key-map
184 speedbar-directory-buttons speedbar-default-directory-list)
185 )
186 "List of named expansion elements for filling the speedbar frame.
187 These expansion lists are only valid for regular files. Special modes
188 still get to override this list on a mode-by-mode basis. This list of
189 lists is of the form (NAME MENU KEYMAP FN1 FN2 ...). NAME is a string
190 representing the types of things to be displayed. MENU is an easymenu
191 structure used when in this mode. KEYMAP is a local keymap to install
192 over the regular speedbar keymap. FN1 ... are functions that will be
193 called in order. These functions will always get the default
194 directory to use passed in as the first parameter, and a 0 as the
195 second parameter. The 0 indicates the uppermost indentation level.
196 They must assume that the cursor is at the position where they start
197 inserting buttons.")
198
199 (defvar speedbar-initial-expansion-list-name "files"
200 "A symbol name representing the expansion list to use.
201 The expansion list `speedbar-initial-expansion-mode-alist' contains
202 the names and associated functions to use for buttons in speedbar.")
203
204 (defvar speedbar-previously-used-expansion-list-name "files"
205 "Save the last expansion list method.
206 This is used for returning to a previous expansion list method when
207 the user is done with the current expansion list.")
208
209 (defvar speedbar-stealthy-function-list
210 '(("files"
211 speedbar-update-current-file
212 speedbar-check-read-only
213 speedbar-check-vc
214 speedbar-check-objects)
215 )
216 "List of functions to periodically call stealthily.
217 This list is of the form:
218 '( (\"NAME\" FUNCTION ...)
219 ...)
220 where NAME is the name of the major display mode these functions are
221 for, and the remaining elements FUNCTION are functions to call in order.
222 Each function must return nil if interrupted, or t if completed.
223 Stealthy functions which have a single operation should always return
224 t. Functions which take a long time should maintain a state (where
225 they are in their speedbar related calculations) and permit
226 interruption. See `speedbar-check-vc' as a good example.")
227
228 (defvar speedbar-mode-functions-list
229 '(("files" (speedbar-item-info . speedbar-files-item-info)
230 (speedbar-line-directory . speedbar-files-line-directory))
231 ("buffers" (speedbar-item-info . speedbar-buffers-item-info)
232 (speedbar-line-directory . speedbar-buffers-line-directory))
233 ("quick buffers" (speedbar-item-info . speedbar-buffers-item-info)
234 (speedbar-line-directory . speedbar-buffers-line-directory))
235 )
236 "List of function tables to use for different major display modes.
237 It is not necessary to define any functions for a specialized mode.
238 This just provides a simple way of adding lots of customizations.
239 Each sublist is of the form:
240 (\"NAME\" (FUNCTIONSYMBOL . REPLACEMENTFUNCTION) ...)
241 Where NAME is the name of the specialized mode. The rest of the list
242 is a set of dotted pairs of the form FUNCTIONSYMBOL, which is the name
243 of a function you would like to replace, and REPLACEMENTFUNCTION,
244 which is a function you can call instead. Not all functions can be
245 replaced this way. Replaceable functions must provide that
246 functionality individually.")
247
248 (defcustom speedbar-mode-specific-contents-flag t
249 "*Non-nil means speedbar will show special mode contents.
250 This permits some modes to create customized contents for the speedbar
251 frame."
252 :group 'speedbar
253 :type 'boolean)
254
255 (defcustom speedbar-query-confirmation-method 'all
256 "*Query control for file operations.
257 The 'always flag means to always query before file operations.
258 The 'none-but-delete flag means to not query before any file
259 operations, except before a file deletion."
260 :group 'speedbar
261 :type '(radio (const :tag "Always Query before some file operations."
262 all)
263 (const :tag "Never Query before file operations, except for deletions."
264 none-but-delete)
265 ;;;; (const :tag "Never Every Query."
266 ;;;; none)
267 ))
268
269 (defvar speedbar-special-mode-expansion-list nil
270 "Default function list for creating specialized button lists.
271 This list is set by modes that wish to have special speedbar displays.
272 The list is of function names. Each function is called with one
273 parameter BUFFER, the originating buffer. The current buffer is the
274 speedbar buffer.")
275
276 (defvar speedbar-special-mode-key-map nil
277 "Default keymap used when identifying a specialized display mode.
278 This keymap is local to each buffer that wants to define special keybindings
279 effective when its display is shown.")
280
281 (defcustom speedbar-before-visiting-file-hook '(push-mark)
282 "*Hooks run before speedbar visits a file in the selected frame.
283 The default buffer is the buffer in the selected window in the attached frame."
284 :group 'speedbar
285 :type 'hook)
286
287 (defcustom speedbar-visiting-file-hook nil
288 "*Hooks run when speedbar visits a file in the selected frame."
289 :group 'speedbar
290 :type 'hook)
291
292 (defcustom speedbar-before-visiting-tag-hook '(push-mark)
293 "*Hooks run before speedbar visits a tag in the selected frame.
294 The default buffer is the buffer in the selected window in the attached frame."
295 :group 'speedbar
296 :type 'hook)
297
298 (defcustom speedbar-visiting-tag-hook '(speedbar-highlight-one-tag-line)
299 "*Hooks run when speedbar visits a tag in the selected frame."
300 :group 'speedbar
301 :type 'hook
302 :options '(speedbar-highlight-one-tag-line
303 speedbar-recenter-to-top
304 speedbar-recenter
305 ))
306
307 (defcustom speedbar-load-hook nil
308 "*Hooks run when speedbar is loaded."
309 :group 'speedbar
310 :type 'hook)
311
312 (defcustom speedbar-reconfigure-keymaps-hook nil
313 "*Hooks run when the keymaps are regenerated."
314 :group 'speedbar
315 :type 'hook)
316
317 (defcustom speedbar-show-unknown-files nil
318 "*Non-nil show files we can't expand with a ? in the expand button.
319 nil means don't show the file in the list."
320 :group 'speedbar
321 :type 'boolean)
322
323 ;;; EVENTUALLY REMOVE THESE
324
325 ;; When I moved to a repeating timer, I had the horrible missfortune
326 ;; of loosing the ability for adaptive speed choice. This update
327 ;; speed currently causes long delays when it should have been turned off.
328 (defvar speedbar-update-speed dframe-update-speed
329 "*Obsoleted variable. Use `dframe-update-speed'.")
330
331 (defvar speedbar-navigating-speed dframe-update-speed
332 "*Obsoleted variable. Use `dframe-update-speed'.")
333 ;;; END REMOVE THESE
334
335 (defcustom speedbar-frame-parameters '((minibuffer . nil)
336 (width . 20)
337 (border-width . 0)
338 (menu-bar-lines . 0)
339 (tool-bar-lines . 0)
340 (unsplittable . t)
341 (left-fringe . 0)
342 )
343 "*Parameters to use when creating the speedbar frame in Emacs.
344 Any parameter supported by a frame may be added. The parameter `height'
345 will be initialized to the height of the frame speedbar is
346 attached to and added to this list before the new frame is initialized."
347 :group 'speedbar
348 :type '(repeat (cons :format "%v"
349 (symbol :tag "Parameter")
350 (sexp :tag "Value"))))
351
352 ;; These values by Hrvoje Niksic <hniksic@srce.hr>
353 (defcustom speedbar-frame-plist
354 '(minibuffer nil width 20 border-width 0
355 internal-border-width 0 unsplittable t
356 default-toolbar-visible-p nil has-modeline-p nil
357 menubar-visible-p nil
358 default-gutter-visible-p nil
359 )
360 "*Parameters to use when creating the speedbar frame in XEmacs.
361 Parameters not listed here which will be added automatically are
362 `height' which will be initialized to the height of the frame speedbar
363 is attached to."
364 :group 'speedbar
365 :type '(repeat (group :inline t
366 (symbol :tag "Property")
367 (sexp :tag "Value"))))
368
369 (defcustom speedbar-use-imenu-flag (fboundp 'imenu)
370 "*Non-nil means use imenu for file parsing. nil to use etags.
371 XEmacs prior to 20.4 doesn't support imenu, therefore the default is to
372 use etags instead. Etags support is not as robust as imenu support."
373 :tag "Use Imenu for tags"
374 :group 'speedbar
375 :type 'boolean)
376
377 (defvar speedbar-dynamic-tags-function-list
378 '((speedbar-fetch-dynamic-imenu . speedbar-insert-imenu-list)
379 (speedbar-fetch-dynamic-etags . speedbar-insert-etags-list))
380 "Set to a list of functions which will return and insert a list of tags.
381 Each element is of the form ( FETCH . INSERT ) where FETCH
382 is a function which takes one parameter (the file to tag) and returns a
383 list of tags. The tag list can be of any form as long as the
384 corresponding insert method can handle it. If it returns t, then an
385 error occurred, and the next fetch routine is tried.
386 INSERT is a function which takes an INDENTation level, and a LIST of
387 tags to insert. It will then create the speedbar buttons.")
388
389 (defcustom speedbar-use-tool-tips-flag (and (not (featurep 'xemacs))
390 (>= emacs-major-version 21))
391 "*Non-nil means to use tool tips if they are avaialble.
392 When tooltips are not available, mouse-tracking and minibuffer
393 display is used instead."
394 :group 'speedbar
395 :type 'boolean)
396
397 (defcustom speedbar-track-mouse-flag (not speedbar-use-tool-tips-flag)
398 "*Non-nil means to display info about the line under the mouse."
399 :group 'speedbar
400 :type 'boolean)
401
402 (defcustom speedbar-default-position 'left-right
403 "*Default position of the speedbar frame.
404 Possible values are 'left, 'right or 'left-right.
405 If value is 'left-right, the most suitable location is
406 determined automatically."
407 :group 'speedbar
408 :type '(radio (const :tag "Automatic" left-right)
409 (const :tag "Left" left)
410 (const :tag "Right" right)))
411
412 (defcustom speedbar-sort-tags nil
413 "*If non-nil, sort tags in the speedbar display. *Obsolete*.
414 Use `semantic-tag-hierarchy-method' instead."
415 :group 'speedbar
416 :type 'boolean)
417
418 (defcustom speedbar-tag-hierarchy-method
419 '(speedbar-prefix-group-tag-hierarchy
420 speedbar-trim-words-tag-hierarchy)
421 "*List of hooks which speedbar will use to organize tags into groups.
422 Groups are defined as expandable meta-tags. Imenu supports
423 such things in some languages, such as separating variables from
424 functions. Each hook takes one argument LST, and may destructively
425 create a new list of the same form. LST is a list of elements of the
426 form:
427 (ELT1 ELT2 ... ELTn)
428 where each ELT is of the form
429 (TAG-NAME-STRING . NUMBER-OR-MARKER)
430 or
431 (GROUP-NAME-STRING ELT1 ELT2... ELTn)"
432 :group 'speedbar
433 :type 'hook
434 :options '(speedbar-prefix-group-tag-hierarchy
435 speedbar-trim-words-tag-hierarchy
436 speedbar-simple-group-tag-hierarchy
437 speedbar-sort-tag-hierarchy)
438 )
439
440 (defcustom speedbar-tag-group-name-minimum-length 4
441 "*The minimum length of a prefix group name before expanding.
442 Thus, if the `speedbar-tag-hierarchy-method' includes `prefix-group'
443 and one such groups common characters is less than this number of
444 characters, then the group name will be changed to the form of:
445 worda to wordb
446 instead of just
447 word
448 This way we won't get silly looking listings."
449 :group 'speedbar
450 :type 'integer)
451
452 (defcustom speedbar-tag-split-minimum-length 20
453 "*Minimum length before we stop trying to create sub-lists in tags.
454 This is used by all tag-hierarchy methods that break large lists into
455 sub-lists."
456 :group 'speedbar
457 :type 'integer)
458
459 (defcustom speedbar-tag-regroup-maximum-length 10
460 "*Maximum length of submenus that are regrouped.
461 If the regrouping option is used, then if two or more short subgroups
462 are next to each other, then they are combined until this number of
463 items is reached."
464 :group 'speedbar
465 :type 'integer)
466
467 (defcustom speedbar-directory-button-trim-method 'span
468 "*Indicates how the directory button will be displayed.
469 Possible values are:
470 'span - span large directories over multiple lines.
471 'trim - trim large directories to only show the last few.
472 nil - no trimming."
473 :group 'speedbar
474 :type '(radio (const :tag "Span large directories over mutiple lines."
475 span)
476 (const :tag "Trim large directories to only show the last few."
477 trim)
478 (const :tag "No trimming." nil)))
479
480 (defcustom speedbar-smart-directory-expand-flag t
481 "*Non-nil means speedbar should use smart expansion.
482 Smart expansion only affects when speedbar wants to display a
483 directory for a file in the attached frame. When smart expansion is
484 enabled, new directories which are children of a displayed directory
485 are expanded in the current framework. If nil, then the current
486 hierarchy would be replaced with the new directory."
487 :group 'speedbar
488 :type 'boolean)
489
490 (defcustom speedbar-indentation-width 1
491 "*When sub-nodes are expanded, the number of spaces used for indentation."
492 :group 'speedbar
493 :type 'integer)
494
495 (defcustom speedbar-hide-button-brackets-flag nil
496 "*Non-nil means speedbar will hide the brackets around the + or -."
497 :group 'speedbar
498 :type 'boolean)
499
500 (defcustom speedbar-before-popup-hook nil
501 "*Hooks called before popping up the speedbar frame."
502 :group 'speedbar
503 :type 'hook)
504
505 (defcustom speedbar-after-create-hook '(speedbar-frame-reposition-smartly)
506 "*Hooks called after popping up the speedbar frame."
507 :group 'speedbar
508 :type 'hook)
509
510 (defcustom speedbar-before-delete-hook nil
511 "*Hooks called before deleting the speedbar frame."
512 :group 'speedbar
513 :type 'hook)
514
515 (defcustom speedbar-mode-hook nil
516 "*Hooks called after creating a speedbar buffer."
517 :group 'speedbar
518 :type 'hook)
519
520 (defcustom speedbar-timer-hook nil
521 "*Hooks called after running the speedbar timer function."
522 :group 'speedbar
523 :type 'hook)
524
525 (defcustom speedbar-verbosity-level 1
526 "*Verbosity level of the speedbar. 0 means say nothing.
527 1 means medium level verbosity. 2 and higher are higher levels of
528 verbosity."
529 :group 'speedbar
530 :type 'integer)
531
532 (defvar speedbar-indicator-separator " "
533 "String separating file text from indicator characters.")
534
535 (defcustom speedbar-vc-do-check t
536 "*Non-nil check all files in speedbar to see if they have been checked out.
537 Any file checked out is marked with `speedbar-vc-indicator'."
538 :group 'speedbar-vc
539 :type 'boolean)
540
541 (defvar speedbar-vc-indicator "*"
542 "Text used to mark files which are currently checked out.
543 Other version control systems can be added by examining the function
544 `speedbar-vc-directory-enable-hook' and `speedbar-vc-in-control-hook'.")
545
546 (defcustom speedbar-vc-directory-enable-hook nil
547 "*Return non-nil if the current directory should be checked for Version Control.
548 Functions in this hook must accept one parameter which is the directory
549 being checked."
550 :group 'speedbar-vc
551 :type 'hook)
552
553 (defcustom speedbar-vc-in-control-hook nil
554 "*Return non-nil if the specified file is under Version Control.
555 Functions in this hook must accept two parameters. The DIRECTORY of the
556 current file, and the FILENAME of the file being checked."
557 :group 'speedbar-vc
558 :type 'hook)
559
560 (defvar speedbar-vc-to-do-point nil
561 "Local variable maintaining the current version control check position.")
562
563 (defcustom speedbar-obj-do-check t
564 "*Non-nil check all files in speedbar to see if they have an object file.
565 Any file checked out is marked with `speedbar-obj-indicator', and the
566 marking is based on `speedbar-obj-alist'"
567 :group 'speedbar-vc
568 :type 'boolean)
569
570 (defvar speedbar-obj-to-do-point nil
571 "Local variable maintaining the current version control check position.")
572
573 (defvar speedbar-obj-indicator '("#" . "!")
574 "Text used to mark files that have a corresponding hidden object file.
575 The car is for an up-to-date object. The cdr is for an out of date object.
576 The expression `speedbar-obj-alist' defines who gets tagged.")
577
578 (defvar speedbar-obj-alist
579 '(("\\.\\([cpC]\\|cpp\\|cc\\|cxx\\)$" . ".o")
580 ("\\.el$" . ".elc")
581 ("\\.java$" . ".class")
582 ("\\.f\\(or\\|90\\|77\\)?$" . ".o")
583 ("\\.tex$" . ".dvi")
584 ("\\.texi$" . ".info"))
585 "Alist of file extensions, and their corresponding object file type.")
586
587 (defvar speedbar-ro-to-do-point nil
588 "Local variable maintaining the current read only check position.")
589
590 (defvar speedbar-object-read-only-indicator "%"
591 "Indicator to append onto a line if that item is Read Only.")
592
593 ;; Note: Look for addition place to add indicator lists that
594 ;; use skip-chars instead of a regular expression.
595 (defvar speedbar-indicator-regex
596 (concat (regexp-quote speedbar-indicator-separator)
597 "\\("
598 (regexp-quote speedbar-vc-indicator)
599 "\\|"
600 (regexp-quote (car speedbar-obj-indicator))
601 "\\|"
602 (regexp-quote (cdr speedbar-obj-indicator))
603 "\\|"
604 (regexp-quote speedbar-object-read-only-indicator)
605 "\\)*")
606 "Regular expression used when identifying files.
607 Permits stripping of indicator characters from a line.")
608
609 (defcustom speedbar-scanner-reset-hook nil
610 "*Hook called whenever generic scanners are reset.
611 Set this to implement your own scanning / rescan safe functions with
612 state data."
613 :group 'speedbar
614 :type 'hook)
615
616 (defvar speedbar-ignored-modes '(fundamental-mode)
617 "*List of major modes which speedbar will not switch directories for.")
618
619 (defun speedbar-extension-list-to-regex (extlist)
620 "Takes EXTLIST, a list of extensions and transforms it into regexp.
621 All the preceding `.' are stripped for an optimized expression starting
622 with `.' followed by extensions, followed by full-filenames."
623 (let ((regex1 nil) (regex2 nil))
624 (while extlist
625 (if (= (string-to-char (car extlist)) ?.)
626 (setq regex1 (concat regex1 (if regex1 "\\|" "")
627 (substring (car extlist) 1)))
628 (setq regex2 (concat regex2 (if regex2 "\\|" "") (car extlist))))
629 (setq extlist (cdr extlist)))
630 ;; concat all the sub-exressions together, making sure all types
631 ;; of parts exist during concatination.
632 (concat "\\("
633 (if regex1 (concat "\\(\\.\\(" regex1 "\\)\\)") "")
634 (if (and regex1 regex2) "\\|" "")
635 (if regex2 (concat "\\(" regex2 "\\)") "")
636 "\\)$")))
637
638 (defvar speedbar-ignored-directory-regexp nil
639 "Regular expression matching directorys speedbar will not switch to.
640 Created from `speedbar-ignored-directory-expressions' with the function
641 `speedbar-extension-list-to-regex' (A misnamed function in this case.)
642 Use the function `speedbar-add-ignored-directory-regexp', or customize the
643 variable `speedbar-ignored-directory-expressions' to modify this variable.")
644
645 (defcustom speedbar-ignored-directory-expressions
646 '("[/\\]logs?[/\\]\\'")
647 "*List of regular expressions matching directories speedbar will ignore.
648 They should included directorys to directories which are notoriously very
649 large and take a long time to load in. Use the function
650 `speedbar-add-ignored-directory-regexp' to add new items to this list after
651 speedbar is loaded. You may place anything you like in this list
652 before speedbar has been loaded."
653 :group 'speedbar
654 :type '(repeat (regexp :tag "Directory Regexp"))
655 :set (lambda (sym val)
656 (setq speedbar-ignored-directory-expressions val
657 speedbar-ignored-directory-regexp
658 (speedbar-extension-list-to-regex val))))
659
660 (defcustom speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\|\\..*\\)\\'"
661 "*Regular expression matching directories not to show in speedbar.
662 They should include commonly existing directories which are not
663 useful, such as version control."
664 :group 'speedbar
665 :type 'string)
666
667 (defvar speedbar-file-unshown-regexp
668 (let ((nstr "") (noext completion-ignored-extensions))
669 (while noext
670 (setq nstr (concat nstr (regexp-quote (car noext)) "\\'"
671 (if (cdr noext) "\\|" ""))
672 noext (cdr noext)))
673 ;; backup refdir lockfile
674 (concat nstr "\\|#[^#]+#$\\|\\.\\.?\\'\\|\\.#"))
675 "*Regexp matching files we don't want displayed in a speedbar buffer.
676 It is generated from the variable `completion-ignored-extensions'")
677
678 (defvar speedbar-file-regexp nil
679 "Regular expression matching files we know how to expand.
680 Created from `speedbar-supported-extension-expressions' with the
681 function `speedbar-extension-list-to-regex'")
682
683 ;; this is dangerous to customize, because the defaults will probably
684 ;; change in the future.
685 (defcustom speedbar-supported-extension-expressions
686 (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
687 ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".f\\(90\\|77\\|or\\)?")
688 (if speedbar-use-imenu-flag
689 '(".ada" ".p[lm]" ".tcl" ".m" ".scm" ".pm" ".py" ".g"
690 ;; html is not supported by default, but an imenu tags package
691 ;; is available. Also, html files are nice to be able to see.
692 ".s?html"
693 ".ma?k" "[Mm]akefile\\(\\.in\\)?")))
694 "*List of regular expressions which will match files supported by tagging.
695 Do not prefix the `.' char with a double \\ to quote it, as the period
696 will be stripped by a simplified optimizer when compiled into a
697 singular expression. This variable will be turned into
698 `speedbar-file-regexp' for use with speedbar. You should use the
699 function `speedbar-add-supported-extension' to add a new extension at
700 runtime, or use the configuration dialog to set it in your .emacs
701 file.
702 If you add an extension to this list, and it does not appear, you may
703 need to also modify `completion-ignored-extension' which will also help
704 file completion."
705 :group 'speedbar
706 :type '(repeat (regexp :tag "Extension Regexp"))
707 :set (lambda (sym val)
708 (set 'speedbar-supported-extension-expressions val)
709 (set 'speedbar-file-regexp (speedbar-extension-list-to-regex val))))
710
711 (setq speedbar-file-regexp
712 (speedbar-extension-list-to-regex speedbar-supported-extension-expressions))
713
714 (defun speedbar-add-supported-extension (extension)
715 "Add EXTENSION as a new supported extension for speedbar tagging.
716 This should start with a `.' if it is not a complete file name, and
717 the dot should NOT be quoted in with \\. Other regular expression
718 matchers are allowed however. EXTENSION may be a single string or a
719 list of strings."
720 (interactive "sExtension: ")
721 (if (not (listp extension)) (setq extension (list extension)))
722 (while extension
723 (if (member (car extension) speedbar-supported-extension-expressions)
724 nil
725 (setq speedbar-supported-extension-expressions
726 (cons (car extension) speedbar-supported-extension-expressions)))
727 (setq extension (cdr extension)))
728 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
729 speedbar-supported-extension-expressions)))
730
731 (defun speedbar-add-ignored-directory-regexp (directory-expression)
732 "Add DIRECTORY-EXPRESSION as a new ignored directory for speedbar tracking.
733 This function will modify `speedbar-ignored-directory-regexp' and add
734 DIRECTORY-EXPRESSION to `speedbar-ignored-directory-expressions'."
735 (interactive "sDirectory regex: ")
736 (if (not (listp directory-expression))
737 (setq directory-expression (list directory-expression)))
738 (while directory-expression
739 (if (member (car directory-expression) speedbar-ignored-directory-expressions)
740 nil
741 (setq speedbar-ignored-directory-expressions
742 (cons (car directory-expression) speedbar-ignored-directory-expressions)))
743 (setq directory-expression (cdr directory-expression)))
744 (setq speedbar-ignored-directory-regexp (speedbar-extension-list-to-regex
745 speedbar-ignored-directory-expressions)))
746
747 ;; If we don't have custom, then we set it here by hand.
748 (if (not (fboundp 'custom-declare-variable))
749 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
750 speedbar-supported-extension-expressions)
751 speedbar-ignored-directory-regexp (speedbar-extension-list-to-regex
752 speedbar-ignored-directory-expressions)))
753
754 (defvar speedbar-update-flag dframe-have-timer-flag
755 "*Non-nil means to automatically update the display.
756 When this is nil then speedbar will not follow the attached frame's directory.
757 When speedbar is active, use:
758
759 \\<speedbar-key-map> `\\[speedbar-toggle-updates]'
760
761 to toggle this value.")
762
763 (defvar speedbar-update-flag-disable nil
764 "Permanently disable changing of the update flag.")
765
766 (defvar speedbar-syntax-table nil
767 "Syntax-table used on the speedbar.")
768
769 (if speedbar-syntax-table
770 nil
771 (setq speedbar-syntax-table (make-syntax-table))
772 ;; turn off paren matching around here.
773 (modify-syntax-entry ?\' " " speedbar-syntax-table)
774 (modify-syntax-entry ?\" " " speedbar-syntax-table)
775 (modify-syntax-entry ?( " " speedbar-syntax-table)
776 (modify-syntax-entry ?) " " speedbar-syntax-table)
777 (modify-syntax-entry ?{ " " speedbar-syntax-table)
778 (modify-syntax-entry ?} " " speedbar-syntax-table)
779 (modify-syntax-entry ?[ " " speedbar-syntax-table)
780 (modify-syntax-entry ?] " " speedbar-syntax-table))
781
782 (defvar speedbar-key-map nil
783 "Keymap used in speedbar buffer.")
784
785 (if speedbar-key-map
786 nil
787 (setq speedbar-key-map (make-keymap))
788 (suppress-keymap speedbar-key-map t)
789
790 ;; control
791 (define-key speedbar-key-map "t" 'speedbar-toggle-updates)
792 (define-key speedbar-key-map "g" 'speedbar-refresh)
793
794 ;; navigation
795 (define-key speedbar-key-map "n" 'speedbar-next)
796 (define-key speedbar-key-map "p" 'speedbar-prev)
797 (define-key speedbar-key-map "\M-n" 'speedbar-restricted-next)
798 (define-key speedbar-key-map "\M-p" 'speedbar-restricted-prev)
799 (define-key speedbar-key-map "\C-\M-n" 'speedbar-forward-list)
800 (define-key speedbar-key-map "\C-\M-p" 'speedbar-backward-list)
801 ;; These commands never seemed useful.
802 ;; (define-key speedbar-key-map " " 'speedbar-scroll-up)
803 ;; (define-key speedbar-key-map [delete] 'speedbar-scroll-down)
804
805 ;; Short cuts I happen to find useful
806 (define-key speedbar-key-map "r"
807 (lambda () (interactive)
808 (speedbar-change-initial-expansion-list
809 speedbar-previously-used-expansion-list-name)))
810 (define-key speedbar-key-map "b"
811 (lambda () (interactive)
812 (speedbar-change-initial-expansion-list "quick buffers")))
813 (define-key speedbar-key-map "f"
814 (lambda () (interactive)
815 (speedbar-change-initial-expansion-list "files")))
816
817 (dframe-update-keymap speedbar-key-map)
818 )
819
820 (defun speedbar-make-specialized-keymap ()
821 "Create a keymap for use with a speedbar major or minor display mode.
822 This basically creates a sparse keymap, and makes it's parent be
823 `speedbar-key-map'."
824 (let ((k (make-sparse-keymap)))
825 (set-keymap-parent k speedbar-key-map)
826 k))
827
828 (defvar speedbar-file-key-map nil
829 "Keymap used in speedbar buffer while files are displayed.")
830
831 (if speedbar-file-key-map
832 nil
833 (setq speedbar-file-key-map (speedbar-make-specialized-keymap))
834
835 ;; Basic tree features
836 (define-key speedbar-file-key-map "e" 'speedbar-edit-line)
837 (define-key speedbar-file-key-map "\C-m" 'speedbar-edit-line)
838 (define-key speedbar-file-key-map "+" 'speedbar-expand-line)
839 (define-key speedbar-file-key-map "=" 'speedbar-expand-line)
840 (define-key speedbar-file-key-map "-" 'speedbar-contract-line)
841
842 (define-key speedbar-file-key-map "[" 'speedbar-expand-line-descendants)
843 (define-key speedbar-file-key-map "]" 'speedbar-contract-line-descendants)
844
845 (define-key speedbar-file-key-map " " 'speedbar-toggle-line-expansion)
846
847 ;; file based commands
848 (define-key speedbar-file-key-map "U" 'speedbar-up-directory)
849 (define-key speedbar-file-key-map "I" 'speedbar-item-info)
850 (define-key speedbar-file-key-map "B" 'speedbar-item-byte-compile)
851 (define-key speedbar-file-key-map "L" 'speedbar-item-load)
852 (define-key speedbar-file-key-map "C" 'speedbar-item-copy)
853 (define-key speedbar-file-key-map "D" 'speedbar-item-delete)
854 (define-key speedbar-file-key-map "O" 'speedbar-item-object-delete)
855 (define-key speedbar-file-key-map "R" 'speedbar-item-rename)
856 (define-key speedbar-file-key-map "M" 'speedbar-create-directory)
857 )
858
859 (defvar speedbar-easymenu-definition-base
860 (append
861 '("Speedbar"
862 ["Update" speedbar-refresh t]
863 ["Auto Update" speedbar-toggle-updates
864 :active (not speedbar-update-flag-disable)
865 :style toggle :selected speedbar-update-flag])
866 (if (and (or (fboundp 'defimage)
867 (fboundp 'make-image-specifier))
868 (if (fboundp 'display-graphic-p)
869 (display-graphic-p)
870 window-system))
871 (list
872 ["Use Images" speedbar-toggle-images
873 :style toggle :selected speedbar-use-images]))
874 )
875 "Base part of the speedbar menu.")
876
877 (defvar speedbar-easymenu-definition-special
878 '(["Edit Item On Line" speedbar-edit-line t]
879 ["Show All Files" speedbar-toggle-show-all-files
880 :style toggle :selected speedbar-show-unknown-files]
881 ["Expand File Tags" speedbar-expand-line
882 (save-excursion (beginning-of-line)
883 (looking-at "[0-9]+: *.\\+. "))]
884 ["Flush Cache & Expand" speedbar-flush-expand-line
885 (save-excursion (beginning-of-line)
886 (looking-at "[0-9]+: *.\\+. "))]
887 ["Expand All Descendants" speedbar-expand-line-descendants
888 (save-excursion (beginning-of-line)
889 (looking-at "[0-9]+: *.\\+. ")) ]
890 ["Contract File Tags" speedbar-contract-line
891 (save-excursion (beginning-of-line)
892 (looking-at "[0-9]+: *.-. "))]
893 ; ["Sort Tags" speedbar-toggle-sorting
894 ; :style toggle :selected speedbar-sort-tags]
895 "----"
896 ["File/Tag Information" speedbar-item-info t]
897 ["Load Lisp File" speedbar-item-load
898 (save-excursion
899 (beginning-of-line)
900 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
901 ["Byte Compile File" speedbar-item-byte-compile
902 (save-excursion
903 (beginning-of-line)
904 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
905 ["Copy File" speedbar-item-copy
906 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *\\["))]
907 ["Rename File" speedbar-item-rename
908 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
909 ["Create Directory" speedbar-create-directory
910 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
911 ["Delete File" speedbar-item-delete
912 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
913 ["Delete Object" speedbar-item-object-delete
914 (save-excursion (beginning-of-line)
915 (looking-at "[0-9]+: *\\[[+-]\\] [^ \n]+ \\*?[!#]$"))]
916 )
917 "Additional menu items while in file-mode.")
918
919 (defvar speedbar-easymenu-definition-trailer
920 (append
921 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
922 (list ["Customize..." speedbar-customize t]))
923 (list
924 ["Detach" speedbar-detach (and speedbar-frame
925 (eq (selected-frame) speedbar-frame)) ]
926 ["Close" dframe-close-frame t]
927 ["Quit" delete-frame t] ))
928 "Menu items appearing at the end of the speedbar menu.")
929
930 (defvar speedbar-desired-buffer nil
931 "Non-nil when speedbar is showing buttons specific to a special mode.
932 In this case it is the originating buffer.")
933 (defvar speedbar-buffer nil
934 "The buffer displaying the speedbar.")
935 (defvar speedbar-frame nil
936 "The frame displaying speedbar.")
937 (defvar speedbar-cached-frame nil
938 "The frame that was last created, then removed from the display.")
939 (defvar speedbar-full-text-cache nil
940 "The last open directory is saved in its entirety for ultra-fast switching.")
941
942 (defvar speedbar-last-selected-file nil
943 "The last file which was selected in speedbar buffer.")
944
945 (defvar speedbar-shown-directories nil
946 "Maintain list of directories simultaneously open in the current speedbar.")
947
948 (defvar speedbar-directory-contents-alist nil
949 "An association list of directories and their contents.
950 Each sublist was returned by `speedbar-file-lists'. This list is
951 maintained to speed up the refresh rate when switching between
952 directories.")
953
954 (defvar speedbar-power-click nil
955 "Never set this by hand. Value is t when S-mouse activity occurs.")
956
957 \f
958 ;;; Compatibility
959 ;;
960 (defalias 'speedbar-make-overlay
961 (if (featurep 'xemacs) 'make-extent 'make-overlay))
962
963 (defalias 'speedbar-overlay-put
964 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
965
966 (defalias 'speedbar-delete-overlay
967 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
968
969 (defalias 'speedbar-mode-line-update
970 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
971 \f
972 ;;; Mode definitions/ user commands
973 ;;
974
975 ;;;###autoload
976 (defalias 'speedbar 'speedbar-frame-mode)
977 ;;;###autoload
978 (defun speedbar-frame-mode (&optional arg)
979 "Enable or disable speedbar. Positive ARG means turn on, negative turn off.
980 nil means toggle. Once the speedbar frame is activated, a buffer in
981 `speedbar-mode' will be displayed. Currently, only one speedbar is
982 supported at a time.
983 `speedbar-before-popup-hook' is called before popping up the speedbar frame.
984 `speedbar-before-delete-hook' is called before the frame is deleted."
985 (interactive "P")
986 ;; Get the buffer to play with
987 (if (not (buffer-live-p speedbar-buffer))
988 (save-excursion
989 (setq speedbar-buffer (get-buffer-create " SPEEDBAR"))
990 (set-buffer speedbar-buffer)
991 (speedbar-mode)))
992 ;; Do the frame thing
993 (dframe-frame-mode arg
994 'speedbar-frame
995 'speedbar-cached-frame
996 'speedbar-buffer
997 "Speedbar"
998 #'speedbar-frame-mode
999 (if dframe-xemacsp
1000 (append speedbar-frame-plist
1001 ;; This is a hack to get speedbar to iconfiy
1002 ;; with the selected frame.
1003 (list 'parent (selected-frame)))
1004 speedbar-frame-parameters)
1005 speedbar-before-delete-hook
1006 speedbar-before-popup-hook
1007 speedbar-after-create-hook)
1008 ;; Start up the timer
1009 (if (not speedbar-frame)
1010 (speedbar-set-timer nil)
1011 (speedbar-reconfigure-keymaps)
1012 (speedbar-update-contents)
1013 (speedbar-set-timer dframe-update-speed)
1014 )
1015 ;; Frame modifications
1016 (set (make-local-variable 'dframe-delete-frame-function)
1017 'speedbar-handle-delete-frame)
1018 ;; hscroll
1019 (set (make-local-variable 'automatic-hscrolling) nil) ; Emacs 21
1020 ;; reset the selection variable
1021 (setq speedbar-last-selected-file nil))
1022
1023 (defun speedbar-frame-reposition-smartly ()
1024 "Reposition the speedbar frame to be next to the attached frame."
1025 (cond ((and dframe-xemacsp
1026 (or (member 'left speedbar-frame-plist)
1027 (member 'top speedbar-frame-plist)))
1028 (dframe-reposition-frame
1029 speedbar-frame
1030 (dframe-attached-frame speedbar-frame)
1031 (cons (car (cdr (member 'left speedbar-frame-plist)))
1032 (car (cdr (member 'top speedbar-frame-plist)))))
1033 )
1034 ((and (not dframe-xemacsp)
1035 (or (assoc 'left speedbar-frame-parameters)
1036 (assoc 'top speedbar-frame-parameters)))
1037 ;; if left/top were specified in the parameters, pass them
1038 ;; down to the reposition function
1039 (dframe-reposition-frame
1040 speedbar-frame
1041 (dframe-attached-frame speedbar-frame)
1042 (cons (cdr (assoc 'left speedbar-frame-parameters))
1043 (cdr (assoc 'top speedbar-frame-parameters))))
1044 )
1045 (t
1046 (dframe-reposition-frame speedbar-frame
1047 (dframe-attached-frame speedbar-frame)
1048 speedbar-default-position))))
1049
1050 (defun speedbar-detach ()
1051 "Detach the current Speedbar from auto-updating.
1052 Doing this allows the creation of a second speedbar."
1053 (interactive)
1054 (let ((buffer speedbar-buffer))
1055 (dframe-detach 'speedbar-frame 'speedbar-cached-frame 'speedbar-buffer)
1056 (save-excursion
1057 (set-buffer buffer)
1058 ;; Permanently disable auto-updating in this speedbar buffer.
1059 (set (make-local-variable 'speedbar-update-flag) nil)
1060 (set (make-local-variable 'speedbar-update-flag-disable) t)
1061 ;; Make local copies of all the different variables to prevent
1062 ;; funny stuff later...
1063 )))
1064
1065 (defsubst speedbar-current-frame ()
1066 "Return the frame to use for speedbar based on current context."
1067 (dframe-current-frame 'speedbar-frame 'speedbar-mode))
1068
1069 (defun speedbar-handle-delete-frame (e)
1070 "Handle a delete frame event E.
1071 If the deleted frame is the frame SPEEDBAR is attached to,
1072 we need to delete speedbar also."
1073 (let ((frame-to-be-deleted (car (car (cdr e)))))
1074 (if (eq frame-to-be-deleted dframe-attached-frame)
1075 (delete-frame speedbar-frame)))
1076 )
1077
1078 ;;;###autoload
1079 (defun speedbar-get-focus ()
1080 "Change frame focus to or from the speedbar frame.
1081 If the selected frame is not speedbar, then speedbar frame is
1082 selected. If the speedbar frame is active, then select the attached frame."
1083 (interactive)
1084 (speedbar-reset-scanners)
1085 (dframe-get-focus 'speedbar-frame 'speedbar-frame-mode
1086 (lambda () (let ((speedbar-update-flag t))
1087 (speedbar-timer-fn)))))
1088
1089 (defsubst speedbar-frame-width ()
1090 "Return the width of the speedbar frame in characters.
1091 nil if it doesn't exist."
1092 (frame-width speedbar-frame))
1093
1094 (defun speedbar-mode ()
1095 "Major mode for managing a display of directories and tags.
1096 \\<speedbar-key-map>
1097 The first line represents the default directory of the speedbar frame.
1098 Each directory segment is a button which jumps speedbar's default
1099 directory to that directory. Buttons are activated by clicking `\\[speedbar-click]'.
1100 In some situations using `\\[dframe-power-click]' is a `power click' which will
1101 rescan cached items, or pop up new frames.
1102
1103 Each line starting with <+> represents a directory. Click on the <+>
1104 to insert the directory listing into the current tree. Click on the
1105 <-> to retract that list. Click on the directory name to go to that
1106 directory as the default.
1107
1108 Each line starting with [+] is a file. If the variable
1109 `speedbar-show-unknown-files' is t, the lines starting with [?] are
1110 files which don't have imenu support, but are not expressly ignored.
1111 Files are completely ignored if they match `speedbar-file-unshown-regexp'
1112 which is generated from `completion-ignored-extensions'.
1113
1114 Files with a `*' character after their name are files checked out of a
1115 version control system. (Currently only RCS is supported.) New
1116 version control systems can be added by examining the documentation
1117 for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'.
1118
1119 Files with a `#' or `!' character after them are source files that
1120 have an object file associated with them. The `!' indicates that the
1121 files is out of date. You can control what source/object associations
1122 exist through the variable `speedbar-obj-alist'.
1123
1124 Click on the [+] to display a list of tags from that file. Click on
1125 the [-] to retract the list. Click on the file name to edit the file
1126 in the attached frame.
1127
1128 If you open tags, you might find a node starting with {+}, which is a
1129 category of tags. Click the {+} to expand the category. Jump-able
1130 tags start with >. Click the name of the tag to go to that position
1131 in the selected file.
1132
1133 \\{speedbar-key-map}"
1134 ;; NOT interactive
1135 (save-excursion
1136 (kill-all-local-variables)
1137 (setq major-mode 'speedbar-mode)
1138 (setq mode-name "Speedbar")
1139 (set-syntax-table speedbar-syntax-table)
1140 (setq font-lock-keywords nil) ;; no font-locking please
1141 (setq truncate-lines t)
1142 (make-local-variable 'frame-title-format)
1143 (setq frame-title-format (concat "Speedbar " speedbar-version))
1144 (setq case-fold-search nil)
1145 (toggle-read-only 1)
1146 (speedbar-set-mode-line-format)
1147 ;; Add in our dframe hooks.
1148 (if speedbar-track-mouse-flag
1149 (setq dframe-track-mouse-function #'speedbar-track-mouse))
1150 (setq dframe-help-echo-function #'speedbar-item-info
1151 dframe-mouse-click-function #'speedbar-click
1152 dframe-mouse-position-function #'speedbar-position-cursor-on-line)
1153 (run-hooks 'speedbar-mode-hook))
1154 speedbar-buffer)
1155
1156 (defmacro speedbar-message (fmt &rest args)
1157 "Like message, but for use in the speedbar frame.
1158 Argument FMT is the format string, and ARGS are the arguments for message."
1159 `(dframe-message ,fmt ,@args))
1160
1161 (defsubst speedbar-y-or-n-p (prompt &optional deleting)
1162 "Like `y-or-n-p', but for use in the speedbar frame.
1163 Argument PROMPT is the prompt to use.
1164 Optional argument DELETING means this is a query that will delete something.
1165 The variable `speedbar-query-confirmation-method' can cause this to
1166 return true without a query."
1167 (or (and (not deleting)
1168 (eq speedbar-query-confirmation-method 'none-but-delete))
1169 (dframe-y-or-n-p prompt)))
1170
1171 (defsubst speedbar-select-attached-frame ()
1172 "Select the frame attached to this speedbar."
1173 (dframe-select-attached-frame (speedbar-current-frame)))
1174
1175 ;; Backwards compatibility
1176 (defalias 'speedbar-with-attached-buffer 'dframe-with-attached-buffer)
1177 (defalias 'speedbar-maybee-jump-to-attached-frame 'dframe-maybee-jump-to-attached-frame)
1178
1179 (defun speedbar-set-mode-line-format ()
1180 "Set the format of the mode line based on the current speedbar environment.
1181 This gives visual indications of what is up. It EXPECTS the speedbar
1182 frame and window to be the currently active frame and window."
1183 (if (and (frame-live-p (speedbar-current-frame))
1184 (or (not dframe-xemacsp)
1185 (with-no-warnings
1186 (specifier-instance has-modeline-p)))
1187 speedbar-buffer)
1188 (save-excursion
1189 (set-buffer speedbar-buffer)
1190 (let* ((w (or (speedbar-frame-width) 20))
1191 (p1 "<<")
1192 (p5 ">>")
1193 (p3 (if speedbar-update-flag "#" "!"))
1194 (p35 (capitalize speedbar-initial-expansion-list-name))
1195 (blank (- w (length p1) (length p3) (length p5) (length p35)
1196 (if line-number-mode 5 1)))
1197 (p2 (if (> blank 0)
1198 (make-string (/ blank 2) ? )
1199 ""))
1200 (p4 (if (> blank 0)
1201 (make-string (+ (/ blank 2) (% blank 2)) ? )
1202 ""))
1203 (tf
1204 (if line-number-mode
1205 (list (concat p1 p2 p3 " " p35) '(line-number-mode " %3l")
1206 (concat p4 p5))
1207 (list (concat p1 p2 p3 p4 p5)))))
1208 (if (not (equal mode-line-format tf))
1209 (progn
1210 (setq mode-line-format tf)
1211 (speedbar-mode-line-update)))))))
1212
1213 (defvar speedbar-previous-menu nil
1214 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1215
1216 (defun speedbar-reconfigure-keymaps ()
1217 "Reconfigure the menu-bar in a speedbar frame.
1218 Different menu items are displayed depending on the current display mode
1219 and the existence of packages."
1220 (let ((md (append
1221 speedbar-easymenu-definition-base
1222 (if speedbar-shown-directories
1223 ;; file display mode version
1224 (speedbar-initial-menu)
1225 (save-excursion
1226 (dframe-select-attached-frame speedbar-frame)
1227 (if (local-variable-p
1228 'speedbar-easymenu-definition-special
1229 (current-buffer))
1230 ;; If bound locally, we can use it
1231 speedbar-easymenu-definition-special)))
1232 ;; Dynamic menu stuff
1233 '("-")
1234 (list (cons "Displays"
1235 (let ((displays nil)
1236 (alist speedbar-initial-expansion-mode-alist))
1237 (while alist
1238 (setq displays
1239 (cons
1240 (vector
1241 (capitalize (car (car alist)))
1242 (list
1243 'speedbar-change-initial-expansion-list
1244 (car (car alist)))
1245 :style 'radio
1246 :selected
1247 `(string= ,(car (car alist))
1248 speedbar-initial-expansion-list-name)
1249 )
1250 displays))
1251 (setq alist (cdr alist)))
1252 displays)))
1253 ;; The trailer
1254 speedbar-easymenu-definition-trailer))
1255 (localmap (save-excursion
1256 (let ((cf (selected-frame)))
1257 (prog2
1258 (dframe-select-attached-frame speedbar-frame)
1259 (if (local-variable-p
1260 'speedbar-special-mode-key-map
1261 (current-buffer))
1262 speedbar-special-mode-key-map)
1263 (select-frame cf))))))
1264 (save-excursion
1265 (set-buffer speedbar-buffer)
1266 (use-local-map (or localmap
1267 (speedbar-initial-keymap)
1268 ;; This creates a small keymap we can glom the
1269 ;; menu adjustments into.
1270 (speedbar-make-specialized-keymap)))
1271 ;; Delete the old menu if applicable.
1272 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1273 (setq speedbar-previous-menu md)
1274 ;; Now add the new menu
1275 (if (not dframe-xemacsp)
1276 (easy-menu-define speedbar-menu-map (current-local-map)
1277 "Speedbar menu" md)
1278 (easy-menu-add md (current-local-map))
1279 ;; XEmacs-specific:
1280 (if (fboundp 'set-buffer-menubar)
1281 (set-buffer-menubar (list md)))))
1282
1283 (run-hooks 'speedbar-reconfigure-keymaps-hook)))
1284
1285 \f
1286 ;;; User Input stuff
1287 ;;
1288 (defun speedbar-customize ()
1289 "Customize speedbar using the Custom package."
1290 (interactive)
1291 (let ((sf (selected-frame)))
1292 (dframe-select-attached-frame speedbar-frame)
1293 (customize-group 'speedbar)
1294 (select-frame sf))
1295 (dframe-maybee-jump-to-attached-frame))
1296
1297 (defun speedbar-track-mouse (event)
1298 "For motion EVENT, display info about the current line."
1299 (if (not speedbar-track-mouse-flag)
1300 nil
1301 (save-excursion
1302 (save-window-excursion
1303 (condition-case nil
1304 (progn
1305 (mouse-set-point event)
1306 (if (eq major-mode 'speedbar-mode)
1307 ;; XEmacs may let us get in here in other mode buffers.
1308 (speedbar-item-info)))
1309 (t (speedbar-message nil)))))))
1310
1311 (defun speedbar-show-info-under-mouse ()
1312 "Call the info function for the line under the mouse.
1313 Optional EVENT is currently not used."
1314 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1315 (if (equal (car pos) speedbar-frame)
1316 (save-excursion
1317 (save-window-excursion
1318 (apply 'set-mouse-position pos)
1319 (speedbar-item-info))))))
1320
1321 (defun speedbar-next (arg)
1322 "Move to the next ARGth line in a speedbar buffer."
1323 (interactive "p")
1324 (forward-line (or arg 1))
1325 (speedbar-item-info)
1326 (speedbar-position-cursor-on-line))
1327
1328 (defun speedbar-prev (arg)
1329 "Move to the previous ARGth line in a speedbar buffer."
1330 (interactive "p")
1331 (speedbar-next (if arg (- arg) -1)))
1332
1333 (defun speedbar-restricted-move (arg)
1334 "Move to the next ARGth line in a speedbar buffer at the same depth.
1335 This means that movement is restricted to a subnode, and that siblings
1336 of intermediate nodes are skipped."
1337 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1338 ;; First find the extent for which we are allowed to move.
1339 (let ((depth (save-excursion (beginning-of-line)
1340 (if (looking-at "[0-9]+:")
1341 (string-to-number (match-string 0))
1342 0)))
1343 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1344 (lastmatch (point)))
1345 (while (/= arg 0)
1346 (forward-line (- crement))
1347 (let ((subdepth (save-excursion (beginning-of-line)
1348 (if (looking-at "[0-9]+:")
1349 (string-to-number (match-string 0))
1350 0))))
1351 (cond ((or (< subdepth depth)
1352 (progn (end-of-line) (eobp))
1353 (progn (beginning-of-line) (bobp)))
1354 ;; We have reached the end of this block.
1355 (goto-char lastmatch)
1356 (setq arg 0)
1357 (error "End of sub-list"))
1358 ((= subdepth depth)
1359 (setq lastmatch (point)
1360 arg (+ arg crement))))))
1361 (speedbar-position-cursor-on-line)))
1362
1363 (defun speedbar-restricted-next (arg)
1364 "Move to the next ARGth line in a speedbar buffer at the same depth.
1365 This means that movement is restricted to a subnode, and that siblings
1366 of intermediate nodes are skipped."
1367 (interactive "p")
1368 (speedbar-restricted-move (or arg 1))
1369 (speedbar-item-info))
1370
1371 (defun speedbar-restricted-prev (arg)
1372 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1373 This means that movement is restricted to a subnode, and that siblings
1374 of intermediate nodes are skipped."
1375 (interactive "p")
1376 (speedbar-restricted-move (if arg (- arg) -1))
1377 (speedbar-item-info))
1378
1379 (defun speedbar-navigate-list (arg)
1380 "Move across ARG groups of similarly typed items in speedbar.
1381 Stop on the first line of the next type of item, or on the last or first item
1382 if we reach a buffer boundary."
1383 (interactive "p")
1384 (beginning-of-line)
1385 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1386 (let ((str (regexp-quote (match-string 0))))
1387 (while (looking-at str)
1388 (speedbar-restricted-move arg)
1389 (beginning-of-line))))
1390 (speedbar-position-cursor-on-line))
1391
1392 (defun speedbar-forward-list ()
1393 "Move forward over the current list.
1394 A LIST in speedbar is a group of similarly typed items, such as directories,
1395 files, or the directory button."
1396 (interactive)
1397 (speedbar-navigate-list 1)
1398 (speedbar-item-info))
1399
1400 (defun speedbar-backward-list ()
1401 "Move backward over the current list.
1402 A LIST in speedbar is a group of similarly typed items, such as directories,
1403 files, or the directory button."
1404 (interactive)
1405 (speedbar-navigate-list -1)
1406 (speedbar-item-info))
1407
1408 (defun speedbar-scroll-up (&optional arg)
1409 "Page down one screen-full of the speedbar, or ARG lines."
1410 (interactive "P")
1411 (scroll-up arg)
1412 (speedbar-position-cursor-on-line))
1413
1414 (defun speedbar-scroll-down (&optional arg)
1415 "Page up one screen-full of the speedbar, or ARG lines."
1416 (interactive "P")
1417 (scroll-down arg)
1418 (speedbar-position-cursor-on-line))
1419
1420 (defun speedbar-up-directory ()
1421 "Keyboard accelerator for moving the default directory up one.
1422 Assumes that the current buffer is the speedbar buffer."
1423 (interactive)
1424 (setq default-directory (expand-file-name (concat default-directory "../")))
1425 (speedbar-update-contents))
1426 \f
1427 ;;; Speedbar file activity (aka creeping featurism)
1428 ;;
1429 (defun speedbar-refresh (&optional arg)
1430 "Refresh the current speedbar display, disposing of any cached data.
1431 Argument ARG represents to force a refresh past any caches that may exist."
1432 (interactive "P")
1433 (let ((dl speedbar-shown-directories)
1434 (dframe-power-click arg)
1435 deactivate-mark)
1436 ;; We need to hack something so this works in detached frames.
1437 (while dl
1438 (adelete 'speedbar-directory-contents-alist (car dl))
1439 (setq dl (cdr dl)))
1440 (if (<= 1 speedbar-verbosity-level)
1441 (speedbar-message "Refreshing speedbar..."))
1442 (speedbar-update-contents)
1443 (speedbar-stealthy-updates)
1444 ;; Reset the timer in case it got really hosed for some reason...
1445 (speedbar-set-timer dframe-update-speed)
1446 (if (<= 1 speedbar-verbosity-level)
1447 (speedbar-message "Refreshing speedbar...done"))))
1448
1449 (defun speedbar-item-load ()
1450 "Load the item under the cursor or mouse if it is a Lisp file."
1451 (interactive)
1452 (let ((f (speedbar-line-file)))
1453 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1454 (if (and (file-exists-p (concat f "c"))
1455 (speedbar-y-or-n-p (format "Load %sc? " f)))
1456 ;; If the compiled version exists, load that instead...
1457 (load-file (concat f "c"))
1458 (load-file f))
1459 (error "Not a loadable file"))))
1460
1461 (defun speedbar-item-byte-compile ()
1462 "Byte compile the item under the cursor or mouse if it is a Lisp file."
1463 (interactive)
1464 (let ((f (speedbar-line-file))
1465 (sf (selected-frame)))
1466 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1467 (progn
1468 (dframe-select-attached-frame speedbar-frame)
1469 (byte-compile-file f nil)
1470 (select-frame sf)
1471 (speedbar-reset-scanners)))
1472 ))
1473
1474 (defun speedbar-mouse-item-info (event)
1475 "Provide information about what the user clicked on.
1476 This should be bound to a mouse EVENT."
1477 (interactive "e")
1478 (mouse-set-point event)
1479 (speedbar-item-info))
1480
1481 (defun speedbar-generic-item-info ()
1482 "Attempt to derive, and then display information about this line item.
1483 File style information is displayed with `speedbar-item-info'."
1484 (save-excursion
1485 (beginning-of-line)
1486 ;; Skip invisible number info.
1487 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1488 ;; Skip items in "folder" type text characters.
1489 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1490 ;; Get the text
1491 (speedbar-message "Text: %s" (buffer-substring-no-properties
1492 (point) (progn (end-of-line) (point))))))
1493
1494 (defun speedbar-item-info ()
1495 "Display info in the mini-buffer about the button the mouse is over.
1496 This function can be replaced in `speedbar-mode-functions-list' as
1497 `speedbar-item-info'."
1498 (interactive)
1499 (let (message-log-max)
1500 (funcall (or (speedbar-fetch-replacement-function 'speedbar-item-info)
1501 'speedbar-generic-item-info))))
1502
1503 (defun speedbar-item-info-file-helper (&optional filename)
1504 "Display info about a file that is on the current line.
1505 nil if not applicable. If FILENAME, then use that instead of reading
1506 it from the speedbar buffer."
1507 (let* ((item (or filename (speedbar-line-file)))
1508 (attr (if item (file-attributes item) nil)))
1509 (if (and item attr) (speedbar-message "%s %-6d %s" (nth 8 attr)
1510 (nth 7 attr) item)
1511 nil)))
1512
1513 (defun speedbar-item-info-tag-helper ()
1514 "Display info about a tag that is on the current line.
1515 nil if not applicable."
1516 (save-excursion
1517 (beginning-of-line)
1518 (if (re-search-forward " [-+=]?> \\([^\n]+\\)"
1519 (save-excursion(end-of-line)(point)) t)
1520 (let* ((tag (match-string 1))
1521 (attr (speedbar-line-token))
1522 (item nil)
1523 (semantic-tagged (if (fboundp 'semantic-tag-p)
1524 (semantic-tag-p attr))))
1525 (if semantic-tagged
1526 (with-no-warnings
1527 (save-excursion
1528 (when (and (semantic-tag-overlay attr)
1529 (semantic-tag-buffer attr))
1530 (set-buffer (semantic-tag-buffer attr)))
1531 (speedbar-message
1532 (funcall semantic-sb-info-format-tag-function attr)
1533 )))
1534 (looking-at "\\([0-9]+\\):")
1535 (setq item (file-name-nondirectory (speedbar-line-directory)))
1536 (speedbar-message "Tag: %s in %s" tag item)))
1537 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1538 (save-excursion(end-of-line)(point)) t)
1539 (speedbar-message "Group of tags \"%s\"" (match-string 1))
1540 (if (re-search-forward " [+-]?[()|@] \\([^\n]+\\)$" nil t)
1541 (let* ((detailtext (match-string 1))
1542 (detail (or (speedbar-line-token) detailtext))
1543 (parent (save-excursion
1544 (beginning-of-line)
1545 (let ((dep (if (looking-at "[0-9]+:")
1546 (1- (string-to-number (match-string 0)))
1547 0)))
1548 (re-search-backward (concat "^"
1549 (int-to-string dep)
1550 ":")
1551 nil t))
1552 (if (looking-at "[0-9]+: +[-+=>]> \\([^\n]+\\)$")
1553 (speedbar-line-token)
1554 nil))))
1555 (if (featurep 'semantic)
1556 (with-no-warnings
1557 (if (semantic-tag-p detail)
1558 (speedbar-message
1559 (funcall semantic-sb-info-format-tag-function detail parent))
1560 (if parent
1561 (speedbar-message "Detail: %s of tag %s" detail
1562 (if (semantic-tag-p parent)
1563 (semantic-format-tag-name parent nil t)
1564 parent))
1565 (speedbar-message "Detail: %s" detail))))
1566 ;; Not using `semantic':
1567 (if parent
1568 (speedbar-message "Detail: %s of tag %s" detail parent)
1569 (speedbar-message "Detail: %s" detail))))
1570 nil)))))
1571
1572 (defun speedbar-files-item-info ()
1573 "Display info in the mini-buffer about the button the mouse is over."
1574 (if (not speedbar-shown-directories)
1575 (speedbar-generic-item-info)
1576 (or (speedbar-item-info-file-helper)
1577 (speedbar-item-info-tag-helper)
1578 (speedbar-generic-item-info))))
1579
1580 (defun speedbar-item-copy ()
1581 "Copy the item under the cursor.
1582 Files can be copied to new names or places."
1583 (interactive)
1584 (let ((f (speedbar-line-file)))
1585 (if (not f) (error "Not a file"))
1586 (if (file-directory-p f)
1587 (error "Cannot copy directory")
1588 (let* ((rt (read-file-name (format "Copy %s to: "
1589 (file-name-nondirectory f))
1590 (file-name-directory f)))
1591 (refresh (member (expand-file-name (file-name-directory rt))
1592 speedbar-shown-directories)))
1593 ;; Create the right file name part
1594 (if (file-directory-p rt)
1595 (setq rt
1596 (concat (expand-file-name rt)
1597 (if (string-match "[/\\]$" rt) "" "/")
1598 (file-name-nondirectory f))))
1599 (if (or (not (file-exists-p rt))
1600 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)
1601 t))
1602 (progn
1603 (copy-file f rt t t)
1604 ;; refresh display if the new place is currently displayed.
1605 (if refresh
1606 (progn
1607 (speedbar-refresh)
1608 (if (not (speedbar-goto-this-file rt))
1609 (speedbar-goto-this-file f))))
1610 ))))))
1611
1612 (defun speedbar-item-rename ()
1613 "Rename the item under the cursor or mouse.
1614 Files can be renamed to new names or moved to new directories."
1615 (interactive)
1616 (let ((f (speedbar-line-file)))
1617 (if f
1618 (let* ((rt (read-file-name (format "Rename %s to: "
1619 (file-name-nondirectory f))
1620 (file-name-directory f)))
1621 (refresh (member (expand-file-name (file-name-directory rt))
1622 speedbar-shown-directories)))
1623 ;; Create the right file name part
1624 (if (file-directory-p rt)
1625 (setq rt
1626 (concat (expand-file-name rt)
1627 (if (string-match "[/\\]\\'" rt) "" "/")
1628 (file-name-nondirectory f))))
1629 (if (or (not (file-exists-p rt))
1630 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)
1631 t))
1632 (progn
1633 (rename-file f rt t)
1634 ;; refresh display if the new place is currently displayed.
1635 (if refresh
1636 (progn
1637 (speedbar-refresh)
1638 (speedbar-goto-this-file rt)
1639 )))))
1640 (error "Not a file"))))
1641
1642 (defun speedbar-create-directory ()
1643 "Create a directory in speedbar."
1644 (interactive)
1645 (let ((f (speedbar-line-file)))
1646 (if f
1647 (let* ((basedir (file-name-directory f))
1648 (nd (read-file-name "Create directory: "
1649 basedir)))
1650 ;; Make the directory
1651 (make-directory nd t)
1652 (speedbar-refresh)
1653 (speedbar-goto-this-file nd)
1654 )
1655 (error "Not a file"))))
1656
1657 (defun speedbar-item-delete ()
1658 "Delete the item under the cursor. Files are removed from disk."
1659 (interactive)
1660 (let ((f (speedbar-line-file)))
1661 (if (not f) (error "Not a file"))
1662 (if (speedbar-y-or-n-p (format "Delete %s? " f) t)
1663 (progn
1664 (if (file-directory-p f)
1665 (delete-directory f)
1666 (delete-file f))
1667 (speedbar-message "Okie dokie..")
1668 (let ((p (point)))
1669 (speedbar-refresh)
1670 (goto-char p))
1671 ))
1672 ))
1673
1674 (defun speedbar-item-object-delete ()
1675 "Delete the object associated from the item under the cursor.
1676 The file is removed from disk. The object is determined from the
1677 variable `speedbar-obj-alist'."
1678 (interactive)
1679 (let* ((f (speedbar-line-file))
1680 (obj nil)
1681 (oa speedbar-obj-alist))
1682 (if (not f) (error "Not a file"))
1683 (while (and oa (not (string-match (car (car oa)) f)))
1684 (setq oa (cdr oa)))
1685 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1686 (if (and oa (file-exists-p obj)
1687 (speedbar-y-or-n-p (format "Delete %s? " obj) t))
1688 (progn
1689 (delete-file obj)
1690 (speedbar-reset-scanners)))))
1691
1692 (defun speedbar-enable-update ()
1693 "Enable automatic updating in speedbar via timers."
1694 (interactive)
1695 (setq speedbar-update-flag t)
1696 (speedbar-set-mode-line-format)
1697 (speedbar-set-timer dframe-update-speed))
1698
1699 (defun speedbar-disable-update ()
1700 "Disable automatic updating and stop consuming resources."
1701 (interactive)
1702 (setq speedbar-update-flag nil)
1703 (speedbar-set-mode-line-format)
1704 (speedbar-set-timer nil))
1705
1706 (defun speedbar-toggle-updates ()
1707 "Toggle automatic update for the speedbar frame."
1708 (interactive)
1709 (if speedbar-update-flag
1710 (speedbar-disable-update)
1711 (speedbar-enable-update)))
1712
1713 (defun speedbar-toggle-images ()
1714 "Toggle use of images in the speedbar frame.
1715 Images are not available in Emacs 20 or earlier."
1716 (interactive)
1717 (setq speedbar-use-images (not speedbar-use-images))
1718 (speedbar-refresh))
1719
1720 (defun speedbar-toggle-sorting ()
1721 "Toggle tag sorting."
1722 (interactive)
1723 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1724
1725 (defun speedbar-toggle-show-all-files ()
1726 "Toggle display of files speedbar can not tag."
1727 (interactive)
1728 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1729 (speedbar-refresh))
1730
1731 (defmacro speedbar-with-writable (&rest forms)
1732 "Allow the buffer to be writable and evaluate FORMS."
1733 (list 'let '((inhibit-read-only t))
1734 (cons 'progn forms)))
1735 (put 'speedbar-with-writable 'lisp-indent-function 0)
1736
1737 (defun speedbar-insert-button (text face mouse function
1738 &optional token prevline)
1739 "Insert TEXT as the next logical speedbar button.
1740 FACE is the face to put on the button, MOUSE is the highlight face to use.
1741 When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
1742 This function assumes that the current buffer is the speedbar buffer.
1743 If PREVLINE, then put this button on the previous line.
1744
1745 This is a convenience function for special modes that create their own
1746 specialized speedbar displays."
1747 (goto-char (point-max))
1748 (let ((start (point)))
1749 (if (/= (current-column) 0) (insert "\n"))
1750 (put-text-property start (point) 'invisible nil))
1751 (if prevline (progn (delete-char -1)
1752 (insert " ") ;back up if desired...
1753 (put-text-property (1- (point)) (point) 'invisible nil)))
1754 (let ((start (point)))
1755 (insert text)
1756 (speedbar-make-button start (point) face mouse function token))
1757 (let ((start (point)))
1758 (insert "\n")
1759 (add-text-properties
1760 start (point) '(face nil invisible nil mouse-face nil))))
1761
1762 (defun speedbar-insert-separator (text)
1763 "Insert a separation label of TEXT.
1764 Separators are not active, have no labels, depth, or actions."
1765 (if speedbar-use-images
1766 (let ((start (point)))
1767 (insert "//")
1768 (speedbar-insert-image-button-maybe start 2)))
1769 (let ((start (point)))
1770 (insert text "\n")
1771 (speedbar-make-button start (point)
1772 'speedbar-separator-face
1773 nil nil nil)))
1774
1775 (defun speedbar-make-button (start end face mouse function &optional token)
1776 "Create a button from START to END, with FACE as the display face.
1777 MOUSE is the mouse face. When this button is clicked on FUNCTION
1778 will be run with the TOKEN parameter (any Lisp object). If FACE
1779 is t use the text properties of the string that is passed as an
1780 argument."
1781 (unless (eq face t)
1782 (put-text-property start end 'face face))
1783 (add-text-properties
1784 start end `(mouse-face ,mouse invisible nil
1785 speedbar-text ,(buffer-substring-no-properties start end)))
1786 (if speedbar-use-tool-tips-flag
1787 (put-text-property start end 'help-echo #'dframe-help-echo))
1788 (if function (put-text-property start end 'speedbar-function function))
1789 (if token (put-text-property start end 'speedbar-token token))
1790 ;; So far the only text we have is less that 3 chars.
1791 (if (<= (- end start) 3)
1792 (speedbar-insert-image-button-maybe start (- end start)))
1793 )
1794 \f
1795 ;;; Initial Expansion list management
1796 ;;
1797 (defun speedbar-initial-expansion-list ()
1798 "Return the current default expansion list.
1799 This is based on `speedbar-initial-expansion-list-name' referencing
1800 `speedbar-initial-expansion-mode-alist'."
1801 ;; cdr1 - name, cdr2 - menu
1802 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
1803 speedbar-initial-expansion-mode-alist)))))
1804
1805 (defun speedbar-initial-menu ()
1806 "Return the current default menu data.
1807 This is based on `speedbar-initial-expansion-list-name' referencing
1808 `speedbar-initial-expansion-mode-alist'."
1809 (symbol-value
1810 (car (cdr (assoc speedbar-initial-expansion-list-name
1811 speedbar-initial-expansion-mode-alist)))))
1812
1813 (defun speedbar-initial-keymap ()
1814 "Return the current default menu data.
1815 This is based on `speedbar-initial-expansion-list-name' referencing
1816 `speedbar-initial-expansion-mode-alist'."
1817 (symbol-value
1818 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
1819 speedbar-initial-expansion-mode-alist))))))
1820
1821 (defun speedbar-initial-stealthy-functions ()
1822 "Return a list of functions to call stealthily.
1823 This is based on `speedbar-initial-expansion-list-name' referencing
1824 `speedbar-stealthy-function-list'."
1825 (cdr (assoc speedbar-initial-expansion-list-name
1826 speedbar-stealthy-function-list)))
1827
1828 (defun speedbar-add-expansion-list (new-list)
1829 "Add NEW-LIST to the list of expansion lists."
1830 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
1831
1832 (defun speedbar-change-initial-expansion-list (new-default)
1833 "Change speedbar's default expansion list to NEW-DEFAULT."
1834 (interactive
1835 (list
1836 (completing-read (format "Speedbar Mode (default %s): "
1837 speedbar-previously-used-expansion-list-name)
1838 speedbar-initial-expansion-mode-alist
1839 nil t "" nil
1840 speedbar-previously-used-expansion-list-name)))
1841 (setq speedbar-previously-used-expansion-list-name
1842 speedbar-initial-expansion-list-name
1843 speedbar-initial-expansion-list-name new-default)
1844 (if (and (speedbar-current-frame) (frame-live-p (speedbar-current-frame)))
1845 (progn
1846 (speedbar-refresh)
1847 (speedbar-reconfigure-keymaps))))
1848
1849 (defun speedbar-fetch-replacement-function (function)
1850 "Return a current mode specific replacement for function, or nil.
1851 Scans `speedbar-mode-functions-list' first for the current mode, then
1852 for FUNCTION."
1853 (cdr (assoc function
1854 (cdr (assoc speedbar-initial-expansion-list-name
1855 speedbar-mode-functions-list)))))
1856
1857 (defun speedbar-add-mode-functions-list (new-list)
1858 "Add NEW-LIST to the list of mode functions.
1859 See `speedbar-mode-functions-list' for details."
1860 (add-to-list 'speedbar-mode-functions-list new-list))
1861
1862 \f
1863 ;;; Special speedbar display management
1864 ;;
1865 (defun speedbar-maybe-add-localized-support (buffer)
1866 "Quick check function called on BUFFERs by the speedbar timer function.
1867 Maintains the value of local variables which control speedbars use
1868 of the special mode functions."
1869 (or speedbar-special-mode-expansion-list
1870 (speedbar-add-localized-speedbar-support buffer)))
1871
1872 (defun speedbar-add-localized-speedbar-support (buffer)
1873 "Add localized speedbar support to BUFFER's mode if it is available."
1874 (interactive "bBuffer: ")
1875 (if (stringp buffer) (setq buffer (get-buffer buffer)))
1876 (if (not (buffer-live-p buffer))
1877 nil
1878 (save-excursion
1879 (set-buffer buffer)
1880 (save-match-data
1881 (let ((ms (symbol-name major-mode)) v)
1882 (if (not (string-match "-mode$" ms))
1883 nil ;; do nothing to broken mode
1884 (setq ms (substring ms 0 (match-beginning 0)))
1885 (setq v (intern-soft (concat ms "-speedbar-buttons")))
1886 (make-local-variable 'speedbar-special-mode-expansion-list)
1887 (if (not v)
1888 (setq speedbar-special-mode-expansion-list t)
1889 ;; If it is autoloaded, we need to load it now so that
1890 ;; we have access to the varialbe -speedbar-menu-items.
1891 ;; Is this XEmacs safe?
1892 (let ((sf (symbol-function v)))
1893 (if (and (listp sf) (eq (car sf) 'autoload))
1894 (load-library (car (cdr sf)))))
1895 (setq speedbar-special-mode-expansion-list (list v))
1896 (setq v (intern-soft (concat ms "-speedbar-key-map")))
1897 (if (not v)
1898 nil ;; don't add special keymap
1899 (make-local-variable 'speedbar-special-mode-key-map)
1900 (setq speedbar-special-mode-key-map
1901 (symbol-value v)))
1902 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
1903 (if (not v)
1904 nil ;; don't add special menus
1905 (make-local-variable 'speedbar-easymenu-definition-special)
1906 (setq speedbar-easymenu-definition-special
1907 (symbol-value v)))
1908 )))))))
1909
1910 (defun speedbar-remove-localized-speedbar-support (buffer)
1911 "Remove any traces that BUFFER supports speedbar in a specialized way."
1912 (save-excursion
1913 (set-buffer buffer)
1914 (kill-local-variable 'speedbar-special-mode-expansion-list)
1915 (kill-local-variable 'speedbar-special-mode-key-map)
1916 (kill-local-variable 'speedbar-easymenu-definition-special)))
1917 \f
1918 ;;; File button management
1919 ;;
1920 (defun speedbar-file-lists (directory)
1921 "Create file lists for DIRECTORY.
1922 The car is the list of directories, the cdr is list of files not
1923 matching ignored headers. Cache any directory files found in
1924 `speedbar-directory-contents-alist' and use that cache before scanning
1925 the file-system."
1926 (setq directory (expand-file-name directory))
1927 ;; If in powerclick mode, then the directory we are getting
1928 ;; should be rescanned.
1929 (if dframe-power-click
1930 (adelete 'speedbar-directory-contents-alist directory))
1931 ;; find the directory, either in the cache, or build it.
1932 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
1933 (let ((default-directory directory)
1934 (dir (directory-files directory nil))
1935 (dirs nil)
1936 (files nil))
1937 (while dir
1938 (if (not
1939 (or (string-match speedbar-file-unshown-regexp (car dir))
1940 (string-match speedbar-directory-unshown-regexp (car dir))))
1941 (if (file-directory-p (car dir))
1942 (setq dirs (cons (car dir) dirs))
1943 (setq files (cons (car dir) files))))
1944 (setq dir (cdr dir)))
1945 (let ((nl (cons (nreverse dirs) (list (nreverse files)))))
1946 (aput 'speedbar-directory-contents-alist directory nl)
1947 nl))
1948 ))
1949
1950 (defun speedbar-directory-buttons (directory index)
1951 "Insert a single button group at point for DIRECTORY.
1952 Each directory directory part is a different button. If part of the directory
1953 matches the user directory ~, then it is replaced with a ~.
1954 INDEX is not used, but is required by the caller."
1955 (let* ((tilde (expand-file-name "~/"))
1956 (dd (expand-file-name directory))
1957 (junk (string-match (regexp-quote tilde) dd))
1958 (displayme (if junk
1959 (concat "~/" (substring dd (match-end 0)))
1960 dd))
1961 (p (point)))
1962 (if (string-match "^~[/\\]?\\'" displayme) (setq displayme tilde))
1963 (insert displayme)
1964 (save-excursion
1965 (goto-char p)
1966 (while (re-search-forward "\\([^/\\]+\\)[/\\]" nil t)
1967 (speedbar-make-button (match-beginning 1) (match-end 1)
1968 'speedbar-directory-face
1969 'speedbar-highlight-face
1970 'speedbar-directory-buttons-follow
1971 (if (and (= (match-beginning 1) p)
1972 (not (char-equal (char-after (+ p 1)) ?:)))
1973 (expand-file-name "~/") ;the tilde
1974 (buffer-substring-no-properties
1975 p (match-end 0)))))
1976 ;; Nuke the beginning of the directory if it's too long...
1977 (cond ((eq speedbar-directory-button-trim-method 'span)
1978 (beginning-of-line)
1979 (let ((ww (or (speedbar-frame-width) 20)))
1980 (move-to-column ww nil)
1981 (while (>= (current-column) ww)
1982 (re-search-backward "[/\\]" nil t)
1983 (if (<= (current-column) 2)
1984 (progn
1985 (re-search-forward "[/\\]" nil t)
1986 (if (< (current-column) 4)
1987 (re-search-forward "[/\\]" nil t))
1988 (forward-char -1)))
1989 (if (looking-at "[/\\]?$")
1990 (beginning-of-line)
1991 (insert "/...\n ")
1992 (move-to-column ww nil)))))
1993 ((eq speedbar-directory-button-trim-method 'trim)
1994 (end-of-line)
1995 (let ((ww (or (speedbar-frame-width) 20))
1996 (tl (current-column)))
1997 (if (< ww tl)
1998 (progn
1999 (move-to-column (- tl ww))
2000 (if (re-search-backward "[/\\]" nil t)
2001 (progn
2002 (delete-region (point-min) (point))
2003 (insert "$")
2004 )))))))
2005 )
2006 (if (string-match "\\`[/\\][^/\\]+[/\\]\\'" displayme)
2007 (progn
2008 (insert " ")
2009 (let ((p (point)))
2010 (insert "<root>")
2011 (speedbar-make-button p (point)
2012 'speedbar-directory-face
2013 'speedbar-highlight-face
2014 'speedbar-directory-buttons-follow
2015 "/"))))
2016 (end-of-line)
2017 (insert-char ?\n 1 nil)))
2018
2019 (defun speedbar-make-tag-line (exp-button-type
2020 exp-button-char exp-button-function
2021 exp-button-data
2022 tag-button tag-button-function tag-button-data
2023 tag-button-face depth)
2024 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2025 This is the button that expands or contracts a node (if applicable),
2026 and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2027 is the function to call if it's clicked on. Button types are
2028 'bracket, 'angle, 'curly, 'expandtag, 'statictag, t, or nil.
2029 EXP-BUTTON-DATA is extra data attached to the text forming the expansion
2030 button.
2031
2032 Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2033 function to call if clicked on, and TAG-BUTTON-DATA is the data to
2034 attach to the text field (such a tag positioning, etc).
2035 TAG-BUTTON-FACE is a face used for this type of tag.
2036
2037 Lastly, DEPTH shows the depth of expansion.
2038
2039 This function assumes that the cursor is in the speedbar window at the
2040 position to insert a new item, and that the new item will end with a CR."
2041 (let ((start (point))
2042 (end (progn
2043 (insert (int-to-string depth) ":")
2044 (point)))
2045 (depthspacesize (* depth speedbar-indentation-width)))
2046 (put-text-property start end 'invisible t)
2047 (insert-char ? depthspacesize nil)
2048 (put-text-property (- (point) depthspacesize) (point) 'invisible nil)
2049 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2050 ((eq exp-button-type 'angle) "<%c>")
2051 ((eq exp-button-type 'curly) "{%c}")
2052 ((eq exp-button-type 'expandtag) " %c>")
2053 ((eq exp-button-type 'statictag) " =>")
2054 (t ">")))
2055 (buttxt (format exp-button exp-button-char))
2056 (start (point))
2057 (end (progn (insert buttxt) (point)))
2058 (bf (if (and exp-button-type (not (eq exp-button-type 'statictag)))
2059 'speedbar-button-face nil))
2060 (mf (if exp-button-function 'speedbar-highlight-face nil))
2061 )
2062 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
2063 (if speedbar-hide-button-brackets-flag
2064 (progn
2065 (put-text-property start (1+ start) 'invisible t)
2066 (put-text-property end (1- end) 'invisible t)))
2067 )
2068 (insert-char ? 1 nil)
2069 (put-text-property (1- (point)) (point) 'invisible nil)
2070 (let ((start (point))
2071 (end (progn (insert tag-button) (point))))
2072 (insert-char ?\n 1 nil)
2073 (put-text-property (1- (point)) (point) 'invisible nil)
2074 (speedbar-make-button start end tag-button-face
2075 (if tag-button-function 'speedbar-highlight-face nil)
2076 tag-button-function tag-button-data))
2077 ))
2078
2079 (defun speedbar-change-expand-button-char (char)
2080 "Change the expansion button character to CHAR for the current line."
2081 (save-excursion
2082 (beginning-of-line)
2083 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2084 (point)) t)
2085 (speedbar-with-writable
2086 (goto-char (match-end 1))
2087 (insert-char char 1 t)
2088 (forward-char -1)
2089 (delete-char -1)
2090 ;;(put-text-property (point) (1- (point)) 'invisible nil)
2091 ;; make sure we fix the image on the text here.
2092 (speedbar-insert-image-button-maybe (- (point) 1) 3)))))
2093
2094 \f
2095 ;;; Build button lists
2096 ;;
2097 (defun speedbar-insert-files-at-point (files level)
2098 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2099 Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2100 don't know how to manage them. The input parameter FILES is a cons
2101 cell of the form ( 'DIRLIST . 'FILELIST )."
2102 ;; Start inserting all the directories
2103 (let ((dirs (car files)))
2104 (while dirs
2105 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired (car dirs)
2106 (car dirs) 'speedbar-dir-follow nil
2107 'speedbar-directory-face level)
2108 (setq dirs (cdr dirs))))
2109 (let ((lst (car (cdr files)))
2110 (case-fold-search t))
2111 (while lst
2112 (let* ((known (string-match speedbar-file-regexp (car lst)))
2113 (expchar (if known ?+ ??))
2114 (fn (if known 'speedbar-tag-file nil)))
2115 (if (or speedbar-show-unknown-files (/= expchar ??))
2116 (speedbar-make-tag-line 'bracket expchar fn (car lst)
2117 (car lst) 'speedbar-find-file nil
2118 'speedbar-file-face level)))
2119 (setq lst (cdr lst)))))
2120
2121 (defun speedbar-default-directory-list (directory index)
2122 "Insert files for DIRECTORY with level INDEX at point."
2123 (speedbar-insert-files-at-point
2124 (speedbar-file-lists directory) index)
2125 (speedbar-reset-scanners)
2126 (if (= index 0)
2127 ;; If the shown files variable has extra directories, then
2128 ;; it is our responsibility to redraw them all
2129 ;; Luckilly, the nature of inserting items into this list means
2130 ;; that by reversing it, we can easilly go in the right order
2131 (let ((sf (cdr (reverse speedbar-shown-directories))))
2132 (setq speedbar-shown-directories
2133 (list (expand-file-name default-directory)))
2134 ;; exand them all as we find them
2135 (while sf
2136 (if (speedbar-goto-this-file (car sf))
2137 (progn
2138 (beginning-of-line)
2139 (if (looking-at "[0-9]+:[ ]*<")
2140 (progn
2141 (goto-char (match-end 0))
2142 (speedbar-do-function-pointer)))))
2143 (setq sf (cdr sf)))
2144 )))
2145 ;;; Generic List support
2146 ;;
2147 ;; Generic lists are hierarchies of tags which we may need to permute
2148 ;; in order to make it look nice.
2149 ;;
2150 ;; A generic list is of the form:
2151 ;; ( ("name" . marker-or-number) <-- one tag at this level
2152 ;; ("name" ("name" . mon) ("name" . mon) ) <-- one group of tags
2153 ;; ("name" mon ("name" . mon) ) <-- group w/ a position and tags
2154 (defun speedbar-generic-list-group-p (sublst)
2155 "Non-nil if SUBLST is a group.
2156 Groups may optionally contain a position."
2157 (and (stringp (car-safe sublst))
2158 (or (and (listp (cdr-safe sublst))
2159 (or (speedbar-generic-list-tag-p (car-safe (cdr-safe sublst)))
2160 (speedbar-generic-list-group-p (car-safe (cdr-safe sublst))
2161 )))
2162 (and (number-or-marker-p (car-safe (cdr-safe sublst)))
2163 (listp (cdr-safe (cdr-safe sublst)))
2164 (speedbar-generic-list-tag-p
2165 (car-safe (cdr-safe (cdr-safe sublst)))))
2166 )))
2167
2168 (defun speedbar-generic-list-positioned-group-p (sublst)
2169 "Non-nil of SUBLST is a group with a position."
2170 (and (stringp (car-safe sublst))
2171 (number-or-marker-p (car-safe (cdr-safe sublst)))
2172 (listp (cdr-safe (cdr-safe sublst)))
2173 (let ((rest (car-safe (cdr-safe (cdr-safe sublst)))))
2174 (or (speedbar-generic-list-tag-p rest)
2175 (speedbar-generic-list-group-p rest)
2176 (speedbar-generic-list-positioned-group-p rest)
2177 ))))
2178
2179 (defun speedbar-generic-list-tag-p (sublst)
2180 "Non nil if SUBLST is a tag."
2181 (and (stringp (car-safe sublst))
2182 (or (and (number-or-marker-p (cdr-safe sublst))
2183 (not (cdr-safe (cdr-safe sublst))))
2184 ;; For semantic/bovine items, this is needed
2185 (symbolp (car-safe (cdr-safe sublst))))
2186 ))
2187
2188 (defun speedbar-sort-tag-hierarchy (lst)
2189 "Sort all elements of tag hierarchy LST."
2190 (sort (copy-alist lst)
2191 (lambda (a b) (string< (car a) (car b)))))
2192
2193 (defun speedbar-try-completion (string alist)
2194 "A wrapper for `try-completion'.
2195 Passes STRING and ALIST to `try-completion' if ALIST
2196 passes some tests."
2197 (if (and (consp alist)
2198 (listp (car alist)) (stringp (car (car alist))))
2199 (try-completion string alist)
2200 nil))
2201
2202 (defun speedbar-prefix-group-tag-hierarchy (lst)
2203 "Prefix group names for tag hierarchy LST."
2204 (let ((newlst nil)
2205 (sublst nil)
2206 (work-list nil)
2207 (junk-list nil)
2208 (short-group-list nil)
2209 (short-start-name nil)
2210 (short-end-name nil)
2211 (num-shorts-grouped 0)
2212 (bins (make-vector 256 nil))
2213 (diff-idx 0))
2214 (if (<= (length lst) speedbar-tag-regroup-maximum-length)
2215 ;; Do nothing. Too short to bother with.
2216 lst
2217 ;; Break out sub-lists
2218 (while lst
2219 (if (speedbar-generic-list-group-p (car-safe lst))
2220 (setq newlst (cons (car lst) newlst))
2221 (setq sublst (cons (car lst) sublst)))
2222 (setq lst (cdr lst)))
2223 ;; Reverse newlst because it was made backwards.
2224 ;; Sublist doesn't need reversing because the act
2225 ;; of binning things will reverse it for us.
2226 (setq newlst (nreverse newlst)
2227 sublst sublst)
2228 ;; Now, first find out how long our list is. Never let a
2229 ;; list get-shorter than our minimum.
2230 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2231 (setq work-list sublst)
2232 (setq diff-idx (length (speedbar-try-completion "" sublst)))
2233 ;; Sort the whole list into bins.
2234 (while sublst
2235 (let ((e (car sublst))
2236 (s (car (car sublst))))
2237 (cond ((<= (length s) diff-idx)
2238 ;; 0 storage bin for shorty.
2239 (aset bins 0 (cons e (aref bins 0))))
2240 (t
2241 ;; stuff into a bin based on ascii value at diff
2242 (aset bins (aref s diff-idx)
2243 (cons e (aref bins (aref s diff-idx)))))))
2244 (setq sublst (cdr sublst)))
2245 ;; Go through all our bins Stick singles into our
2246 ;; junk-list, everything else as sublsts in work-list.
2247 ;; If two neighboring lists are both small, make a grouped
2248 ;; group combinding those two sub-lists.
2249 (setq diff-idx 0)
2250 (while (> 256 diff-idx)
2251 ;; The bins contents are currently in forward order.
2252 (let ((l (aref bins diff-idx)))
2253 (if l
2254 (let ((tmp (cons (speedbar-try-completion "" l) l)))
2255 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2256 (> (+ (length l) (length short-group-list))
2257 speedbar-tag-split-minimum-length))
2258 (progn
2259 ;; We have reached a longer list, so we
2260 ;; must finish off a grouped group.
2261 (cond
2262 ((and short-group-list
2263 (= (length short-group-list)
2264 num-shorts-grouped))
2265 ;; All singles? Junk list
2266 (setq junk-list (append (nreverse short-group-list)
2267 junk-list)))
2268 ((= num-shorts-grouped 1)
2269 ;; Only one short group? Just stick it in
2270 ;; there by itself. Make a group, and find
2271 ;; a subexpression
2272 (let ((subexpression (speedbar-try-completion
2273 "" short-group-list)))
2274 (if (< (length subexpression)
2275 speedbar-tag-group-name-minimum-length)
2276 (setq subexpression
2277 (concat short-start-name
2278 " ("
2279 (substring
2280 (car (car short-group-list))
2281 (length short-start-name))
2282 ")")))
2283 (setq work-list
2284 (cons (cons subexpression
2285 short-group-list)
2286 work-list ))))
2287 (short-group-list
2288 ;; Multiple groups to be named in a special
2289 ;; way by displaying the range over which we
2290 ;; have grouped them.
2291 (setq work-list
2292 (cons (cons (concat short-start-name
2293 " to "
2294 short-end-name)
2295 short-group-list)
2296 work-list))))
2297 ;; Reset short group list information every time.
2298 (setq short-group-list nil
2299 short-start-name nil
2300 short-end-name nil
2301 num-shorts-grouped 0)))
2302 ;; Ok, now that we cleaned up the short-group-list,
2303 ;; we can deal with this new list, to decide if it
2304 ;; should go on one of these sub-lists or not.
2305 (if (< (length l) speedbar-tag-regroup-maximum-length)
2306 (setq short-group-list (append l short-group-list)
2307 num-shorts-grouped (1+ num-shorts-grouped)
2308 short-end-name (car tmp)
2309 short-start-name (if short-start-name
2310 short-start-name
2311 (car tmp)))
2312 (setq work-list (cons tmp work-list))))))
2313 (setq diff-idx (1+ diff-idx))))
2314 ;; Did we run out of things? Drop our new list onto the end.
2315 (cond
2316 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2317 ;; All singles? Junk list
2318 (setq junk-list (append short-group-list junk-list)))
2319 ((= num-shorts-grouped 1)
2320 ;; Only one short group? Just stick it in
2321 ;; there by itself.
2322 (setq work-list
2323 (cons (cons (speedbar-try-completion "" short-group-list)
2324 short-group-list)
2325 work-list)))
2326 (short-group-list
2327 ;; Multiple groups to be named in a special
2328 ;; way by displaying the range over which we
2329 ;; have grouped them.
2330 (setq work-list
2331 (cons (cons (concat short-start-name " to " short-end-name)
2332 short-group-list)
2333 work-list))))
2334 ;; Reverse the work list nreversed when consing.
2335 (setq work-list (nreverse work-list))
2336 ;; Now, stick our new list onto the end of
2337 (if work-list
2338 (if junk-list
2339 (append newlst work-list junk-list)
2340 (append newlst work-list))
2341 (append newlst junk-list)))))
2342
2343 (defun speedbar-trim-words-tag-hierarchy (lst)
2344 "Trim all words in a tag hierarchy.
2345 Base trimming information on word separators, and group names.
2346 Argument LST is the list of tags to trim."
2347 (let ((newlst nil)
2348 (sublst nil)
2349 (trim-prefix nil)
2350 (trim-chars 0)
2351 (trimlst nil))
2352 (while lst
2353 (if (speedbar-generic-list-group-p (car-safe lst))
2354 (setq newlst (cons (car lst) newlst))
2355 (setq sublst (cons (car lst) sublst)))
2356 (setq lst (cdr lst)))
2357 ;; Get the prefix to trim by. Make sure that we don't trim
2358 ;; off silly pieces, only complete understandable words.
2359 (setq trim-prefix (speedbar-try-completion "" sublst)
2360 newlst (nreverse newlst))
2361 (if (or (= (length sublst) 1)
2362 (not trim-prefix)
2363 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2364 (append newlst (nreverse sublst))
2365 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2366 (match-end 0)))
2367 (setq trim-chars (length trim-prefix))
2368 (while sublst
2369 (setq trimlst (cons
2370 (cons (substring (car (car sublst)) trim-chars)
2371 (cdr (car sublst)))
2372 trimlst)
2373 sublst (cdr sublst)))
2374 ;; Put the lists together
2375 (append newlst trimlst))))
2376
2377 (defun speedbar-simple-group-tag-hierarchy (lst)
2378 "Create a simple 'Tags' group with orphaned tags.
2379 Argument LST is the list of tags to sort into groups."
2380 (let ((newlst nil)
2381 (sublst nil))
2382 (while lst
2383 (if (speedbar-generic-list-group-p (car-safe lst))
2384 (setq newlst (cons (car lst) newlst))
2385 (setq sublst (cons (car lst) sublst)))
2386 (setq lst (cdr lst)))
2387 (if (not newlst)
2388 (nreverse sublst)
2389 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2390 (nreverse newlst))))
2391
2392 (defun speedbar-create-tag-hierarchy (lst)
2393 "Adjust the tag hierarchy in LST, and return it.
2394 This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2395 the list."
2396 (let* ((f (save-excursion
2397 (forward-line -1)
2398 (or (speedbar-line-file)
2399 (speedbar-line-directory))))
2400 (methods (if (get-file-buffer f)
2401 (save-excursion (set-buffer (get-file-buffer f))
2402 speedbar-tag-hierarchy-method)
2403 speedbar-tag-hierarchy-method))
2404 (lst (if (fboundp 'copy-tree)
2405 (copy-tree lst)
2406 lst)))
2407 (while methods
2408 (setq lst (funcall (car methods) lst)
2409 methods (cdr methods)))
2410 lst))
2411
2412 (defvar speedbar-generic-list-group-expand-button-type 'curly
2413 "The type of button created for groups of tags.
2414 Good values for this are `curly' and `expandtag'.
2415 Make buffer local for your mode.")
2416
2417 (defvar speedbar-generic-list-tag-button-type nil
2418 "The type of button created for tags in generic lists.
2419 Good values for this are nil and `statictag'.
2420 Make buffer local for your mode.")
2421
2422 (defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2423 "At LEVEL, insert a generic multi-level alist LST.
2424 Associations with lists get {+} tags (to expand into more nodes) and
2425 those with positions just get a > as the indicator. {+} buttons will
2426 have the function EXPAND-FUN and the token is the CDR list. The token
2427 name will have the function FIND-FUN and not token."
2428 ;; Remove imenu rescan button
2429 (if (string= (car (car lst)) "*Rescan*")
2430 (setq lst (cdr lst)))
2431 ;; Get, and set up variables that define how we treat these tags.
2432 (let ((f (save-excursion (forward-line -1)
2433 (or (speedbar-line-file)
2434 (speedbar-line-directory))))
2435 expand-button tag-button)
2436 (save-excursion
2437 (if (get-file-buffer f)
2438 (set-buffer (get-file-buffer f)))
2439 (setq expand-button speedbar-generic-list-group-expand-button-type
2440 tag-button speedbar-generic-list-tag-button-type))
2441 ;; Adjust the list.
2442 (setq lst (speedbar-create-tag-hierarchy lst))
2443 ;; insert the parts
2444 (while lst
2445 (cond ((null (car-safe lst)) nil) ;this would be a separator
2446 ((speedbar-generic-list-tag-p (car lst))
2447 (speedbar-make-tag-line tag-button
2448 nil nil nil ;no expand button data
2449 (car (car lst)) ;button name
2450 find-fun ;function
2451 (cdr (car lst)) ;token is position
2452 'speedbar-tag-face
2453 (1+ level)))
2454 ((speedbar-generic-list-positioned-group-p (car lst))
2455 (speedbar-make-tag-line expand-button
2456 ?+ expand-fun (cdr (cdr (car lst)))
2457 (car (car lst)) ;button name
2458 find-fun ;function
2459 (car (cdr (car lst))) ;token is posn
2460 'speedbar-tag-face
2461 (1+ level)))
2462 ((speedbar-generic-list-group-p (car lst))
2463 (speedbar-make-tag-line expand-button
2464 ?+ expand-fun (cdr (car lst))
2465 (car (car lst)) ;button name
2466 nil nil 'speedbar-tag-face
2467 (1+ level)))
2468 (t (speedbar-message "speedbar-insert-generic-list: malformed list!")
2469 ))
2470 (setq lst (cdr lst)))))
2471
2472 (defun speedbar-insert-imenu-list (indent lst)
2473 "At level INDENT, insert the imenu generated LST."
2474 (speedbar-insert-generic-list indent lst
2475 'speedbar-tag-expand
2476 'speedbar-tag-find))
2477
2478 (defun speedbar-insert-etags-list (indent lst)
2479 "At level INDENT, insert the etags generated LST."
2480 (speedbar-insert-generic-list indent lst
2481 'speedbar-tag-expand
2482 'speedbar-tag-find))
2483 \f
2484 ;;; Timed functions
2485 ;;
2486 (defun speedbar-update-contents ()
2487 "Generically update the contents of the speedbar buffer."
2488 (interactive)
2489 ;; Set the current special buffer
2490 (setq speedbar-desired-buffer nil)
2491
2492 ;; Check for special modes
2493 (speedbar-maybe-add-localized-support (current-buffer))
2494
2495 ;; Choose the correct method of doodling.
2496 (if (and speedbar-mode-specific-contents-flag
2497 (consp speedbar-special-mode-expansion-list)
2498 (local-variable-p
2499 'speedbar-special-mode-expansion-list
2500 (current-buffer)))
2501 ;;(eq (get major-mode 'mode-class 'special)))
2502 (speedbar-update-special-contents)
2503 (speedbar-update-directory-contents)))
2504
2505 (defun speedbar-update-localized-contents ()
2506 "Update the contents of the speedbar buffer for the current situation."
2507 ;; Due to the historical growth of speedbar, we need to do something
2508 ;; special for "files" mode. Too bad.
2509 (let ((name speedbar-initial-expansion-list-name)
2510 (funclst (speedbar-initial-expansion-list))
2511 )
2512 (if (string= name "files")
2513 ;; Do all the files type work. It still goes through the
2514 ;; expansion list stuff. :(
2515 (if (or (member (expand-file-name default-directory)
2516 speedbar-shown-directories)
2517 (and speedbar-ignored-directory-regexp
2518 (string-match
2519 speedbar-ignored-directory-regexp
2520 (expand-file-name default-directory))))
2521 nil
2522 (if (<= 1 speedbar-verbosity-level)
2523 (speedbar-message "Updating speedbar to: %s..."
2524 default-directory))
2525 (speedbar-update-directory-contents)
2526 (if (<= 1 speedbar-verbosity-level)
2527 (progn
2528 (speedbar-message "Updating speedbar to: %s...done"
2529 default-directory)
2530 (speedbar-message nil))))
2531 ;; Else, we can do a short cut. No text cache.
2532 (let ((cbd (expand-file-name default-directory))
2533 )
2534 (set-buffer speedbar-buffer)
2535 (speedbar-with-writable
2536 (let* ((window (get-buffer-window speedbar-buffer 0))
2537 (p (window-point window))
2538 (start (window-start window)))
2539 (erase-buffer)
2540 (dolist (func funclst)
2541 (setq default-directory cbd)
2542 (funcall func cbd 0))
2543 (speedbar-reconfigure-keymaps)
2544 (set-window-point window p)
2545 (set-window-start window start)))
2546 ))))
2547
2548 (defun speedbar-update-directory-contents ()
2549 "Update the contents of the speedbar buffer based on the current directory."
2550 (let ((cbd (expand-file-name default-directory))
2551 cbd-parent
2552 (funclst (speedbar-initial-expansion-list))
2553 (cache speedbar-full-text-cache)
2554 ;; disable stealth during update
2555 (speedbar-stealthy-function-list nil)
2556 (use-cache nil)
2557 (expand-local nil)
2558 ;; Because there is a bug I can't find just yet
2559 (inhibit-quit nil))
2560 (set-buffer speedbar-buffer)
2561 ;; If we are updating contents to where we are, then this is
2562 ;; really a request to update existing contents, so we must be
2563 ;; careful with our text cache!
2564 (if (member cbd speedbar-shown-directories)
2565 (progn
2566 (setq cache nil)
2567 ;; If the current directory is not the last element in the dir
2568 ;; list, then we ALSO need to zap the list of expanded directories
2569 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2570 (setq speedbar-shown-directories (list cbd))))
2571
2572 ;; Build cbd-parent, and see if THAT is in the current shown
2573 ;; directories. First, go through pains to get the parent directory
2574 (if (and speedbar-smart-directory-expand-flag
2575 (save-match-data
2576 (setq cbd-parent cbd)
2577 (if (string-match "[/\\]$" cbd-parent)
2578 (setq cbd-parent (substring cbd-parent 0
2579 (match-beginning 0))))
2580 (setq cbd-parent (file-name-directory cbd-parent)))
2581 (member cbd-parent speedbar-shown-directories))
2582 (setq expand-local t)
2583
2584 ;; If this directory is NOT in the current list of available
2585 ;; directorys, then use the cache, and set the cache to our new
2586 ;; value. Make sure to unhighlight the current file, or if we
2587 ;; come back to this directory, it might be a different file
2588 ;; and then we get a mess!
2589 (if (> (point-max) 1)
2590 (progn
2591 (speedbar-clear-current-file)
2592 (setq speedbar-full-text-cache
2593 (cons speedbar-shown-directories (buffer-string)))))
2594
2595 ;; Check if our new directory is in the list of directories
2596 ;; shown in the text-cache
2597 (if (member cbd (car cache))
2598 (setq speedbar-shown-directories (car cache)
2599 use-cache t)
2600 ;; default the shown directories to this list...
2601 (setq speedbar-shown-directories (list cbd)))
2602 ))
2603 (if (not expand-local) (setq speedbar-last-selected-file nil))
2604 (speedbar-with-writable
2605 (if (and expand-local
2606 ;; Find this directory as a speedbar node.
2607 (speedbar-directory-line cbd))
2608 ;; Open it.
2609 (speedbar-expand-line)
2610 (let* ((window (get-buffer-window speedbar-buffer 0))
2611 (p (window-point window))
2612 (start (window-start window)))
2613 (erase-buffer)
2614 (cond (use-cache
2615 (setq default-directory
2616 (nth (1- (length speedbar-shown-directories))
2617 speedbar-shown-directories))
2618 (insert (cdr cache)))
2619 (t
2620 (dolist (func funclst)
2621 (setq default-directory cbd)
2622 (funcall func cbd 0))))
2623 (set-window-point window p)
2624 (set-window-start window start)))))
2625 (speedbar-reconfigure-keymaps))
2626
2627 (defun speedbar-update-special-contents ()
2628 "Use the mode-specific variable to fill in the speedbar buffer.
2629 This should only be used by modes classified as special."
2630 (let ((funclst speedbar-special-mode-expansion-list)
2631 (specialbuff (current-buffer)))
2632 (save-excursion
2633 (setq speedbar-desired-buffer specialbuff)
2634 (set-buffer speedbar-buffer)
2635 ;; If we are leaving a directory, cache it.
2636 (if (not speedbar-shown-directories)
2637 ;; Do nothing
2638 nil
2639 ;; Clean up directory maintenance stuff
2640 (speedbar-clear-current-file)
2641 (setq speedbar-full-text-cache
2642 (cons speedbar-shown-directories (buffer-string))
2643 speedbar-shown-directories nil))
2644 ;; Now fill in the buffer with our newly found specialized list.
2645 (speedbar-with-writable
2646 (dolist (func funclst)
2647 ;; We do not erase the buffer because these functions may
2648 ;; decide NOT to update themselves.
2649 (funcall func specialbuff)))))
2650 (speedbar-reconfigure-keymaps))
2651
2652 (defun speedbar-set-timer (timeout)
2653 "Set up the speedbar timer with TIMEOUT.
2654 Uses `dframe-set-timer'.
2655 Also resets scanner functions."
2656 (dframe-set-timer timeout 'speedbar-timer-fn 'speedbar-update-flag)
2657 ;; Apply a revert hook that will reset the scanners. We attach to revert
2658 ;; because most reverts occur during VC state change, and this lets our
2659 ;; VC scanner fix itself.
2660 (if timeout
2661 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
2662 (remove-hook 'after-revert-hook 'speedbar-reset-scanners))
2663 ;; change this if it changed for some reason
2664 (speedbar-set-mode-line-format))
2665
2666 (defun speedbar-timer-fn ()
2667 "Run whenever Emacs is idle to update the speedbar item."
2668 (if (or (not (speedbar-current-frame))
2669 (not (frame-live-p (speedbar-current-frame))))
2670 (speedbar-set-timer nil)
2671 ;; Save all the match data so that we don't mess up executing fns
2672 (save-match-data
2673 ;; Only do stuff if the frame is visible, not an icon, and if
2674 ;; it is currently flagged to do something.
2675 (if (and speedbar-update-flag
2676 (speedbar-current-frame)
2677 (frame-visible-p (speedbar-current-frame))
2678 (not (eq (frame-visible-p (speedbar-current-frame)) 'icon)))
2679 (let ((af (selected-frame)))
2680 (save-window-excursion
2681 (dframe-select-attached-frame speedbar-frame)
2682 ;; make sure we at least choose a window to
2683 ;; get a good directory from
2684 (if (window-minibuffer-p (selected-window))
2685 nil
2686 ;; Check for special modes
2687 (speedbar-maybe-add-localized-support (current-buffer))
2688 ;; Update for special mode all the time!
2689 (if (and speedbar-mode-specific-contents-flag
2690 (consp speedbar-special-mode-expansion-list)
2691 (local-variable-p
2692 'speedbar-special-mode-expansion-list
2693 (current-buffer)))
2694 ;;(eq (get major-mode 'mode-class 'special)))
2695 (progn
2696 (if (<= 2 speedbar-verbosity-level)
2697 (speedbar-message
2698 "Updating speedbar to special mode: %s..."
2699 major-mode))
2700 (speedbar-update-special-contents)
2701 (if (<= 2 speedbar-verbosity-level)
2702 (progn
2703 (speedbar-message
2704 "Updating speedbar to special mode: %s...done"
2705 major-mode)
2706 (speedbar-message nil))))
2707 ;; Update all the contents if directories change!
2708 (if (or (member major-mode speedbar-ignored-modes)
2709 (eq af (speedbar-current-frame))
2710 (not (buffer-file-name)))
2711 nil
2712 (speedbar-update-localized-contents)
2713 ))
2714 (select-frame af)))
2715 ;; Now run stealthy updates of time-consuming items
2716 (speedbar-stealthy-updates)))))
2717 (run-hooks 'speedbar-timer-hook))
2718
2719 \f
2720 ;;; Stealthy activities
2721 ;;
2722 (defvar speedbar-stealthy-update-recurse nil
2723 "Recursion avoidance variable for stealthy update.")
2724
2725 (defun speedbar-stealthy-updates ()
2726 "For a given speedbar, run all items in the stealthy function list.
2727 Each item returns t if it completes successfully, or nil if
2728 interrupted by the user."
2729 (if (not speedbar-stealthy-update-recurse)
2730 (let ((l (speedbar-initial-stealthy-functions))
2731 (speedbar-stealthy-update-recurse t))
2732 (unwind-protect
2733 (speedbar-with-writable
2734 (while (and l (funcall (car l)))
2735 ;;(sit-for 0)
2736 (setq l (cdr l))))
2737 ;;(speedbar-message "Exit with %S" (car l))
2738 ))))
2739
2740 (defun speedbar-reset-scanners ()
2741 "Reset any variables used by functions in the stealthy list as state.
2742 If new functions are added, their state needs to be updated here."
2743 (setq speedbar-vc-to-do-point t
2744 speedbar-obj-to-do-point t
2745 speedbar-ro-to-do-point t)
2746 (run-hooks 'speedbar-scanner-reset-hook)
2747 )
2748
2749 (defun speedbar-find-selected-file (file)
2750 "Go to the line where FILE is."
2751
2752 (set-buffer speedbar-buffer)
2753
2754 (goto-char (point-min))
2755 (let ((m nil))
2756 (while (and (setq m (re-search-forward
2757 (concat " \\(" (regexp-quote (file-name-nondirectory file))
2758 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2759 nil t))
2760 (not (string= file
2761 (concat
2762 (speedbar-line-directory
2763 (save-excursion
2764 (goto-char (match-beginning 0))
2765 (beginning-of-line)
2766 (save-match-data
2767 (looking-at "[0-9]+:")
2768 (string-to-number (match-string 0)))))
2769 (match-string 1))))))
2770 (if m
2771 (progn
2772 (goto-char (match-beginning 1))
2773 (match-string 1)))))
2774
2775 (defun speedbar-clear-current-file ()
2776 "Locate the file thought to be current, and remove its highlighting."
2777 (save-excursion
2778 ;;(set-buffer speedbar-buffer)
2779 (if speedbar-last-selected-file
2780 (speedbar-with-writable
2781 (if (speedbar-find-selected-file speedbar-last-selected-file)
2782 (put-text-property (match-beginning 1)
2783 (match-end 1)
2784 'face
2785 'speedbar-file-face))))))
2786
2787 (defun speedbar-update-current-file ()
2788 "Find the current file, and update our visuals to indicate its name.
2789 This is specific to file names. If the file name doesn't show up, but
2790 it should be in the list, then the directory cache needs to be
2791 updated."
2792 (let* ((lastf (selected-frame))
2793 (newcfd (save-excursion
2794 (dframe-select-attached-frame speedbar-frame)
2795 (let ((rf (if (buffer-file-name)
2796 (buffer-file-name)
2797 nil)))
2798 (select-frame lastf)
2799 rf)))
2800 (newcf (if newcfd newcfd))
2801 (lastb (current-buffer))
2802 (sucf-recursive (boundp 'sucf-recursive))
2803 (case-fold-search t))
2804 (if (and newcf
2805 ;; check here, that way we won't refresh to newcf until
2806 ;; its been written, thus saving ourselves some time
2807 (file-exists-p newcf)
2808 (not (string= newcf speedbar-last-selected-file)))
2809 (progn
2810 ;; It is important to select the frame, otherwise the window
2811 ;; we want the cursor to move in will not be updated by the
2812 ;; search-forward command.
2813 (select-frame (speedbar-current-frame))
2814 ;; Remove the old file...
2815 (speedbar-clear-current-file)
2816 ;; now highlight the new one.
2817 ;; (set-buffer speedbar-buffer)
2818 (speedbar-with-writable
2819 (if (speedbar-find-selected-file newcf)
2820 ;; put the property on it
2821 (put-text-property (match-beginning 1)
2822 (match-end 1)
2823 'face
2824 'speedbar-selected-face)
2825 ;; Oops, it's not in the list. Should it be?
2826 (if (and (string-match speedbar-file-regexp newcf)
2827 (string= (file-name-directory newcfd)
2828 (expand-file-name default-directory)))
2829 ;; yes, it is (we will ignore unknowns for now...)
2830 (progn
2831 (speedbar-refresh)
2832 (if (speedbar-find-selected-file newcf)
2833 ;; put the property on it
2834 (put-text-property (match-beginning 1)
2835 (match-end 1)
2836 'face
2837 'speedbar-selected-face)))
2838 ;; if it's not in there now, whatever...
2839 ))
2840 (setq speedbar-last-selected-file newcf))
2841 (if (not sucf-recursive)
2842 (progn
2843
2844 ;;Sat Dec 15 2001 12:40 AM (burton@openprivacy.org): this
2845 ;;doesn't need to be in. We don't want to recenter when we are
2846 ;;updating files.
2847
2848 ;;(speedbar-center-buffer-smartly)
2849
2850 (speedbar-position-cursor-on-line)
2851 ))
2852 (set-buffer lastb)
2853 (select-frame lastf)
2854 )))
2855 ;; return that we are done with this activity.
2856 t)
2857
2858 (defun speedbar-add-indicator (indicator-string &optional replace-this)
2859 "Add INDICATOR-STRING to the end of this speedbar line.
2860 If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2861 the existing indicator is removed. If there is already an
2862 indicator, then do not add a space."
2863 (beginning-of-line)
2864 ;; The nature of the beast: Assume we are in "the right place"
2865 (end-of-line)
2866 (skip-chars-backward (concat " " speedbar-vc-indicator
2867 speedbar-object-read-only-indicator
2868 (car speedbar-obj-indicator)
2869 (cdr speedbar-obj-indicator)))
2870 (if (and (not (looking-at speedbar-indicator-regex))
2871 (not (string= indicator-string " ")))
2872 (insert speedbar-indicator-separator))
2873 (speedbar-with-writable
2874 (save-excursion
2875 (if (and replace-this
2876 (re-search-forward replace-this (save-excursion (end-of-line)
2877 (point))
2878 t))
2879 (delete-region (match-beginning 0) (match-end 0))))
2880 (end-of-line)
2881 (if (not (string= " " indicator-string))
2882 (let ((start (point)))
2883 (insert indicator-string)
2884 (speedbar-insert-image-button-maybe start (length indicator-string))
2885 ))))
2886
2887 (defun speedbar-check-read-only ()
2888 "Scan all the files in a directory, and for each see if it is read only."
2889 ;; Check for to-do to be reset. If reset but no RCS is available
2890 ;; then set to nil (do nothing) otherwise, start at the beginning
2891 (save-excursion
2892 (if speedbar-buffer (set-buffer speedbar-buffer))
2893 (if (eq speedbar-ro-to-do-point t)
2894 (setq speedbar-ro-to-do-point 0))
2895 (if (numberp speedbar-ro-to-do-point)
2896 (progn
2897 (goto-char speedbar-ro-to-do-point)
2898 (while (and (not (input-pending-p))
2899 (re-search-forward "^\\([0-9]+\\):\\s-*[[<][+-\?][]>] "
2900 nil t))
2901 (setq speedbar-ro-to-do-point (point))
2902 (let ((f (speedbar-line-file)))
2903 (if f
2904 (if (not (file-writable-p f))
2905 (speedbar-add-indicator
2906 speedbar-object-read-only-indicator
2907 (regexp-quote speedbar-object-read-only-indicator))
2908 (speedbar-add-indicator
2909 " " (regexp-quote
2910 speedbar-object-read-only-indicator))))))
2911 (if (input-pending-p)
2912 ;; return that we are incomplete
2913 nil
2914 ;; we are done, set to-do to nil
2915 (setq speedbar-ro-to-do-point nil)
2916 ;; and return t
2917 t))
2918 t)))
2919
2920 ;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
2921 ;; Steven L Baur <steve@xemacs.org> said this was important:
2922 (eval-when-compile (or (featurep 'xemacs)
2923 (condition-case () (require 'efs)
2924 (error (require 'ange-ftp)))))
2925
2926 (defun speedbar-check-vc ()
2927 "Scan all files in a directory, and for each see if it's checked out.
2928 See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
2929 to add more types of version control systems."
2930 ;; Check for to-do to be reset. If reset but no RCS is available
2931 ;; then set to nil (do nothing) otherwise, start at the beginning
2932 (save-excursion
2933 (if speedbar-buffer (set-buffer speedbar-buffer))
2934 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
2935 (speedbar-vc-check-dir-p default-directory)
2936 (not (or (and (featurep 'ange-ftp)
2937 (string-match
2938 (car (symbol-value
2939 (if dframe-xemacsp
2940 'ange-ftp-directory-format
2941 'ange-ftp-name-format)))
2942 (expand-file-name default-directory)))
2943 ;; efs support: Bob Weiner
2944 (and (featurep 'efs)
2945 (string-match
2946 (let ((reg (symbol-value 'efs-directory-regexp)))
2947 (if (stringp reg)
2948 reg
2949 (car reg)))
2950 (expand-file-name default-directory))))))
2951 (setq speedbar-vc-to-do-point 0))
2952 (if (numberp speedbar-vc-to-do-point)
2953 (progn
2954 (goto-char speedbar-vc-to-do-point)
2955 (while (and (not (input-pending-p))
2956 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-?]\\] "
2957 nil t))
2958 (setq speedbar-vc-to-do-point (point))
2959 (if (speedbar-check-vc-this-line (match-string 1))
2960 (speedbar-add-indicator speedbar-vc-indicator
2961 (regexp-quote speedbar-vc-indicator))
2962 (speedbar-add-indicator " "
2963 (regexp-quote speedbar-vc-indicator))))
2964 (if (input-pending-p)
2965 ;; return that we are incomplete
2966 nil
2967 ;; we are done, set to-do to nil
2968 (setq speedbar-vc-to-do-point nil)
2969 ;; and return t
2970 t))
2971 t)))
2972
2973 (defun speedbar-check-vc-this-line (depth)
2974 "Return t if the file on this line is check of of a version control system.
2975 Parameter DEPTH is a string with the current depth of indentation of
2976 the file being checked."
2977 (let* ((d (string-to-number depth))
2978 (f (speedbar-line-directory d))
2979 (fn (buffer-substring-no-properties
2980 ;; Skip-chars: thanks ptype@dra.hmg.gb
2981 (point) (progn
2982 (skip-chars-forward "^ "
2983 (save-excursion (end-of-line)
2984 (point)))
2985 (point))))
2986 (fulln (concat f fn)))
2987 (if (<= 2 speedbar-verbosity-level)
2988 (speedbar-message "Speedbar vc check...%s" fulln))
2989 (and (file-writable-p fulln)
2990 (speedbar-this-file-in-vc f fn))))
2991
2992 (defun speedbar-vc-check-dir-p (directory)
2993 "Return t if we should bother checking DIRECTORY for version control files.
2994 This can be overloaded to add new types of version control systems."
2995 (or
2996 ;; Local CVS available in Emacs 21
2997 (and (fboundp 'vc-state)
2998 (file-exists-p (concat directory "CVS/")))
2999 ;; Local RCS
3000 (file-exists-p (concat directory "RCS/"))
3001 ;; Local SCCS
3002 (file-exists-p (concat directory "SCCS/"))
3003 ;; Remote SCCS project
3004 (let ((proj-dir (getenv "PROJECTDIR")))
3005 (if proj-dir
3006 (file-exists-p (concat proj-dir "/SCCS"))
3007 nil))
3008 ;; User extension
3009 (run-hook-with-args-until-success 'speedbar-vc-directory-enable-hook
3010 directory)
3011 ))
3012
3013 (defun speedbar-this-file-in-vc (directory name)
3014 "Check to see if the file in DIRECTORY with NAME is in a version control system.
3015 You can add new VC systems by overriding this function. You can
3016 optimize this function by overriding it and only doing those checks
3017 that will occur on your system."
3018 (or
3019 (if (fboundp 'vc-state)
3020 ;; Emacs 21 handles VC state in a nice way.
3021 (condition-case nil
3022 (let ((state (vc-state (concat directory name))))
3023 (not (or (eq 'up-to-date state)
3024 (null state))))
3025 ;; An error means not in a VC system
3026 (error nil))
3027 (or
3028 ;; RCS file name
3029 (file-exists-p (concat directory "RCS/" name ",v"))
3030 (file-exists-p (concat directory "RCS/" name))
3031 ;; Local SCCS file name
3032 (file-exists-p (concat directory "SCCS/s." name))
3033 ;; Remote SCCS file name
3034 (let ((proj-dir (getenv "PROJECTDIR")))
3035 (if proj-dir
3036 (file-exists-p (concat proj-dir "/SCCS/s." name))
3037 nil))))
3038 ;; User extension
3039 (run-hook-with-args 'speedbar-vc-in-control-hook directory name)
3040 ))
3041
3042 ;; Objet File scanning
3043 (defun speedbar-check-objects ()
3044 "Scan all files in a directory, and for each see if there is an object.
3045 See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
3046 to add more object types."
3047 ;; Check for to-do to be reset. If reset but no RCS is available
3048 ;; then set to nil (do nothing) otherwise, start at the beginning
3049 (save-excursion
3050 (if speedbar-buffer (set-buffer speedbar-buffer))
3051 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
3052 (setq speedbar-obj-to-do-point 0))
3053 (if (numberp speedbar-obj-to-do-point)
3054 (progn
3055 (goto-char speedbar-obj-to-do-point)
3056 (while (and (not (input-pending-p))
3057 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
3058 nil t))
3059 (setq speedbar-obj-to-do-point (point))
3060 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
3061 (if (not ind) (setq ind " "))
3062 (speedbar-add-indicator ind (concat
3063 (car speedbar-obj-indicator)
3064 "\\|"
3065 (cdr speedbar-obj-indicator)))))
3066 (if (input-pending-p)
3067 ;; return that we are incomplete
3068 nil
3069 ;; we are done, set to-do to nil
3070 (setq speedbar-obj-to-do-point nil)
3071 ;; and return t
3072 t))
3073 t)))
3074
3075 (defun speedbar-check-obj-this-line (depth)
3076 "Return t if the file on this line has an associated object.
3077 Parameter DEPTH is a string with the current depth of indentation of
3078 the file being checked."
3079 (let* ((d (string-to-number depth))
3080 (f (speedbar-line-directory d))
3081 (fn (buffer-substring-no-properties
3082 ;; Skip-chars: thanks ptype@dra.hmg.gb
3083 (point) (progn
3084 (skip-chars-forward "^ "
3085 (save-excursion (end-of-line)
3086 (point)))
3087 (point))))
3088 (fulln (concat f fn)))
3089 (if (<= 2 speedbar-verbosity-level)
3090 (speedbar-message "Speedbar obj check...%s" fulln))
3091 (let ((oa speedbar-obj-alist))
3092 (while (and oa (not (string-match (car (car oa)) fulln)))
3093 (setq oa (cdr oa)))
3094 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
3095 (cdr (car oa))))))
3096 nil
3097 ;; Find out if the object is out of date or not.
3098 (let ((date1 (nth 5 (file-attributes fulln)))
3099 (date2 (nth 5 (file-attributes (concat
3100 (file-name-sans-extension fulln)
3101 (cdr (car oa)))))))
3102 (if (or (< (car date1) (car date2))
3103 (and (= (car date1) (car date2))
3104 (< (nth 1 date1) (nth 1 date2))))
3105 (car speedbar-obj-indicator)
3106 (cdr speedbar-obj-indicator)))))))
3107 \f
3108 ;;; Clicking Activity
3109 ;;
3110 (defun speedbar-position-cursor-on-line ()
3111 "Position the cursor on a line."
3112 (let ((oldpos (point)))
3113 (beginning-of-line)
3114 (if (looking-at "[0-9]+:\\s-*..?.? ")
3115 (goto-char (1- (match-end 0)))
3116 (goto-char oldpos))))
3117
3118 (defun speedbar-click (e)
3119 "Activate any speedbar buttons where the mouse is clicked.
3120 This must be bound to a mouse event. A button is any location of text
3121 with a mouse face that has a text property called `speedbar-function'.
3122 Argument E is the click event."
3123 ;; Backward compatibility let statement.
3124 (let ((speedbar-power-click dframe-power-click))
3125 (speedbar-do-function-pointer))
3126 (dframe-quick-mouse e))
3127
3128 (defun speedbar-do-function-pointer ()
3129 "Look under the cursor and examine the text properties.
3130 From this extract the file/tag name, token, indentation level and call
3131 a function if appropriate."
3132 (let* ((speedbar-frame (speedbar-current-frame))
3133 (fn (get-text-property (point) 'speedbar-function))
3134 (tok (get-text-property (point) 'speedbar-token))
3135 ;; The 1-,+ is safe because scaning starts AFTER the point
3136 ;; specified. This lets the search include the character the
3137 ;; cursor is on.
3138 (tp (previous-single-property-change
3139 (1+ (point)) 'speedbar-function))
3140 (np (next-single-property-change
3141 (point) 'speedbar-function))
3142 (txt (buffer-substring-no-properties (or tp (point-min))
3143 (or np (point-max))))
3144 (dent (save-excursion (beginning-of-line)
3145 (string-to-number
3146 (if (looking-at "[0-9]+")
3147 (buffer-substring-no-properties
3148 (match-beginning 0) (match-end 0))
3149 "0")))))
3150 ;;(speedbar-message "%S:%S:%S:%s" fn tok txt dent)
3151 (and fn (funcall fn txt tok dent)))
3152 (speedbar-position-cursor-on-line))
3153 \f
3154 ;;; Reading info from the speedbar buffer
3155 ;;
3156 (defun speedbar-line-text (&optional p)
3157 "Retrieve the text after prefix junk for the current line.
3158 Optional argument P is where to start the search from."
3159 (save-excursion
3160 (if p (goto-char p))
3161 (beginning-of-line)
3162 (if (looking-at (concat
3163 "\\([0-9]+\\): *[[<{]?[-+?= ][]>}@()|] \\([^ \n]+\\)"))
3164 (get-text-property (match-beginning 2) 'speedbar-text)
3165 nil)))
3166
3167 (defun speedbar-line-token (&optional p)
3168 "Retrieve the token information after the prefix junk for the current line.
3169 Optional argument P is where to start the search from."
3170 (save-excursion
3171 (if p (goto-char p))
3172 (beginning-of-line)
3173 (if (looking-at (concat
3174 "\\([0-9]+\\): *[[<{]?[-+?= ][]>}@()|] \\([^ \n]+\\)\\("
3175 speedbar-indicator-regex "\\)?"))
3176 (progn
3177 (goto-char (match-beginning 2))
3178 (get-text-property (point) 'speedbar-token))
3179 nil)))
3180
3181 (defun speedbar-line-file (&optional p)
3182 "Retrieve the file or whatever from the line at point P.
3183 The return value is a string representing the file. If it is a
3184 directory, then it is the directory name."
3185 (save-match-data
3186 (save-restriction
3187 (widen)
3188 (let ((f (speedbar-line-text p)))
3189 (if f
3190 (let* ((depth (string-to-number (match-string 1)))
3191 (directory (speedbar-line-directory depth)))
3192 (if (file-exists-p (concat directory f))
3193 (concat directory f)
3194 nil))
3195 nil)))))
3196
3197 (defun speedbar-goto-this-file (file)
3198 "If FILE is displayed, go to this line and return t.
3199 Otherwise do not move and return nil."
3200 (let ((directory (substring (file-name-directory (expand-file-name file))
3201 (length (expand-file-name default-directory))))
3202 (dest (point)))
3203 (save-match-data
3204 (goto-char (point-min))
3205 ;; scan all the directories
3206 (while (and directory (not (eq directory t)))
3207 (if (string-match "^[/\\]?\\([^/\\]+\\)" directory)
3208 (let ((pp (match-string 1 directory)))
3209 (if (save-match-data
3210 (re-search-forward (concat "> " (regexp-quote pp) "$")
3211 nil t))
3212 (setq directory (substring directory (match-end 1)))
3213 (setq directory nil)))
3214 (setq directory t)))
3215 ;; find the file part
3216 (if (or (not directory) (string= (file-name-nondirectory file) ""))
3217 ;; only had a dir part
3218 (if directory
3219 (progn
3220 (speedbar-position-cursor-on-line)
3221 t)
3222 (goto-char dest) nil)
3223 ;; find the file part
3224 (let ((nd (file-name-nondirectory file)))
3225 (if (re-search-forward
3226 (concat "] \\(" (regexp-quote nd)
3227 "\\)\\(" speedbar-indicator-regex "\\)$")
3228 nil t)
3229 (progn
3230 (speedbar-position-cursor-on-line)
3231 t)
3232 (goto-char dest)
3233 nil))))))
3234
3235 (defun speedbar-line-directory (&optional depth)
3236 "Retrieve the directory name associated with the current line.
3237 This may require traversing backwards from DEPTH and combining the default
3238 directory with these items. This function is replaceable in
3239 `speedbar-mode-functions-list' as `speedbar-line-directory'."
3240 (save-restriction
3241 (widen)
3242 (let ((rf (speedbar-fetch-replacement-function 'speedbar-line-directory)))
3243 (if rf (funcall rf depth) default-directory))))
3244
3245 (defun speedbar-files-line-directory (&optional depth)
3246 "Retrieve the directoryname associated with the current line.
3247 This may require traversing backwards from DEPTH and combining the default
3248 directory with these items."
3249 (save-excursion
3250 (save-match-data
3251 (if (not depth)
3252 (progn
3253 (beginning-of-line)
3254 (looking-at "^\\([0-9]+\\):")
3255 (setq depth (string-to-number (match-string 1)))))
3256 (let ((directory nil))
3257 (setq depth (1- depth))
3258 (while (/= depth -1)
3259 (if (not (re-search-backward (format "^%d:" depth) nil t))
3260 (error "Error building filename of tag")
3261 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)")
3262 (setq directory (concat (speedbar-line-text)
3263 "/"
3264 directory)))
3265 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)")
3266 ;; This is the start of our directory.
3267 (setq directory (speedbar-line-text)))))
3268 (setq depth (1- depth)))
3269 (if (and directory
3270 (string-match (concat speedbar-indicator-regex "$")
3271 directory))
3272 (setq directory (substring directory 0 (match-beginning 0))))
3273 (concat default-directory directory)))))
3274
3275 (defun speedbar-directory-line (directory)
3276 "Position the cursor on the line specified by DIRECTORY."
3277 (save-match-data
3278 (if (string-match "[/\\]$" directory)
3279 (setq directory (substring directory 0 (match-beginning 0))))
3280 (let ((nomatch t) (depth 0)
3281 (fname (file-name-nondirectory directory))
3282 (pname (file-name-directory directory)))
3283 (if (not (member pname speedbar-shown-directories))
3284 (error "Internal Error: File %s not shown in speedbar" directory))
3285 (goto-char (point-min))
3286 (while (and nomatch
3287 (re-search-forward
3288 (concat "[]>] \\(" (regexp-quote fname)
3289 "\\)\\(" speedbar-indicator-regex "\\)?$")
3290 nil t))
3291 (beginning-of-line)
3292 (looking-at "\\([0-9]+\\):")
3293 (setq depth (string-to-number (match-string 0))
3294 nomatch (not (string= pname (speedbar-line-directory depth))))
3295 (end-of-line))
3296 (beginning-of-line)
3297 (not nomatch))))
3298
3299 (defun speedbar-edit-line ()
3300 "Edit whatever tag or file is on the current speedbar line."
3301 (interactive)
3302 (or (save-excursion
3303 (beginning-of-line)
3304 ;; If this fails, then it is a non-standard click, and as such,
3305 ;; perfectly allowed.
3306 (if (re-search-forward "[]>?}] [^ ]"
3307 (save-excursion (end-of-line) (point))
3308 t)
3309 (progn
3310 (forward-char -1)
3311 (speedbar-do-function-pointer))
3312 nil))
3313 (speedbar-do-function-pointer)))
3314
3315 (defun speedbar-expand-line (&optional arg)
3316 "Expand the line under the cursor.
3317 With universal argument ARG, flush cached data."
3318 (interactive "P")
3319 (beginning-of-line)
3320 (let* ((dframe-power-click arg)
3321 (speedbar-power-click arg))
3322 (condition-case nil
3323 (progn
3324 (re-search-forward ":\\s-*.\\+. "
3325 (save-excursion (end-of-line) (point)))
3326 (forward-char -2)
3327 (speedbar-do-function-pointer))
3328 (error (speedbar-position-cursor-on-line)))))
3329
3330 (defun speedbar-flush-expand-line ()
3331 "Expand the line under the cursor and flush any cached information."
3332 (interactive)
3333 (speedbar-expand-line 1))
3334
3335 (defun speedbar-contract-line ()
3336 "Contract the line under the cursor."
3337 (interactive)
3338 (beginning-of-line)
3339 (condition-case nil
3340 (progn
3341 (re-search-forward ":\\s-*.-. "
3342 (save-excursion (end-of-line) (point)))
3343 (forward-char -2)
3344 (speedbar-do-function-pointer))
3345 (error (speedbar-position-cursor-on-line))))
3346
3347 (defun speedbar-toggle-line-expansion ()
3348 "Contract or expand the line under the cursor."
3349 (interactive)
3350 (beginning-of-line)
3351 (condition-case nil
3352 (progn
3353 (re-search-forward ":\\s-*.[-+]. "
3354 (save-excursion (end-of-line) (point)))
3355 (forward-char -2)
3356 (speedbar-do-function-pointer))
3357 (error (speedbar-position-cursor-on-line))))
3358
3359 (defun speedbar-expand-line-descendants (&optional arg)
3360 "Expand the line under the cursor and all descendants.
3361 Optional argument ARG indicates that any cache should be flushed."
3362 (interactive "P")
3363 (speedbar-expand-line arg)
3364 ;; Now, inside the area expaded here, expand all subnodes of
3365 ;; the same descendant type.
3366 (save-excursion
3367 (speedbar-next 1) ;; Move into the list.
3368 (let ((err nil))
3369 (while (not err)
3370 (condition-case nil
3371 (progn
3372 (speedbar-expand-line-descendants arg)
3373 (speedbar-restricted-next 1))
3374 (error (setq err t))))))
3375 )
3376
3377 (defun speedbar-contract-line-descendants ()
3378 "Expand the line under the cursor and all descendants."
3379 (interactive)
3380 (speedbar-contract-line)
3381 ;; Don't need to do anything else since all descendants are
3382 ;; hidden by default anyway. Yay! It's easy.
3383 )
3384
3385 (defun speedbar-find-file (text token indent)
3386 "Speedbar click handler for filenames.
3387 TEXT, the file will be displayed in the attached frame.
3388 TOKEN is unused, but required by the click handler. INDENT is the
3389 current indentation level."
3390 (let ((cdd (speedbar-line-directory indent)))
3391 ;; Run before visiting file hook here.
3392 (let ((f (selected-frame)))
3393 (dframe-select-attached-frame speedbar-frame)
3394 (run-hooks 'speedbar-before-visiting-file-hook)
3395 (select-frame f))
3396 (speedbar-find-file-in-frame (concat cdd text))
3397 (speedbar-stealthy-updates)
3398 (run-hooks 'speedbar-visiting-file-hook)
3399 ;; Reset the timer with a new timeout when cliking a file
3400 ;; in case the user was navigating directories, we can cancel
3401 ;; that other timer.
3402 (speedbar-set-timer dframe-update-speed))
3403 (dframe-maybee-jump-to-attached-frame))
3404
3405 (defun speedbar-dir-follow (text token indent)
3406 "Speedbar click handler for directory names.
3407 Clicking a directory will cause the speedbar to list files in
3408 the subdirectory TEXT. TOKEN is an unused requirement. The
3409 subdirectory chosen will be at INDENT level."
3410 (setq default-directory
3411 (concat (expand-file-name (concat (speedbar-line-directory indent) text))
3412 "/"))
3413 ;; Because we leave speedbar as the current buffer,
3414 ;; update contents will change directory without
3415 ;; having to touch the attached frame. Turn off smart expand just
3416 ;; in case.
3417 (let ((speedbar-smart-directory-expand-flag nil))
3418 (speedbar-update-contents))
3419 (speedbar-set-timer speedbar-navigating-speed)
3420 (setq speedbar-last-selected-file nil)
3421 (speedbar-stealthy-updates))
3422
3423 (defun speedbar-delete-subblock (indent)
3424 "Delete text from point to indentation level INDENT or greater.
3425 Handles end-of-sublist smartly."
3426 (speedbar-with-writable
3427 (save-excursion
3428 (end-of-line) (forward-char 1)
3429 (let ((start (point)))
3430 (while (and (looking-at "^\\([0-9]+\\):")
3431 (> (string-to-number (match-string 1)) indent)
3432 (not (eobp)))
3433 (forward-line 1)
3434 (beginning-of-line))
3435 (delete-region start (point))))))
3436
3437 (defun speedbar-dired (text token indent)
3438 "Speedbar click handler for directory expand button.
3439 Clicking this button expands or contracts a directory. TEXT is the
3440 button clicked which has either a + or -. TOKEN is the directory to be
3441 expanded. INDENT is the current indentation level."
3442 (cond ((string-match "+" text) ;we have to expand this dir
3443 (setq speedbar-shown-directories
3444 (cons (expand-file-name
3445 (concat (speedbar-line-directory indent) token "/"))
3446 speedbar-shown-directories))
3447 (speedbar-change-expand-button-char ?-)
3448 (speedbar-reset-scanners)
3449 (save-excursion
3450 (end-of-line) (forward-char 1)
3451 (speedbar-with-writable
3452 (speedbar-default-directory-list
3453 (concat (speedbar-line-directory indent) token "/")
3454 (1+ indent)))))
3455 ((string-match "-" text) ;we have to contract this node
3456 (speedbar-reset-scanners)
3457 (let ((oldl speedbar-shown-directories)
3458 (newl nil)
3459 (td (expand-file-name
3460 (concat (speedbar-line-directory indent) token))))
3461 (while oldl
3462 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3463 (setq newl (cons (car oldl) newl)))
3464 (setq oldl (cdr oldl)))
3465 (setq speedbar-shown-directories (nreverse newl)))
3466 (speedbar-change-expand-button-char ?+)
3467 (speedbar-delete-subblock indent)
3468 )
3469 (t (error "Ooops... not sure what to do")))
3470 (speedbar-center-buffer-smartly)
3471 (save-excursion (speedbar-stealthy-updates)))
3472
3473 (defun speedbar-directory-buttons-follow (text token indent)
3474 "Speedbar click handler for default directory buttons.
3475 TEXT is the button clicked on. TOKEN is the directory to follow.
3476 INDENT is the current indentation level and is unused."
3477 (if (string-match "^[A-z]:$" token)
3478 (setq default-directory (concat token "/"))
3479 (setq default-directory token))
3480 ;; Because we leave speedbar as the current buffer,
3481 ;; update contents will change directory without
3482 ;; having to touch the attached frame.
3483 (speedbar-update-contents)
3484 (speedbar-set-timer speedbar-navigating-speed))
3485
3486 (defun speedbar-tag-file (text token indent)
3487 "The cursor is on a selected line. Expand the tags in the specified file.
3488 The parameter TEXT and TOKEN are required, where TEXT is the button
3489 clicked, and TOKEN is the file to expand. INDENT is the current
3490 indentation level."
3491 (cond ((string-match "+" text) ;we have to expand this file
3492 (let* ((fn (expand-file-name (concat (speedbar-line-directory indent)
3493 token)))
3494 (mode nil)
3495 (lst (speedbar-fetch-dynamic-tags fn)))
3496 ;; if no list, then remove expando button
3497 (if (not lst)
3498 (speedbar-change-expand-button-char ??)
3499 (speedbar-change-expand-button-char ?-)
3500 (speedbar-with-writable
3501 (save-excursion
3502 (end-of-line) (forward-char 1)
3503 (funcall (car lst) indent (cdr lst)))))))
3504 ((string-match "-" text) ;we have to contract this node
3505 (speedbar-change-expand-button-char ?+)
3506 (speedbar-delete-subblock indent))
3507 (t (error "Ooops... not sure what to do")))
3508 (speedbar-center-buffer-smartly))
3509
3510 (defun speedbar-tag-find (text token indent)
3511 "For the tag TEXT in a file TOKEN, go to that position.
3512 INDENT is the current indentation level."
3513 (let ((file (speedbar-line-directory indent)))
3514 (let ((f (selected-frame)))
3515 (dframe-select-attached-frame speedbar-frame)
3516 (run-hooks 'speedbar-before-visiting-tag-hook)
3517 (select-frame f))
3518 (speedbar-find-file-in-frame file)
3519 (save-excursion (speedbar-stealthy-updates))
3520 ;; Reset the timer with a new timeout when cliking a file
3521 ;; in case the user was navigating directories, we can cancel
3522 ;; that other timer.
3523 (speedbar-set-timer dframe-update-speed)
3524 (goto-char token)
3525 (run-hooks 'speedbar-visiting-tag-hook)
3526 (dframe-maybee-jump-to-attached-frame)
3527 ))
3528
3529 (defun speedbar-tag-expand (text token indent)
3530 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3531 Etags does not support this feature. TEXT will be the button
3532 string. TOKEN will be the list, and INDENT is the current indentation
3533 level."
3534 (cond ((string-match "+" text) ;we have to expand this file
3535 (speedbar-change-expand-button-char ?-)
3536 (speedbar-with-writable
3537 (save-excursion
3538 (end-of-line) (forward-char 1)
3539 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
3540 'speedbar-tag-find))))
3541 ((string-match "-" text) ;we have to contract this node
3542 (speedbar-change-expand-button-char ?+)
3543 (speedbar-delete-subblock indent))
3544 (t (error "Ooops... not sure what to do")))
3545 (speedbar-center-buffer-smartly))
3546 \f
3547 ;;; Loading files into the attached frame.
3548 ;;
3549 (defcustom speedbar-select-frame-method 'attached
3550 "*Specify how to select a frame for displaying a file.
3551 A value of 'attached means to use the attached frame (the frame
3552 that speedbar was started from.) A number such as 1 or -1 means to
3553 pass that number to `other-frame' while selecting a frame from speedbar."
3554 :group 'speedbar
3555 :type 'sexp)
3556
3557 (defun speedbar-find-file-in-frame (file)
3558 "This will load FILE into the speedbar attached frame.
3559 If the file is being displayed in a different frame already, then raise that
3560 frame instead."
3561 (let* ((buff (find-file-noselect file))
3562 (bwin (get-buffer-window buff 0)))
3563 (if bwin
3564 (progn
3565 (select-window bwin)
3566 (raise-frame (window-frame bwin)))
3567 (if dframe-power-click
3568 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3569 (if (numberp speedbar-select-frame-method)
3570 (other-frame speedbar-select-frame-method)
3571 (dframe-select-attached-frame speedbar-frame))
3572 (switch-to-buffer buff))))
3573 )
3574
3575 ;;; Centering Utility
3576 ;;
3577 (defun speedbar-center-buffer-smartly ()
3578 "Recenter a speedbar buffer so the current indentation level is all visible.
3579 This assumes that the cursor is on a file, or tag of a file which the user is
3580 interested in."
3581
3582 (save-selected-window
3583
3584 (select-window (get-buffer-window speedbar-buffer t))
3585
3586 (set-buffer speedbar-buffer)
3587
3588 (if (<= (count-lines (point-min) (point-max))
3589 (1- (window-height (selected-window))))
3590 ;; whole buffer fits
3591 (let ((cp (point)))
3592
3593 (goto-char (point-min))
3594 (recenter 0)
3595 (goto-char cp))
3596 ;; too big
3597 (let (depth start end exp p)
3598 (save-excursion
3599 (beginning-of-line)
3600 (setq depth (if (looking-at "[0-9]+")
3601 (string-to-number (buffer-substring-no-properties
3602 (match-beginning 0) (match-end 0)))
3603 0))
3604 (setq exp (format "^%d:" depth)))
3605 (save-excursion
3606 (end-of-line)
3607 (if (re-search-backward exp nil t)
3608 (setq start (point))
3609 (setq start (point-min)))
3610 (save-excursion ;Not sure about this part.
3611 (end-of-line)
3612 (setq p (point))
3613 (while (and (not (re-search-forward exp nil t))
3614 (>= depth 0))
3615 (setq depth (1- depth))
3616 (setq exp (format "^%d:" depth)))
3617 (if (/= (point) p)
3618 (setq end (point))
3619 (setq end (point-max)))))
3620 ;; Now work out the details of centering
3621 (let ((nl (count-lines start end))
3622 (wl (1- (window-height (selected-window))))
3623 (cp (point)))
3624 (if (> nl wl)
3625 ;; We can't fit it all, so just center on cursor
3626 (progn (goto-char start)
3627 (recenter 1))
3628 ;; we can fit everything on the screen, but...
3629 (if (and (pos-visible-in-window-p start (selected-window))
3630 (pos-visible-in-window-p end (selected-window)))
3631 ;; we are all set!
3632 nil
3633 ;; we need to do something...
3634 (goto-char start)
3635 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3636 (lte (count-lines start (point-max))))
3637 (if (and (< (+ newcent lte) (window-height (selected-window)))
3638 (> (- (window-height (selected-window)) lte 1)
3639 newcent))
3640 (setq newcent (- (window-height (selected-window))
3641 lte 1)))
3642 (recenter newcent))))
3643 (goto-char cp))))))
3644 \f
3645 ;;; Tag Management -- List of expanders:
3646 ;;
3647 (defun speedbar-fetch-dynamic-tags (file)
3648 "Return a list of tags generated dynamically from FILE.
3649 This uses the entries in `speedbar-dynamic-tags-function-list'
3650 to find the proper tags. It is up to each of those individual
3651 functions to do caching and flushing if appropriate."
3652 (save-excursion
3653 ;; If a file is in memory, switch to that buffer. This allows
3654 ;; us to use the local variable. If the file is on disk, we
3655 ;; can try a few of the defaults that can get tags without
3656 ;; opening the file.
3657 (if (get-file-buffer file)
3658 (set-buffer (get-file-buffer file)))
3659 ;; If there is a buffer-local value of
3660 ;; speedbar-dynamic-tags-function-list, it will now be available.
3661 (let ((dtf speedbar-dynamic-tags-function-list)
3662 (ret t))
3663 (while (and (eq ret t) dtf)
3664 (setq ret
3665 (if (fboundp (car (car dtf)))
3666 (funcall (car (car dtf)) file)
3667 t))
3668 (if (eq ret t)
3669 (setq dtf (cdr dtf))))
3670 (if (eq ret t)
3671 ;; No valid tag list, return nil
3672 nil
3673 ;; We have some tags. Return the list with the insert fn
3674 ;; prepended
3675 (cons (cdr (car dtf)) ret)))))
3676
3677 ;;; Tag Management -- Imenu
3678 ;;
3679 (if (not speedbar-use-imenu-flag)
3680
3681 nil
3682
3683 (eval-when-compile (condition-case nil (require 'imenu) (error nil)))
3684
3685 (defun speedbar-fetch-dynamic-imenu (file)
3686 "Load FILE into a buffer, and generate tags using Imenu.
3687 Returns the tag list, or t for an error."
3688 ;; Load this AND compile it in
3689 (require 'imenu)
3690 (set-buffer (find-file-noselect file))
3691 (if dframe-power-click (setq imenu--index-alist nil))
3692 (condition-case nil
3693 (let ((index-alist (imenu--make-index-alist t)))
3694 (if speedbar-sort-tags
3695 (sort (copy-alist index-alist)
3696 (lambda (a b) (string< (car a) (car b))))
3697 index-alist))
3698 (error t)))
3699 )
3700 \f
3701 ;;; Tag Management -- etags (old XEmacs compatibility part)
3702 ;;
3703 (defvar speedbar-fetch-etags-parse-list
3704 '(;; Note that java has the same parse-group as c
3705 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\|cxx\\|hxx\\)\\'" .
3706 speedbar-parse-c-or-c++tag)
3707 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3708 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
3709 ; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3710 ; speedbar-parse-fortran77-tag)
3711 ("\\.tex\\'" . speedbar-parse-tex-string)
3712 ("\\.p\\'" .
3713 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3714 )
3715 "Associations of file extensions and expressions for extracting tags.
3716 To add a new file type, you would want to add a new association to the
3717 list, where the car is the file match, and the cdr is the way to
3718 extract an element from the tags output. If the output is complex,
3719 use a function symbol instead of regexp. The function should expect
3720 to be at the beginning of a line in the etags buffer.
3721
3722 This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3723
3724 (defvar speedbar-fetch-etags-command "etags"
3725 "*Command used to create an etags file.
3726
3727 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3728
3729 (defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3730 "*List of arguments to use with `speedbar-fetch-etags-command'.
3731 This creates an etags output buffer. Use `speedbar-toggle-etags' to
3732 modify this list conveniently.
3733
3734 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3735
3736 (defun speedbar-toggle-etags (flag)
3737 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3738 FLAG then becomes a member of etags command line arguments. If flag
3739 is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3740 value is \"show\" then toggle the value of
3741 `speedbar-show-unknown-files'.
3742
3743 This function is a convenience function for XEmacs menu created by
3744 Farzin Guilak <farzin@protocol.com>."
3745 (interactive)
3746 (cond
3747 ((equal flag "sort")
3748 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3749 ((equal flag "show")
3750 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3751 ((or (equal flag "-C")
3752 (equal flag "-S")
3753 (equal flag "-D"))
3754 (if (member flag speedbar-fetch-etags-arguments)
3755 (setq speedbar-fetch-etags-arguments
3756 (delete flag speedbar-fetch-etags-arguments))
3757 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3758 (t nil)))
3759
3760 (defun speedbar-fetch-dynamic-etags (file)
3761 "For FILE, run etags and create a list of symbols extracted.
3762 Each symbol will be associated with its line position in FILE."
3763 (let ((newlist nil))
3764 (unwind-protect
3765 (save-excursion
3766 (if (get-buffer "*etags tmp*")
3767 (kill-buffer "*etags tmp*")) ;kill to clean it up
3768 (if (<= 1 speedbar-verbosity-level)
3769 (speedbar-message "Fetching etags..."))
3770 (set-buffer (get-buffer-create "*etags tmp*"))
3771 (apply 'call-process speedbar-fetch-etags-command nil
3772 (current-buffer) nil
3773 (append speedbar-fetch-etags-arguments (list file)))
3774 (goto-char (point-min))
3775 (if (<= 1 speedbar-verbosity-level)
3776 (speedbar-message "Fetching etags..."))
3777 (let ((expr
3778 (let ((exprlst speedbar-fetch-etags-parse-list)
3779 (ans nil))
3780 (while (and (not ans) exprlst)
3781 (if (string-match (car (car exprlst)) file)
3782 (setq ans (car exprlst)))
3783 (setq exprlst (cdr exprlst)))
3784 (cdr ans))))
3785 (if expr
3786 (let (tnl)
3787 (set-buffer (get-buffer-create "*etags tmp*"))
3788 (while (not (save-excursion (end-of-line) (eobp)))
3789 (save-excursion
3790 (setq tnl (speedbar-extract-one-symbol expr)))
3791 (if tnl (setq newlist (cons tnl newlist)))
3792 (forward-line 1)))
3793 (speedbar-message
3794 "Sorry, no support for a file of that extension"))))
3795 )
3796 (if speedbar-sort-tags
3797 (sort newlist (lambda (a b) (string< (car a) (car b))))
3798 (reverse newlist))))
3799
3800 ;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3801 ;; sure it's needed with the different sorting method.
3802 ;;
3803 ;(defun speedbar-clean-etags()
3804 ; "Removes spaces before the ^? character, and removes `#define',
3805 ;return types, etc. preceding tags. This ensures that the sort operation
3806 ;works on the tags, not the return types."
3807 ; (save-excursion
3808 ; (goto-char (point-min))
3809 ; (while
3810 ; (re-search-forward "(?[ \t](?\C-?" nil t)
3811 ; (replace-match "\C-?" nil nil))
3812 ; (goto-char (point-min))
3813 ; (while
3814 ; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3815 ; (delete-region (match-beginning 1) (match-end 1)))))
3816
3817 (defun speedbar-extract-one-symbol (expr)
3818 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
3819 The line should contain output from etags. Parse the output using the
3820 regular expression EXPR."
3821 (let* ((sym (if (stringp expr)
3822 (if (save-excursion
3823 (re-search-forward expr (save-excursion
3824 (end-of-line)
3825 (point)) t))
3826 (buffer-substring-no-properties (match-beginning 1)
3827 (match-end 1)))
3828 (funcall expr)))
3829 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3830 (save-excursion
3831 (end-of-line)
3832 (point))
3833 t)))
3834 (if (and j sym)
3835 (1+ (string-to-number (buffer-substring-no-properties
3836 (match-beginning 2)
3837 (match-end 2))))
3838 0))))
3839 (if (/= pos 0)
3840 (cons sym pos)
3841 nil)))
3842
3843 (defun speedbar-parse-c-or-c++tag ()
3844 "Parse a C or C++ tag, which tends to be a little complex."
3845 (save-excursion
3846 (let ((bound (save-excursion (end-of-line) (point))))
3847 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3848 (buffer-substring-no-properties (match-beginning 1)
3849 (match-end 1)))
3850 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3851 (buffer-substring-no-properties (match-beginning 1)
3852 (match-end 1)))
3853 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3854 (buffer-substring-no-properties (match-beginning 1)
3855 (match-end 1)))
3856 (t nil))
3857 )))
3858
3859 (defun speedbar-parse-tex-string ()
3860 "Parse a Tex string. Only find data which is relevant."
3861 (save-excursion
3862 (let ((bound (save-excursion (end-of-line) (point))))
3863 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3864 (buffer-substring-no-properties (match-beginning 0)
3865 (match-end 0)))
3866 (t nil)))))
3867
3868 \f
3869 ;;; BUFFER DISPLAY mode.
3870 ;;
3871 (defvar speedbar-buffers-key-map nil
3872 "Keymap used when in the buffers display mode.")
3873
3874 (if speedbar-buffers-key-map
3875 nil
3876 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3877
3878 ;; Basic tree features
3879 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3880 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3881 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3882 (define-key speedbar-buffers-key-map "=" 'speedbar-expand-line)
3883 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3884 (define-key speedbar-buffers-key-map " " 'speedbar-toggle-line-expansion)
3885
3886 ;; Buffer specific keybindings
3887 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3888 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3889
3890 )
3891
3892 (defvar speedbar-buffer-easymenu-definition
3893 '(["Jump to buffer" speedbar-edit-line t]
3894 ["Expand File Tags" speedbar-expand-line
3895 (save-excursion (beginning-of-line)
3896 (looking-at "[0-9]+: *.\\+. "))]
3897 ["Flush Cache & Expand" speedbar-flush-expand-line
3898 (save-excursion (beginning-of-line)
3899 (looking-at "[0-9]+: *.\\+. "))]
3900 ["Contract File Tags" speedbar-contract-line
3901 (save-excursion (beginning-of-line)
3902 (looking-at "[0-9]+: *.-. "))]
3903 "----"
3904 ["Kill Buffer" speedbar-buffer-kill-buffer
3905 (save-excursion (beginning-of-line)
3906 (looking-at "[0-9]+: *.[-+?]. "))]
3907 ["Revert Buffer" speedbar-buffer-revert-buffer
3908 (save-excursion (beginning-of-line)
3909 (looking-at "[0-9]+: *.[-+?]. "))]
3910 )
3911 "Menu item elements shown when displaying a buffer list.")
3912
3913 (defun speedbar-buffer-buttons (directory zero)
3914 "Create speedbar buttons based on the buffers currently loaded.
3915 DIRECTORY is the directory to the currently active buffer, and ZERO is 0."
3916 (speedbar-buffer-buttons-engine nil))
3917
3918 (defun speedbar-buffer-buttons-temp (directory zero)
3919 "Create speedbar buttons based on the buffers currently loaded.
3920 DIRECTORY is the directory to the currently active buffer, and ZERO is 0."
3921 (speedbar-buffer-buttons-engine t))
3922
3923 (defun speedbar-buffer-buttons-engine (temp)
3924 "Create speedbar buffer buttons.
3925 If TEMP is non-nil, then clicking on a buffer restores the previous display."
3926 (speedbar-insert-separator "Active Buffers:")
3927 (let ((bl (buffer-list))
3928 (case-fold-search t))
3929 (while bl
3930 (if (string-match "^[ *]" (buffer-name (car bl)))
3931 nil
3932 (let* ((known (string-match speedbar-file-regexp
3933 (buffer-name (car bl))))
3934 (expchar (if known ?+ ??))
3935 (fn (if known 'speedbar-tag-file nil))
3936 (fname (save-excursion (set-buffer (car bl))
3937 (buffer-file-name))))
3938 (speedbar-make-tag-line 'bracket expchar fn
3939 (if fname (file-name-nondirectory fname))
3940 (buffer-name (car bl))
3941 'speedbar-buffer-click temp
3942 'speedbar-file-face 0)
3943 (speedbar-buffers-tail-notes (car bl))))
3944 (setq bl (cdr bl)))
3945 (setq bl (buffer-list))
3946 (speedbar-insert-separator "Scratch Buffers:")
3947 (while bl
3948 (if (not (string-match "^\\*" (buffer-name (car bl))))
3949 nil
3950 (if (eq (car bl) speedbar-buffer)
3951 nil
3952 (speedbar-make-tag-line 'bracket ?? nil nil
3953 (buffer-name (car bl))
3954 'speedbar-buffer-click temp
3955 'speedbar-file-face 0)
3956 (speedbar-buffers-tail-notes (car bl))))
3957 (setq bl (cdr bl)))
3958 (setq bl (buffer-list))
3959 ;;(speedbar-insert-separator "Hidden Buffers:")
3960 ;;(while bl
3961 ;; (if (not (string-match "^ " (buffer-name (car bl))))
3962 ;; nil
3963 ;; (if (eq (car bl) speedbar-buffer)
3964 ;; nil
3965 ;; (speedbar-make-tag-line 'bracket ?? nil nil
3966 ;; (buffer-name (car bl))
3967 ;; 'speedbar-buffer-click temp
3968 ;; 'speedbar-file-face 0)
3969 ;; (speedbar-buffers-tail-notes (car bl))))
3970 ;; (setq bl (cdr bl)))
3971 ))
3972
3973 (defun speedbar-buffers-tail-notes (buffer)
3974 "Add a note to the end of the last tag line.
3975 Argument BUFFER is the buffer being tested."
3976 (let (mod ro)
3977 (save-excursion
3978 (set-buffer buffer)
3979 (setq mod (buffer-modified-p)
3980 ro buffer-read-only))
3981 (if ro (speedbar-insert-button "%" nil nil nil nil t))))
3982
3983 (defun speedbar-buffers-item-info ()
3984 "Display information about the current buffer on the current line."
3985 (or (speedbar-item-info-tag-helper)
3986 (let* ((item (speedbar-line-text))
3987 (buffer (if item (get-buffer item) nil)))
3988 (and buffer
3989 (speedbar-message "%s%s %S %d %s"
3990 (if (buffer-modified-p buffer) "* " "")
3991 item
3992 (save-excursion (set-buffer buffer) major-mode)
3993 (save-excursion (set-buffer buffer)
3994 (buffer-size))
3995 (or (buffer-file-name buffer) "<No file>"))))))
3996
3997 (defun speedbar-buffers-line-directory (&optional depth)
3998 "Fetch the full directory to the file (buffer) specified on the current line.
3999 Optional argument DEPTH specifies the current depth of the back search."
4000 (save-excursion
4001 (end-of-line)
4002 (let ((start (point)))
4003 ;; Buffers are always at level 0
4004 (if (not (re-search-backward "^0:" nil t))
4005 nil
4006 (let* ((bn (speedbar-line-text))
4007 (buffer (if bn (get-buffer bn))))
4008 (if buffer
4009 (if (save-excursion
4010 (end-of-line)
4011 (eq start (point)))
4012 (or (save-excursion (set-buffer buffer)
4013 default-directory)
4014 "")
4015 (buffer-file-name buffer))))))))
4016
4017 (defun speedbar-buffer-click (text token indent)
4018 "When the users clicks on a buffer-button in speedbar.
4019 TEXT is the buffer's name, TOKEN and INDENT are unused."
4020 (if dframe-power-click
4021 (let ((pop-up-frames t)) (select-window (display-buffer text)))
4022 (dframe-select-attached-frame speedbar-frame)
4023 (switch-to-buffer text)
4024 (if token (speedbar-change-initial-expansion-list
4025 speedbar-previously-used-expansion-list-name))))
4026
4027 (defun speedbar-buffer-kill-buffer ()
4028 "Kill the buffer the cursor is on in the speedbar buffer."
4029 (interactive)
4030 (or (save-excursion
4031 (let ((text (speedbar-line-text)))
4032 (if (and (get-buffer text)
4033 (speedbar-y-or-n-p (format "Kill buffer %s? " text)))
4034 (kill-buffer text))
4035 (speedbar-refresh)))))
4036
4037 (defun speedbar-buffer-revert-buffer ()
4038 "Revert the buffer the cursor is on in the speedbar buffer."
4039 (interactive)
4040 (save-excursion
4041 (beginning-of-line)
4042 ;; If this fails, then it is a non-standard click, and as such,
4043 ;; perfectly allowed
4044 (if (re-search-forward "[]>?}] [^ ]"
4045 (save-excursion (end-of-line) (point))
4046 t)
4047 (let ((text (progn
4048 (forward-char -1)
4049 (buffer-substring (point) (save-excursion
4050 (end-of-line)
4051 (point))))))
4052 (if (get-buffer text)
4053 (progn
4054 (set-buffer text)
4055 (revert-buffer t)))))))
4056
4057 \f
4058 ;;; Useful hook values and such.
4059 ;;
4060 (defvar speedbar-highlight-one-tag-line nil
4061 "Overlay used for highlighting the most recently jumped to tag line.")
4062
4063 (defun speedbar-highlight-one-tag-line ()
4064 "Highlight the current line, unhighlighting a previously jumped to line."
4065 (speedbar-unhighlight-one-tag-line)
4066 (setq speedbar-highlight-one-tag-line
4067 (speedbar-make-overlay (save-excursion (beginning-of-line) (point))
4068 (save-excursion (end-of-line)
4069 (forward-char 1)
4070 (point))))
4071 (speedbar-overlay-put speedbar-highlight-one-tag-line 'face
4072 'speedbar-highlight-face)
4073 (add-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line)
4074 )
4075
4076 (defun speedbar-unhighlight-one-tag-line ()
4077 "Unhighlight the currently highlighted line."
4078 (if speedbar-highlight-one-tag-line
4079 (progn
4080 (speedbar-delete-overlay speedbar-highlight-one-tag-line)
4081 (setq speedbar-highlight-one-tag-line nil)))
4082 (remove-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line))
4083
4084 (defun speedbar-recenter-to-top ()
4085 "Recenter the current buffer so POINT is on the top of the window."
4086 (recenter 1))
4087
4088 (defun speedbar-recenter ()
4089 "Recenter the current buffer so POINT is in the center of the window."
4090 (recenter (/ (window-height (selected-window)) 2)))
4091
4092 \f
4093 ;;; Color loading section.
4094 ;;
4095 (defface speedbar-button-face '((((class color) (background light))
4096 (:foreground "green4"))
4097 (((class color) (background dark))
4098 (:foreground "green3")))
4099 "Face used for +/- buttons."
4100 :group 'speedbar-faces)
4101
4102 (defface speedbar-file-face '((((class color) (background light))
4103 (:foreground "cyan4"))
4104 (((class color) (background dark))
4105 (:foreground "cyan"))
4106 (t (:bold t)))
4107 "Face used for file names."
4108 :group 'speedbar-faces)
4109
4110 (defface speedbar-directory-face '((((class color) (background light))
4111 (:foreground "blue4"))
4112 (((class color) (background dark))
4113 (:foreground "light blue")))
4114 "Face used for directory names."
4115 :group 'speedbar-faces)
4116 (defface speedbar-tag-face '((((class color) (background light))
4117 (:foreground "brown"))
4118 (((class color) (background dark))
4119 (:foreground "yellow")))
4120 "Face used for displaying tags."
4121 :group 'speedbar-faces)
4122
4123 (defface speedbar-selected-face '((((class color) (background light))
4124 (:foreground "red" :underline t))
4125 (((class color) (background dark))
4126 (:foreground "red" :underline t))
4127 (t (:underline t)))
4128 "Face used to underline the file in the active window."
4129 :group 'speedbar-faces)
4130
4131 (defface speedbar-highlight-face '((((class color) (background light))
4132 (:background "green"))
4133 (((class color) (background dark))
4134 (:background "sea green"))
4135 (((class grayscale monochrome)
4136 (background light))
4137 (:background "black"))
4138 (((class grayscale monochrome)
4139 (background dark))
4140 (:background "white")))
4141 "Face used for highlighting buttons with the mouse."
4142 :group 'speedbar-faces)
4143
4144 (defface speedbar-separator-face '((((class color) (background light))
4145 (:background "blue"
4146 :foreground "white"
4147 :overline "gray"))
4148 (((class color) (background dark))
4149 (:background "blue"
4150 :foreground "white"
4151 :overline "gray"))
4152 (((class grayscale monochrome)
4153 (background light))
4154 (:background "black"
4155 :foreground "white"
4156 :overline "white"))
4157 (((class grayscale monochrome)
4158 (background dark))
4159 (:background "white"
4160 :foreground "black"
4161 :overline "black")))
4162 "Face used for separator labes in a display."
4163 :group 'speedbar-faces)
4164
4165 ;; some edebug hooks
4166 (add-hook 'edebug-setup-hook
4167 (lambda ()
4168 (def-edebug-spec speedbar-with-writable def-body)))
4169
4170 ;; Fix a font lock problem for some versions of Emacs
4171 (if (boundp 'font-lock-global-modes)
4172 (if (listp font-lock-global-modes)
4173 (add-to-list 'font-lock-global-modes '(not speedbar-mode))
4174 )
4175 )
4176
4177 \f
4178 ;;; Obsolete variables and functions
4179
4180 (define-obsolete-variable-alias
4181 'speedbar-ignored-path-regexp 'speedbar-ignored-directory-regexp)
4182
4183 (define-obsolete-variable-alias 'speedbar-ignored-path-expressions
4184 'speedbar-ignored-directory-expressions)
4185
4186 (define-obsolete-function-alias 'speedbar-add-ignored-path-regexp
4187 'speedbar-add-ignored-directory-regexp)
4188
4189 (define-obsolete-function-alias 'speedbar-line-path
4190 'speedbar-line-directory)
4191
4192 (define-obsolete-function-alias 'speedbar-buffers-line-path
4193 'speedbar-buffers-line-directory)
4194
4195 (define-obsolete-function-alias 'speedbar-path-line
4196 'speedbar-directory-line)
4197
4198 (define-obsolete-function-alias 'speedbar-buffers-line-path
4199 'speedbar-buffers-line-directory)
4200
4201 (provide 'speedbar)
4202
4203 ;; run load-time hooks
4204 (run-hooks 'speedbar-load-hook)
4205
4206 ;; arch-tag: 4477e6d1-f78c-48b9-a503-387d3c9767d5
4207 ;;; speedbar ends here