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