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.8 1998/08/14 01:15:33 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 (defcustom 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 :type '(radio (const :tag "File Directorys" file)
278 (defvar speedbar-previously-used-expansion-list-name "files"
279 "Save the last expansion list method.
280 This is used for returning to a previous expansion list method when
281 the user is done with the current expansion list.")
283 (defvar speedbar-stealthy-function-list
285 speedbar-update-current-file speedbar-check-vc speedbar-check-objects)
287 "List of functions to periodically call stealthily.
288 This list is of the form:
289 '( (\"NAME\" FUNCTION ...)
291 where NAME is the name of the major display mode these functions are
292 for, and the remaining elements FUNCTION are functions to call in order.
293 Each function must return nil if interrupted, or t if completed.
294 Stealthy functions which have a single operation should always return
295 t. Functions which take a long time should maintain a state (where
296 they are in their speedbar related calculations) and permit
297 interruption. See `speedbar-check-vc' as a good example.")
299 (defcustom speedbar-mode-specific-contents-flag t
300 "*Non-nil means speedbar will show special mode contents.
301 This permits some modes to create customized contents for the speedbar
306 (defvar speedbar-special-mode-expansion-list nil
307 "Default function list for creating specialized button lists.
308 This list is set by modes that wish to have special speedbar displays.
309 The list is of function names. Each function is called with one
310 parameter BUFFER, the originating buffer. The current buffer is the
313 (defvar speedbar-special-mode-key-map nil
314 "Default keymap used when identifying a specialized display mode.
315 This keymap is local to each buffer that wants to define special keybindings
316 effective when it's display is shown.")
318 (defcustom speedbar-visiting-file-hook nil
319 "Hooks run when speedbar visits a file in the selected frame."
323 (defcustom speedbar-visiting-tag-hook nil
324 "Hooks run when speedbar visits a tag in the selected frame."
328 (defcustom speedbar-load-hook nil
329 "Hooks run when speedbar is loaded."
333 (defcustom speedbar-show-unknown-files nil
334 "*Non-nil show files we can't expand with a ? in the expand button.
335 nil means don't show the file in the list."
339 (defcustom speedbar-update-speed
341 (if speedbar-xemacs20p
342 2 ; 1 is too obrusive in XEmacs
343 5) ; when no idleness, need long delay
345 "*Idle time in seconds needed before speedbar will update itself.
346 Updates occur to allow speedbar to display directory information
347 relevant to the buffer you are currently editing."
351 ;; When I moved to a repeating timer, I had the horrible missfortune
352 ;; of loosing the ability for adaptive speed choice. This update
353 ;; speed currently causes long delays when it should have been turned off.
354 (defcustom speedbar-navigating-speed speedbar-update-speed
355 "*Idle time to wait after navigation commands in speedbar are executed.
356 Navigation commands included expanding/contracting nodes, and moving
357 between different directories."
361 (defcustom speedbar-frame-parameters '((minibuffer . nil)
366 "*Parameters to use when creating the speedbar frame in Emacs.
367 Parameters not listed here which will be added automatically are
368 `height' which will be initialized to the height of the frame speedbar
371 :type '(repeat (sexp :tag "Parameter:")))
373 ;; These values by Hrvoje Niksic <hniksic@srce.hr>
374 (defcustom speedbar-frame-plist
375 '(minibuffer nil width 20 border-width 0
376 internal-border-width 0 unsplittable t
377 default-toolbar-visible-p nil has-modeline-p nil
378 menubar-visible-p nil
379 ;; I don't see the particular value of these three, but...
380 text-pointer-glyph [cursor-font :data "top_left_arrow"]
381 nontext-pointer-glyph [cursor-font :data "top_left_arrow"]
382 selection-pointer-glyph [cursor-font :data "hand2"])
383 "*Parameters to use when creating the speedbar frame in XEmacs.
384 Parameters not listed here which will be added automatically are
385 `height' which will be initialized to the height of the frame speedbar
388 :type '(repeat (group :inline t
389 (symbol :tag "Property")
390 (sexp :tag "Value"))))
392 (defcustom speedbar-use-imenu-flag (stringp (locate-library "imenu"))
393 "*Non-nil means use imenu for file parsing. nil to use etags.
394 XEmacs prior to 20.4 doesn't support imenu, therefore the default is to
395 use etags instead. Etags support is not as robust as imenu support."
400 (defcustom speedbar-track-mouse-flag t
401 "*Non-nil means to display info about the line under the mouse."
405 (defcustom speedbar-sort-tags nil
406 "*If Non-nil, sort tags in the speedbar display. *Obsolete*."
410 (defcustom speedbar-tag-hierarchy-method
411 '(prefix-group trim-words)
412 "*List of methods which speedbar will use to organize tags into groups.
413 Groups are defined as expandable meta-tags. Imenu supports such
414 things in some languages, such as separating variables from functions.
415 Available methods are:
416 sort - Sort tags. (sometimes unnecessary)
417 trim-words - Trim all tags by a common prefix, broken @ word sections.
418 prefix-group - Try to guess groups by prefix.
419 simple-group - If imenu already returned some meta groups, stick all
420 tags that are not in a group into a sub-group."
424 (const :tag "Sort the tags." sort)
425 (const :tag "Trim words to common prefix." trim-words)
426 (const :tag "Create groups from common prefixes." prefix-group)
427 (const :tag "Group loose tags into their own group." simple-group))
430 (defcustom speedbar-tag-split-minimum-length 20
431 "*Minimum length before we stop trying to create sub-lists in tags.
432 This is used by all tag-hierarchy methods that break large lists into
437 (defcustom speedbar-tag-regroup-maximum-length 10
438 "*Maximum length of submenus that are regrouped.
439 If the regrouping option is used, then if two or more short subgroups
440 are next to each other, then they are combined until this number of
445 (defcustom speedbar-activity-change-focus-flag nil
446 "*Non-nil means the selected frame will change based on activity.
447 Thus, if a file is selected for edit, the buffer will appear in the
448 selected frame and the focus will change to that frame."
452 (defcustom speedbar-directory-button-trim-method 'span
453 "*Indicates how the directory button will be displayed.
455 'span - span large directories over multiple lines.
456 'trim - trim large directories to only show the last few.
459 :type '(radio (const :tag "Span large directories over mutiple lines."
461 (const :tag "Trim large directories to only show the last few."
463 (const :tag "No trimming." nil)))
465 (defcustom speedbar-smart-directory-expand-flag t
466 "*Non-nil means speedbar should use smart expansion.
467 Smart expansion only affects when speedbar wants to display a
468 directory for a file in the attached frame. When smart expansion is
469 enabled, new directories which are children of a displayed directory
470 are expanded in the current framework. If nil, then the current
471 hierarchy would be replaced with the new directory."
475 (defvar speedbar-hide-button-brackets-flag nil
476 "*Non-nil means speedbar will hide the brackets around the + or -.")
478 (defcustom speedbar-before-popup-hook nil
479 "*Hooks called before popping up the speedbar frame."
483 (defcustom speedbar-before-delete-hook nil
484 "*Hooks called before deleting the speedbar frame."
488 (defcustom speedbar-mode-hook nil
489 "*Hooks called after creating a speedbar buffer."
493 (defcustom speedbar-timer-hook nil
494 "*Hooks called after running the speedbar timer function."
498 (defcustom speedbar-verbosity-level 1
499 "*Verbosity level of the speedbar. 0 means say nothing.
500 1 means medium level verbosity. 2 and higher are higher levels of
505 (defvar speedbar-indicator-separator " "
506 "String separating file text from indicator characters.")
508 (defcustom speedbar-vc-do-check t
509 "*Non-nil check all files in speedbar to see if they have been checked out.
510 Any file checked out is marked with `speedbar-vc-indicator'"
514 (defvar speedbar-vc-indicator "*"
515 "Text used to mark files which are currently checked out.
516 Currently only RCS is supported. Other version control systems can be
517 added by examining the function `speedbar-this-file-in-vc' and
518 `speedbar-vc-check-dir-p'")
520 (defcustom speedbar-vc-path-enable-hook nil
521 "*Return non-nil if the current path should be checked for Version Control.
522 Functions in this hook must accept one parameter which is the path
527 (defcustom speedbar-vc-in-control-hook nil
528 "*Return non-nil if the specified file is under Version Control.
529 Functions in this hook must accept two parameters. The PATH of the
530 current file, and the FILENAME of the file being checked."
534 (defvar speedbar-vc-to-do-point nil
535 "Local variable maintaining the current version control check position.")
537 (defcustom speedbar-obj-do-check t
538 "*Non-nil check all files in speedbar to see if they have an object file.
539 Any file checked out is marked with `speedbar-obj-indicator', and the
540 marking is based on `speedbar-obj-alist'"
544 (defvar speedbar-obj-to-do-point nil
545 "Local variable maintaining the current version control check position.")
547 (defvar speedbar-obj-indicator '("#" . "!")
548 "Text used to mark files that have a corresponding hidden object file.
549 The car is for an up-to-date object. The cdr is for an out of date object.
550 The expression `speedbar-obj-alist' defines who gets tagged.")
552 (defvar speedbar-obj-alist
553 '(("\\.\\([cpC]\\|cpp\\|cc\\)$" . ".o")
555 ("\\.java$" . ".class")
556 ("\\.f\\(or\\|90\\|77\\)?$" . ".o")
558 ("\\.texi$" . ".info"))
559 "Alist of file extensions, and their corresponding object file type.")
561 (defvar speedbar-indicator-regex
562 (concat (regexp-quote speedbar-indicator-separator)
564 (regexp-quote speedbar-vc-indicator)
566 (regexp-quote (car speedbar-obj-indicator))
568 (regexp-quote (cdr speedbar-obj-indicator))
570 "Regular expression used when identifying files.
571 Permits stripping of indicator characters from a line.")
573 (defcustom speedbar-scanner-reset-hook nil
574 "*Hook called whenever generic scanners are reset.
575 Set this to implement your own scanning / rescan safe functions with
580 (defvar speedbar-ignored-modes nil
581 "*List of major modes which speedbar will not switch directories for.")
583 (defun speedbar-extension-list-to-regex (extlist)
584 "Takes EXTLIST, a list of extensions and transforms it into regexp.
585 All the preceding `.' are stripped for an optimized expression starting
586 with `.' followed by extensions, followed by full-filenames."
587 (let ((regex1 nil) (regex2 nil))
589 (if (= (string-to-char (car extlist)) ?.)
590 (setq regex1 (concat regex1 (if regex1 "\\|" "")
591 (substring (car extlist) 1)))
592 (setq regex2 (concat regex2 (if regex2 "\\|" "") (car extlist))))
593 (setq extlist (cdr extlist)))
594 ;; concat all the sub-exressions together, making sure all types
595 ;; of parts exist during concatination.
597 (if regex1 (concat "\\(\\.\\(" regex1 "\\)\\)") "")
598 (if (and regex1 regex2) "\\|" "")
599 (if regex2 (concat "\\(" regex2 "\\)") "")
602 (defvar speedbar-ignored-path-regexp nil
603 "Regular expression matching paths speedbar will not switch to.
604 Created from `speedbar-ignored-path-expressions' with the function
605 `speedbar-extension-list-to-regex' (A misnamed function in this case.)
606 Use the function `speedbar-add-ignored-path-regexp', or customize the
607 variable `speedbar-ignored-path-expressions' to modify this variable.")
609 (defcustom speedbar-ignored-path-expressions
611 "*List of regular expressions matching directories speedbar will ignore.
612 They should included paths to directories which are notoriously very
613 large and take a long time to load in. Use the function
614 `speedbar-add-ignored-path-regexp' to add new items to this list after
615 speedbar is loaded. You may place anything you like in this list
616 before speedbar has been loaded."
618 :type '(repeat (regexp :tag "Path Regexp"))
619 :set (lambda (sym val)
620 (setq speedbar-ignored-path-expressions val
621 speedbar-ignored-path-regexp
622 (speedbar-extension-list-to-regex val))))
624 (defcustom speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\)\\'"
625 "*Regular expression matching directories not to show in speedbar.
626 They should include commonly existing directories which are not
627 useful, such as version control."
631 (defvar speedbar-file-unshown-regexp
632 (let ((nstr "") (noext completion-ignored-extensions))
634 (setq nstr (concat nstr (regexp-quote (car noext)) "\\'"
635 (if (cdr noext) "\\|" ""))
637 ;; backup refdir lockfile
638 (concat nstr "\\|#[^#]+#$\\|\\.\\.?\\'\\|\\.#"))
639 "*Regexp matching files we don't want displayed in a speedbar buffer.
640 It is generated from the variable `completion-ignored-extensions'")
642 ;; this is dangerous to customize, because the defaults will probably
643 ;; change in the future.
644 (defcustom speedbar-supported-extension-expressions
645 (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
646 ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".f\\(90\\|77\\|or\\)?")
647 (if speedbar-use-imenu-flag
648 '(".ada" ".pl" ".tcl" ".m" ".scm" ".pm" ".py"
649 ;; html is not supported by default, but an imenu tags package
650 ;; is available. Also, html files are nice to be able to see.
652 "Makefile\\(\\.in\\)?")))
653 "*List of regular expressions which will match files supported by tagging.
654 Do not prefix the `.' char with a double \\ to quote it, as the period
655 will be stripped by a simplified optimizer when compiled into a
656 singular expression. This variable will be turned into
657 `speedbar-file-regexp' for use with speedbar. You should use the
658 function `speedbar-add-supported-extension' to add a new extension at
659 runtime, or use the configuration dialog to set it in your .emacs
662 :type '(repeat (regexp :tag "Extension Regexp"))
663 :set (lambda (sym val)
664 (setq speedbar-supported-extension-expressions val
665 speedbar-file-regexp (speedbar-extension-list-to-regex val)))
668 (defvar speedbar-file-regexp
669 (speedbar-extension-list-to-regex speedbar-supported-extension-expressions)
670 "Regular expression matching files we know how to expand.
671 Created from `speedbar-supported-extension-expression' with the
672 function `speedbar-extension-list-to-regex'")
674 (defun speedbar-add-supported-extension (extension)
675 "Add EXTENSION as a new supported extension for speedbar tagging.
676 This should start with a `.' if it is not a complete file name, and
677 the dot should NOT be quoted in with \\. Other regular expression
678 matchers are allowed however. EXTENSION may be a single string or a
680 (interactive "sExtionsion: ")
681 (if (not (listp extension)) (setq extension (list extension)))
683 (if (member (car extension) speedbar-supported-extension-expressions)
685 (setq speedbar-supported-extension-expressions
686 (cons (car extension) speedbar-supported-extension-expressions)))
687 (setq extension (cdr extension)))
688 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
689 speedbar-supported-extension-expressions)))
691 (defun speedbar-add-ignored-path-regexp (path-expression)
692 "Add PATH-EXPRESSION as a new ignored path for speedbar tracking.
693 This function will modify `speedbar-ignored-path-regexp' and add
694 PATH-EXPRESSION to `speedbar-ignored-path-expressions'."
695 (interactive "sPath regex: ")
696 (if (not (listp path-expression))
697 (setq path-expression (list path-expression)))
698 (while path-expression
699 (if (member (car path-expression) speedbar-ignored-path-expressions)
701 (setq speedbar-ignored-path-expressions
702 (cons (car path-expression) speedbar-ignored-path-expressions)))
703 (setq path-expression (cdr path-expression)))
704 (setq speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
705 speedbar-ignored-path-expressions)))
707 ;; If we don't have custom, then we set it here by hand.
708 (if (not (fboundp 'custom-declare-variable))
709 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
710 speedbar-supported-extension-expressions)
711 speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
712 speedbar-ignored-path-expressions)))
714 (defvar speedbar-update-flag (and
715 (or (fboundp 'run-with-idle-timer)
716 (fboundp 'start-itimer)
717 (boundp 'post-command-idle-hook))
719 "*Non-nil means to automatically update the display.
720 When this is nil then speedbar will not follow the attached frame's path.
721 When speedbar is active, use:
723 \\<speedbar-key-map> `\\[speedbar-toggle-updates]'
725 to toggle this value.")
727 (defvar speedbar-syntax-table nil
728 "Syntax-table used on the speedbar.")
730 (if speedbar-syntax-table
732 (setq speedbar-syntax-table (make-syntax-table))
733 ;; turn off paren matching around here.
734 (modify-syntax-entry ?\' " " speedbar-syntax-table)
735 (modify-syntax-entry ?\" " " speedbar-syntax-table)
736 (modify-syntax-entry ?( " " speedbar-syntax-table)
737 (modify-syntax-entry ?) " " speedbar-syntax-table)
738 (modify-syntax-entry ?[ " " speedbar-syntax-table)
739 (modify-syntax-entry ?] " " speedbar-syntax-table))
741 (defvar speedbar-key-map nil
742 "Keymap used in speedbar buffer.")
746 (setq speedbar-key-map (make-keymap))
747 (suppress-keymap speedbar-key-map t)
750 (define-key speedbar-key-map "g" 'speedbar-refresh)
751 (define-key speedbar-key-map "t" 'speedbar-toggle-updates)
752 (define-key speedbar-key-map "q" 'speedbar-close-frame)
753 (define-key speedbar-key-map "Q" 'delete c-frame)
756 (define-key speedbar-key-map "n" 'speedbar-next)
757 (define-key speedbar-key-map "p" 'speedbar-prev)
758 (define-key speedbar-key-map "\M-n" 'speedbar-restricted-next)
759 (define-key speedbar-key-map "\M-p" 'speedbar-restricted-prev)
760 (define-key speedbar-key-map "\C-\M-n" 'speedbar-forward-list)
761 (define-key speedbar-key-map "\C-\M-p" 'speedbar-backward-list)
762 (define-key speedbar-key-map " " 'speedbar-scroll-up)
763 (define-key speedbar-key-map [delete] 'speedbar-scroll-down)
765 ;; Short cuts I happen to find useful
766 (define-key speedbar-key-map "r"
767 (lambda () (interactive)
768 (speedbar-change-initial-expansion-list
769 speedbar-previously-used-expansion-list-name)))
770 (define-key speedbar-key-map "b"
771 (lambda () (interactive)
772 (speedbar-change-initial-expansion-list "quick buffers")))
773 (define-key speedbar-key-map "f"
774 (lambda () (interactive)
775 (speedbar-change-initial-expansion-list "files")))
778 (substitute-key-definition 'switch-to-buffer
779 'speedbar-switch-buffer-attached-frame
780 speedbar-key-map global-map)
784 ;; mouse bindings so we can manipulate the items on each line
785 (define-key speedbar-key-map 'button2 'speedbar-click)
786 (define-key speedbar-key-map '(shift button2) 'speedbar-power-click)
787 ;; Info doc fix from Bob Weiner
788 (if (featurep 'infodoc)
790 (define-key speedbar-key-map 'button3 'speedbar-xemacs-popup-kludge))
791 (define-key speedbar-key-map '(meta button3) 'speedbar-mouse-item-info)
794 ;; mouse bindings so we can manipulate the items on each line
795 (define-key speedbar-key-map [down-mouse-1] 'speedbar-double-click)
796 (define-key speedbar-key-map [mouse-2] 'speedbar-click)
797 ;; This is the power click for new frames, or refreshing a cache
798 (define-key speedbar-key-map [S-mouse-2] 'speedbar-power-click)
799 ;; This adds a small unecessary visual effect
800 ;;(define-key speedbar-key-map [down-mouse-2] 'speedbar-quick-mouse)
801 (define-key speedbar-key-map [M-mouse-2] 'speedbar-mouse-item-info)
803 (define-key speedbar-key-map [down-mouse-3] 'speedbar-emacs-popup-kludge)
805 ;; This lets the user scroll as if we had a scrollbar... well maybe not
806 (define-key speedbar-key-map [mode-line mouse-2] 'speedbar-mouse-hscroll)
807 ;; another handy place users might click to get our menu.
808 (define-key speedbar-key-map [mode-line down-mouse-1]
809 'speedbar-emacs-popup-kludge)
811 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
812 (define-key speedbar-key-map [C-down-mouse-1] 'speedbar-hack-buffer-menu)
814 ;; Lastly, we want to track the mouse. Play here
815 (define-key speedbar-key-map [mouse-movement] 'speedbar-track-mouse)
818 (defun speedbar-make-specialized-keymap ()
819 "Create a keymap for use w/ a speedbar major or minor display mode.
820 This basically creates a sparse keymap, and makes it's parent be
822 (let ((k (make-sparse-keymap)))
823 (set-keymap-parent k speedbar-key-map)
826 (defvar speedbar-file-key-map nil
827 "Keymap used in speedbar buffer while files are displayed.")
829 (if speedbar-file-key-map
831 (setq speedbar-file-key-map (speedbar-make-specialized-keymap))
833 ;; Basic tree features
834 (define-key speedbar-file-key-map "e" 'speedbar-edit-line)
835 (define-key speedbar-file-key-map "\C-m" 'speedbar-edit-line)
836 (define-key speedbar-file-key-map "+" 'speedbar-expand-line)
837 (define-key speedbar-file-key-map "-" 'speedbar-contract-line)
839 ;; file based commands
840 (define-key speedbar-file-key-map "U" 'speedbar-up-directory)
841 (define-key speedbar-file-key-map "I" 'speedbar-item-info)
842 (define-key speedbar-file-key-map "B" 'speedbar-item-byte-compile)
843 (define-key speedbar-file-key-map "L" 'speedbar-item-load)
844 (define-key speedbar-file-key-map "C" 'speedbar-item-copy)
845 (define-key speedbar-file-key-map "D" 'speedbar-item-delete)
846 (define-key speedbar-file-key-map "O" 'speedbar-item-object-delete)
847 (define-key speedbar-file-key-map "R" 'speedbar-item-rename)
850 (defvar speedbar-easymenu-definition-base
852 ["Update" speedbar-refresh t]
853 ["Auto Update" speedbar-toggle-updates
854 :style toggle :selected speedbar-update-flag]
856 "Base part of the speedbar menu.")
858 (defvar speedbar-easymenu-definition-special
859 '(["Edit Item On Line" speedbar-edit-line t]
860 ["Show All Files" speedbar-toggle-show-all-files
861 :style toggle :selected speedbar-show-unknown-files]
862 ["Expand File Tags" speedbar-expand-line
863 (save-excursion (beginning-of-line)
864 (looking-at "[0-9]+: *.\\+. "))]
865 ["Contract File Tags" speedbar-contract-line
866 (save-excursion (beginning-of-line)
867 (looking-at "[0-9]+: *.-. "))]
868 ; ["Sort Tags" speedbar-toggle-sorting
869 ; :style toggle :selected speedbar-sort-tags]
871 ["File/Tag Information" speedbar-item-info t]
872 ["Load Lisp File" speedbar-item-load
875 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
876 ["Byte Compile File" speedbar-item-byte-compile
879 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
880 ["Copy File" speedbar-item-copy
881 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *\\["))]
882 ["Rename File" speedbar-item-rename
883 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
884 ["Delete File" speedbar-item-delete
885 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
886 ["Delete Object" speedbar-item-object-delete
887 (save-excursion (beginning-of-line)
888 (looking-at "[0-9]+: *\\[[+-]\\] [^ \n]+ \\*?[!#]$"))]
890 "Additional menu items while in file-mode.")
892 (defvar speedbar-easymenu-definition-trailer
894 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
895 ["Customize..." speedbar-customize t])
896 ["Close" speedbar-close-frame t]
897 ["Quit" delete-frame t] )
898 "Menu items appearing at the end of the speedbar menu.")
900 (defvar speedbar-desired-buffer nil
901 "Non-nil when speedbar is showing buttons specific a special mode.
902 In this case it is the originating buffer.")
903 (defvar speedbar-buffer nil
904 "The buffer displaying the speedbar.")
905 (defvar speedbar-frame nil
906 "The frame displaying speedbar.")
907 (defvar speedbar-cached-frame nil
908 "The frame that was last created, then removed from the display.")
909 (defvar speedbar-full-text-cache nil
910 "The last open directory is saved in its entirety for ultra-fast switching.")
911 (defvar speedbar-timer nil
912 "The speedbar timer used for updating the buffer.")
913 (defvar speedbar-attached-frame nil
914 "The frame which started speedbar mode.
915 This is the frame from which all data displayed in the speedbar is
916 gathered, and in which files and such are displayed.")
918 (defvar speedbar-last-selected-file nil
919 "The last file which was selected in speedbar buffer.")
921 (defvar speedbar-shown-directories nil
922 "Maintain list of directories simultaneously open in the current speedbar.")
924 (defvar speedbar-directory-contents-alist nil
925 "An association list of directories and their contents.
926 Each sublist was returned by `speedbar-file-lists'. This list is
927 maintained to speed up the refresh rate when switching between
930 (defvar speedbar-power-click nil
931 "Never set this by hand. Value is t when S-mouse activity occurs.")
934 ;;; Mode definitions/ user commands
938 (defalias 'speedbar 'speedbar-frame-mode)
940 (defun speedbar-frame-mode (&optional arg)
941 "Enable or disable speedbar. Positive ARG means turn on, negative turn off.
942 nil means toggle. Once the speedbar frame is activated, a buffer in
943 `speedbar-mode' will be displayed. Currently, only one speedbar is
945 `speedbar-before-popup-hook' is called before popping up the speedbar frame.
946 `speedbar-before-delete-hook' is called before the frame is deleted."
948 ;; toggle frame on and off.
949 (if (not arg) (if (and (frame-live-p speedbar-frame)
950 (frame-visible-p speedbar-frame))
951 (setq arg -1) (setq arg 1)))
952 ;; turn the frame off on neg number
953 (if (and (numberp arg) (< arg 0))
955 (run-hooks 'speedbar-before-delete-hook)
956 (if (and speedbar-frame (frame-live-p speedbar-frame))
958 (setq speedbar-cached-frame speedbar-frame)
959 (make-frame-invisible speedbar-frame)))
960 (setq speedbar-frame nil)
961 (speedbar-set-timer nil)
962 ;; Used to delete the buffer. This has the annoying affect of
963 ;; preventing whatever took its place from ever appearing
964 ;; as the default after a C-x b was typed
965 ;;(if (bufferp speedbar-buffer)
966 ;; (kill-buffer speedbar-buffer))
968 ;; Set this as our currently attached frame
969 (setq speedbar-attached-frame (selected-frame))
970 (run-hooks 'speedbar-before-popup-hook)
971 ;; Get the frame to work in
972 (if (frame-live-p speedbar-cached-frame)
974 (setq speedbar-frame speedbar-cached-frame)
975 (make-frame-visible speedbar-frame)
976 ;; Get the buffer to play with
978 (select-frame speedbar-frame)
979 (if (not (eq (current-buffer) speedbar-buffer))
980 (switch-to-buffer speedbar-buffer))
981 (set-window-dedicated-p (selected-window) t)
982 (raise-frame speedbar-frame)
983 (speedbar-set-timer speedbar-update-speed)
985 (if (frame-live-p speedbar-frame)
986 (raise-frame speedbar-frame)
989 (make-frame (nconc (list 'height
990 (speedbar-needed-height))
991 speedbar-frame-plist))
992 (let* ((mh (frame-parameter nil 'menu-bar-lines))
993 (cfx (frame-parameter nil 'left))
994 (cfy (frame-parameter nil 'top))
995 (cfw (frame-pixel-width))
998 speedbar-frame-parameters
999 (list (cons 'height (+ mh (frame-height))))))
1001 (if (or (< emacs-major-version 20)
1002 (not (eq window-system 'x)))
1004 (let ((x-pointer-shape x-pointer-top-left-arrow)
1005 (x-sensitive-text-pointer-shape
1007 (make-frame params)))))
1008 (set-frame-position frame
1009 ;; Decide which side to put it
1010 ;; on. 200 is just a buffer
1011 ;; for the left edge of the
1012 ;; screen. The extra 10 is just
1013 ;; dressings for window decorations.
1016 (- cfx (frame-pixel-width frame) 10))
1019 ;; reset the selection variable
1020 (setq speedbar-last-selected-file nil)
1021 ;; Put the buffer into the frame
1022 (save-window-excursion
1023 ;; Get the buffer to play with
1025 (select-frame speedbar-frame)
1026 (switch-to-buffer speedbar-buffer)
1027 (set-window-dedicated-p (selected-window) t))
1028 (if (or (null window-system) (eq window-system 'pc))
1030 (select-frame speedbar-frame)
1031 (set-frame-name "Speedbar")))
1032 (speedbar-set-timer speedbar-update-speed)))))
1035 (defun speedbar-get-focus ()
1036 "Change frame focus to or from the speedbar frame.
1037 If the selected frame is not speedbar, then speedbar frame is
1038 selected. If the speedbar frame is active, then select the attached frame."
1040 (if (eq (selected-frame) speedbar-frame)
1041 (if (frame-live-p speedbar-attached-frame)
1042 (select-frame speedbar-attached-frame))
1043 ;; If updates are off, then refresh the frame (they want it now...)
1044 (if (not speedbar-update-flag)
1045 (let ((speedbar-update-flag t))
1046 (speedbar-timer-fn)))
1047 ;; make sure we have a frame
1048 (if (not (frame-live-p speedbar-frame)) (speedbar-frame-mode 1))
1050 (select-frame speedbar-frame)
1054 (defun speedbar-close-frame ()
1055 "Turn off a currently active speedbar."
1057 (speedbar-frame-mode -1)
1058 (select-frame speedbar-attached-frame)
1061 (defun speedbar-switch-buffer-attached-frame (&optional buffer)
1062 "Switch to BUFFER in speedbar's attached frame, and raise that frame.
1063 This overrides the default behavior of `switch-to-buffer' which is
1064 broken because of the dedicated speedbar frame."
1066 ;; Assume we are in the speedbar frame.
1067 (speedbar-get-focus)
1068 ;; Now switch buffers
1070 (switch-to-buffer buffer)
1071 (call-interactively 'switch-to-buffer nil nil)))
1073 (defmacro speedbar-frame-width ()
1074 "Return the width of the speedbar frame in characters.
1075 nil if it doesn't exist."
1076 '(frame-width speedbar-frame))
1078 ;; XEmacs function only.
1079 (defun speedbar-needed-height (&optional frame)
1080 "The needed height for the tool bar FRAME (in characters)."
1081 (or frame (setq frame (selected-frame)))
1082 ;; The 1 is the missing modeline/minibuffer
1083 (+ 1 (/ (frame-pixel-height frame)
1084 (face-height 'default frame))))
1086 (defun speedbar-mode ()
1087 "Major mode for managing a display of directories and tags.
1088 \\<speedbar-key-map>
1089 The first line represents the default path of the speedbar frame.
1090 Each directory segment is a button which jumps speedbar's default
1091 directory to that path. Buttons are activated by clicking `\\[speedbar-click]'.
1092 In some situations using `\\[speedbar-power-click]' is a `power click' which will
1093 rescan cached items, or pop up new frames.
1095 Each line starting with <+> represents a directory. Click on the <+>
1096 to insert the directory listing into the current tree. Click on the
1097 <-> to retract that list. Click on the directory name to go to that
1098 directory as the default.
1100 Each line starting with [+] is a file. If the variable
1101 `speedbar-show-unknown-files' is t, the lines starting with [?] are
1102 files which don't have imenu support, but are not expressly ignored.
1103 Files are completely ignored if they match `speedbar-file-unshown-regexp'
1104 which is generated from `completion-ignored-extensions'.
1106 Files with a `*' character after their name are files checked out of a
1107 version control system. (currently only RCS is supported.) New
1108 version control systems can be added by examining the documentation
1109 for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'
1111 Files with a `#' or `!' character after them are source files that
1112 have an object file associated with them. The `!' indicates that the
1113 files is out of date. You can control what source/object associations
1114 exist through the variable `speedbar-obj-alist'.
1116 Click on the [+] to display a list of tags from that file. Click on
1117 the [-] to retract the list. Click on the file name to edit the file
1118 in the attached frame.
1120 If you open tags, you might find a node starting with {+}, which is a
1121 category of tags. Click the {+} to expand the category. Jump-able
1122 tags start with >. Click the name of the tag to go to that position
1123 in the selected file.
1125 \\{speedbar-key-map}"
1128 (setq speedbar-buffer (set-buffer (get-buffer-create " SPEEDBAR")))
1129 (kill-all-local-variables)
1130 (setq major-mode 'speedbar-mode)
1131 (setq mode-name "Speedbar")
1132 (set-syntax-table speedbar-syntax-table)
1133 (setq font-lock-keywords nil) ;; no font-locking please
1134 (setq truncate-lines t)
1135 (make-local-variable 'frame-title-format)
1136 (setq frame-title-format "Speedbar")
1137 ;; Set this up special just for the speedbar buffer
1138 ;; Terminal minibuffer stuff does not require this.
1139 (if (and window-system (not (eq window-system 'pc))
1140 (null default-minibuffer-frame))
1142 (make-local-variable 'default-minibuffer-frame)
1143 (setq default-minibuffer-frame speedbar-attached-frame)))
1144 ;; Correct use of `temp-buffer-show-function': Bob Weiner
1145 (if (and (boundp 'temp-buffer-show-hook)
1146 (boundp 'temp-buffer-show-function))
1147 (progn (make-local-variable 'temp-buffer-show-hook)
1148 (setq temp-buffer-show-hook temp-buffer-show-function)))
1149 (make-local-variable 'temp-buffer-show-function)
1150 (setq temp-buffer-show-function 'speedbar-temp-buffer-show-function)
1151 (if speedbar-xemacsp
1153 ;; Argh! mouse-track-click-hook doesn't understand the
1154 ;; make-local-hook conventions.
1155 (make-local-variable 'mouse-track-click-hook)
1156 (add-hook 'mouse-track-click-hook
1157 (lambda (event count)
1158 (if (/= (event-button event) 1)
1159 nil ; Do normal operations.
1161 (speedbar-quick-mouse event))
1164 (mouse-set-point event)
1165 (speedbar-do-function-pointer)
1166 (speedbar-quick-mouse event)))
1167 ;; Don't do normal operations.
1169 (make-local-hook 'kill-buffer-hook)
1170 (add-hook 'kill-buffer-hook (lambda () (let ((skilling (boundp 'skilling)))
1173 (if (eq (current-buffer)
1175 (speedbar-frame-mode -1)))))
1177 (speedbar-set-mode-line-format)
1178 (if speedbar-xemacsp
1180 (make-local-variable 'mouse-motion-handler)
1181 (setq mouse-motion-handler 'speedbar-track-mouse-xemacs))
1182 (if speedbar-track-mouse-flag
1184 (make-local-variable 'track-mouse)
1185 (setq track-mouse t))) ;this could be messy.
1186 (setq auto-show-mode nil)) ;no auto-show for Emacs
1187 (run-hooks 'speedbar-mode-hook))
1188 (speedbar-update-contents)
1191 (defun speedbar-show-info-under-mouse (&optional event)
1192 "Call the info function for the line under the mouse.
1193 Optional EVENT is currently not used."
1194 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1195 (if (equal (car pos) speedbar-frame)
1197 (save-window-excursion
1198 (apply 'set-mouse-position pos)
1199 (speedbar-item-info))))))
1201 (defun speedbar-set-mode-line-format ()
1202 "Set the format of the mode line based on the current speedbar environment.
1203 This gives visual indications of what is up. It EXPECTS the speedbar
1204 frame and window to be the currently active frame and window."
1205 (if (and (frame-live-p speedbar-frame)
1206 (or (not speedbar-xemacsp)
1207 (specifier-instance has-modeline-p)))
1209 (set-buffer speedbar-buffer)
1210 (let* ((w (or (speedbar-frame-width) 20))
1213 (p3 (if speedbar-update-flag "SPEEDBAR" "SLOWBAR"))
1214 (blank (- w (length p1) (length p3) (length p5)
1215 (if line-number-mode 4 0)))
1217 (make-string (/ blank 2) ? )
1220 (make-string (+ (/ blank 2) (% blank 2)) ? )
1223 (if line-number-mode
1224 (list (concat p1 p2 p3) '(line-number-mode " %3l")
1226 (list (concat p1 p2 p3 p4 p5)))))
1227 (if (not (equal mode-line-format tf))
1229 (setq mode-line-format tf)
1230 (force-mode-line-update)))))))
1232 (defun speedbar-temp-buffer-show-function (buffer)
1233 "Placed in the variable `temp-buffer-show-function' in `speedbar-mode'.
1234 If a user requests help using \\[help-command] <Key> the temp BUFFER will be
1235 redirected into a window on the attached frame."
1236 (if speedbar-attached-frame (select-frame speedbar-attached-frame))
1237 (pop-to-buffer buffer nil)
1239 ;; Fix for using this hook: Bob Weiner
1240 (cond ((fboundp 'run-hook-with-args)
1241 (run-hook-with-args 'temp-buffer-show-hook buffer))
1242 ((and (boundp 'temp-buffer-show-hook)
1243 (listp temp-buffer-show-hook))
1244 (mapcar (function (lambda (hook) (funcall hook buffer)))
1245 temp-buffer-show-hook))))
1247 (defvar speedbar-previous-menu nil
1248 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1250 (defun speedbar-reconfigure-keymaps ()
1251 "Reconfigure the menu-bar in a speedbar frame.
1252 Different menu items are displayed depending on the current display mode
1253 and the existence of packages."
1255 speedbar-easymenu-definition-base
1256 (if speedbar-shown-directories
1257 ;; file display mode version
1258 (speedbar-initial-menu)
1260 (select-frame speedbar-attached-frame)
1261 (if (local-variable-p
1262 'speedbar-easymenu-definition-special
1264 ;; If bound locally, we can use it
1265 speedbar-easymenu-definition-special)))
1266 ;; Dynamic menu stuff
1268 (list (cons "Displays"
1269 (let ((displays nil)
1270 (alist speedbar-initial-expansion-mode-alist))
1275 (capitalize (car (car alist)))
1277 'speedbar-change-initial-expansion-list
1281 (setq alist (cdr alist)))
1284 speedbar-easymenu-definition-trailer))
1285 (localmap (save-excursion
1286 (let ((cf (selected-frame)))
1288 (select-frame speedbar-attached-frame)
1289 (if (local-variable-p
1290 'speedbar-special-mode-key-map
1292 speedbar-special-mode-key-map)
1293 (select-frame cf))))))
1295 (set-buffer speedbar-buffer)
1296 (use-local-map (or localmap
1297 (speedbar-initial-keymap)
1298 ;; This creates a small keymap we can glom the
1299 ;; menu adjustments into.
1300 (speedbar-make-specialized-keymap)))
1301 ;; Delete the old menu if applicable.
1302 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1303 (setq speedbar-previous-menu md)
1304 ;; Now add the new menu
1305 (if (not speedbar-xemacsp)
1306 (easy-menu-define speedbar-menu-map (current-local-map)
1308 (easy-menu-add md (current-local-map))
1309 (set-buffer-menubar (list md))))))
1312 ;;; User Input stuff
1315 ;; XEmacs: this can be implemented using modeline keymaps, but there
1316 ;; is no use, as we have horizontal scrollbar (as the docstring
1318 (defun speedbar-mouse-hscroll (e)
1319 "Read a mouse event E from the mode line, and horizontally scroll.
1320 If the mouse is being clicked on the far left, or far right of the
1321 mode-line. This is only useful for non-XEmacs"
1323 (let* ((xp (car (nth 2 (car (cdr e)))))
1324 (cpw (/ (frame-pixel-width)
1326 (oc (1+ (/ xp cpw)))
1330 ((> oc (- (window-width) 3))
1332 (t (message "Click on the edge of the modeline to scroll left/right")))
1333 ;;(message "X: Pixel %d Char Pixels %d On char %d" xp cpw oc)
1336 (defun speedbar-customize ()
1337 "Customize speedbar using the Custom package."
1339 (let ((sf (selected-frame)))
1340 (select-frame speedbar-attached-frame)
1341 (customize-group 'speedbar)
1343 (speedbar-maybee-jump-to-attached-frame))
1345 (defun speedbar-track-mouse (event)
1346 "For motion EVENT, display info about the current line."
1348 (if (not speedbar-track-mouse-flag)
1351 (let ((char (nth 1 (car (cdr event)))))
1352 (if (not (numberp char))
1355 ;; (message "%S" event)
1356 (speedbar-item-info)
1359 (defun speedbar-track-mouse-xemacs (event)
1360 "For motion EVENT, display info about the current line."
1361 (if (functionp (default-value 'mouse-motion-handler))
1362 (funcall (default-value 'mouse-motion-handler) event))
1363 (if speedbar-track-mouse-flag
1365 (save-window-excursion
1367 (progn (mouse-set-point event)
1368 ;; Prevent focus-related bugs.
1369 (if (eq major-mode 'speedbar-mode)
1370 (speedbar-item-info)))
1373 ;; In XEmacs, we make popup menus work on the item over mouse (as
1374 ;; opposed to where the point happens to be.) We attain this by
1375 ;; temporarily moving the point to that place.
1376 ;; Hrvoje Niksic <hniksic@srce.hr>
1377 (defun speedbar-xemacs-popup-kludge (event)
1378 "Pop up a menu related to the clicked on item.
1379 Must be bound to EVENT."
1381 (select-frame speedbar-frame)
1383 (goto-char (event-closest-point event))
1385 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
1386 (save-excursion (beginning-of-line) (point)))))
1388 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
1389 ;; menu functions) return immediately.
1391 (while (not (misc-user-event-p (setq new (next-event))))
1392 (dispatch-event new))
1393 (dispatch-event new))))
1395 (defun speedbar-emacs-popup-kludge (e)
1396 "Pop up a menu related to the clicked on item.
1397 Must be bound to event E."
1401 ;; This gets the cursor where the user can see it.
1402 (if (not (bolp)) (forward-char -1))
1404 (if (< emacs-major-version 20)
1405 (mouse-major-mode-menu e)
1406 (mouse-major-mode-menu e nil))))
1408 (defun speedbar-hack-buffer-menu (e)
1409 "Control mouse 1 is buffer menu.
1410 This hack overrides it so that the right thing happens in the main
1411 Emacs frame, not in the speedbar frame.
1412 Argument E is the event causing this activity."
1414 (let ((fn (lookup-key global-map (if speedbar-xemacsp
1420 (set-window-dedicated-p (selected-window) nil)
1421 (call-interactively fn)
1422 (setq newbuff (current-buffer)))
1423 (switch-to-buffer " SPEEDBAR")
1424 (set-window-dedicated-p (selected-window) t))
1425 (speedbar-with-attached-buffer
1426 (switch-to-buffer newbuff))))
1428 (defun speedbar-next (arg)
1429 "Move to the next ARGth line in a speedbar buffer."
1431 (forward-line (or arg 1))
1432 (speedbar-item-info)
1433 (speedbar-position-cursor-on-line))
1435 (defun speedbar-prev (arg)
1436 "Move to the previous ARGth line in a speedbar buffer."
1438 (speedbar-next (if arg (- arg) -1)))
1440 (defun speedbar-restricted-move (arg)
1441 "Move to the next ARGth line in a speedbar buffer at the same depth.
1442 This means that movement is restricted to a subnode, and that siblings
1443 of intermediate nodes are skipped."
1444 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1445 ;; First find the extent for which we are allowed to move.
1446 (let ((depth (save-excursion (beginning-of-line)
1447 (if (looking-at "[0-9]+:")
1448 (string-to-int (match-string 0))
1450 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1451 (lastmatch (point)))
1453 (forward-line (- crement))
1454 (let ((subdepth (save-excursion (beginning-of-line)
1455 (if (looking-at "[0-9]+:")
1456 (string-to-int (match-string 0))
1458 (cond ((or (< subdepth depth)
1459 (progn (end-of-line) (eobp))
1460 (progn (beginning-of-line) (bobp)))
1461 ;; We have reached the end of this block.
1462 (goto-char lastmatch)
1464 (error "End of sub-list"))
1466 (setq lastmatch (point)
1467 arg (+ arg crement))))))
1468 (speedbar-position-cursor-on-line)))
1470 (defun speedbar-restricted-next (arg)
1471 "Move to the next ARGth line in a speedbar buffer at the same depth.
1472 This means that movement is restricted to a subnode, and that siblings
1473 of intermediate nodes are skipped."
1475 (speedbar-restricted-move (or arg 1))
1476 (speedbar-item-info))
1479 (defun speedbar-restricted-prev (arg)
1480 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1481 This means that movement is restricted to a subnode, and that siblings
1482 of intermediate nodes are skipped."
1484 (speedbar-restricted-move (if arg (- arg) -1))
1485 (speedbar-item-info))
1487 (defun speedbar-navigate-list (arg)
1488 "Move across ARG groups of similarly typed items in speedbar.
1489 Stop on the first line of the next type of item, or on the last or first item
1490 if we reach a buffer boundary."
1493 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1494 (let ((str (regexp-quote (match-string 0))))
1495 (while (looking-at str)
1496 (speedbar-restricted-move arg)
1497 (beginning-of-line))))
1498 (speedbar-position-cursor-on-line))
1500 (defun speedbar-forward-list ()
1501 "Move forward over the current list.
1502 A LIST in speedbar is a group of similarly typed items, such as directories,
1503 files, or the directory button."
1505 (speedbar-navigate-list 1)
1506 (speedbar-item-info))
1508 (defun speedbar-backward-list ()
1509 "Move backward over the current list.
1510 A LIST in speedbar is a group of similarly typed items, such as directories,
1511 files, or the directory button."
1513 (speedbar-navigate-list -1)
1514 (speedbar-item-info))
1516 (defun speedbar-scroll-up (&optional arg)
1517 "Page down one screen-full of the speedbar, or ARG lines."
1520 (speedbar-position-cursor-on-line))
1522 (defun speedbar-scroll-down (&optional arg)
1523 "Page up one screen-full of the speedbar, or ARG lines."
1526 (speedbar-position-cursor-on-line))
1528 (defun speedbar-up-directory ()
1529 "Keyboard accelerator for moving the default directory up one.
1530 Assumes that the current buffer is the speedbar buffer"
1532 (setq default-directory (expand-file-name (concat default-directory "../")))
1533 (speedbar-update-contents))
1535 ;;; Speedbar file activity (aka creeping featurism)
1537 (defun speedbar-refresh ()
1538 "Refresh the current speedbar display, disposing of any cached data."
1540 (let ((dl speedbar-shown-directories)
1541 (dm (and (boundp 'deactivate-mark) deactivate-mark)))
1543 (adelete 'speedbar-directory-contents-alist (car dl))
1545 (if (<= 1 speedbar-verbosity-level)
1546 (message "Refreshing speedbar..."))
1547 (speedbar-update-contents)
1548 (speedbar-stealthy-updates)
1549 ;; Reset the timer in case it got really hosed for some reason...
1550 (speedbar-set-timer speedbar-update-speed)
1551 (if (<= 1 speedbar-verbosity-level)
1552 (message "Refreshing speedbar...done"))
1553 (if (boundp 'deactivate-mark) (setq deactivate-mark dm))))
1555 (defun speedbar-item-load ()
1556 "Load the item under the cursor or mouse if it is a Lisp file."
1558 (let ((f (speedbar-line-file)))
1559 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1560 (if (and (file-exists-p (concat f "c"))
1561 (y-or-n-p (format "Load %sc? " f)))
1562 ;; If the compiled version exists, load that instead...
1563 (load-file (concat f "c"))
1565 (error "Not a loadable file"))))
1567 (defun speedbar-item-byte-compile ()
1568 "Byte compile the item under the cursor or mouse if it is a Lisp file."
1570 (let ((f (speedbar-line-file))
1571 (sf (selected-frame)))
1572 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1574 (select-frame speedbar-attached-frame)
1575 (byte-compile-file f nil)
1577 (speedbar-reset-scanners)))
1580 (defun speedbar-mouse-item-info (event)
1581 "Provide information about what the user clicked on.
1582 This should be bound to a mouse EVENT."
1584 (mouse-set-point event)
1585 (speedbar-item-info))
1587 (defun speedbar-generic-item-info ()
1588 "Attempt to derive, and then display information about thils line item.
1589 File style information is displayed with `speedbar-item-info'."
1592 ;; Skip invisible number info.
1593 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1594 ;; Skip items in "folder" type text characters.
1595 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1597 (message "Text: %s" (buffer-substring-no-properties
1598 (point) (progn (end-of-line) (point))))))
1600 (defun speedbar-item-info ()
1601 "Display info in the mini-buffer about the button the mouse is over."
1603 (if (not speedbar-shown-directories)
1604 (speedbar-generic-item-info)
1605 (let* ((item (speedbar-line-file))
1606 (attr (if item (file-attributes item) nil)))
1607 (if (and item attr) (message "%s %-6d %s" (nth 8 attr) (nth 7 attr) item)
1610 (if (not (looking-at "\\([0-9]+\\):"))
1611 (speedbar-generic-item-info)
1612 (setq item (speedbar-line-path (string-to-int (match-string 1))))
1613 (if (re-search-forward "> \\([^ ]+\\)$"
1614 (save-excursion(end-of-line)(point)) t)
1616 (setq attr (get-text-property (match-beginning 1)
1618 (message "Tag: %s in %s @ %s"
1619 (match-string 1) item
1621 (if (markerp attr) (marker-position attr) attr)
1623 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1624 (save-excursion(end-of-line)(point)) t)
1625 (message "Group of tags \"%s\"" (match-string 1))
1626 (speedbar-generic-item-info)))))))))
1628 (defun speedbar-item-copy ()
1629 "Copy the item under the cursor.
1630 Files can be copied to new names or places."
1632 (let ((f (speedbar-line-file)))
1633 (if (not f) (error "Not a file"))
1634 (if (file-directory-p f)
1635 (error "Cannot copy directory")
1636 (let* ((rt (read-file-name (format "Copy %s to: "
1637 (file-name-nondirectory f))
1638 (file-name-directory f)))
1639 (refresh (member (expand-file-name (file-name-directory rt))
1640 speedbar-shown-directories)))
1641 ;; Create the right file name part
1642 (if (file-directory-p rt)
1644 (concat (expand-file-name rt)
1645 (if (string-match "/$" rt) "" "/")
1646 (file-name-nondirectory f))))
1647 (if (or (not (file-exists-p rt))
1648 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1650 (copy-file f rt t t)
1651 ;; refresh display if the new place is currently displayed.
1655 (if (not (speedbar-goto-this-file rt))
1656 (speedbar-goto-this-file f))))
1659 (defun speedbar-item-rename ()
1660 "Rename the item under the cursor or mouse.
1661 Files can be renamed to new names or moved to new directories."
1663 (let ((f (speedbar-line-file)))
1665 (let* ((rt (read-file-name (format "Rename %s to: "
1666 (file-name-nondirectory f))
1667 (file-name-directory f)))
1668 (refresh (member (expand-file-name (file-name-directory rt))
1669 speedbar-shown-directories)))
1670 ;; Create the right file name part
1671 (if (file-directory-p rt)
1673 (concat (expand-file-name rt)
1674 (if (string-match "/\\'" rt) "" "/")
1675 (file-name-nondirectory f))))
1676 (if (or (not (file-exists-p rt))
1677 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1679 (rename-file f rt t)
1680 ;; refresh display if the new place is currently displayed.
1684 (speedbar-goto-this-file rt)
1686 (error "Not a file"))))
1688 (defun speedbar-item-delete ()
1689 "Delete the item under the cursor. Files are removed from disk."
1691 (let ((f (speedbar-line-file)))
1692 (if (not f) (error "Not a file"))
1693 (if (y-or-n-p (format "Delete %s? " f))
1695 (if (file-directory-p f)
1696 (delete-directory f)
1698 (message "Okie dokie..")
1705 (defun speedbar-item-object-delete ()
1706 "Delete the object associated from the item under the cursor.
1707 The file is removed from disk. The object is determined from the
1708 variable `speedbar-obj-alist'."
1710 (let* ((f (speedbar-line-file))
1712 (oa speedbar-obj-alist))
1713 (if (not f) (error "Not a file"))
1714 (while (and oa (not (string-match (car (car oa)) f)))
1716 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1717 (if (and oa (file-exists-p obj)
1718 (y-or-n-p (format "Delete %s? " obj)))
1721 (speedbar-reset-scanners)))))
1723 (defun speedbar-enable-update ()
1724 "Enable automatic updating in speedbar via timers."
1726 (setq speedbar-update-flag t)
1727 (speedbar-set-mode-line-format)
1728 (speedbar-set-timer speedbar-update-speed))
1730 (defun speedbar-disable-update ()
1731 "Disable automatic updating and stop consuming resources."
1733 (setq speedbar-update-flag nil)
1734 (speedbar-set-mode-line-format)
1735 (speedbar-set-timer nil))
1737 (defun speedbar-toggle-updates ()
1738 "Toggle automatic update for the speedbar frame."
1740 (if speedbar-update-flag
1741 (speedbar-disable-update)
1742 (speedbar-enable-update)))
1744 (defun speedbar-toggle-sorting ()
1745 "Toggle automatic update for the speedbar frame."
1747 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1749 (defun speedbar-toggle-show-all-files ()
1750 "Toggle display of files speedbar can not tag."
1752 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1755 ;;; Utility functions
1757 (defun speedbar-set-timer (timeout)
1758 "Apply a timer with TIMEOUT, or remove a timer if TIMOUT is nil.
1759 TIMEOUT is the number of seconds until the speedbar timer is called
1760 again. When TIMEOUT is nil, turn off all timeouts.
1761 This function will also enable or disable the `vc-checkin-hook' used
1762 to track file check ins, and will change the mode line to match
1763 `speedbar-update-flag'."
1768 (progn (delete-itimer speedbar-timer)
1769 (setq speedbar-timer nil)))
1771 (if (and speedbar-xemacsp
1772 (or (>= emacs-major-version 20)
1773 (>= emacs-minor-version 15)))
1774 (setq speedbar-timer (start-itimer "speedbar"
1779 (setq speedbar-timer (start-itimer "speedbar"
1784 ((fboundp 'run-with-idle-timer)
1786 (progn (cancel-timer speedbar-timer)
1787 (setq speedbar-timer nil)))
1789 (setq speedbar-timer
1790 (run-with-idle-timer timeout t 'speedbar-timer-fn))))
1791 ;; Emacs 19.30 (Thanks twice: ptype@dra.hmg.gb)
1792 ((fboundp 'post-command-idle-hook)
1794 (add-hook 'post-command-idle-hook 'speedbar-timer-fn)
1795 (remove-hook 'post-command-idle-hook 'speedbar-timer-fn)))
1796 ;; Older or other Emacsen with no timers. Set up so that its
1797 ;; obvious this emacs can't handle the updates
1799 (setq speedbar-update-flag nil)))
1800 ;; Apply a revert hook that will reset the scanners. We attach to revert
1801 ;; because most reverts occur during VC state change, and this lets our
1802 ;; VC scanner fix itself.
1804 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
1805 (remove-hook 'after-revert-hook 'speedbar-reset-scanners)
1807 ;; change this if it changed for some reason
1808 (speedbar-set-mode-line-format))
1810 (defmacro speedbar-with-writable (&rest forms)
1811 "Allow the buffer to be writable and evaluate FORMS."
1812 (list 'let '((inhibit-read-only t))
1813 '(toggle-read-only -1)
1814 (cons 'progn forms)))
1815 (put 'speedbar-with-writable 'lisp-indent-function 0)
1817 (defun speedbar-select-window (buffer)
1818 "Select a window in which BUFFER is shown.
1819 If it is not shown, force it to appear in the default window."
1820 (let ((win (get-buffer-window buffer speedbar-attached-frame)))
1823 (set-window-buffer (selected-window) buffer))))
1825 (defmacro speedbar-with-attached-buffer (&rest forms)
1826 "Execute FORMS in the attached frame's special buffer.
1827 Optionally select that frame if necessary."
1828 ;; Reset the timer with a new timeout when cliking a file
1829 ;; in case the user was navigating directories, we can cancel
1830 ;; that other timer.
1833 '(speedbar-set-timer speedbar-update-speed)
1835 'let '((cf (selected-frame)))
1836 '(select-frame speedbar-attached-frame)
1837 '(speedbar-select-window speedbar-desired-buffer)
1840 '(speedbar-maybee-jump-to-attached-frame)
1843 (defun speedbar-insert-button (text face mouse function
1844 &optional token prevline)
1845 "Insert TEXT as the next logical speedbar button.
1846 FACE is the face to put on the button, MOUSE is the highlight face to use.
1847 When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
1848 This function assumes that the current buffer is the speedbar buffer.
1849 If PREVLINE, then put this button on the previous line.
1851 This is a convenience function for special mode that create their own
1852 specialized speedbar displays."
1853 (goto-char (point-max))
1854 (if (/= (current-column) 0) (insert "\n"))
1855 (if prevline (progn (delete-char -1) (insert " "))) ;back up if desired...
1856 (let ((start (point)))
1858 (speedbar-make-button start (point) face mouse function token))
1859 (let ((start (point)))
1861 (put-text-property start (point) 'face nil)
1862 (put-text-property start (point) 'mouse-face nil)))
1864 (defun speedbar-make-button (start end face mouse function &optional token)
1865 "Create a button from START to END, with FACE as the display face.
1866 MOUSE is the mouse face. When this button is clicked on FUNCTION
1867 will be run with the TOKEN parameter (any Lisp object)"
1868 (put-text-property start end 'face face)
1869 (put-text-property start end 'mouse-face mouse)
1870 (put-text-property start end 'invisible nil)
1871 (if function (put-text-property start end 'speedbar-function function))
1872 (if token (put-text-property start end 'speedbar-token token))
1875 ;;; Initial Expansion list management
1877 (defun speedbar-initial-expansion-list ()
1878 "Return the current default expansion list.
1879 This is based on `speedbar-initial-expansion-list-name' referencing
1880 `speedbar-initial-expansion-mode-alist'."
1881 ;; cdr1 - name, cdr2 - menu
1882 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
1883 speedbar-initial-expansion-mode-alist)))))
1885 (defun speedbar-initial-menu ()
1886 "Return the current default menu data.
1887 This is based on `speedbar-initial-expansion-list-name' referencing
1888 `speedbar-initial-expansion-mode-alist'."
1890 (car (cdr (assoc speedbar-initial-expansion-list-name
1891 speedbar-initial-expansion-mode-alist)))))
1893 (defun speedbar-initial-keymap ()
1894 "Return the current default menu data.
1895 This is based on `speedbar-initial-expansion-list-name' referencing
1896 `speedbar-initial-expansion-mode-alist'."
1898 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
1899 speedbar-initial-expansion-mode-alist))))))
1901 (defun speedbar-initial-stealthy-functions ()
1902 "Return a list of functions to call stealthily.
1903 This is based on `speedbar-initial-expansion-list-name' referencing
1904 `speedbar-stealthy-function-list'."
1905 (cdr (assoc speedbar-initial-expansion-list-name
1906 speedbar-stealthy-function-list)))
1908 (defun speedbar-add-expansion-list (new-list)
1909 "Add NEW-LIST to the list of expansion lists."
1910 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
1912 (defun speedbar-change-initial-expansion-list (new-default)
1913 "Change speedbar's default expansion list to NEW-DEFAULT."
1916 (completing-read (format "Speedbar Mode (default %s): "
1917 speedbar-previously-used-expansion-list-name)
1918 speedbar-initial-expansion-mode-alist
1920 speedbar-previously-used-expansion-list-name)))
1921 (setq speedbar-previously-used-expansion-list-name
1922 speedbar-initial-expansion-list-name
1923 speedbar-initial-expansion-list-name new-default)
1925 (speedbar-reconfigure-keymaps))
1928 ;;; Special speedbar display management
1930 (defun speedbar-maybe-add-localized-support (buffer)
1931 "Quick check function called on BUFFERs by the speedbar timer function.
1932 Maintains the value of local variables which control speedbars use
1933 of the special mode functions."
1934 (or speedbar-special-mode-expansion-list
1935 (speedbar-add-localized-speedbar-support buffer)))
1937 (defun speedbar-add-localized-speedbar-support (buffer)
1938 "Add localized speedbar support to BUFFER's mode if it is available."
1939 (interactive "bBuffer: ")
1940 (if (stringp buffer) (setq buffer (get-buffer buffer)))
1941 (if (not (buffer-live-p buffer))
1946 (let ((ms (symbol-name major-mode)) v)
1947 (if (not (string-match "-mode$" ms))
1948 nil ;; do nothing to broken mode
1949 (setq ms (substring ms 0 (match-beginning 0)))
1950 (setq v (intern-soft (concat ms "-speedbar-buttons")))
1951 (make-local-variable 'speedbar-special-mode-expansion-list)
1953 (setq speedbar-special-mode-expansion-list t)
1954 ;; If it is autoloaded, we need to load it now so that
1955 ;; we have access to the varialbe -speedbar-menu-items.
1956 ;; Is this XEmacs safe?
1957 (let ((sf (symbol-function v)))
1958 (if (and (listp sf) (eq (car sf) 'autoload))
1959 (load-library (car (cdr sf)))))
1960 (setq speedbar-special-mode-expansion-list (list v))
1961 (setq v (intern-soft (concat ms "-speedbar-key-map")))
1963 nil ;; don't add special keymap
1964 (make-local-variable 'speedbar-special-mode-key-map)
1965 (setq speedbar-special-mode-key-map
1967 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
1969 nil ;; don't add special menus
1970 (make-local-variable 'speedbar-easymenu-definition-special)
1971 (setq speedbar-easymenu-definition-special
1975 (defun speedbar-remove-localized-speedbar-support (buffer)
1976 "Remove any traces that BUFFER supports speedbar in a specialized way."
1979 (kill-local-variable 'speedbar-special-mode-expansion-list)
1980 (kill-local-variable 'speedbar-special-mode-key-map)
1981 (kill-local-variable 'speedbar-easymenu-definition-special)))
1983 ;;; File button management
1985 (defun speedbar-file-lists (directory)
1986 "Create file lists for DIRECTORY.
1987 The car is the list of directories, the cdr is list of files not
1988 matching ignored headers. Cache any directory files found in
1989 `speedbar-directory-contents-alist' and use that cache before scanning
1991 (setq directory (expand-file-name directory))
1992 ;; If in powerclick mode, then the directory we are getting
1993 ;; should be rescanned.
1994 (if speedbar-power-click
1995 (adelete 'speedbar-directory-contents-alist directory))
1996 ;; find the directory, either in the cache, or build it.
1997 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
1998 (let ((default-directory directory)
1999 (dir (directory-files directory nil))
2004 (or (string-match speedbar-file-unshown-regexp (car dir))
2005 (string-match speedbar-directory-unshown-regexp (car dir))))
2006 (if (file-directory-p (car dir))
2007 (setq dirs (cons (car dir) dirs))
2008 (setq files (cons (car dir) files))))
2009 (setq dir (cdr dir)))
2010 (let ((nl (cons (nreverse dirs) (list (nreverse files)))))
2011 (aput 'speedbar-directory-contents-alist directory nl)
2015 (defun speedbar-directory-buttons (directory index)
2016 "Insert a single button group at point for DIRECTORY.
2017 Each directory path part is a different button. If part of the path
2018 matches the user directory ~, then it is replaced with a ~.
2019 INDEX is not used, but is required by the caller."
2020 (let* ((tilde (expand-file-name "~"))
2021 (dd (expand-file-name directory))
2022 (junk (string-match (regexp-quote tilde) dd))
2024 (concat "~" (substring dd (match-end 0)))
2027 (if (string-match "^~/?\\'" displayme) (setq displayme (concat tilde "/")))
2031 (while (re-search-forward "\\([^/]+\\)/" nil t)
2032 (speedbar-make-button (match-beginning 1) (match-end 1)
2033 'speedbar-directory-face
2034 'speedbar-highlight-face
2035 'speedbar-directory-buttons-follow
2036 (if (= (match-beginning 1) p)
2037 (expand-file-name "~/") ;the tilde
2038 (buffer-substring-no-properties
2040 ;; Nuke the beginning of the directory if it's too long...
2041 (cond ((eq speedbar-directory-button-trim-method 'span)
2043 (let ((ww (or (speedbar-frame-width) 20)))
2044 (move-to-column ww nil)
2045 (while (>= (current-column) ww)
2046 (re-search-backward "/" nil t)
2047 (if (<= (current-column) 2)
2049 (re-search-forward "/" nil t)
2050 (if (< (current-column) 4)
2051 (re-search-forward "/" nil t))
2053 (if (looking-at "/?$")
2056 (move-to-column ww nil)))))
2057 ((eq speedbar-directory-button-trim-method 'trim)
2059 (let ((ww (or (speedbar-frame-width) 20))
2060 (tl (current-column)))
2063 (move-to-column (- tl ww))
2064 (if (re-search-backward "/" nil t)
2066 (delete-region (point-min) (point))
2070 (if (string-match "\\`/[^/]+/\\'" displayme)
2075 (speedbar-make-button p (point)
2076 'speedbar-directory-face
2077 'speedbar-highlight-face
2078 'speedbar-directory-buttons-follow
2081 (insert-char ?\n 1 nil)))
2083 (defun speedbar-make-tag-line (exp-button-type
2084 exp-button-char exp-button-function
2086 tag-button tag-button-function tag-button-data
2087 tag-button-face depth)
2088 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2089 This is the button that expands or contracts a node (if applicable),
2090 and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2091 is the function to call if it's clicked on. Button types are
2092 'bracket, 'angle, 'curly, or nil. EXP-BUTTON-DATA is extra data
2093 attached to the text forming the expansion button.
2095 Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2096 function to call if clicked on, and TAG-BUTTON-DATA is the data to
2097 attach to the text field (such a tag positioning, etc).
2098 TAG-BUTTON-FACE is a face used for this type of tag.
2100 Lastly, DEPTH shows the depth of expansion.
2102 This function assumes that the cursor is in the speedbar window at the
2103 position to insert a new item, and that the new item will end with a CR"
2104 (let ((start (point))
2106 (insert (int-to-string depth) ":")
2108 (put-text-property start end 'invisible t)
2110 (insert-char ? depth nil)
2111 (put-text-property (- (point) depth) (point) 'invisible nil)
2112 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2113 ((eq exp-button-type 'angle) "<%c>")
2114 ((eq exp-button-type 'curly) "{%c}")
2116 (buttxt (format exp-button exp-button-char))
2118 (end (progn (insert buttxt) (point)))
2119 (bf (if exp-button-type 'speedbar-button-face nil))
2120 (mf (if exp-button-function 'speedbar-highlight-face nil))
2122 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
2123 (if speedbar-hide-button-brackets-flag
2125 (put-text-property start (1+ start) 'invisible t)
2126 (put-text-property end (1- end) 'invisible t)))
2128 (insert-char ? 1 nil)
2129 (put-text-property (1- (point)) (point) 'invisible nil)
2130 (let ((start (point))
2131 (end (progn (insert tag-button) (point))))
2132 (insert-char ?\n 1 nil)
2133 (put-text-property (1- (point)) (point) 'invisible nil)
2134 (speedbar-make-button start end tag-button-face
2135 (if tag-button-function 'speedbar-highlight-face nil)
2136 tag-button-function tag-button-data))
2139 (defun speedbar-change-expand-button-char (char)
2140 "Change the expansion button character to CHAR for the current line."
2143 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2145 (speedbar-with-writable
2146 (goto-char (match-beginning 1))
2148 (insert-char char 1 t)
2149 (put-text-property (point) (1- (point)) 'invisible nil)))))
2152 ;;; Build button lists
2154 (defun speedbar-insert-files-at-point (files level)
2155 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2156 Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2157 don't know how to manage them. The input parameter FILES is a cons
2158 cell of the form ( 'DIRLIST . 'FILELIST )"
2159 ;; Start inserting all the directories
2160 (let ((dirs (car files)))
2162 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired (car dirs)
2163 (car dirs) 'speedbar-dir-follow nil
2164 'speedbar-directory-face level)
2165 (setq dirs (cdr dirs))))
2166 (let ((lst (car (cdr files)))
2167 (case-fold-search t))
2169 (let* ((known (string-match speedbar-file-regexp (car lst)))
2170 (expchar (if known ?+ ??))
2171 (fn (if known 'speedbar-tag-file nil)))
2172 (if (or speedbar-show-unknown-files (/= expchar ??))
2173 (speedbar-make-tag-line 'bracket expchar fn (car lst)
2174 (car lst) 'speedbar-find-file nil
2175 'speedbar-file-face level)))
2176 (setq lst (cdr lst)))))
2178 (defun speedbar-default-directory-list (directory index)
2179 "Insert files for DIRECTORY with level INDEX at point."
2180 (speedbar-insert-files-at-point
2181 (speedbar-file-lists directory) index)
2182 (speedbar-reset-scanners)
2184 ;; If the shown files variable has extra directories, then
2185 ;; it is our responsibility to redraw them all
2186 ;; Luckilly, the nature of inserting items into this list means
2187 ;; that by reversing it, we can easilly go in the right order
2188 (let ((sf (cdr (reverse speedbar-shown-directories))))
2189 (setq speedbar-shown-directories
2190 (list (expand-file-name default-directory)))
2191 ;; exand them all as we find them
2193 (if (speedbar-goto-this-file (car sf))
2196 (if (looking-at "[0-9]+:[ ]*<")
2198 (goto-char (match-end 0))
2199 (speedbar-do-function-pointer)))
2200 (setq sf (cdr sf)))))
2203 (defun speedbar-apply-one-tag-hierarchy-method (lst method)
2204 "Adjust the tag hierarchy LST by METHOD."
2207 (sort (copy-alist lst)
2208 (lambda (a b) (string< (car a) (car b)))))
2209 ((eq method 'prefix-group)
2214 (short-group-list nil)
2215 (short-start-name nil)
2216 (short-end-name nil)
2217 (num-shorts-grouped 0)
2218 (bins (make-vector 256 nil))
2220 ;; Break out sub-lists
2222 (if (listp (cdr-safe (car-safe lst)))
2223 (setq newlst (cons (car lst) newlst))
2224 (setq sublst (cons (car lst) sublst)))
2225 (setq lst (cdr lst)))
2226 ;; Now, first find out how long our list is. Never let a
2227 ;; list get-shorter than our minimum.
2228 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2229 (setq work-list (nreverse sublst))
2230 (setq diff-idx (length (try-completion "" sublst)))
2231 ;; Sort the whole list into bins.
2233 (let ((e (car sublst))
2234 (s (car (car sublst))))
2235 (cond ((<= (length s) diff-idx)
2236 ;; 0 storage bin for shorty.
2237 (aset bins 0 (cons e (aref bins 0))))
2239 ;; stuff into a bin based on ascii value at diff
2240 (aset bins (aref s diff-idx)
2241 (cons e (aref bins (aref s diff-idx)))))))
2242 (setq sublst (cdr sublst)))
2243 ;; Go through all our bins Stick singles into our
2244 ;; junk-list, everything else as sublsts in work-list.
2245 ;; If two neighboring lists are both small, make a grouped
2246 ;; group combinding those two sub-lists.
2248 (while (> 256 diff-idx)
2249 (let ((l (aref bins diff-idx)))
2251 (let ((tmp (cons (try-completion "" l) l)))
2252 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2253 (> (+ (length l) (length short-group-list))
2254 speedbar-tag-split-minimum-length))
2256 ;; We have reached a longer list, so we
2257 ;; must finish off a grouped group.
2259 ((and short-group-list
2260 (= (length short-group-list)
2261 num-shorts-grouped))
2262 ;; All singles? Junk list
2263 (setq junk-list (append short-group-list
2265 ((= num-shorts-grouped 1)
2266 ;; Only one short group? Just stick it in
2269 (cons (cons (try-completion
2270 "" short-group-list)
2271 (nreverse short-group-list))
2274 ;; Multiple groups to be named in a special
2275 ;; way by displaying the range over which we
2276 ;; have grouped them.
2278 (cons (cons (concat short-start-name
2281 (nreverse short-group-list))
2283 ;; Reset short group list information every time.
2284 (setq short-group-list nil
2285 short-start-name nil
2287 num-shorts-grouped 0)))
2288 ;; Ok, now that we cleaned up the short-group-list,
2289 ;; we can deal with this new list, to decide if it
2290 ;; should go on one of these sub-lists or not.
2291 (if (< (length l) speedbar-tag-regroup-maximum-length)
2292 (setq short-group-list (append short-group-list l)
2293 num-shorts-grouped (1+ num-shorts-grouped)
2294 short-end-name (car tmp)
2295 short-start-name (if short-start-name
2298 (setq work-list (cons tmp work-list))))))
2299 (setq diff-idx (1+ diff-idx))))
2300 ;; Did we run out of things? Drop our new list onto the end.
2302 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2303 ;; All singles? Junk list
2304 (setq junk-list (append short-group-list junk-list)))
2305 ((= num-shorts-grouped 1)
2306 ;; Only one short group? Just stick it in
2309 (cons (cons (try-completion "" short-group-list)
2310 (nreverse short-group-list))
2313 ;; Multiple groups to be named in a special
2314 ;; way by displaying the range over which we
2315 ;; have grouped them.
2317 (cons (cons (concat short-start-name " to " short-end-name)
2318 (nreverse short-group-list))
2320 ;; Now, stick our new list onto the end of
2323 (append (nreverse newlst)
2324 (nreverse work-list)
2326 (append (nreverse newlst)
2327 (nreverse work-list)))
2328 (append (nreverse newlst) junk-list))))
2329 ((eq method 'trim-words)
2336 (if (listp (cdr-safe (car-safe lst)))
2337 (setq newlst (cons (car lst) newlst))
2338 (setq sublst (cons (car lst) sublst)))
2339 (setq lst (cdr lst)))
2340 ;; Get the prefix to trim by. Make sure that we don't trim
2341 ;; off silly pieces, only complete understandable words.
2342 (setq trim-prefix (try-completion "" sublst))
2343 (if (or (= (length sublst) 1)
2345 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2346 (append (nreverse newlst) (nreverse sublst))
2347 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2349 (setq trim-chars (length trim-prefix))
2352 (cons (substring (car (car sublst)) trim-chars)
2355 sublst (cdr sublst)))
2356 ;; Put the lists together
2357 (append (nreverse newlst) trimlst))))
2358 ((eq method 'simple-group)
2362 (if (listp (cdr-safe (car-safe lst)))
2363 (setq newlst (cons (car lst) newlst))
2364 (setq sublst (cons (car lst) sublst)))
2365 (setq lst (cdr lst)))
2368 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2369 (nreverse newlst))))
2372 (defun speedbar-create-tag-hierarchy (lst)
2373 "Adjust the tag hierarchy in LST, and return it.
2374 This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2375 the list. See it's value for details."
2376 (let ((methods speedbar-tag-hierarchy-method))
2378 (setq lst (speedbar-apply-one-tag-hierarchy-method lst (car methods))
2379 methods (cdr methods)))
2382 (defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2383 "At LEVEL, insert a generic multi-level alist LST.
2384 Associations with lists get {+} tags (to expand into more nodes) and
2385 those with positions just get a > as the indicator. {+} buttons will
2386 have the function EXPAND-FUN and the token is the CDR list. The token
2387 name will have the function FIND-FUN and not token."
2388 ;; Remove imenu rescan button
2389 (if (string= (car (car lst)) "*Rescan*")
2390 (setq lst (cdr lst)))
2392 (setq lst (speedbar-create-tag-hierarchy lst))
2395 (cond ((null (car-safe lst)) nil) ;this would be a separator
2396 ((or (numberp (cdr-safe (car-safe lst)))
2397 (markerp (cdr-safe (car-safe lst))))
2398 (speedbar-make-tag-line nil nil nil nil ;no expand button data
2399 (car (car lst)) ;button name
2401 (cdr (car lst)) ;token is position
2404 ((listp (cdr-safe (car-safe lst)))
2405 (speedbar-make-tag-line 'curly ?+ expand-fun (cdr (car lst))
2406 (car (car lst)) ;button name
2407 nil nil 'speedbar-tag-face
2409 (t (message "Ooops!")))
2410 (setq lst (cdr lst))))
2414 (defun speedbar-update-contents ()
2415 "Generically update the contents of the speedbar buffer."
2417 ;; Set the current special buffer
2418 (setq speedbar-desired-buffer nil)
2419 ;; Check for special modes
2420 (speedbar-maybe-add-localized-support (current-buffer))
2421 ;; Choose the correct method of doodling.
2422 (if (and speedbar-mode-specific-contents-flag
2423 (listp speedbar-special-mode-expansion-list)
2424 speedbar-special-mode-expansion-list
2426 'speedbar-special-mode-expansion-list
2428 ;;(eq (get major-mode 'mode-class 'special)))
2429 (speedbar-update-special-contents)
2430 (speedbar-update-directory-contents)))
2432 (defun speedbar-update-directory-contents ()
2433 "Update the contents of the speedbar buffer based on the current directory."
2434 (let ((cbd (expand-file-name default-directory))
2436 (funclst (speedbar-initial-expansion-list))
2437 (cache speedbar-full-text-cache)
2438 ;; disable stealth during update
2439 (speedbar-stealthy-function-list nil)
2442 ;; Because there is a bug I can't find just yet
2445 (set-buffer speedbar-buffer)
2446 ;; If we are updating contents to where we are, then this is
2447 ;; really a request to update existing contents, so we must be
2448 ;; careful with our text cache!
2449 (if (member cbd speedbar-shown-directories)
2452 ;; If the current directory is not the last element in the dir
2453 ;; list, then we ALSO need to zap the list of expanded directories
2454 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2455 (setq speedbar-shown-directories (list cbd))))
2457 ;; Build cbd-parent, and see if THAT is in the current shown
2458 ;; directories. First, go through pains to get the parent directory
2459 (if (and speedbar-smart-directory-expand-flag
2461 (setq cbd-parent cbd)
2462 (if (string-match "/$" cbd-parent)
2463 (setq cbd-parent (substring cbd-parent 0
2464 (match-beginning 0))))
2465 (setq cbd-parent (file-name-directory cbd-parent)))
2466 (member cbd-parent speedbar-shown-directories))
2467 (setq expand-local t)
2469 ;; If this directory is NOT in the current list of available
2470 ;; paths, then use the cache, and set the cache to our new
2471 ;; value. Make sure to unhighlight the current file, or if we
2472 ;; come back to this directory, it might be a different file
2473 ;; and then we get a mess!
2474 (if (> (point-max) 1)
2476 (speedbar-clear-current-file)
2477 (setq speedbar-full-text-cache
2478 (cons speedbar-shown-directories (buffer-string)))))
2480 ;; Check if our new directory is in the list of directories
2481 ;; shown in the text-cache
2482 (if (member cbd (car cache))
2483 (setq speedbar-shown-directories (car cache)
2485 ;; default the shown directories to this list...
2486 (setq speedbar-shown-directories (list cbd)))
2488 (if (not expand-local) (setq speedbar-last-selected-file nil))
2489 (speedbar-with-writable
2490 (if (and expand-local
2491 ;; Find this directory as a speedbar node.
2492 (speedbar-path-line cbd))
2494 (speedbar-expand-line)
2497 (setq default-directory
2498 (nth (1- (length speedbar-shown-directories))
2499 speedbar-shown-directories))
2500 (insert (cdr cache)))
2503 (setq default-directory cbd)
2504 (funcall (car funclst) cbd 0)
2505 (setq funclst (cdr funclst))))))
2506 (goto-char (point-min)))))
2507 (speedbar-reconfigure-keymaps))
2509 (defun speedbar-update-special-contents ()
2510 "Used the mode-specific variable to fill in the speedbar buffer.
2511 This should only be used by modes classified as special."
2512 (let ((funclst speedbar-special-mode-expansion-list)
2513 (specialbuff (current-buffer)))
2515 (setq speedbar-desired-buffer specialbuff)
2516 (set-buffer speedbar-buffer)
2517 ;; If we are leaving a directory, cache it.
2518 (if (not speedbar-shown-directories)
2521 ;; Clean up directory maintenance stuff
2522 (speedbar-clear-current-file)
2523 (setq speedbar-full-text-cache
2524 (cons speedbar-shown-directories (buffer-string))
2525 speedbar-shown-directories nil))
2526 ;; Now fill in the buffer with our newly found specialized list.
2527 (speedbar-with-writable
2529 ;; We do not erase the buffer because these functions may
2530 ;; decide NOT to update themselves.
2531 (funcall (car funclst) specialbuff)
2532 (setq funclst (cdr funclst))))
2533 (goto-char (point-min))))
2534 (speedbar-reconfigure-keymaps))
2536 (defun speedbar-timer-fn ()
2537 "Run whenever Emacs is idle to update the speedbar item."
2538 (if (not (and (frame-live-p speedbar-frame)
2539 (frame-live-p speedbar-attached-frame)))
2540 (speedbar-set-timer nil)
2541 ;; Save all the match data so that we don't mess up executing fns
2543 ;; Only do stuff if the frame is visible, not an icon, and if
2544 ;; it is currently flagged to do something.
2545 (if (and speedbar-update-flag
2546 (frame-visible-p speedbar-frame)
2547 (not (eq (frame-visible-p speedbar-frame) 'icon)))
2548 (let ((af (selected-frame)))
2549 (save-window-excursion
2550 (select-frame speedbar-attached-frame)
2551 ;; make sure we at least choose a window to
2552 ;; get a good directory from
2553 (if (window-minibuffer-p (selected-window))
2555 ;; Check for special modes
2556 (speedbar-maybe-add-localized-support (current-buffer))
2557 ;; Update for special mode all the time!
2558 (if (and speedbar-mode-specific-contents-flag
2559 (listp speedbar-special-mode-expansion-list)
2560 speedbar-special-mode-expansion-list
2562 'speedbar-special-mode-expansion-list
2564 ;;(eq (get major-mode 'mode-class 'special)))
2566 (if (<= 2 speedbar-verbosity-level)
2567 (message "Updating speedbar to special mode: %s..."
2569 (speedbar-update-special-contents)
2570 (if (<= 2 speedbar-verbosity-level)
2572 (message "Updating speedbar to special mode: %s...done"
2575 ;; Update all the contents if directories change!
2576 (if (or (member (expand-file-name default-directory)
2577 speedbar-shown-directories)
2578 (and speedbar-ignored-path-regexp
2580 speedbar-ignored-path-regexp
2581 (expand-file-name default-directory)))
2582 (member major-mode speedbar-ignored-modes)
2583 (eq af speedbar-frame)
2584 (not (buffer-file-name)))
2586 (if (<= 1 speedbar-verbosity-level)
2587 (message "Updating speedbar to: %s..."
2589 (speedbar-update-directory-contents)
2590 (if (<= 1 speedbar-verbosity-level)
2592 (message "Updating speedbar to: %s...done"
2596 ;; Now run stealthy updates of time-consuming items
2597 (speedbar-stealthy-updates)))
2598 ;; Now run the mouse tracking system
2599 (speedbar-show-info-under-mouse)))
2600 (run-hooks 'speedbar-timer-hook))
2603 ;;; Stealthy activities
2605 (defvar speedbar-stealthy-update-recurse nil
2606 "Recursion avoidance variable for stealthy update.")
2608 (defun speedbar-stealthy-updates ()
2609 "For a given speedbar, run all items in the stealthy function list.
2610 Each item returns t if it completes successfully, or nil if
2611 interrupted by the user."
2612 (if (not speedbar-stealthy-update-recurse)
2613 (let ((l (speedbar-initial-stealthy-functions))
2614 (speedbar-stealthy-update-recurse t))
2616 (while (and l (funcall (car l)))
2619 ;;(message "Exit with %S" (car l))
2622 (defun speedbar-reset-scanners ()
2623 "Reset any variables used by functions in the stealthy list as state.
2624 If new functions are added, their state needs to be updated here."
2625 (setq speedbar-vc-to-do-point t
2626 speedbar-obj-to-do-point t)
2627 (run-hooks 'speedbar-scanner-reset-hook)
2630 (defun speedbar-find-selected-file (file)
2631 "Goto the line where FILE is."
2632 (goto-char (point-min))
2634 (while (and (setq m (re-search-forward
2635 (concat " \\(" (file-name-nondirectory file)
2636 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2642 (goto-char (match-beginning 0))
2645 (looking-at "[0-9]+:")
2646 (string-to-number (match-string 0)))))
2647 (match-string 1))))))
2650 (goto-char (match-beginning 1))
2651 (match-string 1)))))
2653 (defun speedbar-clear-current-file ()
2654 "Locate the file thought to be current, and remove its highlighting."
2656 (set-buffer speedbar-buffer)
2657 (if speedbar-last-selected-file
2658 (speedbar-with-writable
2659 (if (speedbar-find-selected-file speedbar-last-selected-file)
2660 (put-text-property (match-beginning 1)
2663 'speedbar-file-face))))))
2665 (defun speedbar-update-current-file ()
2666 "Find the current file, and update our visuals to indicate its name.
2667 This is specific to file names. If the file name doesn't show up, but
2668 it should be in the list, then the directory cache needs to be
2670 (let* ((lastf (selected-frame))
2671 (newcfd (save-excursion
2672 (select-frame speedbar-attached-frame)
2673 (let ((rf (if (buffer-file-name)
2676 (select-frame lastf)
2678 (newcf (if newcfd newcfd))
2679 (lastb (current-buffer))
2680 (sucf-recursive (boundp 'sucf-recursive))
2681 (case-fold-search t))
2683 ;; check here, that way we won't refresh to newcf until
2684 ;; its been written, thus saving ourselves some time
2685 (file-exists-p newcf)
2686 (not (string= newcf speedbar-last-selected-file)))
2688 ;; It is important to select the frame, otherwise the window
2689 ;; we want the cursor to move in will not be updated by the
2690 ;; search-forward command.
2691 (select-frame speedbar-frame)
2692 ;; Remove the old file...
2693 (speedbar-clear-current-file)
2694 ;; now highlight the new one.
2695 (set-buffer speedbar-buffer)
2696 (speedbar-with-writable
2697 (if (speedbar-find-selected-file newcf)
2698 ;; put the property on it
2699 (put-text-property (match-beginning 1)
2702 'speedbar-selected-face)
2703 ;; Oops, it's not in the list. Should it be?
2704 (if (and (string-match speedbar-file-regexp newcf)
2705 (string= (file-name-directory newcfd)
2706 (expand-file-name default-directory)))
2707 ;; yes, it is (we will ignore unknowns for now...)
2710 (if (speedbar-find-selected-file newcf)
2711 ;; put the property on it
2712 (put-text-property (match-beginning 1)
2715 'speedbar-selected-face)))
2716 ;; if it's not in there now, whatever...
2718 (setq speedbar-last-selected-file newcf))
2719 (if (not sucf-recursive)
2721 (speedbar-center-buffer-smartly)
2722 (speedbar-position-cursor-on-line)
2725 (select-frame lastf)
2727 ;; return that we are done with this activity.
2730 (defun speedbar-add-indicator (indicator-string &optional replace-this)
2731 "Add INDICATOR-STRING to the end of this speedbar line.
2732 If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2733 an the existing indicator is removed. If there is already an
2734 indicator, then do not add a space."
2736 ;; The nature of the beast: Assume we are in "the right place"
2738 (skip-chars-backward (concat " " speedbar-vc-indicator
2739 (car speedbar-obj-indicator)
2740 (cdr speedbar-obj-indicator)))
2741 (if (and (not (looking-at speedbar-indicator-regex))
2742 (not (string= indicator-string " ")))
2743 (insert speedbar-indicator-separator))
2744 (speedbar-with-writable
2746 (if (and replace-this
2747 (re-search-forward replace-this (save-excursion (end-of-line)
2750 (delete-region (match-beginning 0) (match-end 0))))
2752 (if (not (string= " " indicator-string))
2753 (insert indicator-string))))
2755 ;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
2756 ;; Steven L Baur <steve@xemacs.org> said this was important:
2757 (eval-when-compile (or (featurep 'xemacs)
2758 (condition-case () (require 'efs)
2759 (error (require 'ange-ftp)))))
2761 (defun speedbar-check-vc ()
2762 "Scan all files in a directory, and for each see if it's checked out.
2763 See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
2764 to add more types of version control systems."
2765 ;; Check for to-do to be reset. If reset but no RCS is available
2766 ;; then set to nil (do nothing) otherwise, start at the beginning
2768 (set-buffer speedbar-buffer)
2769 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
2770 (speedbar-vc-check-dir-p default-directory)
2771 (not (or (and (featurep 'ange-ftp)
2773 (car (if speedbar-xemacsp
2774 ange-ftp-path-format
2775 ange-ftp-name-format))
2776 (expand-file-name default-directory)))
2777 ;; efs support: Bob Weiner
2778 (and (featurep 'efs)
2780 (car efs-path-regexp)
2781 (expand-file-name default-directory))))))
2782 (setq speedbar-vc-to-do-point 0))
2783 (if (numberp speedbar-vc-to-do-point)
2785 (goto-char speedbar-vc-to-do-point)
2786 (while (and (not (input-pending-p))
2787 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2789 (setq speedbar-vc-to-do-point (point))
2790 (if (speedbar-check-vc-this-line (match-string 1))
2791 (speedbar-add-indicator speedbar-vc-indicator
2792 (regexp-quote speedbar-vc-indicator))
2793 (speedbar-add-indicator " "
2794 (regexp-quote speedbar-vc-indicator))))
2795 (if (input-pending-p)
2796 ;; return that we are incomplete
2798 ;; we are done, set to-do to nil
2799 (setq speedbar-vc-to-do-point nil)
2804 (defun speedbar-check-vc-this-line (depth)
2805 "Return t if the file on this line is check of of a version control system.
2806 Parameter DEPTH is a string with the current depth of indentation of
2807 the file being checked."
2808 (let* ((d (string-to-int depth))
2809 (f (speedbar-line-path d))
2810 (fn (buffer-substring-no-properties
2811 ;; Skip-chars: thanks ptype@dra.hmg.gb
2813 (skip-chars-forward "^ "
2814 (save-excursion (end-of-line)
2817 (fulln (concat f fn)))
2818 (if (<= 2 speedbar-verbosity-level)
2819 (message "Speedbar vc check...%s" fulln))
2820 (and (file-writable-p fulln)
2821 (speedbar-this-file-in-vc f fn))))
2823 (defun speedbar-vc-check-dir-p (path)
2824 "Return t if we should bother checking PATH for version control files.
2825 This can be overloaded to add new types of version control systems."
2828 (file-exists-p (concat path "RCS/"))
2830 (file-exists-p (concat path "SCCS/"))
2831 ;; Remote SCCS project
2832 (let ((proj-dir (getenv "PROJECTDIR")))
2834 (file-exists-p (concat proj-dir "/SCCS"))
2837 (run-hook-with-args 'speedbar-vc-path-enable-hook path)
2840 (defun speedbar-this-file-in-vc (path name)
2841 "Check to see if the file in PATH with NAME is in a version control system.
2842 You can add new VC systems by overriding this function. You can
2843 optimize this function by overriding it and only doing those checks
2844 that will occur on your system."
2847 (file-exists-p (concat path "RCS/" name ",v"))
2848 (file-exists-p (concat path "RCS/" name))
2849 ;; Local SCCS file name
2850 (file-exists-p (concat path "SCCS/p." name))
2851 ;; Remote SCCS file name
2852 (let ((proj-dir (getenv "PROJECTDIR")))
2854 (file-exists-p (concat proj-dir "/SCCS/p." name))
2857 (run-hook-with-args 'speedbar-vc-in-control-hook path name)
2860 ;; Objet File scanning
2861 (defun speedbar-check-objects ()
2862 "Scan all files in a directory, and for each see if there is an object.
2863 See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
2864 to add more object types."
2865 ;; Check for to-do to be reset. If reset but no RCS is available
2866 ;; then set to nil (do nothing) otherwise, start at the beginning
2868 (set-buffer speedbar-buffer)
2869 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
2870 (setq speedbar-obj-to-do-point 0))
2871 (if (numberp speedbar-obj-to-do-point)
2873 (goto-char speedbar-obj-to-do-point)
2874 (while (and (not (input-pending-p))
2875 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2877 (setq speedbar-obj-to-do-point (point))
2878 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
2879 (if (not ind) (setq ind " "))
2880 (speedbar-add-indicator ind (concat
2881 (car speedbar-obj-indicator)
2883 (cdr speedbar-obj-indicator)))))
2884 (if (input-pending-p)
2885 ;; return that we are incomplete
2887 ;; we are done, set to-do to nil
2888 (setq speedbar-obj-to-do-point nil)
2893 (defun speedbar-check-obj-this-line (depth)
2894 "Return t if the file on this line has an associated object.
2895 Parameter DEPTH is a string with the current depth of indentation of
2896 the file being checked."
2897 (let* ((d (string-to-int depth))
2898 (f (speedbar-line-path d))
2899 (fn (buffer-substring-no-properties
2900 ;; Skip-chars: thanks ptype@dra.hmg.gb
2902 (skip-chars-forward "^ "
2903 (save-excursion (end-of-line)
2906 (fulln (concat f fn)))
2907 (if (<= 2 speedbar-verbosity-level)
2908 (message "Speedbar obj check...%s" fulln))
2909 (let ((oa speedbar-obj-alist))
2910 (while (and oa (not (string-match (car (car oa)) fulln)))
2912 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
2915 ;; Find out if the object is out of date or not.
2916 (let ((date1 (nth 5 (file-attributes fulln)))
2917 (date2 (nth 5 (file-attributes (concat
2918 (file-name-sans-extension fulln)
2920 (if (or (< (car date1) (car date2))
2921 (and (= (car date1) (car date2))
2922 (< (nth 1 date1) (nth 1 date2))))
2923 (car speedbar-obj-indicator)
2924 (cdr speedbar-obj-indicator)))))))
2926 ;;; Clicking Activity
2928 (defun speedbar-quick-mouse (e)
2929 "Since mouse events are strange, this will keep the mouse nicely positioned.
2930 This should be bound to mouse event E."
2933 (speedbar-position-cursor-on-line)
2936 (defun speedbar-position-cursor-on-line ()
2937 "Position the cursor on a line."
2938 (let ((oldpos (point)))
2940 (if (looking-at "[0-9]+:\\s-*..?.? ")
2941 (goto-char (1- (match-end 0)))
2942 (goto-char oldpos))))
2944 (defun speedbar-power-click (e)
2945 "Activate any speedbar button as a power click.
2946 This should be bound to mouse event E."
2948 (let ((speedbar-power-click t))
2949 (speedbar-click e)))
2951 (defun speedbar-click (e)
2952 "Activate any speedbar buttons where the mouse is clicked.
2953 This must be bound to a mouse event. A button is any location of text
2954 with a mouse face that has a text property called `speedbar-function'.
2955 This should be bound to mouse event E."
2958 (speedbar-do-function-pointer)
2959 (speedbar-quick-mouse e))
2961 (defun speedbar-double-click (e)
2962 "Activate any speedbar buttons where the mouse is clicked.
2963 This must be bound to a mouse event. A button is any location of text
2964 with a mouse face that has a text property called `speedbar-function'.
2965 This should be bound to mouse event E."
2967 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
2968 (cond ((eq (car e) 'down-mouse-1)
2969 (mouse-set-point e))
2970 ((eq (car e) 'mouse-1)
2971 (speedbar-quick-mouse e))
2972 ((or (eq (car e) 'double-down-mouse-1)
2973 (eq (car e) 'triple-down-mouse-1))
2975 (speedbar-do-function-pointer)
2976 (speedbar-quick-mouse e))))
2978 (defun speedbar-do-function-pointer ()
2979 "Look under the cursor and examine the text properties.
2980 From this extract the file/tag name, token, indentation level and call
2981 a function if appropriate"
2982 (let* ((fn (get-text-property (point) 'speedbar-function))
2983 (tok (get-text-property (point) 'speedbar-token))
2984 ;; The 1-,+ is safe because scaning starts AFTER the point
2985 ;; specified. This lets the search include the character the
2987 (tp (previous-single-property-change
2988 (1+ (point)) 'speedbar-function))
2989 (np (next-single-property-change
2990 (point) 'speedbar-function))
2991 (txt (buffer-substring-no-properties (or tp (point-min))
2992 (or np (point-max))))
2993 (dent (save-excursion (beginning-of-line)
2995 (if (looking-at "[0-9]+")
2996 (buffer-substring-no-properties
2997 (match-beginning 0) (match-end 0))
2999 ;;(message "%S:%S:%S:%s" fn tok txt dent)
3000 (and fn (funcall fn txt tok dent)))
3001 (speedbar-position-cursor-on-line))
3003 ;;; Reading info from the speedbar buffer
3005 (defun speedbar-line-file (&optional p)
3006 "Retrieve the file or whatever from the line at P point.
3007 The return value is a string representing the file. If it is a
3008 directory, then it is the directory name."
3012 (if (looking-at (concat
3013 "\\([0-9]+\\): *[[<][-+?][]>] \\([^ \n]+\\)\\("
3014 speedbar-indicator-regex "\\)?"))
3015 (let* ((depth (string-to-int (match-string 1)))
3016 (path (speedbar-line-path depth))
3017 (f (match-string 2)))
3021 (defun speedbar-goto-this-file (file)
3022 "If FILE is displayed, goto this line and return t.
3023 Otherwise do not move and return nil."
3024 (let ((path (substring (file-name-directory (expand-file-name file))
3025 (length (expand-file-name default-directory))))
3028 (goto-char (point-min))
3029 ;; scan all the directories
3030 (while (and path (not (eq path t)))
3031 (if (string-match "^/?\\([^/]+\\)" path)
3032 (let ((pp (match-string 1 path)))
3033 (if (save-match-data
3034 (re-search-forward (concat "> " (regexp-quote pp) "$")
3036 (setq path (substring path (match-end 1)))
3039 ;; find the file part
3040 (if (or (not path) (string= (file-name-nondirectory file) ""))
3041 ;; only had a dir part
3044 (speedbar-position-cursor-on-line)
3046 (goto-char dest) nil)
3047 ;; find the file part
3048 (let ((nd (file-name-nondirectory file)))
3049 (if (re-search-forward
3050 (concat "] \\(" (regexp-quote nd)
3051 "\\)\\(" speedbar-indicator-regex "\\)$")
3054 (speedbar-position-cursor-on-line)
3059 (defun speedbar-line-path (depth)
3060 "Retrieve the pathname associated with the current line.
3061 This may require traversing backwards from DEPTH and combining the default
3062 directory with these items."
3064 ((string= speedbar-initial-expansion-list-name "files")
3068 (setq depth (1- depth))
3069 (while (/= depth -1)
3070 (if (not (re-search-backward (format "^%d:" depth) nil t))
3071 (error "Error building path of tag")
3072 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)$")
3073 (setq path (concat (buffer-substring-no-properties
3074 (match-beginning 1) (match-end 1))
3077 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)$")
3078 ;; This is the start of our path.
3079 (setq path (buffer-substring-no-properties
3080 (match-beginning 1) (match-end 1))))))
3081 (setq depth (1- depth)))
3083 (string-match (concat speedbar-indicator-regex "$")
3085 (setq path (substring path 0 (match-beginning 0))))
3086 (concat default-directory path)))))
3088 ;; If we aren't in file mode, then return an empty string to make
3089 ;; sure that we can still get some stuff done.
3092 (defun speedbar-path-line (path)
3093 "Position the cursor on the line specified by PATH."
3095 (if (string-match "/$" path)
3096 (setq path (substring path 0 (match-beginning 0))))
3097 (let ((nomatch t) (depth 0)
3098 (fname (file-name-nondirectory path))
3099 (pname (file-name-directory path)))
3100 (if (not (member pname speedbar-shown-directories))
3101 (error "Internal Error: File %s not shown in speedbar" path))
3102 (goto-char (point-min))
3105 (concat "[]>] \\(" (regexp-quote fname)
3106 "\\)\\(" speedbar-indicator-regex "\\)?$")
3109 (looking-at "\\([0-9]+\\):")
3110 (setq depth (string-to-int (match-string 0))
3111 nomatch (not (string= pname (speedbar-line-path depth))))
3116 (defun speedbar-edit-line ()
3117 "Edit whatever tag or file is on the current speedbar line."
3121 ;; If this fails, then it is a non-standard click, and as such,
3122 ;; perfectly allowed.
3123 (if (re-search-forward "[]>?}] [^ ]"
3124 (save-excursion (end-of-line) (point))
3126 (speedbar-do-function-pointer)
3128 (speedbar-do-function-pointer)))
3130 (defun speedbar-expand-line ()
3131 "Expand the line under the cursor."
3134 (re-search-forward ":\\s-*.\\+. " (save-excursion (end-of-line) (point)))
3136 (speedbar-do-function-pointer))
3138 (defun speedbar-contract-line ()
3139 "Contract the line under the cursor."
3142 (re-search-forward ":\\s-*.-. " (save-excursion (end-of-line) (point)))
3144 (speedbar-do-function-pointer))
3146 (if speedbar-xemacsp
3147 (defalias 'speedbar-mouse-event-p 'button-press-event-p)
3148 (defun speedbar-mouse-event-p (event)
3149 "Return t if the event is a mouse related event"
3150 ;; And Emacs does it this way
3151 (if (and (listp event)
3152 (member (event-basic-type event)
3153 '(mouse-1 mouse-2 mouse-3)))
3157 (defun speedbar-maybee-jump-to-attached-frame ()
3158 "Jump to the attached frame ONLY if this was not a mouse event."
3159 (if (or (not (speedbar-mouse-event-p last-input-event))
3160 speedbar-activity-change-focus-flag)
3162 (select-frame speedbar-attached-frame)
3165 (defun speedbar-find-file (text token indent)
3166 "Speedbar click handler for filenames.
3167 TEXT, the file will be displayed in the attached frame.
3168 TOKEN is unused, but required by the click handler. INDENT is the
3169 current indentation level."
3170 (let ((cdd (speedbar-line-path indent)))
3171 (speedbar-find-file-in-frame (concat cdd text))
3172 (speedbar-stealthy-updates)
3173 (run-hooks 'speedbar-visiting-file-hook)
3174 ;; Reset the timer with a new timeout when cliking a file
3175 ;; in case the user was navigating directories, we can cancel
3176 ;; that other timer.
3177 (speedbar-set-timer speedbar-update-speed))
3178 (speedbar-maybee-jump-to-attached-frame))
3180 (defun speedbar-dir-follow (text token indent)
3181 "Speedbar click handler for directory names.
3182 Clicking a directory will cause the speedbar to list files in the
3183 the subdirectory TEXT. TOKEN is an unused requirement. The
3184 subdirectory chosen will be at INDENT level."
3185 (setq default-directory
3186 (concat (expand-file-name (concat (speedbar-line-path indent) text))
3188 ;; Because we leave speedbar as the current buffer,
3189 ;; update contents will change directory without
3190 ;; having to touch the attached frame. Turn off smart expand just
3192 (let ((speedbar-smart-directory-expand-flag nil))
3193 (speedbar-update-contents))
3194 (speedbar-set-timer speedbar-navigating-speed)
3195 (setq speedbar-last-selected-file nil)
3196 (speedbar-stealthy-updates))
3198 (defun speedbar-delete-subblock (indent)
3199 "Delete text from point to indentation level INDENT or greater.
3200 Handles end-of-sublist smartly."
3201 (speedbar-with-writable
3203 (end-of-line) (forward-char 1)
3204 (let ((start (point)))
3205 (while (and (looking-at "^\\([0-9]+\\):")
3206 (> (string-to-int (match-string 1)) indent)
3209 (beginning-of-line))
3210 (delete-region start (point))))))
3212 (defun speedbar-dired (text token indent)
3213 "Speedbar click handler for directory expand button.
3214 Clicking this button expands or contracts a directory. TEXT is the
3215 button clicked which has either a + or -. TOKEN is the directory to be
3216 expanded. INDENT is the current indentation level."
3217 (cond ((string-match "+" text) ;we have to expand this dir
3218 (setq speedbar-shown-directories
3219 (cons (expand-file-name
3220 (concat (speedbar-line-path indent) token "/"))
3221 speedbar-shown-directories))
3222 (speedbar-change-expand-button-char ?-)
3223 (speedbar-reset-scanners)
3225 (end-of-line) (forward-char 1)
3226 (speedbar-with-writable
3227 (speedbar-default-directory-list
3228 (concat (speedbar-line-path indent) token "/")
3230 ((string-match "-" text) ;we have to contract this node
3231 (speedbar-reset-scanners)
3232 (let ((oldl speedbar-shown-directories)
3234 (td (expand-file-name
3235 (concat (speedbar-line-path indent) token))))
3237 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3238 (setq newl (cons (car oldl) newl)))
3239 (setq oldl (cdr oldl)))
3240 (setq speedbar-shown-directories newl))
3241 (speedbar-change-expand-button-char ?+)
3242 (speedbar-delete-subblock indent)
3244 (t (error "Ooops... not sure what to do")))
3245 (speedbar-center-buffer-smartly)
3246 (setq speedbar-last-selected-file nil)
3247 (save-excursion (speedbar-stealthy-updates)))
3249 (defun speedbar-directory-buttons-follow (text token indent)
3250 "Speedbar click handler for default directory buttons.
3251 TEXT is the button clicked on. TOKEN is the directory to follow.
3252 INDENT is the current indentation level and is unused."
3253 (if (string-match "^[A-z]:$" token)
3254 (setq default-directory (concat token (char-to-string directory-sep-char)))
3255 (setq default-directory token))
3256 ;; Because we leave speedbar as the current buffer,
3257 ;; update contents will change directory without
3258 ;; having to touch the attached frame.
3259 (speedbar-update-contents)
3260 (speedbar-set-timer speedbar-navigating-speed))
3262 (defun speedbar-tag-file (text token indent)
3263 "The cursor is on a selected line. Expand the tags in the specified file.
3264 The parameter TEXT and TOKEN are required, where TEXT is the button
3265 clicked, and TOKEN is the file to expand. INDENT is the current
3267 (cond ((string-match "+" text) ;we have to expand this file
3268 (let* ((fn (expand-file-name (concat (speedbar-line-path indent)
3270 (lst (if speedbar-use-imenu-flag
3271 (let ((tim (speedbar-fetch-dynamic-imenu fn)))
3273 (speedbar-fetch-dynamic-etags fn)
3275 (speedbar-fetch-dynamic-etags fn))))
3276 ;; if no list, then remove expando button
3278 (speedbar-change-expand-button-char ??)
3279 (speedbar-change-expand-button-char ?-)
3280 (speedbar-with-writable
3282 (end-of-line) (forward-char 1)
3283 (speedbar-insert-generic-list indent
3284 lst 'speedbar-tag-expand
3285 'speedbar-tag-find))))))
3286 ((string-match "-" text) ;we have to contract this node
3287 (speedbar-change-expand-button-char ?+)
3288 (speedbar-delete-subblock indent))
3289 (t (error "Ooops... not sure what to do")))
3290 (speedbar-center-buffer-smartly))
3292 (defun speedbar-tag-find (text token indent)
3293 "For the tag TEXT in a file TOKEN, goto that position.
3294 INDENT is the current indentation level."
3295 (let ((file (speedbar-line-path indent)))
3296 (speedbar-find-file-in-frame file)
3297 (save-excursion (speedbar-stealthy-updates))
3298 ;; Reset the timer with a new timeout when cliking a file
3299 ;; in case the user was navigating directories, we can cancel
3300 ;; that other timer.
3301 (speedbar-set-timer speedbar-update-speed)
3303 (run-hooks 'speedbar-visiting-tag-hook)
3305 (speedbar-maybee-jump-to-attached-frame)
3308 (defun speedbar-tag-expand (text token indent)
3309 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3310 Etags does not support this feature. TEXT will be the button
3311 string. TOKEN will be the list, and INDENT is the current indentation
3313 (cond ((string-match "+" text) ;we have to expand this file
3314 (speedbar-change-expand-button-char ?-)
3315 (speedbar-with-writable
3317 (end-of-line) (forward-char 1)
3318 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
3319 'speedbar-tag-find))))
3320 ((string-match "-" text) ;we have to contract this node
3321 (speedbar-change-expand-button-char ?+)
3322 (speedbar-delete-subblock indent))
3323 (t (error "Ooops... not sure what to do")))
3324 (speedbar-center-buffer-smartly))
3326 ;;; Loading files into the attached frame.
3328 (defun speedbar-find-file-in-frame (file)
3329 "This will load FILE into the speedbar attached frame.
3330 If the file is being displayed in a different frame already, then raise that
3332 (let* ((buff (find-file-noselect file))
3333 (bwin (get-buffer-window buff 0)))
3336 (select-window bwin)
3337 (raise-frame (window-frame bwin)))
3338 (if speedbar-power-click
3339 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3340 (select-frame speedbar-attached-frame)
3341 (switch-to-buffer buff))))
3344 ;;; Centering Utility
3346 (defun speedbar-center-buffer-smartly ()
3347 "Recenter a speedbar buffer so the current indentation level is all visible.
3348 This assumes that the cursor is on a file, or tag of a file which the user is
3350 (if (<= (count-lines (point-min) (point-max))
3351 (1- (window-height (selected-window))))
3352 ;; whole buffer fits
3354 (goto-char (point-min))
3358 (let (depth start end exp p)
3361 (setq depth (if (looking-at "[0-9]+")
3362 (string-to-int (buffer-substring-no-properties
3363 (match-beginning 0) (match-end 0)))
3365 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3368 (if (re-search-backward exp nil t)
3369 (setq start (point))
3370 (setq start (point-min)))
3371 (save-excursion ;Not sure about this part.
3374 (while (and (not (re-search-forward exp nil t))
3376 (setq depth (1- depth))
3377 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3380 (setq end (point-max)))))
3381 ;; Now work out the details of centering
3382 (let ((nl (count-lines start end))
3384 (if (> nl (window-height (selected-window)))
3385 ;; We can't fit it all, so just center on cursor
3386 (progn (goto-char start)
3388 ;; we can fit everything on the screen, but...
3389 (if (and (pos-visible-in-window-p start (selected-window))
3390 (pos-visible-in-window-p end (selected-window)))
3393 ;; we need to do something...
3395 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3396 (lte (count-lines start (point-max))))
3397 (if (and (< (+ newcent lte) (window-height (selected-window)))
3398 (> (- (window-height (selected-window)) lte 1)
3400 (setq newcent (- (window-height (selected-window))
3402 (recenter newcent))))
3406 ;;; Tag Management -- Imenu
3408 (if (not speedbar-use-imenu-flag)
3412 (eval-when-compile (if (locate-library "imenu") (require 'imenu)))
3414 (defun speedbar-fetch-dynamic-imenu (file)
3415 "Load FILE into a buffer, and generate tags using Imenu.
3416 Returns the tag list, or t for an error."
3417 ;; Load this AND compile it in
3420 (set-buffer (find-file-noselect file))
3421 (if speedbar-power-click (setq imenu--index-alist nil))
3423 (let ((index-alist (imenu--make-index-alist t)))
3424 (if speedbar-sort-tags
3425 (sort (copy-alist index-alist)
3426 (lambda (a b) (string< (car a) (car b))))
3431 ;;; Tag Management -- etags (old XEmacs compatibility part)
3433 (defvar speedbar-fetch-etags-parse-list
3434 '(;; Note that java has the same parse-group as c
3435 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\)\\'" .
3436 speedbar-parse-c-or-c++tag)
3437 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3438 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
3439 ; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3440 ; speedbar-parse-fortran77-tag)
3441 ("\\.tex\\'" . speedbar-parse-tex-string)
3443 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3445 "Associations of file extensions and expressions for extracting tags.
3446 To add a new file type, you would want to add a new association to the
3447 list, where the car is the file match, and the cdr is the way to
3448 extract an element from the tags output. If the output is complex,
3449 use a function symbol instead of regexp. The function should expect
3450 to be at the beginning of a line in the etags buffer.
3452 This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3454 (defvar speedbar-fetch-etags-command "etags"
3455 "*Command used to create an etags file.
3457 This variable is ignored if `speedbar-use-imenu-flag' is t")
3459 (defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3460 "*List of arguments to use with `speedbar-fetch-etags-command'.
3461 This creates an etags output buffer. Use `speedbar-toggle-etags' to
3462 modify this list conveniently.
3464 This variable is ignored if `speedbar-use-imenu-flag' is t")
3466 (defun speedbar-toggle-etags (flag)
3467 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3468 FLAG then becomes a member of etags command line arguments. If flag
3469 is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3470 value is \"show\" then toggle the value of
3471 `speedbar-show-unknown-files'.
3473 This function is a convenience function for XEmacs menu created by
3474 Farzin Guilak <farzin@protocol.com>"
3477 ((equal flag "sort")
3478 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3479 ((equal flag "show")
3480 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3481 ((or (equal flag "-C")
3484 (if (member flag speedbar-fetch-etags-arguments)
3485 (setq speedbar-fetch-etags-arguments
3486 (delete flag speedbar-fetch-etags-arguments))
3487 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3490 (defun speedbar-fetch-dynamic-etags (file)
3491 "For FILE, run etags and create a list of symbols extracted.
3492 Each symbol will be associated with its line position in FILE."
3493 (let ((newlist nil))
3496 (if (get-buffer "*etags tmp*")
3497 (kill-buffer "*etags tmp*")) ;kill to clean it up
3498 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3499 (set-buffer (get-buffer-create "*etags tmp*"))
3500 (apply 'call-process speedbar-fetch-etags-command nil
3501 (current-buffer) nil
3502 (append speedbar-fetch-etags-arguments (list file)))
3503 (goto-char (point-min))
3504 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3506 (let ((exprlst speedbar-fetch-etags-parse-list)
3508 (while (and (not ans) exprlst)
3509 (if (string-match (car (car exprlst)) file)
3510 (setq ans (car exprlst)))
3511 (setq exprlst (cdr exprlst)))
3515 (while (not (save-excursion (end-of-line) (eobp)))
3517 (setq tnl (speedbar-extract-one-symbol expr)))
3518 (if tnl (setq newlist (cons tnl newlist)))
3520 (message "Sorry, no support for a file of that extension"))))
3522 (if speedbar-sort-tags
3523 (sort newlist (lambda (a b) (string< (car a) (car b))))
3524 (reverse newlist))))
3526 ;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3527 ;; sure it's needed with the different sorting method.
3529 ;(defun speedbar-clean-etags()
3530 ; "Removes spaces before the ^? character, and removes `#define',
3531 ;return types, etc. preceding tags. This ensures that the sort operation
3532 ;works on the tags, not the return types."
3534 ; (goto-char (point-min))
3536 ; (re-search-forward "(?[ \t](?\C-?" nil t)
3537 ; (replace-match "\C-?" nil nil))
3538 ; (goto-char (point-min))
3540 ; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3541 ; (delete-region (match-beginning 1) (match-end 1)))))
3543 (defun speedbar-extract-one-symbol (expr)
3544 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
3545 The line should contain output from etags. Parse the output using the
3546 regular expression EXPR"
3547 (let* ((sym (if (stringp expr)
3549 (re-search-forward expr (save-excursion
3552 (buffer-substring-no-properties (match-beginning 1)
3555 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3561 (1+ (string-to-int (buffer-substring-no-properties
3569 (defun speedbar-parse-c-or-c++tag ()
3570 "Parse a c or c++ tag, which tends to be a little complex."
3572 (let ((bound (save-excursion (end-of-line) (point))))
3573 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3574 (buffer-substring-no-properties (match-beginning 1)
3576 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3577 (buffer-substring-no-properties (match-beginning 1)
3579 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3580 (buffer-substring-no-properties (match-beginning 1)
3585 (defun speedbar-parse-tex-string ()
3586 "Parse a Tex string. Only find data which is relevant."
3588 (let ((bound (save-excursion (end-of-line) (point))))
3589 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3590 (buffer-substring-no-properties (match-beginning 0)
3595 ;;; BUFFER DISPLAY mode.
3597 (defvar speedbar-buffers-key-map nil
3598 "Keymap used when in the buffers display mode.")
3600 (if speedbar-buffers-key-map
3602 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3604 ;; Basic tree features
3605 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3606 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3607 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3608 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3610 ;; Buffer specific keybindings
3611 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3612 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3616 (defvar speedbar-buffer-easymenu-definition
3617 '(["Jump to buffer" speedbar-edit-line t]
3618 ["Expand File Tags" speedbar-expand-line
3619 (save-excursion (beginning-of-line)
3620 (looking-at "[0-9]+: *.\\+. "))]
3621 ["Contract File Tags" speedbar-contract-line
3622 (save-excursion (beginning-of-line)
3623 (looking-at "[0-9]+: *.-. "))]
3625 "Menu item elements shown when displaying a buffer list.")
3627 (defun speedbar-buffer-buttons (directory zero)
3628 "Create speedbar buttons based on the buffers currently loaded.
3629 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3630 (speedbar-buffer-buttons-engine nil))
3632 (defun speedbar-buffer-buttons-temp (directory zero)
3633 "Create speedbar buttons based on the buffers currently loaded.
3634 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3635 (speedbar-buffer-buttons-engine t))
3637 (defun speedbar-buffer-buttons-engine (temp)
3638 "Create speedbar buffer buttons.
3639 If TEMP is non-nil, then clicking on a buffer restores the previous display."
3640 (insert "Active Buffers:\n")
3641 (let ((bl (buffer-list)))
3643 (if (string-match "^[ *]" (buffer-name (car bl)))
3645 (let* ((known (string-match speedbar-file-regexp
3646 (buffer-name (car bl))))
3647 (expchar (if known ?+ ??))
3648 (fn (if known 'speedbar-tag-file nil))
3649 (fname (save-excursion (set-buffer (car bl))
3650 (buffer-file-name))))
3651 (speedbar-make-tag-line 'bracket expchar fn fname
3652 (buffer-name (car bl))
3653 'speedbar-buffer-click temp
3654 'speedbar-file-face 0)))
3656 (setq bl (buffer-list))
3657 (insert "Scratch Buffers:\n")
3659 (if (not (string-match "^\\*" (buffer-name (car bl))))
3661 (if (eq (car bl) speedbar-buffer)
3663 (speedbar-make-tag-line 'bracket ?? nil nil
3664 (buffer-name (car bl))
3665 'speedbar-buffer-click temp
3666 'speedbar-file-face 0)))
3668 (setq bl (buffer-list))
3669 (insert "Hidden Buffers:\n")
3671 (if (not (string-match "^ " (buffer-name (car bl))))
3673 (if (eq (car bl) speedbar-buffer)
3675 (speedbar-make-tag-line 'bracket ?? nil nil
3676 (buffer-name (car bl))
3677 'speedbar-buffer-click temp
3678 'speedbar-file-face 0)))
3679 (setq bl (cdr bl)))))
3681 (defun speedbar-buffer-click (text token indent)
3682 "When the users clicks on a buffer-button in speedbar.
3683 TEXT is the buffer's name, TOKEN and INDENT are unused."
3684 (if speedbar-power-click
3685 (let ((pop-up-frames t)) (select-window (display-buffer text)))
3686 (select-frame speedbar-attached-frame)
3687 (switch-to-buffer text)
3688 (if token (speedbar-change-initial-expansion-list
3689 speedbar-previously-used-expansion-list-name))))
3691 (defun speedbar-buffer-kill-buffer ()
3692 "Kill the buffer the cursor is on in the speedbar buffer."
3696 ;; If this fails, then it is a non-standard click, and as such,
3697 ;; perfectly allowed.
3698 (if (re-search-forward "[]>?}] [^ ]"
3699 (save-excursion (end-of-line) (point))
3703 (buffer-substring (point) (save-excursion
3706 (if (and (get-buffer text)
3707 (y-or-n-p (format "Kill buffer %s? " text)))
3709 (speedbar-refresh))))))
3711 (defun speedbar-buffer-revert-buffer ()
3712 "Revert the buffer the cursor is on in the speedbar buffer."
3716 ;; If this fails, then it is a non-standard click, and as such,
3717 ;; perfectly allowed
3718 (if (re-search-forward "[]>?}] [^ ]"
3719 (save-excursion (end-of-line) (point))
3723 (buffer-substring (point) (save-excursion
3726 (if (get-buffer text)
3729 (revert-buffer t)))))))
3733 ;;; Color loading section This is messy *Blech!*
3735 (defface speedbar-button-face '((((class color) (background light))
3736 (:foreground "green4"))
3737 (((class color) (background dark))
3738 (:foreground "green3")))
3739 "Face used for +/- buttons."
3740 :group 'speedbar-faces)
3742 (defface speedbar-file-face '((((class color) (background light))
3743 (:foreground "cyan4"))
3744 (((class color) (background dark))
3745 (:foreground "cyan"))
3747 "Face used for file names."
3748 :group 'speedbar-faces)
3750 (defface speedbar-directory-face '((((class color) (background light))
3751 (:foreground "blue4"))
3752 (((class color) (background dark))
3753 (:foreground "light blue")))
3754 "Faced used for directory names."
3755 :group 'speedbar-faces)
3756 (defface speedbar-tag-face '((((class color) (background light))
3757 (:foreground "brown"))
3758 (((class color) (background dark))
3759 (:foreground "yellow")))
3760 "Face used for displaying tags."
3761 :group 'speedbar-faces)
3763 (defface speedbar-selected-face '((((class color) (background light))
3764 (:foreground "red" :underline t))
3765 (((class color) (background dark))
3766 (:foreground "red" :underline t))
3768 "Face used to underline the file in the active window."
3769 :group 'speedbar-faces)
3771 (defface speedbar-highlight-face '((((class color) (background light))
3772 (:background "green"))
3773 (((class color) (background dark))
3774 (:background "sea green"))
3775 (((class grayscale monochrome)
3777 (:background "black"))
3778 (((class grayscale monochrome)
3780 (:background "white")))
3781 "Face used for highlighting buttons with the mouse."
3782 :group 'speedbar-faces)
3784 ;; some edebug hooks
3785 (add-hook 'edebug-setup-hook
3787 (def-edebug-spec speedbar-with-writable def-body)))
3790 ;;; speedbar ends here
3792 ;; run load-time hooks
3793 (run-hooks 'speedbar-load-hook)