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