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