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