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