]> code.delx.au - gnu-emacs/blob - lisp/speedbar.el
Fix typos.
[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 ["Close" dframe-close-frame t]
925 ["Quit" delete-frame t] ))
926 "Menu items appearing at the end of the speedbar menu.")
927
928 (defvar speedbar-desired-buffer nil
929 "Non-nil when speedbar is showing buttons specific to a special mode.
930 In this case it is the originating buffer.")
931 (defvar speedbar-buffer nil
932 "The buffer displaying the speedbar.")
933 (defvar speedbar-frame nil
934 "The frame displaying speedbar.")
935 (defvar speedbar-cached-frame nil
936 "The frame that was last created, then removed from the display.")
937 (defvar speedbar-full-text-cache nil
938 "The last open directory is saved in its entirety for ultra-fast switching.")
939
940 (defvar speedbar-last-selected-file nil
941 "The last file which was selected in speedbar buffer.")
942
943 (defvar speedbar-shown-directories nil
944 "Maintain list of directories simultaneously open in the current speedbar.")
945
946 (defvar speedbar-directory-contents-alist nil
947 "An association list of directories and their contents.
948 Each sublist was returned by `speedbar-file-lists'. This list is
949 maintained to speed up the refresh rate when switching between
950 directories.")
951
952 (defvar speedbar-power-click nil
953 "Never set this by hand. Value is t when S-mouse activity occurs.")
954
955 \f
956 ;;; Compatibility
957 ;;
958 (defalias 'speedbar-make-overlay
959 (if (featurep 'xemacs) 'make-extent 'make-overlay))
960
961 (defalias 'speedbar-overlay-put
962 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
963
964 (defalias 'speedbar-delete-overlay
965 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
966
967 (defalias 'speedbar-mode-line-update
968 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
969 \f
970 ;;; Mode definitions/ user commands
971 ;;
972
973 ;;;###autoload
974 (defalias 'speedbar 'speedbar-frame-mode)
975 ;;;###autoload
976 (defun speedbar-frame-mode (&optional arg)
977 "Enable or disable speedbar. Positive ARG means turn on, negative turn off.
978 nil means toggle. Once the speedbar frame is activated, a buffer in
979 `speedbar-mode' will be displayed. Currently, only one speedbar is
980 supported at a time.
981 `speedbar-before-popup-hook' is called before popping up the speedbar frame.
982 `speedbar-before-delete-hook' is called before the frame is deleted."
983 (interactive "P")
984 ;; Get the buffer to play with
985 (if (not (buffer-live-p speedbar-buffer))
986 (save-excursion
987 (setq speedbar-buffer (get-buffer-create " SPEEDBAR"))
988 (set-buffer speedbar-buffer)
989 (speedbar-mode)))
990 ;; Do the frame thing
991 (dframe-frame-mode arg
992 'speedbar-frame
993 'speedbar-cached-frame
994 'speedbar-buffer
995 "Speedbar"
996 #'speedbar-frame-mode
997 (if dframe-xemacsp
998 (append speedbar-frame-plist
999 ;; This is a hack to get speedbar to iconfiy
1000 ;; with the selected frame.
1001 (list 'parent (selected-frame)))
1002 speedbar-frame-parameters)
1003 speedbar-before-delete-hook
1004 speedbar-before-popup-hook
1005 speedbar-after-create-hook)
1006 ;; Start up the timer
1007 (if (not speedbar-frame)
1008 (speedbar-set-timer nil)
1009 (speedbar-reconfigure-keymaps)
1010 (speedbar-update-contents)
1011 (speedbar-set-timer dframe-update-speed)
1012 )
1013 ;; Frame modifications
1014 (set (make-local-variable 'dframe-delete-frame-function)
1015 'speedbar-handle-delete-frame)
1016 ;; hscroll
1017 (set (make-local-variable 'automatic-hscrolling) nil) ; Emacs 21
1018 ;; reset the selection variable
1019 (setq speedbar-last-selected-file nil))
1020
1021 (defun speedbar-frame-reposition-smartly ()
1022 "Reposition the speedbar frame to be next to the attached frame."
1023 (cond ((and dframe-xemacsp
1024 (or (member 'left speedbar-frame-plist)
1025 (member 'top speedbar-frame-plist)))
1026 (dframe-reposition-frame
1027 speedbar-frame
1028 (dframe-attached-frame speedbar-frame)
1029 (cons (car (cdr (member 'left speedbar-frame-plist)))
1030 (car (cdr (member 'top speedbar-frame-plist)))))
1031 )
1032 ((and (not dframe-xemacsp)
1033 (or (assoc 'left speedbar-frame-parameters)
1034 (assoc 'top speedbar-frame-parameters)))
1035 ;; if left/top were specified in the parameters, pass them
1036 ;; down to the reposition function
1037 (dframe-reposition-frame
1038 speedbar-frame
1039 (dframe-attached-frame speedbar-frame)
1040 (cons (cdr (assoc 'left speedbar-frame-parameters))
1041 (cdr (assoc 'top speedbar-frame-parameters))))
1042 )
1043 (t
1044 (dframe-reposition-frame speedbar-frame
1045 (dframe-attached-frame speedbar-frame)
1046 speedbar-default-position))))
1047
1048 (defsubst speedbar-current-frame ()
1049 "Return the frame to use for speedbar based on current context."
1050 (dframe-current-frame 'speedbar-frame 'speedbar-mode))
1051
1052 (defun speedbar-handle-delete-frame (e)
1053 "Handle a delete frame event E.
1054 If the deleted frame is the frame SPEEDBAR is attached to,
1055 we need to delete speedbar also."
1056 (let ((frame-to-be-deleted (car (car (cdr e)))))
1057 (if (eq frame-to-be-deleted dframe-attached-frame)
1058 (delete-frame speedbar-frame)))
1059 )
1060
1061 ;;;###autoload
1062 (defun speedbar-get-focus ()
1063 "Change frame focus to or from the speedbar frame.
1064 If the selected frame is not speedbar, then speedbar frame is
1065 selected. If the speedbar frame is active, then select the attached frame."
1066 (interactive)
1067 (speedbar-reset-scanners)
1068 (dframe-get-focus 'speedbar-frame 'speedbar-frame-mode
1069 (lambda () (let ((speedbar-update-flag t))
1070 (speedbar-timer-fn)))))
1071
1072 (defsubst speedbar-frame-width ()
1073 "Return the width of the speedbar frame in characters.
1074 nil if it doesn't exist."
1075 (frame-width speedbar-frame))
1076
1077 (defun speedbar-mode ()
1078 "Major mode for managing a display of directories and tags.
1079 \\<speedbar-key-map>
1080 The first line represents the default directory of the speedbar frame.
1081 Each directory segment is a button which jumps speedbar's default
1082 directory to that directory. Buttons are activated by clicking `\\[speedbar-click]'.
1083 In some situations using `\\[dframe-power-click]' is a `power click' which will
1084 rescan cached items, or pop up new frames.
1085
1086 Each line starting with <+> represents a directory. Click on the <+>
1087 to insert the directory listing into the current tree. Click on the
1088 <-> to retract that list. Click on the directory name to go to that
1089 directory as the default.
1090
1091 Each line starting with [+] is a file. If the variable
1092 `speedbar-show-unknown-files' is t, the lines starting with [?] are
1093 files which don't have imenu support, but are not expressly ignored.
1094 Files are completely ignored if they match `speedbar-file-unshown-regexp'
1095 which is generated from `completion-ignored-extensions'.
1096
1097 Files with a `*' character after their name are files checked out of a
1098 version control system. (Currently only RCS is supported.) New
1099 version control systems can be added by examining the documentation
1100 for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'.
1101
1102 Files with a `#' or `!' character after them are source files that
1103 have an object file associated with them. The `!' indicates that the
1104 files is out of date. You can control what source/object associations
1105 exist through the variable `speedbar-obj-alist'.
1106
1107 Click on the [+] to display a list of tags from that file. Click on
1108 the [-] to retract the list. Click on the file name to edit the file
1109 in the attached frame.
1110
1111 If you open tags, you might find a node starting with {+}, which is a
1112 category of tags. Click the {+} to expand the category. Jump-able
1113 tags start with >. Click the name of the tag to go to that position
1114 in the selected file.
1115
1116 \\{speedbar-key-map}"
1117 ;; NOT interactive
1118 (save-excursion
1119 (kill-all-local-variables)
1120 (setq major-mode 'speedbar-mode)
1121 (setq mode-name "Speedbar")
1122 (set-syntax-table speedbar-syntax-table)
1123 (setq font-lock-keywords nil) ;; no font-locking please
1124 (setq truncate-lines t)
1125 (make-local-variable 'frame-title-format)
1126 (setq frame-title-format (concat "Speedbar " speedbar-version))
1127 (setq case-fold-search nil)
1128 (toggle-read-only 1)
1129 (speedbar-set-mode-line-format)
1130 ;; Add in our dframe hooks.
1131 (if speedbar-track-mouse-flag
1132 (setq dframe-track-mouse-function #'speedbar-track-mouse))
1133 (setq dframe-help-echo-function #'speedbar-item-info
1134 dframe-mouse-click-function #'speedbar-click
1135 dframe-mouse-position-function #'speedbar-position-cursor-on-line)
1136 (run-hooks 'speedbar-mode-hook))
1137 speedbar-buffer)
1138
1139 (defmacro speedbar-message (fmt &rest args)
1140 "Like message, but for use in the speedbar frame.
1141 Argument FMT is the format string, and ARGS are the arguments for message."
1142 `(dframe-message ,fmt ,@args))
1143
1144 (defsubst speedbar-y-or-n-p (prompt &optional deleting)
1145 "Like `y-or-n-p', but for use in the speedbar frame.
1146 Argument PROMPT is the prompt to use.
1147 Optional argument DELETING means this is a query that will delete something.
1148 The variable `speedbar-query-confirmation-method' can cause this to
1149 return true without a query."
1150 (or (and (not deleting)
1151 (eq speedbar-query-confirmation-method 'none-but-delete))
1152 (dframe-y-or-n-p prompt)))
1153
1154 (defsubst speedbar-select-attached-frame ()
1155 "Select the frame attached to this speedbar."
1156 (dframe-select-attached-frame (speedbar-current-frame)))
1157
1158 ;; Backwards compatibility
1159 (defalias 'speedbar-with-attached-buffer 'dframe-with-attached-buffer)
1160 (defalias 'speedbar-maybee-jump-to-attached-frame 'dframe-maybee-jump-to-attached-frame)
1161
1162 (defun speedbar-set-mode-line-format ()
1163 "Set the format of the mode line based on the current speedbar environment.
1164 This gives visual indications of what is up. It EXPECTS the speedbar
1165 frame and window to be the currently active frame and window."
1166 (if (and (frame-live-p (speedbar-current-frame))
1167 (or (not dframe-xemacsp)
1168 (with-no-warnings
1169 (specifier-instance has-modeline-p)))
1170 speedbar-buffer)
1171 (save-excursion
1172 (set-buffer speedbar-buffer)
1173 (let* ((w (or (speedbar-frame-width) 20))
1174 (p1 "<<")
1175 (p5 ">>")
1176 (p3 (if speedbar-update-flag "#" "!"))
1177 (p35 (capitalize speedbar-initial-expansion-list-name))
1178 (blank (- w (length p1) (length p3) (length p5) (length p35)
1179 (if line-number-mode 5 1)))
1180 (p2 (if (> blank 0)
1181 (make-string (/ blank 2) ? )
1182 ""))
1183 (p4 (if (> blank 0)
1184 (make-string (+ (/ blank 2) (% blank 2)) ? )
1185 ""))
1186 (tf
1187 (if line-number-mode
1188 (list (concat p1 p2 p3 " " p35) '(line-number-mode " %3l")
1189 (concat p4 p5))
1190 (list (concat p1 p2 p3 p4 p5)))))
1191 (if (not (equal mode-line-format tf))
1192 (progn
1193 (setq mode-line-format tf)
1194 (speedbar-mode-line-update)))))))
1195
1196 (defvar speedbar-previous-menu nil
1197 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1198
1199 (defun speedbar-reconfigure-keymaps ()
1200 "Reconfigure the menu-bar in a speedbar frame.
1201 Different menu items are displayed depending on the current display mode
1202 and the existence of packages."
1203 (let ((md (append
1204 speedbar-easymenu-definition-base
1205 (if speedbar-shown-directories
1206 ;; file display mode version
1207 (speedbar-initial-menu)
1208 (save-excursion
1209 (dframe-select-attached-frame speedbar-frame)
1210 (eval (nth 1 (assoc speedbar-initial-expansion-list-name
1211 speedbar-initial-expansion-mode-alist)))))
1212 ;; Dynamic menu stuff
1213 '("-")
1214 (list (cons "Displays"
1215 (let ((displays nil)
1216 (alist speedbar-initial-expansion-mode-alist))
1217 (while alist
1218 (setq displays
1219 (cons
1220 (vector
1221 (capitalize (car (car alist)))
1222 (list
1223 'speedbar-change-initial-expansion-list
1224 (car (car alist)))
1225 :style 'radio
1226 :selected
1227 `(string= ,(car (car alist))
1228 speedbar-initial-expansion-list-name)
1229 )
1230 displays))
1231 (setq alist (cdr alist)))
1232 displays)))
1233 ;; The trailer
1234 speedbar-easymenu-definition-trailer))
1235 (localmap (save-excursion
1236 (let ((cf (selected-frame)))
1237 (prog2
1238 (dframe-select-attached-frame speedbar-frame)
1239 (if (local-variable-p
1240 'speedbar-special-mode-key-map
1241 (current-buffer))
1242 speedbar-special-mode-key-map)
1243 (select-frame cf))))))
1244 (save-excursion
1245 (set-buffer speedbar-buffer)
1246 (use-local-map (or localmap
1247 (speedbar-initial-keymap)
1248 ;; This creates a small keymap we can glom the
1249 ;; menu adjustments into.
1250 (speedbar-make-specialized-keymap)))
1251 ;; Delete the old menu if applicable.
1252 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1253 (setq speedbar-previous-menu md)
1254 ;; Now add the new menu
1255 (if (not dframe-xemacsp)
1256 (easy-menu-define speedbar-menu-map (current-local-map)
1257 "Speedbar menu" md)
1258 (easy-menu-add md (current-local-map))
1259 ;; XEmacs-specific:
1260 (if (fboundp 'set-buffer-menubar)
1261 (set-buffer-menubar (list md)))))
1262
1263 (run-hooks 'speedbar-reconfigure-keymaps-hook)))
1264
1265 \f
1266 ;;; User Input stuff
1267 ;;
1268 (defun speedbar-customize ()
1269 "Customize speedbar using the Custom package."
1270 (interactive)
1271 (let ((sf (selected-frame)))
1272 (dframe-select-attached-frame speedbar-frame)
1273 (customize-group 'speedbar)
1274 (select-frame sf))
1275 (dframe-maybee-jump-to-attached-frame))
1276
1277 (defun speedbar-track-mouse (event)
1278 "For motion EVENT, display info about the current line."
1279 (if (not speedbar-track-mouse-flag)
1280 nil
1281 (save-excursion
1282 (save-window-excursion
1283 (condition-case nil
1284 (progn
1285 (mouse-set-point event)
1286 (if (eq major-mode 'speedbar-mode)
1287 ;; XEmacs may let us get in here in other mode buffers.
1288 (speedbar-item-info)))
1289 (t (speedbar-message nil)))))))
1290
1291 (defun speedbar-show-info-under-mouse ()
1292 "Call the info function for the line under the mouse.
1293 Optional EVENT is currently not used."
1294 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1295 (if (equal (car pos) speedbar-frame)
1296 (save-excursion
1297 (save-window-excursion
1298 (apply 'set-mouse-position pos)
1299 (speedbar-item-info))))))
1300
1301 (defun speedbar-next (arg)
1302 "Move to the next ARGth line in a speedbar buffer."
1303 (interactive "p")
1304 (forward-line (or arg 1))
1305 (speedbar-item-info)
1306 (speedbar-position-cursor-on-line))
1307
1308 (defun speedbar-prev (arg)
1309 "Move to the previous ARGth line in a speedbar buffer."
1310 (interactive "p")
1311 (speedbar-next (if arg (- arg) -1)))
1312
1313 (defun speedbar-restricted-move (arg)
1314 "Move to the next ARGth line in a speedbar buffer at the same depth.
1315 This means that movement is restricted to a subnode, and that siblings
1316 of intermediate nodes are skipped."
1317 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1318 ;; First find the extent for which we are allowed to move.
1319 (let ((depth (save-excursion (beginning-of-line)
1320 (if (looking-at "[0-9]+:")
1321 (string-to-number (match-string 0))
1322 0)))
1323 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1324 (lastmatch (point)))
1325 (while (/= arg 0)
1326 (forward-line (- crement))
1327 (let ((subdepth (save-excursion (beginning-of-line)
1328 (if (looking-at "[0-9]+:")
1329 (string-to-number (match-string 0))
1330 0))))
1331 (cond ((or (< subdepth depth)
1332 (progn (end-of-line) (eobp))
1333 (progn (beginning-of-line) (bobp)))
1334 ;; We have reached the end of this block.
1335 (goto-char lastmatch)
1336 (setq arg 0)
1337 (error "End of sub-list"))
1338 ((= subdepth depth)
1339 (setq lastmatch (point)
1340 arg (+ arg crement))))))
1341 (speedbar-position-cursor-on-line)))
1342
1343 (defun speedbar-restricted-next (arg)
1344 "Move to the next ARGth line in a speedbar buffer at the same depth.
1345 This means that movement is restricted to a subnode, and that siblings
1346 of intermediate nodes are skipped."
1347 (interactive "p")
1348 (speedbar-restricted-move (or arg 1))
1349 (speedbar-item-info))
1350
1351 (defun speedbar-restricted-prev (arg)
1352 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1353 This means that movement is restricted to a subnode, and that siblings
1354 of intermediate nodes are skipped."
1355 (interactive "p")
1356 (speedbar-restricted-move (if arg (- arg) -1))
1357 (speedbar-item-info))
1358
1359 (defun speedbar-navigate-list (arg)
1360 "Move across ARG groups of similarly typed items in speedbar.
1361 Stop on the first line of the next type of item, or on the last or first item
1362 if we reach a buffer boundary."
1363 (interactive "p")
1364 (beginning-of-line)
1365 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1366 (let ((str (regexp-quote (match-string 0))))
1367 (while (looking-at str)
1368 (speedbar-restricted-move arg)
1369 (beginning-of-line))))
1370 (speedbar-position-cursor-on-line))
1371
1372 (defun speedbar-forward-list ()
1373 "Move forward over the current list.
1374 A LIST in speedbar is a group of similarly typed items, such as directories,
1375 files, or the directory button."
1376 (interactive)
1377 (speedbar-navigate-list 1)
1378 (speedbar-item-info))
1379
1380 (defun speedbar-backward-list ()
1381 "Move backward over the current list.
1382 A LIST in speedbar is a group of similarly typed items, such as directories,
1383 files, or the directory button."
1384 (interactive)
1385 (speedbar-navigate-list -1)
1386 (speedbar-item-info))
1387
1388 (defun speedbar-scroll-up (&optional arg)
1389 "Page down one screen-full of the speedbar, or ARG lines."
1390 (interactive "P")
1391 (scroll-up arg)
1392 (speedbar-position-cursor-on-line))
1393
1394 (defun speedbar-scroll-down (&optional arg)
1395 "Page up one screen-full of the speedbar, or ARG lines."
1396 (interactive "P")
1397 (scroll-down arg)
1398 (speedbar-position-cursor-on-line))
1399
1400 (defun speedbar-up-directory ()
1401 "Keyboard accelerator for moving the default directory up one.
1402 Assumes that the current buffer is the speedbar buffer."
1403 (interactive)
1404 (setq default-directory (expand-file-name (concat default-directory "../")))
1405 (speedbar-update-contents))
1406 \f
1407 ;;; Speedbar file activity (aka creeping featurism)
1408 ;;
1409 (defun speedbar-refresh (&optional arg)
1410 "Refresh the current speedbar display, disposing of any cached data.
1411 Argument ARG represents to force a refresh past any caches that may exist."
1412 (interactive "P")
1413 (let ((dl speedbar-shown-directories)
1414 (dframe-power-click arg)
1415 deactivate-mark)
1416 ;; We need to hack something so this works in detached frames.
1417 (while dl
1418 (adelete 'speedbar-directory-contents-alist (car dl))
1419 (setq dl (cdr dl)))
1420 (if (<= 1 speedbar-verbosity-level)
1421 (speedbar-message "Refreshing speedbar..."))
1422 (speedbar-update-contents)
1423 (speedbar-stealthy-updates)
1424 ;; Reset the timer in case it got really hosed for some reason...
1425 (speedbar-set-timer dframe-update-speed)
1426 (if (<= 1 speedbar-verbosity-level)
1427 (speedbar-message "Refreshing speedbar...done"))))
1428
1429 (defun speedbar-item-load ()
1430 "Load the item under the cursor or mouse if it is a Lisp file."
1431 (interactive)
1432 (let ((f (speedbar-line-file)))
1433 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1434 (if (and (file-exists-p (concat f "c"))
1435 (speedbar-y-or-n-p (format "Load %sc? " f)))
1436 ;; If the compiled version exists, load that instead...
1437 (load-file (concat f "c"))
1438 (load-file f))
1439 (error "Not a loadable file"))))
1440
1441 (defun speedbar-item-byte-compile ()
1442 "Byte compile the item under the cursor or mouse if it is a Lisp file."
1443 (interactive)
1444 (let ((f (speedbar-line-file))
1445 (sf (selected-frame)))
1446 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1447 (progn
1448 (dframe-select-attached-frame speedbar-frame)
1449 (byte-compile-file f nil)
1450 (select-frame sf)
1451 (speedbar-reset-scanners)))
1452 ))
1453
1454 (defun speedbar-mouse-item-info (event)
1455 "Provide information about what the user clicked on.
1456 This should be bound to a mouse EVENT."
1457 (interactive "e")
1458 (mouse-set-point event)
1459 (speedbar-item-info))
1460
1461 (defun speedbar-generic-item-info ()
1462 "Attempt to derive, and then display information about this line item.
1463 File style information is displayed with `speedbar-item-info'."
1464 (save-excursion
1465 (beginning-of-line)
1466 ;; Skip invisible number info.
1467 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1468 ;; Skip items in "folder" type text characters.
1469 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1470 ;; Get the text
1471 (speedbar-message "Text: %s" (buffer-substring-no-properties
1472 (point) (progn (end-of-line) (point))))))
1473
1474 (defun speedbar-item-info ()
1475 "Display info in the mini-buffer about the button the mouse is over.
1476 This function can be replaced in `speedbar-mode-functions-list' as
1477 `speedbar-item-info'."
1478 (interactive)
1479 (let (message-log-max)
1480 (funcall (or (speedbar-fetch-replacement-function 'speedbar-item-info)
1481 'speedbar-generic-item-info))))
1482
1483 (defun speedbar-item-info-file-helper (&optional filename)
1484 "Display info about a file that is on the current line.
1485 nil if not applicable. If FILENAME, then use that instead of reading
1486 it from the speedbar buffer."
1487 (let* ((item (or filename (speedbar-line-file)))
1488 (attr (if item (file-attributes item) nil)))
1489 (if (and item attr) (speedbar-message "%s %-6d %s" (nth 8 attr)
1490 (nth 7 attr) item)
1491 nil)))
1492
1493 (defun speedbar-item-info-tag-helper ()
1494 "Display info about a tag that is on the current line.
1495 nil if not applicable."
1496 (save-excursion
1497 (beginning-of-line)
1498 (if (re-search-forward " [-+=]?> \\([^\n]+\\)"
1499 (save-excursion(end-of-line)(point)) t)
1500 (let* ((tag (match-string 1))
1501 (attr (speedbar-line-token))
1502 (item nil)
1503 (semantic-tagged (if (fboundp 'semantic-tag-p)
1504 (semantic-tag-p attr))))
1505 (if semantic-tagged
1506 (with-no-warnings
1507 (save-excursion
1508 (when (and (semantic-tag-overlay attr)
1509 (semantic-tag-buffer attr))
1510 (set-buffer (semantic-tag-buffer attr)))
1511 (speedbar-message
1512 (funcall semantic-sb-info-format-tag-function attr)
1513 )))
1514 (looking-at "\\([0-9]+\\):")
1515 (setq item (file-name-nondirectory (speedbar-line-directory)))
1516 (speedbar-message "Tag: %s in %s" tag item)))
1517 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1518 (save-excursion(end-of-line)(point)) t)
1519 (speedbar-message "Group of tags \"%s\"" (match-string 1))
1520 (if (re-search-forward " [+-]?[()|@] \\([^\n]+\\)$" nil t)
1521 (let* ((detailtext (match-string 1))
1522 (detail (or (speedbar-line-token) detailtext))
1523 (parent (save-excursion
1524 (beginning-of-line)
1525 (let ((dep (if (looking-at "[0-9]+:")
1526 (1- (string-to-number (match-string 0)))
1527 0)))
1528 (re-search-backward (concat "^"
1529 (int-to-string dep)
1530 ":")
1531 nil t))
1532 (if (looking-at "[0-9]+: +[-+=>]> \\([^\n]+\\)$")
1533 (speedbar-line-token)
1534 nil))))
1535 (if (featurep 'semantic)
1536 (with-no-warnings
1537 (if (semantic-tag-p detail)
1538 (speedbar-message
1539 (funcall semantic-sb-info-format-tag-function detail parent))
1540 (if parent
1541 (speedbar-message "Detail: %s of tag %s" detail
1542 (if (semantic-tag-p parent)
1543 (semantic-format-tag-name parent nil t)
1544 parent))
1545 (speedbar-message "Detail: %s" detail))))
1546 ;; Not using `semantic':
1547 (if parent
1548 (speedbar-message "Detail: %s of tag %s" detail parent)
1549 (speedbar-message "Detail: %s" detail))))
1550 nil)))))
1551
1552 (defun speedbar-files-item-info ()
1553 "Display info in the mini-buffer about the button the mouse is over."
1554 (if (not speedbar-shown-directories)
1555 (speedbar-generic-item-info)
1556 (or (speedbar-item-info-file-helper)
1557 (speedbar-item-info-tag-helper)
1558 (speedbar-generic-item-info))))
1559
1560 (defun speedbar-item-copy ()
1561 "Copy the item under the cursor.
1562 Files can be copied to new names or places."
1563 (interactive)
1564 (let ((f (speedbar-line-file)))
1565 (if (not f) (error "Not a file"))
1566 (if (file-directory-p f)
1567 (error "Cannot copy directory")
1568 (let* ((rt (read-file-name (format "Copy %s to: "
1569 (file-name-nondirectory f))
1570 (file-name-directory f)))
1571 (refresh (member (expand-file-name (file-name-directory rt))
1572 speedbar-shown-directories)))
1573 ;; Create the right file name part
1574 (if (file-directory-p rt)
1575 (setq rt
1576 (concat (expand-file-name rt)
1577 (if (string-match "[/\\]$" rt) "" "/")
1578 (file-name-nondirectory f))))
1579 (if (or (not (file-exists-p rt))
1580 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)
1581 t))
1582 (progn
1583 (copy-file f rt t t)
1584 ;; refresh display if the new place is currently displayed.
1585 (if refresh
1586 (progn
1587 (speedbar-refresh)
1588 (if (not (speedbar-goto-this-file rt))
1589 (speedbar-goto-this-file f))))
1590 ))))))
1591
1592 (defun speedbar-item-rename ()
1593 "Rename the item under the cursor or mouse.
1594 Files can be renamed to new names or moved to new directories."
1595 (interactive)
1596 (let ((f (speedbar-line-file)))
1597 (if f
1598 (let* ((rt (read-file-name (format "Rename %s to: "
1599 (file-name-nondirectory f))
1600 (file-name-directory f)))
1601 (refresh (member (expand-file-name (file-name-directory rt))
1602 speedbar-shown-directories)))
1603 ;; Create the right file name part
1604 (if (file-directory-p rt)
1605 (setq rt
1606 (concat (expand-file-name rt)
1607 (if (string-match "[/\\]\\'" rt) "" "/")
1608 (file-name-nondirectory f))))
1609 (if (or (not (file-exists-p rt))
1610 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)
1611 t))
1612 (progn
1613 (rename-file f rt t)
1614 ;; refresh display if the new place is currently displayed.
1615 (if refresh
1616 (progn
1617 (speedbar-refresh)
1618 (speedbar-goto-this-file rt)
1619 )))))
1620 (error "Not a file"))))
1621
1622 (defun speedbar-create-directory ()
1623 "Create a directory in speedbar."
1624 (interactive)
1625 (let ((f (speedbar-line-file)))
1626 (if f
1627 (let* ((basedir (file-name-directory f))
1628 (nd (read-file-name "Create directory: "
1629 basedir)))
1630 ;; Make the directory
1631 (make-directory nd t)
1632 (speedbar-refresh)
1633 (speedbar-goto-this-file nd)
1634 )
1635 (error "Not a file"))))
1636
1637 (defun speedbar-item-delete ()
1638 "Delete the item under the cursor. Files are removed from disk."
1639 (interactive)
1640 (let ((f (speedbar-line-file)))
1641 (if (not f) (error "Not a file"))
1642 (if (speedbar-y-or-n-p (format "Delete %s? " f) t)
1643 (progn
1644 (if (file-directory-p f)
1645 (delete-directory f)
1646 (delete-file f))
1647 (speedbar-message "Okie dokie..")
1648 (let ((p (point)))
1649 (speedbar-refresh)
1650 (goto-char p))
1651 ))
1652 ))
1653
1654 (defun speedbar-item-object-delete ()
1655 "Delete the object associated from the item under the cursor.
1656 The file is removed from disk. The object is determined from the
1657 variable `speedbar-obj-alist'."
1658 (interactive)
1659 (let* ((f (speedbar-line-file))
1660 (obj nil)
1661 (oa speedbar-obj-alist))
1662 (if (not f) (error "Not a file"))
1663 (while (and oa (not (string-match (car (car oa)) f)))
1664 (setq oa (cdr oa)))
1665 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1666 (if (and oa (file-exists-p obj)
1667 (speedbar-y-or-n-p (format "Delete %s? " obj) t))
1668 (progn
1669 (delete-file obj)
1670 (speedbar-reset-scanners)))))
1671
1672 (defun speedbar-enable-update ()
1673 "Enable automatic updating in speedbar via timers."
1674 (interactive)
1675 (setq speedbar-update-flag t)
1676 (speedbar-set-mode-line-format)
1677 (speedbar-set-timer dframe-update-speed))
1678
1679 (defun speedbar-disable-update ()
1680 "Disable automatic updating and stop consuming resources."
1681 (interactive)
1682 (setq speedbar-update-flag nil)
1683 (speedbar-set-mode-line-format)
1684 (speedbar-set-timer nil))
1685
1686 (defun speedbar-toggle-updates ()
1687 "Toggle automatic update for the speedbar frame."
1688 (interactive)
1689 (if speedbar-update-flag
1690 (speedbar-disable-update)
1691 (speedbar-enable-update)))
1692
1693 (defun speedbar-toggle-images ()
1694 "Toggle use of images in the speedbar frame.
1695 Images are not available in Emacs 20 or earlier."
1696 (interactive)
1697 (setq speedbar-use-images (not speedbar-use-images))
1698 (speedbar-refresh))
1699
1700 (defun speedbar-toggle-sorting ()
1701 "Toggle tag sorting."
1702 (interactive)
1703 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1704
1705 (defun speedbar-toggle-show-all-files ()
1706 "Toggle display of files speedbar can not tag."
1707 (interactive)
1708 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1709 (speedbar-refresh))
1710
1711 (defmacro speedbar-with-writable (&rest forms)
1712 "Allow the buffer to be writable and evaluate FORMS."
1713 (list 'let '((inhibit-read-only t))
1714 (cons 'progn forms)))
1715 (put 'speedbar-with-writable 'lisp-indent-function 0)
1716
1717 (defun speedbar-insert-button (text face mouse function
1718 &optional token prevline)
1719 "Insert TEXT as the next logical speedbar button.
1720 FACE is the face to put on the button, MOUSE is the highlight face to use.
1721 When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
1722 This function assumes that the current buffer is the speedbar buffer.
1723 If PREVLINE, then put this button on the previous line.
1724
1725 This is a convenience function for special modes that create their own
1726 specialized speedbar displays."
1727 (goto-char (point-max))
1728 (let ((start (point)))
1729 (if (/= (current-column) 0) (insert "\n"))
1730 (put-text-property start (point) 'invisible nil))
1731 (if prevline (progn (delete-char -1)
1732 (insert " ") ;back up if desired...
1733 (put-text-property (1- (point)) (point) 'invisible nil)))
1734 (let ((start (point)))
1735 (insert text)
1736 (speedbar-make-button start (point) face mouse function token))
1737 (let ((start (point)))
1738 (insert "\n")
1739 (add-text-properties
1740 start (point) '(face nil invisible nil mouse-face nil))))
1741
1742 (defun speedbar-insert-separator (text)
1743 "Insert a separation label of TEXT.
1744 Separators are not active, have no labels, depth, or actions."
1745 (if speedbar-use-images
1746 (let ((start (point)))
1747 (insert "//")
1748 (speedbar-insert-image-button-maybe start 2)))
1749 (let ((start (point)))
1750 (insert text "\n")
1751 (speedbar-make-button start (point)
1752 'speedbar-separator-face
1753 nil nil nil)))
1754
1755 (defun speedbar-make-button (start end face mouse function &optional token)
1756 "Create a button from START to END, with FACE as the display face.
1757 MOUSE is the mouse face. When this button is clicked on FUNCTION
1758 will be run with the TOKEN parameter (any Lisp object). If FACE
1759 is t use the text properties of the string that is passed as an
1760 argument."
1761 (unless (eq face t)
1762 (put-text-property start end 'face face))
1763 (add-text-properties
1764 start end `(mouse-face ,mouse invisible nil
1765 speedbar-text ,(buffer-substring-no-properties start end)))
1766 (if speedbar-use-tool-tips-flag
1767 (put-text-property start end 'help-echo #'dframe-help-echo))
1768 (if function (put-text-property start end 'speedbar-function function))
1769 (if token (put-text-property start end 'speedbar-token token))
1770 ;; So far the only text we have is less that 3 chars.
1771 (if (<= (- end start) 3)
1772 (speedbar-insert-image-button-maybe start (- end start)))
1773 )
1774 \f
1775 ;;; Initial Expansion list management
1776 ;;
1777 (defun speedbar-initial-expansion-list ()
1778 "Return the current default expansion list.
1779 This is based on `speedbar-initial-expansion-list-name' referencing
1780 `speedbar-initial-expansion-mode-alist'."
1781 ;; cdr1 - name, cdr2 - menu
1782 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
1783 speedbar-initial-expansion-mode-alist)))))
1784
1785 (defun speedbar-initial-menu ()
1786 "Return the current default menu data.
1787 This is based on `speedbar-initial-expansion-list-name' referencing
1788 `speedbar-initial-expansion-mode-alist'."
1789 (symbol-value
1790 (car (cdr (assoc speedbar-initial-expansion-list-name
1791 speedbar-initial-expansion-mode-alist)))))
1792
1793 (defun speedbar-initial-keymap ()
1794 "Return the current default menu data.
1795 This is based on `speedbar-initial-expansion-list-name' referencing
1796 `speedbar-initial-expansion-mode-alist'."
1797 (symbol-value
1798 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
1799 speedbar-initial-expansion-mode-alist))))))
1800
1801 (defun speedbar-initial-stealthy-functions ()
1802 "Return a list of functions to call stealthily.
1803 This is based on `speedbar-initial-expansion-list-name' referencing
1804 `speedbar-stealthy-function-list'."
1805 (cdr (assoc speedbar-initial-expansion-list-name
1806 speedbar-stealthy-function-list)))
1807
1808 (defun speedbar-add-expansion-list (new-list)
1809 "Add NEW-LIST to the list of expansion lists."
1810 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
1811
1812 (defun speedbar-change-initial-expansion-list (new-default)
1813 "Change speedbar's default expansion list to NEW-DEFAULT."
1814 (interactive
1815 (list
1816 (completing-read (format "Speedbar Mode (default %s): "
1817 speedbar-previously-used-expansion-list-name)
1818 speedbar-initial-expansion-mode-alist
1819 nil t "" nil
1820 speedbar-previously-used-expansion-list-name)))
1821 (setq speedbar-previously-used-expansion-list-name
1822 speedbar-initial-expansion-list-name
1823 speedbar-initial-expansion-list-name new-default)
1824 (if (and (speedbar-current-frame) (frame-live-p (speedbar-current-frame)))
1825 (progn
1826 (speedbar-refresh)
1827 (speedbar-reconfigure-keymaps))))
1828
1829 (defun speedbar-fetch-replacement-function (function)
1830 "Return a current mode specific replacement for function, or nil.
1831 Scans `speedbar-mode-functions-list' first for the current mode, then
1832 for FUNCTION."
1833 (cdr (assoc function
1834 (cdr (assoc speedbar-initial-expansion-list-name
1835 speedbar-mode-functions-list)))))
1836
1837 (defun speedbar-add-mode-functions-list (new-list)
1838 "Add NEW-LIST to the list of mode functions.
1839 See `speedbar-mode-functions-list' for details."
1840 (add-to-list 'speedbar-mode-functions-list new-list))
1841
1842 \f
1843 ;;; Special speedbar display management
1844 ;;
1845 (defun speedbar-maybe-add-localized-support (buffer)
1846 "Quick check function called on BUFFERs by the speedbar timer function.
1847 Maintains the value of local variables which control speedbars use
1848 of the special mode functions."
1849 (or speedbar-special-mode-expansion-list
1850 (speedbar-add-localized-speedbar-support buffer)))
1851
1852 (defun speedbar-add-localized-speedbar-support (buffer)
1853 "Add localized speedbar support to BUFFER's mode if it is available."
1854 (interactive "bBuffer: ")
1855 (if (stringp buffer) (setq buffer (get-buffer buffer)))
1856 (if (not (buffer-live-p buffer))
1857 nil
1858 (save-excursion
1859 (set-buffer buffer)
1860 (save-match-data
1861 (let ((ms (symbol-name major-mode)) v)
1862 (if (not (string-match "-mode$" ms))
1863 nil ;; do nothing to broken mode
1864 (setq ms (substring ms 0 (match-beginning 0)))
1865 (setq v (intern-soft (concat ms "-speedbar-buttons")))
1866 (make-local-variable 'speedbar-special-mode-expansion-list)
1867 (if (not v)
1868 (setq speedbar-special-mode-expansion-list t)
1869 ;; If it is autoloaded, we need to load it now so that
1870 ;; we have access to the varialbe -speedbar-menu-items.
1871 ;; Is this XEmacs safe?
1872 (let ((sf (symbol-function v)))
1873 (if (and (listp sf) (eq (car sf) 'autoload))
1874 (load-library (car (cdr sf)))))
1875 (setq speedbar-special-mode-expansion-list (list v))
1876 (setq v (intern-soft (concat ms "-speedbar-key-map")))
1877 (if (not v)
1878 nil ;; don't add special keymap
1879 (make-local-variable 'speedbar-special-mode-key-map)
1880 (setq speedbar-special-mode-key-map
1881 (symbol-value v)))
1882 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
1883 (if (not v)
1884 nil ;; don't add special menus
1885 (make-local-variable 'speedbar-easymenu-definition-special)
1886 (setq speedbar-easymenu-definition-special
1887 (symbol-value v)))
1888 )))))))
1889
1890 (defun speedbar-remove-localized-speedbar-support (buffer)
1891 "Remove any traces that BUFFER supports speedbar in a specialized way."
1892 (save-excursion
1893 (set-buffer buffer)
1894 (kill-local-variable 'speedbar-special-mode-expansion-list)
1895 (kill-local-variable 'speedbar-special-mode-key-map)
1896 (kill-local-variable 'speedbar-easymenu-definition-special)))
1897 \f
1898 ;;; File button management
1899 ;;
1900 (defun speedbar-file-lists (directory)
1901 "Create file lists for DIRECTORY.
1902 The car is the list of directories, the cdr is list of files not
1903 matching ignored headers. Cache any directory files found in
1904 `speedbar-directory-contents-alist' and use that cache before scanning
1905 the file-system."
1906 (setq directory (expand-file-name directory))
1907 ;; If in powerclick mode, then the directory we are getting
1908 ;; should be rescanned.
1909 (if dframe-power-click
1910 (adelete 'speedbar-directory-contents-alist directory))
1911 ;; find the directory, either in the cache, or build it.
1912 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
1913 (let ((default-directory directory)
1914 (dir (directory-files directory nil))
1915 (dirs nil)
1916 (files nil))
1917 (while dir
1918 (if (not
1919 (or (string-match speedbar-file-unshown-regexp (car dir))
1920 (string-match speedbar-directory-unshown-regexp (car dir))))
1921 (if (file-directory-p (car dir))
1922 (setq dirs (cons (car dir) dirs))
1923 (setq files (cons (car dir) files))))
1924 (setq dir (cdr dir)))
1925 (let ((nl (cons (nreverse dirs) (list (nreverse files)))))
1926 (aput 'speedbar-directory-contents-alist directory nl)
1927 nl))
1928 ))
1929
1930 (defun speedbar-directory-buttons (directory index)
1931 "Insert a single button group at point for DIRECTORY.
1932 Each directory directory part is a different button. If part of the directory
1933 matches the user directory ~, then it is replaced with a ~.
1934 INDEX is not used, but is required by the caller."
1935 (let* ((tilde (expand-file-name "~/"))
1936 (dd (expand-file-name directory))
1937 (junk (string-match (regexp-quote tilde) dd))
1938 (displayme (if junk
1939 (concat "~/" (substring dd (match-end 0)))
1940 dd))
1941 (p (point)))
1942 (if (string-match "^~[/\\]?\\'" displayme) (setq displayme tilde))
1943 (insert displayme)
1944 (save-excursion
1945 (goto-char p)
1946 (while (re-search-forward "\\([^/\\]+\\)[/\\]" nil t)
1947 (speedbar-make-button (match-beginning 1) (match-end 1)
1948 'speedbar-directory-face
1949 'speedbar-highlight-face
1950 'speedbar-directory-buttons-follow
1951 (if (and (= (match-beginning 1) p)
1952 (not (char-equal (char-after (+ p 1)) ?:)))
1953 (expand-file-name "~/") ;the tilde
1954 (buffer-substring-no-properties
1955 p (match-end 0)))))
1956 ;; Nuke the beginning of the directory if it's too long...
1957 (cond ((eq speedbar-directory-button-trim-method 'span)
1958 (beginning-of-line)
1959 (let ((ww (or (speedbar-frame-width) 20)))
1960 (move-to-column ww nil)
1961 (while (>= (current-column) ww)
1962 (re-search-backward "[/\\]" nil t)
1963 (if (<= (current-column) 2)
1964 (progn
1965 (re-search-forward "[/\\]" nil t)
1966 (if (< (current-column) 4)
1967 (re-search-forward "[/\\]" nil t))
1968 (forward-char -1)))
1969 (if (looking-at "[/\\]?$")
1970 (beginning-of-line)
1971 (insert "/...\n ")
1972 (move-to-column ww nil)))))
1973 ((eq speedbar-directory-button-trim-method 'trim)
1974 (end-of-line)
1975 (let ((ww (or (speedbar-frame-width) 20))
1976 (tl (current-column)))
1977 (if (< ww tl)
1978 (progn
1979 (move-to-column (- tl ww))
1980 (if (re-search-backward "[/\\]" nil t)
1981 (progn
1982 (delete-region (point-min) (point))
1983 (insert "$")
1984 )))))))
1985 )
1986 (if (string-match "\\`[/\\][^/\\]+[/\\]\\'" displayme)
1987 (progn
1988 (insert " ")
1989 (let ((p (point)))
1990 (insert "<root>")
1991 (speedbar-make-button p (point)
1992 'speedbar-directory-face
1993 'speedbar-highlight-face
1994 'speedbar-directory-buttons-follow
1995 "/"))))
1996 (end-of-line)
1997 (insert-char ?\n 1 nil)))
1998
1999 (defun speedbar-make-tag-line (exp-button-type
2000 exp-button-char exp-button-function
2001 exp-button-data
2002 tag-button tag-button-function tag-button-data
2003 tag-button-face depth)
2004 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2005 This is the button that expands or contracts a node (if applicable),
2006 and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2007 is the function to call if it's clicked on. Button types are
2008 'bracket, 'angle, 'curly, 'expandtag, 'statictag, t, or nil.
2009 EXP-BUTTON-DATA is extra data attached to the text forming the expansion
2010 button.
2011
2012 Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2013 function to call if clicked on, and TAG-BUTTON-DATA is the data to
2014 attach to the text field (such a tag positioning, etc).
2015 TAG-BUTTON-FACE is a face used for this type of tag.
2016
2017 Lastly, DEPTH shows the depth of expansion.
2018
2019 This function assumes that the cursor is in the speedbar window at the
2020 position to insert a new item, and that the new item will end with a CR."
2021 (let ((start (point))
2022 (end (progn
2023 (insert (int-to-string depth) ":")
2024 (point)))
2025 (depthspacesize (* depth speedbar-indentation-width)))
2026 (put-text-property start end 'invisible t)
2027 (insert-char ? depthspacesize nil)
2028 (put-text-property (- (point) depthspacesize) (point) 'invisible nil)
2029 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2030 ((eq exp-button-type 'angle) "<%c>")
2031 ((eq exp-button-type 'curly) "{%c}")
2032 ((eq exp-button-type 'expandtag) " %c>")
2033 ((eq exp-button-type 'statictag) " =>")
2034 (t ">")))
2035 (buttxt (format exp-button exp-button-char))
2036 (start (point))
2037 (end (progn (insert buttxt) (point)))
2038 (bf (if (and exp-button-type (not (eq exp-button-type 'statictag)))
2039 'speedbar-button-face nil))
2040 (mf (if exp-button-function 'speedbar-highlight-face nil))
2041 )
2042 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
2043 (if speedbar-hide-button-brackets-flag
2044 (progn
2045 (put-text-property start (1+ start) 'invisible t)
2046 (put-text-property end (1- end) 'invisible t)))
2047 )
2048 (insert-char ? 1 nil)
2049 (put-text-property (1- (point)) (point) 'invisible nil)
2050 (let ((start (point))
2051 (end (progn (insert tag-button) (point))))
2052 (insert-char ?\n 1 nil)
2053 (put-text-property (1- (point)) (point) 'invisible nil)
2054 (speedbar-make-button start end tag-button-face
2055 (if tag-button-function 'speedbar-highlight-face nil)
2056 tag-button-function tag-button-data))
2057 ))
2058
2059 (defun speedbar-change-expand-button-char (char)
2060 "Change the expansion button character to CHAR for the current line."
2061 (save-excursion
2062 (beginning-of-line)
2063 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2064 (point)) t)
2065 (speedbar-with-writable
2066 (goto-char (match-end 1))
2067 (insert-char char 1 t)
2068 (forward-char -1)
2069 (delete-char -1)
2070 ;;(put-text-property (point) (1- (point)) 'invisible nil)
2071 ;; make sure we fix the image on the text here.
2072 (speedbar-insert-image-button-maybe (- (point) 1) 3)))))
2073
2074 \f
2075 ;;; Build button lists
2076 ;;
2077 (defun speedbar-insert-files-at-point (files level)
2078 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2079 Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2080 don't know how to manage them. The input parameter FILES is a cons
2081 cell of the form ( 'DIRLIST . 'FILELIST )."
2082 ;; Start inserting all the directories
2083 (let ((dirs (car files)))
2084 (while dirs
2085 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired (car dirs)
2086 (car dirs) 'speedbar-dir-follow nil
2087 'speedbar-directory-face level)
2088 (setq dirs (cdr dirs))))
2089 (let ((lst (car (cdr files)))
2090 (case-fold-search t))
2091 (while lst
2092 (let* ((known (string-match speedbar-file-regexp (car lst)))
2093 (expchar (if known ?+ ??))
2094 (fn (if known 'speedbar-tag-file nil)))
2095 (if (or speedbar-show-unknown-files (/= expchar ??))
2096 (speedbar-make-tag-line 'bracket expchar fn (car lst)
2097 (car lst) 'speedbar-find-file nil
2098 'speedbar-file-face level)))
2099 (setq lst (cdr lst)))))
2100
2101 (defun speedbar-default-directory-list (directory index)
2102 "Insert files for DIRECTORY with level INDEX at point."
2103 (speedbar-insert-files-at-point
2104 (speedbar-file-lists directory) index)
2105 (speedbar-reset-scanners)
2106 (if (= index 0)
2107 ;; If the shown files variable has extra directories, then
2108 ;; it is our responsibility to redraw them all
2109 ;; Luckilly, the nature of inserting items into this list means
2110 ;; that by reversing it, we can easilly go in the right order
2111 (let ((sf (cdr (reverse speedbar-shown-directories))))
2112 (setq speedbar-shown-directories
2113 (list (expand-file-name default-directory)))
2114 ;; exand them all as we find them
2115 (while sf
2116 (if (speedbar-goto-this-file (car sf))
2117 (progn
2118 (beginning-of-line)
2119 (if (looking-at "[0-9]+:[ ]*<")
2120 (progn
2121 (goto-char (match-end 0))
2122 (speedbar-do-function-pointer)))))
2123 (setq sf (cdr sf)))
2124 )))
2125 ;;; Generic List support
2126 ;;
2127 ;; Generic lists are hierarchies of tags which we may need to permute
2128 ;; in order to make it look nice.
2129 ;;
2130 ;; A generic list is of the form:
2131 ;; ( ("name" . marker-or-number) <-- one tag at this level
2132 ;; ("name" ("name" . mon) ("name" . mon) ) <-- one group of tags
2133 ;; ("name" mon ("name" . mon) ) <-- group w/ a position and tags
2134 (defun speedbar-generic-list-group-p (sublst)
2135 "Non-nil if SUBLST is a group.
2136 Groups may optionally contain a position."
2137 (and (stringp (car-safe sublst))
2138 (or (and (listp (cdr-safe sublst))
2139 (or (speedbar-generic-list-tag-p (car-safe (cdr-safe sublst)))
2140 (speedbar-generic-list-group-p (car-safe (cdr-safe sublst))
2141 )))
2142 (and (number-or-marker-p (car-safe (cdr-safe sublst)))
2143 (listp (cdr-safe (cdr-safe sublst)))
2144 (speedbar-generic-list-tag-p
2145 (car-safe (cdr-safe (cdr-safe sublst)))))
2146 )))
2147
2148 (defun speedbar-generic-list-positioned-group-p (sublst)
2149 "Non-nil of SUBLST is a group with a position."
2150 (and (stringp (car-safe sublst))
2151 (number-or-marker-p (car-safe (cdr-safe sublst)))
2152 (listp (cdr-safe (cdr-safe sublst)))
2153 (let ((rest (car-safe (cdr-safe (cdr-safe sublst)))))
2154 (or (speedbar-generic-list-tag-p rest)
2155 (speedbar-generic-list-group-p rest)
2156 (speedbar-generic-list-positioned-group-p rest)
2157 ))))
2158
2159 (defun speedbar-generic-list-tag-p (sublst)
2160 "Non-nil if SUBLST is a tag."
2161 (and (stringp (car-safe sublst))
2162 (or (and (number-or-marker-p (cdr-safe sublst))
2163 (not (cdr-safe (cdr-safe sublst))))
2164 ;; For semantic/bovine items, this is needed
2165 (symbolp (car-safe (cdr-safe sublst))))
2166 ))
2167
2168 (defun speedbar-sort-tag-hierarchy (lst)
2169 "Sort all elements of tag hierarchy LST."
2170 (sort (copy-alist lst)
2171 (lambda (a b) (string< (car a) (car b)))))
2172
2173 (defun speedbar-try-completion (string alist)
2174 "A wrapper for `try-completion'.
2175 Passes STRING and ALIST to `try-completion' if ALIST
2176 passes some tests."
2177 (if (and (consp alist)
2178 (listp (car alist)) (stringp (car (car alist))))
2179 (try-completion string alist)
2180 nil))
2181
2182 (defun speedbar-prefix-group-tag-hierarchy (lst)
2183 "Prefix group names for tag hierarchy LST."
2184 (let ((newlst nil)
2185 (sublst nil)
2186 (work-list nil)
2187 (junk-list nil)
2188 (short-group-list nil)
2189 (short-start-name nil)
2190 (short-end-name nil)
2191 (num-shorts-grouped 0)
2192 (bins (make-vector 256 nil))
2193 (diff-idx 0))
2194 (if (<= (length lst) speedbar-tag-regroup-maximum-length)
2195 ;; Do nothing. Too short to bother with.
2196 lst
2197 ;; Break out sub-lists
2198 (while lst
2199 (if (speedbar-generic-list-group-p (car-safe lst))
2200 (setq newlst (cons (car lst) newlst))
2201 (setq sublst (cons (car lst) sublst)))
2202 (setq lst (cdr lst)))
2203 ;; Reverse newlst because it was made backwards.
2204 ;; Sublist doesn't need reversing because the act
2205 ;; of binning things will reverse it for us.
2206 (setq newlst (nreverse newlst)
2207 sublst sublst)
2208 ;; Now, first find out how long our list is. Never let a
2209 ;; list get-shorter than our minimum.
2210 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2211 (setq work-list sublst)
2212 (setq diff-idx (length (speedbar-try-completion "" sublst)))
2213 ;; Sort the whole list into bins.
2214 (while sublst
2215 (let ((e (car sublst))
2216 (s (car (car sublst))))
2217 (cond ((<= (length s) diff-idx)
2218 ;; 0 storage bin for shorty.
2219 (aset bins 0 (cons e (aref bins 0))))
2220 (t
2221 ;; stuff into a bin based on ascii value at diff
2222 (aset bins (aref s diff-idx)
2223 (cons e (aref bins (aref s diff-idx)))))))
2224 (setq sublst (cdr sublst)))
2225 ;; Go through all our bins Stick singles into our
2226 ;; junk-list, everything else as sublsts in work-list.
2227 ;; If two neighboring lists are both small, make a grouped
2228 ;; group combinding those two sub-lists.
2229 (setq diff-idx 0)
2230 (while (> 256 diff-idx)
2231 ;; The bins contents are currently in forward order.
2232 (let ((l (aref bins diff-idx)))
2233 (if l
2234 (let ((tmp (cons (speedbar-try-completion "" l) l)))
2235 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2236 (> (+ (length l) (length short-group-list))
2237 speedbar-tag-split-minimum-length))
2238 (progn
2239 ;; We have reached a longer list, so we
2240 ;; must finish off a grouped group.
2241 (cond
2242 ((and short-group-list
2243 (= (length short-group-list)
2244 num-shorts-grouped))
2245 ;; All singles? Junk list
2246 (setq junk-list (append (nreverse short-group-list)
2247 junk-list)))
2248 ((= num-shorts-grouped 1)
2249 ;; Only one short group? Just stick it in
2250 ;; there by itself. Make a group, and find
2251 ;; a subexpression
2252 (let ((subexpression (speedbar-try-completion
2253 "" short-group-list)))
2254 (if (< (length subexpression)
2255 speedbar-tag-group-name-minimum-length)
2256 (setq subexpression
2257 (concat short-start-name
2258 " ("
2259 (substring
2260 (car (car short-group-list))
2261 (length short-start-name))
2262 ")")))
2263 (setq work-list
2264 (cons (cons subexpression
2265 short-group-list)
2266 work-list ))))
2267 (short-group-list
2268 ;; Multiple groups to be named in a special
2269 ;; way by displaying the range over which we
2270 ;; have grouped them.
2271 (setq work-list
2272 (cons (cons (concat short-start-name
2273 " to "
2274 short-end-name)
2275 short-group-list)
2276 work-list))))
2277 ;; Reset short group list information every time.
2278 (setq short-group-list nil
2279 short-start-name nil
2280 short-end-name nil
2281 num-shorts-grouped 0)))
2282 ;; Ok, now that we cleaned up the short-group-list,
2283 ;; we can deal with this new list, to decide if it
2284 ;; should go on one of these sub-lists or not.
2285 (if (< (length l) speedbar-tag-regroup-maximum-length)
2286 (setq short-group-list (append l short-group-list)
2287 num-shorts-grouped (1+ num-shorts-grouped)
2288 short-end-name (car tmp)
2289 short-start-name (if short-start-name
2290 short-start-name
2291 (car tmp)))
2292 (setq work-list (cons tmp work-list))))))
2293 (setq diff-idx (1+ diff-idx))))
2294 ;; Did we run out of things? Drop our new list onto the end.
2295 (cond
2296 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2297 ;; All singles? Junk list
2298 (setq junk-list (append short-group-list junk-list)))
2299 ((= num-shorts-grouped 1)
2300 ;; Only one short group? Just stick it in
2301 ;; there by itself.
2302 (setq work-list
2303 (cons (cons (speedbar-try-completion "" short-group-list)
2304 short-group-list)
2305 work-list)))
2306 (short-group-list
2307 ;; Multiple groups to be named in a special
2308 ;; way by displaying the range over which we
2309 ;; have grouped them.
2310 (setq work-list
2311 (cons (cons (concat short-start-name " to " short-end-name)
2312 short-group-list)
2313 work-list))))
2314 ;; Reverse the work list nreversed when consing.
2315 (setq work-list (nreverse work-list))
2316 ;; Now, stick our new list onto the end of
2317 (if work-list
2318 (if junk-list
2319 (append newlst work-list junk-list)
2320 (append newlst work-list))
2321 (append newlst junk-list)))))
2322
2323 (defun speedbar-trim-words-tag-hierarchy (lst)
2324 "Trim all words in a tag hierarchy.
2325 Base trimming information on word separators, and group names.
2326 Argument LST is the list of tags to trim."
2327 (let ((newlst nil)
2328 (sublst nil)
2329 (trim-prefix nil)
2330 (trim-chars 0)
2331 (trimlst nil))
2332 (while lst
2333 (if (speedbar-generic-list-group-p (car-safe lst))
2334 (setq newlst (cons (car lst) newlst))
2335 (setq sublst (cons (car lst) sublst)))
2336 (setq lst (cdr lst)))
2337 ;; Get the prefix to trim by. Make sure that we don't trim
2338 ;; off silly pieces, only complete understandable words.
2339 (setq trim-prefix (speedbar-try-completion "" sublst)
2340 newlst (nreverse newlst))
2341 (if (or (= (length sublst) 1)
2342 (not trim-prefix)
2343 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2344 (append newlst (nreverse sublst))
2345 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2346 (match-end 0)))
2347 (setq trim-chars (length trim-prefix))
2348 (while sublst
2349 (setq trimlst (cons
2350 (cons (substring (car (car sublst)) trim-chars)
2351 (cdr (car sublst)))
2352 trimlst)
2353 sublst (cdr sublst)))
2354 ;; Put the lists together
2355 (append newlst trimlst))))
2356
2357 (defun speedbar-simple-group-tag-hierarchy (lst)
2358 "Create a simple 'Tags' group with orphaned tags.
2359 Argument LST is the list of tags to sort into groups."
2360 (let ((newlst nil)
2361 (sublst nil))
2362 (while lst
2363 (if (speedbar-generic-list-group-p (car-safe lst))
2364 (setq newlst (cons (car lst) newlst))
2365 (setq sublst (cons (car lst) sublst)))
2366 (setq lst (cdr lst)))
2367 (if (not newlst)
2368 (nreverse sublst)
2369 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2370 (nreverse newlst))))
2371
2372 (defun speedbar-create-tag-hierarchy (lst)
2373 "Adjust the tag hierarchy in LST, and return it.
2374 This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2375 the list."
2376 (let* ((f (save-excursion
2377 (forward-line -1)
2378 (or (speedbar-line-file)
2379 (speedbar-line-directory))))
2380 (methods (if (get-file-buffer f)
2381 (save-excursion (set-buffer (get-file-buffer f))
2382 speedbar-tag-hierarchy-method)
2383 speedbar-tag-hierarchy-method))
2384 (lst (if (fboundp 'copy-tree)
2385 (copy-tree lst)
2386 lst)))
2387 (while methods
2388 (setq lst (funcall (car methods) lst)
2389 methods (cdr methods)))
2390 lst))
2391
2392 (defvar speedbar-generic-list-group-expand-button-type 'curly
2393 "The type of button created for groups of tags.
2394 Good values for this are `curly' and `expandtag'.
2395 Make buffer local for your mode.")
2396
2397 (defvar speedbar-generic-list-tag-button-type nil
2398 "The type of button created for tags in generic lists.
2399 Good values for this are nil and `statictag'.
2400 Make buffer local for your mode.")
2401
2402 (defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2403 "At LEVEL, insert a generic multi-level alist LST.
2404 Associations with lists get {+} tags (to expand into more nodes) and
2405 those with positions just get a > as the indicator. {+} buttons will
2406 have the function EXPAND-FUN and the token is the CDR list. The token
2407 name will have the function FIND-FUN and not token."
2408 ;; Remove imenu rescan button
2409 (if (string= (car (car lst)) "*Rescan*")
2410 (setq lst (cdr lst)))
2411 ;; Get, and set up variables that define how we treat these tags.
2412 (let ((f (save-excursion (forward-line -1)
2413 (or (speedbar-line-file)
2414 (speedbar-line-directory))))
2415 expand-button tag-button)
2416 (save-excursion
2417 (if (get-file-buffer f)
2418 (set-buffer (get-file-buffer f)))
2419 (setq expand-button speedbar-generic-list-group-expand-button-type
2420 tag-button speedbar-generic-list-tag-button-type))
2421 ;; Adjust the list.
2422 (setq lst (speedbar-create-tag-hierarchy lst))
2423 ;; insert the parts
2424 (while lst
2425 (cond ((null (car-safe lst)) nil) ;this would be a separator
2426 ((speedbar-generic-list-tag-p (car lst))
2427 (speedbar-make-tag-line tag-button
2428 nil nil nil ;no expand button data
2429 (car (car lst)) ;button name
2430 find-fun ;function
2431 (cdr (car lst)) ;token is position
2432 'speedbar-tag-face
2433 (1+ level)))
2434 ((speedbar-generic-list-positioned-group-p (car lst))
2435 (speedbar-make-tag-line expand-button
2436 ?+ expand-fun (cdr (cdr (car lst)))
2437 (car (car lst)) ;button name
2438 find-fun ;function
2439 (car (cdr (car lst))) ;token is posn
2440 'speedbar-tag-face
2441 (1+ level)))
2442 ((speedbar-generic-list-group-p (car lst))
2443 (speedbar-make-tag-line expand-button
2444 ?+ expand-fun (cdr (car lst))
2445 (car (car lst)) ;button name
2446 nil nil 'speedbar-tag-face
2447 (1+ level)))
2448 (t (speedbar-message "speedbar-insert-generic-list: malformed list!")
2449 ))
2450 (setq lst (cdr lst)))))
2451
2452 (defun speedbar-insert-imenu-list (indent lst)
2453 "At level INDENT, insert the imenu generated LST."
2454 (speedbar-insert-generic-list indent lst
2455 'speedbar-tag-expand
2456 'speedbar-tag-find))
2457
2458 (defun speedbar-insert-etags-list (indent lst)
2459 "At level INDENT, insert the etags generated LST."
2460 (speedbar-insert-generic-list indent lst
2461 'speedbar-tag-expand
2462 'speedbar-tag-find))
2463 \f
2464 ;;; Timed functions
2465 ;;
2466 (defun speedbar-update-contents ()
2467 "Generically update the contents of the speedbar buffer."
2468 (interactive)
2469 ;; Set the current special buffer
2470 (setq speedbar-desired-buffer nil)
2471
2472 ;; Check for special modes
2473 (speedbar-maybe-add-localized-support (current-buffer))
2474
2475 ;; Choose the correct method of doodling.
2476 (if (and speedbar-mode-specific-contents-flag
2477 (consp speedbar-special-mode-expansion-list)
2478 (local-variable-p
2479 'speedbar-special-mode-expansion-list
2480 (current-buffer)))
2481 ;;(eq (get major-mode 'mode-class 'special)))
2482 (speedbar-update-special-contents)
2483 (speedbar-update-directory-contents)))
2484
2485 (defun speedbar-update-localized-contents ()
2486 "Update the contents of the speedbar buffer for the current situation."
2487 ;; Due to the historical growth of speedbar, we need to do something
2488 ;; special for "files" mode. Too bad.
2489 (let ((name speedbar-initial-expansion-list-name)
2490 (funclst (speedbar-initial-expansion-list))
2491 )
2492 (if (string= name "files")
2493 ;; Do all the files type work. It still goes through the
2494 ;; expansion list stuff. :(
2495 (if (or (member (expand-file-name default-directory)
2496 speedbar-shown-directories)
2497 (and speedbar-ignored-directory-regexp
2498 (string-match
2499 speedbar-ignored-directory-regexp
2500 (expand-file-name default-directory))))
2501 nil
2502 (if (<= 1 speedbar-verbosity-level)
2503 (speedbar-message "Updating speedbar to: %s..."
2504 default-directory))
2505 (speedbar-update-directory-contents)
2506 (if (<= 1 speedbar-verbosity-level)
2507 (progn
2508 (speedbar-message "Updating speedbar to: %s...done"
2509 default-directory)
2510 (speedbar-message nil))))
2511 ;; Else, we can do a short cut. No text cache.
2512 (let ((cbd (expand-file-name default-directory)))
2513 (set-buffer speedbar-buffer)
2514 (speedbar-with-writable
2515 (let* ((window (get-buffer-window speedbar-buffer 0))
2516 (p (window-point window))
2517 (start (window-start window)))
2518 (erase-buffer)
2519 (dolist (func funclst)
2520 (setq default-directory cbd)
2521 (funcall func cbd 0))
2522 (speedbar-reconfigure-keymaps)
2523 (set-window-point window p)
2524 (set-window-start window start)))))))
2525
2526 (defun speedbar-update-directory-contents ()
2527 "Update the contents of the speedbar buffer based on the current directory."
2528 (let ((cbd (expand-file-name default-directory))
2529 cbd-parent
2530 (funclst (speedbar-initial-expansion-list))
2531 (cache speedbar-full-text-cache)
2532 ;; disable stealth during update
2533 (speedbar-stealthy-function-list nil)
2534 (use-cache nil)
2535 (expand-local nil)
2536 ;; Because there is a bug I can't find just yet
2537 (inhibit-quit nil))
2538 (set-buffer speedbar-buffer)
2539 ;; If we are updating contents to where we are, then this is
2540 ;; really a request to update existing contents, so we must be
2541 ;; careful with our text cache!
2542 (if (member cbd speedbar-shown-directories)
2543 (progn
2544 (setq cache nil)
2545 ;; If the current directory is not the last element in the dir
2546 ;; list, then we ALSO need to zap the list of expanded directories
2547 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2548 (setq speedbar-shown-directories (list cbd))))
2549
2550 ;; Build cbd-parent, and see if THAT is in the current shown
2551 ;; directories. First, go through pains to get the parent directory
2552 (if (and speedbar-smart-directory-expand-flag
2553 (save-match-data
2554 (setq cbd-parent cbd)
2555 (if (string-match "[/\\]$" cbd-parent)
2556 (setq cbd-parent (substring cbd-parent 0
2557 (match-beginning 0))))
2558 (setq cbd-parent (file-name-directory cbd-parent)))
2559 (member cbd-parent speedbar-shown-directories))
2560 (setq expand-local t)
2561
2562 ;; If this directory is NOT in the current list of available
2563 ;; directorys, then use the cache, and set the cache to our new
2564 ;; value. Make sure to unhighlight the current file, or if we
2565 ;; come back to this directory, it might be a different file
2566 ;; and then we get a mess!
2567 (if (> (point-max) 1)
2568 (progn
2569 (speedbar-clear-current-file)
2570 (setq speedbar-full-text-cache
2571 (cons speedbar-shown-directories (buffer-string)))))
2572
2573 ;; Check if our new directory is in the list of directories
2574 ;; shown in the text-cache
2575 (if (member cbd (car cache))
2576 (setq speedbar-shown-directories (car cache)
2577 use-cache t)
2578 ;; default the shown directories to this list...
2579 (setq speedbar-shown-directories (list cbd)))
2580 ))
2581 (if (not expand-local) (setq speedbar-last-selected-file nil))
2582 (speedbar-with-writable
2583 (if (and expand-local
2584 ;; Find this directory as a speedbar node.
2585 (speedbar-directory-line cbd))
2586 ;; Open it.
2587 (speedbar-expand-line)
2588 (let* ((window (get-buffer-window speedbar-buffer 0))
2589 (p (window-point window))
2590 (start (window-start window)))
2591 (erase-buffer)
2592 (cond (use-cache
2593 (setq default-directory
2594 (nth (1- (length speedbar-shown-directories))
2595 speedbar-shown-directories))
2596 (insert (cdr cache)))
2597 (t
2598 (dolist (func funclst)
2599 (setq default-directory cbd)
2600 (funcall func cbd 0))))
2601 (set-window-point window p)
2602 (set-window-start window start)))))
2603 (speedbar-reconfigure-keymaps))
2604
2605 (defun speedbar-update-special-contents ()
2606 "Use the mode-specific variable to fill in the speedbar buffer.
2607 This should only be used by modes classified as special."
2608 (let ((funclst speedbar-special-mode-expansion-list)
2609 (specialbuff (current-buffer)))
2610 (save-excursion
2611 (setq speedbar-desired-buffer specialbuff)
2612 (set-buffer speedbar-buffer)
2613 ;; If we are leaving a directory, cache it.
2614 (if (not speedbar-shown-directories)
2615 ;; Do nothing
2616 nil
2617 ;; Clean up directory maintenance stuff
2618 (speedbar-clear-current-file)
2619 (setq speedbar-full-text-cache
2620 (cons speedbar-shown-directories (buffer-string))
2621 speedbar-shown-directories nil))
2622 ;; Now fill in the buffer with our newly found specialized list.
2623 (speedbar-with-writable
2624 (dolist (func funclst)
2625 ;; We do not erase the buffer because these functions may
2626 ;; decide NOT to update themselves.
2627 (funcall func specialbuff)))))
2628 (speedbar-reconfigure-keymaps))
2629
2630 (defun speedbar-set-timer (timeout)
2631 "Set up the speedbar timer with TIMEOUT.
2632 Uses `dframe-set-timer'.
2633 Also resets scanner functions."
2634 (dframe-set-timer timeout 'speedbar-timer-fn 'speedbar-update-flag)
2635 ;; Apply a revert hook that will reset the scanners. We attach to revert
2636 ;; because most reverts occur during VC state change, and this lets our
2637 ;; VC scanner fix itself.
2638 (if timeout
2639 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
2640 (remove-hook 'after-revert-hook 'speedbar-reset-scanners))
2641 ;; change this if it changed for some reason
2642 (speedbar-set-mode-line-format))
2643
2644 (defun speedbar-timer-fn ()
2645 "Run whenever Emacs is idle to update the speedbar item."
2646 (if (or (not (speedbar-current-frame))
2647 (not (frame-live-p (speedbar-current-frame))))
2648 (speedbar-set-timer nil)
2649 ;; Save all the match data so that we don't mess up executing fns
2650 (save-match-data
2651 ;; Only do stuff if the frame is visible, not an icon, and if
2652 ;; it is currently flagged to do something.
2653 (if (and speedbar-update-flag
2654 (speedbar-current-frame)
2655 (frame-visible-p (speedbar-current-frame))
2656 (not (eq (frame-visible-p (speedbar-current-frame)) 'icon)))
2657 (let ((af (selected-frame)))
2658 (dframe-select-attached-frame speedbar-frame)
2659 ;; make sure we at least choose a window to
2660 ;; get a good directory from
2661 (if (window-minibuffer-p (selected-window))
2662 nil
2663 ;; Check for special modes
2664 (speedbar-maybe-add-localized-support (current-buffer))
2665 ;; Update for special mode all the time!
2666 (if (and speedbar-mode-specific-contents-flag
2667 (consp speedbar-special-mode-expansion-list)
2668 (local-variable-p
2669 'speedbar-special-mode-expansion-list
2670 (current-buffer)))
2671 ;;(eq (get major-mode 'mode-class 'special)))
2672 (progn
2673 (if (<= 2 speedbar-verbosity-level)
2674 (speedbar-message
2675 "Updating speedbar to special mode: %s..."
2676 major-mode))
2677 (speedbar-update-special-contents)
2678 (if (<= 2 speedbar-verbosity-level)
2679 (progn
2680 (speedbar-message
2681 "Updating speedbar to special mode: %s...done"
2682 major-mode)
2683 (speedbar-message nil))))
2684 (speedbar-update-localized-contents))
2685 (select-frame af))
2686 ;; Now run stealthy updates of time-consuming items
2687 (speedbar-stealthy-updates)))))
2688 (run-hooks 'speedbar-timer-hook))
2689
2690 \f
2691 ;;; Stealthy activities
2692 ;;
2693 (defvar speedbar-stealthy-update-recurse nil
2694 "Recursion avoidance variable for stealthy update.")
2695
2696 (defun speedbar-stealthy-updates ()
2697 "For a given speedbar, run all items in the stealthy function list.
2698 Each item returns t if it completes successfully, or nil if
2699 interrupted by the user."
2700 (if (not speedbar-stealthy-update-recurse)
2701 (let ((l (speedbar-initial-stealthy-functions))
2702 (speedbar-stealthy-update-recurse t))
2703 (unwind-protect
2704 (speedbar-with-writable
2705 (while (and l (funcall (car l)))
2706 ;;(sit-for 0)
2707 (setq l (cdr l))))
2708 ;;(speedbar-message "Exit with %S" (car l))
2709 ))))
2710
2711 (defun speedbar-reset-scanners ()
2712 "Reset any variables used by functions in the stealthy list as state.
2713 If new functions are added, their state needs to be updated here."
2714 (setq speedbar-vc-to-do-point t
2715 speedbar-obj-to-do-point t
2716 speedbar-ro-to-do-point t)
2717 (run-hooks 'speedbar-scanner-reset-hook)
2718 )
2719
2720 (defun speedbar-find-selected-file (file)
2721 "Go to the line where FILE is."
2722
2723 (set-buffer speedbar-buffer)
2724
2725 (goto-char (point-min))
2726 (let ((m nil))
2727 (while (and (setq m (re-search-forward
2728 (concat " \\(" (regexp-quote (file-name-nondirectory file))
2729 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2730 nil t))
2731 (not (string= file
2732 (concat
2733 (speedbar-line-directory
2734 (save-excursion
2735 (goto-char (match-beginning 0))
2736 (beginning-of-line)
2737 (save-match-data
2738 (looking-at "[0-9]+:")
2739 (string-to-number (match-string 0)))))
2740 (match-string 1))))))
2741 (if m
2742 (progn
2743 (goto-char (match-beginning 1))
2744 (match-string 1)))))
2745
2746 (defun speedbar-clear-current-file ()
2747 "Locate the file thought to be current, and remove its highlighting."
2748 (save-excursion
2749 ;;(set-buffer speedbar-buffer)
2750 (if speedbar-last-selected-file
2751 (speedbar-with-writable
2752 (if (speedbar-find-selected-file speedbar-last-selected-file)
2753 (put-text-property (match-beginning 1)
2754 (match-end 1)
2755 'face
2756 'speedbar-file-face))))))
2757
2758 (defun speedbar-update-current-file ()
2759 "Find the current file, and update our visuals to indicate its name.
2760 This is specific to file names. If the file name doesn't show up, but
2761 it should be in the list, then the directory cache needs to be
2762 updated."
2763 (let* ((lastf (selected-frame))
2764 (newcfd (save-excursion
2765 (dframe-select-attached-frame speedbar-frame)
2766 (let ((rf (if (buffer-file-name)
2767 (buffer-file-name)
2768 nil)))
2769 (select-frame lastf)
2770 rf)))
2771 (newcf (if newcfd newcfd))
2772 (lastb (current-buffer))
2773 (sucf-recursive (boundp 'sucf-recursive))
2774 (case-fold-search t))
2775 (if (and newcf
2776 ;; check here, that way we won't refresh to newcf until
2777 ;; its been written, thus saving ourselves some time
2778 (file-exists-p newcf)
2779 (not (string= newcf speedbar-last-selected-file)))
2780 (progn
2781 ;; It is important to select the frame, otherwise the window
2782 ;; we want the cursor to move in will not be updated by the
2783 ;; search-forward command.
2784 (select-frame (speedbar-current-frame))
2785 ;; Remove the old file...
2786 (speedbar-clear-current-file)
2787 ;; now highlight the new one.
2788 ;; (set-buffer speedbar-buffer)
2789 (speedbar-with-writable
2790 (if (speedbar-find-selected-file newcf)
2791 ;; put the property on it
2792 (put-text-property (match-beginning 1)
2793 (match-end 1)
2794 'face
2795 'speedbar-selected-face)
2796 ;; Oops, it's not in the list. Should it be?
2797 (if (and (string-match speedbar-file-regexp newcf)
2798 (string= (file-name-directory newcfd)
2799 (expand-file-name default-directory)))
2800 ;; yes, it is (we will ignore unknowns for now...)
2801 (progn
2802 (speedbar-refresh)
2803 (if (speedbar-find-selected-file newcf)
2804 ;; put the property on it
2805 (put-text-property (match-beginning 1)
2806 (match-end 1)
2807 'face
2808 'speedbar-selected-face)))
2809 ;; if it's not in there now, whatever...
2810 ))
2811 (setq speedbar-last-selected-file newcf))
2812 (if (not sucf-recursive)
2813 (progn
2814
2815 ;;Sat Dec 15 2001 12:40 AM (burton@openprivacy.org): this
2816 ;;doesn't need to be in. We don't want to recenter when we are
2817 ;;updating files.
2818
2819 ;;(speedbar-center-buffer-smartly)
2820
2821 (speedbar-position-cursor-on-line)
2822 ))
2823 (set-buffer lastb)
2824 (select-frame lastf)
2825 )))
2826 ;; return that we are done with this activity.
2827 t)
2828
2829 (defun speedbar-add-indicator (indicator-string &optional replace-this)
2830 "Add INDICATOR-STRING to the end of this speedbar line.
2831 If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2832 the existing indicator is removed. If there is already an
2833 indicator, then do not add a space."
2834 (beginning-of-line)
2835 ;; The nature of the beast: Assume we are in "the right place"
2836 (end-of-line)
2837 (skip-chars-backward (concat " " speedbar-vc-indicator
2838 speedbar-object-read-only-indicator
2839 (car speedbar-obj-indicator)
2840 (cdr speedbar-obj-indicator)))
2841 (if (and (not (looking-at speedbar-indicator-regex))
2842 (not (string= indicator-string " ")))
2843 (insert speedbar-indicator-separator))
2844 (speedbar-with-writable
2845 (save-excursion
2846 (if (and replace-this
2847 (re-search-forward replace-this (save-excursion (end-of-line)
2848 (point))
2849 t))
2850 (delete-region (match-beginning 0) (match-end 0))))
2851 (end-of-line)
2852 (if (not (string= " " indicator-string))
2853 (let ((start (point)))
2854 (insert indicator-string)
2855 (speedbar-insert-image-button-maybe start (length indicator-string))
2856 ))))
2857
2858 (defun speedbar-check-read-only ()
2859 "Scan all the files in a directory, and for each see if it is read only."
2860 ;; Check for to-do to be reset. If reset but no RCS is available
2861 ;; then set to nil (do nothing) otherwise, start at the beginning
2862 (save-excursion
2863 (if speedbar-buffer (set-buffer speedbar-buffer))
2864 (if (eq speedbar-ro-to-do-point t)
2865 (setq speedbar-ro-to-do-point 0))
2866 (if (numberp speedbar-ro-to-do-point)
2867 (progn
2868 (goto-char speedbar-ro-to-do-point)
2869 (while (and (not (input-pending-p))
2870 (re-search-forward "^\\([0-9]+\\):\\s-*[[<][+-\?][]>] "
2871 nil t))
2872 (setq speedbar-ro-to-do-point (point))
2873 (let ((f (speedbar-line-file)))
2874 (if f
2875 (if (not (file-writable-p f))
2876 (speedbar-add-indicator
2877 speedbar-object-read-only-indicator
2878 (regexp-quote speedbar-object-read-only-indicator))
2879 (speedbar-add-indicator
2880 " " (regexp-quote
2881 speedbar-object-read-only-indicator))))))
2882 (if (input-pending-p)
2883 ;; return that we are incomplete
2884 nil
2885 ;; we are done, set to-do to nil
2886 (setq speedbar-ro-to-do-point nil)
2887 ;; and return t
2888 t))
2889 t)))
2890
2891 ;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
2892 ;; Steven L Baur <steve@xemacs.org> said this was important:
2893 (eval-when-compile (or (featurep 'xemacs)
2894 (condition-case () (require 'efs)
2895 (error (require 'ange-ftp)))))
2896
2897 (defun speedbar-check-vc ()
2898 "Scan all files in a directory, and for each see if it's checked out.
2899 See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
2900 to add more types of version control systems."
2901 ;; Check for to-do to be reset. If reset but no RCS is available
2902 ;; then set to nil (do nothing) otherwise, start at the beginning
2903 (save-excursion
2904 (if speedbar-buffer (set-buffer speedbar-buffer))
2905 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
2906 (speedbar-vc-check-dir-p default-directory)
2907 (not (or (and (featurep 'ange-ftp)
2908 (string-match
2909 (car (symbol-value
2910 (if dframe-xemacsp
2911 'ange-ftp-directory-format
2912 'ange-ftp-name-format)))
2913 (expand-file-name default-directory)))
2914 ;; efs support: Bob Weiner
2915 (and (featurep 'efs)
2916 (string-match
2917 (let ((reg (symbol-value 'efs-directory-regexp)))
2918 (if (stringp reg)
2919 reg
2920 (car reg)))
2921 (expand-file-name default-directory))))))
2922 (setq speedbar-vc-to-do-point 0))
2923 (if (numberp speedbar-vc-to-do-point)
2924 (progn
2925 (goto-char speedbar-vc-to-do-point)
2926 (while (and (not (input-pending-p))
2927 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-?]\\] "
2928 nil t))
2929 (setq speedbar-vc-to-do-point (point))
2930 (if (speedbar-check-vc-this-line (match-string 1))
2931 (speedbar-add-indicator speedbar-vc-indicator
2932 (regexp-quote speedbar-vc-indicator))
2933 (speedbar-add-indicator " "
2934 (regexp-quote speedbar-vc-indicator))))
2935 (if (input-pending-p)
2936 ;; return that we are incomplete
2937 nil
2938 ;; we are done, set to-do to nil
2939 (setq speedbar-vc-to-do-point nil)
2940 ;; and return t
2941 t))
2942 t)))
2943
2944 (defun speedbar-check-vc-this-line (depth)
2945 "Return t if the file on this line is check of of a version control system.
2946 Parameter DEPTH is a string with the current depth of indentation of
2947 the file being checked."
2948 (let* ((d (string-to-number depth))
2949 (f (speedbar-line-directory d))
2950 (fn (buffer-substring-no-properties
2951 ;; Skip-chars: thanks ptype@dra.hmg.gb
2952 (point) (progn
2953 (skip-chars-forward "^ "
2954 (save-excursion (end-of-line)
2955 (point)))
2956 (point))))
2957 (fulln (concat f fn)))
2958 (if (<= 2 speedbar-verbosity-level)
2959 (speedbar-message "Speedbar vc check...%s" fulln))
2960 (and (file-writable-p fulln)
2961 (speedbar-this-file-in-vc f fn))))
2962
2963 (defun speedbar-vc-check-dir-p (directory)
2964 "Return t if we should bother checking DIRECTORY for version control files.
2965 This can be overloaded to add new types of version control systems."
2966 (or
2967 ;; Local CVS available in Emacs 21
2968 (and (fboundp 'vc-state)
2969 (file-exists-p (concat directory "CVS/")))
2970 ;; Local RCS
2971 (file-exists-p (concat directory "RCS/"))
2972 ;; Local SCCS
2973 (file-exists-p (concat directory "SCCS/"))
2974 ;; Remote SCCS project
2975 (let ((proj-dir (getenv "PROJECTDIR")))
2976 (if proj-dir
2977 (file-exists-p (concat proj-dir "/SCCS"))
2978 nil))
2979 ;; User extension
2980 (run-hook-with-args-until-success 'speedbar-vc-directory-enable-hook
2981 directory)
2982 ))
2983
2984 (defun speedbar-this-file-in-vc (directory name)
2985 "Check to see if the file in DIRECTORY with NAME is in a version control system.
2986 You can add new VC systems by overriding this function. You can
2987 optimize this function by overriding it and only doing those checks
2988 that will occur on your system."
2989 (or
2990 (if (fboundp 'vc-state)
2991 ;; Emacs 21 handles VC state in a nice way.
2992 (condition-case nil
2993 (let ((state (vc-state (concat directory name))))
2994 (not (or (eq 'up-to-date state)
2995 (null state))))
2996 ;; An error means not in a VC system
2997 (error nil))
2998 (or
2999 ;; RCS file name
3000 (file-exists-p (concat directory "RCS/" name ",v"))
3001 (file-exists-p (concat directory "RCS/" name))
3002 ;; Local SCCS file name
3003 (file-exists-p (concat directory "SCCS/s." name))
3004 ;; Remote SCCS file name
3005 (let ((proj-dir (getenv "PROJECTDIR")))
3006 (if proj-dir
3007 (file-exists-p (concat proj-dir "/SCCS/s." name))
3008 nil))))
3009 ;; User extension
3010 (run-hook-with-args 'speedbar-vc-in-control-hook directory name)
3011 ))
3012
3013 ;; Objet File scanning
3014 (defun speedbar-check-objects ()
3015 "Scan all files in a directory, and for each see if there is an object.
3016 See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
3017 to add more object types."
3018 ;; Check for to-do to be reset. If reset but no RCS is available
3019 ;; then set to nil (do nothing) otherwise, start at the beginning
3020 (save-excursion
3021 (if speedbar-buffer (set-buffer speedbar-buffer))
3022 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
3023 (setq speedbar-obj-to-do-point 0))
3024 (if (numberp speedbar-obj-to-do-point)
3025 (progn
3026 (goto-char speedbar-obj-to-do-point)
3027 (while (and (not (input-pending-p))
3028 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
3029 nil t))
3030 (setq speedbar-obj-to-do-point (point))
3031 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
3032 (if (not ind) (setq ind " "))
3033 (speedbar-add-indicator ind (concat
3034 (car speedbar-obj-indicator)
3035 "\\|"
3036 (cdr speedbar-obj-indicator)))))
3037 (if (input-pending-p)
3038 ;; return that we are incomplete
3039 nil
3040 ;; we are done, set to-do to nil
3041 (setq speedbar-obj-to-do-point nil)
3042 ;; and return t
3043 t))
3044 t)))
3045
3046 (defun speedbar-check-obj-this-line (depth)
3047 "Return t if the file on this line has an associated object.
3048 Parameter DEPTH is a string with the current depth of indentation of
3049 the file being checked."
3050 (let* ((d (string-to-number depth))
3051 (f (speedbar-line-directory d))
3052 (fn (buffer-substring-no-properties
3053 ;; Skip-chars: thanks ptype@dra.hmg.gb
3054 (point) (progn
3055 (skip-chars-forward "^ "
3056 (save-excursion (end-of-line)
3057 (point)))
3058 (point))))
3059 (fulln (concat f fn)))
3060 (if (<= 2 speedbar-verbosity-level)
3061 (speedbar-message "Speedbar obj check...%s" fulln))
3062 (let ((oa speedbar-obj-alist))
3063 (while (and oa (not (string-match (car (car oa)) fulln)))
3064 (setq oa (cdr oa)))
3065 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
3066 (cdr (car oa))))))
3067 nil
3068 ;; Find out if the object is out of date or not.
3069 (let ((date1 (nth 5 (file-attributes fulln)))
3070 (date2 (nth 5 (file-attributes (concat
3071 (file-name-sans-extension fulln)
3072 (cdr (car oa)))))))
3073 (if (or (< (car date1) (car date2))
3074 (and (= (car date1) (car date2))
3075 (< (nth 1 date1) (nth 1 date2))))
3076 (car speedbar-obj-indicator)
3077 (cdr speedbar-obj-indicator)))))))
3078 \f
3079 ;;; Clicking Activity
3080 ;;
3081 (defun speedbar-position-cursor-on-line ()
3082 "Position the cursor on a line."
3083 (let ((oldpos (point)))
3084 (beginning-of-line)
3085 (if (looking-at "[0-9]+:\\s-*..?.? ")
3086 (goto-char (1- (match-end 0)))
3087 (goto-char oldpos))))
3088
3089 (defun speedbar-click (e)
3090 "Activate any speedbar buttons where the mouse is clicked.
3091 This must be bound to a mouse event. A button is any location of text
3092 with a mouse face that has a text property called `speedbar-function'.
3093 Argument E is the click event."
3094 ;; Backward compatibility let statement.
3095 (let ((speedbar-power-click dframe-power-click))
3096 (speedbar-do-function-pointer))
3097 (dframe-quick-mouse e))
3098
3099 (defun speedbar-do-function-pointer ()
3100 "Look under the cursor and examine the text properties.
3101 From this extract the file/tag name, token, indentation level and call
3102 a function if appropriate."
3103 (let* ((speedbar-frame (speedbar-current-frame))
3104 (fn (get-text-property (point) 'speedbar-function))
3105 (tok (get-text-property (point) 'speedbar-token))
3106 ;; The 1-,+ is safe because scaning starts AFTER the point
3107 ;; specified. This lets the search include the character the
3108 ;; cursor is on.
3109 (tp (previous-single-property-change
3110 (1+ (point)) 'speedbar-function))
3111 (np (next-single-property-change
3112 (point) 'speedbar-function))
3113 (txt (buffer-substring-no-properties (or tp (point-min))
3114 (or np (point-max))))
3115 (dent (save-excursion (beginning-of-line)
3116 (string-to-number
3117 (if (looking-at "[0-9]+")
3118 (buffer-substring-no-properties
3119 (match-beginning 0) (match-end 0))
3120 "0")))))
3121 ;;(speedbar-message "%S:%S:%S:%s" fn tok txt dent)
3122 (and fn (funcall fn txt tok dent)))
3123 (speedbar-position-cursor-on-line))
3124 \f
3125 ;;; Reading info from the speedbar buffer
3126 ;;
3127 (defun speedbar-line-text (&optional p)
3128 "Retrieve the text after prefix junk for the current line.
3129 Optional argument P is where to start the search from."
3130 (save-excursion
3131 (if p (goto-char p))
3132 (beginning-of-line)
3133 (if (looking-at (concat
3134 "\\([0-9]+\\): *[[<{]?[-+?= ][]>}@()|] \\([^ \n]+\\)"))
3135 (get-text-property (match-beginning 2) 'speedbar-text)
3136 nil)))
3137
3138 (defun speedbar-line-token (&optional p)
3139 "Retrieve the token information after the prefix junk for the current line.
3140 Optional argument P is where to start the search from."
3141 (save-excursion
3142 (if p (goto-char p))
3143 (beginning-of-line)
3144 (if (looking-at (concat
3145 "\\([0-9]+\\): *[[<{]?[-+?= ][]>}@()|] \\([^ \n]+\\)\\("
3146 speedbar-indicator-regex "\\)?"))
3147 (progn
3148 (goto-char (match-beginning 2))
3149 (get-text-property (point) 'speedbar-token))
3150 nil)))
3151
3152 (defun speedbar-line-file (&optional p)
3153 "Retrieve the file or whatever from the line at point P.
3154 The return value is a string representing the file. If it is a
3155 directory, then it is the directory name."
3156 (save-match-data
3157 (save-restriction
3158 (widen)
3159 (let ((f (speedbar-line-text p)))
3160 (if f
3161 (let* ((depth (string-to-number (match-string 1)))
3162 (directory (speedbar-line-directory depth)))
3163 (if (file-exists-p (concat directory f))
3164 (concat directory f)
3165 nil))
3166 nil)))))
3167
3168 (defun speedbar-goto-this-file (file)
3169 "If FILE is displayed, go to this line and return t.
3170 Otherwise do not move and return nil."
3171 (let ((directory (substring (file-name-directory (expand-file-name file))
3172 (length (expand-file-name default-directory))))
3173 (dest (point)))
3174 (save-match-data
3175 (goto-char (point-min))
3176 ;; scan all the directories
3177 (while (and directory (not (eq directory t)))
3178 (if (string-match "^[/\\]?\\([^/\\]+\\)" directory)
3179 (let ((pp (match-string 1 directory)))
3180 (if (save-match-data
3181 (re-search-forward (concat "> " (regexp-quote pp) "$")
3182 nil t))
3183 (setq directory (substring directory (match-end 1)))
3184 (setq directory nil)))
3185 (setq directory t)))
3186 ;; find the file part
3187 (if (or (not directory) (string= (file-name-nondirectory file) ""))
3188 ;; only had a dir part
3189 (if directory
3190 (progn
3191 (speedbar-position-cursor-on-line)
3192 t)
3193 (goto-char dest) nil)
3194 ;; find the file part
3195 (let ((nd (file-name-nondirectory file)))
3196 (if (re-search-forward
3197 (concat "] \\(" (regexp-quote nd)
3198 "\\)\\(" speedbar-indicator-regex "\\)$")
3199 nil t)
3200 (progn
3201 (speedbar-position-cursor-on-line)
3202 t)
3203 (goto-char dest)
3204 nil))))))
3205
3206 (defun speedbar-line-directory (&optional depth)
3207 "Retrieve the directory name associated with the current line.
3208 This may require traversing backwards from DEPTH and combining the default
3209 directory with these items. This function is replaceable in
3210 `speedbar-mode-functions-list' as `speedbar-line-directory'."
3211 (save-restriction
3212 (widen)
3213 (let ((rf (speedbar-fetch-replacement-function 'speedbar-line-directory)))
3214 (if rf (funcall rf depth) default-directory))))
3215
3216 (defun speedbar-files-line-directory (&optional depth)
3217 "Retrieve the directoryname associated with the current line.
3218 This may require traversing backwards from DEPTH and combining the default
3219 directory with these items."
3220 (save-excursion
3221 (save-match-data
3222 (if (not depth)
3223 (progn
3224 (beginning-of-line)
3225 (looking-at "^\\([0-9]+\\):")
3226 (setq depth (string-to-number (match-string 1)))))
3227 (let ((directory nil))
3228 (setq depth (1- depth))
3229 (while (/= depth -1)
3230 (if (not (re-search-backward (format "^%d:" depth) nil t))
3231 (error "Error building filename of tag")
3232 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)")
3233 (setq directory (concat (speedbar-line-text)
3234 "/"
3235 directory)))
3236 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)")
3237 ;; This is the start of our directory.
3238 (setq directory (speedbar-line-text)))))
3239 (setq depth (1- depth)))
3240 (if (and directory
3241 (string-match (concat speedbar-indicator-regex "$")
3242 directory))
3243 (setq directory (substring directory 0 (match-beginning 0))))
3244 (concat default-directory directory)))))
3245
3246 (defun speedbar-directory-line (directory)
3247 "Position the cursor on the line specified by DIRECTORY."
3248 (save-match-data
3249 (if (string-match "[/\\]$" directory)
3250 (setq directory (substring directory 0 (match-beginning 0))))
3251 (let ((nomatch t) (depth 0)
3252 (fname (file-name-nondirectory directory))
3253 (pname (file-name-directory directory)))
3254 (if (not (member pname speedbar-shown-directories))
3255 (error "Internal Error: File %s not shown in speedbar" directory))
3256 (goto-char (point-min))
3257 (while (and nomatch
3258 (re-search-forward
3259 (concat "[]>] \\(" (regexp-quote fname)
3260 "\\)\\(" speedbar-indicator-regex "\\)?$")
3261 nil t))
3262 (beginning-of-line)
3263 (looking-at "\\([0-9]+\\):")
3264 (setq depth (string-to-number (match-string 0))
3265 nomatch (not (string= pname (speedbar-line-directory depth))))
3266 (end-of-line))
3267 (beginning-of-line)
3268 (not nomatch))))
3269
3270 (defun speedbar-edit-line ()
3271 "Edit whatever tag or file is on the current speedbar line."
3272 (interactive)
3273 (or (save-excursion
3274 (beginning-of-line)
3275 ;; If this fails, then it is a non-standard click, and as such,
3276 ;; perfectly allowed.
3277 (if (re-search-forward "[]>?}] [^ ]"
3278 (save-excursion (end-of-line) (point))
3279 t)
3280 (progn
3281 (forward-char -1)
3282 (speedbar-do-function-pointer))
3283 nil))
3284 (speedbar-do-function-pointer)))
3285
3286 (defun speedbar-expand-line (&optional arg)
3287 "Expand the line under the cursor.
3288 With universal argument ARG, flush cached data."
3289 (interactive "P")
3290 (beginning-of-line)
3291 (let* ((dframe-power-click arg)
3292 (speedbar-power-click arg))
3293 (condition-case nil
3294 (progn
3295 (re-search-forward ":\\s-*.\\+. "
3296 (save-excursion (end-of-line) (point)))
3297 (forward-char -2)
3298 (speedbar-do-function-pointer))
3299 (error (speedbar-position-cursor-on-line)))))
3300
3301 (defun speedbar-flush-expand-line ()
3302 "Expand the line under the cursor and flush any cached information."
3303 (interactive)
3304 (speedbar-expand-line 1))
3305
3306 (defun speedbar-contract-line ()
3307 "Contract the line under the cursor."
3308 (interactive)
3309 (beginning-of-line)
3310 (condition-case nil
3311 (progn
3312 (re-search-forward ":\\s-*.-. "
3313 (save-excursion (end-of-line) (point)))
3314 (forward-char -2)
3315 (speedbar-do-function-pointer))
3316 (error (speedbar-position-cursor-on-line))))
3317
3318 (defun speedbar-toggle-line-expansion ()
3319 "Contract or expand the line under the cursor."
3320 (interactive)
3321 (beginning-of-line)
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-expand-line-descendants (&optional arg)
3331 "Expand the line under the cursor and all descendants.
3332 Optional argument ARG indicates that any cache should be flushed."
3333 (interactive "P")
3334 (speedbar-expand-line arg)
3335 ;; Now, inside the area expaded here, expand all subnodes of
3336 ;; the same descendant type.
3337 (save-excursion
3338 (speedbar-next 1) ;; Move into the list.
3339 (let ((err nil))
3340 (while (not err)
3341 (condition-case nil
3342 (progn
3343 (speedbar-expand-line-descendants arg)
3344 (speedbar-restricted-next 1))
3345 (error (setq err t))))))
3346 )
3347
3348 (defun speedbar-contract-line-descendants ()
3349 "Expand the line under the cursor and all descendants."
3350 (interactive)
3351 (speedbar-contract-line)
3352 ;; Don't need to do anything else since all descendants are
3353 ;; hidden by default anyway. Yay! It's easy.
3354 )
3355
3356 (defun speedbar-find-file (text token indent)
3357 "Speedbar click handler for filenames.
3358 TEXT, the file will be displayed in the attached frame.
3359 TOKEN is unused, but required by the click handler. INDENT is the
3360 current indentation level."
3361 (let ((cdd (speedbar-line-directory indent)))
3362 ;; Run before visiting file hook here.
3363 (let ((f (selected-frame)))
3364 (dframe-select-attached-frame speedbar-frame)
3365 (run-hooks 'speedbar-before-visiting-file-hook)
3366 (select-frame f))
3367 (speedbar-find-file-in-frame (concat cdd text))
3368 (speedbar-stealthy-updates)
3369 (run-hooks 'speedbar-visiting-file-hook)
3370 ;; Reset the timer with a new timeout when cliking a file
3371 ;; in case the user was navigating directories, we can cancel
3372 ;; that other timer.
3373 (speedbar-set-timer dframe-update-speed))
3374 (dframe-maybee-jump-to-attached-frame))
3375
3376 (defun speedbar-dir-follow (text token indent)
3377 "Speedbar click handler for directory names.
3378 Clicking a directory will cause the speedbar to list files in
3379 the subdirectory TEXT. TOKEN is an unused requirement. The
3380 subdirectory chosen will be at INDENT level."
3381 (setq default-directory
3382 (concat (expand-file-name (concat (speedbar-line-directory indent) text))
3383 "/"))
3384 ;; Because we leave speedbar as the current buffer,
3385 ;; update contents will change directory without
3386 ;; having to touch the attached frame. Turn off smart expand just
3387 ;; in case.
3388 (let ((speedbar-smart-directory-expand-flag nil))
3389 (speedbar-update-contents))
3390 (speedbar-set-timer speedbar-navigating-speed)
3391 (setq speedbar-last-selected-file nil)
3392 (speedbar-stealthy-updates))
3393
3394 (defun speedbar-delete-subblock (indent)
3395 "Delete text from point to indentation level INDENT or greater.
3396 Handles end-of-sublist smartly."
3397 (speedbar-with-writable
3398 (save-excursion
3399 (end-of-line) (forward-char 1)
3400 (let ((start (point)))
3401 (while (and (looking-at "^\\([0-9]+\\):")
3402 (> (string-to-number (match-string 1)) indent)
3403 (not (eobp)))
3404 (forward-line 1)
3405 (beginning-of-line))
3406 (delete-region start (point))))))
3407
3408 (defun speedbar-dired (text token indent)
3409 "Speedbar click handler for directory expand button.
3410 Clicking this button expands or contracts a directory. TEXT is the
3411 button clicked which has either a + or -. TOKEN is the directory to be
3412 expanded. INDENT is the current indentation level."
3413 (cond ((string-match "+" text) ;we have to expand this dir
3414 (setq speedbar-shown-directories
3415 (cons (expand-file-name
3416 (concat (speedbar-line-directory indent) token "/"))
3417 speedbar-shown-directories))
3418 (speedbar-change-expand-button-char ?-)
3419 (speedbar-reset-scanners)
3420 (save-excursion
3421 (end-of-line) (forward-char 1)
3422 (speedbar-with-writable
3423 (speedbar-default-directory-list
3424 (concat (speedbar-line-directory indent) token "/")
3425 (1+ indent)))))
3426 ((string-match "-" text) ;we have to contract this node
3427 (speedbar-reset-scanners)
3428 (let ((oldl speedbar-shown-directories)
3429 (newl nil)
3430 (td (expand-file-name
3431 (concat (speedbar-line-directory indent) token))))
3432 (while oldl
3433 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3434 (setq newl (cons (car oldl) newl)))
3435 (setq oldl (cdr oldl)))
3436 (setq speedbar-shown-directories (nreverse newl)))
3437 (speedbar-change-expand-button-char ?+)
3438 (speedbar-delete-subblock indent)
3439 )
3440 (t (error "Ooops... not sure what to do")))
3441 (speedbar-center-buffer-smartly)
3442 (save-excursion (speedbar-stealthy-updates)))
3443
3444 (defun speedbar-directory-buttons-follow (text token indent)
3445 "Speedbar click handler for default directory buttons.
3446 TEXT is the button clicked on. TOKEN is the directory to follow.
3447 INDENT is the current indentation level and is unused."
3448 (if (string-match "^[A-z]:$" token)
3449 (setq default-directory (concat token "/"))
3450 (setq default-directory token))
3451 ;; Because we leave speedbar as the current buffer,
3452 ;; update contents will change directory without
3453 ;; having to touch the attached frame.
3454 (speedbar-update-contents)
3455 (speedbar-set-timer speedbar-navigating-speed))
3456
3457 (defun speedbar-tag-file (text token indent)
3458 "The cursor is on a selected line. Expand the tags in the specified file.
3459 The parameter TEXT and TOKEN are required, where TEXT is the button
3460 clicked, and TOKEN is the file to expand. INDENT is the current
3461 indentation level."
3462 (cond ((string-match "+" text) ;we have to expand this file
3463 (let* ((fn (expand-file-name (concat (speedbar-line-directory indent)
3464 token)))
3465 (mode nil)
3466 (lst (speedbar-fetch-dynamic-tags fn)))
3467 ;; if no list, then remove expando button
3468 (if (not lst)
3469 (speedbar-change-expand-button-char ??)
3470 (speedbar-change-expand-button-char ?-)
3471 (speedbar-with-writable
3472 (save-excursion
3473 (end-of-line) (forward-char 1)
3474 (funcall (car lst) indent (cdr lst)))))))
3475 ((string-match "-" text) ;we have to contract this node
3476 (speedbar-change-expand-button-char ?+)
3477 (speedbar-delete-subblock indent))
3478 (t (error "Ooops... not sure what to do")))
3479 (speedbar-center-buffer-smartly))
3480
3481 (defun speedbar-tag-find (text token indent)
3482 "For the tag TEXT in a file TOKEN, go to that position.
3483 INDENT is the current indentation level."
3484 (let ((file (speedbar-line-directory indent)))
3485 (let ((f (selected-frame)))
3486 (dframe-select-attached-frame speedbar-frame)
3487 (run-hooks 'speedbar-before-visiting-tag-hook)
3488 (select-frame f))
3489 (speedbar-find-file-in-frame file)
3490 (save-excursion (speedbar-stealthy-updates))
3491 ;; Reset the timer with a new timeout when cliking a file
3492 ;; in case the user was navigating directories, we can cancel
3493 ;; that other timer.
3494 (speedbar-set-timer dframe-update-speed)
3495 (goto-char token)
3496 (run-hooks 'speedbar-visiting-tag-hook)
3497 (dframe-maybee-jump-to-attached-frame)
3498 ))
3499
3500 (defun speedbar-tag-expand (text token indent)
3501 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3502 Etags does not support this feature. TEXT will be the button
3503 string. TOKEN will be the list, and INDENT is the current indentation
3504 level."
3505 (cond ((string-match "+" text) ;we have to expand this file
3506 (speedbar-change-expand-button-char ?-)
3507 (speedbar-with-writable
3508 (save-excursion
3509 (end-of-line) (forward-char 1)
3510 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
3511 'speedbar-tag-find))))
3512 ((string-match "-" text) ;we have to contract this node
3513 (speedbar-change-expand-button-char ?+)
3514 (speedbar-delete-subblock indent))
3515 (t (error "Ooops... not sure what to do")))
3516 (speedbar-center-buffer-smartly))
3517 \f
3518 ;;; Loading files into the attached frame.
3519 ;;
3520 (defcustom speedbar-select-frame-method 'attached
3521 "*Specify how to select a frame for displaying a file.
3522 A value of 'attached means to use the attached frame (the frame
3523 that speedbar was started from.) A number such as 1 or -1 means to
3524 pass that number to `other-frame' while selecting a frame from speedbar."
3525 :group 'speedbar
3526 :type 'sexp)
3527
3528 (defun speedbar-find-file-in-frame (file)
3529 "This will load FILE into the speedbar attached frame.
3530 If the file is being displayed in a different frame already, then raise that
3531 frame instead."
3532 (let* ((buff (find-file-noselect file))
3533 (bwin (get-buffer-window buff 0)))
3534 (if bwin
3535 (progn
3536 (select-window bwin)
3537 (raise-frame (window-frame bwin)))
3538 (if dframe-power-click
3539 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3540 (if (numberp speedbar-select-frame-method)
3541 (other-frame speedbar-select-frame-method)
3542 (dframe-select-attached-frame speedbar-frame))
3543 (switch-to-buffer buff))))
3544 )
3545
3546 ;;; Centering Utility
3547 ;;
3548 (defun speedbar-center-buffer-smartly ()
3549 "Recenter a speedbar buffer so the current indentation level is all visible.
3550 This assumes that the cursor is on a file, or tag of a file which the user is
3551 interested in."
3552
3553 (save-selected-window
3554
3555 (select-window (get-buffer-window speedbar-buffer t))
3556
3557 (set-buffer speedbar-buffer)
3558
3559 (if (<= (count-lines (point-min) (point-max))
3560 (1- (window-height (selected-window))))
3561 ;; whole buffer fits
3562 (let ((cp (point)))
3563
3564 (goto-char (point-min))
3565 (recenter 0)
3566 (goto-char cp))
3567 ;; too big
3568 (let (depth start end exp p)
3569 (save-excursion
3570 (beginning-of-line)
3571 (setq depth (if (looking-at "[0-9]+")
3572 (string-to-number (buffer-substring-no-properties
3573 (match-beginning 0) (match-end 0)))
3574 0))
3575 (setq exp (format "^%d:" depth)))
3576 (save-excursion
3577 (end-of-line)
3578 (if (re-search-backward exp nil t)
3579 (setq start (point))
3580 (setq start (point-min)))
3581 (save-excursion ;Not sure about this part.
3582 (end-of-line)
3583 (setq p (point))
3584 (while (and (not (re-search-forward exp nil t))
3585 (>= depth 0))
3586 (setq depth (1- depth))
3587 (setq exp (format "^%d:" depth)))
3588 (if (/= (point) p)
3589 (setq end (point))
3590 (setq end (point-max)))))
3591 ;; Now work out the details of centering
3592 (let ((nl (count-lines start end))
3593 (wl (1- (window-height (selected-window))))
3594 (cp (point)))
3595 (if (> nl wl)
3596 ;; We can't fit it all, so just center on cursor
3597 (progn (goto-char start)
3598 (recenter 1))
3599 ;; we can fit everything on the screen, but...
3600 (if (and (pos-visible-in-window-p start (selected-window))
3601 (pos-visible-in-window-p end (selected-window)))
3602 ;; we are all set!
3603 nil
3604 ;; we need to do something...
3605 (goto-char start)
3606 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3607 (lte (count-lines start (point-max))))
3608 (if (and (< (+ newcent lte) (window-height (selected-window)))
3609 (> (- (window-height (selected-window)) lte 1)
3610 newcent))
3611 (setq newcent (- (window-height (selected-window))
3612 lte 1)))
3613 (recenter newcent))))
3614 (goto-char cp))))))
3615 \f
3616 ;;; Tag Management -- List of expanders:
3617 ;;
3618 (defun speedbar-fetch-dynamic-tags (file)
3619 "Return a list of tags generated dynamically from FILE.
3620 This uses the entries in `speedbar-dynamic-tags-function-list'
3621 to find the proper tags. It is up to each of those individual
3622 functions to do caching and flushing if appropriate."
3623 (save-excursion
3624 ;; If a file is in memory, switch to that buffer. This allows
3625 ;; us to use the local variable. If the file is on disk, we
3626 ;; can try a few of the defaults that can get tags without
3627 ;; opening the file.
3628 (if (get-file-buffer file)
3629 (set-buffer (get-file-buffer file)))
3630 ;; If there is a buffer-local value of
3631 ;; speedbar-dynamic-tags-function-list, it will now be available.
3632 (let ((dtf speedbar-dynamic-tags-function-list)
3633 (ret t))
3634 (while (and (eq ret t) dtf)
3635 (setq ret
3636 (if (fboundp (car (car dtf)))
3637 (funcall (car (car dtf)) file)
3638 t))
3639 (if (eq ret t)
3640 (setq dtf (cdr dtf))))
3641 (if (eq ret t)
3642 ;; No valid tag list, return nil
3643 nil
3644 ;; We have some tags. Return the list with the insert fn
3645 ;; prepended
3646 (cons (cdr (car dtf)) ret)))))
3647
3648 ;;; Tag Management -- Imenu
3649 ;;
3650 (if (not speedbar-use-imenu-flag)
3651
3652 nil
3653
3654 (eval-when-compile (condition-case nil (require 'imenu) (error nil)))
3655
3656 (defun speedbar-fetch-dynamic-imenu (file)
3657 "Load FILE into a buffer, and generate tags using Imenu.
3658 Returns the tag list, or t for an error."
3659 ;; Load this AND compile it in
3660 (require 'imenu)
3661 (set-buffer (find-file-noselect file))
3662 (if dframe-power-click (setq imenu--index-alist nil))
3663 (condition-case nil
3664 (let ((index-alist (imenu--make-index-alist t)))
3665 (if speedbar-sort-tags
3666 (sort (copy-alist index-alist)
3667 (lambda (a b) (string< (car a) (car b))))
3668 index-alist))
3669 (error t)))
3670 )
3671 \f
3672 ;;; Tag Management -- etags (old XEmacs compatibility part)
3673 ;;
3674 (defvar speedbar-fetch-etags-parse-list
3675 '(;; Note that java has the same parse-group as c
3676 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\|cxx\\|hxx\\)\\'" .
3677 speedbar-parse-c-or-c++tag)
3678 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3679 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
3680 ; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3681 ; speedbar-parse-fortran77-tag)
3682 ("\\.tex\\'" . speedbar-parse-tex-string)
3683 ("\\.p\\'" .
3684 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3685 )
3686 "Associations of file extensions and expressions for extracting tags.
3687 To add a new file type, you would want to add a new association to the
3688 list, where the car is the file match, and the cdr is the way to
3689 extract an element from the tags output. If the output is complex,
3690 use a function symbol instead of regexp. The function should expect
3691 to be at the beginning of a line in the etags buffer.
3692
3693 This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3694
3695 (defvar speedbar-fetch-etags-command "etags"
3696 "*Command used to create an etags file.
3697
3698 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3699
3700 (defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3701 "*List of arguments to use with `speedbar-fetch-etags-command'.
3702 This creates an etags output buffer. Use `speedbar-toggle-etags' to
3703 modify this list conveniently.
3704
3705 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3706
3707 (defun speedbar-toggle-etags (flag)
3708 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3709 FLAG then becomes a member of etags command line arguments. If flag
3710 is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3711 value is \"show\" then toggle the value of
3712 `speedbar-show-unknown-files'.
3713
3714 This function is a convenience function for XEmacs menu created by
3715 Farzin Guilak <farzin@protocol.com>."
3716 (interactive)
3717 (cond
3718 ((equal flag "sort")
3719 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3720 ((equal flag "show")
3721 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3722 ((or (equal flag "-C")
3723 (equal flag "-S")
3724 (equal flag "-D"))
3725 (if (member flag speedbar-fetch-etags-arguments)
3726 (setq speedbar-fetch-etags-arguments
3727 (delete flag speedbar-fetch-etags-arguments))
3728 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3729 (t nil)))
3730
3731 (defun speedbar-fetch-dynamic-etags (file)
3732 "For FILE, run etags and create a list of symbols extracted.
3733 Each symbol will be associated with its line position in FILE."
3734 (let ((newlist nil))
3735 (unwind-protect
3736 (save-excursion
3737 (if (get-buffer "*etags tmp*")
3738 (kill-buffer "*etags tmp*")) ;kill to clean it up
3739 (if (<= 1 speedbar-verbosity-level)
3740 (speedbar-message "Fetching etags..."))
3741 (set-buffer (get-buffer-create "*etags tmp*"))
3742 (apply 'call-process speedbar-fetch-etags-command nil
3743 (current-buffer) nil
3744 (append speedbar-fetch-etags-arguments (list file)))
3745 (goto-char (point-min))
3746 (if (<= 1 speedbar-verbosity-level)
3747 (speedbar-message "Fetching etags..."))
3748 (let ((expr
3749 (let ((exprlst speedbar-fetch-etags-parse-list)
3750 (ans nil))
3751 (while (and (not ans) exprlst)
3752 (if (string-match (car (car exprlst)) file)
3753 (setq ans (car exprlst)))
3754 (setq exprlst (cdr exprlst)))
3755 (cdr ans))))
3756 (if expr
3757 (let (tnl)
3758 (set-buffer (get-buffer-create "*etags tmp*"))
3759 (while (not (save-excursion (end-of-line) (eobp)))
3760 (save-excursion
3761 (setq tnl (speedbar-extract-one-symbol expr)))
3762 (if tnl (setq newlist (cons tnl newlist)))
3763 (forward-line 1)))
3764 (speedbar-message
3765 "Sorry, no support for a file of that extension"))))
3766 )
3767 (if speedbar-sort-tags
3768 (sort newlist (lambda (a b) (string< (car a) (car b))))
3769 (reverse newlist))))
3770
3771 ;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3772 ;; sure it's needed with the different sorting method.
3773 ;;
3774 ;(defun speedbar-clean-etags()
3775 ; "Removes spaces before the ^? character, and removes `#define',
3776 ;return types, etc. preceding tags. This ensures that the sort operation
3777 ;works on the tags, not the return types."
3778 ; (save-excursion
3779 ; (goto-char (point-min))
3780 ; (while
3781 ; (re-search-forward "(?[ \t](?\C-?" nil t)
3782 ; (replace-match "\C-?" nil nil))
3783 ; (goto-char (point-min))
3784 ; (while
3785 ; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3786 ; (delete-region (match-beginning 1) (match-end 1)))))
3787
3788 (defun speedbar-extract-one-symbol (expr)
3789 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
3790 The line should contain output from etags. Parse the output using the
3791 regular expression EXPR."
3792 (let* ((sym (if (stringp expr)
3793 (if (save-excursion
3794 (re-search-forward expr (save-excursion
3795 (end-of-line)
3796 (point)) t))
3797 (buffer-substring-no-properties (match-beginning 1)
3798 (match-end 1)))
3799 (funcall expr)))
3800 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3801 (save-excursion
3802 (end-of-line)
3803 (point))
3804 t)))
3805 (if (and j sym)
3806 (1+ (string-to-number (buffer-substring-no-properties
3807 (match-beginning 2)
3808 (match-end 2))))
3809 0))))
3810 (if (/= pos 0)
3811 (cons sym pos)
3812 nil)))
3813
3814 (defun speedbar-parse-c-or-c++tag ()
3815 "Parse a C or C++ tag, which tends to be a little complex."
3816 (save-excursion
3817 (let ((bound (save-excursion (end-of-line) (point))))
3818 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3819 (buffer-substring-no-properties (match-beginning 1)
3820 (match-end 1)))
3821 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3822 (buffer-substring-no-properties (match-beginning 1)
3823 (match-end 1)))
3824 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3825 (buffer-substring-no-properties (match-beginning 1)
3826 (match-end 1)))
3827 (t nil))
3828 )))
3829
3830 (defun speedbar-parse-tex-string ()
3831 "Parse a Tex string. Only find data which is relevant."
3832 (save-excursion
3833 (let ((bound (save-excursion (end-of-line) (point))))
3834 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3835 (buffer-substring-no-properties (match-beginning 0)
3836 (match-end 0)))
3837 (t nil)))))
3838
3839 \f
3840 ;;; BUFFER DISPLAY mode.
3841 ;;
3842 (defvar speedbar-buffers-key-map nil
3843 "Keymap used when in the buffers display mode.")
3844
3845 (if speedbar-buffers-key-map
3846 nil
3847 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3848
3849 ;; Basic tree features
3850 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3851 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3852 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3853 (define-key speedbar-buffers-key-map "=" 'speedbar-expand-line)
3854 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3855 (define-key speedbar-buffers-key-map " " 'speedbar-toggle-line-expansion)
3856
3857 ;; Buffer specific keybindings
3858 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3859 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3860
3861 )
3862
3863 (defvar speedbar-buffer-easymenu-definition
3864 '(["Jump to buffer" speedbar-edit-line t]
3865 ["Expand File Tags" speedbar-expand-line
3866 (save-excursion (beginning-of-line)
3867 (looking-at "[0-9]+: *.\\+. "))]
3868 ["Flush Cache & Expand" speedbar-flush-expand-line
3869 (save-excursion (beginning-of-line)
3870 (looking-at "[0-9]+: *.\\+. "))]
3871 ["Contract File Tags" speedbar-contract-line
3872 (save-excursion (beginning-of-line)
3873 (looking-at "[0-9]+: *.-. "))]
3874 "----"
3875 ["Kill Buffer" speedbar-buffer-kill-buffer
3876 (save-excursion (beginning-of-line)
3877 (looking-at "[0-9]+: *.[-+?]. "))]
3878 ["Revert Buffer" speedbar-buffer-revert-buffer
3879 (save-excursion (beginning-of-line)
3880 (looking-at "[0-9]+: *.[-+?]. "))]
3881 )
3882 "Menu item elements shown when displaying a buffer list.")
3883
3884 (defun speedbar-buffer-buttons (directory zero)
3885 "Create speedbar buttons based on the buffers currently loaded.
3886 DIRECTORY is the directory to the currently active buffer, and ZERO is 0."
3887 (speedbar-buffer-buttons-engine nil))
3888
3889 (defun speedbar-buffer-buttons-temp (directory zero)
3890 "Create speedbar buttons based on the buffers currently loaded.
3891 DIRECTORY is the directory to the currently active buffer, and ZERO is 0."
3892 (speedbar-buffer-buttons-engine t))
3893
3894 (defun speedbar-buffer-buttons-engine (temp)
3895 "Create speedbar buffer buttons.
3896 If TEMP is non-nil, then clicking on a buffer restores the previous display."
3897 (speedbar-insert-separator "Active Buffers:")
3898 (let ((bl (buffer-list))
3899 (case-fold-search t))
3900 (while bl
3901 (if (string-match "^[ *]" (buffer-name (car bl)))
3902 nil
3903 (let* ((known (string-match speedbar-file-regexp
3904 (buffer-name (car bl))))
3905 (expchar (if known ?+ ??))
3906 (fn (if known 'speedbar-tag-file nil))
3907 (fname (save-excursion (set-buffer (car bl))
3908 (buffer-file-name))))
3909 (speedbar-make-tag-line 'bracket expchar fn
3910 (if fname (file-name-nondirectory fname))
3911 (buffer-name (car bl))
3912 'speedbar-buffer-click temp
3913 'speedbar-file-face 0)
3914 (speedbar-buffers-tail-notes (car bl))))
3915 (setq bl (cdr bl)))
3916 (setq bl (buffer-list))
3917 (speedbar-insert-separator "Scratch Buffers:")
3918 (while bl
3919 (if (not (string-match "^\\*" (buffer-name (car bl))))
3920 nil
3921 (if (eq (car bl) speedbar-buffer)
3922 nil
3923 (speedbar-make-tag-line 'bracket ?? nil nil
3924 (buffer-name (car bl))
3925 'speedbar-buffer-click temp
3926 'speedbar-file-face 0)
3927 (speedbar-buffers-tail-notes (car bl))))
3928 (setq bl (cdr bl)))
3929 (setq bl (buffer-list))
3930 ;;(speedbar-insert-separator "Hidden Buffers:")
3931 ;;(while bl
3932 ;; (if (not (string-match "^ " (buffer-name (car bl))))
3933 ;; nil
3934 ;; (if (eq (car bl) speedbar-buffer)
3935 ;; nil
3936 ;; (speedbar-make-tag-line 'bracket ?? nil nil
3937 ;; (buffer-name (car bl))
3938 ;; 'speedbar-buffer-click temp
3939 ;; 'speedbar-file-face 0)
3940 ;; (speedbar-buffers-tail-notes (car bl))))
3941 ;; (setq bl (cdr bl)))
3942 ))
3943
3944 (defun speedbar-buffers-tail-notes (buffer)
3945 "Add a note to the end of the last tag line.
3946 Argument BUFFER is the buffer being tested."
3947 (let (mod ro)
3948 (save-excursion
3949 (set-buffer buffer)
3950 (setq mod (buffer-modified-p)
3951 ro buffer-read-only))
3952 (if ro (speedbar-insert-button "%" nil nil nil nil t))))
3953
3954 (defun speedbar-buffers-item-info ()
3955 "Display information about the current buffer on the current line."
3956 (or (speedbar-item-info-tag-helper)
3957 (let* ((item (speedbar-line-text))
3958 (buffer (if item (get-buffer item) nil)))
3959 (and buffer
3960 (speedbar-message "%s%s %S %d %s"
3961 (if (buffer-modified-p buffer) "* " "")
3962 item
3963 (save-excursion (set-buffer buffer) major-mode)
3964 (save-excursion (set-buffer buffer)
3965 (buffer-size))
3966 (or (buffer-file-name buffer) "<No file>"))))))
3967
3968 (defun speedbar-buffers-line-directory (&optional depth)
3969 "Fetch the full directory to the file (buffer) specified on the current line.
3970 Optional argument DEPTH specifies the current depth of the back search."
3971 (save-excursion
3972 (end-of-line)
3973 (let ((start (point)))
3974 ;; Buffers are always at level 0
3975 (if (not (re-search-backward "^0:" nil t))
3976 nil
3977 (let* ((bn (speedbar-line-text))
3978 (buffer (if bn (get-buffer bn))))
3979 (if buffer
3980 (if (save-excursion
3981 (end-of-line)
3982 (eq start (point)))
3983 (or (save-excursion (set-buffer buffer)
3984 default-directory)
3985 "")
3986 (buffer-file-name buffer))))))))
3987
3988 (defun speedbar-buffer-click (text token indent)
3989 "When the users clicks on a buffer-button in speedbar.
3990 TEXT is the buffer's name, TOKEN and INDENT are unused."
3991 (if dframe-power-click
3992 (let ((pop-up-frames t)) (select-window (display-buffer text)))
3993 (dframe-select-attached-frame speedbar-frame)
3994 (switch-to-buffer text)
3995 (if token (speedbar-change-initial-expansion-list
3996 speedbar-previously-used-expansion-list-name))))
3997
3998 (defun speedbar-buffer-kill-buffer ()
3999 "Kill the buffer the cursor is on in the speedbar buffer."
4000 (interactive)
4001 (or (save-excursion
4002 (let ((text (speedbar-line-text)))
4003 (if (and (get-buffer text)
4004 (speedbar-y-or-n-p (format "Kill buffer %s? " text)))
4005 (kill-buffer text))
4006 (speedbar-refresh)))))
4007
4008 (defun speedbar-buffer-revert-buffer ()
4009 "Revert the buffer the cursor is on in the speedbar buffer."
4010 (interactive)
4011 (save-excursion
4012 (beginning-of-line)
4013 ;; If this fails, then it is a non-standard click, and as such,
4014 ;; perfectly allowed
4015 (if (re-search-forward "[]>?}] [^ ]"
4016 (save-excursion (end-of-line) (point))
4017 t)
4018 (let ((text (progn
4019 (forward-char -1)
4020 (buffer-substring (point) (save-excursion
4021 (end-of-line)
4022 (point))))))
4023 (if (get-buffer text)
4024 (progn
4025 (set-buffer text)
4026 (revert-buffer t)))))))
4027
4028 \f
4029 ;;; Useful hook values and such.
4030 ;;
4031 (defvar speedbar-highlight-one-tag-line nil
4032 "Overlay used for highlighting the most recently jumped to tag line.")
4033
4034 (defun speedbar-highlight-one-tag-line ()
4035 "Highlight the current line, unhighlighting a previously jumped to line."
4036 (speedbar-unhighlight-one-tag-line)
4037 (setq speedbar-highlight-one-tag-line
4038 (speedbar-make-overlay (save-excursion (beginning-of-line) (point))
4039 (save-excursion (end-of-line)
4040 (forward-char 1)
4041 (point))))
4042 (speedbar-overlay-put speedbar-highlight-one-tag-line 'face
4043 'speedbar-highlight-face)
4044 (add-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line)
4045 )
4046
4047 (defun speedbar-unhighlight-one-tag-line ()
4048 "Unhighlight the currently highlighted line."
4049 (if speedbar-highlight-one-tag-line
4050 (progn
4051 (speedbar-delete-overlay speedbar-highlight-one-tag-line)
4052 (setq speedbar-highlight-one-tag-line nil)))
4053 (remove-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line))
4054
4055 (defun speedbar-recenter-to-top ()
4056 "Recenter the current buffer so POINT is on the top of the window."
4057 (recenter 1))
4058
4059 (defun speedbar-recenter ()
4060 "Recenter the current buffer so POINT is in the center of the window."
4061 (recenter (/ (window-height (selected-window)) 2)))
4062
4063 \f
4064 ;;; Color loading section.
4065 ;;
4066 (defface speedbar-button-face '((((class color) (background light))
4067 (:foreground "green4"))
4068 (((class color) (background dark))
4069 (:foreground "green3")))
4070 "Face used for +/- buttons."
4071 :group 'speedbar-faces)
4072
4073 (defface speedbar-file-face '((((class color) (background light))
4074 (:foreground "cyan4"))
4075 (((class color) (background dark))
4076 (:foreground "cyan"))
4077 (t (:bold t)))
4078 "Face used for file names."
4079 :group 'speedbar-faces)
4080
4081 (defface speedbar-directory-face '((((class color) (background light))
4082 (:foreground "blue4"))
4083 (((class color) (background dark))
4084 (:foreground "light blue")))
4085 "Face used for directory names."
4086 :group 'speedbar-faces)
4087 (defface speedbar-tag-face '((((class color) (background light))
4088 (:foreground "brown"))
4089 (((class color) (background dark))
4090 (:foreground "yellow")))
4091 "Face used for displaying tags."
4092 :group 'speedbar-faces)
4093
4094 (defface speedbar-selected-face '((((class color) (background light))
4095 (:foreground "red" :underline t))
4096 (((class color) (background dark))
4097 (:foreground "red" :underline t))
4098 (t (:underline t)))
4099 "Face used to underline the file in the active window."
4100 :group 'speedbar-faces)
4101
4102 (defface speedbar-highlight-face '((((class color) (background light))
4103 (:background "green"))
4104 (((class color) (background dark))
4105 (:background "sea green"))
4106 (((class grayscale monochrome)
4107 (background light))
4108 (:background "black"))
4109 (((class grayscale monochrome)
4110 (background dark))
4111 (:background "white")))
4112 "Face used for highlighting buttons with the mouse."
4113 :group 'speedbar-faces)
4114
4115 (defface speedbar-separator-face '((((class color) (background light))
4116 (:background "blue"
4117 :foreground "white"
4118 :overline "gray"))
4119 (((class color) (background dark))
4120 (:background "blue"
4121 :foreground "white"
4122 :overline "gray"))
4123 (((class grayscale monochrome)
4124 (background light))
4125 (:background "black"
4126 :foreground "white"
4127 :overline "white"))
4128 (((class grayscale monochrome)
4129 (background dark))
4130 (:background "white"
4131 :foreground "black"
4132 :overline "black")))
4133 "Face used for separator labes in a display."
4134 :group 'speedbar-faces)
4135
4136 ;; some edebug hooks
4137 (add-hook 'edebug-setup-hook
4138 (lambda ()
4139 (def-edebug-spec speedbar-with-writable def-body)))
4140
4141 ;; Fix a font lock problem for some versions of Emacs
4142 (if (boundp 'font-lock-global-modes)
4143 (if (listp font-lock-global-modes)
4144 (add-to-list 'font-lock-global-modes '(not speedbar-mode))
4145 )
4146 )
4147
4148 \f
4149 ;;; Obsolete variables and functions
4150
4151 (define-obsolete-variable-alias
4152 'speedbar-ignored-path-regexp 'speedbar-ignored-directory-regexp)
4153
4154 (define-obsolete-variable-alias 'speedbar-ignored-path-expressions
4155 'speedbar-ignored-directory-expressions)
4156
4157 (define-obsolete-function-alias 'speedbar-add-ignored-path-regexp
4158 'speedbar-add-ignored-directory-regexp)
4159
4160 (define-obsolete-function-alias 'speedbar-line-path
4161 'speedbar-line-directory)
4162
4163 (define-obsolete-function-alias 'speedbar-buffers-line-path
4164 'speedbar-buffers-line-directory)
4165
4166 (define-obsolete-function-alias 'speedbar-path-line
4167 'speedbar-directory-line)
4168
4169 (define-obsolete-function-alias 'speedbar-buffers-line-path
4170 'speedbar-buffers-line-directory)
4171
4172 (provide 'speedbar)
4173
4174 ;; run load-time hooks
4175 (run-hooks 'speedbar-load-hook)
4176
4177 ;; arch-tag: 4477e6d1-f78c-48b9-a503-387d3c9767d5
4178 ;;; speedbar ends here