(speedbar-file-regexp): Definition moved up.
[emacs.git] / lisp / speedbar.el
blob524a6d4f2f84669cf0e2ab83ee1ec14294be1003
1 ;;; speedbar.el --- quick access to files and tags in a frame
3 ;;; Copyright (C) 1996, 97, 98, 99, 2000, 01 Free Software Foundation
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 0.11a
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' to 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 can be easily added.
95 ;; AUCTEX users: The imenu tags for AUCTEX mode doesn't work very
96 ;; well. Use the imenu keywords from tex-mode.el for better results.
98 ;; This file requires the library package assoc (association lists)
100 ;;; Developing for speedbar
102 ;; Adding a speedbar specialized display mode:
104 ;; Speedbar can be configured to create a special display for certain
105 ;; modes that do not display traditional file/tag data. Rmail, Info,
106 ;; and the debugger are examples. These modes can, however, benefit
107 ;; from a speedbar style display in their own way.
109 ;; If your `major-mode' is `foo-mode', the only requirement is to
110 ;; create a function called `foo-speedbar-buttons' which takes one
111 ;; argument, BUFFER. BUFFER will be the buffer speedbar wants filled.
112 ;; In `foo-speedbar-buttons' there are several functions that make
113 ;; building a speedbar display easy. See the documentation for
114 ;; `speedbar-with-writable' (needed because the buffer is usually
115 ;; read-only) `speedbar-make-tag-line', `speedbar-insert-button', and
116 ;; `speedbar-insert-generic-list'. If you use
117 ;; `speedbar-insert-generic-list', also read the doc for
118 ;; `speedbar-tag-hierarchy-method' in case you wish to override it.
119 ;; The macro `speedbar-with-attached-buffer' brings you back to the
120 ;; buffer speedbar is displaying for.
122 ;; For those functions that make buttons, the "function" should be a
123 ;; symbol that is the function to call when clicked on. The "token"
124 ;; is extra data you can pass along. The "function" must take three
125 ;; parameters. They are (TEXT TOKEN INDENT). TEXT is the text of the
126 ;; button clicked on. TOKEN is the data passed in when you create the
127 ;; button. INDENT is an indentation level, or 0. You can store
128 ;; indentation levels with `speedbar-make-tag-line' which creates a
129 ;; line with an expander (eg. [+]) and a text button.
131 ;; Some useful functions when writing expand functions, and click
132 ;; functions are `speedbar-change-expand-button-char',
133 ;; `speedbar-delete-subblock', and `speedbar-center-buffer-smartly'.
134 ;; The variable `speedbar-power-click' is set to t in your functions
135 ;; when the user shift-clicks. This is an indication of anything from
136 ;; refreshing cached data to making a buffer appear in a new frame.
138 ;; If you wish to add to the default speedbar menu for the case of
139 ;; `foo-mode', create a variable `foo-speedbar-menu-items'. This
140 ;; should be a list compatible with the `easymenu' package. It will
141 ;; be spliced into the main menu. (Available with click-mouse-3). If
142 ;; you wish to have extra key bindings in your special mode, create a
143 ;; variable `foo-speedbar-key-map'. Instead of using `make-keymap',
144 ;; or `make-sparse-keymap', use the function
145 ;; `speedbar-make-specialized-keymap'. This lets you inherit all of
146 ;; speedbar's default bindings with low overhead.
148 ;; Adding a speedbar top-level display mode:
150 ;; Unlike the specialized modes, there are no name requirements,
151 ;; however the methods for writing a button display, menu, and keymap
152 ;; are the same. Once you create these items, you can call the
153 ;; function `speedbar-add-expansion-list'. It takes one parameter
154 ;; which is a list element of the form (NAME MENU KEYMAP &rest
155 ;; BUTTON-FUNCTIONS). NAME is a string that will show up in the
156 ;; Displays menu item. MENU is a symbol containing the menu items to
157 ;; splice in. KEYMAP is a symbol holding the keymap to use, and
158 ;; BUTTON-FUNCTIONS are the function names to call, in order, to create
159 ;; the display.
160 ;; Another tweakable variable is `speedbar-stealthy-function-list'
161 ;; which is of the form (NAME &rest FUNCTION ...). NAME is the string
162 ;; name matching `speedbar-add-expansion-list'. (It does not need to
163 ;; exist.). This provides additional display info which might be
164 ;; time-consuming to calculate.
165 ;; Lastly, `speedbar-mode-functions-list' allows you to set special
166 ;; function overrides. At the moment very few functions may be
167 ;; overridden, but more will be added as the need is discovered.
169 ;;; TODO:
170 ;; - More functions to create buttons and options
171 ;; - Timeout directories we haven't visited in a while.
173 (require 'assoc)
174 (require 'easymenu)
176 (condition-case nil
177 (require 'image)
178 (error nil))
180 (defvar speedbar-xemacsp (string-match "XEmacs" emacs-version)
181 "Non-nil if we are running in the XEmacs environment.")
182 (defvar speedbar-xemacs20p (and speedbar-xemacsp
183 (>= emacs-major-version 20)))
185 ;; customization stuff
186 (defgroup speedbar nil
187 "File and tag browser frame."
188 :group 'etags
189 :group 'tools
190 :group 'convenience
191 :version "20.3")
193 (defgroup speedbar-faces nil
194 "Faces used in speedbar."
195 :prefix "speedbar-"
196 :group 'speedbar
197 :group 'faces)
199 (defgroup speedbar-vc nil
200 "Version control display in speedbar."
201 :prefix "speedbar-"
202 :group 'speedbar)
204 ;;; Code:
205 (defvar speedbar-initial-expansion-mode-alist
206 '(("buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
207 speedbar-buffer-buttons)
208 ("quick buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
209 speedbar-buffer-buttons-temp)
210 ;; Files last, means first in the Displays menu
211 ("files" speedbar-easymenu-definition-special speedbar-file-key-map
212 speedbar-directory-buttons speedbar-default-directory-list)
214 "List of named expansion elements for filling the speedbar frame.
215 These expansion lists are only valid for regular files. Special modes
216 still get to override this list on a mode-by-mode basis. This list of
217 lists is of the form (NAME MENU KEYMAP FN1 FN2 ...). NAME is a string
218 representing the types of things to be displayed. MENU is an easymenu
219 structure used when in this mode. KEYMAP is a local keymap to install
220 over the regular speedbar keymap. FN1 ... are functions that will be
221 called in order. These functions will always get the default
222 directory to use passed in as the first parameter, and a 0 as the
223 second parameter. The 0 indicates the uppermost indentation level.
224 They must assume that the cursor is at the position where they start
225 inserting buttons.")
227 (defvar speedbar-initial-expansion-list-name "files"
228 "A symbol name representing the expansion list to use.
229 The expansion list `speedbar-initial-expansion-mode-alist' contains
230 the names and associated functions to use for buttons in speedbar.")
232 (defvar speedbar-previously-used-expansion-list-name "files"
233 "Save the last expansion list method.
234 This is used for returning to a previous expansion list method when
235 the user is done with the current expansion list.")
237 (defvar speedbar-stealthy-function-list
238 '(("files"
239 speedbar-update-current-file speedbar-check-vc speedbar-check-objects)
241 "List of functions to periodically call stealthily.
242 This list is of the form:
243 '( (\"NAME\" FUNCTION ...)
244 ...)
245 where NAME is the name of the major display mode these functions are
246 for, and the remaining elements FUNCTION are functions to call in order.
247 Each function must return nil if interrupted, or t if completed.
248 Stealthy functions which have a single operation should always return
249 t. Functions which take a long time should maintain a state (where
250 they are in their speedbar related calculations) and permit
251 interruption. See `speedbar-check-vc' as a good example.")
253 (defvar speedbar-mode-functions-list
254 '(("files" (speedbar-item-info . speedbar-files-item-info)
255 (speedbar-line-path . speedbar-files-line-path))
256 ("buffers" (speedbar-item-info . speedbar-buffers-item-info)
257 (speedbar-line-path . speedbar-buffers-line-path))
258 ("quick buffers" (speedbar-item-info . speedbar-buffers-item-info)
259 (speedbar-line-path . speedbar-buffers-line-path))
261 "List of function tables to use for different major display modes.
262 It is not necessary to define any functions for a specialized mode.
263 This just provides a simple way of adding lots of customizations.
264 Each sublist is of the form:
265 (\"NAME\" (FUNCTIONSYMBOL . REPLACEMENTFUNCTION) ...)
266 Where NAME is the name of the specialized mode. The rest of the list
267 is a set of dotted pairs of the form FUNCTIONSYMBOL, which is the name
268 of a function you would like to replace, and REPLACEMENTFUNCTION,
269 which is a function you can call instead. Not all functions can be
270 replaced this way. Replaceable functions must provide that
271 functionality individually.")
273 (defcustom speedbar-mode-specific-contents-flag t
274 "*Non-nil means speedbar will show special mode contents.
275 This permits some modes to create customized contents for the speedbar
276 frame."
277 :group 'speedbar
278 :type 'boolean)
280 (defvar speedbar-special-mode-expansion-list nil
281 "Default function list for creating specialized button lists.
282 This list is set by modes that wish to have special speedbar displays.
283 The list is of function names. Each function is called with one
284 parameter BUFFER, the originating buffer. The current buffer is the
285 speedbar buffer.")
287 (defvar speedbar-special-mode-key-map nil
288 "Default keymap used when identifying a specialized display mode.
289 This keymap is local to each buffer that wants to define special keybindings
290 effective when its display is shown.")
292 (defcustom speedbar-visiting-file-hook nil
293 "Hooks run when speedbar visits a file in the selected frame."
294 :group 'speedbar
295 :type 'hook)
297 (defcustom speedbar-visiting-tag-hook '(speedbar-highlight-one-tag-line)
298 "Hooks run when speedbar visits a tag in the selected frame."
299 :group 'speedbar
300 :type 'hook
301 :version "21.1"
302 :options '(speedbar-highlight-one-tag-line
303 speedbar-recenter-to-top
304 speedbar-recenter
307 (defcustom speedbar-load-hook nil
308 "Hooks run when speedbar is loaded."
309 :group 'speedbar
310 :type 'hook)
312 (defcustom speedbar-reconfigure-keymaps-hook nil
313 "Hooks run when the keymaps are regenerated."
314 :group 'speedbar
315 :version "21.1"
316 :type 'hook)
318 (defcustom speedbar-show-unknown-files nil
319 "*Non-nil show files we can't expand with a ? in the expand button.
320 nil means don't show the file in the list."
321 :group 'speedbar
322 :type 'boolean)
324 (defcustom speedbar-update-speed
325 (if speedbar-xemacsp
326 (if speedbar-xemacs20p
327 2 ; 1 is too obrusive in XEmacs
328 5) ; when no idleness, need long delay
330 "*Idle time in seconds needed before speedbar will update itself.
331 Updates occur to allow speedbar to display directory information
332 relevant to the buffer you are currently editing."
333 :group 'speedbar
334 :type 'integer)
336 ;; When I moved to a repeating timer, I had the horrible missfortune
337 ;; of loosing the ability for adaptive speed choice. This update
338 ;; speed currently causes long delays when it should have been turned off.
339 (defcustom speedbar-navigating-speed speedbar-update-speed
340 "*Idle time to wait after navigation commands in speedbar are executed.
341 Navigation commands included expanding/contracting nodes, and moving
342 between different directories."
343 :group 'speedbar
344 :type 'integer)
346 (defcustom speedbar-frame-parameters '((minibuffer . nil)
347 (width . 20)
348 (border-width . 0)
349 (menu-bar-lines . 0)
350 (tool-bar-lines . 0)
351 (unsplittable . t))
352 "*Parameters to use when creating the speedbar frame in Emacs.
353 Any parameter supported by a frame may be added. The parameter `height'
354 will be initialized to the height of the frame speedbar is
355 attached to and added to this list before the new frame is initialized."
356 :group 'speedbar
357 :type '(repeat (sexp :tag "Parameter:")))
359 ;; These values by Hrvoje Niksic <hniksic@srce.hr>
360 (defcustom speedbar-frame-plist
361 '(minibuffer nil width 20 border-width 0
362 internal-border-width 0 unsplittable t
363 default-toolbar-visible-p nil has-modeline-p nil
364 menubar-visible-p nil)
365 "*Parameters to use when creating the speedbar frame in XEmacs.
366 Parameters not listed here which will be added automatically are
367 `height' which will be initialized to the height of the frame speedbar
368 is attached to."
369 :group 'speedbar
370 :type '(repeat (group :inline t
371 (symbol :tag "Property")
372 (sexp :tag "Value"))))
374 (defcustom speedbar-use-imenu-flag (stringp (locate-library "imenu"))
375 "*Non-nil means use imenu for file parsing. nil to use etags.
376 XEmacs prior to 20.4 doesn't support imenu, therefore the default is to
377 use etags instead. Etags support is not as robust as imenu support."
378 :tag "Use Imenu for tags"
379 :group 'speedbar
380 :type 'boolean)
382 (defvar speedbar-dynamic-tags-function-list
383 '((speedbar-fetch-dynamic-imenu . speedbar-insert-imenu-list)
384 (speedbar-fetch-dynamic-etags . speedbar-insert-etags-list))
385 "Set to a list of functions which will return and insert a list of tags.
386 Each element is of the form ( FETCH . INSERT ) where FETCH
387 is a function which takes one parameter (the file to tag) and returns a
388 list of tags. The tag list can be of any form as long as the
389 corresponding insert method can handle it. If it returns t, then an
390 error occurred, and the next fetch routine is tried.
391 INSERT is a function which takes an INDENTation level, and a LIST of
392 tags to insert. It will then create the speedbar buttons.")
394 (defcustom speedbar-track-mouse-flag t
395 "*Non-nil means to display info about the line under the mouse."
396 :group 'speedbar
397 :type 'boolean)
399 (defcustom speedbar-sort-tags nil
400 "*If non-nil, sort tags in the speedbar display. *Obsolete*."
401 :group 'speedbar
402 :type 'boolean)
404 (defcustom speedbar-tag-hierarchy-method
405 '(speedbar-prefix-group-tag-hierarchy
406 speedbar-trim-words-tag-hierarchy)
407 "*List of hooks which speedbar will use to organize tags into groups.
408 Groups are defined as expandable meta-tags. Imenu supports
409 such things in some languages, such as separating variables from
410 functions. Each hook takes one argument LST, and may destructively
411 create a new list of the same form. LST is a list of elements of the
412 form:
413 (ELT1 ELT2 ... ELTn)
414 where each ELT is of the form
415 (TAG-NAME-STRING . NUMBER-OR-MARKER)
417 (GROUP-NAME-STRING ELT1 ELT2... ELTn)"
418 :group 'speedbar
419 :type 'hook
420 :options '(speedbar-sort-tag-hierarchy
421 speedbar-trim-words-tag-hierarchy
422 speedbar-prefix-group-tag-hierarchy
423 speedbar-simple-group-tag-hierarchy)
426 (defcustom speedbar-tag-group-name-minimum-length 4
427 "*The minimum length of a prefix group name before expanding.
428 Thus, if the `speedbar-tag-hierarchy-method' includes `prefix-group'
429 and one such groups common characters is less than this number of
430 characters, then the group name will be changed to the form of:
431 worda to wordb
432 instead of just
433 word
434 This way we won't get silly looking listings."
435 :group 'speedbar
436 :type 'integer)
438 (defcustom speedbar-tag-split-minimum-length 20
439 "*Minimum length before we stop trying to create sub-lists in tags.
440 This is used by all tag-hierarchy methods that break large lists into
441 sub-lists."
442 :group 'speedbar
443 :type 'integer)
445 (defcustom speedbar-tag-regroup-maximum-length 10
446 "*Maximum length of submenus that are regrouped.
447 If the regrouping option is used, then if two or more short subgroups
448 are next to each other, then they are combined until this number of
449 items is reached."
450 :group 'speedbar
451 :type 'integer)
453 (defcustom speedbar-activity-change-focus-flag nil
454 "*Non-nil means the selected frame will change based on activity.
455 Thus, if a file is selected for edit, the buffer will appear in the
456 selected frame and the focus will change to that frame."
457 :group 'speedbar
458 :type 'boolean)
460 (defcustom speedbar-directory-button-trim-method 'span
461 "*Indicates how the directory button will be displayed.
462 Possible values are:
463 'span - span large directories over multiple lines.
464 'trim - trim large directories to only show the last few.
465 nil - no trimming."
466 :group 'speedbar
467 :type '(radio (const :tag "Span large directories over mutiple lines."
468 span)
469 (const :tag "Trim large directories to only show the last few."
470 trim)
471 (const :tag "No trimming." nil)))
473 (defcustom speedbar-smart-directory-expand-flag t
474 "*Non-nil means speedbar should use smart expansion.
475 Smart expansion only affects when speedbar wants to display a
476 directory for a file in the attached frame. When smart expansion is
477 enabled, new directories which are children of a displayed directory
478 are expanded in the current framework. If nil, then the current
479 hierarchy would be replaced with the new directory."
480 :group 'speedbar
481 :type 'boolean)
483 (defcustom speedbar-indentation-width 1
484 "*When sub-nodes are expanded, the number of spaces used for indentation."
485 :group 'speedbar
486 :version "21.1"
487 :type 'integer)
489 (defcustom speedbar-hide-button-brackets-flag nil
490 "*Non-nil means speedbar will hide the brackets around the + or -."
491 :group 'speedbar
492 :version "21.1"
493 :type 'boolean)
495 (defcustom speedbar-use-images (and (or (fboundp 'defimage)
496 (fboundp 'make-image-specifier))
497 (if (fboundp 'display-graphic-p)
498 (display-graphic-p)
499 window-system))
500 "*Non-nil if speedbar should display icons."
501 :group 'speedbar
502 :version "21.1"
503 :type 'boolean)
505 (defcustom speedbar-before-popup-hook nil
506 "*Hooks called before popping up the speedbar frame."
507 :group 'speedbar
508 :type 'hook)
510 (defcustom speedbar-before-delete-hook nil
511 "*Hooks called before deleting the speedbar frame."
512 :group 'speedbar
513 :type 'hook)
515 (defcustom speedbar-mode-hook nil
516 "*Hooks called after creating a speedbar buffer."
517 :group 'speedbar
518 :type 'hook)
520 (defcustom speedbar-timer-hook nil
521 "*Hooks called after running the speedbar timer function."
522 :group 'speedbar
523 :type 'hook)
525 (defcustom speedbar-verbosity-level 1
526 "*Verbosity level of the speedbar. 0 means say nothing.
527 1 means medium level verbosity. 2 and higher are higher levels of
528 verbosity."
529 :group 'speedbar
530 :type 'integer)
532 (defvar speedbar-indicator-separator " "
533 "String separating file text from indicator characters.")
535 (defcustom speedbar-vc-do-check t
536 "*Non-nil check all files in speedbar to see if they have been checked out.
537 Any file checked out is marked with `speedbar-vc-indicator'."
538 :group 'speedbar-vc
539 :type 'boolean)
541 (defvar speedbar-vc-indicator "*"
542 "Text used to mark files which are currently checked out.
543 Other version control systems can be added by examining the function
544 `speedbar-vc-path-enable-hook' and `speedbar-vc-in-control-hook'.")
546 (defcustom speedbar-vc-path-enable-hook nil
547 "*Return non-nil if the current path should be checked for Version Control.
548 Functions in this hook must accept one parameter which is the path
549 being checked."
550 :group 'speedbar-vc
551 :type 'hook)
553 (defcustom speedbar-vc-in-control-hook nil
554 "*Return non-nil if the specified file is under Version Control.
555 Functions in this hook must accept two parameters. The PATH of the
556 current file, and the FILENAME of the file being checked."
557 :group 'speedbar-vc
558 :type 'hook)
560 (defvar speedbar-vc-to-do-point nil
561 "Local variable maintaining the current version control check position.")
563 (defcustom speedbar-obj-do-check t
564 "*Non-nil check all files in speedbar to see if they have an object file.
565 Any file checked out is marked with `speedbar-obj-indicator', and the
566 marking is based on `speedbar-obj-alist'."
567 :group 'speedbar-vc
568 :type 'boolean)
570 (defvar speedbar-obj-to-do-point nil
571 "Local variable maintaining the current version control check position.")
573 (defvar speedbar-obj-indicator '("#" . "!")
574 "Text used to mark files that have a corresponding hidden object file.
575 The car is for an up-to-date object. The cdr is for an out of date object.
576 The expression `speedbar-obj-alist' defines who gets tagged.")
578 (defvar speedbar-obj-alist
579 '(("\\.\\([cpC]\\|cpp\\|cc\\)$" . ".o")
580 ("\\.el$" . ".elc")
581 ("\\.java$" . ".class")
582 ("\\.f\\(or\\|90\\|77\\)?$" . ".o")
583 ("\\.tex$" . ".dvi")
584 ("\\.texi$" . ".info"))
585 "Alist of file extensions, and their corresponding object file type.")
587 (defvar speedbar-indicator-regex
588 (concat (regexp-quote speedbar-indicator-separator)
589 "\\("
590 (regexp-quote speedbar-vc-indicator)
591 "\\|"
592 (regexp-quote (car speedbar-obj-indicator))
593 "\\|"
594 (regexp-quote (cdr speedbar-obj-indicator))
595 "\\)*")
596 "Regular expression used when identifying files.
597 Permits stripping of indicator characters from a line.")
599 (defcustom speedbar-scanner-reset-hook nil
600 "*Hook called whenever generic scanners are reset.
601 Set this to implement your own scanning / rescan safe functions with
602 state data."
603 :group 'speedbar
604 :type 'hook)
606 (defvar speedbar-ignored-modes nil
607 "*List of major modes which speedbar will not switch directories for.")
609 (defun speedbar-extension-list-to-regex (extlist)
610 "Takes EXTLIST, a list of extensions and transforms it into regexp.
611 All the preceding `.' are stripped for an optimized expression starting
612 with `.' followed by extensions, followed by full-filenames."
613 (let ((regex1 nil) (regex2 nil))
614 (while extlist
615 (if (= (string-to-char (car extlist)) ?.)
616 (setq regex1 (concat regex1 (if regex1 "\\|" "")
617 (substring (car extlist) 1)))
618 (setq regex2 (concat regex2 (if regex2 "\\|" "") (car extlist))))
619 (setq extlist (cdr extlist)))
620 ;; concat all the sub-exressions together, making sure all types
621 ;; of parts exist during concatination.
622 (concat "\\("
623 (if regex1 (concat "\\(\\.\\(" regex1 "\\)\\)") "")
624 (if (and regex1 regex2) "\\|" "")
625 (if regex2 (concat "\\(" regex2 "\\)") "")
626 "\\)$")))
628 (defvar speedbar-ignored-path-regexp nil
629 "Regular expression matching paths speedbar will not switch to.
630 Created from `speedbar-ignored-path-expressions' with the function
631 `speedbar-extension-list-to-regex' (A misnamed function in this case.)
632 Use the function `speedbar-add-ignored-path-regexp', or customize the
633 variable `speedbar-ignored-path-expressions' to modify this variable.")
635 (defcustom speedbar-ignored-path-expressions
636 '("[/\\]logs?[/\\]\\'")
637 "*List of regular expressions matching directories speedbar will ignore.
638 They should included paths to directories which are notoriously very
639 large and take a long time to load in. Use the function
640 `speedbar-add-ignored-path-regexp' to add new items to this list after
641 speedbar is loaded. You may place anything you like in this list
642 before speedbar has been loaded."
643 :group 'speedbar
644 :type '(repeat (regexp :tag "Path Regexp"))
645 :set (lambda (sym val)
646 (setq speedbar-ignored-path-expressions val
647 speedbar-ignored-path-regexp
648 (speedbar-extension-list-to-regex val))))
650 (defcustom speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\)\\'"
651 "*Regular expression matching directories not to show in speedbar.
652 They should include commonly existing directories which are not
653 useful, such as version control."
654 :group 'speedbar
655 :type 'string)
657 (defvar speedbar-file-unshown-regexp
658 (let ((nstr "") (noext completion-ignored-extensions))
659 (while noext
660 (setq nstr (concat nstr (regexp-quote (car noext)) "\\'"
661 (if (cdr noext) "\\|" ""))
662 noext (cdr noext)))
663 ;; backup refdir lockfile
664 (concat nstr "\\|#[^#]+#$\\|\\.\\.?\\'\\|\\.#"))
665 "*Regexp matching files we don't want displayed in a speedbar buffer.
666 It is generated from the variable `completion-ignored-extensions'")
668 ;; This can't be further down, since it is needed just after.
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 ;; this is dangerous to customize, because the defaults will probably
676 ;; change in the future.
677 (defcustom speedbar-supported-extension-expressions
678 (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
679 ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".f\\(90\\|77\\|or\\)?")
680 (if speedbar-use-imenu-flag
681 '(".ada" ".p[lm]" ".tcl" ".m" ".scm" ".pm" ".py" ".g"
682 ;; html is not supported by default, but an imenu tags package
683 ;; is available. Also, html files are nice to be able to see.
684 ".s?html"
685 "[Mm]akefile\\(\\.in\\)?")))
686 "*List of regular expressions which will match files supported by tagging.
687 Do not prefix the `.' char with a double \\ to quote it, as the period
688 will be stripped by a simplified optimizer when compiled into a
689 singular expression. This variable will be turned into
690 `speedbar-file-regexp' for use with speedbar. You should use the
691 function `speedbar-add-supported-extension' to add a new extension at
692 runtime, or use the configuration dialog to set it in your .emacs
693 file."
694 :group 'speedbar
695 :version "21.1"
696 :type '(repeat (regexp :tag "Extension Regexp"))
697 :set (lambda (sym val)
698 (setq speedbar-supported-extension-expressions val
699 speedbar-file-regexp (speedbar-extension-list-to-regex val))))
701 (defcustom speedbar-scan-subdirs nil
702 "*Non-nil means speedbar will check if subdirs are empty.
703 That way you don't have to click on them to find out. But this
704 incurs extra I/O, hence it slows down directory display
705 proportionally to the number of subdirs."
706 :group 'speedbar
707 :type 'boolean
708 :version 21.4)
710 (defun speedbar-add-supported-extension (extension)
711 "Add EXTENSION as a new supported extension for speedbar tagging.
712 This should start with a `.' if it is not a complete file name, and
713 the dot should NOT be quoted in with \\. Other regular expression
714 matchers are allowed however. EXTENSION may be a single string or a
715 list of strings."
716 (interactive "sExtension: ")
717 (if (not (listp extension)) (setq extension (list extension)))
718 (while extension
719 (if (member (car extension) speedbar-supported-extension-expressions)
721 (setq speedbar-supported-extension-expressions
722 (cons (car extension) speedbar-supported-extension-expressions)))
723 (setq extension (cdr extension)))
724 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
725 speedbar-supported-extension-expressions)))
727 (defun speedbar-add-ignored-path-regexp (path-expression)
728 "Add PATH-EXPRESSION as a new ignored path for speedbar tracking.
729 This function will modify `speedbar-ignored-path-regexp' and add
730 PATH-EXPRESSION to `speedbar-ignored-path-expressions'."
731 (interactive "sPath regex: ")
732 (if (not (listp path-expression))
733 (setq path-expression (list path-expression)))
734 (while path-expression
735 (if (member (car path-expression) speedbar-ignored-path-expressions)
737 (setq speedbar-ignored-path-expressions
738 (cons (car path-expression) speedbar-ignored-path-expressions)))
739 (setq path-expression (cdr path-expression)))
740 (setq speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
741 speedbar-ignored-path-expressions)))
743 ;; If we don't have custom, then we set it here by hand.
744 (if (not (fboundp 'custom-declare-variable))
745 (setq speedbar-file-regexp (speedbar-extension-list-to-regex
746 speedbar-supported-extension-expressions)
747 speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
748 speedbar-ignored-path-expressions)))
750 (defvar speedbar-update-flag (and
751 (or (fboundp 'run-with-idle-timer)
752 (fboundp 'start-itimer)
753 (boundp 'post-command-idle-hook))
754 (if (fboundp 'display-graphic-p)
755 (display-graphic-p)
756 window-system))
757 "*Non-nil means to automatically update the display.
758 When this is nil then speedbar will not follow the attached frame's path.
759 When speedbar is active, use:
761 \\<speedbar-key-map> `\\[speedbar-toggle-updates]'
763 to toggle this value.")
765 (defvar speedbar-syntax-table nil
766 "Syntax-table used on the speedbar.")
768 (if speedbar-syntax-table
770 (setq speedbar-syntax-table (make-syntax-table))
771 ;; turn off paren matching around here.
772 (modify-syntax-entry ?\' " " speedbar-syntax-table)
773 (modify-syntax-entry ?\" " " speedbar-syntax-table)
774 (modify-syntax-entry ?( " " speedbar-syntax-table)
775 (modify-syntax-entry ?) " " speedbar-syntax-table)
776 (modify-syntax-entry ?{ " " speedbar-syntax-table)
777 (modify-syntax-entry ?} " " speedbar-syntax-table)
778 (modify-syntax-entry ?[ " " speedbar-syntax-table)
779 (modify-syntax-entry ?] " " speedbar-syntax-table))
781 (defvar speedbar-key-map nil
782 "Keymap used in speedbar buffer.")
784 (if speedbar-key-map
786 (setq speedbar-key-map (make-keymap))
787 (suppress-keymap speedbar-key-map t)
789 ;; control
790 (define-key speedbar-key-map "g" 'speedbar-refresh)
791 (define-key speedbar-key-map "t" 'speedbar-toggle-updates)
792 (define-key speedbar-key-map "q" 'speedbar-close-frame)
793 (define-key speedbar-key-map "Q" 'delete-frame)
795 ;; navigation
796 (define-key speedbar-key-map "n" 'speedbar-next)
797 (define-key speedbar-key-map "p" 'speedbar-prev)
798 (define-key speedbar-key-map "\M-n" 'speedbar-restricted-next)
799 (define-key speedbar-key-map "\M-p" 'speedbar-restricted-prev)
800 (define-key speedbar-key-map "\C-\M-n" 'speedbar-forward-list)
801 (define-key speedbar-key-map "\C-\M-p" 'speedbar-backward-list)
802 (define-key speedbar-key-map " " 'speedbar-scroll-up)
803 (define-key speedbar-key-map [delete] 'speedbar-scroll-down)
805 ;; Short cuts I happen to find useful
806 (define-key speedbar-key-map "r"
807 (lambda () (interactive)
808 (speedbar-change-initial-expansion-list
809 speedbar-previously-used-expansion-list-name)))
810 (define-key speedbar-key-map "b"
811 (lambda () (interactive)
812 (speedbar-change-initial-expansion-list "quick buffers")))
813 (define-key speedbar-key-map "f"
814 (lambda () (interactive)
815 (speedbar-change-initial-expansion-list "files")))
817 ;; Overrides
818 (substitute-key-definition 'switch-to-buffer
819 'speedbar-switch-buffer-attached-frame
820 speedbar-key-map global-map)
822 (if speedbar-xemacsp
823 (progn
824 ;; mouse bindings so we can manipulate the items on each line
825 (define-key speedbar-key-map 'button2 'speedbar-click)
826 (define-key speedbar-key-map '(shift button2) 'speedbar-power-click)
827 ;; Info doc fix from Bob Weiner
828 (if (featurep 'infodoc)
830 (define-key speedbar-key-map 'button3 'speedbar-xemacs-popup-kludge))
831 (define-key speedbar-key-map '(meta button3) 'speedbar-mouse-item-info)
834 ;; mouse bindings so we can manipulate the items on each line
835 (define-key speedbar-key-map [down-mouse-1] 'speedbar-double-click)
836 (define-key speedbar-key-map [mouse-2] 'speedbar-click)
837 ;; This is the power click for new frames, or refreshing a cache
838 (define-key speedbar-key-map [S-mouse-2] 'speedbar-power-click)
839 ;; This adds a small unecessary visual effect
840 ;;(define-key speedbar-key-map [down-mouse-2] 'speedbar-quick-mouse)
841 (define-key speedbar-key-map [M-mouse-2] 'speedbar-mouse-item-info)
843 (define-key speedbar-key-map [down-mouse-3] 'speedbar-emacs-popup-kludge)
845 ;; This lets the user scroll as if we had a scrollbar... well maybe not
846 (define-key speedbar-key-map [mode-line mouse-2] 'speedbar-mouse-hscroll)
847 ;; another handy place users might click to get our menu.
848 (define-key speedbar-key-map [mode-line down-mouse-1]
849 'speedbar-emacs-popup-kludge)
851 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
852 (define-key speedbar-key-map [C-down-mouse-1] 'speedbar-hack-buffer-menu)
854 ;; Lastly, we want to track the mouse. Play here
855 (define-key speedbar-key-map [mouse-movement] 'speedbar-track-mouse)
858 (defun speedbar-make-specialized-keymap ()
859 "Create a keymap for use with a speedbar major or minor display mode.
860 This basically creates a sparse keymap, and makes it's parent be
861 `speedbar-key-map'."
862 (let ((k (make-sparse-keymap)))
863 (set-keymap-parent k speedbar-key-map)
866 (defvar speedbar-file-key-map nil
867 "Keymap used in speedbar buffer while files are displayed.")
869 (if speedbar-file-key-map
871 (setq speedbar-file-key-map (speedbar-make-specialized-keymap))
873 ;; Basic tree features
874 (define-key speedbar-file-key-map "e" 'speedbar-edit-line)
875 (define-key speedbar-file-key-map "\C-m" 'speedbar-edit-line)
876 (define-key speedbar-file-key-map "+" 'speedbar-expand-line)
877 (define-key speedbar-file-key-map "=" 'speedbar-expand-line)
878 (define-key speedbar-file-key-map "-" 'speedbar-contract-line)
880 ;; file based commands
881 (define-key speedbar-file-key-map "U" 'speedbar-up-directory)
882 (define-key speedbar-file-key-map "I" 'speedbar-item-info)
883 (define-key speedbar-file-key-map "B" 'speedbar-item-byte-compile)
884 (define-key speedbar-file-key-map "L" 'speedbar-item-load)
885 (define-key speedbar-file-key-map "C" 'speedbar-item-copy)
886 (define-key speedbar-file-key-map "D" 'speedbar-item-delete)
887 (define-key speedbar-file-key-map "O" 'speedbar-item-object-delete)
888 (define-key speedbar-file-key-map "R" 'speedbar-item-rename)
891 (defvar speedbar-easymenu-definition-base
892 (append
893 '("Speedbar"
894 ["Update" speedbar-refresh t]
895 ["Auto Update" speedbar-toggle-updates
896 :style toggle :selected speedbar-update-flag])
897 (if (and (or (fboundp 'defimage)
898 (fboundp 'make-image-specifier))
899 (if (fboundp 'display-graphic-p)
900 (display-graphic-p)
901 window-system))
902 (list
903 ["Use Images" speedbar-toggle-images
904 :style toggle :selected speedbar-use-images]))
906 "Base part of the speedbar menu.")
908 (defvar speedbar-easymenu-definition-special
909 '(["Edit Item On Line" speedbar-edit-line t]
910 ["Show All Files" speedbar-toggle-show-all-files
911 :style toggle :selected speedbar-show-unknown-files]
912 ["Expand File Tags" speedbar-expand-line
913 (save-excursion (beginning-of-line)
914 (looking-at "[0-9]+: *.\\+. "))]
915 ["Flush Cache & Expand" speedbar-flush-expand-line
916 (save-excursion (beginning-of-line)
917 (looking-at "[0-9]+: *.\\+. "))]
918 ["Contract File Tags" speedbar-contract-line
919 (save-excursion (beginning-of-line)
920 (looking-at "[0-9]+: *.-. "))]
921 ; ["Sort Tags" speedbar-toggle-sorting
922 ; :style toggle :selected speedbar-sort-tags]
923 "----"
924 ["File/Tag Information" speedbar-item-info t]
925 ["Load Lisp File" speedbar-item-load
926 (save-excursion
927 (beginning-of-line)
928 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
929 ["Byte Compile File" speedbar-item-byte-compile
930 (save-excursion
931 (beginning-of-line)
932 (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
933 ["Copy File" speedbar-item-copy
934 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *\\["))]
935 ["Rename File" speedbar-item-rename
936 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
937 ["Delete File" speedbar-item-delete
938 (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
939 ["Delete Object" speedbar-item-object-delete
940 (save-excursion (beginning-of-line)
941 (looking-at "[0-9]+: *\\[[+-]\\] [^ \n]+ \\*?[!#]$"))]
943 "Additional menu items while in file-mode.")
945 (defvar speedbar-easymenu-definition-trailer
946 (append
947 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
948 (list ["Customize..." speedbar-customize t]))
949 (list
950 ["Close" speedbar-close-frame t]
951 ["Quit" delete-frame t] ))
952 "Menu items appearing at the end of the speedbar menu.")
954 (defvar speedbar-desired-buffer nil
955 "Non-nil when speedbar is showing buttons specific to a special mode.
956 In this case it is the originating buffer.")
957 (defvar speedbar-buffer nil
958 "The buffer displaying the speedbar.")
959 (defvar speedbar-frame nil
960 "The frame displaying speedbar.")
961 (defvar speedbar-cached-frame nil
962 "The frame that was last created, then removed from the display.")
963 (defvar speedbar-full-text-cache nil
964 "The last open directory is saved in its entirety for ultra-fast switching.")
965 (defvar speedbar-timer nil
966 "The speedbar timer used for updating the buffer.")
967 (defvar speedbar-attached-frame nil
968 "The frame which started speedbar mode.
969 This is the frame from which all data displayed in the speedbar is
970 gathered, and in which files and such are displayed.")
972 (defvar speedbar-last-selected-file nil
973 "The last file which was selected in speedbar buffer.")
975 (defvar speedbar-shown-directories nil
976 "Maintain list of directories simultaneously open in the current speedbar.")
978 (defvar speedbar-directory-contents-alist nil
979 "An association list of directories and their contents.
980 Each sublist was returned by `speedbar-file-lists'. This list is
981 maintained to speed up the refresh rate when switching between
982 directories.")
984 (defvar speedbar-power-click nil
985 "Never set this by hand. Value is t when S-mouse activity occurs.")
988 ;;; Compatibility
990 (if (fboundp 'frame-parameter)
992 (defalias 'speedbar-frame-parameter 'frame-parameter)
994 (defun speedbar-frame-parameter (frame parameter)
995 "Return FRAME's PARAMETER value."
996 (cdr (assoc parameter (frame-parameters frame)))))
998 (if (fboundp 'make-overlay)
999 (progn
1000 (defalias 'speedbar-make-overlay 'make-overlay)
1001 (defalias 'speedbar-overlay-put 'overlay-put)
1002 (defalias 'speedbar-delete-overlay 'delete-overlay)
1003 (defalias 'speedbar-overlay-start 'overlay-start)
1004 (defalias 'speedbar-overlay-end 'overlay-end)
1005 (defalias 'speedbar-mode-line-update 'force-mode-line-update))
1006 (defalias 'speedbar-make-overlay 'make-extent)
1007 (defalias 'speedbar-overlay-put 'set-extent-property)
1008 (defalias 'speedbar-delete-overlay 'delete-extent)
1009 (defalias 'speedbar-overlay-start 'extent-start)
1010 (defalias 'speedbar-overlay-end 'extent-end)
1011 (defalias 'speedbar-mode-line-update 'redraw-modeline))
1013 ;;; Mode definitions/ user commands
1016 ;;;###autoload
1017 (defalias 'speedbar 'speedbar-frame-mode)
1018 ;;;###autoload
1019 (defun speedbar-frame-mode (&optional arg)
1020 "Enable or disable speedbar. Positive ARG means turn on, negative turn off.
1021 nil means toggle. Once the speedbar frame is activated, a buffer in
1022 `speedbar-mode' will be displayed. Currently, only one speedbar is
1023 supported at a time.
1024 `speedbar-before-popup-hook' is called before popping up the speedbar frame.
1025 `speedbar-before-delete-hook' is called before the frame is deleted."
1026 (interactive "P")
1027 ;; toggle frame on and off.
1028 (if (not arg) (if (and (frame-live-p speedbar-frame)
1029 (frame-visible-p speedbar-frame))
1030 (setq arg -1) (setq arg 1)))
1031 ;; turn the frame off on neg number
1032 (if (and (numberp arg) (< arg 0))
1033 (progn
1034 (run-hooks 'speedbar-before-delete-hook)
1035 (if (and speedbar-frame (frame-live-p speedbar-frame))
1036 (progn
1037 (setq speedbar-cached-frame speedbar-frame)
1038 (make-frame-invisible speedbar-frame)))
1039 (setq speedbar-frame nil)
1040 (speedbar-set-timer nil)
1041 ;; Used to delete the buffer. This has the annoying affect of
1042 ;; preventing whatever took its place from ever appearing
1043 ;; as the default after a C-x b was typed
1044 ;;(if (bufferp speedbar-buffer)
1045 ;; (kill-buffer speedbar-buffer))
1047 ;; Set this as our currently attached frame
1048 (setq speedbar-attached-frame (selected-frame))
1049 (run-hooks 'speedbar-before-popup-hook)
1050 ;; Get the frame to work in
1051 (if (frame-live-p speedbar-cached-frame)
1052 (progn
1053 (setq speedbar-frame speedbar-cached-frame)
1054 (make-frame-visible speedbar-frame)
1055 ;; Get the buffer to play with
1056 (speedbar-mode)
1057 (select-frame speedbar-frame)
1058 (if (not (eq (current-buffer) speedbar-buffer))
1059 (switch-to-buffer speedbar-buffer))
1060 (set-window-dedicated-p (selected-window) t)
1061 (raise-frame speedbar-frame)
1062 (speedbar-set-timer speedbar-update-speed)
1064 (if (frame-live-p speedbar-frame)
1065 (raise-frame speedbar-frame)
1066 (setq speedbar-frame
1067 (if speedbar-xemacsp
1068 ;; Only guess height if it is not specified.
1069 (if (member 'height speedbar-frame-plist)
1070 (make-frame speedbar-frame-plist)
1071 (make-frame (nconc (list 'height
1072 (speedbar-needed-height))
1073 speedbar-frame-plist)))
1074 (let* ((mh (speedbar-frame-parameter nil 'menu-bar-lines))
1075 (cfx (speedbar-frame-parameter nil 'left))
1076 (cfy (speedbar-frame-parameter nil 'top))
1077 (cfw (frame-pixel-width))
1078 (params
1079 ;; Only add a guessed height if one is not specified
1080 ;; in the input parameters.
1081 (if (assoc 'height speedbar-frame-parameters)
1082 speedbar-frame-parameters
1083 (append
1084 speedbar-frame-parameters
1085 (list (cons 'height (+ mh (frame-height)))))))
1086 (frame
1087 (if (or (< emacs-major-version 20)
1088 (not (eq window-system 'x)))
1089 (make-frame params)
1090 (let ((x-pointer-shape x-pointer-top-left-arrow)
1091 (x-sensitive-text-pointer-shape
1092 x-pointer-hand2))
1093 (make-frame params)))))
1094 ;; Position speedbar frame.
1095 (if (or (not window-system) (eq window-system 'pc)
1096 (assoc 'left speedbar-frame-parameters)
1097 (assoc 'top speedbar-frame-parameters))
1098 ;; Do no positioning if not on a windowing system,
1099 ;; or if left/top were specified in the parameters.
1100 frame
1101 (let ((cfx
1102 (if (not (consp cfx))
1104 ;; If cfx is a list, that means we grow
1105 ;; from a specific edge of the display.
1106 ;; Convert that to the distance from the
1107 ;; left side of the display.
1108 (if (eq (car cfx) '-)
1109 ;; A - means distance from the right edge
1110 ;; of the display, or DW - cfx - framewidth
1111 (- (x-display-pixel-width) (car (cdr cfx))
1112 (frame-pixel-width))
1113 (car (cdr cfx))))))
1114 (modify-frame-parameters
1115 frame
1116 (list
1117 (cons
1118 'left
1119 ;; Decide which side to put it
1120 ;; on. 200 is just a buffer
1121 ;; for the left edge of the
1122 ;; screen. The extra 10 is just
1123 ;; dressings for window decorations.
1124 (let ((sfw (frame-pixel-width frame)))
1125 (let ((left-guess (- cfx 10 sfw))
1126 (right-guess (+ cfx cfw 5)))
1127 (let ((left-margin left-guess)
1128 (right-margin
1129 (- (x-display-pixel-width)
1130 right-guess 5 sfw)))
1131 (cond ((>= left-margin 0) left-guess)
1132 ((>= right-margin 0) right-guess)
1133 ;; otherwise choose side we overlap less
1134 ((> left-margin right-margin) 0)
1135 (t (- (x-display-pixel-width) sfw 5)))))))
1136 (cons 'top cfy)))
1137 frame)))))
1138 ;; reset the selection variable
1139 (setq speedbar-last-selected-file nil)
1140 ;; Put the buffer into the frame
1141 (save-window-excursion
1142 ;; Get the buffer to play with
1143 (speedbar-mode)
1144 (select-frame speedbar-frame)
1145 (switch-to-buffer speedbar-buffer)
1146 (set-window-dedicated-p (selected-window) t))
1147 (if (and (or (null window-system) (eq window-system 'pc))
1148 (fboundp 'set-frame-name))
1149 (progn
1150 (select-frame speedbar-frame)
1151 (set-frame-name "Speedbar")))
1152 (speedbar-set-timer speedbar-update-speed)))))
1154 ;;;###autoload
1155 (defun speedbar-get-focus ()
1156 "Change frame focus to or from the speedbar frame.
1157 If the selected frame is not speedbar, then speedbar frame is
1158 selected. If the speedbar frame is active, then select the attached frame."
1159 (interactive)
1160 (if (eq (selected-frame) speedbar-frame)
1161 (if (frame-live-p speedbar-attached-frame)
1162 (select-frame speedbar-attached-frame))
1163 ;; If updates are off, then refresh the frame (they want it now...)
1164 (if (not speedbar-update-flag)
1165 (let ((speedbar-update-flag t))
1166 (speedbar-timer-fn)))
1167 ;; make sure we have a frame
1168 (if (not (frame-live-p speedbar-frame)) (speedbar-frame-mode 1))
1169 ;; go there
1170 (select-frame speedbar-frame)
1172 (other-frame 0))
1174 (defun speedbar-close-frame ()
1175 "Turn off a currently active speedbar."
1176 (interactive)
1177 (speedbar-frame-mode -1)
1178 (select-frame speedbar-attached-frame)
1179 (other-frame 0))
1181 (defun speedbar-switch-buffer-attached-frame (&optional buffer)
1182 "Switch to BUFFER in speedbar's attached frame, and raise that frame.
1183 This overrides the default behavior of `switch-to-buffer' which is
1184 broken because of the dedicated speedbar frame."
1185 (interactive)
1186 ;; Assume we are in the speedbar frame.
1187 (speedbar-get-focus)
1188 ;; Now switch buffers
1189 (if buffer
1190 (switch-to-buffer buffer)
1191 (call-interactively 'switch-to-buffer nil nil)))
1193 (defmacro speedbar-frame-width ()
1194 "Return the width of the speedbar frame in characters.
1195 nil if it doesn't exist."
1196 '(frame-width speedbar-frame))
1198 ;; XEmacs function only.
1199 (defun speedbar-needed-height (&optional frame)
1200 "The needed height for the tool bar FRAME (in characters)."
1201 (or frame (setq frame (selected-frame)))
1202 ;; The 1 is the missing modeline/minibuffer
1203 (+ 1 (/ (frame-pixel-height frame)
1204 (face-height 'default frame))))
1206 (defun speedbar-mode ()
1207 "Major mode for managing a display of directories and tags.
1208 \\<speedbar-key-map>
1209 The first line represents the default path of the speedbar frame.
1210 Each directory segment is a button which jumps speedbar's default
1211 directory to that path. Buttons are activated by clicking `\\[speedbar-click]'.
1212 In some situations using `\\[speedbar-power-click]' is a `power click' which will
1213 rescan cached items, or pop up new frames.
1215 Each line starting with <+> represents a directory. Click on the <+>
1216 to insert the directory listing into the current tree. Click on the
1217 <-> to retract that list. Click on the directory name to go to that
1218 directory as the default.
1220 Each line starting with [+] is a file. If the variable
1221 `speedbar-show-unknown-files' is t, the lines starting with [?] are
1222 files which don't have imenu support, but are not expressly ignored.
1223 Files are completely ignored if they match `speedbar-file-unshown-regexp'
1224 which is generated from `completion-ignored-extensions'.
1226 Files with a `*' character after their name are files checked out of a
1227 version control system. (Currently only RCS is supported.) New
1228 version control systems can be added by examining the documentation
1229 for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'.
1231 Files with a `#' or `!' character after them are source files that
1232 have an object file associated with them. The `!' indicates that the
1233 files is out of date. You can control what source/object associations
1234 exist through the variable `speedbar-obj-alist'.
1236 Click on the [+] to display a list of tags from that file. Click on
1237 the [-] to retract the list. Click on the file name to edit the file
1238 in the attached frame.
1240 If you open tags, you might find a node starting with {+}, which is a
1241 category of tags. Click the {+} to expand the category. Jump-able
1242 tags start with >. Click the name of the tag to go to that position
1243 in the selected file.
1245 \\{speedbar-key-map}"
1246 ;; NOT interactive
1247 (save-excursion
1248 (setq speedbar-buffer (set-buffer (get-buffer-create " SPEEDBAR")))
1249 (kill-all-local-variables)
1250 (setq major-mode 'speedbar-mode)
1251 (setq mode-name "Speedbar")
1252 (set-syntax-table speedbar-syntax-table)
1253 (setq font-lock-keywords nil) ;; no font-locking please
1254 (setq truncate-lines t)
1255 (make-local-variable 'frame-title-format)
1256 (setq frame-title-format "Speedbar")
1257 ;; Set this up special just for the speedbar buffer
1258 ;; Terminal minibuffer stuff does not require this.
1259 (if (and window-system (not (eq window-system 'pc))
1260 (null default-minibuffer-frame))
1261 (progn
1262 (make-local-variable 'default-minibuffer-frame)
1263 (setq default-minibuffer-frame speedbar-attached-frame)))
1264 ;; Correct use of `temp-buffer-show-function': Bob Weiner
1265 (if (and (boundp 'temp-buffer-show-hook)
1266 (boundp 'temp-buffer-show-function))
1267 (progn (make-local-variable 'temp-buffer-show-hook)
1268 (setq temp-buffer-show-hook temp-buffer-show-function)))
1269 (make-local-variable 'temp-buffer-show-function)
1270 (setq temp-buffer-show-function 'speedbar-temp-buffer-show-function)
1271 (if speedbar-xemacsp
1272 (progn
1273 ;; Argh! mouse-track-click-hook doesn't understand the
1274 ;; make-local-hook conventions.
1275 (make-local-variable 'mouse-track-click-hook)
1276 (add-hook 'mouse-track-click-hook
1277 (lambda (event count)
1278 (if (/= (event-button event) 1)
1279 nil ; Do normal operations.
1280 (cond ((eq count 1)
1281 (speedbar-quick-mouse event))
1282 ((or (eq count 2)
1283 (eq count 3))
1284 (speedbar-mouse-set-point event)
1285 (speedbar-do-function-pointer)
1286 (speedbar-quick-mouse event)))
1287 ;; Don't do normal operations.
1288 t)))))
1289 (add-hook 'kill-buffer-hook (lambda () (let ((skilling (boundp 'skilling)))
1290 (if skilling
1292 (if (eq (current-buffer)
1293 speedbar-buffer)
1294 (speedbar-frame-mode -1)))))
1295 t t)
1296 (toggle-read-only 1)
1297 (speedbar-set-mode-line-format)
1298 (if speedbar-xemacsp
1299 (with-no-warnings
1300 (set (make-local-variable 'mouse-motion-handler)
1301 'speedbar-track-mouse-xemacs))
1302 (if speedbar-track-mouse-flag
1303 (set (make-local-variable 'track-mouse) t)) ;this could be messy.
1304 (setq auto-show-mode nil)) ;no auto-show for Emacs
1305 (run-hooks 'speedbar-mode-hook))
1306 (speedbar-update-contents)
1307 speedbar-buffer)
1309 (defmacro speedbar-with-attached-buffer (&rest forms)
1310 "Execute FORMS in the attached frame's special buffer.
1311 Optionally select that frame if necessary."
1312 `(save-selected-window
1313 (speedbar-set-timer speedbar-update-speed)
1314 (select-frame speedbar-attached-frame)
1315 ,@forms
1316 (speedbar-maybee-jump-to-attached-frame)))
1318 (defun speedbar-message (fmt &rest args)
1319 "Like message, but for use in the speedbar frame.
1320 Argument FMT is the format string, and ARGS are the arguments for message."
1321 (save-selected-window
1322 (select-frame speedbar-attached-frame)
1323 (apply 'message fmt args)))
1325 (defun speedbar-y-or-n-p (prompt)
1326 "Like `y-or-n-p', but for use in the speedbar frame.
1327 Argument PROMPT is the prompt to use."
1328 (save-selected-window
1329 (if (and default-minibuffer-frame (not (eq default-minibuffer-frame
1330 speedbar-attached-frame)))
1331 (select-frame speedbar-attached-frame))
1332 (y-or-n-p prompt)))
1334 (defun speedbar-show-info-under-mouse (&optional event)
1335 "Call the info function for the line under the mouse.
1336 Optional EVENT is currently not used."
1337 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1338 (if (equal (car pos) speedbar-frame)
1339 (save-excursion
1340 (save-window-excursion
1341 (apply 'set-mouse-position pos)
1342 (speedbar-item-info))))))
1344 (defun speedbar-set-mode-line-format ()
1345 "Set the format of the mode line based on the current speedbar environment.
1346 This gives visual indications of what is up. It EXPECTS the speedbar
1347 frame and window to be the currently active frame and window."
1348 (if (and (frame-live-p speedbar-frame)
1349 (or (not speedbar-xemacsp)
1350 (with-no-warnings
1351 (specifier-instance has-modeline-p))))
1352 (save-excursion
1353 (set-buffer speedbar-buffer)
1354 (let* ((w (or (speedbar-frame-width) 20))
1355 (p1 "<<")
1356 (p5 ">>")
1357 (p3 (if speedbar-update-flag "SPEEDBAR" "SLOWBAR"))
1358 (blank (- w (length p1) (length p3) (length p5)
1359 (if line-number-mode 4 0)))
1360 (p2 (if (> blank 0)
1361 (make-string (/ blank 2) ? )
1362 ""))
1363 (p4 (if (> blank 0)
1364 (make-string (+ (/ blank 2) (% blank 2)) ? )
1365 ""))
1367 (if line-number-mode
1368 (list (concat p1 p2 p3) '(line-number-mode " %3l")
1369 (concat p4 p5))
1370 (list (concat p1 p2 p3 p4 p5)))))
1371 (if (not (equal mode-line-format tf))
1372 (progn
1373 (setq mode-line-format tf)
1374 (speedbar-mode-line-update)))))))
1376 (defun speedbar-temp-buffer-show-function (buffer)
1377 "Placed in the variable `temp-buffer-show-function' in `speedbar-mode'.
1378 If a user requests help using \\[help-command] <Key> the temp BUFFER will be
1379 redirected into a window on the attached frame."
1380 (if speedbar-attached-frame (select-frame speedbar-attached-frame))
1381 (pop-to-buffer buffer nil)
1382 (other-window -1)
1383 ;; Fix for using this hook on some platforms: Bob Weiner
1384 (cond ((not speedbar-xemacsp)
1385 (run-hooks 'temp-buffer-show-hook))
1386 ((fboundp 'run-hook-with-args)
1387 (run-hook-with-args 'temp-buffer-show-hook buffer))
1388 ((and (boundp 'temp-buffer-show-hook)
1389 (listp temp-buffer-show-hook))
1390 (mapcar (function (lambda (hook) (funcall hook buffer)))
1391 temp-buffer-show-hook))))
1393 (defvar speedbar-previous-menu nil
1394 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1396 (defun speedbar-reconfigure-keymaps ()
1397 "Reconfigure the menu-bar in a speedbar frame.
1398 Different menu items are displayed depending on the current display mode
1399 and the existence of packages."
1400 (let ((md (append
1401 speedbar-easymenu-definition-base
1402 (if speedbar-shown-directories
1403 ;; file display mode version
1404 (speedbar-initial-menu)
1405 (save-excursion
1406 (select-frame speedbar-attached-frame)
1407 (if (local-variable-p
1408 'speedbar-easymenu-definition-special
1409 (current-buffer))
1410 ;; If bound locally, we can use it
1411 speedbar-easymenu-definition-special)))
1412 ;; Dynamic menu stuff
1413 '("-")
1414 (list (cons "Displays"
1415 (let ((displays nil)
1416 (alist speedbar-initial-expansion-mode-alist))
1417 (while alist
1418 (setq displays
1419 (cons
1420 (vector
1421 (capitalize (car (car alist)))
1422 (list
1423 'speedbar-change-initial-expansion-list
1424 (car (car alist)))
1426 displays))
1427 (setq alist (cdr alist)))
1428 displays)))
1429 ;; The trailer
1430 speedbar-easymenu-definition-trailer))
1431 (localmap (save-excursion
1432 (let ((cf (selected-frame)))
1433 (prog2
1434 (select-frame speedbar-attached-frame)
1435 (if (local-variable-p
1436 'speedbar-special-mode-key-map
1437 (current-buffer))
1438 speedbar-special-mode-key-map)
1439 (select-frame cf))))))
1440 (save-excursion
1441 (set-buffer speedbar-buffer)
1442 (use-local-map (or localmap
1443 (speedbar-initial-keymap)
1444 ;; This creates a small keymap we can glom the
1445 ;; menu adjustments into.
1446 (speedbar-make-specialized-keymap)))
1447 ;; Delete the old menu if applicable.
1448 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1449 (setq speedbar-previous-menu md)
1450 ;; Now add the new menu
1451 (if (not speedbar-xemacsp)
1452 (easy-menu-define speedbar-menu-map (current-local-map)
1453 "Speedbar menu" md)
1454 (easy-menu-add md (current-local-map))
1455 (set-buffer-menubar (list md))))
1456 (run-hooks 'speedbar-reconfigure-keymaps-hook)))
1459 ;;; User Input stuff
1462 ;; XEmacs: this can be implemented using modeline keymaps, but there
1463 ;; is no use, as we have horizontal scrollbar (as the docstring
1464 ;; hints.)
1465 (defun speedbar-mouse-hscroll (e)
1466 "Read a mouse event E from the mode line, and horizontally scroll.
1467 If the mouse is being clicked on the far left, or far right of the
1468 mode-line. This is only useful for non-XEmacs."
1469 (interactive "e")
1470 (let* ((xp (car (nth 2 (car (cdr e)))))
1471 (cpw (/ (frame-pixel-width)
1472 (frame-width)))
1473 (oc (1+ (/ xp cpw)))
1475 (cond ((< oc 3)
1476 (scroll-left 2))
1477 ((> oc (- (window-width) 3))
1478 (scroll-right 2))
1479 (t (speedbar-message
1480 "Click on the edge of the modeline to scroll left/right")))
1481 ;;(speedbar-message "X: Pixel %d Char Pixels %d On char %d" xp cpw oc)
1484 (defun speedbar-customize ()
1485 "Customize speedbar using the Custom package."
1486 (interactive)
1487 (let ((sf (selected-frame)))
1488 (select-frame speedbar-attached-frame)
1489 (customize-group 'speedbar)
1490 (select-frame sf))
1491 (speedbar-maybee-jump-to-attached-frame))
1493 (defun speedbar-track-mouse (event)
1494 "For motion EVENT, display info about the current line."
1495 (interactive "e")
1496 (if (not speedbar-track-mouse-flag)
1498 (save-excursion
1499 (let ((char (nth 1 (car (cdr event)))))
1500 (if (not (numberp char))
1501 (speedbar-message nil)
1502 (goto-char char)
1503 ;; (speedbar-message "%S" event)
1504 (speedbar-item-info)
1505 )))))
1507 (defun speedbar-track-mouse-xemacs (event)
1508 "For motion EVENT, display info about the current line."
1509 (if (functionp (default-value 'mouse-motion-handler))
1510 (funcall (default-value 'mouse-motion-handler) event))
1511 (if speedbar-track-mouse-flag
1512 (save-excursion
1513 (save-window-excursion
1514 (condition-case ()
1515 (progn (mouse-set-point event)
1516 ;; Prevent focus-related bugs.
1517 (if (eq major-mode 'speedbar-mode)
1518 (speedbar-item-info)))
1519 (error nil))))))
1521 ;; In XEmacs, we make popup menus work on the item over mouse (as
1522 ;; opposed to where the point happens to be.) We attain this by
1523 ;; temporarily moving the point to that place.
1524 ;; Hrvoje Niksic <hniksic@srce.hr>
1525 (defun speedbar-xemacs-popup-kludge (event)
1526 "Pop up a menu related to the clicked on item.
1527 Must be bound to EVENT."
1528 (interactive "e")
1529 (select-frame speedbar-frame)
1530 (save-excursion
1531 (goto-char (event-closest-point event))
1532 (beginning-of-line)
1533 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
1534 (save-excursion (beginning-of-line) (point)))))
1535 (popup-mode-menu)
1536 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
1537 ;; menu functions) return immediately.
1538 (let (new)
1539 (while (not (misc-user-event-p (setq new (next-event))))
1540 (dispatch-event new))
1541 (dispatch-event new))))
1543 (defun speedbar-emacs-popup-kludge (e)
1544 "Pop up a menu related to the clicked on item.
1545 Must be bound to event E."
1546 (interactive "e")
1547 (save-excursion
1548 (mouse-set-point e)
1549 ;; This gets the cursor where the user can see it.
1550 (if (not (bolp)) (forward-char -1))
1551 (sit-for 0)
1552 (mouse-major-mode-menu e nil)))
1554 (defun speedbar-hack-buffer-menu (e)
1555 "Control mouse 1 is buffer menu.
1556 This hack overrides it so that the right thing happens in the main
1557 Emacs frame, not in the speedbar frame.
1558 Argument E is the event causing this activity."
1559 (interactive "e")
1560 (let ((fn (lookup-key global-map (if speedbar-xemacsp
1561 '(control button1)
1562 [C-down-mouse-1])))
1563 (newbuff nil))
1564 (unwind-protect
1565 (save-excursion
1566 (set-window-dedicated-p (selected-window) nil)
1567 (call-interactively fn)
1568 (setq newbuff (current-buffer)))
1569 (switch-to-buffer speedbar-buffer)
1570 (set-window-dedicated-p (selected-window) t))
1571 (if (not (eq newbuff speedbar-buffer))
1572 (speedbar-with-attached-buffer
1573 (switch-to-buffer newbuff)))))
1575 (defun speedbar-next (arg)
1576 "Move to the next ARGth line in a speedbar buffer."
1577 (interactive "p")
1578 (forward-line (or arg 1))
1579 (speedbar-item-info)
1580 (speedbar-position-cursor-on-line))
1582 (defun speedbar-prev (arg)
1583 "Move to the previous ARGth line in a speedbar buffer."
1584 (interactive "p")
1585 (speedbar-next (if arg (- arg) -1)))
1587 (defun speedbar-restricted-move (arg)
1588 "Move to the next ARGth line in a speedbar buffer at the same depth.
1589 This means that movement is restricted to a subnode, and that siblings
1590 of intermediate nodes are skipped."
1591 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1592 ;; First find the extent for which we are allowed to move.
1593 (let ((depth (save-excursion (beginning-of-line)
1594 (if (looking-at "[0-9]+:")
1595 (string-to-int (match-string 0))
1596 0)))
1597 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1598 (lastmatch (point)))
1599 (while (/= arg 0)
1600 (forward-line (- crement))
1601 (let ((subdepth (save-excursion (beginning-of-line)
1602 (if (looking-at "[0-9]+:")
1603 (string-to-int (match-string 0))
1604 0))))
1605 (cond ((or (< subdepth depth)
1606 (progn (end-of-line) (eobp))
1607 (progn (beginning-of-line) (bobp)))
1608 ;; We have reached the end of this block.
1609 (goto-char lastmatch)
1610 (setq arg 0)
1611 (error "End of sub-list"))
1612 ((= subdepth depth)
1613 (setq lastmatch (point)
1614 arg (+ arg crement))))))
1615 (speedbar-position-cursor-on-line)))
1617 (defun speedbar-restricted-next (arg)
1618 "Move to the next ARGth line in a speedbar buffer at the same depth.
1619 This means that movement is restricted to a subnode, and that siblings
1620 of intermediate nodes are skipped."
1621 (interactive "p")
1622 (speedbar-restricted-move (or arg 1))
1623 (speedbar-item-info))
1626 (defun speedbar-restricted-prev (arg)
1627 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1628 This means that movement is restricted to a subnode, and that siblings
1629 of intermediate nodes are skipped."
1630 (interactive "p")
1631 (speedbar-restricted-move (if arg (- arg) -1))
1632 (speedbar-item-info))
1634 (defun speedbar-navigate-list (arg)
1635 "Move across ARG groups of similarly typed items in speedbar.
1636 Stop on the first line of the next type of item, or on the last or first item
1637 if we reach a buffer boundary."
1638 (interactive "p")
1639 (beginning-of-line)
1640 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1641 (let ((str (regexp-quote (match-string 0))))
1642 (while (looking-at str)
1643 (speedbar-restricted-move arg)
1644 (beginning-of-line))))
1645 (speedbar-position-cursor-on-line))
1647 (defun speedbar-forward-list ()
1648 "Move forward over the current list.
1649 A LIST in speedbar is a group of similarly typed items, such as directories,
1650 files, or the directory button."
1651 (interactive)
1652 (speedbar-navigate-list 1)
1653 (speedbar-item-info))
1655 (defun speedbar-backward-list ()
1656 "Move backward over the current list.
1657 A LIST in speedbar is a group of similarly typed items, such as directories,
1658 files, or the directory button."
1659 (interactive)
1660 (speedbar-navigate-list -1)
1661 (speedbar-item-info))
1663 (defun speedbar-scroll-up (&optional arg)
1664 "Page down one screen-full of the speedbar, or ARG lines."
1665 (interactive "P")
1666 (scroll-up arg)
1667 (speedbar-position-cursor-on-line))
1669 (defun speedbar-scroll-down (&optional arg)
1670 "Page up one screen-full of the speedbar, or ARG lines."
1671 (interactive "P")
1672 (scroll-down arg)
1673 (speedbar-position-cursor-on-line))
1675 (defun speedbar-up-directory ()
1676 "Keyboard accelerator for moving the default directory up one.
1677 Assumes that the current buffer is the speedbar buffer."
1678 (interactive)
1679 (setq default-directory (expand-file-name (concat default-directory "../")))
1680 (speedbar-update-contents))
1682 ;;; Speedbar file activity (aka creeping featurism)
1684 (defun speedbar-refresh ()
1685 "Refresh the current speedbar display, disposing of any cached data."
1686 (interactive)
1687 (let ((dl speedbar-shown-directories)
1688 deactivate-mark)
1689 (while dl
1690 (adelete 'speedbar-directory-contents-alist (car dl))
1691 (setq dl (cdr dl)))
1692 (if (<= 1 speedbar-verbosity-level)
1693 (speedbar-message "Refreshing speedbar..."))
1694 (speedbar-update-contents)
1695 (speedbar-stealthy-updates)
1696 ;; Reset the timer in case it got really hosed for some reason...
1697 (speedbar-set-timer speedbar-update-speed)
1698 (if (<= 1 speedbar-verbosity-level)
1699 (speedbar-message "Refreshing speedbar...done"))))
1701 (defun speedbar-item-load ()
1702 "Load the item under the cursor or mouse if it is a Lisp file."
1703 (interactive)
1704 (let ((f (speedbar-line-file)))
1705 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1706 (if (and (file-exists-p (concat f "c"))
1707 (speedbar-y-or-n-p (format "Load %sc? " f)))
1708 ;; If the compiled version exists, load that instead...
1709 (load-file (concat f "c"))
1710 (load-file f))
1711 (error "Not a loadable file"))))
1713 (defun speedbar-item-byte-compile ()
1714 "Byte compile the item under the cursor or mouse if it is a Lisp file."
1715 (interactive)
1716 (let ((f (speedbar-line-file))
1717 (sf (selected-frame)))
1718 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1719 (progn
1720 (select-frame speedbar-attached-frame)
1721 (byte-compile-file f nil)
1722 (select-frame sf)
1723 (speedbar-reset-scanners)))
1726 (defun speedbar-mouse-item-info (event)
1727 "Provide information about what the user clicked on.
1728 This should be bound to a mouse EVENT."
1729 (interactive "e")
1730 (mouse-set-point event)
1731 (speedbar-item-info))
1733 (defun speedbar-generic-item-info ()
1734 "Attempt to derive, and then display information about this line item.
1735 File style information is displayed with `speedbar-item-info'."
1736 (save-excursion
1737 (beginning-of-line)
1738 ;; Skip invisible number info.
1739 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1740 ;; Skip items in "folder" type text characters.
1741 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1742 ;; Get the text
1743 (speedbar-message "Text: %s" (buffer-substring-no-properties
1744 (point) (progn (end-of-line) (point))))))
1746 (defun speedbar-item-info ()
1747 "Display info in the mini-buffer about the button the mouse is over.
1748 This function can be replaced in `speedbar-mode-functions-list' as
1749 `speedbar-item-info'."
1750 (interactive)
1751 (let (message-log-max)
1752 (funcall (or (speedbar-fetch-replacement-function 'speedbar-item-info)
1753 'speedbar-generic-item-info))))
1755 (defun speedbar-item-info-file-helper (&optional filename)
1756 "Display info about a file that is on the current line.
1757 nil if not applicable. If FILENAME, then use that instead of reading
1758 it from the speedbar buffer."
1759 (let* ((item (or filename (speedbar-line-file)))
1760 (attr (if item (file-attributes item) nil)))
1761 (if (and item attr) (speedbar-message "%s %-6d %s" (nth 8 attr)
1762 (nth 7 attr) item)
1763 nil)))
1765 (defun speedbar-item-info-tag-helper ()
1766 "Display info about a tag that is on the current line.
1767 nil if not applicable."
1768 (save-excursion
1769 (beginning-of-line)
1770 (if (re-search-forward " [-+=]?> \\([^\n]+\\)"
1771 (save-excursion(end-of-line)(point)) t)
1772 (let ((tag (match-string 1))
1773 (attr (speedbar-line-token))
1774 (item nil))
1775 (if (and (featurep 'semantic) (semantic-token-p attr))
1776 (speedbar-message (semantic-summerize-nonterminal attr))
1777 (looking-at "\\([0-9]+\\):")
1778 (setq item (file-name-nondirectory (speedbar-line-path)))
1779 (speedbar-message "Tag: %s in %s" tag item)))
1780 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1781 (save-excursion(end-of-line)(point)) t)
1782 (speedbar-message "Group of tags \"%s\"" (match-string 1))
1783 (if (re-search-forward " [+-]?[()|@] \\([^\n]+\\)$" nil t)
1784 (let* ((detailtext (match-string 1))
1785 (detail (or (speedbar-line-token) detailtext))
1786 (parent (save-excursion
1787 (beginning-of-line)
1788 (let ((dep (if (looking-at "[0-9]+:")
1789 (1- (string-to-int (match-string 0)))
1790 0)))
1791 (re-search-backward (concat "^"
1792 (int-to-string dep)
1793 ":")
1794 nil t))
1795 (if (looking-at "[0-9]+: +[-+=>]> \\([^\n]+\\)$")
1796 (speedbar-line-token)
1797 nil))))
1798 (if (and (featurep 'semantic) (semantic-token-p detail))
1799 (speedbar-message
1800 (semantic-summerize-nonterminal detail parent))
1801 (if parent
1802 (speedbar-message "Detail: %s of tag %s" detail
1803 (if (and (featurep 'semantic)
1804 (semantic-token-p parent))
1805 (semantic-token-name parent)
1806 parent))
1807 (speedbar-message "Detail: %s" detail))))
1808 nil)))))
1810 (defun speedbar-files-item-info ()
1811 "Display info in the mini-buffer about the button the mouse is over."
1812 (if (not speedbar-shown-directories)
1813 (speedbar-generic-item-info)
1814 (or (speedbar-item-info-file-helper)
1815 (speedbar-item-info-tag-helper)
1816 (speedbar-generic-item-info))))
1818 (defun speedbar-item-copy ()
1819 "Copy the item under the cursor.
1820 Files can be copied to new names or places."
1821 (interactive)
1822 (let ((f (speedbar-line-file)))
1823 (if (not f) (error "Not a file"))
1824 (if (file-directory-p f)
1825 (error "Cannot copy directory")
1826 (let* ((rt (read-file-name (format "Copy %s to: "
1827 (file-name-nondirectory f))
1828 (file-name-directory f)))
1829 (refresh (member (expand-file-name (file-name-directory rt))
1830 speedbar-shown-directories)))
1831 ;; Create the right file name part
1832 (if (file-directory-p rt)
1833 (setq rt
1834 (concat (expand-file-name rt)
1835 (if (string-match "[/\\]$" rt) "" "/")
1836 (file-name-nondirectory f))))
1837 (if (or (not (file-exists-p rt))
1838 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)))
1839 (progn
1840 (copy-file f rt t t)
1841 ;; refresh display if the new place is currently displayed.
1842 (if refresh
1843 (progn
1844 (speedbar-refresh)
1845 (if (not (speedbar-goto-this-file rt))
1846 (speedbar-goto-this-file f))))
1847 ))))))
1849 (defun speedbar-item-rename ()
1850 "Rename the item under the cursor or mouse.
1851 Files can be renamed to new names or moved to new directories."
1852 (interactive)
1853 (let ((f (speedbar-line-file)))
1854 (if f
1855 (let* ((rt (read-file-name (format "Rename %s to: "
1856 (file-name-nondirectory f))
1857 (file-name-directory f)))
1858 (refresh (member (expand-file-name (file-name-directory rt))
1859 speedbar-shown-directories)))
1860 ;; Create the right file name part
1861 (if (file-directory-p rt)
1862 (setq rt
1863 (concat (expand-file-name rt)
1864 (if (string-match "[/\\]\\'" rt) "" "/")
1865 (file-name-nondirectory f))))
1866 (if (or (not (file-exists-p rt))
1867 (speedbar-y-or-n-p (format "Overwrite %s with %s? " rt f)))
1868 (progn
1869 (rename-file f rt t)
1870 ;; refresh display if the new place is currently displayed.
1871 (if refresh
1872 (progn
1873 (speedbar-refresh)
1874 (speedbar-goto-this-file rt)
1875 )))))
1876 (error "Not a file"))))
1878 (defun speedbar-item-delete ()
1879 "Delete the item under the cursor. Files are removed from disk."
1880 (interactive)
1881 (let ((f (speedbar-line-file)))
1882 (if (not f) (error "Not a file"))
1883 (if (speedbar-y-or-n-p (format "Delete %s? " f))
1884 (progn
1885 (if (file-directory-p f)
1886 (delete-directory f)
1887 (delete-file f))
1888 (speedbar-message "Okie dokie..")
1889 (let ((p (point)))
1890 (speedbar-refresh)
1891 (goto-char p))
1895 (defun speedbar-item-object-delete ()
1896 "Delete the object associated from the item under the cursor.
1897 The file is removed from disk. The object is determined from the
1898 variable `speedbar-obj-alist'."
1899 (interactive)
1900 (let* ((f (speedbar-line-file))
1901 (obj nil)
1902 (oa speedbar-obj-alist))
1903 (if (not f) (error "Not a file"))
1904 (while (and oa (not (string-match (car (car oa)) f)))
1905 (setq oa (cdr oa)))
1906 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1907 (if (and oa (file-exists-p obj)
1908 (speedbar-y-or-n-p (format "Delete %s? " obj)))
1909 (progn
1910 (delete-file obj)
1911 (speedbar-reset-scanners)))))
1913 (defun speedbar-enable-update ()
1914 "Enable automatic updating in speedbar via timers."
1915 (interactive)
1916 (setq speedbar-update-flag t)
1917 (speedbar-set-mode-line-format)
1918 (speedbar-set-timer speedbar-update-speed))
1920 (defun speedbar-disable-update ()
1921 "Disable automatic updating and stop consuming resources."
1922 (interactive)
1923 (setq speedbar-update-flag nil)
1924 (speedbar-set-mode-line-format)
1925 (speedbar-set-timer nil))
1927 (defun speedbar-toggle-updates ()
1928 "Toggle automatic update for the speedbar frame."
1929 (interactive)
1930 (if speedbar-update-flag
1931 (speedbar-disable-update)
1932 (speedbar-enable-update)))
1934 (defun speedbar-toggle-images ()
1935 "Toggle images for the speedbar frame."
1936 (interactive)
1937 (setq speedbar-use-images (not speedbar-use-images))
1938 (speedbar-refresh))
1940 (defun speedbar-toggle-sorting ()
1941 "Toggle sorting for the speedbar frame."
1942 (interactive)
1943 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1945 (defun speedbar-toggle-show-all-files ()
1946 "Toggle display of files speedbar can not tag."
1947 (interactive)
1948 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1949 (speedbar-refresh))
1951 ;;; Utility functions
1953 (defun speedbar-set-timer (timeout)
1954 "Apply a timer with TIMEOUT, or remove a timer if TIMEOUT is nil.
1955 TIMEOUT is the number of seconds until the speedbar timer is called
1956 again. When TIMEOUT is nil, turn off all timeouts.
1957 This function will also enable or disable the `vc-checkin-hook' used
1958 to track file check ins, and will change the mode line to match
1959 `speedbar-update-flag'."
1960 (cond
1961 ;; XEmacs
1962 (speedbar-xemacsp
1963 (if speedbar-timer
1964 (progn (delete-itimer speedbar-timer)
1965 (setq speedbar-timer nil)))
1966 (if timeout
1967 (if (and speedbar-xemacsp
1968 (or (>= emacs-major-version 20)
1969 (>= emacs-minor-version 15)))
1970 (setq speedbar-timer (start-itimer "speedbar"
1971 'speedbar-timer-fn
1972 timeout
1973 timeout
1975 (setq speedbar-timer (start-itimer "speedbar"
1976 'speedbar-timer-fn
1977 timeout
1978 nil)))))
1979 ;; Post 19.31 Emacs
1980 ((fboundp 'run-with-idle-timer)
1981 (if speedbar-timer
1982 (progn (cancel-timer speedbar-timer)
1983 (setq speedbar-timer nil)))
1984 (if timeout
1985 (setq speedbar-timer
1986 (run-with-idle-timer timeout t 'speedbar-timer-fn))))
1987 ;; Emacs 19.30 (Thanks twice: ptype@dra.hmg.gb)
1988 ((fboundp 'post-command-idle-hook)
1989 (if timeout
1990 (add-hook 'post-command-idle-hook 'speedbar-timer-fn)
1991 (remove-hook 'post-command-idle-hook 'speedbar-timer-fn)))
1992 ;; Older or other Emacsen with no timers. Set up so that its
1993 ;; obvious this emacs can't handle the updates
1995 (setq speedbar-update-flag nil)))
1996 ;; Apply a revert hook that will reset the scanners. We attach to revert
1997 ;; because most reverts occur during VC state change, and this lets our
1998 ;; VC scanner fix itself.
1999 (if timeout
2000 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
2001 (remove-hook 'after-revert-hook 'speedbar-reset-scanners)
2003 ;; change this if it changed for some reason
2004 (speedbar-set-mode-line-format))
2006 (defmacro speedbar-with-writable (&rest forms)
2007 "Allow the buffer to be writable and evaluate FORMS."
2008 (list 'let '((inhibit-read-only t))
2009 (cons 'progn forms)))
2010 (put 'speedbar-with-writable 'lisp-indent-function 0)
2012 (defun speedbar-select-window (buffer)
2013 "Select a window in which BUFFER is shown.
2014 If it is not shown, force it to appear in the default window."
2015 (let ((win (get-buffer-window buffer speedbar-attached-frame)))
2016 (if win
2017 (select-window win)
2018 (set-window-buffer (selected-window) buffer))))
2020 (defun speedbar-insert-button (text face mouse function
2021 &optional token prevline)
2022 "Insert TEXT as the next logical speedbar button.
2023 FACE is the face to put on the button, MOUSE is the highlight face to use.
2024 When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
2025 This function assumes that the current buffer is the speedbar buffer.
2026 If PREVLINE, then put this button on the previous line.
2028 This is a convenience function for special modes that create their own
2029 specialized speedbar displays."
2030 (goto-char (point-max))
2031 (let ((start (point)))
2032 (if (/= (current-column) 0) (insert "\n"))
2033 (put-text-property start (point) 'invisible nil))
2034 (if prevline (progn (delete-char -1)
2035 (insert " ") ;back up if desired...
2036 (put-text-property (1- (point)) (point) 'invisible nil)))
2037 (let ((start (point)))
2038 (insert text)
2039 (speedbar-make-button start (point) face mouse function token))
2040 (let ((start (point)))
2041 (insert "\n")
2042 (put-text-property start (point) 'face nil)
2043 (put-text-property start (point) 'invisible nil)
2044 (put-text-property start (point) 'mouse-face nil)))
2046 (defun speedbar-make-button (start end face mouse function &optional token)
2047 "Create a button from START to END, with FACE as the display face.
2048 MOUSE is the mouse face. When this button is clicked on FUNCTION
2049 will be run with the TOKEN parameter (any Lisp object)."
2050 (put-text-property start end 'face face)
2051 (put-text-property start end 'mouse-face mouse)
2052 (put-text-property start end 'invisible nil)
2053 (if function (put-text-property start end 'speedbar-function function))
2054 (if token (put-text-property start end 'speedbar-token token))
2055 ;; So far the only text we have is less that 3 chars.
2056 (if (<= (- end start) 3)
2057 (speedbar-insert-image-button-maybe start (- end start)))
2060 ;;; Initial Expansion list management
2062 (defun speedbar-initial-expansion-list ()
2063 "Return the current default expansion list.
2064 This is based on `speedbar-initial-expansion-list-name' referencing
2065 `speedbar-initial-expansion-mode-alist'."
2066 ;; cdr1 - name, cdr2 - menu
2067 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
2068 speedbar-initial-expansion-mode-alist)))))
2070 (defun speedbar-initial-menu ()
2071 "Return the current default menu data.
2072 This is based on `speedbar-initial-expansion-list-name' referencing
2073 `speedbar-initial-expansion-mode-alist'."
2074 (symbol-value
2075 (car (cdr (assoc speedbar-initial-expansion-list-name
2076 speedbar-initial-expansion-mode-alist)))))
2078 (defun speedbar-initial-keymap ()
2079 "Return the current default menu data.
2080 This is based on `speedbar-initial-expansion-list-name' referencing
2081 `speedbar-initial-expansion-mode-alist'."
2082 (symbol-value
2083 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
2084 speedbar-initial-expansion-mode-alist))))))
2086 (defun speedbar-initial-stealthy-functions ()
2087 "Return a list of functions to call stealthily.
2088 This is based on `speedbar-initial-expansion-list-name' referencing
2089 `speedbar-stealthy-function-list'."
2090 (cdr (assoc speedbar-initial-expansion-list-name
2091 speedbar-stealthy-function-list)))
2093 (defun speedbar-add-expansion-list (new-list)
2094 "Add NEW-LIST to the list of expansion lists."
2095 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
2097 (defun speedbar-change-initial-expansion-list (new-default)
2098 "Change speedbar's default expansion list to NEW-DEFAULT."
2099 (interactive
2100 (list
2101 (completing-read (format "Speedbar Mode (default %s): "
2102 speedbar-previously-used-expansion-list-name)
2103 speedbar-initial-expansion-mode-alist
2104 nil t "" nil
2105 speedbar-previously-used-expansion-list-name)))
2106 (setq speedbar-previously-used-expansion-list-name
2107 speedbar-initial-expansion-list-name
2108 speedbar-initial-expansion-list-name new-default)
2109 (speedbar-refresh)
2110 (speedbar-reconfigure-keymaps))
2112 (defun speedbar-fetch-replacement-function (function)
2113 "Return a current mode specific replacement for function, or nil.
2114 Scans `speedbar-mode-functions-list' first for the current mode, then
2115 for FUNCTION."
2116 (cdr (assoc function
2117 (cdr (assoc speedbar-initial-expansion-list-name
2118 speedbar-mode-functions-list)))))
2120 (defun speedbar-add-mode-functions-list (new-list)
2121 "Add NEW-LIST to the list of mode functions.
2122 See `speedbar-mode-functions-list' for details."
2123 (add-to-list 'speedbar-mode-functions-list new-list))
2126 ;;; Special speedbar display management
2128 (defun speedbar-maybe-add-localized-support (buffer)
2129 "Quick check function called on BUFFERs by the speedbar timer function.
2130 Maintains the value of local variables which control speedbars use
2131 of the special mode functions."
2132 (or speedbar-special-mode-expansion-list
2133 (speedbar-add-localized-speedbar-support buffer)))
2135 (defun speedbar-add-localized-speedbar-support (buffer)
2136 "Add localized speedbar support to BUFFER's mode if it is available."
2137 (interactive "bBuffer: ")
2138 (if (stringp buffer) (setq buffer (get-buffer buffer)))
2139 (if (not (buffer-live-p buffer))
2141 (save-excursion
2142 (set-buffer buffer)
2143 (save-match-data
2144 (let ((ms (symbol-name major-mode)) v)
2145 (if (not (string-match "-mode$" ms))
2146 nil ;; do nothing to broken mode
2147 (setq ms (substring ms 0 (match-beginning 0)))
2148 (setq v (intern-soft (concat ms "-speedbar-buttons")))
2149 (make-local-variable 'speedbar-special-mode-expansion-list)
2150 (if (not v)
2151 (setq speedbar-special-mode-expansion-list t)
2152 ;; If it is autoloaded, we need to load it now so that
2153 ;; we have access to the varialbe -speedbar-menu-items.
2154 ;; Is this XEmacs safe?
2155 (let ((sf (symbol-function v)))
2156 (if (and (listp sf) (eq (car sf) 'autoload))
2157 (load-library (car (cdr sf)))))
2158 (setq speedbar-special-mode-expansion-list (list v))
2159 (setq v (intern-soft (concat ms "-speedbar-key-map")))
2160 (if (not v)
2161 nil ;; don't add special keymap
2162 (make-local-variable 'speedbar-special-mode-key-map)
2163 (setq speedbar-special-mode-key-map
2164 (symbol-value v)))
2165 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
2166 (if (not v)
2167 nil ;; don't add special menus
2168 (make-local-variable 'speedbar-easymenu-definition-special)
2169 (setq speedbar-easymenu-definition-special
2170 (symbol-value v)))
2171 )))))))
2173 (defun speedbar-remove-localized-speedbar-support (buffer)
2174 "Remove any traces that BUFFER supports speedbar in a specialized way."
2175 (save-excursion
2176 (set-buffer buffer)
2177 (kill-local-variable 'speedbar-special-mode-expansion-list)
2178 (kill-local-variable 'speedbar-special-mode-key-map)
2179 (kill-local-variable 'speedbar-easymenu-definition-special)))
2181 ;;; File button management
2183 (defun speedbar-file-lists (directory)
2184 "Create file lists for DIRECTORY.
2185 The car is the list of directories, the cdr is list of files not
2186 matching ignored headers. Cache any directory files found in
2187 `speedbar-directory-contents-alist' and use that cache before scanning
2188 the file-system."
2189 (setq directory (expand-file-name directory))
2190 ;; If in powerclick mode, then the directory we are getting
2191 ;; should be rescanned.
2192 (if speedbar-power-click
2193 (adelete 'speedbar-directory-contents-alist directory))
2194 ;; find the directory, either in the cache, or build it.
2195 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
2196 (let ((default-directory directory)
2197 (case-fold-search read-file-name-completion-ignore-case)
2198 dirs files)
2199 (dolist (file (directory-files directory nil))
2200 (or (string-match speedbar-file-unshown-regexp file)
2201 (string-match speedbar-directory-unshown-regexp file)
2202 (if (file-directory-p file)
2203 (setq dirs (cons file dirs))
2204 (setq files (cons file files)))))
2205 (let ((nl `(,(nreverse dirs) ,(nreverse files))))
2206 (aput 'speedbar-directory-contents-alist directory nl)
2207 nl))))
2209 (defun speedbar-directory-buttons (directory index)
2210 "Insert a single button group at point for DIRECTORY.
2211 Each directory path part is a different button. If part of the path
2212 matches the user directory ~, then it is replaced with a ~.
2213 INDEX is not used, but is required by the caller."
2214 (let* ((tilde (expand-file-name "~/"))
2215 (dd (expand-file-name directory))
2216 (junk (string-match (regexp-quote tilde) dd))
2217 (displayme (if junk
2218 (concat "~/" (substring dd (match-end 0)))
2219 dd))
2220 (p (point)))
2221 (if (string-match "^~[/\\]?\\'" displayme) (setq displayme tilde))
2222 (insert displayme)
2223 (save-excursion
2224 (goto-char p)
2225 (while (re-search-forward "\\([^/\\]+\\)[/\\]" nil t)
2226 (speedbar-make-button (match-beginning 1) (match-end 1)
2227 'speedbar-directory-face
2228 'speedbar-highlight-face
2229 'speedbar-directory-buttons-follow
2230 (if (and (= (match-beginning 1) p)
2231 (not (char-equal (char-after (+ p 1)) ?:)))
2232 (expand-file-name "~/") ;the tilde
2233 (buffer-substring-no-properties
2234 p (match-end 0)))))
2235 ;; Nuke the beginning of the directory if it's too long...
2236 (cond ((eq speedbar-directory-button-trim-method 'span)
2237 (beginning-of-line)
2238 (let ((ww (or (speedbar-frame-width) 20)))
2239 (move-to-column ww nil)
2240 (while (>= (current-column) ww)
2241 (re-search-backward "[/\\]" nil t)
2242 (if (<= (current-column) 2)
2243 (progn
2244 (re-search-forward "[/\\]" nil t)
2245 (if (< (current-column) 4)
2246 (re-search-forward "[/\\]" nil t))
2247 (forward-char -1)))
2248 (if (looking-at "[/\\]?$")
2249 (beginning-of-line)
2250 (insert "/...\n ")
2251 (move-to-column ww nil)))))
2252 ((eq speedbar-directory-button-trim-method 'trim)
2253 (end-of-line)
2254 (let ((ww (or (speedbar-frame-width) 20))
2255 (tl (current-column)))
2256 (if (< ww tl)
2257 (progn
2258 (move-to-column (- tl ww))
2259 (if (re-search-backward "[/\\]" nil t)
2260 (progn
2261 (delete-region (point-min) (point))
2262 (insert "$")
2263 )))))))
2265 (if (string-match "\\`[/\\][^/\\]+[/\\]\\'" displayme)
2266 (progn
2267 (insert " ")
2268 (let ((p (point)))
2269 (insert "<root>")
2270 (speedbar-make-button p (point)
2271 'speedbar-directory-face
2272 'speedbar-highlight-face
2273 'speedbar-directory-buttons-follow
2274 "/"))))
2275 (end-of-line)
2276 (insert-char ?\n 1 nil)))
2278 (defun speedbar-make-tag-line (exp-button-type
2279 exp-button-char exp-button-function
2280 exp-button-data
2281 tag-button tag-button-function tag-button-data
2282 tag-button-face depth)
2283 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2284 This is the button that expands or contracts a node (if applicable),
2285 and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2286 is the function to call if it's clicked on. Button types are
2287 'bracket, 'angle, 'curly, or nil. EXP-BUTTON-DATA is extra data
2288 attached to the text forming the expansion button.
2290 Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2291 function to call if clicked on, and TAG-BUTTON-DATA is the data to
2292 attach to the text field (such a tag positioning, etc).
2293 TAG-BUTTON-FACE is a face used for this type of tag.
2295 Lastly, DEPTH shows the depth of expansion.
2297 This function assumes that the cursor is in the speedbar window at the
2298 position to insert a new item, and that the new item will end with a CR."
2299 (let ((start (point))
2300 (end (progn
2301 (insert (int-to-string depth) ":")
2302 (point)))
2303 (depthspacesize (* depth speedbar-indentation-width)))
2304 (put-text-property start end 'invisible t)
2305 (insert-char ? depthspacesize nil)
2306 (put-text-property (- (point) depthspacesize) (point) 'invisible nil)
2307 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2308 ((eq exp-button-type 'angle) "<%c>")
2309 ((eq exp-button-type 'curly) "{%c}")
2310 (t ">")))
2311 (buttxt (format exp-button exp-button-char))
2312 (start (point))
2313 (end (progn (insert buttxt) (point)))
2314 (bf (if exp-button-type 'speedbar-button-face nil))
2315 (mf (if exp-button-function 'speedbar-highlight-face nil))
2317 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
2318 (if speedbar-hide-button-brackets-flag
2319 (progn
2320 (put-text-property start (1+ start) 'invisible t)
2321 (put-text-property end (1- end) 'invisible t)))
2323 (insert-char ? 1 nil)
2324 (put-text-property (1- (point)) (point) 'invisible nil)
2325 (let ((start (point))
2326 (end (progn (insert tag-button) (point))))
2327 (insert-char ?\n 1 nil)
2328 (put-text-property (1- (point)) (point) 'invisible nil)
2329 (speedbar-make-button start end tag-button-face
2330 (if tag-button-function 'speedbar-highlight-face nil)
2331 tag-button-function tag-button-data))
2334 (defun speedbar-change-expand-button-char (char)
2335 "Change the expansion button character to CHAR for the current line."
2336 (save-excursion
2337 (beginning-of-line)
2338 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2339 (point)) t)
2340 (speedbar-with-writable
2341 (goto-char (match-beginning 1))
2342 (delete-char 1)
2343 (insert-char char 1 t)
2344 (put-text-property (point) (1- (point)) 'invisible nil)
2345 ;; make sure we fix the image on the text here.
2346 (speedbar-insert-image-button-maybe (- (point) 2) 3)))))
2349 ;;; Build button lists
2351 (defun speedbar-insert-files-at-point (files level directory)
2352 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2353 Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2354 don't know how to manage them. The input parameter FILES is a cons
2355 cell of the form ( 'DIRLIST . 'FILELIST )."
2356 ;; Start inserting all the directories
2357 (dolist (dir (car files))
2358 (if (if speedbar-scan-subdirs
2359 (condition-case nil
2360 (let ((l (speedbar-file-lists (concat directory dir))))
2361 (or (car l) (cadr l)))
2362 (file-error))
2363 (file-readable-p (concat directory dir)))
2364 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired dir
2365 dir 'speedbar-dir-follow nil
2366 'speedbar-directory-face level)
2367 (speedbar-make-tag-line 'angle ? nil dir
2368 dir 'speedbar-dir-follow nil
2369 'speedbar-directory-face level)))
2370 (let ((case-fold-search read-file-name-completion-ignore-case))
2371 (dolist (file (cadr files))
2372 (let* ((known (and (file-readable-p (concat directory file))
2373 (string-match speedbar-file-regexp file)))
2374 (expchar (if known ?+ ??))
2375 (fn (if known 'speedbar-tag-file nil)))
2376 (if (or speedbar-show-unknown-files (/= expchar ??))
2377 (speedbar-make-tag-line 'bracket expchar fn file
2378 file 'speedbar-find-file nil
2379 'speedbar-file-face level))))))
2381 (defun speedbar-default-directory-list (directory index)
2382 "Insert files for DIRECTORY with level INDEX at point."
2383 (speedbar-insert-files-at-point
2384 (speedbar-file-lists directory) index directory)
2385 (speedbar-reset-scanners)
2386 (if (= index 0)
2387 ;; If the shown files variable has extra directories, then
2388 ;; it is our responsibility to redraw them all
2389 ;; Luckilly, the nature of inserting items into this list means
2390 ;; that by reversing it, we can easilly go in the right order
2391 (let ((sf (cdr (reverse speedbar-shown-directories))))
2392 (setq speedbar-shown-directories
2393 (list (expand-file-name default-directory)))
2394 ;; exand them all as we find them
2395 (while sf
2396 (if (speedbar-goto-this-file (car sf))
2397 (progn
2398 (beginning-of-line)
2399 (if (looking-at "[0-9]+:[ ]*<")
2400 (progn
2401 (goto-char (match-end 0))
2402 (speedbar-do-function-pointer)))))
2403 (setq sf (cdr sf)))
2406 (defun speedbar-sort-tag-hierarchy (lst)
2407 "Sort all elements of tag hierarchy LST."
2408 (sort (copy-alist lst)
2409 (lambda (a b) (string< (car a) (car b)))))
2411 (defun speedbar-prefix-group-tag-hierarchy (lst)
2412 "Prefix group names for tag hierarchy LST."
2413 (let ((newlst nil)
2414 (sublst nil)
2415 (work-list nil)
2416 (junk-list nil)
2417 (short-group-list nil)
2418 (short-start-name nil)
2419 (short-end-name nil)
2420 (num-shorts-grouped 0)
2421 (bins (make-vector 256 nil))
2422 (diff-idx 0))
2423 ;; Break out sub-lists
2424 (while lst
2425 (if (and (listp (cdr-safe (car-safe lst)))
2426 ;; This one is for bovine tokens
2427 (not (symbolp (car-safe (cdr-safe (car-safe lst))))))
2428 (setq newlst (cons (car lst) newlst))
2429 (setq sublst (cons (car lst) sublst)))
2430 (setq lst (cdr lst)))
2431 ;; Reverse newlst because it was made backwards.
2432 ;; Sublist doesn't need reversing because the act
2433 ;; of binning things will reverse it for us.
2434 (setq newlst (nreverse newlst))
2435 ;; Now, first find out how long our list is. Never let a
2436 ;; list get-shorter than our minimum.
2437 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2438 (setq work-list (nreverse sublst))
2439 (setq diff-idx (length (try-completion "" sublst)))
2440 ;; Sort the whole list into bins.
2441 (while sublst
2442 (let ((e (car sublst))
2443 (s (car (car sublst))))
2444 (cond ((<= (length s) diff-idx)
2445 ;; 0 storage bin for shorty.
2446 (aset bins 0 (cons e (aref bins 0))))
2448 ;; stuff into a bin based on ascii value at diff
2449 (aset bins (aref s diff-idx)
2450 (cons e (aref bins (aref s diff-idx)))))))
2451 (setq sublst (cdr sublst)))
2452 ;; Go through all our bins Stick singles into our
2453 ;; junk-list, everything else as sublsts in work-list.
2454 ;; If two neighboring lists are both small, make a grouped
2455 ;; group combinding those two sub-lists.
2456 (setq diff-idx 0)
2457 (while (> 256 diff-idx)
2458 (let ((l (nreverse;; Reverse the list since they are stuck in
2459 ;; backwards.
2460 (aref bins diff-idx))))
2461 (if l
2462 (let ((tmp (cons (try-completion "" l) l)))
2463 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2464 (> (+ (length l) (length short-group-list))
2465 speedbar-tag-split-minimum-length))
2466 (progn
2467 ;; We have reached a longer list, so we
2468 ;; must finish off a grouped group.
2469 (cond
2470 ((and short-group-list
2471 (= (length short-group-list)
2472 num-shorts-grouped))
2473 ;; All singles? Junk list
2474 (setq junk-list (append short-group-list
2475 junk-list)))
2476 ((= num-shorts-grouped 1)
2477 ;; Only one short group? Just stick it in
2478 ;; there by itself. Make a group, and find
2479 ;; a subexpression
2480 (let ((subexpression (try-completion
2481 "" short-group-list)))
2482 (if (< (length subexpression)
2483 speedbar-tag-group-name-minimum-length)
2484 (setq subexpression
2485 (concat short-start-name
2486 " ("
2487 (substring
2488 (car (car short-group-list))
2489 (length short-start-name))
2490 ")")))
2491 (setq work-list
2492 (cons (cons subexpression
2493 short-group-list)
2494 work-list))))
2495 (short-group-list
2496 ;; Multiple groups to be named in a special
2497 ;; way by displaying the range over which we
2498 ;; have grouped them.
2499 (setq work-list
2500 (cons (cons (concat short-start-name
2501 " to "
2502 short-end-name)
2503 (nreverse short-group-list))
2504 work-list))))
2505 ;; Reset short group list information every time.
2506 (setq short-group-list nil
2507 short-start-name nil
2508 short-end-name nil
2509 num-shorts-grouped 0)))
2510 ;; Ok, now that we cleaned up the short-group-list,
2511 ;; we can deal with this new list, to decide if it
2512 ;; should go on one of these sub-lists or not.
2513 (if (< (length l) speedbar-tag-regroup-maximum-length)
2514 (setq short-group-list (append short-group-list l)
2515 num-shorts-grouped (1+ num-shorts-grouped)
2516 short-end-name (car tmp)
2517 short-start-name (if short-start-name
2518 short-start-name
2519 (car tmp)))
2520 (setq work-list (cons tmp work-list))))))
2521 (setq diff-idx (1+ diff-idx))))
2522 ;; Did we run out of things? Drop our new list onto the end.
2523 (cond
2524 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2525 ;; All singles? Junk list
2526 (setq junk-list (append short-group-list junk-list)))
2527 ((= num-shorts-grouped 1)
2528 ;; Only one short group? Just stick it in
2529 ;; there by itself.
2530 (setq work-list
2531 (cons (cons (try-completion "" short-group-list)
2532 short-group-list)
2533 work-list)))
2534 (short-group-list
2535 ;; Multiple groups to be named in a special
2536 ;; way by displaying the range over which we
2537 ;; have grouped them.
2538 (setq work-list
2539 (cons (cons (concat short-start-name " to " short-end-name)
2540 short-group-list)
2541 work-list))))
2542 ;; Reverse the work list nreversed when consing.
2543 (setq work-list (nreverse work-list))
2544 ;; Now, stick our new list onto the end of
2545 (if work-list
2546 (if junk-list
2547 (append newlst work-list junk-list)
2548 (append newlst work-list))
2549 (append newlst junk-list))))
2551 (defun speedbar-trim-words-tag-hierarchy (lst)
2552 "Trim all words in a tag hierarchy.
2553 Base trimming information on word separators, and group names.
2554 Argument LST is the list of tags to trim."
2555 (let ((newlst nil)
2556 (sublst nil)
2557 (trim-prefix nil)
2558 (trim-chars 0)
2559 (trimlst nil))
2560 (while lst
2561 (if (listp (cdr-safe (car-safe lst)))
2562 (setq newlst (cons (car lst) newlst))
2563 (setq sublst (cons (car lst) sublst)))
2564 (setq lst (cdr lst)))
2565 ;; Get the prefix to trim by. Make sure that we don't trim
2566 ;; off silly pieces, only complete understandable words.
2567 (setq trim-prefix (try-completion "" sublst))
2568 (if (or (= (length sublst) 1)
2569 (not trim-prefix)
2570 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2571 (append (nreverse newlst) (nreverse sublst))
2572 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2573 (match-end 0)))
2574 (setq trim-chars (length trim-prefix))
2575 (while sublst
2576 (setq trimlst (cons
2577 (cons (substring (car (car sublst)) trim-chars)
2578 (cdr (car sublst)))
2579 trimlst)
2580 sublst (cdr sublst)))
2581 ;; Put the lists together
2582 (append (nreverse newlst) trimlst))))
2584 (defun speedbar-simple-group-tag-hierarchy (lst)
2585 "Create a simple 'Tags' group with orphaned tags.
2586 Argument LST is the list of tags to sort into groups."
2587 (let ((newlst nil)
2588 (sublst nil))
2589 (while lst
2590 (if (listp (cdr-safe (car-safe lst)))
2591 (setq newlst (cons (car lst) newlst))
2592 (setq sublst (cons (car lst) sublst)))
2593 (setq lst (cdr lst)))
2594 (if (not newlst)
2595 (nreverse sublst)
2596 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2597 (nreverse newlst))))
2599 (defun speedbar-create-tag-hierarchy (lst)
2600 "Adjust the tag hierarchy in LST, and return it.
2601 This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2602 the list."
2603 (let* ((f (save-excursion
2604 (forward-line -1)
2605 (speedbar-line-path)))
2606 (methods (if (get-file-buffer f)
2607 (save-excursion (set-buffer (get-file-buffer f))
2608 speedbar-tag-hierarchy-method)
2609 speedbar-tag-hierarchy-method))
2610 (lst (if (fboundp 'copy-tree)
2611 (copy-tree lst)
2612 lst)))
2613 (while methods
2614 (setq lst (funcall (car methods) lst)
2615 methods (cdr methods)))
2616 lst))
2618 (defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2619 "At LEVEL, insert a generic multi-level alist LST.
2620 Associations with lists get {+} tags (to expand into more nodes) and
2621 those with positions just get a > as the indicator. {+} buttons will
2622 have the function EXPAND-FUN and the token is the CDR list. The token
2623 name will have the function FIND-FUN and not token."
2624 ;; Remove imenu rescan button
2625 (if (string= (car (car lst)) "*Rescan*")
2626 (setq lst (cdr lst)))
2627 ;; Adjust the list.
2628 (setq lst (speedbar-create-tag-hierarchy lst))
2629 ;; insert the parts
2630 (while lst
2631 (cond ((null (car-safe lst)) nil) ;this would be a separator
2632 ((or (numberp (cdr-safe (car-safe lst)))
2633 (markerp (cdr-safe (car-safe lst))))
2634 (speedbar-make-tag-line nil nil nil nil ;no expand button data
2635 (car (car lst)) ;button name
2636 find-fun ;function
2637 (cdr (car lst)) ;token is position
2638 'speedbar-tag-face
2639 (1+ level)))
2640 ((listp (cdr-safe (car-safe lst)))
2641 (speedbar-make-tag-line 'curly ?+ expand-fun (cdr (car lst))
2642 (car (car lst)) ;button name
2643 nil nil 'speedbar-tag-face
2644 (1+ level)))
2645 (t (speedbar-message "Ooops!")))
2646 (setq lst (cdr lst))))
2648 (defun speedbar-insert-imenu-list (indent lst)
2649 "At level INDENT, insert the imenu generated LST."
2650 (speedbar-insert-generic-list indent lst
2651 'speedbar-tag-expand
2652 'speedbar-tag-find))
2654 (defun speedbar-insert-etags-list (indent lst)
2655 "At level INDENT, insert the etags generated LST."
2656 (speedbar-insert-generic-list indent lst
2657 'speedbar-tag-expand
2658 'speedbar-tag-find))
2660 ;;; Timed functions
2662 (defun speedbar-update-contents ()
2663 "Generically update the contents of the speedbar buffer."
2664 (interactive)
2665 ;; Set the current special buffer
2666 (setq speedbar-desired-buffer nil)
2667 ;; Check for special modes
2668 (speedbar-maybe-add-localized-support (current-buffer))
2669 ;; Choose the correct method of doodling.
2670 (if (and speedbar-mode-specific-contents-flag
2671 (listp speedbar-special-mode-expansion-list)
2672 speedbar-special-mode-expansion-list
2673 (local-variable-p
2674 'speedbar-special-mode-expansion-list
2675 (current-buffer)))
2676 ;;(eq (get major-mode 'mode-class 'special)))
2677 (speedbar-update-special-contents)
2678 (speedbar-update-directory-contents)))
2680 (defun speedbar-update-directory-contents ()
2681 "Update the contents of the speedbar buffer based on the current directory."
2682 (let ((cbd (expand-file-name default-directory))
2683 cbd-parent
2684 (funclst (speedbar-initial-expansion-list))
2685 (cache speedbar-full-text-cache)
2686 ;; disable stealth during update
2687 (speedbar-stealthy-function-list nil)
2688 (use-cache nil)
2689 (expand-local nil)
2690 ;; Because there is a bug I can't find just yet
2691 (inhibit-quit nil))
2692 (save-excursion
2693 (set-buffer speedbar-buffer)
2694 ;; If we are updating contents to where we are, then this is
2695 ;; really a request to update existing contents, so we must be
2696 ;; careful with our text cache!
2697 (if (member cbd speedbar-shown-directories)
2698 (progn
2699 (setq cache nil)
2700 ;; If the current directory is not the last element in the dir
2701 ;; list, then we ALSO need to zap the list of expanded directories
2702 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2703 (setq speedbar-shown-directories (list cbd))))
2705 ;; Build cbd-parent, and see if THAT is in the current shown
2706 ;; directories. First, go through pains to get the parent directory
2707 (if (and speedbar-smart-directory-expand-flag
2708 (save-match-data
2709 (setq cbd-parent cbd)
2710 (if (string-match "[/\\]$" cbd-parent)
2711 (setq cbd-parent (substring cbd-parent 0
2712 (match-beginning 0))))
2713 (setq cbd-parent (file-name-directory cbd-parent)))
2714 (member cbd-parent speedbar-shown-directories))
2715 (setq expand-local t)
2717 ;; If this directory is NOT in the current list of available
2718 ;; paths, then use the cache, and set the cache to our new
2719 ;; value. Make sure to unhighlight the current file, or if we
2720 ;; come back to this directory, it might be a different file
2721 ;; and then we get a mess!
2722 (if (> (point-max) 1)
2723 (progn
2724 (speedbar-clear-current-file)
2725 (setq speedbar-full-text-cache
2726 (cons speedbar-shown-directories (buffer-string)))))
2728 ;; Check if our new directory is in the list of directories
2729 ;; shown in the text-cache
2730 (if (member cbd (car cache))
2731 (setq speedbar-shown-directories (car cache)
2732 use-cache t)
2733 ;; default the shown directories to this list...
2734 (setq speedbar-shown-directories (list cbd)))
2736 (if (not expand-local) (setq speedbar-last-selected-file nil))
2737 (speedbar-with-writable
2738 (if (and expand-local
2739 ;; Find this directory as a speedbar node.
2740 (speedbar-path-line cbd))
2741 ;; Open it.
2742 (speedbar-expand-line)
2743 (erase-buffer)
2744 (cond (use-cache
2745 (setq default-directory
2746 (nth (1- (length speedbar-shown-directories))
2747 speedbar-shown-directories))
2748 (insert (cdr cache)))
2750 (while funclst
2751 (setq default-directory cbd)
2752 (funcall (car funclst) cbd 0)
2753 (setq funclst (cdr funclst))))))
2754 (goto-char (point-min)))))
2755 (speedbar-reconfigure-keymaps))
2757 (defun speedbar-update-special-contents ()
2758 "Use the mode-specific variable to fill in the speedbar buffer.
2759 This should only be used by modes classified as special."
2760 (let ((funclst speedbar-special-mode-expansion-list)
2761 (specialbuff (current-buffer)))
2762 (save-excursion
2763 (setq speedbar-desired-buffer specialbuff)
2764 (set-buffer speedbar-buffer)
2765 ;; If we are leaving a directory, cache it.
2766 (if (not speedbar-shown-directories)
2767 ;; Do nothing
2769 ;; Clean up directory maintenance stuff
2770 (speedbar-clear-current-file)
2771 (setq speedbar-full-text-cache
2772 (cons speedbar-shown-directories (buffer-string))
2773 speedbar-shown-directories nil))
2774 ;; Now fill in the buffer with our newly found specialized list.
2775 (speedbar-with-writable
2776 (while funclst
2777 ;; We do not erase the buffer because these functions may
2778 ;; decide NOT to update themselves.
2779 (funcall (car funclst) specialbuff)
2780 (setq funclst (cdr funclst))))
2781 (goto-char (point-min))))
2782 (speedbar-reconfigure-keymaps))
2784 (defun speedbar-timer-fn ()
2785 "Run whenever Emacs is idle to update the speedbar item."
2786 (if (not (and (frame-live-p speedbar-frame)
2787 (frame-live-p speedbar-attached-frame)))
2788 (speedbar-set-timer nil)
2789 ;; Save all the match data so that we don't mess up executing fns
2790 (save-match-data
2791 ;; Only do stuff if the frame is visible, not an icon, and if
2792 ;; it is currently flagged to do something.
2793 (if (and speedbar-update-flag
2794 (frame-visible-p speedbar-frame)
2795 (not (eq (frame-visible-p speedbar-frame) 'icon)))
2796 (let ((af (selected-frame)))
2797 (save-window-excursion
2798 (select-frame speedbar-attached-frame)
2799 ;; make sure we at least choose a window to
2800 ;; get a good directory from
2801 (if (window-minibuffer-p (selected-window))
2803 ;; Check for special modes
2804 (speedbar-maybe-add-localized-support (current-buffer))
2805 ;; Update for special mode all the time!
2806 (if (and speedbar-mode-specific-contents-flag
2807 (listp speedbar-special-mode-expansion-list)
2808 speedbar-special-mode-expansion-list
2809 (local-variable-p
2810 'speedbar-special-mode-expansion-list
2811 (current-buffer)))
2812 ;;(eq (get major-mode 'mode-class 'special)))
2813 (progn
2814 (if (<= 2 speedbar-verbosity-level)
2815 (speedbar-message
2816 "Updating speedbar to special mode: %s..."
2817 major-mode))
2818 (speedbar-update-special-contents)
2819 (if (<= 2 speedbar-verbosity-level)
2820 (progn
2821 (speedbar-message
2822 "Updating speedbar to special mode: %s...done"
2823 major-mode)
2824 (speedbar-message nil))))
2825 ;; Update all the contents if directories change!
2826 (if (or (member (expand-file-name default-directory)
2827 speedbar-shown-directories)
2828 (and speedbar-ignored-path-regexp
2829 (string-match
2830 speedbar-ignored-path-regexp
2831 (expand-file-name default-directory)))
2832 (member major-mode speedbar-ignored-modes)
2833 (eq af speedbar-frame)
2834 (not (buffer-file-name)))
2836 (if (<= 1 speedbar-verbosity-level)
2837 (speedbar-message "Updating speedbar to: %s..."
2838 default-directory))
2839 (speedbar-update-directory-contents)
2840 (if (<= 1 speedbar-verbosity-level)
2841 (progn
2842 (speedbar-message "Updating speedbar to: %s...done"
2843 default-directory)
2844 (speedbar-message nil)))))
2845 (select-frame af)))
2846 ;; Now run stealthy updates of time-consuming items
2847 (speedbar-stealthy-updates)))
2848 ;; Now run the mouse tracking system
2849 (speedbar-show-info-under-mouse)))
2850 (run-hooks 'speedbar-timer-hook))
2853 ;;; Stealthy activities
2855 (defvar speedbar-stealthy-update-recurse nil
2856 "Recursion avoidance variable for stealthy update.")
2858 (defun speedbar-stealthy-updates ()
2859 "For a given speedbar, run all items in the stealthy function list.
2860 Each item returns t if it completes successfully, or nil if
2861 interrupted by the user."
2862 (if (not speedbar-stealthy-update-recurse)
2863 (let ((l (speedbar-initial-stealthy-functions))
2864 (speedbar-stealthy-update-recurse t))
2865 (unwind-protect
2866 (speedbar-with-writable
2867 (while (and l (funcall (car l)))
2868 ;;(sit-for 0)
2869 (setq l (cdr l))))
2870 ;;(speedbar-message "Exit with %S" (car l))
2871 ))))
2873 (defun speedbar-reset-scanners ()
2874 "Reset any variables used by functions in the stealthy list as state.
2875 If new functions are added, their state needs to be updated here."
2876 (setq speedbar-vc-to-do-point t
2877 speedbar-obj-to-do-point t)
2878 (run-hooks 'speedbar-scanner-reset-hook)
2881 (defun speedbar-find-selected-file (file)
2882 "Go to the line where FILE is."
2883 (goto-char (point-min))
2884 (let ((m nil))
2885 (while (and (setq m (re-search-forward
2886 (concat " \\(" (regexp-quote (file-name-nondirectory file))
2887 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2888 nil t))
2889 (not (string= file
2890 (concat
2891 (speedbar-line-path
2892 (save-excursion
2893 (goto-char (match-beginning 0))
2894 (beginning-of-line)
2895 (save-match-data
2896 (looking-at "[0-9]+:")
2897 (string-to-number (match-string 0)))))
2898 (match-string 1))))))
2899 (if m
2900 (progn
2901 (goto-char (match-beginning 1))
2902 (match-string 1)))))
2904 (defun speedbar-clear-current-file ()
2905 "Locate the file thought to be current, and remove its highlighting."
2906 (save-excursion
2907 (set-buffer speedbar-buffer)
2908 (if speedbar-last-selected-file
2909 (speedbar-with-writable
2910 (if (speedbar-find-selected-file speedbar-last-selected-file)
2911 (put-text-property (match-beginning 1)
2912 (match-end 1)
2913 'face
2914 'speedbar-file-face))))))
2916 (defun speedbar-update-current-file ()
2917 "Find the current file, and update our visuals to indicate its name.
2918 This is specific to file names. If the file name doesn't show up, but
2919 it should be in the list, then the directory cache needs to be
2920 updated."
2921 (let* ((lastf (selected-frame))
2922 (newcfd (save-excursion
2923 (select-frame speedbar-attached-frame)
2924 (let ((rf (if (buffer-file-name)
2925 (buffer-file-name)
2926 nil)))
2927 (select-frame lastf)
2928 rf)))
2929 (newcf (if newcfd newcfd))
2930 (lastb (current-buffer))
2931 (sucf-recursive (boundp 'sucf-recursive))
2932 (case-fold-search read-file-name-completion-ignore-case))
2933 (if (and newcf
2934 ;; check here, that way we won't refresh to newcf until
2935 ;; its been written, thus saving ourselves some time
2936 (file-exists-p newcf)
2937 (not (string= newcf speedbar-last-selected-file)))
2938 (progn
2939 ;; It is important to select the frame, otherwise the window
2940 ;; we want the cursor to move in will not be updated by the
2941 ;; search-forward command.
2942 (select-frame speedbar-frame)
2943 ;; Remove the old file...
2944 (speedbar-clear-current-file)
2945 ;; now highlight the new one.
2946 (set-buffer speedbar-buffer)
2947 (speedbar-with-writable
2948 (if (speedbar-find-selected-file newcf)
2949 ;; put the property on it
2950 (put-text-property (match-beginning 1)
2951 (match-end 1)
2952 'face
2953 'speedbar-selected-face)
2954 ;; Oops, it's not in the list. Should it be?
2955 (if (and (string-match speedbar-file-regexp newcf)
2956 (string= (file-name-directory newcfd)
2957 (expand-file-name default-directory)))
2958 ;; yes, it is (we will ignore unknowns for now...)
2959 (progn
2960 (speedbar-refresh)
2961 (if (speedbar-find-selected-file newcf)
2962 ;; put the property on it
2963 (put-text-property (match-beginning 1)
2964 (match-end 1)
2965 'face
2966 'speedbar-selected-face)))
2967 ;; if it's not in there now, whatever...
2969 (setq speedbar-last-selected-file newcf))
2970 (if (not sucf-recursive)
2971 (progn
2972 (speedbar-center-buffer-smartly)
2973 (speedbar-position-cursor-on-line)
2975 (set-buffer lastb)
2976 (select-frame lastf)
2978 ;; return that we are done with this activity.
2981 (defun speedbar-add-indicator (indicator-string &optional replace-this)
2982 "Add INDICATOR-STRING to the end of this speedbar line.
2983 If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2984 the existing indicator is removed. If there is already an
2985 indicator, then do not add a space."
2986 (beginning-of-line)
2987 ;; The nature of the beast: Assume we are in "the right place"
2988 (end-of-line)
2989 (skip-chars-backward (concat " " speedbar-vc-indicator
2990 (car speedbar-obj-indicator)
2991 (cdr speedbar-obj-indicator)))
2992 (if (and (not (looking-at speedbar-indicator-regex))
2993 (not (string= indicator-string " ")))
2994 (insert speedbar-indicator-separator))
2995 (speedbar-with-writable
2996 (save-excursion
2997 (if (and replace-this
2998 (re-search-forward replace-this (save-excursion (end-of-line)
2999 (point))
3001 (delete-region (match-beginning 0) (match-end 0))))
3002 (end-of-line)
3003 (if (not (string= " " indicator-string))
3004 (insert indicator-string))))
3006 ;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
3007 ;; Steven L Baur <steve@xemacs.org> said this was important:
3008 (eval-when-compile (or (featurep 'xemacs)
3009 (condition-case () (require 'efs)
3010 (error (require 'ange-ftp)))))
3012 (defun speedbar-check-vc ()
3013 "Scan all files in a directory, and for each see if it's checked out.
3014 See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
3015 to add more types of version control systems."
3016 ;; Check for to-do to be reset. If reset but no RCS is available
3017 ;; then set to nil (do nothing) otherwise, start at the beginning
3018 (save-excursion
3019 (set-buffer speedbar-buffer)
3020 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
3021 (speedbar-vc-check-dir-p default-directory)
3022 (not (or (and (featurep 'ange-ftp)
3023 (string-match
3024 (car (if speedbar-xemacsp
3025 ange-ftp-path-format
3026 ange-ftp-name-format))
3027 (expand-file-name default-directory)))
3028 ;; efs support: Bob Weiner
3029 (and (featurep 'efs)
3030 (string-match
3031 (car efs-path-regexp)
3032 (expand-file-name default-directory))))))
3033 (setq speedbar-vc-to-do-point 0))
3034 (if (numberp speedbar-vc-to-do-point)
3035 (progn
3036 (goto-char speedbar-vc-to-do-point)
3037 (while (and (not (input-pending-p))
3038 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
3039 nil t))
3040 (setq speedbar-vc-to-do-point (point))
3041 (if (speedbar-check-vc-this-line (match-string 1))
3042 (speedbar-add-indicator speedbar-vc-indicator
3043 (regexp-quote speedbar-vc-indicator))
3044 (speedbar-add-indicator " "
3045 (regexp-quote speedbar-vc-indicator))))
3046 (if (input-pending-p)
3047 ;; return that we are incomplete
3049 ;; we are done, set to-do to nil
3050 (setq speedbar-vc-to-do-point nil)
3051 ;; and return t
3053 t)))
3055 (defun speedbar-check-vc-this-line (depth)
3056 "Return t if the file on this line is check of of a version control system.
3057 Parameter DEPTH is a string with the current depth of indentation of
3058 the file being checked."
3059 (let* ((d (string-to-int depth))
3060 (f (speedbar-line-path d))
3061 (fn (buffer-substring-no-properties
3062 ;; Skip-chars: thanks ptype@dra.hmg.gb
3063 (point) (progn
3064 (skip-chars-forward "^ "
3065 (save-excursion (end-of-line)
3066 (point)))
3067 (point))))
3068 (fulln (concat f fn)))
3069 (if (<= 2 speedbar-verbosity-level)
3070 (speedbar-message "Speedbar vc check...%s" fulln))
3071 (and (file-writable-p fulln)
3072 (speedbar-this-file-in-vc f fn))))
3074 (defun speedbar-vc-check-dir-p (path)
3075 "Return t if we should bother checking PATH for version control files.
3076 This can be overloaded to add new types of version control systems."
3078 ;; Local RCS
3079 (file-exists-p (concat path "RCS/"))
3080 ;; Local SCCS
3081 (file-exists-p (concat path "SCCS/"))
3082 ;; Remote SCCS project
3083 (let ((proj-dir (getenv "PROJECTDIR")))
3084 (if proj-dir
3085 (file-exists-p (concat proj-dir "/SCCS"))
3086 nil))
3087 ;; User extension
3088 (run-hook-with-args 'speedbar-vc-path-enable-hook path)
3091 (defun speedbar-this-file-in-vc (path name)
3092 "Check to see if the file in PATH with NAME is in a version control system.
3093 You can add new VC systems by overriding this function. You can
3094 optimize this function by overriding it and only doing those checks
3095 that will occur on your system."
3097 ;; RCS file name
3098 (file-exists-p (concat path "RCS/" name ",v"))
3099 (file-exists-p (concat path "RCS/" name))
3100 ;; Local SCCS file name
3101 (file-exists-p (concat path "SCCS/s." name))
3102 ;; Remote SCCS file name
3103 (let ((proj-dir (getenv "PROJECTDIR")))
3104 (if proj-dir
3105 (file-exists-p (concat proj-dir "/SCCS/s." name))
3106 nil))
3107 ;; User extension
3108 (run-hook-with-args 'speedbar-vc-in-control-hook path name)
3111 ;; Objet File scanning
3112 (defun speedbar-check-objects ()
3113 "Scan all files in a directory, and for each see if there is an object.
3114 See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
3115 to add more object types."
3116 ;; Check for to-do to be reset. If reset but no RCS is available
3117 ;; then set to nil (do nothing) otherwise, start at the beginning
3118 (save-excursion
3119 (set-buffer speedbar-buffer)
3120 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
3121 (setq speedbar-obj-to-do-point 0))
3122 (if (numberp speedbar-obj-to-do-point)
3123 (progn
3124 (goto-char speedbar-obj-to-do-point)
3125 (while (and (not (input-pending-p))
3126 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
3127 nil t))
3128 (setq speedbar-obj-to-do-point (point))
3129 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
3130 (if (not ind) (setq ind " "))
3131 (speedbar-add-indicator ind (concat
3132 (car speedbar-obj-indicator)
3133 "\\|"
3134 (cdr speedbar-obj-indicator)))))
3135 (if (input-pending-p)
3136 ;; return that we are incomplete
3138 ;; we are done, set to-do to nil
3139 (setq speedbar-obj-to-do-point nil)
3140 ;; and return t
3142 t)))
3144 (defun speedbar-check-obj-this-line (depth)
3145 "Return t if the file on this line has an associated object.
3146 Parameter DEPTH is a string with the current depth of indentation of
3147 the file being checked."
3148 (let* ((d (string-to-int depth))
3149 (f (speedbar-line-path d))
3150 (fn (buffer-substring-no-properties
3151 ;; Skip-chars: thanks ptype@dra.hmg.gb
3152 (point) (progn
3153 (skip-chars-forward "^ "
3154 (save-excursion (end-of-line)
3155 (point)))
3156 (point))))
3157 (fulln (concat f fn)))
3158 (if (<= 2 speedbar-verbosity-level)
3159 (speedbar-message "Speedbar obj check...%s" fulln))
3160 (let ((oa speedbar-obj-alist))
3161 (while (and oa (not (string-match (car (car oa)) fulln)))
3162 (setq oa (cdr oa)))
3163 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
3164 (cdr (car oa))))))
3166 ;; Find out if the object is out of date or not.
3167 (let ((date1 (nth 5 (file-attributes fulln)))
3168 (date2 (nth 5 (file-attributes (concat
3169 (file-name-sans-extension fulln)
3170 (cdr (car oa)))))))
3171 (if (or (< (car date1) (car date2))
3172 (and (= (car date1) (car date2))
3173 (< (nth 1 date1) (nth 1 date2))))
3174 (car speedbar-obj-indicator)
3175 (cdr speedbar-obj-indicator)))))))
3177 ;;; Clicking Activity
3179 (defun speedbar-mouse-set-point (e)
3180 "Set POINT based on event E.
3181 Handle clicking on images in XEmacs."
3182 (if (and (fboundp 'event-over-glyph-p) (event-over-glyph-p e))
3183 ;; We are in XEmacs, and clicked on a picture
3184 (let ((ext (event-glyph-extent e)))
3185 ;; This position is back inside the extent where the
3186 ;; junk we pushed into the property list lives.
3187 (if (extent-end-position ext)
3188 (goto-char (1- (extent-end-position ext)))
3189 (mouse-set-point e)))
3190 ;; We are not in XEmacs, OR we didn't click on a picture.
3191 (mouse-set-point e)))
3193 (defun speedbar-quick-mouse (e)
3194 "Since mouse events are strange, this will keep the mouse nicely positioned.
3195 This should be bound to mouse event E."
3196 (interactive "e")
3197 (speedbar-mouse-set-point e)
3198 (speedbar-position-cursor-on-line)
3201 (defun speedbar-position-cursor-on-line ()
3202 "Position the cursor on a line."
3203 (let ((oldpos (point)))
3204 (beginning-of-line)
3205 (if (looking-at "[0-9]+:\\s-*..?.? ")
3206 (goto-char (1- (match-end 0)))
3207 (goto-char oldpos))))
3209 (defun speedbar-power-click (e)
3210 "Activate any speedbar button as a power click.
3211 A power click will dispose of cached data (if available) or bring a buffer
3212 up into a different window.
3213 This should be bound to mouse event E."
3214 (interactive "e")
3215 (let ((speedbar-power-click t))
3216 (speedbar-click e)))
3218 (defun speedbar-click (e)
3219 "Activate any speedbar buttons where the mouse is clicked.
3220 This must be bound to a mouse event. A button is any location of text
3221 with a mouse face that has a text property called `speedbar-function'.
3222 This should be bound to mouse event E."
3223 (interactive "e")
3224 (speedbar-mouse-set-point e)
3225 (speedbar-do-function-pointer)
3226 (speedbar-quick-mouse e))
3228 (defun speedbar-double-click (e)
3229 "Activate any speedbar buttons where the mouse is clicked.
3230 This must be bound to a mouse event. A button is any location of text
3231 with a mouse face that has a text property called `speedbar-function'.
3232 This should be bound to mouse event E."
3233 (interactive "e")
3234 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
3235 (cond ((eq (car e) 'down-mouse-1)
3236 (speedbar-mouse-set-point e))
3237 ((eq (car e) 'mouse-1)
3238 (speedbar-quick-mouse e))
3239 ((or (eq (car e) 'double-down-mouse-1)
3240 (eq (car e) 'triple-down-mouse-1))
3241 (speedbar-mouse-set-point e)
3242 (speedbar-do-function-pointer)
3243 (speedbar-quick-mouse e))))
3245 (defun speedbar-do-function-pointer ()
3246 "Look under the cursor and examine the text properties.
3247 From this extract the file/tag name, token, indentation level and call
3248 a function if appropriate."
3249 (let* ((fn (get-text-property (point) 'speedbar-function))
3250 (tok (get-text-property (point) 'speedbar-token))
3251 ;; The 1-,+ is safe because scaning starts AFTER the point
3252 ;; specified. This lets the search include the character the
3253 ;; cursor is on.
3254 (tp (previous-single-property-change
3255 (1+ (point)) 'speedbar-function))
3256 (np (next-single-property-change
3257 (point) 'speedbar-function))
3258 (txt (buffer-substring-no-properties (or tp (point-min))
3259 (or np (point-max))))
3260 (dent (save-excursion (beginning-of-line)
3261 (string-to-number
3262 (if (looking-at "[0-9]+")
3263 (buffer-substring-no-properties
3264 (match-beginning 0) (match-end 0))
3265 "0")))))
3266 ;;(speedbar-message "%S:%S:%S:%s" fn tok txt dent)
3267 (and fn (funcall fn txt tok dent)))
3268 (speedbar-position-cursor-on-line))
3270 ;;; Reading info from the speedbar buffer
3272 (defun speedbar-line-text (&optional p)
3273 "Retrieve the text after prefix junk for the current line.
3274 Optional argument P is where to start the search from."
3275 (save-excursion
3276 (if p (goto-char p))
3277 (beginning-of-line)
3278 (if (looking-at (concat
3279 "\\([0-9]+\\): *[[<{][-+?][]>}] \\([^ \n]+\\)\\("
3280 speedbar-indicator-regex "\\)?"))
3281 (match-string 2)
3282 nil)))
3284 (defun speedbar-line-token (&optional p)
3285 "Retrieve the token information after the prefix junk for the current line.
3286 Optional argument P is where to start the search from."
3287 (save-excursion
3288 (if p (goto-char p))
3289 (beginning-of-line)
3290 (if (looking-at (concat
3291 "\\([0-9]+\\): *[[<{]?[-+?=][]>}@()|] \\([^ \n]+\\)\\("
3292 speedbar-indicator-regex "\\)?"))
3293 (progn
3294 (goto-char (match-beginning 2))
3295 (get-text-property (point) 'speedbar-token))
3296 nil)))
3298 (defun speedbar-line-file (&optional p)
3299 "Retrieve the file or whatever from the line at point P.
3300 The return value is a string representing the file. If it is a
3301 directory, then it is the directory name."
3302 (save-match-data
3303 (let ((f (speedbar-line-text p)))
3304 (if f
3305 (let* ((depth (string-to-int (match-string 1)))
3306 (path (speedbar-line-path depth)))
3307 (if (file-exists-p (concat path f))
3308 (concat path f)
3309 nil))
3310 nil))))
3312 (defun speedbar-goto-this-file (file)
3313 "If FILE is displayed, go to this line and return t.
3314 Otherwise do not move and return nil."
3315 (let ((path (substring (file-name-directory (expand-file-name file))
3316 (length (expand-file-name default-directory))))
3317 (dest (point)))
3318 (save-match-data
3319 (goto-char (point-min))
3320 ;; scan all the directories
3321 (while (and path (not (eq path t)))
3322 (if (string-match "^[/\\]?\\([^/\\]+\\)" path)
3323 (let ((pp (match-string 1 path)))
3324 (if (save-match-data
3325 (re-search-forward (concat "> " (regexp-quote pp) "$")
3326 nil t))
3327 (setq path (substring path (match-end 1)))
3328 (setq path nil)))
3329 (setq path t)))
3330 ;; find the file part
3331 (if (or (not path) (string= (file-name-nondirectory file) ""))
3332 ;; only had a dir part
3333 (if path
3334 (progn
3335 (speedbar-position-cursor-on-line)
3337 (goto-char dest) nil)
3338 ;; find the file part
3339 (let ((nd (file-name-nondirectory file)))
3340 (if (re-search-forward
3341 (concat "] \\(" (regexp-quote nd)
3342 "\\)\\(" speedbar-indicator-regex "\\)$")
3343 nil t)
3344 (progn
3345 (speedbar-position-cursor-on-line)
3347 (goto-char dest)
3348 nil))))))
3350 (defun speedbar-line-path (&optional depth)
3351 "Retrieve the pathname associated with the current line.
3352 This may require traversing backwards from DEPTH and combining the default
3353 directory with these items. This function is replaceable in
3354 `speedbar-mode-functions-list' as `speedbar-line-path'."
3355 (let ((rf (speedbar-fetch-replacement-function 'speedbar-line-path)))
3356 (if rf (funcall rf depth) default-directory)))
3358 (defun speedbar-files-line-path (&optional depth)
3359 "Retrieve the pathname associated with the current line.
3360 This may require traversing backwards from DEPTH and combining the default
3361 directory with these items."
3362 (save-excursion
3363 (save-match-data
3364 (if (not depth)
3365 (progn
3366 (beginning-of-line)
3367 (looking-at "^\\([0-9]+\\):")
3368 (setq depth (string-to-int (match-string 1)))))
3369 (let ((path nil))
3370 (setq depth (1- depth))
3371 (while (/= depth -1)
3372 (if (not (re-search-backward (format "^%d:" depth) nil t))
3373 (error "Error building path of tag")
3374 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)$")
3375 (setq path (concat (buffer-substring-no-properties
3376 (match-beginning 1) (match-end 1))
3378 path)))
3379 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)$")
3380 ;; This is the start of our path.
3381 (setq path (buffer-substring-no-properties
3382 (match-beginning 1) (match-end 1))))))
3383 (setq depth (1- depth)))
3384 (if (and path
3385 (string-match (concat speedbar-indicator-regex "$")
3386 path))
3387 (setq path (substring path 0 (match-beginning 0))))
3388 (concat default-directory path)))))
3390 (defun speedbar-path-line (path)
3391 "Position the cursor on the line specified by PATH."
3392 (save-match-data
3393 (if (string-match "[/\\]$" path)
3394 (setq path (substring path 0 (match-beginning 0))))
3395 (let ((nomatch t) (depth 0)
3396 (fname (file-name-nondirectory path))
3397 (pname (file-name-directory path)))
3398 (if (not (member pname speedbar-shown-directories))
3399 (error "Internal Error: File %s not shown in speedbar" path))
3400 (goto-char (point-min))
3401 (while (and nomatch
3402 (re-search-forward
3403 (concat "[]>] \\(" (regexp-quote fname)
3404 "\\)\\(" speedbar-indicator-regex "\\)?$")
3405 nil t))
3406 (beginning-of-line)
3407 (looking-at "\\([0-9]+\\):")
3408 (setq depth (string-to-int (match-string 0))
3409 nomatch (not (string= pname (speedbar-line-path depth))))
3410 (end-of-line))
3411 (beginning-of-line)
3412 (not nomatch))))
3414 (defun speedbar-edit-line ()
3415 "Edit whatever tag or file is on the current speedbar line."
3416 (interactive)
3417 (or (save-excursion
3418 (beginning-of-line)
3419 ;; If this fails, then it is a non-standard click, and as such,
3420 ;; perfectly allowed.
3421 (if (re-search-forward "[]>?}] [^ ]"
3422 (save-excursion (end-of-line) (point))
3424 (progn
3425 (forward-char -1)
3426 (speedbar-do-function-pointer))
3427 nil))
3428 (speedbar-do-function-pointer)))
3430 (defun speedbar-expand-line (&optional arg)
3431 "Expand the line under the cursor.
3432 With universal argument ARG, flush cached data."
3433 (interactive "P")
3434 (beginning-of-line)
3435 (let ((speedbar-power-click arg))
3436 (condition-case nil
3437 (progn
3438 (re-search-forward ":\\s-*.\\+. "
3439 (save-excursion (end-of-line) (point)))
3440 (forward-char -2)
3441 (speedbar-do-function-pointer))
3442 (error (speedbar-position-cursor-on-line)))))
3444 (defun speedbar-flush-expand-line ()
3445 "Expand the line under the cursor and flush any cached information."
3446 (interactive)
3447 (speedbar-expand-line 1))
3449 (defun speedbar-contract-line ()
3450 "Contract the line under the cursor."
3451 (interactive)
3452 (beginning-of-line)
3453 (condition-case nil
3454 (progn
3455 (re-search-forward ":\\s-*.-. "
3456 (save-excursion (end-of-line) (point)))
3457 (forward-char -2)
3458 (speedbar-do-function-pointer))
3459 (error (speedbar-position-cursor-on-line))))
3461 (if speedbar-xemacsp
3462 (defalias 'speedbar-mouse-event-p 'button-press-event-p)
3463 (defun speedbar-mouse-event-p (event)
3464 "Return t if the event is a mouse related event."
3465 ;; And Emacs does it this way
3466 (if (and (listp event)
3467 (member (event-basic-type event)
3468 '(mouse-1 mouse-2 mouse-3)))
3470 nil)))
3472 (defun speedbar-maybee-jump-to-attached-frame ()
3473 "Jump to the attached frame ONLY if this was not a mouse event."
3474 (if (or (not (speedbar-mouse-event-p last-input-event))
3475 speedbar-activity-change-focus-flag)
3476 (progn
3477 (select-frame speedbar-attached-frame)
3478 (other-frame 0))))
3480 (defun speedbar-find-file (text token indent)
3481 "Speedbar click handler for filenames.
3482 TEXT, the file will be displayed in the attached frame.
3483 TOKEN is unused, but required by the click handler. INDENT is the
3484 current indentation level."
3485 (let ((cdd (speedbar-line-path indent)))
3486 (speedbar-find-file-in-frame (concat cdd text))
3487 (speedbar-stealthy-updates)
3488 (run-hooks 'speedbar-visiting-file-hook)
3489 ;; Reset the timer with a new timeout when cliking a file
3490 ;; in case the user was navigating directories, we can cancel
3491 ;; that other timer.
3492 (speedbar-set-timer speedbar-update-speed))
3493 (speedbar-maybee-jump-to-attached-frame))
3495 (defun speedbar-dir-follow (text token indent)
3496 "Speedbar click handler for directory names.
3497 Clicking a directory will cause the speedbar to list files in
3498 the subdirectory TEXT. TOKEN is an unused requirement. The
3499 subdirectory chosen will be at INDENT level."
3500 (setq default-directory
3501 (concat (expand-file-name (concat (speedbar-line-path indent) text))
3502 "/"))
3503 ;; Because we leave speedbar as the current buffer,
3504 ;; update contents will change directory without
3505 ;; having to touch the attached frame. Turn off smart expand just
3506 ;; in case.
3507 (let ((speedbar-smart-directory-expand-flag nil))
3508 (speedbar-update-contents))
3509 (speedbar-set-timer speedbar-navigating-speed)
3510 (setq speedbar-last-selected-file nil)
3511 (speedbar-stealthy-updates))
3513 (defun speedbar-delete-subblock (indent)
3514 "Delete text from point to indentation level INDENT or greater.
3515 Handles end-of-sublist smartly."
3516 (speedbar-with-writable
3517 (save-excursion
3518 (end-of-line) (forward-char 1)
3519 (let ((start (point)))
3520 (while (and (looking-at "^\\([0-9]+\\):")
3521 (> (string-to-int (match-string 1)) indent)
3522 (not (eobp)))
3523 (forward-line 1)
3524 (beginning-of-line))
3525 (delete-region start (point))))))
3527 (defun speedbar-dired (text token indent)
3528 "Speedbar click handler for directory expand button.
3529 Clicking this button expands or contracts a directory. TEXT is the
3530 button clicked which has either a + or -. TOKEN is the directory to be
3531 expanded. INDENT is the current indentation level."
3532 (cond ((string-match "+" text) ;we have to expand this dir
3533 (setq speedbar-shown-directories
3534 (cons (expand-file-name
3535 (concat (speedbar-line-path indent) token "/"))
3536 speedbar-shown-directories))
3537 (speedbar-change-expand-button-char ?-)
3538 (speedbar-reset-scanners)
3539 (save-excursion
3540 (end-of-line) (forward-char 1)
3541 (speedbar-with-writable
3542 (speedbar-default-directory-list
3543 (concat (speedbar-line-path indent) token "/")
3544 (1+ indent)))))
3545 ((string-match "-" text) ;we have to contract this node
3546 (speedbar-reset-scanners)
3547 (let ((oldl speedbar-shown-directories)
3548 (newl nil)
3549 (td (expand-file-name
3550 (concat (speedbar-line-path indent) token))))
3551 (while oldl
3552 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3553 (setq newl (cons (car oldl) newl)))
3554 (setq oldl (cdr oldl)))
3555 (setq speedbar-shown-directories (nreverse newl)))
3556 (speedbar-change-expand-button-char ?+)
3557 (speedbar-delete-subblock indent)
3559 (t (error "Ooops... not sure what to do")))
3560 (speedbar-center-buffer-smartly)
3561 (setq speedbar-last-selected-file nil)
3562 (save-excursion (speedbar-stealthy-updates)))
3564 (defun speedbar-directory-buttons-follow (text token indent)
3565 "Speedbar click handler for default directory buttons.
3566 TEXT is the button clicked on. TOKEN is the directory to follow.
3567 INDENT is the current indentation level and is unused."
3568 (if (string-match "^[A-z]:$" token)
3569 (setq default-directory (concat token "/"))
3570 (setq default-directory token))
3571 ;; Because we leave speedbar as the current buffer,
3572 ;; update contents will change directory without
3573 ;; having to touch the attached frame.
3574 (speedbar-update-contents)
3575 (speedbar-set-timer speedbar-navigating-speed))
3577 (defun speedbar-tag-file (text token indent)
3578 "The cursor is on a selected line. Expand the tags in the specified file.
3579 The parameter TEXT and TOKEN are required, where TEXT is the button
3580 clicked, and TOKEN is the file to expand. INDENT is the current
3581 indentation level."
3582 (cond ((string-match "+" text) ;we have to expand this file
3583 (let* ((fn (expand-file-name (concat (speedbar-line-path indent)
3584 token)))
3585 (mode nil)
3586 (lst (speedbar-fetch-dynamic-tags fn)))
3587 ;; if no list, then remove expando button
3588 (if (not lst)
3589 (speedbar-change-expand-button-char ??)
3590 (speedbar-change-expand-button-char ?-)
3591 (speedbar-with-writable
3592 (save-excursion
3593 (end-of-line) (forward-char 1)
3594 (funcall (car lst) indent (cdr lst)))))))
3595 ((string-match "-" text) ;we have to contract this node
3596 (speedbar-change-expand-button-char ?+)
3597 (speedbar-delete-subblock indent))
3598 (t (error "Ooops... not sure what to do")))
3599 (speedbar-center-buffer-smartly))
3601 (defun speedbar-tag-find (text token indent)
3602 "For the tag TEXT in a file TOKEN, go to that position.
3603 INDENT is the current indentation level."
3604 (let ((file (speedbar-line-path indent)))
3605 (speedbar-find-file-in-frame file)
3606 (save-excursion (speedbar-stealthy-updates))
3607 ;; Reset the timer with a new timeout when cliking a file
3608 ;; in case the user was navigating directories, we can cancel
3609 ;; that other timer.
3610 (speedbar-set-timer speedbar-update-speed)
3611 (goto-char token)
3612 (run-hooks 'speedbar-visiting-tag-hook)
3613 ;;(recenter)
3614 (speedbar-maybee-jump-to-attached-frame)
3617 (defun speedbar-tag-expand (text token indent)
3618 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3619 Etags does not support this feature. TEXT will be the button
3620 string. TOKEN will be the list, and INDENT is the current indentation
3621 level."
3622 (cond ((string-match "+" text) ;we have to expand this file
3623 (speedbar-change-expand-button-char ?-)
3624 (speedbar-with-writable
3625 (save-excursion
3626 (end-of-line) (forward-char 1)
3627 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
3628 'speedbar-tag-find))))
3629 ((string-match "-" text) ;we have to contract this node
3630 (speedbar-change-expand-button-char ?+)
3631 (speedbar-delete-subblock indent))
3632 (t (error "Ooops... not sure what to do")))
3633 (speedbar-center-buffer-smartly))
3635 ;;; Loading files into the attached frame.
3637 (defun speedbar-find-file-in-frame (file)
3638 "This will load FILE into the speedbar attached frame.
3639 If the file is being displayed in a different frame already, then raise that
3640 frame instead."
3641 (let* ((buff (find-file-noselect file))
3642 (bwin (get-buffer-window buff 0)))
3643 (if bwin
3644 (progn
3645 (select-window bwin)
3646 (raise-frame (window-frame bwin)))
3647 (if speedbar-power-click
3648 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3649 (select-frame speedbar-attached-frame)
3650 (switch-to-buffer buff))))
3653 ;;; Centering Utility
3655 (defun speedbar-center-buffer-smartly ()
3656 "Recenter a speedbar buffer so the current indentation level is all visible.
3657 This assumes that the cursor is on a file, or tag of a file which the user is
3658 interested in."
3659 (if (<= (count-lines (point-min) (point-max))
3660 (1- (window-height (selected-window))))
3661 ;; whole buffer fits
3662 (let ((cp (point)))
3663 (goto-char (point-min))
3664 (recenter 0)
3665 (goto-char cp))
3666 ;; too big
3667 (let (depth start end exp p)
3668 (save-excursion
3669 (beginning-of-line)
3670 (setq depth (if (looking-at "[0-9]+")
3671 (string-to-int (buffer-substring-no-properties
3672 (match-beginning 0) (match-end 0)))
3674 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3675 (save-excursion
3676 (end-of-line)
3677 (if (re-search-backward exp nil t)
3678 (setq start (point))
3679 (setq start (point-min)))
3680 (save-excursion ;Not sure about this part.
3681 (end-of-line)
3682 (setq p (point))
3683 (while (and (not (re-search-forward exp nil t))
3684 (>= depth 0))
3685 (setq depth (1- depth))
3686 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3687 (if (/= (point) p)
3688 (setq end (point))
3689 (setq end (point-max)))))
3690 ;; Now work out the details of centering
3691 (let ((nl (count-lines start end))
3692 (cp (point)))
3693 (if (> nl (window-height (selected-window)))
3694 ;; We can't fit it all, so just center on cursor
3695 (progn (goto-char start)
3696 (recenter 1))
3697 ;; we can fit everything on the screen, but...
3698 (if (and (pos-visible-in-window-p start (selected-window))
3699 (pos-visible-in-window-p end (selected-window)))
3700 ;; we are all set!
3702 ;; we need to do something...
3703 (goto-char start)
3704 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3705 (lte (count-lines start (point-max))))
3706 (if (and (< (+ newcent lte) (window-height (selected-window)))
3707 (> (- (window-height (selected-window)) lte 1)
3708 newcent))
3709 (setq newcent (- (window-height (selected-window))
3710 lte 1)))
3711 (recenter newcent))))
3712 (goto-char cp)))))
3715 ;;; Tag Management -- List of expanders:
3717 (defun speedbar-fetch-dynamic-tags (file)
3718 "Return a list of tags generated dynamically from FILE.
3719 This uses the entries in `speedbar-dynamic-tags-function-list'
3720 to find the proper tags. It is up to each of those individual
3721 functions to do caching and flushing if appropriate."
3722 (save-excursion
3723 (set-buffer (find-file-noselect file))
3724 ;; If there is a buffer-local value of
3725 ;; speedbar-dynamic-tags-function-list, it will now be available.
3726 (let ((dtf speedbar-dynamic-tags-function-list)
3727 (ret t))
3728 (while (and (eq ret t) dtf)
3729 (setq ret
3730 (if (fboundp (car (car dtf)))
3731 (funcall (car (car dtf)) (buffer-file-name))
3733 (if (eq ret t)
3734 (setq dtf (cdr dtf))))
3735 (if (eq ret t)
3736 ;; No valid tag list, return nil
3738 ;; We have some tags. Return the list with the insert fn
3739 ;; prepended
3740 (cons (cdr (car dtf)) ret)))))
3742 ;;; Tag Management -- Imenu
3744 (if (not speedbar-use-imenu-flag)
3748 (eval-when-compile (if (locate-library "imenu") (require 'imenu)))
3750 (defun speedbar-fetch-dynamic-imenu (file)
3751 "Load FILE into a buffer, and generate tags using Imenu.
3752 Returns the tag list, or t for an error."
3753 ;; Load this AND compile it in
3754 (require 'imenu)
3755 (if speedbar-power-click (setq imenu--index-alist nil))
3756 (condition-case nil
3757 (let ((index-alist (imenu--make-index-alist t)))
3758 (if speedbar-sort-tags
3759 (sort (copy-alist index-alist)
3760 (lambda (a b) (string< (car a) (car b))))
3761 index-alist))
3762 (error t)))
3765 ;;; Tag Management -- etags (old XEmacs compatibility part)
3767 (defvar speedbar-fetch-etags-parse-list
3768 '(;; Note that java has the same parse-group as c
3769 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\)\\'" .
3770 speedbar-parse-c-or-c++tag)
3771 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3772 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
3773 ; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3774 ; speedbar-parse-fortran77-tag)
3775 ("\\.tex\\'" . speedbar-parse-tex-string)
3776 ("\\.p\\'" .
3777 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3779 "Associations of file extensions and expressions for extracting tags.
3780 To add a new file type, you would want to add a new association to the
3781 list, where the car is the file match, and the cdr is the way to
3782 extract an element from the tags output. If the output is complex,
3783 use a function symbol instead of regexp. The function should expect
3784 to be at the beginning of a line in the etags buffer.
3786 This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3788 (defvar speedbar-fetch-etags-command "etags"
3789 "*Command used to create an etags file.
3791 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3793 (defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3794 "*List of arguments to use with `speedbar-fetch-etags-command'.
3795 This creates an etags output buffer. Use `speedbar-toggle-etags' to
3796 modify this list conveniently.
3798 This variable is ignored if `speedbar-use-imenu-flag' is t.")
3800 (defun speedbar-toggle-etags (flag)
3801 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3802 FLAG then becomes a member of etags command line arguments. If flag
3803 is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3804 value is \"show\" then toggle the value of
3805 `speedbar-show-unknown-files'.
3807 This function is a convenience function for XEmacs menu created by
3808 Farzin Guilak <farzin@protocol.com>."
3809 (interactive)
3810 (cond
3811 ((equal flag "sort")
3812 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3813 ((equal flag "show")
3814 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3815 ((or (equal flag "-C")
3816 (equal flag "-S")
3817 (equal flag "-D"))
3818 (if (member flag speedbar-fetch-etags-arguments)
3819 (setq speedbar-fetch-etags-arguments
3820 (delete flag speedbar-fetch-etags-arguments))
3821 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3822 (t nil)))
3824 (defun speedbar-fetch-dynamic-etags (file)
3825 "For FILE, run etags and create a list of symbols extracted.
3826 Each symbol will be associated with its line position in FILE."
3827 (let ((newlist nil))
3828 (unwind-protect
3829 (save-excursion
3830 (if (get-buffer "*etags tmp*")
3831 (kill-buffer "*etags tmp*")) ;kill to clean it up
3832 (if (<= 1 speedbar-verbosity-level)
3833 (speedbar-message "Fetching etags..."))
3834 (set-buffer (get-buffer-create "*etags tmp*"))
3835 (apply 'call-process speedbar-fetch-etags-command nil
3836 (current-buffer) nil
3837 (append speedbar-fetch-etags-arguments (list file)))
3838 (goto-char (point-min))
3839 (if (<= 1 speedbar-verbosity-level)
3840 (speedbar-message "Fetching etags..."))
3841 (let ((expr
3842 (let ((exprlst speedbar-fetch-etags-parse-list)
3843 (ans nil))
3844 (while (and (not ans) exprlst)
3845 (if (string-match (car (car exprlst)) file)
3846 (setq ans (car exprlst)))
3847 (setq exprlst (cdr exprlst)))
3848 (cdr ans))))
3849 (if expr
3850 (let (tnl)
3851 (set-buffer (get-buffer-create "*etags tmp*"))
3852 (while (not (save-excursion (end-of-line) (eobp)))
3853 (save-excursion
3854 (setq tnl (speedbar-extract-one-symbol expr)))
3855 (if tnl (setq newlist (cons tnl newlist)))
3856 (forward-line 1)))
3857 (speedbar-message
3858 "Sorry, no support for a file of that extension"))))
3860 (if speedbar-sort-tags
3861 (sort newlist (lambda (a b) (string< (car a) (car b))))
3862 (reverse newlist))))
3864 ;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3865 ;; sure it's needed with the different sorting method.
3867 ;(defun speedbar-clean-etags()
3868 ; "Removes spaces before the ^? character, and removes `#define',
3869 ;return types, etc. preceding tags. This ensures that the sort operation
3870 ;works on the tags, not the return types."
3871 ; (save-excursion
3872 ; (goto-char (point-min))
3873 ; (while
3874 ; (re-search-forward "(?[ \t](?\C-?" nil t)
3875 ; (replace-match "\C-?" nil nil))
3876 ; (goto-char (point-min))
3877 ; (while
3878 ; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3879 ; (delete-region (match-beginning 1) (match-end 1)))))
3881 (defun speedbar-extract-one-symbol (expr)
3882 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
3883 The line should contain output from etags. Parse the output using the
3884 regular expression EXPR."
3885 (let* ((sym (if (stringp expr)
3886 (if (save-excursion
3887 (re-search-forward expr (save-excursion
3888 (end-of-line)
3889 (point)) t))
3890 (buffer-substring-no-properties (match-beginning 1)
3891 (match-end 1)))
3892 (funcall expr)))
3893 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3894 (save-excursion
3895 (end-of-line)
3896 (point))
3897 t)))
3898 (if (and j sym)
3899 (1+ (string-to-int (buffer-substring-no-properties
3900 (match-beginning 2)
3901 (match-end 2))))
3902 0))))
3903 (if (/= pos 0)
3904 (cons sym pos)
3905 nil)))
3907 (defun speedbar-parse-c-or-c++tag ()
3908 "Parse a C or C++ tag, which tends to be a little complex."
3909 (save-excursion
3910 (let ((bound (save-excursion (end-of-line) (point))))
3911 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3912 (buffer-substring-no-properties (match-beginning 1)
3913 (match-end 1)))
3914 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3915 (buffer-substring-no-properties (match-beginning 1)
3916 (match-end 1)))
3917 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3918 (buffer-substring-no-properties (match-beginning 1)
3919 (match-end 1)))
3920 (t nil))
3923 (defun speedbar-parse-tex-string ()
3924 "Parse a Tex string. Only find data which is relevant."
3925 (save-excursion
3926 (let ((bound (save-excursion (end-of-line) (point))))
3927 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3928 (buffer-substring-no-properties (match-beginning 0)
3929 (match-end 0)))
3930 (t nil)))))
3933 ;;; BUFFER DISPLAY mode.
3935 (defvar speedbar-buffers-key-map nil
3936 "Keymap used when in the buffers display mode.")
3938 (if speedbar-buffers-key-map
3940 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3942 ;; Basic tree features
3943 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3944 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3945 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3946 (define-key speedbar-buffers-key-map "=" 'speedbar-expand-line)
3947 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3949 ;; Buffer specific keybindings
3950 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3951 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3955 (defvar speedbar-buffer-easymenu-definition
3956 '(["Jump to buffer" speedbar-edit-line t]
3957 ["Expand File Tags" speedbar-expand-line
3958 (save-excursion (beginning-of-line)
3959 (looking-at "[0-9]+: *.\\+. "))]
3960 ["Flush Cache & Expand" speedbar-flush-expand-line
3961 (save-excursion (beginning-of-line)
3962 (looking-at "[0-9]+: *.\\+. "))]
3963 ["Contract File Tags" speedbar-contract-line
3964 (save-excursion (beginning-of-line)
3965 (looking-at "[0-9]+: *.-. "))]
3966 ["Kill Buffer" speedbar-buffer-kill-buffer
3967 (save-excursion (beginning-of-line)
3968 (looking-at "[0-9]+: *.-. "))]
3969 ["Revert Buffer" speedbar-buffer-revert-buffer
3970 (save-excursion (beginning-of-line)
3971 (looking-at "[0-9]+: *.-. "))]
3973 "Menu item elements shown when displaying a buffer list.")
3975 (defun speedbar-buffer-buttons (directory zero)
3976 "Create speedbar buttons based on the buffers currently loaded.
3977 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3978 (speedbar-buffer-buttons-engine nil))
3980 (defun speedbar-buffer-buttons-temp (directory zero)
3981 "Create speedbar buttons based on the buffers currently loaded.
3982 DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3983 (speedbar-buffer-buttons-engine t))
3985 (defun speedbar-buffer-buttons-engine (temp)
3986 "Create speedbar buffer buttons.
3987 If TEMP is non-nil, then clicking on a buffer restores the previous display."
3988 (insert "Active Buffers:\n")
3989 (let ((bl (buffer-list)))
3990 (while bl
3991 (if (string-match "^[ *]" (buffer-name (car bl)))
3993 (let* ((known (string-match speedbar-file-regexp
3994 (buffer-name (car bl))))
3995 (expchar (if known ?+ ??))
3996 (fn (if known 'speedbar-tag-file nil))
3997 (fname (save-excursion (set-buffer (car bl))
3998 (buffer-file-name))))
3999 (speedbar-make-tag-line 'bracket expchar fn
4000 (if fname (file-name-nondirectory fname))
4001 (buffer-name (car bl))
4002 'speedbar-buffer-click temp
4003 'speedbar-file-face 0)))
4004 (setq bl (cdr bl)))
4005 (setq bl (buffer-list))
4006 (insert "Scratch Buffers:\n")
4007 (while bl
4008 (if (not (string-match "^\\*" (buffer-name (car bl))))
4010 (if (eq (car bl) speedbar-buffer)
4012 (speedbar-make-tag-line 'bracket ?? nil nil
4013 (buffer-name (car bl))
4014 'speedbar-buffer-click temp
4015 'speedbar-file-face 0)))
4016 (setq bl (cdr bl)))
4017 (setq bl (buffer-list))
4018 (insert "Hidden Buffers:\n")
4019 (while bl
4020 (if (not (string-match "^ " (buffer-name (car bl))))
4022 (if (eq (car bl) speedbar-buffer)
4024 (speedbar-make-tag-line 'bracket ?? nil nil
4025 (buffer-name (car bl))
4026 'speedbar-buffer-click temp
4027 'speedbar-file-face 0)))
4028 (setq bl (cdr bl)))))
4030 (defun speedbar-buffers-item-info ()
4031 "Display information about the current buffer on the current line."
4032 (or (speedbar-item-info-tag-helper)
4033 (let* ((item (speedbar-line-text))
4034 (buffer (if item (get-buffer item) nil)))
4035 (and buffer
4036 (speedbar-message "%s%s %S %d %s"
4037 (if (buffer-modified-p buffer) "* " "")
4038 item
4039 (save-excursion (set-buffer buffer) major-mode)
4040 (save-excursion (set-buffer buffer)
4041 (buffer-size))
4042 (or (buffer-file-name buffer) "<No file>"))))))
4044 (defun speedbar-buffers-line-path (&optional depth)
4045 "Fetch the full path to the file (buffer) specified on the current line.
4046 Optional argument DEPTH specifies the current depth of the back search."
4047 (save-excursion
4048 (end-of-line)
4049 (let ((start (point)))
4050 ;; Buffers are always at level 0
4051 (if (not (re-search-backward "^0:" nil t))
4053 (let* ((bn (speedbar-line-text))
4054 (buffer (if bn (get-buffer bn))))
4055 (if buffer
4056 (if (save-excursion
4057 (end-of-line)
4058 (eq start (point)))
4059 (file-name-directory (buffer-file-name buffer))
4060 (buffer-file-name buffer))))))))
4062 (defun speedbar-buffer-click (text token indent)
4063 "When the users clicks on a buffer-button in speedbar.
4064 TEXT is the buffer's name, TOKEN and INDENT are unused."
4065 (if speedbar-power-click
4066 (let ((pop-up-frames t)) (select-window (display-buffer text)))
4067 (select-frame speedbar-attached-frame)
4068 (switch-to-buffer text)
4069 (if token (speedbar-change-initial-expansion-list
4070 speedbar-previously-used-expansion-list-name))))
4072 (defun speedbar-buffer-kill-buffer ()
4073 "Kill the buffer the cursor is on in the speedbar buffer."
4074 (interactive)
4075 (or (save-excursion
4076 (beginning-of-line)
4077 ;; If this fails, then it is a non-standard click, and as such,
4078 ;; perfectly allowed.
4079 (if (re-search-forward "[]>?}] [^ ]"
4080 (save-excursion (end-of-line) (point))
4082 (let ((text (progn
4083 (forward-char -1)
4084 (buffer-substring (point) (save-excursion
4085 (end-of-line)
4086 (point))))))
4087 (if (and (get-buffer text)
4088 (speedbar-y-or-n-p (format "Kill buffer %s? " text)))
4089 (kill-buffer text))
4090 (speedbar-refresh))))))
4092 (defun speedbar-buffer-revert-buffer ()
4093 "Revert the buffer the cursor is on in the speedbar buffer."
4094 (interactive)
4095 (save-excursion
4096 (beginning-of-line)
4097 ;; If this fails, then it is a non-standard click, and as such,
4098 ;; perfectly allowed
4099 (if (re-search-forward "[]>?}] [^ ]"
4100 (save-excursion (end-of-line) (point))
4102 (let ((text (progn
4103 (forward-char -1)
4104 (buffer-substring (point) (save-excursion
4105 (end-of-line)
4106 (point))))))
4107 (if (get-buffer text)
4108 (progn
4109 (set-buffer text)
4110 (revert-buffer t)))))))
4113 ;;; Useful hook values and such.
4115 (defvar speedbar-highlight-one-tag-line nil
4116 "Overlay used for highlighting the most recently jumped to tag line.")
4118 (defun speedbar-highlight-one-tag-line ()
4119 "Highlight the current line, unhighlighting a previously jumped to line."
4120 (speedbar-unhighlight-one-tag-line)
4121 (setq speedbar-highlight-one-tag-line
4122 (speedbar-make-overlay (save-excursion (beginning-of-line) (point))
4123 (save-excursion (end-of-line)
4124 (forward-char 1)
4125 (point))))
4126 (speedbar-overlay-put speedbar-highlight-one-tag-line 'face
4127 'speedbar-highlight-face)
4128 (add-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line)
4131 (defun speedbar-unhighlight-one-tag-line ()
4132 "Unhighlight the currently highlighted line."
4133 (if speedbar-highlight-one-tag-line
4134 (progn
4135 (speedbar-delete-overlay speedbar-highlight-one-tag-line)
4136 (setq speedbar-highlight-one-tag-line nil)))
4137 (remove-hook 'pre-command-hook 'speedbar-unhighlight-one-tag-line))
4139 (defun speedbar-recenter-to-top ()
4140 "Recenter the current buffer so POINT is on the top of the window."
4141 (recenter 1))
4143 (defun speedbar-recenter ()
4144 "Recenter the current buffer so POINT is in the center of the window."
4145 (recenter (/ (window-height (selected-window)) 2)))
4148 ;;; Color loading section.
4150 (defface speedbar-button-face '((((class color) (background light))
4151 (:foreground "green4"))
4152 (((class color) (background dark))
4153 (:foreground "green3")))
4154 "Face used for +/- buttons."
4155 :group 'speedbar-faces)
4157 (defface speedbar-file-face '((((class color) (background light))
4158 (:foreground "cyan4"))
4159 (((class color) (background dark))
4160 (:foreground "cyan"))
4161 (t (:weight bold)))
4162 "Face used for file names."
4163 :group 'speedbar-faces)
4165 (defface speedbar-directory-face '((((class color) (background light))
4166 (:foreground "blue4"))
4167 (((class color) (background dark))
4168 (:foreground "light blue")))
4169 "Face used for directory names."
4170 :group 'speedbar-faces)
4171 (defface speedbar-tag-face '((((class color) (background light))
4172 (:foreground "brown"))
4173 (((class color) (background dark))
4174 (:foreground "yellow")))
4175 "Face used for displaying tags."
4176 :group 'speedbar-faces)
4178 (defface speedbar-selected-face '((((class color) (background light))
4179 (:foreground "red" :underline t))
4180 (((class color) (background dark))
4181 (:foreground "red" :underline t))
4182 (t (:underline t)))
4183 "Face used to underline the file in the active window."
4184 :group 'speedbar-faces)
4186 (defface speedbar-highlight-face '((((class color) (background light))
4187 (:background "green"))
4188 (((class color) (background dark))
4189 (:background "sea green"))
4190 (((class grayscale mono)
4191 (background light))
4192 (:background "black"))
4193 (((class grayscale mono)
4194 (background dark))
4195 (:background "white")))
4196 "Face used for highlighting buttons with the mouse."
4197 :group 'speedbar-faces)
4200 ;;; Image loading and inlining
4203 ;;; Some images if defimage is available:
4204 (eval-when-compile
4206 (if (fboundp 'defimage)
4207 (defalias 'defimage-speedbar 'defimage)
4209 (if (not (fboundp 'make-glyph))
4211 (defmacro defimage-speedbar (variable imagespec docstring)
4212 "Don't bother loading up an image...
4213 Argument VARIABLE is the variable to define.
4214 Argument IMAGESPEC is the list defining the image to create.
4215 Argument DOCSTRING is the documentation for VARIABLE."
4216 `(defvar ,variable nil ,docstring))
4218 ;; ELSE
4219 (defun speedbar-find-image-on-load-path (image)
4220 "Find the image file IMAGE on the load path."
4221 (let ((l load-path)
4222 (r nil))
4223 (while (and l (not r))
4224 (if (file-exists-p (concat (car l) "/" image))
4225 (setq r (concat (car l) "/" image)))
4226 (setq l (cdr l)))
4229 (defun speedbar-convert-emacs21-imagespec-to-xemacs (spec)
4230 "Convert the Emacs21 image SPEC into an XEmacs image spec."
4231 (let* ((sl (car spec))
4232 (itype (nth 1 sl))
4233 (ifile (nth 3 sl)))
4234 (vector itype ':file (speedbar-find-image-on-load-path ifile))))
4236 (defmacro defimage-speedbar (variable imagespec docstring)
4237 "Define VARIABLE as an image if `defimage' is not available.
4238 IMAGESPEC is the image data, and DOCSTRING is documentation for the image."
4239 `(defvar ,variable
4240 ;; The Emacs21 version of defimage looks just like the XEmacs image
4241 ;; specifier, except that it needs a :type keyword. If we line
4242 ;; stuff up right, we can use this cheat to support XEmacs specifiers.
4243 (condition-case nil
4244 (make-glyph
4245 (make-image-specifier
4246 (speedbar-convert-emacs21-imagespec-to-xemacs (quote ,imagespec)))
4247 'buffer)
4248 (error nil))
4249 ,docstring)))))
4251 (defimage-speedbar speedbar-directory-plus
4252 ((:type xpm :file "sb-dir-plus.xpm" :ascent center))
4253 "Image used for closed directories with stuff in them.")
4255 (defimage-speedbar speedbar-directory-minus
4256 ((:type xpm :file "sb-dir-minus.xpm" :ascent center))
4257 "Image used for open directories with stuff in them.")
4259 (defimage-speedbar speedbar-directory
4260 ((:type xpm :file "sb-dir.xpm" :ascent center))
4261 "Image used for empty or unreadable directories.")
4263 (defimage-speedbar speedbar-page-plus
4264 ((:type xpm :file "sb-pg-plus.xpm" :ascent center))
4265 "Image used for closed files with stuff in them.")
4267 (defimage-speedbar speedbar-page-minus
4268 ((:type xpm :file "sb-pg-minus.xpm" :ascent center))
4269 "Image used for open files with stuff in them.")
4271 (defimage-speedbar speedbar-page
4272 ((:type xpm :file "sb-pg.xpm" :ascent center))
4273 "Image used for files that can't be opened.")
4275 (defimage-speedbar speedbar-tag
4276 ((:type xpm :file "sb-tag.xpm" :ascent center))
4277 "Image used for tags.")
4279 (defimage-speedbar speedbar-tag-plus
4280 ((:type xpm :file "sb-tag-plus.xpm" :ascent center))
4281 "Image used for closed tag groups.")
4283 (defimage-speedbar speedbar-tag-minus
4284 ((:type xpm :file "sb-tag-minus.xpm" :ascent center))
4285 "Image used for open tag groups.")
4287 (defimage-speedbar speedbar-tag-gt
4288 ((:type xpm :file "sb-tag-gt.xpm" :ascent center))
4289 "Image used for open tag groups.")
4291 (defimage-speedbar speedbar-tag-v
4292 ((:type xpm :file "sb-tag-v.xpm" :ascent center))
4293 "Image used for open tag groups.")
4295 (defimage-speedbar speedbar-tag-type
4296 ((:type xpm :file "sb-tag-type.xpm" :ascent center))
4297 "Image used for open tag groups.")
4299 (defimage-speedbar speedbar-mail
4300 ((:type xpm :file "sb-mail.xpm" :ascent center))
4301 "Image used for open tag groups.")
4303 (defvar speedbar-expand-image-button-alist
4304 '(("<+>" . speedbar-directory-plus)
4305 ("<->" . speedbar-directory-minus)
4306 ("< >" . speedbar-directory)
4307 ("[+]" . speedbar-page-plus)
4308 ("[-]" . speedbar-page-minus)
4309 ("[?]" . speedbar-page)
4310 ("{+}" . speedbar-tag-plus)
4311 ("{-}" . speedbar-tag-minus)
4312 ("<M>" . speedbar-mail)
4313 (" =>" . speedbar-tag)
4314 (" +>" . speedbar-tag-gt)
4315 (" ->" . speedbar-tag-v)
4316 (">" . speedbar-tag)
4317 ("@" . speedbar-tag-type)
4318 (" @" . speedbar-tag-type)
4320 "List of text and image associations.")
4322 (defun speedbar-insert-image-button-maybe (start length)
4323 "Insert an image button based on text starting at START for LENGTH chars.
4324 If buttontext is unknown, just insert that text.
4325 If we have an image associated with it, use that image."
4326 (if speedbar-use-images
4327 (let* ((bt (buffer-substring start (+ length start)))
4328 (a (assoc bt speedbar-expand-image-button-alist)))
4329 ;; Regular images (created with `insert-image' are intangible
4330 ;; which (I suppose) make them more compatible with XEmacs 21.
4331 ;; Unfortunatly, there is a giant pile o code dependent on the
4332 ;; underlying text. This means if we leave it tangible, then I
4333 ;; don't have to change said giant piles o code.
4334 (if (and a (symbol-value (cdr a)))
4335 (if (featurep 'xemacs)
4336 (add-text-properties (+ start (length bt)) start
4337 (list 'end-glyph (symbol-value (cdr a))
4338 'rear-nonsticky (list 'display)
4339 'invisible t
4340 'detachable t))
4341 (add-text-properties start (+ start (length bt))
4342 (list 'display (symbol-value (cdr a))
4343 'rear-nonsticky (list 'display))))
4344 ;(message "Bad text [%s]" (buffer-substring start (+ start length)))
4345 ))))
4348 ;; some edebug hooks
4349 (add-hook 'edebug-setup-hook
4350 (lambda ()
4351 (def-edebug-spec speedbar-with-writable def-body)))
4353 (provide 'speedbar)
4355 ;; run load-time hooks
4356 (run-hooks 'speedbar-load-hook)
4358 ;;; arch-tag: 4477e6d1-f78c-48b9-a503-387d3c9767d5
4359 ;;; speedbar.el ends here