1 ;;; speedbar --- quick access to files and tags in a frame
3 ;;; Copyright (C) 1996, 97, 98 Free Software Foundation
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: file, tags, tools
8 ;; X-RCS: $Id: speedbar.el,v 1.17 1998/10/04 13:00:45 zappo Exp zappo $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; The speedbar provides a frame in which files, and locations in
30 ;; files are displayed. These items can be clicked on with mouse-2
31 ;; in order to make the last active frame display that file location.
35 ;; If speedbar came to you as a part of Emacs, simply type
36 ;; `M-x speedbar', and it will be autoloaded for you.
38 ;; If speedbar is not a part of your distribution, then add
39 ;; this to your .emacs file:
41 ;; (autoload 'speedbar-frame-mode "speedbar" "Popup a speedbar frame" t)
42 ;; (autoload 'speedbar-get-focus "speedbar" "Jump to speedbar frame" t)
44 ;; If you want to choose it from a menu, such as "Tools", you can do this:
47 ;; (define-key-after (lookup-key global-map [menu-bar tools])
48 ;; [speedbar] '("Speedbar" . speedbar-frame-mode) [calendar])
51 ;; (add-menu-button '("Tools")
52 ;; ["Speedbar" speedbar-frame-mode
54 ;; :selected (and (boundp 'speedbar-frame)
55 ;; (frame-live-p speedbar-frame)
56 ;; (frame-visible-p speedbar-frame))]
59 ;; If you want to access speedbar using only the keyboard, do this:
61 ;; (global-set-key [(f4)] 'speedbar-get-focus)
63 ;; This will let you hit f4 (or whatever key you choose) to jump
64 ;; focus to the speedbar frame. Pressing it again will bring you back
65 ;; to the attached frame. Pressing RET or e to jump to a file
66 ;; or tag will move you back to the attached frame. The command
67 ;; `speedbar-get-focus' will also create a speedbar frame if it does
70 ;; Customizing Speedbar:
72 ;; Once a speedbar frame is active, it takes advantage of idle time
73 ;; to keep its contents updated. The contents is usually a list of
74 ;; files in the directory of the currently active buffer. When
75 ;; applicable, tags in the active file can be expanded.
77 ;; To add new supported files types into speedbar, use the function
78 ;; `speedbar-add-supported-extension' If speedbar complains that the
79 ;; file type is not supported, that means there is no built in
80 ;; support from imenu, and the etags part wasn't set up correctly. You
81 ;; may add elements to `speedbar-supported-extension-expressions' as long
82 ;; as it is done before speedbar is loaded.
84 ;; To prevent speedbar from following you into certain directories
85 ;; use the function `speedbar-add-ignored-path-regexp' too add a new
86 ;; regular expression matching a type of path. You may add list
87 ;; elements to `speedbar-ignored-path-expressions' as long as it is
88 ;; done before speedbar is loaded.
90 ;; To add new file types to imenu, see the documentation in the
91 ;; file imenu.el that comes with Emacs. To add new file types which
92 ;; etags supports, you need to modify the variable
93 ;; `speedbar-fetch-etags-parse-list'.
95 ;; If the updates are going too slow for you, modify the variable
96 ;; `speedbar-update-speed' to a longer idle time before updates.
98 ;; If you navigate directories, you will probably notice that you
99 ;; will navigate to a directory which is eventually replaced after
100 ;; you go back to editing a file (unless you pull up a new file.)
101 ;; The delay time before this happens is in
102 ;; `speedbar-navigating-speed', and defaults to 10 seconds.
104 ;; To enable mouse tracking with information in the minibuffer of
105 ;; the attached frame, use the variable `speedbar-track-mouse-flag'.
107 ;; Tag layout can be modified through `speedbar-tag-hierarchy-method',
108 ;; which controls how tags are layed out. It is actually a list of
109 ;; functions that filter the data. The default groups large tag lists
110 ;; into sub-lists. A long flat list can be used instead if needed.
111 ;; Other filters could be easily added.
113 ;; Users of XEmacs previous to 20 may want to change the default
114 ;; timeouts for `speedbar-update-speed' to something longer as XEmacs
115 ;; doesn't have idle timers, the speedbar timer keeps going off
116 ;; arbitrarily while you're typing. It's quite pesky.
118 ;; Users of really old emacsen without the needed timers will not
119 ;; have speedbar updating automatically. Use "r" to refresh the
120 ;; display after changing directories. Remember, do not interrupt the
121 ;; stealthy updates or your display may not be completely refreshed.
123 ;; AUC-TEX users: The imenu tags for AUC-TEX mode don't work very
124 ;; well. Use the imenu keywords from tex-mode.el for better results.
126 ;; This file requires the library package assoc (association lists)
127 ;; and the package custom (for easy configuration of speedbar)
128 ;; http://www.dina.kvl.dk/~abraham/custom/
130 ;;; Developing for speedbar
132 ;; Adding a speedbar specialized display mode:
134 ;; Speedbar can be configured to create a special display for certain
135 ;; modes that do not display tradition file/tag data. Rmail, Info,
136 ;; and the debugger are examples. These modes can, however, benefit
137 ;; from a speedbar style display in their own way.
139 ;; If your `major-mode' is `foo-mode', the only requirement is to
140 ;; create a function called `foo-speedbar-buttons' which takes one
141 ;; argument, BUFFER. BUFFER will be the buffer speedbar wants filled.
142 ;; In `foo-speedbar-buttons' there are several functions that make
143 ;; building a speedbar display easy. See the documentation for
144 ;; `speedbar-with-writable' (needed because the buffer is usually
145 ;; read-only) `speedbar-make-tag-line', `speedbar-insert-button', and
146 ;; `speedbar-insert-generic-list'. If you use
147 ;; `speedbar-insert-generic-list', also read the doc for
148 ;; `speedbar-tag-hierarchy-method' in case you wish to override it.
149 ;; The function `speedbar-with-attached-buffer' brings you back to the
150 ;; buffer speedbar is displaying for.
152 ;; For those functions that make buttons, the "function" should be a
153 ;; symbol that is the function to call when clicked on. The "token"
154 ;; is extra data you can pass along. The "function" must take three
155 ;; parameters. They are (TEXT TOKEN INDENT). TEXT is the text of the
156 ;; button clicked on. TOKEN is the data passed in when you create the
157 ;; button. INDENT is an indentation level, or 0. You can store
158 ;; indentation levels with `speedbar-make-tag-line' which creates a
159 ;; line with an expander (eg. [+]) and a text button.
161 ;; Some useful functions when writing expand functions, and click
162 ;; functions are `speedbar-change-expand-button-char',
163 ;; `speedbar-delete-subblock', and `speedbar-center-buffer-smartly'.
164 ;; The variable `speedbar-power-click' is set to t in your functions
165 ;; when the user shift-clicks. This indications anything from
166 ;; refreshing cached data to making a buffer appear in a new frame.
168 ;; If you wish to add to the default speedbar menu for the case of
169 ;; `foo-mode', create a variable `foo-speedbar-menu-items'. This
170 ;; should be a list compatible with the `easymenu' package. It will
171 ;; be spliced into the main menu. (Available with click-mouse-3). If
172 ;; you wish to have extra key bindings in your special mode, create a
173 ;; variable `foo-speedbar-key-map'. Instead of using `make-keymap',
174 ;; or `make-sparse-keymap', use the function
175 ;; `speedbar-make-specialized-keymap'. This lets you inherit all of
176 ;; speedbar's default bindings with low overhead.
178 ;; Adding a speedbar top-level display mode:
180 ;; Unlike the specialized modes, there are no name requirements,
181 ;; however the methods for writing a button display, menu, and keymap
182 ;; are the same. Once you create these items, you can call the
183 ;; function `speedbar-add-expansion-list'. It takes one parameter
184 ;; which is a list element of the form (NAME MENU KEYMAP &rest
185 ;; BUTTON-FUNCTIONS). NAME is a string that will show up in the
186 ;; Displays menu item. MENU is a symbol containing the menu items to
187 ;; splice in. KEYMAP is a symbol holding the keymap to use, and
188 ;; BUTTON-FUNCTIONS are the function names to call, in order, to create
192 ;; - More functions to create buttons and options
193 ;; - Timeout directories we haven't visited in a while.
194 ;; - Remeber tags when refreshing the display. (Refresh tags too?)
195 ;; - More 'special mode support.
200 (defvar speedbar-xemacsp (string-match "XEmacs" emacs-version)
201 "Non-nil if we are running in the XEmacs environment.")
202 (defvar speedbar-xemacs20p (and speedbar-xemacsp
203 (= emacs-major-version 20)))
205 ;; From custom web page for compatibility between versions of custom:
210 (if (and (featurep 'custom) (fboundp 'custom-declare-variable)
211 ;; Some XEmacsen w/ custom don't have :set keyword.
212 ;; This protects them against custom.
213 (fboundp 'custom-initialize-set))
214 nil ;; We've got what we needed
215 ;; We have the old custom-library, hack around it!
216 (defmacro defgroup (&rest args)
218 (defmacro defface (var values doc &rest args)
220 (defvar (, var) (quote (, var)))
221 ;; To make colors for your faces you need to set your .Xdefaults
222 ;; or set them up ahead of time in your .emacs file.
225 (defmacro defcustom (var value doc &rest args)
226 (` (defvar (, var) (, value) (, doc))))))
228 ;; customization stuff
229 (defgroup speedbar nil
230 "File and tag browser frame."
236 (defgroup speedbar-faces nil
237 "Faces used in speedbar."
242 (defgroup speedbar-vc nil
243 "Version control display in speedbar."
248 (defvar speedbar-initial-expansion-mode-alist
249 '(("buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
250 speedbar-buffer-buttons)
251 ("quick buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
252 speedbar-buffer-buttons-temp)
253 ;; Files last, means first in the Displays menu
254 ("files" speedbar-easymenu-definition-special speedbar-file-key-map
255 speedbar-directory-buttons speedbar-default-directory-list)
257 "List of named expansion elements for filling the speedbar frame.
258 These expansion lists are only valid for regular files. Special modes
259 still get to override this list on a mode-by-mode basis. This list of
260 lists is of the form (NAME MENU KEYMAP FN1 FN2 ...). NAME is a string
261 representing the types of things to be displayed. MENU is an easymenu
262 structure used when in this mode. KEYMAP is a local keymap to install
263 over the regular speedbar keymap. FN1 ... are functions that will be
264 called in order. These functions will always get the default
265 directory to use passed in as the first parameter, and a 0 as the
266 second parameter. The 0 indicates the uppermost indentation level.
267 They must assume that the cursor is at the position where they start
270 (defvar speedbar-initial-expansion-list-name "files"
271 "A symbol name representing the expansion list to use.
272 The expansion list `speedbar-initial-expansion-mode-alist' contains
273 the names and associated functions to use for buttons in speedbar.")
275 (defvar speedbar-previously-used-expansion-list-name "files"
276 "Save the last expansion list method.
277 This is used for returning to a previous expansion list method when
278 the user is done with the current expansion list.")
280 (defvar speedbar-stealthy-function-list
282 speedbar-update-current-file speedbar-check-vc speedbar-check-objects)
284 "List of functions to periodically call stealthily.
285 This list is of the form:
286 '( (\"NAME\" FUNCTION ...)
288 where NAME is the name of the major display mode these functions are
289 for, and the remaining elements FUNCTION are functions to call in order.
290 Each function must return nil if interrupted, or t if completed.
291 Stealthy functions which have a single operation should always return
292 t. Functions which take a long time should maintain a state (where
293 they are in their speedbar related calculations) and permit
294 interruption. See `speedbar-check-vc' as a good example.")
296 (defcustom speedbar-mode-specific-contents-flag t
297 "*Non-nil means speedbar will show special mode contents.
298 This permits some modes to create customized contents for the speedbar
303 (defvar speedbar-special-mode-expansion-list nil
304 "Default function list for creating specialized button lists.
305 This list is set by modes that wish to have special speedbar displays.
306 The list is of function names. Each function is called with one
307 parameter BUFFER, the originating buffer. The current buffer is the
310 (defvar speedbar-special-mode-key-map nil
311 "Default keymap used when identifying a specialized display mode.
312 This keymap is local to each buffer that wants to define special keybindings
313 effective when it's display is shown.")
315 (defcustom speedbar-visiting-file-hook nil
316 "Hooks run when speedbar visits a file in the selected frame."
320 (defcustom speedbar-visiting-tag-hook nil
321 "Hooks run when speedbar visits a tag in the selected frame."
325 (defcustom speedbar-load-hook nil
326 "Hooks run when speedbar is loaded."
330 (defcustom speedbar-show-unknown-files nil
331 "*Non-nil show files we can't expand with a ? in the expand button.
332 nil means don't show the file in the list."
336 (defcustom speedbar-update-speed
338 (if speedbar-xemacs20p
339 2 ; 1 is too obrusive in XEmacs
340 5) ; when no idleness, need long delay
342 "*Idle time in seconds needed before speedbar will update itself.
343 Updates occur to allow speedbar to display directory information
344 relevant to the buffer you are currently editing."
348 ;; When I moved to a repeating timer, I had the horrible missfortune
349 ;; of loosing the ability for adaptive speed choice. This update
350 ;; speed currently causes long delays when it should have been turned off.
351 (defcustom speedbar-navigating-speed speedbar-update-speed
352 "*Idle time to wait after navigation commands in speedbar are executed.
353 Navigation commands included expanding/contracting nodes, and moving
354 between different directories."
358 (defcustom speedbar-frame-parameters '((minibuffer . nil)
363 "*Parameters to use when creating the speedbar frame in Emacs.
364 Parameters not listed here which will be added automatically are
365 `height' which will be initialized to the height of the frame speedbar
368 :type '(repeat (sexp :tag "Parameter:")))
370 ;; These values by Hrvoje Niksic <hniksic@srce.hr>
371 (defcustom speedbar-frame-plist
372 '(minibuffer nil width 20 border-width 0
373 internal-border-width 0 unsplittable t
374 default-toolbar-visible-p nil has-modeline-p nil
375 menubar-visible-p nil)
376 "*Parameters to use when creating the speedbar frame in XEmacs.
377 Parameters not listed here which will be added automatically are
378 `height' which will be initialized to the height of the frame speedbar
381 :type '(repeat (group :inline t
382 (symbol :tag "Property")
383 (sexp :tag "Value"))))
385 (defcustom speedbar-use-imenu-flag (stringp (locate-library "imenu"))
386 "*Non-nil means use imenu for file parsing. nil to use etags.
387 XEmacs prior to 20.4 doesn't support imenu, therefore the default is to
388 use etags instead. Etags support is not as robust as imenu support."
393 (defcustom speedbar-track-mouse-flag t
394 "*Non-nil means to display info about the line under the mouse."
398 (defcustom speedbar-sort-tags nil
399 "*If Non-nil, sort tags in the speedbar display. *Obsolete*."
403 (defcustom speedbar-tag-hierarchy-method
404 '(prefix-group trim-words)
405 "*List of methods which speedbar will use to organize tags into groups.
406 Groups are defined as expandable meta-tags. Imenu supports such
407 things in some languages, such as separating variables from functions.
408 Available methods are:
409 sort - Sort tags. (sometimes unnecessary)
410 trim-words - Trim all tags by a common prefix, broken @ word sections.
411 prefix-group - Try to guess groups by prefix.
412 simple-group - If imenu already returned some meta groups, stick all
413 tags that are not in a group into a sub-group."
417 (const :tag "Sort the tags." sort)
418 (const :tag "Trim words to common prefix." trim-words)
419 (const :tag "Create groups from common prefixes." prefix-group)
420 (const :tag "Group loose tags into their own group." simple-group))
423 (defcustom speedbar-tag-group-name-minimum-length 4
424 "*The minimum length of a prefix group name before expanding.
425 Thus, if the `speedbar-tag-hierarchy-method' includes `prefix-group'
426 and one such groups common characters is less than this number of
427 characters, then the group name will be changed to the form of:
431 This way we won't get silly looking listings."
435 (defcustom speedbar-tag-split-minimum-length 20
436 "*Minimum length before we stop trying to create sub-lists in tags.
437 This is used by all tag-hierarchy methods that break large lists into
442 (defcustom speedbar-tag-regroup-maximum-length 10
443 "*Maximum length of submenus that are regrouped.
444 If the regrouping option is used, then if two or more short subgroups
445 are next to each other, then they are combined until this number of
450 (defcustom speedbar-activity-change-focus-flag nil
451 "*Non-nil means the selected frame will change based on activity.
452 Thus, if a file is selected for edit, the buffer will appear in the
453 selected frame and the focus will change to that frame."
457 (defcustom speedbar-directory-button-trim-method 'span
458 "*Indicates how the directory button will be displayed.
460 'span - span large directories over multiple lines.
461 'trim - trim large directories to only show the last few.
464 :type '(radio (const :tag "Span large directories over mutiple lines."
466 (const :tag "Trim large directories to only show the last few."
468 (const :tag "No trimming." nil)))
470 (defcustom speedbar-smart-directory-expand-flag t
471 "*Non-nil means speedbar should use smart expansion.
472 Smart expansion only affects when speedbar wants to display a
473 directory for a file in the attached frame. When smart expansion is
474 enabled, new directories which are children of a displayed directory
475 are expanded in the current framework. If nil, then the current
476 hierarchy would be replaced with the new directory."
480 (defvar speedbar-hide-button-brackets-flag nil
481 "*Non-nil means speedbar will hide the brackets around the + or -.")
483 (defcustom speedbar-before-popup-hook nil
484 "*Hooks called before popping up the speedbar frame."
488 (defcustom speedbar-before-delete-hook nil
489 "*Hooks called before deleting the speedbar frame."
493 (defcustom speedbar-mode-hook nil
494 "*Hooks called after creating a speedbar buffer."
498 (defcustom speedbar-timer-hook nil
499 "*Hooks called after running the speedbar timer function."
503 (defcustom speedbar-verbosity-level 1
504 "*Verbosity level of the speedbar. 0 means say nothing.
505 1 means medium level verbosity. 2 and higher are higher levels of
510 (defvar speedbar-indicator-separator " "
511 "String separating file text from indicator characters.")
513 (defcustom speedbar-vc-do-check t
514 "*Non-nil check all files in speedbar to see if they have been checked out.
515 Any file checked out is marked with `speedbar-vc-indicator'"
519 (defvar speedbar-vc-indicator "*"
520 "Text used to mark files which are currently checked out.
521 Currently only RCS is supported. Other version control systems can be
522 added by examining the function `speedbar-this-file-in-vc' and
523 `speedbar-vc-check-dir-p'")
525 (defcustom speedbar-vc-path-enable-hook nil
526 "*Return non-nil if the current path should be checked for Version Control.
527 Functions in this hook must accept one parameter which is the path
532 (defcustom speedbar-vc-in-control-hook nil
533 "*Return non-nil if the specified file is under Version Control.
534 Functions in this hook must accept two parameters. The PATH of the
535 current file, and the FILENAME of the file being checked."
539 (defvar speedbar-vc-to-do-point nil
540 "Local variable maintaining the current version control check position.")
542 (defcustom speedbar-obj-do-check t
543 "*Non-nil check all files in speedbar to see if they have an object file.
544 Any file checked out is marked with `speedbar-obj-indicator', and the
545 marking is based on `speedbar-obj-alist'"
549 (defvar speedbar-obj-to-do-point nil
550 "Local variable maintaining the current version control check position.")
552 (defvar speedbar-obj-indicator '("#" . "!")
553 "Text used to mark files that have a corresponding hidden object file.
554 The car is for an up-to-date object. The cdr is for an out of date object.
555 The expression `speedbar-obj-alist' defines who gets tagged.")
557 (defvar speedbar-obj-alist
558 '(("\\.\\([cpC]\\|cpp\\|cc\\)$" . ".o")
560 ("\\.java$" . ".class")
561 ("\\.f\\(or\\|90\\|77\\)?$" . ".o")
563 ("\\.texi$" . ".info"))
564 "Alist of file extensions, and their corresponding object file type.")
566 (defvar speedbar-indicator-regex
567 (concat (regexp-quote speedbar-indicator-separator)
569 (regexp-quote speedbar-vc-indicator)
571 (regexp-quote (car speedbar-obj-indicator))
573 (regexp-quote (cdr speedbar-obj-indicator))
575 "Regular expression used when identifying files.
576 Permits stripping of indicator characters from a line.")
578 (defcustom speedbar-scanner-reset-hook nil
579 "*Hook called whenever generic scanners are reset.
580 Set this to implement your own scanning / rescan safe functions with
585 (defvar speedbar-ignored-modes nil
586 "*List of major modes which speedbar will not switch directories for.")
588 (defun speedbar-extension-list-to-regex (extlist)
589 "Takes EXTLIST, a list of extensions and transforms it into regexp.
590 All the preceding `.' are stripped for an optimized expression starting
591 with `.' followed by extensions, followed by full-filenames."
592 (let ((regex1 nil) (regex2 nil))
594 (if (= (string-to-char (car extlist)) ?.)
595 (setq regex1 (concat regex1 (if regex1 "\\|" "")
596 (substring (car extlist) 1)))
597 (setq regex2 (concat regex2 (if regex2 "\\|" "") (car extlist))))
598 (setq extlist (cdr extlist)))
599 ;; concat all the sub-exressions together, making sure all types
600 ;; of parts exist during concatination.
602 (if regex1 (concat "\\(\\.\\(" regex1 "\\)\\)") "")
603 (if (and regex1 regex2) "\\|" "")
604 (if regex2 (concat "\\(" regex2 "\\)") "")
607 (defvar speedbar-ignored-path-regexp nil
608 "Regular expression matching paths speedbar will not switch to.
609 Created from `speedbar-ignored-path-expressions' with the function
610 `speedbar-extension-list-to-regex' (A misnamed function in this case.)
611 Use the function `speedbar-add-ignored-path-regexp', or customize the
612 variable `speedbar-ignored-path-expressions' to modify this variable.")
614 (defcustom speedbar-ignored-path-expressions
616 "*List of regular expressions matching directories speedbar will ignore.
617 They should included paths to directories which are notoriously very
618 large and take a long time to load in. Use the function
619 `speedbar-add-ignored-path-regexp' to add new items to this list after
620 speedbar is loaded. You may place anything you like in this list
621 before speedbar has been loaded."
623 :type '(repeat (regexp :tag "Path Regexp"))
624 :set (lambda (sym val)
625 (setq speedbar-ignored-path-expressions val
626 speedbar-ignored-path-regexp
627 (speedbar-extension-list-to-regex val))))
629 (defcustom speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\)\\'"
630 "*Regular expression matching directories not to show in speedbar.
631 They should include commonly existing directories which are not
632 useful, such as version control."
636 (defvar speedbar-file-unshown-regexp
637 (let ((nstr "") (noext completion-ignored-extensions))
639 (setq nstr (concat nstr (regexp-quote (car noext)) "\\'"
640 (if (cdr noext) "\\|" ""))
642 ;; backup refdir lockfile
643 (concat nstr "\\|#[^#]+#$\\|\\.\\.?\\'\\|\\.#"))
644 "*Regexp matching files we don't want displayed in a speedbar buffer.
645 It is generated from the variable `completion-ignored-extensions'")
647 ;; this is dangerous to customize, because the defaults will probably
648 ;; change in the future.
649 (defcustom speedbar-supported-extension-expressions
650 (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
651 ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".f\\(90\\|77\\|or\\)?")
652 (if speedbar-use-imenu-flag
653 '(".ada" ".pl" ".tcl" ".m" ".scm" ".pm" ".py"
654 ;; html is not supported by default, but an imenu tags package
655 ;; is available. Also, html files are nice to be able to see.
657 "Makefile\\(\\.in\\)?")))
658 "*List of regular expressions which will match files supported by tagging.
659 Do not prefix the `.' char with a double \\ to quote it, as the period
660 will be stripped by a simplified optimizer when compiled into a
661 singular expression. This variable will be turned into
662 `speedbar-file-regexp' for use with speedbar. You should use the
663 function `speedbar-add-supported-extension' to add a new extension at
664 runtime, or use the configuration dialog to set it in your .emacs
667 :type '(repeat (regexp :tag "Extension Regexp"))
668 :set (lambda (sym val)
669 (setq speedbar-supported-extension-expressions val
670 speedbar-file-regexp (speedbar-extension-list-to-regex val)))
673 (defvar speedbar-file-regexp
674 (speedbar-extension-list-to-regex speedbar-supported-extension-expressions)
675 "Regular expression matching files we know how to expand.
676 Created from `speedbar-supported-extension-expression' with the
677 function `speedbar-extension-list-to-regex'")
679 (defun speedbar-add-supported-extension (extension)
680 "Add EXTENSION as a new supported extension for speedbar tagging.
681 This should start with a `.' if it is not a complete file name, and
682 the dot should NOT be quoted in with \\. Other regular expression
683 matchers are allowed however. EXTENSION may be a single string or a
685 (interactive "sExtionsion: ")
686 (if (not (listp extension)) (setq extension (list extension)))
688 (if (member (car extension) speedbar-supported-extension-expressions)
690 (setq speedbar-supported-extension-expressions
691 (cons (car extension) speedbar-supported-extension-expressions)))
692 (setq extension (cdr extension)))
693 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
694 speedbar-supported-extension-expressions)))
696 (defun speedbar-add-ignored-path-regexp (path-expression)
697 "Add PATH-EXPRESSION as a new ignored path for speedbar tracking.
698 This function will modify `speedbar-ignored-path-regexp' and add
699 PATH-EXPRESSION to `speedbar-ignored-path-expressions'."
700 (interactive "sPath regex: ")
701 (if (not (listp path-expression))
702 (setq path-expression (list path-expression)))
703 (while path-expression
704 (if (member (car path-expression) speedbar-ignored-path-expressions)
706 (setq speedbar-ignored-path-expressions
707 (cons (car path-expression) speedbar-ignored-path-expressions)))
708 (setq path-expression (cdr path-expression)))
709 (setq speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
710 speedbar-ignored-path-expressions)))
712 ;; If we don't have custom, then we set it here by hand.
713 (if (not (fboundp 'custom-declare-variable))
714 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
715 speedbar-supported-extension-expressions)
716 speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
717 speedbar-ignored-path-expressions)))
719 (defvar speedbar-update-flag (and
720 (or (fboundp 'run-with-idle-timer)
721 (fboundp 'start-itimer)
722 (boundp 'post-command-idle-hook))
724 "*Non-nil means to automatically update the display.
725 When this is nil then speedbar will not follow the attached frame's path.
726 When speedbar is active, use:
728 \\<speedbar-key-map> `\\[speedbar-toggle-updates]'
730 to toggle this value.")
732 (defvar speedbar-syntax-table nil
733 "Syntax-table used on the speedbar.")
735 (if speedbar-syntax-table
737 (setq speedbar-syntax-table (make-syntax-table))
738 ;; turn off paren matching around here.
739 (modify-syntax-entry ?\' " " speedbar-syntax-table)
740 (modify-syntax-entry ?\" " " speedbar-syntax-table)
741 (modify-syntax-entry ?( " " speedbar-syntax-table)
742 (modify-syntax-entry ?) " " speedbar-syntax-table)
743 (modify-syntax-entry ?[ " " speedbar-syntax-table)
744 (modify-syntax-entry ?] " " speedbar-syntax-table))
746 (defvar speedbar-key-map nil
747 "Keymap used in speedbar buffer.")
751 (setq speedbar-key-map (make-keymap))
752 (suppress-keymap speedbar-key-map t)
755 (define-key speedbar-key-map "g" 'speedbar-refresh)
756 (define-key speedbar-key-map "t" 'speedbar-toggle-updates)
757 (define-key speedbar-key-map "q" 'speedbar-close-frame)
758 (define-key speedbar-key-map "Q" 'delete-frame)
761 (define-key speedbar-key-map "n" 'speedbar-next)
762 (define-key speedbar-key-map "p" 'speedbar-prev)
763 (define-key speedbar-key-map "\M-n" 'speedbar-restricted-next)
764 (define-key speedbar-key-map "\M-p" 'speedbar-restricted-prev)
765 (define-key speedbar-key-map "\C-\M-n" 'speedbar-forward-list)
766 (define-key speedbar-key-map "\C-\M-p" 'speedbar-backward-list)
767 (define-key speedbar-key-map " " 'speedbar-scroll-up)
768 (define-key speedbar-key-map [delete] 'speedbar-scroll-down)
770 ;; Short cuts I happen to find useful
771 (define-key speedbar-key-map "r"
772 (lambda () (interactive)
773 (speedbar-change-initial-expansion-list
774 speedbar-previously-used-expansion-list-name)))
775 (define-key speedbar-key-map "b"
776 (lambda () (interactive)
777 (speedbar-change-initial-expansion-list "quick buffers")))
778 (define-key speedbar-key-map "f"
779 (lambda () (interactive)
780 (speedbar-change-initial-expansion-list "files")))
783 (substitute-key-definition 'switch-to-buffer
784 'speedbar-switch-buffer-attached-frame
785 speedbar-key-map global-map)
789 ;; mouse bindings so we can manipulate the items on each line
790 (define-key speedbar-key-map 'button2 'speedbar-click)
791 (define-key speedbar-key-map '(shift button2) 'speedbar-power-click)
792 ;; Info doc fix from Bob Weiner
793 (if (featurep 'infodoc)
795 (define-key speedbar-key-map 'button3 'speedbar-xemacs-popup-kludge))
796 (define-key speedbar-key-map '(meta button3) 'speedbar-mouse-item-info)
799 ;; mouse bindings so we can manipulate the items on each line
800 (define-key speedbar-key-map [down-mouse-1] 'speedbar-double-click)
801 (define-key speedbar-key-map [mouse-2] 'speedbar-click)
802 ;; This is the power click for new frames, or refreshing a cache
803 (define-key speedbar-key-map [S-mouse-2] 'speedbar-power-click)
804 ;; This adds a small unecessary visual effect
805 ;;(define-key speedbar-key-map [down-mouse-2] 'speedbar-quick-mouse)
806 (define-key speedbar-key-map [M-mouse-2] 'speedbar-mouse-item-info)
808 (define-key speedbar-key-map [down-mouse-3] 'speedbar-emacs-popup-kludge)
810 ;; This lets the user scroll as if we had a scrollbar... well maybe not
811 (define-key speedbar-key-map [mode-line mouse-2] 'speedbar-mouse-hscroll)
812 ;; another handy place users might click to get our menu.
813 (define-key speedbar-key-map [mode-line down-mouse-1]
814 'speedbar-emacs-popup-kludge)
816 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
817 (define-key speedbar-key-map [C-down-mouse-1] 'speedbar-hack-buffer-menu)
819 ;; Lastly, we want to track the mouse. Play here
820 (define-key speedbar-key-map [mouse-movement] 'speedbar-track-mouse)
823 (defun speedbar-make-specialized-keymap ()
824 "Create a keymap for use w/ a speedbar major or minor display mode.
825 This basically creates a sparse keymap, and makes it's parent be
827 (let ((k (make-sparse-keymap)))
828 (set-keymap-parent k speedbar-key-map)
831 (defvar speedbar-file-key-map nil
832 "Keymap used in speedbar buffer while files are displayed.")
834 (if speedbar-file-key-map
836 (setq speedbar-file-key-map (speedbar-make-specialized-keymap))
838 ;; Basic tree features
839 (define-key speedbar-file-key-map "e" 'speedbar-edit-line)
840 (define-key speedbar-file-key-map "\C-m" 'speedbar-edit-line)
841 (define-key speedbar-file-key-map "+" 'speedbar-expand-line)
842 (define-key speedbar-file-key-map "-" 'speedbar-contract-line)
844 ;; file based commands
845 (define-key speedbar-file-key-map "U" 'speedbar-up-directory)
846 (define-key speedbar-file-key-map "I" 'speedbar-item-info)
847 (define-key speedbar-file-key-map "B" 'speedbar-item-byte-compile)
848 (define-key speedbar-file-key-map "L" 'speedbar-item-load)
849 (define-key speedbar-file-key-map "C" 'speedbar-item-copy)
850 (define-key speedbar-file-key-map "D" 'speedbar-item-delete)
851 (define-key speedbar-file-key-map "O" 'speedbar-item-object-delete)
852 (define-key speedbar-file-key-map "R" 'speedbar-item-rename)
855 (defvar speedbar-easymenu-definition-base
857 ["Update" speedbar-refresh t]
858 ["Auto Update" speedbar-toggle-updates
859 :style toggle :selected speedbar-update-flag]
861 "Base part of the speedbar menu.")
863 (defvar speedbar-easymenu-definition-special
864 '(["Edit Item On Line" speedbar-edit-line t]
865 ["Show All Files" speedbar-toggle-show-all-files
866 :style toggle :selected speedbar-show-unknown-files]
867 ["Expand File Tags" speedbar-expand-line
868 (save-excursion (beginning-of-line)
869 (looking-at "[0-9]+: *.\\+. "))]
870 ["Contract File Tags" speedbar-contract-line
871 (save-excursion (beginning-of-line)
872 (looking-at "[0-9]+: *.-. "))]
873 ; ["Sort Tags" speedbar-toggle-sorting
874 ; :style toggle :selected speedbar-sort-tags]
876 ["File/Tag Information" speedbar-item-info t]
877 ["Load Lisp File" speedbar-item-load
880 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
881 ["Byte Compile File" speedbar-item-byte-compile
884 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
885 ["Copy File" speedbar-item-copy
886 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *\\["))]
887 ["Rename File" speedbar-item-rename
888 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
889 ["Delete File" speedbar-item-delete
890 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
891 ["Delete Object" speedbar-item-object-delete
892 (save-excursion (beginning-of-line)
893 (looking-at "[0-9]+: *\\[[+-]\\] [^ \n]+ \\*?[!#]$"))]
895 "Additional menu items while in file-mode.")
897 (defvar speedbar-easymenu-definition-trailer
899 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
900 ["Customize..." speedbar-customize t])
901 ["Close" speedbar-close-frame t]
902 ["Quit" delete-frame t] )
903 "Menu items appearing at the end of the speedbar menu.")
905 (defvar speedbar-desired-buffer nil
906 "Non-nil when speedbar is showing buttons specific a special mode.
907 In this case it is the originating buffer.")
908 (defvar speedbar-buffer nil
909 "The buffer displaying the speedbar.")
910 (defvar speedbar-frame nil
911 "The frame displaying speedbar.")
912 (defvar speedbar-cached-frame nil
913 "The frame that was last created, then removed from the display.")
914 (defvar speedbar-full-text-cache nil
915 "The last open directory is saved in its entirety for ultra-fast switching.")
916 (defvar speedbar-timer nil
917 "The speedbar timer used for updating the buffer.")
918 (defvar speedbar-attached-frame nil
919 "The frame which started speedbar mode.
920 This is the frame from which all data displayed in the speedbar is
921 gathered, and in which files and such are displayed.")
923 (defvar speedbar-last-selected-file nil
924 "The last file which was selected in speedbar buffer.")
926 (defvar speedbar-shown-directories nil
927 "Maintain list of directories simultaneously open in the current speedbar.")
929 (defvar speedbar-directory-contents-alist nil
930 "An association list of directories and their contents.
931 Each sublist was returned by `speedbar-file-lists'. This list is
932 maintained to speed up the refresh rate when switching between
935 (defvar speedbar-power-click nil
936 "Never set this by hand. Value is t when S-mouse activity occurs.")
941 (if (fboundp 'frame-parameter)
943 (defalias 'speedbar-frame-parameter 'frame-parameter)
945 (defun speedbar-frame-parameter (frame parameter)
946 "Return FRAME's PARAMETER value."
947 (cdr (assoc parameter (frame-parameters frame)))))
949 ;;; Mode definitions/ user commands
953 (defalias 'speedbar 'speedbar-frame-mode)
955 (defun speedbar-frame-mode (&optional arg)
956 "Enable or disable speedbar. Positive ARG means turn on, negative turn off.
957 nil means toggle. Once the speedbar frame is activated, a buffer in
958 `speedbar-mode' will be displayed. Currently, only one speedbar is
960 `speedbar-before-popup-hook' is called before popping up the speedbar frame.
961 `speedbar-before-delete-hook' is called before the frame is deleted."
963 ;; toggle frame on and off.
964 (if (not arg) (if (and (frame-live-p speedbar-frame)
965 (frame-visible-p speedbar-frame))
966 (setq arg -1) (setq arg 1)))
967 ;; turn the frame off on neg number
968 (if (and (numberp arg) (< arg 0))
970 (run-hooks 'speedbar-before-delete-hook)
971 (if (and speedbar-frame (frame-live-p speedbar-frame))
973 (setq speedbar-cached-frame speedbar-frame)
974 (make-frame-invisible speedbar-frame)))
975 (setq speedbar-frame nil)
976 (speedbar-set-timer nil)
977 ;; Used to delete the buffer. This has the annoying affect of
978 ;; preventing whatever took its place from ever appearing
979 ;; as the default after a C-x b was typed
980 ;;(if (bufferp speedbar-buffer)
981 ;; (kill-buffer speedbar-buffer))
983 ;; Set this as our currently attached frame
984 (setq speedbar-attached-frame (selected-frame))
985 (run-hooks 'speedbar-before-popup-hook)
986 ;; Get the frame to work in
987 (if (frame-live-p speedbar-cached-frame)
989 (setq speedbar-frame speedbar-cached-frame)
990 (make-frame-visible speedbar-frame)
991 ;; Get the buffer to play with
993 (select-frame speedbar-frame)
994 (if (not (eq (current-buffer) speedbar-buffer))
995 (switch-to-buffer speedbar-buffer))
996 (set-window-dedicated-p (selected-window) t)
997 (raise-frame speedbar-frame)
998 (speedbar-set-timer speedbar-update-speed)
1000 (if (frame-live-p speedbar-frame)
1001 (raise-frame speedbar-frame)
1002 (setq speedbar-frame
1003 (if speedbar-xemacsp
1004 ;; Only guess height if it is not specified.
1005 (if (member 'height speedbar-frame-plist)
1006 (make-frame speedbar-frame-plist)
1007 (make-frame (nconc (list 'height
1008 (speedbar-needed-height))
1009 speedbar-frame-plist)))
1010 (let* ((mh (speedbar-frame-parameter nil 'menu-bar-lines))
1011 (cfx (speedbar-frame-parameter nil 'left))
1012 (cfy (speedbar-frame-parameter nil 'top))
1013 (cfw (frame-pixel-width))
1015 ;; Only add a guessed height if one is not specified
1016 ;; in the input parameters.
1017 (if (assoc 'height speedbar-frame-parameters)
1018 speedbar-frame-parameters
1020 speedbar-frame-parameters
1021 (list (cons 'height (+ mh (frame-height)))))))
1023 (if (or (< emacs-major-version 20)
1024 (not (eq window-system 'x)))
1026 (let ((x-pointer-shape x-pointer-top-left-arrow)
1027 (x-sensitive-text-pointer-shape
1029 (make-frame params)))))
1030 ;; Position speedbar frame.
1031 (if (or (not window-system) (eq window-system 'pc)
1032 (assoc 'left speedbar-frame-parameters)
1033 (assoc 'top speedbar-frame-parameters))
1034 ;; Do no positioning if not on a windowing system,
1035 ;; or if left/top were specified in the parameters.
1038 (if (not (consp cfx))
1040 ;; If cfx is a list, that means we grow
1041 ;; from a specific edge of the display.
1042 ;; Convert that to the distance from the
1043 ;; left side of the display.
1044 (if (eq (car cfx) '-)
1045 ;; A - means distance from the right edge
1046 ;; of the display, or DW - cfx - framewidth
1047 (- (x-display-pixel-width) (car (cdr cfx))
1048 (frame-pixel-width))
1050 (modify-frame-parameters
1055 ;; Decide which side to put it
1056 ;; on. 200 is just a buffer
1057 ;; for the left edge of the
1058 ;; screen. The extra 10 is just
1059 ;; dressings for window decorations.
1060 (let ((sfw (frame-pixel-width frame)))
1061 (let ((left-guess (- cfx 10 sfw))
1062 (right-guess (+ cfx cfw 5)))
1063 (let ((left-margin left-guess)
1065 (- (x-display-pixel-width)
1066 right-guess 5 sfw)))
1067 (cond ((>= left-margin 0) left-guess)
1068 ((>= right-margin 0) right-guess)
1069 ;; otherwise choose side we overlap less
1070 ((> left-margin right-margin) 0)
1071 (t (- (x-display-pixel-width) sfw 5)))))))
1074 ;; reset the selection variable
1075 (setq speedbar-last-selected-file nil)
1076 ;; Put the buffer into the frame
1077 (save-window-excursion
1078 ;; Get the buffer to play with
1080 (select-frame speedbar-frame)
1081 (switch-to-buffer speedbar-buffer)
1082 (set-window-dedicated-p (selected-window) t))
1083 (if (and (or (null window-system) (eq window-system 'pc))
1084 (fboundp 'set-frame-name))
1086 (select-frame speedbar-frame)
1087 (set-frame-name "Speedbar")))
1088 (speedbar-set-timer speedbar-update-speed)))))
1091 (defun speedbar-get-focus ()
1092 "Change frame focus to or from the speedbar frame.
1093 If the selected frame is not speedbar, then speedbar frame is
1094 selected. If the speedbar frame is active, then select the attached frame."
1096 (if (eq (selected-frame) speedbar-frame)
1097 (if (frame-live-p speedbar-attached-frame)
1098 (select-frame speedbar-attached-frame))
1099 ;; If updates are off, then refresh the frame (they want it now...)
1100 (if (not speedbar-update-flag)
1101 (let ((speedbar-update-flag t))
1102 (speedbar-timer-fn)))
1103 ;; make sure we have a frame
1104 (if (not (frame-live-p speedbar-frame)) (speedbar-frame-mode 1))
1106 (select-frame speedbar-frame)
1110 (defun speedbar-close-frame ()
1111 "Turn off a currently active speedbar."
1113 (speedbar-frame-mode -1)
1114 (select-frame speedbar-attached-frame)
1117 (defun speedbar-switch-buffer-attached-frame (&optional buffer)
1118 "Switch to BUFFER in speedbar's attached frame, and raise that frame.
1119 This overrides the default behavior of `switch-to-buffer' which is
1120 broken because of the dedicated speedbar frame."
1122 ;; Assume we are in the speedbar frame.
1123 (speedbar-get-focus)
1124 ;; Now switch buffers
1126 (switch-to-buffer buffer)
1127 (call-interactively 'switch-to-buffer nil nil)))
1129 (defmacro speedbar-frame-width ()
1130 "Return the width of the speedbar frame in characters.
1131 nil if it doesn't exist."
1132 '(frame-width speedbar-frame))
1134 ;; XEmacs function only.
1135 (defun speedbar-needed-height (&optional frame)
1136 "The needed height for the tool bar FRAME (in characters)."
1137 (or frame (setq frame (selected-frame)))
1138 ;; The 1 is the missing modeline/minibuffer
1139 (+ 1 (/ (frame-pixel-height frame)
1140 (face-height 'default frame))))
1142 (defun speedbar-mode ()
1143 "Major mode for managing a display of directories and tags.
1144 \\<speedbar-key-map>
1145 The first line represents the default path of the speedbar frame.
1146 Each directory segment is a button which jumps speedbar's default
1147 directory to that path. Buttons are activated by clicking `\\[speedbar-click]'.
1148 In some situations using `\\[speedbar-power-click]' is a `power click' which will
1149 rescan cached items, or pop up new frames.
1151 Each line starting with <+> represents a directory. Click on the <+>
1152 to insert the directory listing into the current tree. Click on the
1153 <-> to retract that list. Click on the directory name to go to that
1154 directory as the default.
1156 Each line starting with [+] is a file. If the variable
1157 `speedbar-show-unknown-files' is t, the lines starting with [?] are
1158 files which don't have imenu support, but are not expressly ignored.
1159 Files are completely ignored if they match `speedbar-file-unshown-regexp'
1160 which is generated from `completion-ignored-extensions'.
1162 Files with a `*' character after their name are files checked out of a
1163 version control system. (currently only RCS is supported.) New
1164 version control systems can be added by examining the documentation
1165 for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'
1167 Files with a `#' or `!' character after them are source files that
1168 have an object file associated with them. The `!' indicates that the
1169 files is out of date. You can control what source/object associations
1170 exist through the variable `speedbar-obj-alist'.
1172 Click on the [+] to display a list of tags from that file. Click on
1173 the [-] to retract the list. Click on the file name to edit the file
1174 in the attached frame.
1176 If you open tags, you might find a node starting with {+}, which is a
1177 category of tags. Click the {+} to expand the category. Jump-able
1178 tags start with >. Click the name of the tag to go to that position
1179 in the selected file.
1181 \\{speedbar-key-map}"
1184 (setq speedbar-buffer (set-buffer (get-buffer-create " SPEEDBAR")))
1185 (kill-all-local-variables)
1186 (setq major-mode 'speedbar-mode)
1187 (setq mode-name "Speedbar")
1188 (set-syntax-table speedbar-syntax-table)
1189 (setq font-lock-keywords nil) ;; no font-locking please
1190 (setq truncate-lines t)
1191 (make-local-variable 'frame-title-format)
1192 (setq frame-title-format "Speedbar")
1193 ;; Set this up special just for the speedbar buffer
1194 ;; Terminal minibuffer stuff does not require this.
1195 (if (and window-system (not (eq window-system 'pc))
1196 (null default-minibuffer-frame))
1198 (make-local-variable 'default-minibuffer-frame)
1199 (setq default-minibuffer-frame speedbar-attached-frame)))
1200 ;; Correct use of `temp-buffer-show-function': Bob Weiner
1201 (if (and (boundp 'temp-buffer-show-hook)
1202 (boundp 'temp-buffer-show-function))
1203 (progn (make-local-variable 'temp-buffer-show-hook)
1204 (setq temp-buffer-show-hook temp-buffer-show-function)))
1205 (make-local-variable 'temp-buffer-show-function)
1206 (setq temp-buffer-show-function 'speedbar-temp-buffer-show-function)
1207 (if speedbar-xemacsp
1209 ;; Argh! mouse-track-click-hook doesn't understand the
1210 ;; make-local-hook conventions.
1211 (make-local-variable 'mouse-track-click-hook)
1212 (add-hook 'mouse-track-click-hook
1213 (lambda (event count)
1214 (if (/= (event-button event) 1)
1215 nil ; Do normal operations.
1217 (speedbar-quick-mouse event))
1220 (mouse-set-point event)
1221 (speedbar-do-function-pointer)
1222 (speedbar-quick-mouse event)))
1223 ;; Don't do normal operations.
1225 (make-local-hook 'kill-buffer-hook)
1226 (add-hook 'kill-buffer-hook (lambda () (let ((skilling (boundp 'skilling)))
1229 (if (eq (current-buffer)
1231 (speedbar-frame-mode -1)))))
1233 (toggle-read-only 1)
1234 (speedbar-set-mode-line-format)
1235 (if speedbar-xemacsp
1237 (make-local-variable 'mouse-motion-handler)
1238 (setq mouse-motion-handler 'speedbar-track-mouse-xemacs))
1239 (if speedbar-track-mouse-flag
1241 (make-local-variable 'track-mouse)
1242 (setq track-mouse t))) ;this could be messy.
1243 (setq auto-show-mode nil)) ;no auto-show for Emacs
1244 (run-hooks 'speedbar-mode-hook))
1245 (speedbar-update-contents)
1248 (defun speedbar-show-info-under-mouse (&optional event)
1249 "Call the info function for the line under the mouse.
1250 Optional EVENT is currently not used."
1251 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1252 (if (equal (car pos) speedbar-frame)
1254 (save-window-excursion
1255 (apply 'set-mouse-position pos)
1256 (speedbar-item-info))))))
1258 (defun speedbar-set-mode-line-format ()
1259 "Set the format of the mode line based on the current speedbar environment.
1260 This gives visual indications of what is up. It EXPECTS the speedbar
1261 frame and window to be the currently active frame and window."
1262 (if (and (frame-live-p speedbar-frame)
1263 (or (not speedbar-xemacsp)
1264 (specifier-instance has-modeline-p)))
1266 (set-buffer speedbar-buffer)
1267 (let* ((w (or (speedbar-frame-width) 20))
1270 (p3 (if speedbar-update-flag "SPEEDBAR" "SLOWBAR"))
1271 (blank (- w (length p1) (length p3) (length p5)
1272 (if line-number-mode 4 0)))
1274 (make-string (/ blank 2) ? )
1277 (make-string (+ (/ blank 2) (% blank 2)) ? )
1280 (if line-number-mode
1281 (list (concat p1 p2 p3) '(line-number-mode " %3l")
1283 (list (concat p1 p2 p3 p4 p5)))))
1284 (if (not (equal mode-line-format tf))
1286 (setq mode-line-format tf)
1287 (force-mode-line-update)))))))
1289 (defun speedbar-temp-buffer-show-function (buffer)
1290 "Placed in the variable `temp-buffer-show-function' in `speedbar-mode'.
1291 If a user requests help using \\[help-command] <Key> the temp BUFFER will be
1292 redirected into a window on the attached frame."
1293 (if speedbar-attached-frame (select-frame speedbar-attached-frame))
1294 (pop-to-buffer buffer nil)
1296 ;; Fix for using this hook on some platforms: Bob Weiner
1297 (cond ((not speedbar-xemacsp)
1298 (run-hooks 'temp-buffer-show-hook))
1299 ((fboundp 'run-hook-with-args)
1300 (run-hook-with-args 'temp-buffer-show-hook buffer))
1301 ((and (boundp 'temp-buffer-show-hook)
1302 (listp temp-buffer-show-hook))
1303 (mapcar (function (lambda (hook) (funcall hook buffer)))
1304 temp-buffer-show-hook))))
1306 (defvar speedbar-previous-menu nil
1307 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1309 (defun speedbar-reconfigure-keymaps ()
1310 "Reconfigure the menu-bar in a speedbar frame.
1311 Different menu items are displayed depending on the current display mode
1312 and the existence of packages."
1314 speedbar-easymenu-definition-base
1315 (if speedbar-shown-directories
1316 ;; file display mode version
1317 (speedbar-initial-menu)
1319 (select-frame speedbar-attached-frame)
1320 (if (local-variable-p
1321 'speedbar-easymenu-definition-special
1323 ;; If bound locally, we can use it
1324 speedbar-easymenu-definition-special)))
1325 ;; Dynamic menu stuff
1327 (list (cons "Displays"
1328 (let ((displays nil)
1329 (alist speedbar-initial-expansion-mode-alist))
1334 (capitalize (car (car alist)))
1336 'speedbar-change-initial-expansion-list
1340 (setq alist (cdr alist)))
1343 speedbar-easymenu-definition-trailer))
1344 (localmap (save-excursion
1345 (let ((cf (selected-frame)))
1347 (select-frame speedbar-attached-frame)
1348 (if (local-variable-p
1349 'speedbar-special-mode-key-map
1351 speedbar-special-mode-key-map)
1352 (select-frame cf))))))
1354 (set-buffer speedbar-buffer)
1355 (use-local-map (or localmap
1356 (speedbar-initial-keymap)
1357 ;; This creates a small keymap we can glom the
1358 ;; menu adjustments into.
1359 (speedbar-make-specialized-keymap)))
1360 ;; Delete the old menu if applicable.
1361 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1362 (setq speedbar-previous-menu md)
1363 ;; Now add the new menu
1364 (if (not speedbar-xemacsp)
1365 (easy-menu-define speedbar-menu-map (current-local-map)
1367 (easy-menu-add md (current-local-map))
1368 (set-buffer-menubar (list md))))))
1371 ;;; User Input stuff
1374 ;; XEmacs: this can be implemented using modeline keymaps, but there
1375 ;; is no use, as we have horizontal scrollbar (as the docstring
1377 (defun speedbar-mouse-hscroll (e)
1378 "Read a mouse event E from the mode line, and horizontally scroll.
1379 If the mouse is being clicked on the far left, or far right of the
1380 mode-line. This is only useful for non-XEmacs"
1382 (let* ((xp (car (nth 2 (car (cdr e)))))
1383 (cpw (/ (frame-pixel-width)
1385 (oc (1+ (/ xp cpw)))
1389 ((> oc (- (window-width) 3))
1391 (t (message "Click on the edge of the modeline to scroll left/right")))
1392 ;;(message "X: Pixel %d Char Pixels %d On char %d" xp cpw oc)
1395 (defun speedbar-customize ()
1396 "Customize speedbar using the Custom package."
1398 (let ((sf (selected-frame)))
1399 (select-frame speedbar-attached-frame)
1400 (customize-group 'speedbar)
1402 (speedbar-maybee-jump-to-attached-frame))
1404 (defun speedbar-track-mouse (event)
1405 "For motion EVENT, display info about the current line."
1407 (if (not speedbar-track-mouse-flag)
1410 (let ((char (nth 1 (car (cdr event)))))
1411 (if (not (numberp char))
1414 ;; (message "%S" event)
1415 (speedbar-item-info)
1418 (defun speedbar-track-mouse-xemacs (event)
1419 "For motion EVENT, display info about the current line."
1420 (if (functionp (default-value 'mouse-motion-handler))
1421 (funcall (default-value 'mouse-motion-handler) event))
1422 (if speedbar-track-mouse-flag
1424 (save-window-excursion
1426 (progn (mouse-set-point event)
1427 ;; Prevent focus-related bugs.
1428 (if (eq major-mode 'speedbar-mode)
1429 (speedbar-item-info)))
1432 ;; In XEmacs, we make popup menus work on the item over mouse (as
1433 ;; opposed to where the point happens to be.) We attain this by
1434 ;; temporarily moving the point to that place.
1435 ;; Hrvoje Niksic <hniksic@srce.hr>
1436 (defun speedbar-xemacs-popup-kludge (event)
1437 "Pop up a menu related to the clicked on item.
1438 Must be bound to EVENT."
1440 (select-frame speedbar-frame)
1442 (goto-char (event-closest-point event))
1444 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
1445 (save-excursion (beginning-of-line) (point)))))
1447 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
1448 ;; menu functions) return immediately.
1450 (while (not (misc-user-event-p (setq new (next-event))))
1451 (dispatch-event new))
1452 (dispatch-event new))))
1454 (defun speedbar-emacs-popup-kludge (e)
1455 "Pop up a menu related to the clicked on item.
1456 Must be bound to event E."
1460 ;; This gets the cursor where the user can see it.
1461 (if (not (bolp)) (forward-char -1))
1463 (if (< emacs-major-version 20)
1464 (mouse-major-mode-menu e)
1465 (mouse-major-mode-menu e nil))))
1467 (defun speedbar-hack-buffer-menu (e)
1468 "Control mouse 1 is buffer menu.
1469 This hack overrides it so that the right thing happens in the main
1470 Emacs frame, not in the speedbar frame.
1471 Argument E is the event causing this activity."
1473 (let ((fn (lookup-key global-map (if speedbar-xemacsp
1479 (set-window-dedicated-p (selected-window) nil)
1480 (call-interactively fn)
1481 (setq newbuff (current-buffer)))
1482 (switch-to-buffer " SPEEDBAR")
1483 (set-window-dedicated-p (selected-window) t))
1484 (speedbar-with-attached-buffer
1485 (switch-to-buffer newbuff))))
1487 (defun speedbar-next (arg)
1488 "Move to the next ARGth line in a speedbar buffer."
1490 (forward-line (or arg 1))
1491 (speedbar-item-info)
1492 (speedbar-position-cursor-on-line))
1494 (defun speedbar-prev (arg)
1495 "Move to the previous ARGth line in a speedbar buffer."
1497 (speedbar-next (if arg (- arg) -1)))
1499 (defun speedbar-restricted-move (arg)
1500 "Move to the next ARGth line in a speedbar buffer at the same depth.
1501 This means that movement is restricted to a subnode, and that siblings
1502 of intermediate nodes are skipped."
1503 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1504 ;; First find the extent for which we are allowed to move.
1505 (let ((depth (save-excursion (beginning-of-line)
1506 (if (looking-at "[0-9]+:")
1507 (string-to-int (match-string 0))
1509 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1510 (lastmatch (point)))
1512 (forward-line (- crement))
1513 (let ((subdepth (save-excursion (beginning-of-line)
1514 (if (looking-at "[0-9]+:")
1515 (string-to-int (match-string 0))
1517 (cond ((or (< subdepth depth)
1518 (progn (end-of-line) (eobp))
1519 (progn (beginning-of-line) (bobp)))
1520 ;; We have reached the end of this block.
1521 (goto-char lastmatch)
1523 (error "End of sub-list"))
1525 (setq lastmatch (point)
1526 arg (+ arg crement))))))
1527 (speedbar-position-cursor-on-line)))
1529 (defun speedbar-restricted-next (arg)
1530 "Move to the next ARGth line in a speedbar buffer at the same depth.
1531 This means that movement is restricted to a subnode, and that siblings
1532 of intermediate nodes are skipped."
1534 (speedbar-restricted-move (or arg 1))
1535 (speedbar-item-info))
1538 (defun speedbar-restricted-prev (arg)
1539 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1540 This means that movement is restricted to a subnode, and that siblings
1541 of intermediate nodes are skipped."
1543 (speedbar-restricted-move (if arg (- arg) -1))
1544 (speedbar-item-info))
1546 (defun speedbar-navigate-list (arg)
1547 "Move across ARG groups of similarly typed items in speedbar.
1548 Stop on the first line of the next type of item, or on the last or first item
1549 if we reach a buffer boundary."
1552 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1553 (let ((str (regexp-quote (match-string 0))))
1554 (while (looking-at str)
1555 (speedbar-restricted-move arg)
1556 (beginning-of-line))))
1557 (speedbar-position-cursor-on-line))
1559 (defun speedbar-forward-list ()
1560 "Move forward over the current list.
1561 A LIST in speedbar is a group of similarly typed items, such as directories,
1562 files, or the directory button."
1564 (speedbar-navigate-list 1)
1565 (speedbar-item-info))
1567 (defun speedbar-backward-list ()
1568 "Move backward over the current list.
1569 A LIST in speedbar is a group of similarly typed items, such as directories,
1570 files, or the directory button."
1572 (speedbar-navigate-list -1)
1573 (speedbar-item-info))
1575 (defun speedbar-scroll-up (&optional arg)
1576 "Page down one screen-full of the speedbar, or ARG lines."
1579 (speedbar-position-cursor-on-line))
1581 (defun speedbar-scroll-down (&optional arg)
1582 "Page up one screen-full of the speedbar, or ARG lines."
1585 (speedbar-position-cursor-on-line))
1587 (defun speedbar-up-directory ()
1588 "Keyboard accelerator for moving the default directory up one.
1589 Assumes that the current buffer is the speedbar buffer"
1591 (setq default-directory (expand-file-name (concat default-directory "../")))
1592 (speedbar-update-contents))
1594 ;;; Speedbar file activity (aka creeping featurism)
1596 (defun speedbar-refresh ()
1597 "Refresh the current speedbar display, disposing of any cached data."
1599 (let ((dl speedbar-shown-directories)
1600 (dm (and (boundp 'deactivate-mark) deactivate-mark)))
1602 (adelete 'speedbar-directory-contents-alist (car dl))
1604 (if (<= 1 speedbar-verbosity-level)
1605 (message "Refreshing speedbar..."))
1606 (speedbar-update-contents)
1607 (speedbar-stealthy-updates)
1608 ;; Reset the timer in case it got really hosed for some reason...
1609 (speedbar-set-timer speedbar-update-speed)
1610 (if (<= 1 speedbar-verbosity-level)
1611 (message "Refreshing speedbar...done"))
1612 (if (boundp 'deactivate-mark) (setq deactivate-mark dm))))
1614 (defun speedbar-item-load ()
1615 "Load the item under the cursor or mouse if it is a Lisp file."
1617 (let ((f (speedbar-line-file)))
1618 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1619 (if (and (file-exists-p (concat f "c"))
1620 (y-or-n-p (format "Load %sc? " f)))
1621 ;; If the compiled version exists, load that instead...
1622 (load-file (concat f "c"))
1624 (error "Not a loadable file"))))
1626 (defun speedbar-item-byte-compile ()
1627 "Byte compile the item under the cursor or mouse if it is a Lisp file."
1629 (let ((f (speedbar-line-file))
1630 (sf (selected-frame)))
1631 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1633 (select-frame speedbar-attached-frame)
1634 (byte-compile-file f nil)
1636 (speedbar-reset-scanners)))
1639 (defun speedbar-mouse-item-info (event)
1640 "Provide information about what the user clicked on.
1641 This should be bound to a mouse EVENT."
1643 (mouse-set-point event)
1644 (speedbar-item-info))
1646 (defun speedbar-generic-item-info ()
1647 "Attempt to derive, and then display information about thils line item.
1648 File style information is displayed with `speedbar-item-info'."
1651 ;; Skip invisible number info.
1652 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1653 ;; Skip items in "folder" type text characters.
1654 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1656 (message "Text: %s" (buffer-substring-no-properties
1657 (point) (progn (end-of-line) (point))))))
1659 (defun speedbar-item-info ()
1660 "Display info in the mini-buffer about the button the mouse is over."
1662 (if (not speedbar-shown-directories)
1663 (speedbar-generic-item-info)
1664 (let* ((item (speedbar-line-file))
1665 (attr (if item (file-attributes item) nil)))
1666 (if (and item attr) (message "%s %-6d %s" (nth 8 attr) (nth 7 attr) item)
1669 (if (not (looking-at "\\([0-9]+\\):"))
1670 (speedbar-generic-item-info)
1671 (setq item (speedbar-line-path (string-to-int (match-string 1))))
1672 (if (re-search-forward "> \\([^ ]+\\)$"
1673 (save-excursion(end-of-line)(point)) t)
1675 (setq attr (get-text-property (match-beginning 1)
1677 (message "Tag: %s in %s @ %s"
1678 (match-string 1) item
1680 (if (markerp attr) (marker-position attr) attr)
1682 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1683 (save-excursion(end-of-line)(point)) t)
1684 (message "Group of tags \"%s\"" (match-string 1))
1685 (speedbar-generic-item-info)))))))))
1687 (defun speedbar-item-copy ()
1688 "Copy the item under the cursor.
1689 Files can be copied to new names or places."
1691 (let ((f (speedbar-line-file)))
1692 (if (not f) (error "Not a file"))
1693 (if (file-directory-p f)
1694 (error "Cannot copy directory")
1695 (let* ((rt (read-file-name (format "Copy %s to: "
1696 (file-name-nondirectory f))
1697 (file-name-directory f)))
1698 (refresh (member (expand-file-name (file-name-directory rt))
1699 speedbar-shown-directories)))
1700 ;; Create the right file name part
1701 (if (file-directory-p rt)
1703 (concat (expand-file-name rt)
1704 (if (string-match "/$" rt) "" "/")
1705 (file-name-nondirectory f))))
1706 (if (or (not (file-exists-p rt))
1707 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1709 (copy-file f rt t t)
1710 ;; refresh display if the new place is currently displayed.
1714 (if (not (speedbar-goto-this-file rt))
1715 (speedbar-goto-this-file f))))
1718 (defun speedbar-item-rename ()
1719 "Rename the item under the cursor or mouse.
1720 Files can be renamed to new names or moved to new directories."
1722 (let ((f (speedbar-line-file)))
1724 (let* ((rt (read-file-name (format "Rename %s to: "
1725 (file-name-nondirectory f))
1726 (file-name-directory f)))
1727 (refresh (member (expand-file-name (file-name-directory rt))
1728 speedbar-shown-directories)))
1729 ;; Create the right file name part
1730 (if (file-directory-p rt)
1732 (concat (expand-file-name rt)
1733 (if (string-match "/\\'" rt) "" "/")
1734 (file-name-nondirectory f))))
1735 (if (or (not (file-exists-p rt))
1736 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1738 (rename-file f rt t)
1739 ;; refresh display if the new place is currently displayed.
1743 (speedbar-goto-this-file rt)
1745 (error "Not a file"))))
1747 (defun speedbar-item-delete ()
1748 "Delete the item under the cursor. Files are removed from disk."
1750 (let ((f (speedbar-line-file)))
1751 (if (not f) (error "Not a file"))
1752 (if (y-or-n-p (format "Delete %s? " f))
1754 (if (file-directory-p f)
1755 (delete-directory f)
1757 (message "Okie dokie..")
1764 (defun speedbar-item-object-delete ()
1765 "Delete the object associated from the item under the cursor.
1766 The file is removed from disk. The object is determined from the
1767 variable `speedbar-obj-alist'."
1769 (let* ((f (speedbar-line-file))
1771 (oa speedbar-obj-alist))
1772 (if (not f) (error "Not a file"))
1773 (while (and oa (not (string-match (car (car oa)) f)))
1775 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1776 (if (and oa (file-exists-p obj)
1777 (y-or-n-p (format "Delete %s? " obj)))
1780 (speedbar-reset-scanners)))))
1782 (defun speedbar-enable-update ()
1783 "Enable automatic updating in speedbar via timers."
1785 (setq speedbar-update-flag t)
1786 (speedbar-set-mode-line-format)
1787 (speedbar-set-timer speedbar-update-speed))
1789 (defun speedbar-disable-update ()
1790 "Disable automatic updating and stop consuming resources."
1792 (setq speedbar-update-flag nil)
1793 (speedbar-set-mode-line-format)
1794 (speedbar-set-timer nil))
1796 (defun speedbar-toggle-updates ()
1797 "Toggle automatic update for the speedbar frame."
1799 (if speedbar-update-flag
1800 (speedbar-disable-update)
1801 (speedbar-enable-update)))
1803 (defun speedbar-toggle-sorting ()
1804 "Toggle automatic update for the speedbar frame."
1806 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1808 (defun speedbar-toggle-show-all-files ()
1809 "Toggle display of files speedbar can not tag."
1811 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1814 ;;; Utility functions
1816 (defun speedbar-set-timer (timeout)
1817 "Apply a timer with TIMEOUT, or remove a timer if TIMOUT is nil.
1818 TIMEOUT is the number of seconds until the speedbar timer is called
1819 again. When TIMEOUT is nil, turn off all timeouts.
1820 This function will also enable or disable the `vc-checkin-hook' used
1821 to track file check ins, and will change the mode line to match
1822 `speedbar-update-flag'."
1827 (progn (delete-itimer speedbar-timer)
1828 (setq speedbar-timer nil)))
1830 (if (and speedbar-xemacsp
1831 (or (>= emacs-major-version 20)
1832 (>= emacs-minor-version 15)))
1833 (setq speedbar-timer (start-itimer "speedbar"
1838 (setq speedbar-timer (start-itimer "speedbar"
1843 ((fboundp 'run-with-idle-timer)
1845 (progn (cancel-timer speedbar-timer)
1846 (setq speedbar-timer nil)))
1848 (setq speedbar-timer
1849 (run-with-idle-timer timeout t 'speedbar-timer-fn))))
1850 ;; Emacs 19.30 (Thanks twice: ptype@dra.hmg.gb)
1851 ((fboundp 'post-command-idle-hook)
1853 (add-hook 'post-command-idle-hook 'speedbar-timer-fn)
1854 (remove-hook 'post-command-idle-hook 'speedbar-timer-fn)))
1855 ;; Older or other Emacsen with no timers. Set up so that its
1856 ;; obvious this emacs can't handle the updates
1858 (setq speedbar-update-flag nil)))
1859 ;; Apply a revert hook that will reset the scanners. We attach to revert
1860 ;; because most reverts occur during VC state change, and this lets our
1861 ;; VC scanner fix itself.
1863 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
1864 (remove-hook 'after-revert-hook 'speedbar-reset-scanners)
1866 ;; change this if it changed for some reason
1867 (speedbar-set-mode-line-format))
1869 (defmacro speedbar-with-writable (&rest forms)
1870 "Allow the buffer to be writable and evaluate FORMS."
1871 (list 'let '((inhibit-read-only t))
1872 (cons 'progn forms)))
1873 (put 'speedbar-with-writable 'lisp-indent-function 0)
1875 (defun speedbar-select-window (buffer)
1876 "Select a window in which BUFFER is shown.
1877 If it is not shown, force it to appear in the default window."
1878 (let ((win (get-buffer-window buffer speedbar-attached-frame)))
1881 (set-window-buffer (selected-window) buffer))))
1883 (defmacro speedbar-with-attached-buffer (&rest forms)
1884 "Execute FORMS in the attached frame's special buffer.
1885 Optionally select that frame if necessary."
1886 ;; Reset the timer with a new timeout when cliking a file
1887 ;; in case the user was navigating directories, we can cancel
1888 ;; that other timer.
1891 '(speedbar-set-timer speedbar-update-speed)
1893 'let '((cf (selected-frame)))
1894 '(select-frame speedbar-attached-frame)
1895 '(speedbar-select-window speedbar-desired-buffer)
1898 '(speedbar-maybee-jump-to-attached-frame)
1901 (defun speedbar-insert-button (text face mouse function
1902 &optional token prevline)
1903 "Insert TEXT as the next logical speedbar button.
1904 FACE is the face to put on the button, MOUSE is the highlight face to use.
1905 When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
1906 This function assumes that the current buffer is the speedbar buffer.
1907 If PREVLINE, then put this button on the previous line.
1909 This is a convenience function for special mode that create their own
1910 specialized speedbar displays."
1911 (goto-char (point-max))
1912 (if (/= (current-column) 0) (insert "\n"))
1913 (if prevline (progn (delete-char -1) (insert " "))) ;back up if desired...
1914 (let ((start (point)))
1916 (speedbar-make-button start (point) face mouse function token))
1917 (let ((start (point)))
1919 (put-text-property start (point) 'face nil)
1920 (put-text-property start (point) 'mouse-face nil)))
1922 (defun speedbar-make-button (start end face mouse function &optional token)
1923 "Create a button from START to END, with FACE as the display face.
1924 MOUSE is the mouse face. When this button is clicked on FUNCTION
1925 will be run with the TOKEN parameter (any Lisp object)"
1926 (put-text-property start end 'face face)
1927 (put-text-property start end 'mouse-face mouse)
1928 (put-text-property start end 'invisible nil)
1929 (if function (put-text-property start end 'speedbar-function function))
1930 (if token (put-text-property start end 'speedbar-token token))
1933 ;;; Initial Expansion list management
1935 (defun speedbar-initial-expansion-list ()
1936 "Return the current default expansion list.
1937 This is based on `speedbar-initial-expansion-list-name' referencing
1938 `speedbar-initial-expansion-mode-alist'."
1939 ;; cdr1 - name, cdr2 - menu
1940 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
1941 speedbar-initial-expansion-mode-alist)))))
1943 (defun speedbar-initial-menu ()
1944 "Return the current default menu data.
1945 This is based on `speedbar-initial-expansion-list-name' referencing
1946 `speedbar-initial-expansion-mode-alist'."
1948 (car (cdr (assoc speedbar-initial-expansion-list-name
1949 speedbar-initial-expansion-mode-alist)))))
1951 (defun speedbar-initial-keymap ()
1952 "Return the current default menu data.
1953 This is based on `speedbar-initial-expansion-list-name' referencing
1954 `speedbar-initial-expansion-mode-alist'."
1956 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
1957 speedbar-initial-expansion-mode-alist))))))
1959 (defun speedbar-initial-stealthy-functions ()
1960 "Return a list of functions to call stealthily.
1961 This is based on `speedbar-initial-expansion-list-name' referencing
1962 `speedbar-stealthy-function-list'."
1963 (cdr (assoc speedbar-initial-expansion-list-name
1964 speedbar-stealthy-function-list)))
1966 (defun speedbar-add-expansion-list (new-list)
1967 "Add NEW-LIST to the list of expansion lists."
1968 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
1970 (defun speedbar-change-initial-expansion-list (new-default)
1971 "Change speedbar's default expansion list to NEW-DEFAULT."
1974 (completing-read (format "Speedbar Mode (default %s): "
1975 speedbar-previously-used-expansion-list-name)
1976 speedbar-initial-expansion-mode-alist
1978 speedbar-previously-used-expansion-list-name)))
1979 (setq speedbar-previously-used-expansion-list-name
1980 speedbar-initial-expansion-list-name
1981 speedbar-initial-expansion-list-name new-default)
1983 (speedbar-reconfigure-keymaps))
1986 ;;; Special speedbar display management
1988 (defun speedbar-maybe-add-localized-support (buffer)
1989 "Quick check function called on BUFFERs by the speedbar timer function.
1990 Maintains the value of local variables which control speedbars use
1991 of the special mode functions."
1992 (or speedbar-special-mode-expansion-list
1993 (speedbar-add-localized-speedbar-support buffer)))
1995 (defun speedbar-add-localized-speedbar-support (buffer)
1996 "Add localized speedbar support to BUFFER's mode if it is available."
1997 (interactive "bBuffer: ")
1998 (if (stringp buffer) (setq buffer (get-buffer buffer)))
1999 (if (not (buffer-live-p buffer))
2004 (let ((ms (symbol-name major-mode)) v)
2005 (if (not (string-match "-mode$" ms))
2006 nil ;; do nothing to broken mode
2007 (setq ms (substring ms 0 (match-beginning 0)))
2008 (setq v (intern-soft (concat ms "-speedbar-buttons")))
2009 (make-local-variable 'speedbar-special-mode-expansion-list)
2011 (setq speedbar-special-mode-expansion-list t)
2012 ;; If it is autoloaded, we need to load it now so that
2013 ;; we have access to the varialbe -speedbar-menu-items.
2014 ;; Is this XEmacs safe?
2015 (let ((sf (symbol-function v)))
2016 (if (and (listp sf) (eq (car sf) 'autoload))
2017 (load-library (car (cdr sf)))))
2018 (setq speedbar-special-mode-expansion-list (list v))
2019 (setq v (intern-soft (concat ms "-speedbar-key-map")))
2021 nil ;; don't add special keymap
2022 (make-local-variable 'speedbar-special-mode-key-map)
2023 (setq speedbar-special-mode-key-map
2025 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
2027 nil ;; don't add special menus
2028 (make-local-variable 'speedbar-easymenu-definition-special)
2029 (setq speedbar-easymenu-definition-special
2033 (defun speedbar-remove-localized-speedbar-support (buffer)
2034 "Remove any traces that BUFFER supports speedbar in a specialized way."
2037 (kill-local-variable 'speedbar-special-mode-expansion-list)
2038 (kill-local-variable 'speedbar-special-mode-key-map)
2039 (kill-local-variable 'speedbar-easymenu-definition-special)))
2041 ;;; File button management
2043 (defun speedbar-file-lists (directory)
2044 "Create file lists for DIRECTORY.
2045 The car is the list of directories, the cdr is list of files not
2046 matching ignored headers. Cache any directory files found in
2047 `speedbar-directory-contents-alist' and use that cache before scanning
2049 (setq directory (expand-file-name directory))
2050 ;; If in powerclick mode, then the directory we are getting
2051 ;; should be rescanned.
2052 (if speedbar-power-click
2053 (adelete 'speedbar-directory-contents-alist directory))
2054 ;; find the directory, either in the cache, or build it.
2055 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
2056 (let ((default-directory directory)
2057 (dir (directory-files directory nil))
2062 (or (string-match speedbar-file-unshown-regexp (car dir))
2063 (string-match speedbar-directory-unshown-regexp (car dir))))
2064 (if (file-directory-p (car dir))
2065 (setq dirs (cons (car dir) dirs))
2066 (setq files (cons (car dir) files))))
2067 (setq dir (cdr dir)))
2068 (let ((nl (cons (nreverse dirs) (list (nreverse files)))))
2069 (aput 'speedbar-directory-contents-alist directory nl)
2073 (defun speedbar-directory-buttons (directory index)
2074 "Insert a single button group at point for DIRECTORY.
2075 Each directory path part is a different button. If part of the path
2076 matches the user directory ~, then it is replaced with a ~.
2077 INDEX is not used, but is required by the caller."
2078 (let* ((tilde (expand-file-name "~"))
2079 (dd (expand-file-name directory))
2080 (junk (string-match (regexp-quote tilde) dd))
2082 (concat "~" (substring dd (match-end 0)))
2085 (if (string-match "^~/?\\'" displayme) (setq displayme (concat tilde "/")))
2089 (while (re-search-forward "\\([^/]+\\)/" nil t)
2090 (speedbar-make-button (match-beginning 1) (match-end 1)
2091 'speedbar-directory-face
2092 'speedbar-highlight-face
2093 'speedbar-directory-buttons-follow
2094 (if (= (match-beginning 1) p)
2095 (expand-file-name "~/") ;the tilde
2096 (buffer-substring-no-properties
2098 ;; Nuke the beginning of the directory if it's too long...
2099 (cond ((eq speedbar-directory-button-trim-method 'span)
2101 (let ((ww (or (speedbar-frame-width) 20)))
2102 (move-to-column ww nil)
2103 (while (>= (current-column) ww)
2104 (re-search-backward "/" nil t)
2105 (if (<= (current-column) 2)
2107 (re-search-forward "/" nil t)
2108 (if (< (current-column) 4)
2109 (re-search-forward "/" nil t))
2111 (if (looking-at "/?$")
2114 (move-to-column ww nil)))))
2115 ((eq speedbar-directory-button-trim-method 'trim)
2117 (let ((ww (or (speedbar-frame-width) 20))
2118 (tl (current-column)))
2121 (move-to-column (- tl ww))
2122 (if (re-search-backward "/" nil t)
2124 (delete-region (point-min) (point))
2128 (if (string-match "\\`/[^/]+/\\'" displayme)
2133 (speedbar-make-button p (point)
2134 'speedbar-directory-face
2135 'speedbar-highlight-face
2136 'speedbar-directory-buttons-follow
2139 (insert-char ?\n 1 nil)))
2141 (defun speedbar-make-tag-line (exp-button-type
2142 exp-button-char exp-button-function
2144 tag-button tag-button-function tag-button-data
2145 tag-button-face depth)
2146 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2147 This is the button that expands or contracts a node (if applicable),
2148 and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2149 is the function to call if it's clicked on. Button types are
2150 'bracket, 'angle, 'curly, or nil. EXP-BUTTON-DATA is extra data
2151 attached to the text forming the expansion button.
2153 Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2154 function to call if clicked on, and TAG-BUTTON-DATA is the data to
2155 attach to the text field (such a tag positioning, etc).
2156 TAG-BUTTON-FACE is a face used for this type of tag.
2158 Lastly, DEPTH shows the depth of expansion.
2160 This function assumes that the cursor is in the speedbar window at the
2161 position to insert a new item, and that the new item will end with a CR"
2162 (let ((start (point))
2164 (insert (int-to-string depth) ":")
2166 (put-text-property start end 'invisible t)
2168 (insert-char ? depth nil)
2169 (put-text-property (- (point) depth) (point) 'invisible nil)
2170 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2171 ((eq exp-button-type 'angle) "<%c>")
2172 ((eq exp-button-type 'curly) "{%c}")
2174 (buttxt (format exp-button exp-button-char))
2176 (end (progn (insert buttxt) (point)))
2177 (bf (if exp-button-type 'speedbar-button-face nil))
2178 (mf (if exp-button-function 'speedbar-highlight-face nil))
2180 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
2181 (if speedbar-hide-button-brackets-flag
2183 (put-text-property start (1+ start) 'invisible t)
2184 (put-text-property end (1- end) 'invisible t)))
2186 (insert-char ? 1 nil)
2187 (put-text-property (1- (point)) (point) 'invisible nil)
2188 (let ((start (point))
2189 (end (progn (insert tag-button) (point))))
2190 (insert-char ?\n 1 nil)
2191 (put-text-property (1- (point)) (point) 'invisible nil)
2192 (speedbar-make-button start end tag-button-face
2193 (if tag-button-function 'speedbar-highlight-face nil)
2194 tag-button-function tag-button-data))
2197 (defun speedbar-change-expand-button-char (char)
2198 "Change the expansion button character to CHAR for the current line."
2201 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2203 (speedbar-with-writable
2204 (goto-char (match-beginning 1))
2206 (insert-char char 1 t)
2207 (put-text-property (point) (1- (point)) 'invisible nil)))))
2210 ;;; Build button lists
2212 (defun speedbar-insert-files-at-point (files level)
2213 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2214 Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2215 don't know how to manage them. The input parameter FILES is a cons
2216 cell of the form ( 'DIRLIST . 'FILELIST )"
2217 ;; Start inserting all the directories
2218 (let ((dirs (car files)))
2220 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired (car dirs)
2221 (car dirs) 'speedbar-dir-follow nil
2222 'speedbar-directory-face level)
2223 (setq dirs (cdr dirs))))
2224 (let ((lst (car (cdr files)))
2225 (case-fold-search t))
2227 (let* ((known (string-match speedbar-file-regexp (car lst)))
2228 (expchar (if known ?+ ??))
2229 (fn (if known 'speedbar-tag-file nil)))
2230 (if (or speedbar-show-unknown-files (/= expchar ??))
2231 (speedbar-make-tag-line 'bracket expchar fn (car lst)
2232 (car lst) 'speedbar-find-file nil
2233 'speedbar-file-face level)))
2234 (setq lst (cdr lst)))))
2236 (defun speedbar-default-directory-list (directory index)
2237 "Insert files for DIRECTORY with level INDEX at point."
2238 (speedbar-insert-files-at-point
2239 (speedbar-file-lists directory) index)
2240 (speedbar-reset-scanners)
2242 ;; If the shown files variable has extra directories, then
2243 ;; it is our responsibility to redraw them all
2244 ;; Luckilly, the nature of inserting items into this list means
2245 ;; that by reversing it, we can easilly go in the right order
2246 (let ((sf (cdr (reverse speedbar-shown-directories))))
2247 (setq speedbar-shown-directories
2248 (list (expand-file-name default-directory)))
2249 ;; exand them all as we find them
2251 (if (speedbar-goto-this-file (car sf))
2254 (if (looking-at "[0-9]+:[ ]*<")
2256 (goto-char (match-end 0))
2257 (speedbar-do-function-pointer)))
2258 (setq sf (cdr sf)))))
2261 (defun speedbar-apply-one-tag-hierarchy-method (lst method)
2262 "Adjust the tag hierarchy LST by METHOD."
2265 (sort (copy-alist lst)
2266 (lambda (a b) (string< (car a) (car b)))))
2267 ((eq method 'prefix-group)
2272 (short-group-list nil)
2273 (short-start-name nil)
2274 (short-end-name nil)
2275 (num-shorts-grouped 0)
2276 (bins (make-vector 256 nil))
2278 ;; Break out sub-lists
2280 (if (listp (cdr-safe (car-safe lst)))
2281 (setq newlst (cons (car lst) newlst))
2282 (setq sublst (cons (car lst) sublst)))
2283 (setq lst (cdr lst)))
2284 ;; Reverse newlst because it was made backwards.
2285 ;; Sublist doesn't need reversing because the act
2286 ;; of binning things will reverse it for us.
2287 (setq newlst (nreverse newlst))
2288 ;; Now, first find out how long our list is. Never let a
2289 ;; list get-shorter than our minimum.
2290 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2291 (setq work-list (nreverse sublst))
2292 (setq diff-idx (length (try-completion "" sublst)))
2293 ;; Sort the whole list into bins.
2295 (let ((e (car sublst))
2296 (s (car (car sublst))))
2297 (cond ((<= (length s) diff-idx)
2298 ;; 0 storage bin for shorty.
2299 (aset bins 0 (cons e (aref bins 0))))
2301 ;; stuff into a bin based on ascii value at diff
2302 (aset bins (aref s diff-idx)
2303 (cons e (aref bins (aref s diff-idx)))))))
2304 (setq sublst (cdr sublst)))
2305 ;; Go through all our bins Stick singles into our
2306 ;; junk-list, everything else as sublsts in work-list.
2307 ;; If two neighboring lists are both small, make a grouped
2308 ;; group combinding those two sub-lists.
2310 (while (> 256 diff-idx)
2311 (let ((l (nreverse ;; Reverse the list since they are stuck in
2313 (aref bins diff-idx))))
2315 (let ((tmp (cons (try-completion "" l) l)))
2316 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2317 (> (+ (length l) (length short-group-list))
2318 speedbar-tag-split-minimum-length))
2320 ;; We have reached a longer list, so we
2321 ;; must finish off a grouped group.
2323 ((and short-group-list
2324 (= (length short-group-list)
2325 num-shorts-grouped))
2326 ;; All singles? Junk list
2327 (setq junk-list (append short-group-list
2329 ((= num-shorts-grouped 1)
2330 ;; Only one short group? Just stick it in
2331 ;; there by itself. Make a group, and find
2333 (let ((subexpression (try-completion
2334 "" short-group-list)))
2335 (if (< (length subexpression)
2336 speedbar-tag-group-name-minimum-length)
2338 (concat short-start-name
2341 (car (car short-group-list))
2342 (length short-start-name))
2345 (cons (cons subexpression
2349 ;; Multiple groups to be named in a special
2350 ;; way by displaying the range over which we
2351 ;; have grouped them.
2353 (cons (cons (concat short-start-name
2356 (nreverse short-group-list))
2358 ;; Reset short group list information every time.
2359 (setq short-group-list nil
2360 short-start-name nil
2362 num-shorts-grouped 0)))
2363 ;; Ok, now that we cleaned up the short-group-list,
2364 ;; we can deal with this new list, to decide if it
2365 ;; should go on one of these sub-lists or not.
2366 (if (< (length l) speedbar-tag-regroup-maximum-length)
2367 (setq short-group-list (append short-group-list l)
2368 num-shorts-grouped (1+ num-shorts-grouped)
2369 short-end-name (car tmp)
2370 short-start-name (if short-start-name
2373 (setq work-list (cons tmp work-list))))))
2374 (setq diff-idx (1+ diff-idx))))
2375 ;; Did we run out of things? Drop our new list onto the end.
2377 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2378 ;; All singles? Junk list
2379 (setq junk-list (append short-group-list junk-list)))
2380 ((= num-shorts-grouped 1)
2381 ;; Only one short group? Just stick it in
2384 (cons (cons (try-completion "" short-group-list)
2388 ;; Multiple groups to be named in a special
2389 ;; way by displaying the range over which we
2390 ;; have grouped them.
2392 (cons (cons (concat short-start-name " to " short-end-name)
2395 ;; Reverse the work list nreversed when consing.
2396 (setq work-list (nreverse work-list))
2397 ;; Now, stick our new list onto the end of
2400 (append newlst work-list junk-list)
2401 (append newlst work-list))
2402 (append newlst junk-list))))
2403 ((eq method 'trim-words)
2410 (if (listp (cdr-safe (car-safe lst)))
2411 (setq newlst (cons (car lst) newlst))
2412 (setq sublst (cons (car lst) sublst)))
2413 (setq lst (cdr lst)))
2414 ;; Get the prefix to trim by. Make sure that we don't trim
2415 ;; off silly pieces, only complete understandable words.
2416 (setq trim-prefix (try-completion "" sublst))
2417 (if (or (= (length sublst) 1)
2419 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2420 (append (nreverse newlst) (nreverse sublst))
2421 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2423 (setq trim-chars (length trim-prefix))
2426 (cons (substring (car (car sublst)) trim-chars)
2429 sublst (cdr sublst)))
2430 ;; Put the lists together
2431 (append (nreverse newlst) trimlst))))
2432 ((eq method 'simple-group)
2436 (if (listp (cdr-safe (car-safe lst)))
2437 (setq newlst (cons (car lst) newlst))
2438 (setq sublst (cons (car lst) sublst)))
2439 (setq lst (cdr lst)))
2442 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2443 (nreverse newlst))))
2446 (defun speedbar-create-tag-hierarchy (lst)
2447 "Adjust the tag hierarchy in LST, and return it.
2448 This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2449 the list. See it's value for details."
2450 (let* ((f (save-excursion
2452 (speedbar-line-path)))
2453 (methods (if (get-file-buffer f)
2454 (save-excursion (set-buffer (get-file-buffer f))
2455 speedbar-tag-hierarchy-method)
2456 speedbar-tag-hierarchy-method)))
2458 (setq lst (speedbar-apply-one-tag-hierarchy-method lst (car methods))
2459 methods (cdr methods)))
2462 (defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2463 "At LEVEL, insert a generic multi-level alist LST.
2464 Associations with lists get {+} tags (to expand into more nodes) and
2465 those with positions just get a > as the indicator. {+} buttons will
2466 have the function EXPAND-FUN and the token is the CDR list. The token
2467 name will have the function FIND-FUN and not token."
2468 ;; Remove imenu rescan button
2469 (if (string= (car (car lst)) "*Rescan*")
2470 (setq lst (cdr lst)))
2472 (setq lst (speedbar-create-tag-hierarchy lst))
2475 (cond ((null (car-safe lst)) nil) ;this would be a separator
2476 ((or (numberp (cdr-safe (car-safe lst)))
2477 (markerp (cdr-safe (car-safe lst))))
2478 (speedbar-make-tag-line nil nil nil nil ;no expand button data
2479 (car (car lst)) ;button name
2481 (cdr (car lst)) ;token is position
2484 ((listp (cdr-safe (car-safe lst)))
2485 (speedbar-make-tag-line 'curly ?+ expand-fun (cdr (car lst))
2486 (car (car lst)) ;button name
2487 nil nil 'speedbar-tag-face
2489 (t (message "Ooops!")))
2490 (setq lst (cdr lst))))
2494 (defun speedbar-update-contents ()
2495 "Generically update the contents of the speedbar buffer."
2497 ;; Set the current special buffer
2498 (setq speedbar-desired-buffer nil)
2499 ;; Check for special modes
2500 (speedbar-maybe-add-localized-support (current-buffer))
2501 ;; Choose the correct method of doodling.
2502 (if (and speedbar-mode-specific-contents-flag
2503 (listp speedbar-special-mode-expansion-list)
2504 speedbar-special-mode-expansion-list
2506 'speedbar-special-mode-expansion-list
2508 ;;(eq (get major-mode 'mode-class 'special)))
2509 (speedbar-update-special-contents)
2510 (speedbar-update-directory-contents)))
2512 (defun speedbar-update-directory-contents ()
2513 "Update the contents of the speedbar buffer based on the current directory."
2514 (let ((cbd (expand-file-name default-directory))
2516 (funclst (speedbar-initial-expansion-list))
2517 (cache speedbar-full-text-cache)
2518 ;; disable stealth during update
2519 (speedbar-stealthy-function-list nil)
2522 ;; Because there is a bug I can't find just yet
2525 (set-buffer speedbar-buffer)
2526 ;; If we are updating contents to where we are, then this is
2527 ;; really a request to update existing contents, so we must be
2528 ;; careful with our text cache!
2529 (if (member cbd speedbar-shown-directories)
2532 ;; If the current directory is not the last element in the dir
2533 ;; list, then we ALSO need to zap the list of expanded directories
2534 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2535 (setq speedbar-shown-directories (list cbd))))
2537 ;; Build cbd-parent, and see if THAT is in the current shown
2538 ;; directories. First, go through pains to get the parent directory
2539 (if (and speedbar-smart-directory-expand-flag
2541 (setq cbd-parent cbd)
2542 (if (string-match "/$" cbd-parent)
2543 (setq cbd-parent (substring cbd-parent 0
2544 (match-beginning 0))))
2545 (setq cbd-parent (file-name-directory cbd-parent)))
2546 (member cbd-parent speedbar-shown-directories))
2547 (setq expand-local t)
2549 ;; If this directory is NOT in the current list of available
2550 ;; paths, then use the cache, and set the cache to our new
2551 ;; value. Make sure to unhighlight the current file, or if we
2552 ;; come back to this directory, it might be a different file
2553 ;; and then we get a mess!
2554 (if (> (point-max) 1)
2556 (speedbar-clear-current-file)
2557 (setq speedbar-full-text-cache
2558 (cons speedbar-shown-directories (buffer-string)))))
2560 ;; Check if our new directory is in the list of directories
2561 ;; shown in the text-cache
2562 (if (member cbd (car cache))
2563 (setq speedbar-shown-directories (car cache)
2565 ;; default the shown directories to this list...
2566 (setq speedbar-shown-directories (list cbd)))
2568 (if (not expand-local) (setq speedbar-last-selected-file nil))
2569 (speedbar-with-writable
2570 (if (and expand-local
2571 ;; Find this directory as a speedbar node.
2572 (speedbar-path-line cbd))
2574 (speedbar-expand-line)
2577 (setq default-directory
2578 (nth (1- (length speedbar-shown-directories))
2579 speedbar-shown-directories))
2580 (insert (cdr cache)))
2583 (setq default-directory cbd)
2584 (funcall (car funclst) cbd 0)
2585 (setq funclst (cdr funclst))))))
2586 (goto-char (point-min)))))
2587 (speedbar-reconfigure-keymaps))
2589 (defun speedbar-update-special-contents ()
2590 "Used the mode-specific variable to fill in the speedbar buffer.
2591 This should only be used by modes classified as special."
2592 (let ((funclst speedbar-special-mode-expansion-list)
2593 (specialbuff (current-buffer)))
2595 (setq speedbar-desired-buffer specialbuff)
2596 (set-buffer speedbar-buffer)
2597 ;; If we are leaving a directory, cache it.
2598 (if (not speedbar-shown-directories)
2601 ;; Clean up directory maintenance stuff
2602 (speedbar-clear-current-file)
2603 (setq speedbar-full-text-cache
2604 (cons speedbar-shown-directories (buffer-string))
2605 speedbar-shown-directories nil))
2606 ;; Now fill in the buffer with our newly found specialized list.
2607 (speedbar-with-writable
2609 ;; We do not erase the buffer because these functions may
2610 ;; decide NOT to update themselves.
2611 (funcall (car funclst) specialbuff)
2612 (setq funclst (cdr funclst))))
2613 (goto-char (point-min))))
2614 (speedbar-reconfigure-keymaps))
2616 (defun speedbar-timer-fn ()
2617 "Run whenever Emacs is idle to update the speedbar item."
2618 (if (not (and (frame-live-p speedbar-frame)
2619 (frame-live-p speedbar-attached-frame)))
2620 (speedbar-set-timer nil)
2621 ;; Save all the match data so that we don't mess up executing fns
2623 ;; Only do stuff if the frame is visible, not an icon, and if
2624 ;; it is currently flagged to do something.
2625 (if (and speedbar-update-flag
2626 (frame-visible-p speedbar-frame)
2627 (not (eq (frame-visible-p speedbar-frame) 'icon)))
2628 (let ((af (selected-frame)))
2629 (save-window-excursion
2630 (select-frame speedbar-attached-frame)
2631 ;; make sure we at least choose a window to
2632 ;; get a good directory from
2633 (if (window-minibuffer-p (selected-window))
2635 ;; Check for special modes
2636 (speedbar-maybe-add-localized-support (current-buffer))
2637 ;; Update for special mode all the time!
2638 (if (and speedbar-mode-specific-contents-flag
2639 (listp speedbar-special-mode-expansion-list)
2640 speedbar-special-mode-expansion-list
2642 'speedbar-special-mode-expansion-list
2644 ;;(eq (get major-mode 'mode-class 'special)))
2646 (if (<= 2 speedbar-verbosity-level)
2647 (message "Updating speedbar to special mode: %s..."
2649 (speedbar-update-special-contents)
2650 (if (<= 2 speedbar-verbosity-level)
2652 (message "Updating speedbar to special mode: %s...done"
2655 ;; Update all the contents if directories change!
2656 (if (or (member (expand-file-name default-directory)
2657 speedbar-shown-directories)
2658 (and speedbar-ignored-path-regexp
2660 speedbar-ignored-path-regexp
2661 (expand-file-name default-directory)))
2662 (member major-mode speedbar-ignored-modes)
2663 (eq af speedbar-frame)
2664 (not (buffer-file-name)))
2666 (if (<= 1 speedbar-verbosity-level)
2667 (message "Updating speedbar to: %s..."
2669 (speedbar-update-directory-contents)
2670 (if (<= 1 speedbar-verbosity-level)
2672 (message "Updating speedbar to: %s...done"
2676 ;; Now run stealthy updates of time-consuming items
2677 (speedbar-stealthy-updates)))
2678 ;; Now run the mouse tracking system
2679 (speedbar-show-info-under-mouse)))
2680 (run-hooks 'speedbar-timer-hook))
2683 ;;; Stealthy activities
2685 (defvar speedbar-stealthy-update-recurse nil
2686 "Recursion avoidance variable for stealthy update.")
2688 (defun speedbar-stealthy-updates ()
2689 "For a given speedbar, run all items in the stealthy function list.
2690 Each item returns t if it completes successfully, or nil if
2691 interrupted by the user."
2692 (if (not speedbar-stealthy-update-recurse)
2693 (let ((l (speedbar-initial-stealthy-functions))
2694 (speedbar-stealthy-update-recurse t))
2696 (speedbar-with-writable
2697 (while (and l (funcall (car l)))
2700 ;;(message "Exit with %S" (car l))
2703 (defun speedbar-reset-scanners ()
2704 "Reset any variables used by functions in the stealthy list as state.
2705 If new functions are added, their state needs to be updated here."
2706 (setq speedbar-vc-to-do-point t
2707 speedbar-obj-to-do-point t)
2708 (run-hooks 'speedbar-scanner-reset-hook)
2711 (defun speedbar-find-selected-file (file)
2712 "Goto the line where FILE is."
2713 (goto-char (point-min))
2715 (while (and (setq m (re-search-forward
2716 (concat " \\(" (file-name-nondirectory file)
2717 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2723 (goto-char (match-beginning 0))
2726 (looking-at "[0-9]+:")
2727 (string-to-number (match-string 0)))))
2728 (match-string 1))))))
2731 (goto-char (match-beginning 1))
2732 (match-string 1)))))
2734 (defun speedbar-clear-current-file ()
2735 "Locate the file thought to be current, and remove its highlighting."
2737 (set-buffer speedbar-buffer)
2738 (if speedbar-last-selected-file
2739 (speedbar-with-writable
2740 (if (speedbar-find-selected-file speedbar-last-selected-file)
2741 (put-text-property (match-beginning 1)
2744 'speedbar-file-face))))))
2746 (defun speedbar-update-current-file ()
2747 "Find the current file, and update our visuals to indicate its name.
2748 This is specific to file names. If the file name doesn't show up, but
2749 it should be in the list, then the directory cache needs to be
2751 (let* ((lastf (selected-frame))
2752 (newcfd (save-excursion
2753 (select-frame speedbar-attached-frame)
2754 (let ((rf (if (buffer-file-name)
2757 (select-frame lastf)
2759 (newcf (if newcfd newcfd))
2760 (lastb (current-buffer))
2761 (sucf-recursive (boundp 'sucf-recursive))
2762 (case-fold-search t))
2764 ;; check here, that way we won't refresh to newcf until
2765 ;; its been written, thus saving ourselves some time
2766 (file-exists-p newcf)
2767 (not (string= newcf speedbar-last-selected-file)))
2769 ;; It is important to select the frame, otherwise the window
2770 ;; we want the cursor to move in will not be updated by the
2771 ;; search-forward command.
2772 (select-frame speedbar-frame)
2773 ;; Remove the old file...
2774 (speedbar-clear-current-file)
2775 ;; now highlight the new one.
2776 (set-buffer speedbar-buffer)
2777 (speedbar-with-writable
2778 (if (speedbar-find-selected-file newcf)
2779 ;; put the property on it
2780 (put-text-property (match-beginning 1)
2783 'speedbar-selected-face)
2784 ;; Oops, it's not in the list. Should it be?
2785 (if (and (string-match speedbar-file-regexp newcf)
2786 (string= (file-name-directory newcfd)
2787 (expand-file-name default-directory)))
2788 ;; yes, it is (we will ignore unknowns for now...)
2791 (if (speedbar-find-selected-file newcf)
2792 ;; put the property on it
2793 (put-text-property (match-beginning 1)
2796 'speedbar-selected-face)))
2797 ;; if it's not in there now, whatever...
2799 (setq speedbar-last-selected-file newcf))
2800 (if (not sucf-recursive)
2802 (speedbar-center-buffer-smartly)
2803 (speedbar-position-cursor-on-line)
2806 (select-frame lastf)
2808 ;; return that we are done with this activity.
2811 (defun speedbar-add-indicator (indicator-string &optional replace-this)
2812 "Add INDICATOR-STRING to the end of this speedbar line.
2813 If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2814 an the existing indicator is removed. If there is already an
2815 indicator, then do not add a space."
2817 ;; The nature of the beast: Assume we are in "the right place"
2819 (skip-chars-backward (concat " " speedbar-vc-indicator
2820 (car speedbar-obj-indicator)
2821 (cdr speedbar-obj-indicator)))
2822 (if (and (not (looking-at speedbar-indicator-regex))
2823 (not (string= indicator-string " ")))
2824 (insert speedbar-indicator-separator))
2825 (speedbar-with-writable
2827 (if (and replace-this
2828 (re-search-forward replace-this (save-excursion (end-of-line)
2831 (delete-region (match-beginning 0) (match-end 0))))
2833 (if (not (string= " " indicator-string))
2834 (insert indicator-string))))
2836 ;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
2837 ;; Steven L Baur <steve@xemacs.org> said this was important:
2838 (eval-when-compile (or (featurep 'xemacs)
2839 (condition-case () (require 'efs)
2840 (error (require 'ange-ftp)))))
2842 (defun speedbar-check-vc ()
2843 "Scan all files in a directory, and for each see if it's checked out.
2844 See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
2845 to add more types of version control systems."
2846 ;; Check for to-do to be reset. If reset but no RCS is available
2847 ;; then set to nil (do nothing) otherwise, start at the beginning
2849 (set-buffer speedbar-buffer)
2850 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
2851 (speedbar-vc-check-dir-p default-directory)
2852 (not (or (and (featurep 'ange-ftp)
2854 (car (if speedbar-xemacsp
2855 ange-ftp-path-format
2856 ange-ftp-name-format))
2857 (expand-file-name default-directory)))
2858 ;; efs support: Bob Weiner
2859 (and (featurep 'efs)
2861 (car efs-path-regexp)
2862 (expand-file-name default-directory))))))
2863 (setq speedbar-vc-to-do-point 0))
2864 (if (numberp speedbar-vc-to-do-point)
2866 (goto-char speedbar-vc-to-do-point)
2867 (while (and (not (input-pending-p))
2868 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2870 (setq speedbar-vc-to-do-point (point))
2871 (if (speedbar-check-vc-this-line (match-string 1))
2872 (speedbar-add-indicator speedbar-vc-indicator
2873 (regexp-quote speedbar-vc-indicator))
2874 (speedbar-add-indicator " "
2875 (regexp-quote speedbar-vc-indicator))))
2876 (if (input-pending-p)
2877 ;; return that we are incomplete
2879 ;; we are done, set to-do to nil
2880 (setq speedbar-vc-to-do-point nil)
2885 (defun speedbar-check-vc-this-line (depth)
2886 "Return t if the file on this line is check of of a version control system.
2887 Parameter DEPTH is a string with the current depth of indentation of
2888 the file being checked."
2889 (let* ((d (string-to-int depth))
2890 (f (speedbar-line-path d))
2891 (fn (buffer-substring-no-properties
2892 ;; Skip-chars: thanks ptype@dra.hmg.gb
2894 (skip-chars-forward "^ "
2895 (save-excursion (end-of-line)
2898 (fulln (concat f fn)))
2899 (if (<= 2 speedbar-verbosity-level)
2900 (message "Speedbar vc check...%s" fulln))
2901 (and (file-writable-p fulln)
2902 (speedbar-this-file-in-vc f fn))))
2904 (defun speedbar-vc-check-dir-p (path)
2905 "Return t if we should bother checking PATH for version control files.
2906 This can be overloaded to add new types of version control systems."
2909 (file-exists-p (concat path "RCS/"))
2911 (file-exists-p (concat path "SCCS/"))
2912 ;; Remote SCCS project
2913 (let ((proj-dir (getenv "PROJECTDIR")))
2915 (file-exists-p (concat proj-dir "/SCCS"))
2918 (run-hook-with-args 'speedbar-vc-path-enable-hook path)
2921 (defun speedbar-this-file-in-vc (path name)
2922 "Check to see if the file in PATH with NAME is in a version control system.
2923 You can add new VC systems by overriding this function. You can
2924 optimize this function by overriding it and only doing those checks
2925 that will occur on your system."
2928 (file-exists-p (concat path "RCS/" name ",v"))
2929 (file-exists-p (concat path "RCS/" name))
2930 ;; Local SCCS file name
2931 (file-exists-p (concat path "SCCS/s." name))
2932 ;; Remote SCCS file name
2933 (let ((proj-dir (getenv "PROJECTDIR")))
2935 (file-exists-p (concat proj-dir "/SCCS/s." name))
2938 (run-hook-with-args 'speedbar-vc-in-control-hook path name)
2941 ;; Objet File scanning
2942 (defun speedbar-check-objects ()
2943 "Scan all files in a directory, and for each see if there is an object.
2944 See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
2945 to add more object types."
2946 ;; Check for to-do to be reset. If reset but no RCS is available
2947 ;; then set to nil (do nothing) otherwise, start at the beginning
2949 (set-buffer speedbar-buffer)
2950 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
2951 (setq speedbar-obj-to-do-point 0))
2952 (if (numberp speedbar-obj-to-do-point)
2954 (goto-char speedbar-obj-to-do-point)
2955 (while (and (not (input-pending-p))
2956 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2958 (setq speedbar-obj-to-do-point (point))
2959 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
2960 (if (not ind) (setq ind " "))
2961 (speedbar-add-indicator ind (concat
2962 (car speedbar-obj-indicator)
2964 (cdr speedbar-obj-indicator)))))
2965 (if (input-pending-p)
2966 ;; return that we are incomplete
2968 ;; we are done, set to-do to nil
2969 (setq speedbar-obj-to-do-point nil)
2974 (defun speedbar-check-obj-this-line (depth)
2975 "Return t if the file on this line has an associated object.
2976 Parameter DEPTH is a string with the current depth of indentation of
2977 the file being checked."
2978 (let* ((d (string-to-int depth))
2979 (f (speedbar-line-path d))
2980 (fn (buffer-substring-no-properties
2981 ;; Skip-chars: thanks ptype@dra.hmg.gb
2983 (skip-chars-forward "^ "
2984 (save-excursion (end-of-line)
2987 (fulln (concat f fn)))
2988 (if (<= 2 speedbar-verbosity-level)
2989 (message "Speedbar obj check...%s" fulln))
2990 (let ((oa speedbar-obj-alist))
2991 (while (and oa (not (string-match (car (car oa)) fulln)))
2993 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
2996 ;; Find out if the object is out of date or not.
2997 (let ((date1 (nth 5 (file-attributes fulln)))
2998 (date2 (nth 5 (file-attributes (concat
2999 (file-name-sans-extension fulln)
3001 (if (or (< (car date1) (car date2))
3002 (and (= (car date1) (car date2))
3003 (< (nth 1 date1) (nth 1 date2))))
3004 (car speedbar-obj-indicator)
3005 (cdr speedbar-obj-indicator)))))))
3007 ;;; Clicking Activity
3009 (defun speedbar-quick-mouse (e)
3010 "Since mouse events are strange, this will keep the mouse nicely positioned.
3011 This should be bound to mouse event E."
3014 (speedbar-position-cursor-on-line)
3017 (defun speedbar-position-cursor-on-line ()
3018 "Position the cursor on a line."
3019 (let ((oldpos (point)))
3021 (if (looking-at "[0-9]+:\\s-*..?.? ")
3022 (goto-char (1- (match-end 0)))
3023 (goto-char oldpos))))
3025 (defun speedbar-power-click (e)
3026 "Activate any speedbar button as a power click.
3027 This should be bound to mouse event E."
3029 (let ((speedbar-power-click t))
3030 (speedbar-click e)))
3032 (defun speedbar-click (e)
3033 "Activate any speedbar buttons where the mouse is clicked.
3034 This must be bound to a mouse event. A button is any location of text
3035 with a mouse face that has a text property called `speedbar-function'.
3036 This should be bound to mouse event E."
3039 (speedbar-do-function-pointer)
3040 (speedbar-quick-mouse e))
3042 (defun speedbar-double-click (e)
3043 "Activate any speedbar buttons where the mouse is clicked.
3044 This must be bound to a mouse event. A button is any location of text
3045 with a mouse face that has a text property called `speedbar-function'.
3046 This should be bound to mouse event E."
3048 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
3049 (cond ((eq (car e) 'down-mouse-1)
3050 (mouse-set-point e))
3051 ((eq (car e) 'mouse-1)
3052 (speedbar-quick-mouse e))
3053 ((or (eq (car e) 'double-down-mouse-1)
3054 (eq (car e) 'triple-down-mouse-1))
3056 (speedbar-do-function-pointer)
3057 (speedbar-quick-mouse e))))
3059 (defun speedbar-do-function-pointer ()
3060 "Look under the cursor and examine the text properties.
3061 From this extract the file/tag name, token, indentation level and call
3062 a function if appropriate"
3063 (let* ((fn (get-text-property (point) 'speedbar-function))
3064 (tok (get-text-property (point) 'speedbar-token))
3065 ;; The 1-,+ is safe because scaning starts AFTER the point
3066 ;; specified. This lets the search include the character the
3068 (tp (previous-single-property-change
3069 (1+ (point)) 'speedbar-function))
3070 (np (next-single-property-change
3071 (point) 'speedbar-function))
3072 (txt (buffer-substring-no-properties (or tp (point-min))
3073 (or np (point-max))))
3074 (dent (save-excursion (beginning-of-line)
3076 (if (looking-at "[0-9]+")
3077 (buffer-substring-no-properties
3078 (match-beginning 0) (match-end 0))
3080 ;;(message "%S:%S:%S:%s" fn tok txt dent)
3081 (and fn (funcall fn txt tok dent)))
3082 (speedbar-position-cursor-on-line))
3084 ;;; Reading info from the speedbar buffer
3086 (defun speedbar-line-file (&optional p)
3087 "Retrieve the file or whatever from the line at P point.
3088 The return value is a string representing the file. If it is a
3089 directory, then it is the directory name."
3093 (if (looking-at (concat
3094 "\\([0-9]+\\): *[[<][-+?][]>] \\([^ \n]+\\)\\("
3095 speedbar-indicator-regex "\\)?"))
3096 (let* ((depth (string-to-int (match-string 1)))
3097 (path (speedbar-line-path depth))
3098 (f (match-string 2)))
3102 (defun speedbar-goto-this-file (file)
3103 "If FILE is displayed, goto this line and return t.
3104 Otherwise do not move and return nil."
3105 (let ((path (substring (file-name-directory (expand-file-name file))
3106 (length (expand-file-name default-directory))))
3109 (goto-char (point-min))
3110 ;; scan all the directories
3111 (while (and path (not (eq path t)))
3112 (if (string-match "^/?\\([^/]+\\)" path)
3113 (let ((pp (match-string 1 path)))
3114 (if (save-match-data
3115 (re-search-forward (concat "> " (regexp-quote pp) "$")
3117 (setq path (substring path (match-end 1)))
3120 ;; find the file part
3121 (if (or (not path) (string= (file-name-nondirectory file) ""))
3122 ;; only had a dir part
3125 (speedbar-position-cursor-on-line)
3127 (goto-char dest) nil)
3128 ;; find the file part
3129 (let ((nd (file-name-nondirectory file)))
3130 (if (re-search-forward
3131 (concat "] \\(" (regexp-quote nd)
3132 "\\)\\(" speedbar-indicator-regex "\\)$")
3135 (speedbar-position-cursor-on-line)
3140 (defun speedbar-line-path (&optional depth)
3141 "Retrieve the pathname associated with the current line.
3142 This may require traversing backwards from DEPTH and combining the default
3143 directory with these items."
3145 ((string= speedbar-initial-expansion-list-name "files")
3151 (looking-at "^\\([0-9]+\\):")
3152 (setq depth (string-to-int (match-string 1)))))
3154 (setq depth (1- depth))
3155 (while (/= depth -1)
3156 (if (not (re-search-backward (format "^%d:" depth) nil t))
3157 (error "Error building path of tag")
3158 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)$")
3159 (setq path (concat (buffer-substring-no-properties
3160 (match-beginning 1) (match-end 1))
3163 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)$")
3164 ;; This is the start of our path.
3165 (setq path (buffer-substring-no-properties
3166 (match-beginning 1) (match-end 1))))))
3167 (setq depth (1- depth)))
3169 (string-match (concat speedbar-indicator-regex "$")
3171 (setq path (substring path 0 (match-beginning 0))))
3172 (concat default-directory path)))))
3174 ;; If we aren't in file mode, then return an empty string to make
3175 ;; sure that we can still get some stuff done.
3178 (defun speedbar-path-line (path)
3179 "Position the cursor on the line specified by PATH."
3181 (if (string-match "/$" path)
3182 (setq path (substring path 0 (match-beginning 0))))
3183 (let ((nomatch t) (depth 0)
3184 (fname (file-name-nondirectory path))
3185 (pname (file-name-directory path)))
3186 (if (not (member pname speedbar-shown-directories))
3187 (error "Internal Error: File %s not shown in speedbar" path))
3188 (goto-char (point-min))
3191 (concat "[]>] \\(" (regexp-quote fname)
3192 "\\)\\(" speedbar-indicator-regex "\\)?$")
3195 (looking-at "\\([0-9]+\\):")
3196 (setq depth (string-to-int (match-string 0))
3197 nomatch (not (string= pname (speedbar-line-path depth))))
3202 (defun speedbar-edit-line ()
3203 "Edit whatever tag or file is on the current speedbar line."
3207 ;; If this fails, then it is a non-standard click, and as such,
3208 ;; perfectly allowed.
3209 (if (re-search-forward "[]>?}] [^ ]"
3210 (save-excursion (end-of-line) (point))
3212 (speedbar-do-function-pointer)
3214 (speedbar-do-function-pointer)))
3216 (defun speedbar-expand-line ()
3217 "Expand the line under the cursor."
3220 (re-search-forward ":\\s-*.\\+. " (save-excursion (end-of-line) (point)))
3222 (speedbar-do-function-pointer))
3224 (defun speedbar-contract-line ()
3225 "Contract the line under the cursor."
3228 (re-search-forward ":\\s-*.-. " (save-excursion (end-of-line) (point)))
3230 (speedbar-do-function-pointer))
3232 (if speedbar-xemacsp
3233 (defalias 'speedbar-mouse-event-p 'button-press-event-p)
3234 (defun speedbar-mouse-event-p (event)
3235 "Return t if the event is a mouse related event"
3236 ;; And Emacs does it this way
3237 (if (and (listp event)
3238 (member (event-basic-type event)
3239 '(mouse-1 mouse-2 mouse-3)))
3243 (defun speedbar-maybee-jump-to-attached-frame ()
3244 "Jump to the attached frame ONLY if this was not a mouse event."
3245 (if (or (not (speedbar-mouse-event-p last-input-event))
3246 speedbar-activity-change-focus-flag)
3248 (select-frame speedbar-attached-frame)
3251 (defun speedbar-find-file (text token indent)
3252 "Speedbar click handler for filenames.
3253 TEXT, the file will be displayed in the attached frame.
3254 TOKEN is unused, but required by the click handler. INDENT is the
3255 current indentation level."
3256 (let ((cdd (speedbar-line-path indent)))
3257 (speedbar-find-file-in-frame (concat cdd text))
3258 (speedbar-stealthy-updates)
3259 (run-hooks 'speedbar-visiting-file-hook)
3260 ;; Reset the timer with a new timeout when cliking a file
3261 ;; in case the user was navigating directories, we can cancel
3262 ;; that other timer.
3263 (speedbar-set-timer speedbar-update-speed))
3264 (speedbar-maybee-jump-to-attached-frame))
3266 (defun speedbar-dir-follow (text token indent)
3267 "Speedbar click handler for directory names.
3268 Clicking a directory will cause the speedbar to list files in the
3269 the subdirectory TEXT. TOKEN is an unused requirement. The
3270 subdirectory chosen will be at INDENT level."
3271 (setq default-directory
3272 (concat (expand-file-name (concat (speedbar-line-path indent) text))
3274 ;; Because we leave speedbar as the current buffer,
3275 ;; update contents will change directory without
3276 ;; having to touch the attached frame. Turn off smart expand just
3278 (let ((speedbar-smart-directory-expand-flag nil))
3279 (speedbar-update-contents))
3280 (speedbar-set-timer speedbar-navigating-speed)
3281 (setq speedbar-last-selected-file nil)
3282 (speedbar-stealthy-updates))
3284 (defun speedbar-delete-subblock (indent)
3285 "Delete text from point to indentation level INDENT or greater.
3286 Handles end-of-sublist smartly."
3287 (speedbar-with-writable
3289 (end-of-line) (forward-char 1)
3290 (let ((start (point)))
3291 (while (and (looking-at "^\\([0-9]+\\):")
3292 (> (string-to-int (match-string 1)) indent)
3295 (beginning-of-line))
3296 (delete-region start (point))))))
3298 (defun speedbar-dired (text token indent)
3299 "Speedbar click handler for directory expand button.
3300 Clicking this button expands or contracts a directory. TEXT is the
3301 button clicked which has either a + or -. TOKEN is the directory to be
3302 expanded. INDENT is the current indentation level."
3303 (cond ((string-match "+" text) ;we have to expand this dir
3304 (setq speedbar-shown-directories
3305 (cons (expand-file-name
3306 (concat (speedbar-line-path indent) token "/"))
3307 speedbar-shown-directories))
3308 (speedbar-change-expand-button-char ?-)
3309 (speedbar-reset-scanners)
3311 (end-of-line) (forward-char 1)
3312 (speedbar-with-writable
3313 (speedbar-default-directory-list
3314 (concat (speedbar-line-path indent) token "/")
3316 ((string-match "-" text) ;we have to contract this node
3317 (speedbar-reset-scanners)
3318 (let ((oldl speedbar-shown-directories)
3320 (td (expand-file-name
3321 (concat (speedbar-line-path indent) token))))
3323 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3324 (setq newl (cons (car oldl) newl)))
3325 (setq oldl (cdr oldl)))
3326 (setq speedbar-shown-directories newl))
3327 (speedbar-change-expand-button-char ?+)
3328 (speedbar-delete-subblock indent)
3330 (t (error "Ooops... not sure what to do")))
3331 (speedbar-center-buffer-smartly)
3332 (setq speedbar-last-selected-file nil)
3333 (save-excursion (speedbar-stealthy-updates)))
3335 (defun speedbar-directory-buttons-follow (text token indent)
3336 "Speedbar click handler for default directory buttons.
3337 TEXT is the button clicked on. TOKEN is the directory to follow.
3338 INDENT is the current indentation level and is unused."
3339 (if (string-match "^[A-z]:$" token)
3340 (setq default-directory (concat token (char-to-string directory-sep-char)))
3341 (setq default-directory token))
3342 ;; Because we leave speedbar as the current buffer,
3343 ;; update contents will change directory without
3344 ;; having to touch the attached frame.
3345 (speedbar-update-contents)
3346 (speedbar-set-timer speedbar-navigating-speed))
3348 (defun speedbar-tag-file (text token indent)
3349 "The cursor is on a selected line. Expand the tags in the specified file.
3350 The parameter TEXT and TOKEN are required, where TEXT is the button
3351 clicked, and TOKEN is the file to expand. INDENT is the current
3353 (cond ((string-match "+" text) ;we have to expand this file
3354 (let* ((fn (expand-file-name (concat (speedbar-line-path indent)
3356 (lst (if speedbar-use-imenu-flag
3357 (let ((tim (speedbar-fetch-dynamic-imenu fn)))
3359 (speedbar-fetch-dynamic-etags fn)
3361 (speedbar-fetch-dynamic-etags fn))))
3362 ;; if no list, then remove expando button
3364 (speedbar-change-expand-button-char ??)
3365 (speedbar-change-expand-button-char ?-)
3366 (speedbar-with-writable
3368 (end-of-line) (forward-char 1)
3369 (speedbar-insert-generic-list indent
3370 lst 'speedbar-tag-expand
3371 'speedbar-tag-find))))))
3372 ((string-match "-" text) ;we have to contract this node
3373 (speedbar-change-expand-button-char ?+)
3374 (speedbar-delete-subblock indent))
3375 (t (error "Ooops... not sure what to do")))
3376 (speedbar-center-buffer-smartly))
3378 (defun speedbar-tag-find (text token indent)
3379 "For the tag TEXT in a file TOKEN, goto that position.
3380 INDENT is the current indentation level."
3381 (let ((file (speedbar-line-path indent)))
3382 (speedbar-find-file-in-frame file)
3383 (save-excursion (speedbar-stealthy-updates))
3384 ;; Reset the timer with a new timeout when cliking a file
3385 ;; in case the user was navigating directories, we can cancel
3386 ;; that other timer.
3387 (speedbar-set-timer speedbar-update-speed)
3389 (run-hooks 'speedbar-visiting-tag-hook)
3391 (speedbar-maybee-jump-to-attached-frame)
3394 (defun speedbar-tag-expand (text token indent)
3395 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3396 Etags does not support this feature. TEXT will be the button
3397 string. TOKEN will be the list, and INDENT is the current indentation
3399 (cond ((string-match "+" text) ;we have to expand this file
3400 (speedbar-change-expand-button-char ?-)
3401 (speedbar-with-writable
3403 (end-of-line) (forward-char 1)
3404 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
3405 'speedbar-tag-find))))
3406 ((string-match "-" text) ;we have to contract this node
3407 (speedbar-change-expand-button-char ?+)
3408 (speedbar-delete-subblock indent))
3409 (t (error "Ooops... not sure what to do")))
3410 (speedbar-center-buffer-smartly))
3412 ;;; Loading files into the attached frame.
3414 (defun speedbar-find-file-in-frame (file)
3415 "This will load FILE into the speedbar attached frame.
3416 If the file is being displayed in a different frame already, then raise that
3418 (let* ((buff (find-file-noselect file))
3419 (bwin (get-buffer-window buff 0)))
3422 (select-window bwin)
3423 (raise-frame (window-frame bwin)))
3424 (if speedbar-power-click
3425 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3426 (select-frame speedbar-attached-frame)
3427 (switch-to-buffer buff))))
3430 ;;; Centering Utility
3432 (defun speedbar-center-buffer-smartly ()
3433 "Recenter a speedbar buffer so the current indentation level is all visible.
3434 This assumes that the cursor is on a file, or tag of a file which the user is
3436 (if (<= (count-lines (point-min) (point-max))
3437 (1- (window-height (selected-window))))
3438 ;; whole buffer fits
3440 (goto-char (point-min))
3444 (let (depth start end exp p)
3447 (setq depth (if (looking-at "[0-9]+")
3448 (string-to-int (buffer-substring-no-properties
3449 (match-beginning 0) (match-end 0)))
3451 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3454 (if (re-search-backward exp nil t)
3455 (setq start (point))
3456 (setq start (point-min)))
3457 (save-excursion ;Not sure about this part.
3460 (while (and (not (re-search-forward exp nil t))
3462 (setq depth (1- depth))
3463 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3466 (setq end (point-max)))))
3467 ;; Now work out the details of centering
3468 (let ((nl (count-lines start end))
3470 (if (> nl (window-height (selected-window)))
3471 ;; We can't fit it all, so just center on cursor
3472 (progn (goto-char start)
3474 ;; we can fit everything on the screen, but...
3475 (if (and (pos-visible-in-window-p start (selected-window))
3476 (pos-visible-in-window-p end (selected-window)))
3479 ;; we need to do something...
3481 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3482 (lte (count-lines start (point-max))))
3483 (if (and (< (+ newcent lte) (window-height (selected-window)))
3484 (> (- (window-height (selected-window)) lte 1)
3486 (setq newcent (- (window-height (selected-window))
3488 (recenter newcent))))
3492 ;;; Tag Management -- Imenu
3494 (if (not speedbar-use-imenu-flag)
3498 (eval-when-compile (if (locate-library "imenu") (require 'imenu)))
3500 (defun speedbar-fetch-dynamic-imenu (file)
3501 "Load FILE into a buffer, and generate tags using Imenu.
3502 Returns the tag list, or t for an error."
3503 ;; Load this AND compile it in
3506 (set-buffer (find-file-noselect file))
3507 (if speedbar-power-click (setq imenu--index-alist nil))
3509 (let ((index-alist (imenu--make-index-alist t)))
3510 (if speedbar-sort-tags
3511 (sort (copy-alist index-alist)
3512 (lambda (a b) (string< (car a) (car b))))
3517 ;;; Tag Management -- etags (old XEmacs compatibility part)
3519 (defvar speedbar-fetch-etags-parse-list
3520 '(;; Note that java has the same parse-group as c
3521 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\)\\'" .
3522 speedbar-parse-c-or-c++tag)
3523 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3524 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
3525 ; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3526 ; speedbar-parse-fortran77-tag)
3527 ("\\.tex\\'" . speedbar-parse-tex-string)
3529 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3531 "Associations of file extensions and expressions for extracting tags.
3532 To add a new file type, you would want to add a new association to the
3533 list, where the car is the file match, and the cdr is the way to
3534 extract an element from the tags output. If the output is complex,
3535 use a function symbol instead of regexp. The function should expect
3536 to be at the beginning of a line in the etags buffer.
3538 This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3540 (defvar speedbar-fetch-etags-command "etags"
3541 "*Command used to create an etags file.
3543 This variable is ignored if `speedbar-use-imenu-flag' is t")
3545 (defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3546 "*List of arguments to use with `speedbar-fetch-etags-command'.
3547 This creates an etags output buffer. Use `speedbar-toggle-etags' to
3548 modify this list conveniently.
3550 This variable is ignored if `speedbar-use-imenu-flag' is t")
3552 (defun speedbar-toggle-etags (flag)
3553 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3554 FLAG then becomes a member of etags command line arguments. If flag
3555 is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3556 value is \"show\" then toggle the value of
3557 `speedbar-show-unknown-files'.
3559 This function is a convenience function for XEmacs menu created by
3560 Farzin Guilak <farzin@protocol.com>"
3563 ((equal flag "sort")
3564 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3565 ((equal flag "show")
3566 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3567 ((or (equal flag "-C")
3570 (if (member flag speedbar-fetch-etags-arguments)
3571 (setq speedbar-fetch-etags-arguments
3572 (delete flag speedbar-fetch-etags-arguments))
3573 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3576 (defun speedbar-fetch-dynamic-etags (file)
3577 "For FILE, run etags and create a list of symbols extracted.
3578 Each symbol will be associated with its line position in FILE."
3579 (let ((newlist nil))
3582 (if (get-buffer "*etags tmp*")
3583 (kill-buffer "*etags tmp*")) ;kill to clean it up
3584 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3585 (set-buffer (get-buffer-create "*etags tmp*"))
3586 (apply 'call-process speedbar-fetch-etags-command nil
3587 (current-buffer) nil
3588 (append speedbar-fetch-etags-arguments (list file)))
3589 (goto-char (point-min))
3590 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3592 (let ((exprlst speedbar-fetch-etags-parse-list)
3594 (while (and (not ans) exprlst)
3595 (if (string-match (car (car exprlst)) file)
3596 (setq ans (car exprlst)))
3597 (setq exprlst (cdr exprlst)))
3601 (while (not (save-excursion (end-of-line) (eobp)))
3603 (setq tnl (speedbar-extract-one-symbol expr)))
3604 (if tnl (setq newlist (cons tnl newlist)))
3606 (message "Sorry, no support for a file of that extension"))))
3608 (if speedbar-sort-tags
3609 (sort newlist (lambda (a b) (string< (car a) (car b))))
3610 (reverse newlist))))
3612 ;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3613 ;; sure it's needed with the different sorting method.
3615 ;(defun speedbar-clean-etags()
3616 ; "Removes spaces before the ^? character, and removes `#define',
3617 ;return types, etc. preceding tags. This ensures that the sort operation
3618 ;works on the tags, not the return types."
3620 ; (goto-char (point-min))
3622 ; (re-search-forward "(?[ \t](?\C-?" nil t)
3623 ; (replace-match "\C-?" nil nil))
3624 ; (goto-char (point-min))
3626 ; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3627 ; (delete-region (match-beginning 1) (match-end 1)))))
3629 (defun speedbar-extract-one-symbol (expr)
3630 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
3631 The line should contain output from etags. Parse the output using the
3632 regular expression EXPR"
3633 (let* ((sym (if (stringp expr)
3635 (re-search-forward expr (save-excursion
3638 (buffer-substring-no-properties (match-beginning 1)
3641 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3647 (1+ (string-to-int (buffer-substring-no-properties
3655 (defun speedbar-parse-c-or-c++tag ()
3656 "Parse a c or c++ tag, which tends to be a little complex."
3658 (let ((bound (save-excursion (end-of-line) (point))))
3659 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3660 (buffer-substring-no-properties (match-beginning 1)
3662 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3663 (buffer-substring-no-properties (match-beginning 1)
3665 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3666 (buffer-substring-no-properties (match-beginning 1)
3671 (defun speedbar-parse-tex-string ()
3672 "Parse a Tex string. Only find data which is relevant."
3674 (let ((bound (save-excursion (end-of-line) (point))))
3675 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3676 (buffer-substring-no-properties (match-beginning 0)
3681 ;;; BUFFER DISPLAY mode.
3683 (defvar speedbar-buffers-key-map nil
3684 "Keymap used when in the buffers display mode.")
3686 (if speedbar-buffers-key-map
3688 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3690 ;; Basic tree features
3691 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3692 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3693 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3694 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3696 ;; Buffer specific keybindings
3697 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3698 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3702 (defvar speedbar-buffer-easymenu-definition
3703 '(["Jump to buffer" speedbar-edit-line t]
3704 ["Expand File Tags" speedbar-expand-line
3705 (save-excursion (beginning-of-line)
3706 (looking-at "[0-9]+: *.\\+. "))]
3707 ["Contract File Tags" speedbar-contract-line
3708 (save-excursion (beginning-of-line)
3709 (looking-at "[0-9]+: *.-. "))]
3711 "Menu item elements shown when displaying a buffer list.")
3713 (defun speedbar-buffer-buttons (directory zero)
3714 "Create speedbar buttons based on the buffers currently loaded.
3715 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3716 (speedbar-buffer-buttons-engine nil))
3718 (defun speedbar-buffer-buttons-temp (directory zero)
3719 "Create speedbar buttons based on the buffers currently loaded.
3720 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3721 (speedbar-buffer-buttons-engine t))
3723 (defun speedbar-buffer-buttons-engine (temp)
3724 "Create speedbar buffer buttons.
3725 If TEMP is non-nil, then clicking on a buffer restores the previous display."
3726 (insert "Active Buffers:\n")
3727 (let ((bl (buffer-list)))
3729 (if (string-match "^[ *]" (buffer-name (car bl)))
3731 (let* ((known (string-match speedbar-file-regexp
3732 (buffer-name (car bl))))
3733 (expchar (if known ?+ ??))
3734 (fn (if known 'speedbar-tag-file nil))
3735 (fname (save-excursion (set-buffer (car bl))
3736 (buffer-file-name))))
3737 (speedbar-make-tag-line 'bracket expchar fn fname
3738 (buffer-name (car bl))
3739 'speedbar-buffer-click temp
3740 'speedbar-file-face 0)))
3742 (setq bl (buffer-list))
3743 (insert "Scratch Buffers:\n")
3745 (if (not (string-match "^\\*" (buffer-name (car bl))))
3747 (if (eq (car bl) speedbar-buffer)
3749 (speedbar-make-tag-line 'bracket ?? nil nil
3750 (buffer-name (car bl))
3751 'speedbar-buffer-click temp
3752 'speedbar-file-face 0)))
3754 (setq bl (buffer-list))
3755 (insert "Hidden Buffers:\n")
3757 (if (not (string-match "^ " (buffer-name (car bl))))
3759 (if (eq (car bl) speedbar-buffer)
3761 (speedbar-make-tag-line 'bracket ?? nil nil
3762 (buffer-name (car bl))
3763 'speedbar-buffer-click temp
3764 'speedbar-file-face 0)))
3765 (setq bl (cdr bl)))))
3767 (defun speedbar-buffer-click (text token indent)
3768 "When the users clicks on a buffer-button in speedbar.
3769 TEXT is the buffer's name, TOKEN and INDENT are unused."
3770 (if speedbar-power-click
3771 (let ((pop-up-frames t)) (select-window (display-buffer text)))
3772 (select-frame speedbar-attached-frame)
3773 (switch-to-buffer text)
3774 (if token (speedbar-change-initial-expansion-list
3775 speedbar-previously-used-expansion-list-name))))
3777 (defun speedbar-buffer-kill-buffer ()
3778 "Kill the buffer the cursor is on in the speedbar buffer."
3782 ;; If this fails, then it is a non-standard click, and as such,
3783 ;; perfectly allowed.
3784 (if (re-search-forward "[]>?}] [^ ]"
3785 (save-excursion (end-of-line) (point))
3789 (buffer-substring (point) (save-excursion
3792 (if (and (get-buffer text)
3793 (y-or-n-p (format "Kill buffer %s? " text)))
3795 (speedbar-refresh))))))
3797 (defun speedbar-buffer-revert-buffer ()
3798 "Revert the buffer the cursor is on in the speedbar buffer."
3802 ;; If this fails, then it is a non-standard click, and as such,
3803 ;; perfectly allowed
3804 (if (re-search-forward "[]>?}] [^ ]"
3805 (save-excursion (end-of-line) (point))
3809 (buffer-substring (point) (save-excursion
3812 (if (get-buffer text)
3815 (revert-buffer t)))))))
3819 ;;; Color loading section This is messy *Blech!*
3821 (defface speedbar-button-face '((((class color) (background light))
3822 (:foreground "green4"))
3823 (((class color) (background dark))
3824 (:foreground "green3")))
3825 "Face used for +/- buttons."
3826 :group 'speedbar-faces)
3828 (defface speedbar-file-face '((((class color) (background light))
3829 (:foreground "cyan4"))
3830 (((class color) (background dark))
3831 (:foreground "cyan"))
3833 "Face used for file names."
3834 :group 'speedbar-faces)
3836 (defface speedbar-directory-face '((((class color) (background light))
3837 (:foreground "blue4"))
3838 (((class color) (background dark))
3839 (:foreground "light blue")))
3840 "Faced used for directory names."
3841 :group 'speedbar-faces)
3842 (defface speedbar-tag-face '((((class color) (background light))
3843 (:foreground "brown"))
3844 (((class color) (background dark))
3845 (:foreground "yellow")))
3846 "Face used for displaying tags."
3847 :group 'speedbar-faces)
3849 (defface speedbar-selected-face '((((class color) (background light))
3850 (:foreground "red" :underline t))
3851 (((class color) (background dark))
3852 (:foreground "red" :underline t))
3854 "Face used to underline the file in the active window."
3855 :group 'speedbar-faces)
3857 (defface speedbar-highlight-face '((((class color) (background light))
3858 (:background "green"))
3859 (((class color) (background dark))
3860 (:background "sea green"))
3861 (((class grayscale monochrome)
3863 (:background "black"))
3864 (((class grayscale monochrome)
3866 (:background "white")))
3867 "Face used for highlighting buttons with the mouse."
3868 :group 'speedbar-faces)
3870 ;; some edebug hooks
3871 (add-hook 'edebug-setup-hook
3873 (def-edebug-spec speedbar-with-writable def-body)))
3876 ;;; speedbar ends here
3878 ;; run load-time hooks
3879 (run-hooks 'speedbar-load-hook)