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