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