(speedbar-temp-buffer-show-function): For emacs don't call hook
[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
1095518a 8;; X-RCS: $Id: speedbar.el,v 1.11 1998/08/24 00:37:22 zappo Exp zappo $
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)
2d0327e5 1177 (toggle-read-only 1)
6b3eac8d 1178 (speedbar-set-mode-line-format)
59588cd4
KH
1179 (if speedbar-xemacsp
1180 (progn
1181 (make-local-variable 'mouse-motion-handler)
1182 (setq mouse-motion-handler 'speedbar-track-mouse-xemacs))
1183 (if speedbar-track-mouse-flag
1184 (progn
1185 (make-local-variable 'track-mouse)
1186 (setq track-mouse t))) ;this could be messy.
1187 (setq auto-show-mode nil)) ;no auto-show for Emacs
6b3eac8d
DN
1188 (run-hooks 'speedbar-mode-hook))
1189 (speedbar-update-contents)
1190 speedbar-buffer)
1191
59588cd4
KH
1192(defun speedbar-show-info-under-mouse (&optional event)
1193 "Call the info function for the line under the mouse.
1194Optional EVENT is currently not used."
1195 (let ((pos (mouse-position))) ; we ignore event until I use it later.
1196 (if (equal (car pos) speedbar-frame)
1197 (save-excursion
1198 (save-window-excursion
1199 (apply 'set-mouse-position pos)
1200 (speedbar-item-info))))))
1201
6b3eac8d
DN
1202(defun speedbar-set-mode-line-format ()
1203 "Set the format of the mode line based on the current speedbar environment.
1204This gives visual indications of what is up. It EXPECTS the speedbar
1205frame and window to be the currently active frame and window."
1206 (if (and (frame-live-p speedbar-frame)
1207 (or (not speedbar-xemacsp)
1208 (specifier-instance has-modeline-p)))
1209 (save-excursion
1210 (set-buffer speedbar-buffer)
1211 (let* ((w (or (speedbar-frame-width) 20))
1212 (p1 "<<")
1213 (p5 ">>")
1214 (p3 (if speedbar-update-flag "SPEEDBAR" "SLOWBAR"))
1215 (blank (- w (length p1) (length p3) (length p5)
1216 (if line-number-mode 4 0)))
1217 (p2 (if (> blank 0)
1218 (make-string (/ blank 2) ? )
1219 ""))
1220 (p4 (if (> blank 0)
1221 (make-string (+ (/ blank 2) (% blank 2)) ? )
1222 ""))
1223 (tf
1224 (if line-number-mode
1225 (list (concat p1 p2 p3) '(line-number-mode " %3l")
1226 (concat p4 p5))
1227 (list (concat p1 p2 p3 p4 p5)))))
1228 (if (not (equal mode-line-format tf))
1229 (progn
1230 (setq mode-line-format tf)
1231 (force-mode-line-update)))))))
1232
1233(defun speedbar-temp-buffer-show-function (buffer)
1234 "Placed in the variable `temp-buffer-show-function' in `speedbar-mode'.
1235If a user requests help using \\[help-command] <Key> the temp BUFFER will be
1236redirected into a window on the attached frame."
1237 (if speedbar-attached-frame (select-frame speedbar-attached-frame))
1238 (pop-to-buffer buffer nil)
1239 (other-window -1)
1095518a
EL
1240 ;; Fix for using this hook on some platforms: Bob Weiner
1241 (cond ((not speedbar-xemacsp)
1242 (run-hooks 'temp-buffer-show-hook))
1243 ((fboundp 'run-hook-with-args)
59588cd4
KH
1244 (run-hook-with-args 'temp-buffer-show-hook buffer))
1245 ((and (boundp 'temp-buffer-show-hook)
1246 (listp temp-buffer-show-hook))
1247 (mapcar (function (lambda (hook) (funcall hook buffer)))
1248 temp-buffer-show-hook))))
1249
0e596101
EL
1250(defvar speedbar-previous-menu nil
1251 "The menu before the last `speedbar-reconfigure-keymaps' was called.")
1252
59588cd4 1253(defun speedbar-reconfigure-keymaps ()
6b3eac8d
DN
1254 "Reconfigure the menu-bar in a speedbar frame.
1255Different menu items are displayed depending on the current display mode
1256and the existence of packages."
59588cd4
KH
1257 (let ((md (append
1258 speedbar-easymenu-definition-base
1259 (if speedbar-shown-directories
1260 ;; file display mode version
1261 (speedbar-initial-menu)
1262 (save-excursion
1263 (select-frame speedbar-attached-frame)
1264 (if (local-variable-p
1265 'speedbar-easymenu-definition-special
1266 (current-buffer))
1267 ;; If bound locally, we can use it
1268 speedbar-easymenu-definition-special)))
1269 ;; Dynamic menu stuff
1270 '("-")
1271 (list (cons "Displays"
1272 (let ((displays nil)
1273 (alist speedbar-initial-expansion-mode-alist))
1274 (while alist
1275 (setq displays
1276 (cons
1277 (vector
1278 (capitalize (car (car alist)))
1279 (list
1280 'speedbar-change-initial-expansion-list
1281 (car (car alist)))
1282 t)
1283 displays))
1284 (setq alist (cdr alist)))
1285 displays)))
1286 ;; The trailer
1287 speedbar-easymenu-definition-trailer))
1288 (localmap (save-excursion
1289 (let ((cf (selected-frame)))
1290 (prog2
1291 (select-frame speedbar-attached-frame)
1292 (if (local-variable-p
1293 'speedbar-special-mode-key-map
1294 (current-buffer))
1295 speedbar-special-mode-key-map)
1296 (select-frame cf))))))
1297 (save-excursion
1298 (set-buffer speedbar-buffer)
1299 (use-local-map (or localmap
1300 (speedbar-initial-keymap)
1301 ;; This creates a small keymap we can glom the
1302 ;; menu adjustments into.
1303 (speedbar-make-specialized-keymap)))
0e596101
EL
1304 ;; Delete the old menu if applicable.
1305 (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
1306 (setq speedbar-previous-menu md)
1307 ;; Now add the new menu
59588cd4
KH
1308 (if (not speedbar-xemacsp)
1309 (easy-menu-define speedbar-menu-map (current-local-map)
1310 "Speedbar menu" md)
0e596101 1311 (easy-menu-add md (current-local-map))
59588cd4 1312 (set-buffer-menubar (list md))))))
6b3eac8d
DN
1313
1314\f
1315;;; User Input stuff
1316;;
1317
1318;; XEmacs: this can be implemented using modeline keymaps, but there
1319;; is no use, as we have horizontal scrollbar (as the docstring
1320;; hints.)
1321(defun speedbar-mouse-hscroll (e)
1322 "Read a mouse event E from the mode line, and horizontally scroll.
1323If the mouse is being clicked on the far left, or far right of the
1324mode-line. This is only useful for non-XEmacs"
1325 (interactive "e")
1326 (let* ((xp (car (nth 2 (car (cdr e)))))
1327 (cpw (/ (frame-pixel-width)
1328 (frame-width)))
1329 (oc (1+ (/ xp cpw)))
1330 )
1331 (cond ((< oc 3)
1332 (scroll-left 2))
1333 ((> oc (- (window-width) 3))
1334 (scroll-right 2))
1335 (t (message "Click on the edge of the modeline to scroll left/right")))
1336 ;;(message "X: Pixel %d Char Pixels %d On char %d" xp cpw oc)
1337 ))
1338
1339(defun speedbar-customize ()
1340 "Customize speedbar using the Custom package."
1341 (interactive)
1342 (let ((sf (selected-frame)))
1343 (select-frame speedbar-attached-frame)
1344 (customize-group 'speedbar)
1345 (select-frame sf))
1346 (speedbar-maybee-jump-to-attached-frame))
1347
59588cd4
KH
1348(defun speedbar-track-mouse (event)
1349 "For motion EVENT, display info about the current line."
1350 (interactive "e")
1351 (if (not speedbar-track-mouse-flag)
1352 nil
1353 (save-excursion
1354 (let ((char (nth 1 (car (cdr event)))))
1355 (if (not (numberp char))
1356 (message nil)
1357 (goto-char char)
1358 ;; (message "%S" event)
1359 (speedbar-item-info)
1360 )))))
1361
1362(defun speedbar-track-mouse-xemacs (event)
1363 "For motion EVENT, display info about the current line."
1364 (if (functionp (default-value 'mouse-motion-handler))
1365 (funcall (default-value 'mouse-motion-handler) event))
1366 (if speedbar-track-mouse-flag
1367 (save-excursion
1368 (save-window-excursion
1369 (condition-case ()
1370 (progn (mouse-set-point event)
1371 ;; Prevent focus-related bugs.
1372 (if (eq major-mode 'speedbar-mode)
1373 (speedbar-item-info)))
1374 (error nil))))))
1375
6b3eac8d
DN
1376;; In XEmacs, we make popup menus work on the item over mouse (as
1377;; opposed to where the point happens to be.) We attain this by
1378;; temporarily moving the point to that place.
1379;; Hrvoje Niksic <hniksic@srce.hr>
1380(defun speedbar-xemacs-popup-kludge (event)
1381 "Pop up a menu related to the clicked on item.
1382Must be bound to EVENT."
1383 (interactive "e")
59588cd4 1384 (select-frame speedbar-frame)
6b3eac8d
DN
1385 (save-excursion
1386 (goto-char (event-closest-point event))
1387 (beginning-of-line)
1388 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
1389 (save-excursion (beginning-of-line) (point)))))
1390 (popup-mode-menu)
1391 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
1392 ;; menu functions) return immediately.
1393 (let (new)
1394 (while (not (misc-user-event-p (setq new (next-event))))
1395 (dispatch-event new))
1396 (dispatch-event new))))
1397
1398(defun speedbar-emacs-popup-kludge (e)
1399 "Pop up a menu related to the clicked on item.
1400Must be bound to event E."
1401 (interactive "e")
1402 (save-excursion
1403 (mouse-set-point e)
1404 ;; This gets the cursor where the user can see it.
1405 (if (not (bolp)) (forward-char -1))
1406 (sit-for 0)
1407 (if (< emacs-major-version 20)
1408 (mouse-major-mode-menu e)
1409 (mouse-major-mode-menu e nil))))
1410
0e596101
EL
1411(defun speedbar-hack-buffer-menu (e)
1412 "Control mouse 1 is buffer menu.
1413This hack overrides it so that the right thing happens in the main
1414Emacs frame, not in the speedbar frame.
1415Argument E is the event causing this activity."
1416 (interactive "e")
1417 (let ((fn (lookup-key global-map (if speedbar-xemacsp
1418 '(control button1)
1419 [C-down-mouse-1])))
1420 (newbuff nil))
1421 (unwind-protect
1422 (save-excursion
1423 (set-window-dedicated-p (selected-window) nil)
1424 (call-interactively fn)
1425 (setq newbuff (current-buffer)))
1426 (switch-to-buffer " SPEEDBAR")
1427 (set-window-dedicated-p (selected-window) t))
1428 (speedbar-with-attached-buffer
1429 (switch-to-buffer newbuff))))
1430
6b3eac8d
DN
1431(defun speedbar-next (arg)
1432 "Move to the next ARGth line in a speedbar buffer."
1433 (interactive "p")
1434 (forward-line (or arg 1))
1435 (speedbar-item-info)
1436 (speedbar-position-cursor-on-line))
1437
1438(defun speedbar-prev (arg)
1439 "Move to the previous ARGth line in a speedbar buffer."
1440 (interactive "p")
1441 (speedbar-next (if arg (- arg) -1)))
1442
59588cd4
KH
1443(defun speedbar-restricted-move (arg)
1444 "Move to the next ARGth line in a speedbar buffer at the same depth.
1445This means that movement is restricted to a subnode, and that siblings
1446of intermediate nodes are skipped."
1447 (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
1448 ;; First find the extent for which we are allowed to move.
1449 (let ((depth (save-excursion (beginning-of-line)
1450 (if (looking-at "[0-9]+:")
1451 (string-to-int (match-string 0))
1452 0)))
1453 (crement (if (< arg 0) 1 -1)) ; decrement or increment
1454 (lastmatch (point)))
1455 (while (/= arg 0)
1456 (forward-line (- crement))
1457 (let ((subdepth (save-excursion (beginning-of-line)
1458 (if (looking-at "[0-9]+:")
1459 (string-to-int (match-string 0))
1460 0))))
1461 (cond ((or (< subdepth depth)
1462 (progn (end-of-line) (eobp))
1463 (progn (beginning-of-line) (bobp)))
1464 ;; We have reached the end of this block.
1465 (goto-char lastmatch)
1466 (setq arg 0)
1467 (error "End of sub-list"))
1468 ((= subdepth depth)
1469 (setq lastmatch (point)
1470 arg (+ arg crement))))))
1471 (speedbar-position-cursor-on-line)))
1472
1473(defun speedbar-restricted-next (arg)
1474 "Move to the next ARGth line in a speedbar buffer at the same depth.
1475This means that movement is restricted to a subnode, and that siblings
1476of intermediate nodes are skipped."
1477 (interactive "p")
1478 (speedbar-restricted-move (or arg 1))
1479 (speedbar-item-info))
1480
1481
1482(defun speedbar-restricted-prev (arg)
1483 "Move to the previous ARGth line in a speedbar buffer at the same depth.
1484This means that movement is restricted to a subnode, and that siblings
1485of intermediate nodes are skipped."
1486 (interactive "p")
1487 (speedbar-restricted-move (if arg (- arg) -1))
1488 (speedbar-item-info))
1489
1490(defun speedbar-navigate-list (arg)
1491 "Move across ARG groups of similarly typed items in speedbar.
1492Stop on the first line of the next type of item, or on the last or first item
1493if we reach a buffer boundary."
1494 (interactive "p")
1495 (beginning-of-line)
1496 (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
1497 (let ((str (regexp-quote (match-string 0))))
1498 (while (looking-at str)
1499 (speedbar-restricted-move arg)
1500 (beginning-of-line))))
1501 (speedbar-position-cursor-on-line))
1502
1503(defun speedbar-forward-list ()
1504 "Move forward over the current list.
1505A LIST in speedbar is a group of similarly typed items, such as directories,
1506files, or the directory button."
1507 (interactive)
1508 (speedbar-navigate-list 1)
1509 (speedbar-item-info))
1510
1511(defun speedbar-backward-list ()
1512 "Move backward over the current list.
1513A LIST in speedbar is a group of similarly typed items, such as directories,
1514files, or the directory button."
1515 (interactive)
1516 (speedbar-navigate-list -1)
1517 (speedbar-item-info))
1518
6b3eac8d
DN
1519(defun speedbar-scroll-up (&optional arg)
1520 "Page down one screen-full of the speedbar, or ARG lines."
1521 (interactive "P")
1522 (scroll-up arg)
1523 (speedbar-position-cursor-on-line))
1524
1525(defun speedbar-scroll-down (&optional arg)
1526 "Page up one screen-full of the speedbar, or ARG lines."
1527 (interactive "P")
1528 (scroll-down arg)
1529 (speedbar-position-cursor-on-line))
1530
1531(defun speedbar-up-directory ()
1532 "Keyboard accelerator for moving the default directory up one.
1533Assumes that the current buffer is the speedbar buffer"
1534 (interactive)
1535 (setq default-directory (expand-file-name (concat default-directory "../")))
1536 (speedbar-update-contents))
1537\f
1538;;; Speedbar file activity (aka creeping featurism)
1539;;
1540(defun speedbar-refresh ()
1541 "Refresh the current speedbar display, disposing of any cached data."
1542 (interactive)
0e596101
EL
1543 (let ((dl speedbar-shown-directories)
1544 (dm (and (boundp 'deactivate-mark) deactivate-mark)))
6b3eac8d
DN
1545 (while dl
1546 (adelete 'speedbar-directory-contents-alist (car dl))
0e596101 1547 (setq dl (cdr dl)))
61d7e1dc
EL
1548 (if (<= 1 speedbar-verbosity-level)
1549 (message "Refreshing speedbar..."))
0e596101
EL
1550 (speedbar-update-contents)
1551 (speedbar-stealthy-updates)
1552 ;; Reset the timer in case it got really hosed for some reason...
1553 (speedbar-set-timer speedbar-update-speed)
1554 (if (<= 1 speedbar-verbosity-level)
61d7e1dc 1555 (message "Refreshing speedbar...done"))
0e596101 1556 (if (boundp 'deactivate-mark) (setq deactivate-mark dm))))
6b3eac8d
DN
1557
1558(defun speedbar-item-load ()
59588cd4 1559 "Load the item under the cursor or mouse if it is a Lisp file."
6b3eac8d
DN
1560 (interactive)
1561 (let ((f (speedbar-line-file)))
1562 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1563 (if (and (file-exists-p (concat f "c"))
1564 (y-or-n-p (format "Load %sc? " f)))
1565 ;; If the compiled version exists, load that instead...
1566 (load-file (concat f "c"))
1567 (load-file f))
59588cd4 1568 (error "Not a loadable file"))))
6b3eac8d
DN
1569
1570(defun speedbar-item-byte-compile ()
59588cd4 1571 "Byte compile the item under the cursor or mouse if it is a Lisp file."
6b3eac8d
DN
1572 (interactive)
1573 (let ((f (speedbar-line-file))
1574 (sf (selected-frame)))
1575 (if (and (file-exists-p f) (string-match "\\.el\\'" f))
1576 (progn
1577 (select-frame speedbar-attached-frame)
1578 (byte-compile-file f nil)
59588cd4
KH
1579 (select-frame sf)
1580 (speedbar-reset-scanners)))
6b3eac8d
DN
1581 ))
1582
1583(defun speedbar-mouse-item-info (event)
1584 "Provide information about what the user clicked on.
1585This should be bound to a mouse EVENT."
1586 (interactive "e")
1587 (mouse-set-point event)
1588 (speedbar-item-info))
1589
59588cd4
KH
1590(defun speedbar-generic-item-info ()
1591 "Attempt to derive, and then display information about thils line item.
1592File style information is displayed with `speedbar-item-info'."
1593 (save-excursion
1594 (beginning-of-line)
1595 ;; Skip invisible number info.
1596 (if (looking-at "\\([0-9]+\\):") (goto-char (match-end 0)))
1597 ;; Skip items in "folder" type text characters.
1598 (if (looking-at "\\s-*[[<({].[]>)}] ") (goto-char (match-end 0)))
1599 ;; Get the text
1600 (message "Text: %s" (buffer-substring-no-properties
1601 (point) (progn (end-of-line) (point))))))
1602
6b3eac8d
DN
1603(defun speedbar-item-info ()
1604 "Display info in the mini-buffer about the button the mouse is over."
1605 (interactive)
1606 (if (not speedbar-shown-directories)
59588cd4 1607 (speedbar-generic-item-info)
6b3eac8d
DN
1608 (let* ((item (speedbar-line-file))
1609 (attr (if item (file-attributes item) nil)))
59588cd4 1610 (if (and item attr) (message "%s %-6d %s" (nth 8 attr) (nth 7 attr) item)
6b3eac8d
DN
1611 (save-excursion
1612 (beginning-of-line)
59588cd4
KH
1613 (if (not (looking-at "\\([0-9]+\\):"))
1614 (speedbar-generic-item-info)
1615 (setq item (speedbar-line-path (string-to-int (match-string 1))))
1616 (if (re-search-forward "> \\([^ ]+\\)$"
1617 (save-excursion(end-of-line)(point)) t)
1618 (progn
1619 (setq attr (get-text-property (match-beginning 1)
1620 'speedbar-token))
1621 (message "Tag: %s in %s @ %s"
1622 (match-string 1) item
1623 (if attr
1624 (if (markerp attr) (marker-position attr) attr)
1625 0)))
1626 (if (re-search-forward "{[+-]} \\([^\n]+\\)$"
1627 (save-excursion(end-of-line)(point)) t)
1628 (message "Group of tags \"%s\"" (match-string 1))
1629 (speedbar-generic-item-info)))))))))
6b3eac8d
DN
1630
1631(defun speedbar-item-copy ()
1632 "Copy the item under the cursor.
1633Files can be copied to new names or places."
1634 (interactive)
1635 (let ((f (speedbar-line-file)))
59588cd4 1636 (if (not f) (error "Not a file"))
6b3eac8d 1637 (if (file-directory-p f)
59588cd4 1638 (error "Cannot copy directory")
6b3eac8d
DN
1639 (let* ((rt (read-file-name (format "Copy %s to: "
1640 (file-name-nondirectory f))
1641 (file-name-directory f)))
1642 (refresh (member (expand-file-name (file-name-directory rt))
1643 speedbar-shown-directories)))
1644 ;; Create the right file name part
1645 (if (file-directory-p rt)
1646 (setq rt
1647 (concat (expand-file-name rt)
1648 (if (string-match "/$" rt) "" "/")
1649 (file-name-nondirectory f))))
1650 (if (or (not (file-exists-p rt))
1651 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1652 (progn
1653 (copy-file f rt t t)
1654 ;; refresh display if the new place is currently displayed.
1655 (if refresh
1656 (progn
1657 (speedbar-refresh)
1658 (if (not (speedbar-goto-this-file rt))
1659 (speedbar-goto-this-file f))))
1660 ))))))
1661
1662(defun speedbar-item-rename ()
1663 "Rename the item under the cursor or mouse.
1664Files can be renamed to new names or moved to new directories."
1665 (interactive)
1666 (let ((f (speedbar-line-file)))
1667 (if f
1668 (let* ((rt (read-file-name (format "Rename %s to: "
1669 (file-name-nondirectory f))
1670 (file-name-directory f)))
1671 (refresh (member (expand-file-name (file-name-directory rt))
1672 speedbar-shown-directories)))
1673 ;; Create the right file name part
1674 (if (file-directory-p rt)
1675 (setq rt
1676 (concat (expand-file-name rt)
1677 (if (string-match "/\\'" rt) "" "/")
1678 (file-name-nondirectory f))))
1679 (if (or (not (file-exists-p rt))
1680 (y-or-n-p (format "Overwrite %s with %s? " rt f)))
1681 (progn
1682 (rename-file f rt t)
1683 ;; refresh display if the new place is currently displayed.
1684 (if refresh
1685 (progn
1686 (speedbar-refresh)
1687 (speedbar-goto-this-file rt)
1688 )))))
59588cd4 1689 (error "Not a file"))))
6b3eac8d
DN
1690
1691(defun speedbar-item-delete ()
1692 "Delete the item under the cursor. Files are removed from disk."
1693 (interactive)
1694 (let ((f (speedbar-line-file)))
59588cd4 1695 (if (not f) (error "Not a file"))
6b3eac8d
DN
1696 (if (y-or-n-p (format "Delete %s? " f))
1697 (progn
1698 (if (file-directory-p f)
1699 (delete-directory f)
1700 (delete-file f))
1701 (message "Okie dokie..")
1702 (let ((p (point)))
1703 (speedbar-refresh)
1704 (goto-char p))
1705 ))
1706 ))
1707
59588cd4
KH
1708(defun speedbar-item-object-delete ()
1709 "Delete the object associated from the item under the cursor.
1710The file is removed from disk. The object is determined from the
1711variable `speedbar-obj-alist'."
1712 (interactive)
1713 (let* ((f (speedbar-line-file))
1714 (obj nil)
1715 (oa speedbar-obj-alist))
1716 (if (not f) (error "Not a file"))
1717 (while (and oa (not (string-match (car (car oa)) f)))
1718 (setq oa (cdr oa)))
1719 (setq obj (concat (file-name-sans-extension f) (cdr (car oa))))
1720 (if (and oa (file-exists-p obj)
1721 (y-or-n-p (format "Delete %s? " obj)))
1722 (progn
1723 (delete-file obj)
1724 (speedbar-reset-scanners)))))
1725
6b3eac8d
DN
1726(defun speedbar-enable-update ()
1727 "Enable automatic updating in speedbar via timers."
1728 (interactive)
1729 (setq speedbar-update-flag t)
1730 (speedbar-set-mode-line-format)
1731 (speedbar-set-timer speedbar-update-speed))
1732
1733(defun speedbar-disable-update ()
1734 "Disable automatic updating and stop consuming resources."
1735 (interactive)
1736 (setq speedbar-update-flag nil)
1737 (speedbar-set-mode-line-format)
1738 (speedbar-set-timer nil))
1739
1740(defun speedbar-toggle-updates ()
1741 "Toggle automatic update for the speedbar frame."
1742 (interactive)
1743 (if speedbar-update-flag
1744 (speedbar-disable-update)
1745 (speedbar-enable-update)))
1746
1747(defun speedbar-toggle-sorting ()
1748 "Toggle automatic update for the speedbar frame."
1749 (interactive)
1750 (setq speedbar-sort-tags (not speedbar-sort-tags)))
1751
1752(defun speedbar-toggle-show-all-files ()
1753 "Toggle display of files speedbar can not tag."
1754 (interactive)
1755 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files))
1756 (speedbar-refresh))
1757\f
1758;;; Utility functions
1759;;
1760(defun speedbar-set-timer (timeout)
1761 "Apply a timer with TIMEOUT, or remove a timer if TIMOUT is nil.
1762TIMEOUT is the number of seconds until the speedbar timer is called
1763again. When TIMEOUT is nil, turn off all timeouts.
1764This function will also enable or disable the `vc-checkin-hook' used
1765to track file check ins, and will change the mode line to match
1766`speedbar-update-flag'."
1767 (cond
1768 ;; XEmacs
1769 (speedbar-xemacsp
1770 (if speedbar-timer
1771 (progn (delete-itimer speedbar-timer)
1772 (setq speedbar-timer nil)))
1773 (if timeout
1774 (if (and speedbar-xemacsp
1775 (or (>= emacs-major-version 20)
1776 (>= emacs-minor-version 15)))
1777 (setq speedbar-timer (start-itimer "speedbar"
1778 'speedbar-timer-fn
1779 timeout
1780 timeout
1781 t))
1782 (setq speedbar-timer (start-itimer "speedbar"
1783 'speedbar-timer-fn
1784 timeout
1785 nil)))))
1786 ;; Post 19.31 Emacs
1787 ((fboundp 'run-with-idle-timer)
1788 (if speedbar-timer
1789 (progn (cancel-timer speedbar-timer)
1790 (setq speedbar-timer nil)))
1791 (if timeout
1792 (setq speedbar-timer
1793 (run-with-idle-timer timeout t 'speedbar-timer-fn))))
1794 ;; Emacs 19.30 (Thanks twice: ptype@dra.hmg.gb)
1795 ((fboundp 'post-command-idle-hook)
1796 (if timeout
1797 (add-hook 'post-command-idle-hook 'speedbar-timer-fn)
1798 (remove-hook 'post-command-idle-hook 'speedbar-timer-fn)))
1799 ;; Older or other Emacsen with no timers. Set up so that its
1800 ;; obvious this emacs can't handle the updates
1801 (t
1802 (setq speedbar-update-flag nil)))
1803 ;; Apply a revert hook that will reset the scanners. We attach to revert
1804 ;; because most reverts occur during VC state change, and this lets our
1805 ;; VC scanner fix itself.
1806 (if timeout
1807 (add-hook 'after-revert-hook 'speedbar-reset-scanners)
1808 (remove-hook 'after-revert-hook 'speedbar-reset-scanners)
1809 )
1810 ;; change this if it changed for some reason
1811 (speedbar-set-mode-line-format))
1812
1813(defmacro speedbar-with-writable (&rest forms)
1814 "Allow the buffer to be writable and evaluate FORMS."
1815 (list 'let '((inhibit-read-only t))
6b3eac8d
DN
1816 (cons 'progn forms)))
1817(put 'speedbar-with-writable 'lisp-indent-function 0)
1818
1819(defun speedbar-select-window (buffer)
59588cd4 1820 "Select a window in which BUFFER is shown.
6b3eac8d
DN
1821If it is not shown, force it to appear in the default window."
1822 (let ((win (get-buffer-window buffer speedbar-attached-frame)))
1823 (if win
1824 (select-window win)
59588cd4 1825 (set-window-buffer (selected-window) buffer))))
6b3eac8d
DN
1826
1827(defmacro speedbar-with-attached-buffer (&rest forms)
1828 "Execute FORMS in the attached frame's special buffer.
1829Optionally select that frame if necessary."
1830 ;; Reset the timer with a new timeout when cliking a file
1831 ;; in case the user was navigating directories, we can cancel
1832 ;; that other timer.
1833 (list
1834 'progn
1835 '(speedbar-set-timer speedbar-update-speed)
1836 (list
1837 'let '((cf (selected-frame)))
1838 '(select-frame speedbar-attached-frame)
1839 '(speedbar-select-window speedbar-desired-buffer)
1840 (cons 'progn forms)
1841 '(select-frame cf)
1842 '(speedbar-maybee-jump-to-attached-frame)
1843 )))
1844
1845(defun speedbar-insert-button (text face mouse function
1846 &optional token prevline)
1847 "Insert TEXT as the next logical speedbar button.
1848FACE is the face to put on the button, MOUSE is the highlight face to use.
1849When the user clicks on TEXT, FUNCTION is called with the TOKEN parameter.
1850This function assumes that the current buffer is the speedbar buffer.
1851If PREVLINE, then put this button on the previous line.
1852
1853This is a convenience function for special mode that create their own
1854specialized speedbar displays."
1855 (goto-char (point-max))
1856 (if (/= (current-column) 0) (insert "\n"))
1857 (if prevline (progn (delete-char -1) (insert " "))) ;back up if desired...
1858 (let ((start (point)))
1859 (insert text)
1860 (speedbar-make-button start (point) face mouse function token))
1861 (let ((start (point)))
1862 (insert "\n")
1863 (put-text-property start (point) 'face nil)
1864 (put-text-property start (point) 'mouse-face nil)))
1865
1866(defun speedbar-make-button (start end face mouse function &optional token)
1867 "Create a button from START to END, with FACE as the display face.
1868MOUSE is the mouse face. When this button is clicked on FUNCTION
59588cd4 1869will be run with the TOKEN parameter (any Lisp object)"
6b3eac8d
DN
1870 (put-text-property start end 'face face)
1871 (put-text-property start end 'mouse-face mouse)
1872 (put-text-property start end 'invisible nil)
1873 (if function (put-text-property start end 'speedbar-function function))
1874 (if token (put-text-property start end 'speedbar-token token))
1875 )
1876\f
59588cd4
KH
1877;;; Initial Expansion list management
1878;;
1879(defun speedbar-initial-expansion-list ()
1880 "Return the current default expansion list.
1881This is based on `speedbar-initial-expansion-list-name' referencing
1882`speedbar-initial-expansion-mode-alist'."
1883 ;; cdr1 - name, cdr2 - menu
1884 (cdr (cdr (cdr (assoc speedbar-initial-expansion-list-name
1885 speedbar-initial-expansion-mode-alist)))))
1886
1887(defun speedbar-initial-menu ()
1888 "Return the current default menu data.
1889This is based on `speedbar-initial-expansion-list-name' referencing
1890`speedbar-initial-expansion-mode-alist'."
1891 (symbol-value
1892 (car (cdr (assoc speedbar-initial-expansion-list-name
1893 speedbar-initial-expansion-mode-alist)))))
1894
1895(defun speedbar-initial-keymap ()
1896 "Return the current default menu data.
1897This is based on `speedbar-initial-expansion-list-name' referencing
1898`speedbar-initial-expansion-mode-alist'."
1899 (symbol-value
1900 (car (cdr (cdr (assoc speedbar-initial-expansion-list-name
1901 speedbar-initial-expansion-mode-alist))))))
1902
1903(defun speedbar-initial-stealthy-functions ()
1904 "Return a list of functions to call stealthily.
1905This is based on `speedbar-initial-expansion-list-name' referencing
1906`speedbar-stealthy-function-list'."
1907 (cdr (assoc speedbar-initial-expansion-list-name
1908 speedbar-stealthy-function-list)))
1909
1910(defun speedbar-add-expansion-list (new-list)
1911 "Add NEW-LIST to the list of expansion lists."
1912 (add-to-list 'speedbar-initial-expansion-mode-alist new-list))
1913
1914(defun speedbar-change-initial-expansion-list (new-default)
1915 "Change speedbar's default expansion list to NEW-DEFAULT."
1916 (interactive
1917 (list
1918 (completing-read (format "Speedbar Mode (default %s): "
1919 speedbar-previously-used-expansion-list-name)
1920 speedbar-initial-expansion-mode-alist
1921 nil t "" nil
1922 speedbar-previously-used-expansion-list-name)))
1923 (setq speedbar-previously-used-expansion-list-name
1924 speedbar-initial-expansion-list-name
1925 speedbar-initial-expansion-list-name new-default)
1926 (speedbar-refresh)
1927 (speedbar-reconfigure-keymaps))
1928
1929\f
1930;;; Special speedbar display management
1931;;
1932(defun speedbar-maybe-add-localized-support (buffer)
1933 "Quick check function called on BUFFERs by the speedbar timer function.
1934Maintains the value of local variables which control speedbars use
1935of the special mode functions."
1936 (or speedbar-special-mode-expansion-list
1937 (speedbar-add-localized-speedbar-support buffer)))
1938
1939(defun speedbar-add-localized-speedbar-support (buffer)
1940 "Add localized speedbar support to BUFFER's mode if it is available."
1941 (interactive "bBuffer: ")
1942 (if (stringp buffer) (setq buffer (get-buffer buffer)))
1943 (if (not (buffer-live-p buffer))
1944 nil
1945 (save-excursion
1946 (set-buffer buffer)
1947 (save-match-data
1948 (let ((ms (symbol-name major-mode)) v)
1949 (if (not (string-match "-mode$" ms))
1950 nil ;; do nothing to broken mode
1951 (setq ms (substring ms 0 (match-beginning 0)))
1952 (setq v (intern-soft (concat ms "-speedbar-buttons")))
1953 (make-local-variable 'speedbar-special-mode-expansion-list)
1954 (if (not v)
1955 (setq speedbar-special-mode-expansion-list t)
1956 ;; If it is autoloaded, we need to load it now so that
1957 ;; we have access to the varialbe -speedbar-menu-items.
1958 ;; Is this XEmacs safe?
1959 (let ((sf (symbol-function v)))
1960 (if (and (listp sf) (eq (car sf) 'autoload))
1961 (load-library (car (cdr sf)))))
1962 (setq speedbar-special-mode-expansion-list (list v))
1963 (setq v (intern-soft (concat ms "-speedbar-key-map")))
1964 (if (not v)
1965 nil ;; don't add special keymap
1966 (make-local-variable 'speedbar-special-mode-key-map)
1967 (setq speedbar-special-mode-key-map
1968 (symbol-value v)))
1969 (setq v (intern-soft (concat ms "-speedbar-menu-items")))
1970 (if (not v)
1971 nil ;; don't add special menus
1972 (make-local-variable 'speedbar-easymenu-definition-special)
1973 (setq speedbar-easymenu-definition-special
1974 (symbol-value v)))
1975 )))))))
1976
1977(defun speedbar-remove-localized-speedbar-support (buffer)
1978 "Remove any traces that BUFFER supports speedbar in a specialized way."
1979 (save-excursion
1980 (set-buffer buffer)
1981 (kill-local-variable 'speedbar-special-mode-expansion-list)
1982 (kill-local-variable 'speedbar-special-mode-key-map)
1983 (kill-local-variable 'speedbar-easymenu-definition-special)))
1984\f
6b3eac8d
DN
1985;;; File button management
1986;;
1987(defun speedbar-file-lists (directory)
1988 "Create file lists for DIRECTORY.
1989The car is the list of directories, the cdr is list of files not
1990matching ignored headers. Cache any directory files found in
1991`speedbar-directory-contents-alist' and use that cache before scanning
1992the file-system"
1993 (setq directory (expand-file-name directory))
1994 ;; If in powerclick mode, then the directory we are getting
1995 ;; should be rescanned.
1996 (if speedbar-power-click
1997 (adelete 'speedbar-directory-contents-alist directory))
1998 ;; find the directory, either in the cache, or build it.
1999 (or (cdr-safe (assoc directory speedbar-directory-contents-alist))
2000 (let ((default-directory directory)
2001 (dir (directory-files directory nil))
2002 (dirs nil)
2003 (files nil))
2004 (while dir
59588cd4
KH
2005 (if (not
2006 (or (string-match speedbar-file-unshown-regexp (car dir))
2007 (string-match speedbar-directory-unshown-regexp (car dir))))
6b3eac8d
DN
2008 (if (file-directory-p (car dir))
2009 (setq dirs (cons (car dir) dirs))
2010 (setq files (cons (car dir) files))))
2011 (setq dir (cdr dir)))
2012 (let ((nl (cons (nreverse dirs) (list (nreverse files)))))
2013 (aput 'speedbar-directory-contents-alist directory nl)
2014 nl))
2015 ))
2016
2017(defun speedbar-directory-buttons (directory index)
2018 "Insert a single button group at point for DIRECTORY.
2019Each directory path part is a different button. If part of the path
2020matches the user directory ~, then it is replaced with a ~.
2021INDEX is not used, but is required by the caller."
2022 (let* ((tilde (expand-file-name "~"))
2023 (dd (expand-file-name directory))
2024 (junk (string-match (regexp-quote tilde) dd))
2025 (displayme (if junk
2026 (concat "~" (substring dd (match-end 0)))
2027 dd))
2028 (p (point)))
2029 (if (string-match "^~/?\\'" displayme) (setq displayme (concat tilde "/")))
2030 (insert displayme)
2031 (save-excursion
2032 (goto-char p)
2033 (while (re-search-forward "\\([^/]+\\)/" nil t)
2034 (speedbar-make-button (match-beginning 1) (match-end 1)
2035 'speedbar-directory-face
2036 'speedbar-highlight-face
2037 'speedbar-directory-buttons-follow
2038 (if (= (match-beginning 1) p)
2039 (expand-file-name "~/") ;the tilde
2040 (buffer-substring-no-properties
2041 p (match-end 0)))))
2042 ;; Nuke the beginning of the directory if it's too long...
2043 (cond ((eq speedbar-directory-button-trim-method 'span)
2044 (beginning-of-line)
2045 (let ((ww (or (speedbar-frame-width) 20)))
2046 (move-to-column ww nil)
2047 (while (>= (current-column) ww)
2048 (re-search-backward "/" nil t)
2049 (if (<= (current-column) 2)
2050 (progn
2051 (re-search-forward "/" nil t)
2052 (if (< (current-column) 4)
2053 (re-search-forward "/" nil t))
2054 (forward-char -1)))
2055 (if (looking-at "/?$")
2056 (beginning-of-line)
2057 (insert "/...\n ")
2058 (move-to-column ww nil)))))
2059 ((eq speedbar-directory-button-trim-method 'trim)
2060 (end-of-line)
2061 (let ((ww (or (speedbar-frame-width) 20))
2062 (tl (current-column)))
2063 (if (< ww tl)
2064 (progn
2065 (move-to-column (- tl ww))
2066 (if (re-search-backward "/" nil t)
2067 (progn
2068 (delete-region (point-min) (point))
2069 (insert "$")
2070 )))))))
2071 )
2072 (if (string-match "\\`/[^/]+/\\'" displayme)
2073 (progn
2074 (insert " ")
2075 (let ((p (point)))
2076 (insert "<root>")
2077 (speedbar-make-button p (point)
2078 'speedbar-directory-face
2079 'speedbar-highlight-face
2080 'speedbar-directory-buttons-follow
2081 "/"))))
2082 (end-of-line)
2083 (insert-char ?\n 1 nil)))
2084
2085(defun speedbar-make-tag-line (exp-button-type
2086 exp-button-char exp-button-function
2087 exp-button-data
2088 tag-button tag-button-function tag-button-data
2089 tag-button-face depth)
2090 "Create a tag line with EXP-BUTTON-TYPE for the small expansion button.
2091This is the button that expands or contracts a node (if applicable),
2092and EXP-BUTTON-CHAR the character in it (+, -, ?, etc). EXP-BUTTON-FUNCTION
2093is the function to call if it's clicked on. Button types are
2094'bracket, 'angle, 'curly, or nil. EXP-BUTTON-DATA is extra data
2095attached to the text forming the expansion button.
2096
2097Next, TAG-BUTTON is the text of the tag. TAG-BUTTON-FUNCTION is the
2098function to call if clicked on, and TAG-BUTTON-DATA is the data to
2099attach to the text field (such a tag positioning, etc).
2100TAG-BUTTON-FACE is a face used for this type of tag.
2101
2102Lastly, DEPTH shows the depth of expansion.
2103
2104This function assumes that the cursor is in the speedbar window at the
2105position to insert a new item, and that the new item will end with a CR"
2106 (let ((start (point))
2107 (end (progn
2108 (insert (int-to-string depth) ":")
2109 (point))))
2110 (put-text-property start end 'invisible t)
2111 )
2112 (insert-char ? depth nil)
2113 (put-text-property (- (point) depth) (point) 'invisible nil)
2114 (let* ((exp-button (cond ((eq exp-button-type 'bracket) "[%c]")
2115 ((eq exp-button-type 'angle) "<%c>")
2116 ((eq exp-button-type 'curly) "{%c}")
2117 (t ">")))
2118 (buttxt (format exp-button exp-button-char))
2119 (start (point))
2120 (end (progn (insert buttxt) (point)))
2121 (bf (if exp-button-type 'speedbar-button-face nil))
2122 (mf (if exp-button-function 'speedbar-highlight-face nil))
2123 )
2124 (speedbar-make-button start end bf mf exp-button-function exp-button-data)
59588cd4
KH
2125 (if speedbar-hide-button-brackets-flag
2126 (progn
2127 (put-text-property start (1+ start) 'invisible t)
2128 (put-text-property end (1- end) 'invisible t)))
6b3eac8d
DN
2129 )
2130 (insert-char ? 1 nil)
2131 (put-text-property (1- (point)) (point) 'invisible nil)
2132 (let ((start (point))
2133 (end (progn (insert tag-button) (point))))
2134 (insert-char ?\n 1 nil)
2135 (put-text-property (1- (point)) (point) 'invisible nil)
2136 (speedbar-make-button start end tag-button-face
2137 (if tag-button-function 'speedbar-highlight-face nil)
2138 tag-button-function tag-button-data))
2139)
2140
2141(defun speedbar-change-expand-button-char (char)
2142 "Change the expansion button character to CHAR for the current line."
2143 (save-excursion
2144 (beginning-of-line)
2145 (if (re-search-forward ":\\s-*.\\([-+?]\\)" (save-excursion (end-of-line)
2146 (point)) t)
2147 (speedbar-with-writable
2148 (goto-char (match-beginning 1))
2149 (delete-char 1)
59588cd4
KH
2150 (insert-char char 1 t)
2151 (put-text-property (point) (1- (point)) 'invisible nil)))))
6b3eac8d
DN
2152
2153\f
2154;;; Build button lists
2155;;
2156(defun speedbar-insert-files-at-point (files level)
2157 "Insert list of FILES starting at point, and indenting all files to LEVEL.
2158Tag expandable items with a +, otherwise a ?. Don't highlight ? as we
2159don't know how to manage them. The input parameter FILES is a cons
59588cd4 2160cell of the form ( 'DIRLIST . 'FILELIST )"
6b3eac8d
DN
2161 ;; Start inserting all the directories
2162 (let ((dirs (car files)))
2163 (while dirs
2164 (speedbar-make-tag-line 'angle ?+ 'speedbar-dired (car dirs)
2165 (car dirs) 'speedbar-dir-follow nil
2166 'speedbar-directory-face level)
2167 (setq dirs (cdr dirs))))
59588cd4
KH
2168 (let ((lst (car (cdr files)))
2169 (case-fold-search t))
6b3eac8d
DN
2170 (while lst
2171 (let* ((known (string-match speedbar-file-regexp (car lst)))
2172 (expchar (if known ?+ ??))
2173 (fn (if known 'speedbar-tag-file nil)))
2174 (if (or speedbar-show-unknown-files (/= expchar ??))
2175 (speedbar-make-tag-line 'bracket expchar fn (car lst)
2176 (car lst) 'speedbar-find-file nil
2177 'speedbar-file-face level)))
2178 (setq lst (cdr lst)))))
2179
2180(defun speedbar-default-directory-list (directory index)
2181 "Insert files for DIRECTORY with level INDEX at point."
2182 (speedbar-insert-files-at-point
2183 (speedbar-file-lists directory) index)
2184 (speedbar-reset-scanners)
2185 (if (= index 0)
2186 ;; If the shown files variable has extra directories, then
2187 ;; it is our responsibility to redraw them all
2188 ;; Luckilly, the nature of inserting items into this list means
2189 ;; that by reversing it, we can easilly go in the right order
2190 (let ((sf (cdr (reverse speedbar-shown-directories))))
2191 (setq speedbar-shown-directories
2192 (list (expand-file-name default-directory)))
2193 ;; exand them all as we find them
2194 (while sf
2195 (if (speedbar-goto-this-file (car sf))
2196 (progn
2197 (beginning-of-line)
2198 (if (looking-at "[0-9]+:[ ]*<")
2199 (progn
2200 (goto-char (match-end 0))
2201 (speedbar-do-function-pointer)))
2202 (setq sf (cdr sf)))))
2203 )))
2204
59588cd4
KH
2205(defun speedbar-apply-one-tag-hierarchy-method (lst method)
2206 "Adjust the tag hierarchy LST by METHOD."
2207 (cond
2208 ((eq method 'sort)
2209 (sort (copy-alist lst)
2210 (lambda (a b) (string< (car a) (car b)))))
2211 ((eq method 'prefix-group)
2212 (let ((newlst nil)
2213 (sublst nil)
2214 (work-list nil)
2215 (junk-list nil)
2216 (short-group-list nil)
2217 (short-start-name nil)
2218 (short-end-name nil)
2219 (num-shorts-grouped 0)
2220 (bins (make-vector 256 nil))
2221 (diff-idx 0))
2222 ;; Break out sub-lists
2223 (while lst
2224 (if (listp (cdr-safe (car-safe lst)))
2225 (setq newlst (cons (car lst) newlst))
2226 (setq sublst (cons (car lst) sublst)))
2227 (setq lst (cdr lst)))
2228 ;; Now, first find out how long our list is. Never let a
2229 ;; list get-shorter than our minimum.
2230 (if (<= (length sublst) speedbar-tag-split-minimum-length)
2231 (setq work-list (nreverse sublst))
2232 (setq diff-idx (length (try-completion "" sublst)))
2233 ;; Sort the whole list into bins.
2234 (while sublst
2235 (let ((e (car sublst))
2236 (s (car (car sublst))))
2237 (cond ((<= (length s) diff-idx)
2238 ;; 0 storage bin for shorty.
2239 (aset bins 0 (cons e (aref bins 0))))
2240 (t
2241 ;; stuff into a bin based on ascii value at diff
2242 (aset bins (aref s diff-idx)
2243 (cons e (aref bins (aref s diff-idx)))))))
2244 (setq sublst (cdr sublst)))
2245 ;; Go through all our bins Stick singles into our
2246 ;; junk-list, everything else as sublsts in work-list.
2247 ;; If two neighboring lists are both small, make a grouped
2248 ;; group combinding those two sub-lists.
2249 (setq diff-idx 0)
2250 (while (> 256 diff-idx)
2251 (let ((l (aref bins diff-idx)))
2252 (if l
2253 (let ((tmp (cons (try-completion "" l) l)))
2254 (if (or (> (length l) speedbar-tag-regroup-maximum-length)
2255 (> (+ (length l) (length short-group-list))
2256 speedbar-tag-split-minimum-length))
2257 (progn
2258 ;; We have reached a longer list, so we
2259 ;; must finish off a grouped group.
2260 (cond
2261 ((and short-group-list
2262 (= (length short-group-list)
2263 num-shorts-grouped))
2264 ;; All singles? Junk list
2265 (setq junk-list (append short-group-list
2266 junk-list)))
2267 ((= num-shorts-grouped 1)
2268 ;; Only one short group? Just stick it in
2269 ;; there by itself.
2270 (setq work-list
2271 (cons (cons (try-completion
2272 "" short-group-list)
2273 (nreverse short-group-list))
2274 work-list)))
2275 (short-group-list
2276 ;; Multiple groups to be named in a special
2277 ;; way by displaying the range over which we
2278 ;; have grouped them.
2279 (setq work-list
2280 (cons (cons (concat short-start-name
2281 " to "
2282 short-end-name)
2283 (nreverse short-group-list))
2284 work-list))))
2285 ;; Reset short group list information every time.
2286 (setq short-group-list nil
2287 short-start-name nil
2288 short-end-name nil
2289 num-shorts-grouped 0)))
2290 ;; Ok, now that we cleaned up the short-group-list,
2291 ;; we can deal with this new list, to decide if it
2292 ;; should go on one of these sub-lists or not.
2293 (if (< (length l) speedbar-tag-regroup-maximum-length)
2294 (setq short-group-list (append short-group-list l)
2295 num-shorts-grouped (1+ num-shorts-grouped)
2296 short-end-name (car tmp)
2297 short-start-name (if short-start-name
2298 short-start-name
2299 (car tmp)))
2300 (setq work-list (cons tmp work-list))))))
2301 (setq diff-idx (1+ diff-idx))))
2302 ;; Did we run out of things? Drop our new list onto the end.
2303 (cond
2304 ((and short-group-list (= (length short-group-list) num-shorts-grouped))
2305 ;; All singles? Junk list
2306 (setq junk-list (append short-group-list junk-list)))
2307 ((= num-shorts-grouped 1)
2308 ;; Only one short group? Just stick it in
2309 ;; there by itself.
2310 (setq work-list
2311 (cons (cons (try-completion "" short-group-list)
2312 (nreverse short-group-list))
2313 work-list)))
2314 (short-group-list
2315 ;; Multiple groups to be named in a special
2316 ;; way by displaying the range over which we
2317 ;; have grouped them.
2318 (setq work-list
2319 (cons (cons (concat short-start-name " to " short-end-name)
2320 (nreverse short-group-list))
2321 work-list))))
2322 ;; Now, stick our new list onto the end of
2323 (if work-list
2324 (if junk-list
2325 (append (nreverse newlst)
2326 (nreverse work-list)
2327 junk-list)
2328 (append (nreverse newlst)
2329 (nreverse work-list)))
2330 (append (nreverse newlst) junk-list))))
2331 ((eq method 'trim-words)
2332 (let ((newlst nil)
2333 (sublst nil)
2334 (trim-prefix nil)
2335 (trim-chars 0)
2336 (trimlst nil))
2337 (while lst
2338 (if (listp (cdr-safe (car-safe lst)))
2339 (setq newlst (cons (car lst) newlst))
2340 (setq sublst (cons (car lst) sublst)))
2341 (setq lst (cdr lst)))
2342 ;; Get the prefix to trim by. Make sure that we don't trim
2343 ;; off silly pieces, only complete understandable words.
2344 (setq trim-prefix (try-completion "" sublst))
2345 (if (or (= (length sublst) 1)
2346 (not trim-prefix)
2347 (not (string-match "\\(\\w+\\W+\\)+" trim-prefix)))
2348 (append (nreverse newlst) (nreverse sublst))
2349 (setq trim-prefix (substring trim-prefix (match-beginning 0)
2350 (match-end 0)))
2351 (setq trim-chars (length trim-prefix))
2352 (while sublst
2353 (setq trimlst (cons
2354 (cons (substring (car (car sublst)) trim-chars)
2355 (cdr (car sublst)))
2356 trimlst)
2357 sublst (cdr sublst)))
2358 ;; Put the lists together
2359 (append (nreverse newlst) trimlst))))
2360 ((eq method 'simple-group)
2361 (let ((newlst nil)
2362 (sublst nil))
2363 (while lst
2364 (if (listp (cdr-safe (car-safe lst)))
2365 (setq newlst (cons (car lst) newlst))
2366 (setq sublst (cons (car lst) sublst)))
2367 (setq lst (cdr lst)))
2368 (if (not newlst)
2369 (nreverse sublst)
2370 (setq newlst (cons (cons "Tags" (nreverse sublst)) newlst))
2371 (nreverse newlst))))
2372 (t lst)))
2373
2374(defun speedbar-create-tag-hierarchy (lst)
2375 "Adjust the tag hierarchy in LST, and return it.
2376This uses `speedbar-tag-hierarchy-method' to determine how to adjust
2377the list. See it's value for details."
2378 (let ((methods speedbar-tag-hierarchy-method))
2379 (while methods
2380 (setq lst (speedbar-apply-one-tag-hierarchy-method lst (car methods))
2381 methods (cdr methods)))
2382 lst))
2383
6b3eac8d
DN
2384(defun speedbar-insert-generic-list (level lst expand-fun find-fun)
2385 "At LEVEL, insert a generic multi-level alist LST.
2386Associations with lists get {+} tags (to expand into more nodes) and
2387those with positions just get a > as the indicator. {+} buttons will
2388have the function EXPAND-FUN and the token is the CDR list. The token
2389name will have the function FIND-FUN and not token."
2390 ;; Remove imenu rescan button
2391 (if (string= (car (car lst)) "*Rescan*")
2392 (setq lst (cdr lst)))
59588cd4
KH
2393 ;; Adjust the list.
2394 (setq lst (speedbar-create-tag-hierarchy lst))
6b3eac8d
DN
2395 ;; insert the parts
2396 (while lst
2397 (cond ((null (car-safe lst)) nil) ;this would be a separator
2398 ((or (numberp (cdr-safe (car-safe lst)))
2399 (markerp (cdr-safe (car-safe lst))))
2400 (speedbar-make-tag-line nil nil nil nil ;no expand button data
2401 (car (car lst)) ;button name
2402 find-fun ;function
2403 (cdr (car lst)) ;token is position
2404 'speedbar-tag-face
2405 (1+ level)))
2406 ((listp (cdr-safe (car-safe lst)))
2407 (speedbar-make-tag-line 'curly ?+ expand-fun (cdr (car lst))
2408 (car (car lst)) ;button name
2409 nil nil 'speedbar-tag-face
2410 (1+ level)))
2411 (t (message "Ooops!")))
2412 (setq lst (cdr lst))))
2413\f
2414;;; Timed functions
2415;;
2416(defun speedbar-update-contents ()
2417 "Generically update the contents of the speedbar buffer."
2418 (interactive)
2419 ;; Set the current special buffer
2420 (setq speedbar-desired-buffer nil)
59588cd4
KH
2421 ;; Check for special modes
2422 (speedbar-maybe-add-localized-support (current-buffer))
2423 ;; Choose the correct method of doodling.
6b3eac8d 2424 (if (and speedbar-mode-specific-contents-flag
59588cd4 2425 (listp speedbar-special-mode-expansion-list)
6b3eac8d
DN
2426 speedbar-special-mode-expansion-list
2427 (local-variable-p
2428 'speedbar-special-mode-expansion-list
2429 (current-buffer)))
2430 ;;(eq (get major-mode 'mode-class 'special)))
2431 (speedbar-update-special-contents)
2432 (speedbar-update-directory-contents)))
2433
2434(defun speedbar-update-directory-contents ()
2435 "Update the contents of the speedbar buffer based on the current directory."
2436 (let ((cbd (expand-file-name default-directory))
2437 cbd-parent
59588cd4 2438 (funclst (speedbar-initial-expansion-list))
6b3eac8d
DN
2439 (cache speedbar-full-text-cache)
2440 ;; disable stealth during update
2441 (speedbar-stealthy-function-list nil)
2442 (use-cache nil)
2443 (expand-local nil)
2444 ;; Because there is a bug I can't find just yet
2445 (inhibit-quit nil))
2446 (save-excursion
2447 (set-buffer speedbar-buffer)
2448 ;; If we are updating contents to where we are, then this is
2449 ;; really a request to update existing contents, so we must be
2450 ;; careful with our text cache!
2451 (if (member cbd speedbar-shown-directories)
59588cd4
KH
2452 (progn
2453 (setq cache nil)
2454 ;; If the current directory is not the last element in the dir
2455 ;; list, then we ALSO need to zap the list of expanded directories
2456 (if (/= (length (member cbd speedbar-shown-directories)) 1)
2457 (setq speedbar-shown-directories (list cbd))))
6b3eac8d
DN
2458
2459 ;; Build cbd-parent, and see if THAT is in the current shown
2460 ;; directories. First, go through pains to get the parent directory
2461 (if (and speedbar-smart-directory-expand-flag
2462 (save-match-data
2463 (setq cbd-parent cbd)
2464 (if (string-match "/$" cbd-parent)
59588cd4
KH
2465 (setq cbd-parent (substring cbd-parent 0
2466 (match-beginning 0))))
6b3eac8d
DN
2467 (setq cbd-parent (file-name-directory cbd-parent)))
2468 (member cbd-parent speedbar-shown-directories))
2469 (setq expand-local t)
2470
2471 ;; If this directory is NOT in the current list of available
2472 ;; paths, then use the cache, and set the cache to our new
2473 ;; value. Make sure to unhighlight the current file, or if we
2474 ;; come back to this directory, it might be a different file
2475 ;; and then we get a mess!
2476 (if (> (point-max) 1)
2477 (progn
2478 (speedbar-clear-current-file)
2479 (setq speedbar-full-text-cache
2480 (cons speedbar-shown-directories (buffer-string)))))
2481
2482 ;; Check if our new directory is in the list of directories
2483 ;; shown in the text-cache
2484 (if (member cbd (car cache))
2485 (setq speedbar-shown-directories (car cache)
2486 use-cache t)
2487 ;; default the shown directories to this list...
2488 (setq speedbar-shown-directories (list cbd)))
2489 ))
a4252bdb 2490 (if (not expand-local) (setq speedbar-last-selected-file nil))
6b3eac8d
DN
2491 (speedbar-with-writable
2492 (if (and expand-local
2493 ;; Find this directory as a speedbar node.
2494 (speedbar-path-line cbd))
2495 ;; Open it.
2496 (speedbar-expand-line)
2497 (erase-buffer)
2498 (cond (use-cache
2499 (setq default-directory
2500 (nth (1- (length speedbar-shown-directories))
2501 speedbar-shown-directories))
2502 (insert (cdr cache)))
2503 (t
2504 (while funclst
2505 (setq default-directory cbd)
2506 (funcall (car funclst) cbd 0)
2507 (setq funclst (cdr funclst))))))
2508 (goto-char (point-min)))))
59588cd4 2509 (speedbar-reconfigure-keymaps))
6b3eac8d
DN
2510
2511(defun speedbar-update-special-contents ()
2512 "Used the mode-specific variable to fill in the speedbar buffer.
2513This should only be used by modes classified as special."
2514 (let ((funclst speedbar-special-mode-expansion-list)
2515 (specialbuff (current-buffer)))
2516 (save-excursion
2517 (setq speedbar-desired-buffer specialbuff)
2518 (set-buffer speedbar-buffer)
2519 ;; If we are leaving a directory, cache it.
2520 (if (not speedbar-shown-directories)
2521 ;; Do nothing
2522 nil
2523 ;; Clean up directory maintenance stuff
2524 (speedbar-clear-current-file)
2525 (setq speedbar-full-text-cache
2526 (cons speedbar-shown-directories (buffer-string))
2527 speedbar-shown-directories nil))
2528 ;; Now fill in the buffer with our newly found specialized list.
2529 (speedbar-with-writable
2530 (while funclst
2531 ;; We do not erase the buffer because these functions may
2532 ;; decide NOT to update themselves.
2533 (funcall (car funclst) specialbuff)
2534 (setq funclst (cdr funclst))))
2535 (goto-char (point-min))))
59588cd4 2536 (speedbar-reconfigure-keymaps))
6b3eac8d
DN
2537
2538(defun speedbar-timer-fn ()
59588cd4 2539 "Run whenever Emacs is idle to update the speedbar item."
6b3eac8d
DN
2540 (if (not (and (frame-live-p speedbar-frame)
2541 (frame-live-p speedbar-attached-frame)))
2542 (speedbar-set-timer nil)
2543 ;; Save all the match data so that we don't mess up executing fns
2544 (save-match-data
a4252bdb
EL
2545 ;; Only do stuff if the frame is visible, not an icon, and if
2546 ;; it is currently flagged to do something.
2547 (if (and speedbar-update-flag
2548 (frame-visible-p speedbar-frame)
2549 (not (eq (frame-visible-p speedbar-frame) 'icon)))
6b3eac8d
DN
2550 (let ((af (selected-frame)))
2551 (save-window-excursion
2552 (select-frame speedbar-attached-frame)
2553 ;; make sure we at least choose a window to
2554 ;; get a good directory from
a4252bdb
EL
2555 (if (window-minibuffer-p (selected-window))
2556 nil
2557 ;; Check for special modes
2558 (speedbar-maybe-add-localized-support (current-buffer))
2559 ;; Update for special mode all the time!
2560 (if (and speedbar-mode-specific-contents-flag
2561 (listp speedbar-special-mode-expansion-list)
2562 speedbar-special-mode-expansion-list
2563 (local-variable-p
2564 'speedbar-special-mode-expansion-list
2565 (current-buffer)))
2566 ;;(eq (get major-mode 'mode-class 'special)))
2567 (progn
2568 (if (<= 2 speedbar-verbosity-level)
2569 (message "Updating speedbar to special mode: %s..."
2570 major-mode))
2571 (speedbar-update-special-contents)
2572 (if (<= 2 speedbar-verbosity-level)
2573 (progn
2574 (message "Updating speedbar to special mode: %s...done"
2575 major-mode)
2576 (message nil))))
2577 ;; Update all the contents if directories change!
2578 (if (or (member (expand-file-name default-directory)
2579 speedbar-shown-directories)
2580 (and speedbar-ignored-path-regexp
2581 (string-match
2582 speedbar-ignored-path-regexp
2583 (expand-file-name default-directory)))
2584 (member major-mode speedbar-ignored-modes)
2585 (eq af speedbar-frame)
2586 (not (buffer-file-name)))
2587 nil
2588 (if (<= 1 speedbar-verbosity-level)
2589 (message "Updating speedbar to: %s..."
2590 default-directory))
2591 (speedbar-update-directory-contents)
2592 (if (<= 1 speedbar-verbosity-level)
61d7e1dc 2593 (progn
a4252bdb
EL
2594 (message "Updating speedbar to: %s...done"
2595 default-directory)
2596 (message nil)))))
2597 (select-frame af)))
6b3eac8d 2598 ;; Now run stealthy updates of time-consuming items
59588cd4
KH
2599 (speedbar-stealthy-updates)))
2600 ;; Now run the mouse tracking system
2601 (speedbar-show-info-under-mouse)))
6b3eac8d
DN
2602 (run-hooks 'speedbar-timer-hook))
2603
2604\f
2605;;; Stealthy activities
2606;;
59588cd4
KH
2607(defvar speedbar-stealthy-update-recurse nil
2608 "Recursion avoidance variable for stealthy update.")
2609
6b3eac8d
DN
2610(defun speedbar-stealthy-updates ()
2611 "For a given speedbar, run all items in the stealthy function list.
2612Each item returns t if it completes successfully, or nil if
2613interrupted by the user."
59588cd4
KH
2614 (if (not speedbar-stealthy-update-recurse)
2615 (let ((l (speedbar-initial-stealthy-functions))
2616 (speedbar-stealthy-update-recurse t))
2617 (unwind-protect
2618 (while (and l (funcall (car l)))
2619 ;(sit-for 0)
2620 (setq l (cdr l)))
2621 ;;(message "Exit with %S" (car l))
2622 ))))
6b3eac8d
DN
2623
2624(defun speedbar-reset-scanners ()
2625 "Reset any variables used by functions in the stealthy list as state.
2626If new functions are added, their state needs to be updated here."
59588cd4
KH
2627 (setq speedbar-vc-to-do-point t
2628 speedbar-obj-to-do-point t)
6b3eac8d
DN
2629 (run-hooks 'speedbar-scanner-reset-hook)
2630 )
2631
fd7bd989
EL
2632(defun speedbar-find-selected-file (file)
2633 "Goto the line where FILE is."
2634 (goto-char (point-min))
2635 (let ((m nil))
2636 (while (and (setq m (re-search-forward
2637 (concat " \\(" (file-name-nondirectory file)
2638 "\\)\\(" speedbar-indicator-regex "\\)?\n")
2639 nil t))
2640 (not (string= file
2641 (concat
2642 (speedbar-line-path
2643 (save-excursion
2644 (goto-char (match-beginning 0))
2645 (beginning-of-line)
2646 (save-match-data
2647 (looking-at "[0-9]+:")
2648 (string-to-number (match-string 0)))))
2649 (match-string 1))))))
2650 (if m
2651 (progn
2652 (goto-char (match-beginning 1))
2653 (match-string 1)))))
2654
6b3eac8d
DN
2655(defun speedbar-clear-current-file ()
2656 "Locate the file thought to be current, and remove its highlighting."
2657 (save-excursion
2658 (set-buffer speedbar-buffer)
2659 (if speedbar-last-selected-file
2660 (speedbar-with-writable
fd7bd989 2661 (if (speedbar-find-selected-file speedbar-last-selected-file)
6b3eac8d
DN
2662 (put-text-property (match-beginning 1)
2663 (match-end 1)
2664 'face
2665 'speedbar-file-face))))))
2666
2667(defun speedbar-update-current-file ()
2668 "Find the current file, and update our visuals to indicate its name.
2669This is specific to file names. If the file name doesn't show up, but
2670it should be in the list, then the directory cache needs to be
2671updated."
2672 (let* ((lastf (selected-frame))
2673 (newcfd (save-excursion
2674 (select-frame speedbar-attached-frame)
2675 (let ((rf (if (buffer-file-name)
2676 (buffer-file-name)
2677 nil)))
2678 (select-frame lastf)
2679 rf)))
fd7bd989 2680 (newcf (if newcfd newcfd))
6b3eac8d 2681 (lastb (current-buffer))
59588cd4
KH
2682 (sucf-recursive (boundp 'sucf-recursive))
2683 (case-fold-search t))
6b3eac8d
DN
2684 (if (and newcf
2685 ;; check here, that way we won't refresh to newcf until
2686 ;; its been written, thus saving ourselves some time
2687 (file-exists-p newcf)
2688 (not (string= newcf speedbar-last-selected-file)))
2689 (progn
2690 ;; It is important to select the frame, otherwise the window
2691 ;; we want the cursor to move in will not be updated by the
2692 ;; search-forward command.
2693 (select-frame speedbar-frame)
2694 ;; Remove the old file...
2695 (speedbar-clear-current-file)
2696 ;; now highlight the new one.
2697 (set-buffer speedbar-buffer)
2698 (speedbar-with-writable
fd7bd989
EL
2699 (if (speedbar-find-selected-file newcf)
2700 ;; put the property on it
2701 (put-text-property (match-beginning 1)
2702 (match-end 1)
2703 'face
2704 'speedbar-selected-face)
6b3eac8d
DN
2705 ;; Oops, it's not in the list. Should it be?
2706 (if (and (string-match speedbar-file-regexp newcf)
2707 (string= (file-name-directory newcfd)
2708 (expand-file-name default-directory)))
2709 ;; yes, it is (we will ignore unknowns for now...)
2710 (progn
2711 (speedbar-refresh)
fd7bd989 2712 (if (speedbar-find-selected-file newcf)
6b3eac8d
DN
2713 ;; put the property on it
2714 (put-text-property (match-beginning 1)
2715 (match-end 1)
2716 'face
2717 'speedbar-selected-face)))
2718 ;; if it's not in there now, whatever...
2719 ))
2720 (setq speedbar-last-selected-file newcf))
2721 (if (not sucf-recursive)
0e596101
EL
2722 (progn
2723 (speedbar-center-buffer-smartly)
2724 (speedbar-position-cursor-on-line)
2725 ))
6b3eac8d
DN
2726 (set-buffer lastb)
2727 (select-frame lastf)
2728 )))
2729 ;; return that we are done with this activity.
2730 t)
2731
59588cd4
KH
2732(defun speedbar-add-indicator (indicator-string &optional replace-this)
2733 "Add INDICATOR-STRING to the end of this speedbar line.
2734If INDICATOR-STRING is space, and REPLACE-THIS is a character, then
2735an the existing indicator is removed. If there is already an
2736indicator, then do not add a space."
2737 (beginning-of-line)
2738 ;; The nature of the beast: Assume we are in "the right place"
2739 (end-of-line)
2740 (skip-chars-backward (concat " " speedbar-vc-indicator
2741 (car speedbar-obj-indicator)
2742 (cdr speedbar-obj-indicator)))
2743 (if (and (not (looking-at speedbar-indicator-regex))
2744 (not (string= indicator-string " ")))
2745 (insert speedbar-indicator-separator))
2746 (speedbar-with-writable
2747 (save-excursion
2748 (if (and replace-this
2749 (re-search-forward replace-this (save-excursion (end-of-line)
2750 (point))
2751 t))
2752 (delete-region (match-beginning 0) (match-end 0))))
2753 (end-of-line)
2754 (if (not (string= " " indicator-string))
2755 (insert indicator-string))))
2756
2757;; Load efs/ange-ftp only if compiling to remove byte-compiler warnings.
6b3eac8d 2758;; Steven L Baur <steve@xemacs.org> said this was important:
59588cd4
KH
2759(eval-when-compile (or (featurep 'xemacs)
2760 (condition-case () (require 'efs)
2761 (error (require 'ange-ftp)))))
6b3eac8d
DN
2762
2763(defun speedbar-check-vc ()
2764 "Scan all files in a directory, and for each see if it's checked out.
2765See `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p' for how
2766to add more types of version control systems."
2767 ;; Check for to-do to be reset. If reset but no RCS is available
2768 ;; then set to nil (do nothing) otherwise, start at the beginning
2769 (save-excursion
2770 (set-buffer speedbar-buffer)
2771 (if (and speedbar-vc-do-check (eq speedbar-vc-to-do-point t)
2772 (speedbar-vc-check-dir-p default-directory)
59588cd4
KH
2773 (not (or (and (featurep 'ange-ftp)
2774 (string-match
2775 (car (if speedbar-xemacsp
2776 ange-ftp-path-format
2777 ange-ftp-name-format))
2778 (expand-file-name default-directory)))
2779 ;; efs support: Bob Weiner
2780 (and (featurep 'efs)
2781 (string-match
2782 (car efs-path-regexp)
2783 (expand-file-name default-directory))))))
6b3eac8d
DN
2784 (setq speedbar-vc-to-do-point 0))
2785 (if (numberp speedbar-vc-to-do-point)
2786 (progn
2787 (goto-char speedbar-vc-to-do-point)
2788 (while (and (not (input-pending-p))
2789 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2790 nil t))
2791 (setq speedbar-vc-to-do-point (point))
2792 (if (speedbar-check-vc-this-line (match-string 1))
59588cd4
KH
2793 (speedbar-add-indicator speedbar-vc-indicator
2794 (regexp-quote speedbar-vc-indicator))
2795 (speedbar-add-indicator " "
2796 (regexp-quote speedbar-vc-indicator))))
6b3eac8d
DN
2797 (if (input-pending-p)
2798 ;; return that we are incomplete
2799 nil
2800 ;; we are done, set to-do to nil
2801 (setq speedbar-vc-to-do-point nil)
2802 ;; and return t
2803 t))
2804 t)))
2805
2806(defun speedbar-check-vc-this-line (depth)
2807 "Return t if the file on this line is check of of a version control system.
2808Parameter DEPTH is a string with the current depth of indentation of
2809the file being checked."
2810 (let* ((d (string-to-int depth))
2811 (f (speedbar-line-path d))
2812 (fn (buffer-substring-no-properties
2813 ;; Skip-chars: thanks ptype@dra.hmg.gb
2814 (point) (progn
2815 (skip-chars-forward "^ "
2816 (save-excursion (end-of-line)
2817 (point)))
2818 (point))))
2819 (fulln (concat f fn)))
2820 (if (<= 2 speedbar-verbosity-level)
2821 (message "Speedbar vc check...%s" fulln))
2822 (and (file-writable-p fulln)
2823 (speedbar-this-file-in-vc f fn))))
2824
2825(defun speedbar-vc-check-dir-p (path)
2826 "Return t if we should bother checking PATH for version control files.
2827This can be overloaded to add new types of version control systems."
2828 (or
2829 ;; Local RCS
2830 (file-exists-p (concat path "RCS/"))
2831 ;; Local SCCS
2832 (file-exists-p (concat path "SCCS/"))
2833 ;; Remote SCCS project
2834 (let ((proj-dir (getenv "PROJECTDIR")))
2835 (if proj-dir
2836 (file-exists-p (concat proj-dir "/SCCS"))
2837 nil))
2838 ;; User extension
2839 (run-hook-with-args 'speedbar-vc-path-enable-hook path)
2840 ))
2841
2842(defun speedbar-this-file-in-vc (path name)
2843 "Check to see if the file in PATH with NAME is in a version control system.
2844You can add new VC systems by overriding this function. You can
2845optimize this function by overriding it and only doing those checks
2846that will occur on your system."
2847 (or
2848 ;; RCS file name
2849 (file-exists-p (concat path "RCS/" name ",v"))
0e596101 2850 (file-exists-p (concat path "RCS/" name))
6b3eac8d
DN
2851 ;; Local SCCS file name
2852 (file-exists-p (concat path "SCCS/p." name))
2853 ;; Remote SCCS file name
2854 (let ((proj-dir (getenv "PROJECTDIR")))
2855 (if proj-dir
2856 (file-exists-p (concat proj-dir "/SCCS/p." name))
2857 nil))
2858 ;; User extension
2859 (run-hook-with-args 'speedbar-vc-in-control-hook path name)
2860 ))
59588cd4
KH
2861
2862;; Objet File scanning
2863(defun speedbar-check-objects ()
2864 "Scan all files in a directory, and for each see if there is an object.
2865See `speedbar-check-obj-this-line' and `speedbar-obj-alist' for how
2866to add more object types."
2867 ;; Check for to-do to be reset. If reset but no RCS is available
2868 ;; then set to nil (do nothing) otherwise, start at the beginning
2869 (save-excursion
2870 (set-buffer speedbar-buffer)
2871 (if (and speedbar-obj-do-check (eq speedbar-obj-to-do-point t))
2872 (setq speedbar-obj-to-do-point 0))
2873 (if (numberp speedbar-obj-to-do-point)
2874 (progn
2875 (goto-char speedbar-obj-to-do-point)
2876 (while (and (not (input-pending-p))
2877 (re-search-forward "^\\([0-9]+\\):\\s-*\\[[+-]\\] "
2878 nil t))
2879 (setq speedbar-obj-to-do-point (point))
2880 (let ((ind (speedbar-check-obj-this-line (match-string 1))))
2881 (if (not ind) (setq ind " "))
2882 (speedbar-add-indicator ind (concat
2883 (car speedbar-obj-indicator)
2884 "\\|"
2885 (cdr speedbar-obj-indicator)))))
2886 (if (input-pending-p)
2887 ;; return that we are incomplete
2888 nil
2889 ;; we are done, set to-do to nil
2890 (setq speedbar-obj-to-do-point nil)
2891 ;; and return t
2892 t))
2893 t)))
2894
2895(defun speedbar-check-obj-this-line (depth)
2896 "Return t if the file on this line has an associated object.
2897Parameter DEPTH is a string with the current depth of indentation of
2898the file being checked."
2899 (let* ((d (string-to-int depth))
2900 (f (speedbar-line-path d))
2901 (fn (buffer-substring-no-properties
2902 ;; Skip-chars: thanks ptype@dra.hmg.gb
2903 (point) (progn
2904 (skip-chars-forward "^ "
2905 (save-excursion (end-of-line)
2906 (point)))
2907 (point))))
2908 (fulln (concat f fn)))
2909 (if (<= 2 speedbar-verbosity-level)
2910 (message "Speedbar obj check...%s" fulln))
2911 (let ((oa speedbar-obj-alist))
2912 (while (and oa (not (string-match (car (car oa)) fulln)))
2913 (setq oa (cdr oa)))
2914 (if (not (and oa (file-exists-p (concat (file-name-sans-extension fulln)
2915 (cdr (car oa))))))
2916 nil
2917 ;; Find out if the object is out of date or not.
2918 (let ((date1 (nth 5 (file-attributes fulln)))
2919 (date2 (nth 5 (file-attributes (concat
2920 (file-name-sans-extension fulln)
2921 (cdr (car oa)))))))
2922 (if (or (< (car date1) (car date2))
2923 (and (= (car date1) (car date2))
2924 (< (nth 1 date1) (nth 1 date2))))
2925 (car speedbar-obj-indicator)
2926 (cdr speedbar-obj-indicator)))))))
6b3eac8d
DN
2927\f
2928;;; Clicking Activity
2929;;
2930(defun speedbar-quick-mouse (e)
2931 "Since mouse events are strange, this will keep the mouse nicely positioned.
2932This should be bound to mouse event E."
2933 (interactive "e")
2934 (mouse-set-point e)
2935 (speedbar-position-cursor-on-line)
2936 )
2937
2938(defun speedbar-position-cursor-on-line ()
2939 "Position the cursor on a line."
2940 (let ((oldpos (point)))
2941 (beginning-of-line)
2942 (if (looking-at "[0-9]+:\\s-*..?.? ")
2943 (goto-char (1- (match-end 0)))
2944 (goto-char oldpos))))
2945
2946(defun speedbar-power-click (e)
2947 "Activate any speedbar button as a power click.
2948This should be bound to mouse event E."
2949 (interactive "e")
2950 (let ((speedbar-power-click t))
2951 (speedbar-click e)))
2952
2953(defun speedbar-click (e)
2954 "Activate any speedbar buttons where the mouse is clicked.
2955This must be bound to a mouse event. A button is any location of text
2956with a mouse face that has a text property called `speedbar-function'.
2957This should be bound to mouse event E."
2958 (interactive "e")
2959 (mouse-set-point e)
2960 (speedbar-do-function-pointer)
2961 (speedbar-quick-mouse e))
2962
2963(defun speedbar-double-click (e)
2964 "Activate any speedbar buttons where the mouse is clicked.
2965This must be bound to a mouse event. A button is any location of text
2966with a mouse face that has a text property called `speedbar-function'.
2967This should be bound to mouse event E."
2968 (interactive "e")
2969 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
2970 (cond ((eq (car e) 'down-mouse-1)
2971 (mouse-set-point e))
2972 ((eq (car e) 'mouse-1)
2973 (speedbar-quick-mouse e))
2974 ((or (eq (car e) 'double-down-mouse-1)
59588cd4 2975 (eq (car e) 'triple-down-mouse-1))
6b3eac8d
DN
2976 (mouse-set-point e)
2977 (speedbar-do-function-pointer)
2978 (speedbar-quick-mouse e))))
2979
2980(defun speedbar-do-function-pointer ()
2981 "Look under the cursor and examine the text properties.
2982From this extract the file/tag name, token, indentation level and call
2983a function if appropriate"
2984 (let* ((fn (get-text-property (point) 'speedbar-function))
2985 (tok (get-text-property (point) 'speedbar-token))
2986 ;; The 1-,+ is safe because scaning starts AFTER the point
2987 ;; specified. This lets the search include the character the
2988 ;; cursor is on.
2989 (tp (previous-single-property-change
2990 (1+ (point)) 'speedbar-function))
2991 (np (next-single-property-change
2992 (point) 'speedbar-function))
2993 (txt (buffer-substring-no-properties (or tp (point-min))
2994 (or np (point-max))))
2995 (dent (save-excursion (beginning-of-line)
2996 (string-to-number
2997 (if (looking-at "[0-9]+")
2998 (buffer-substring-no-properties
2999 (match-beginning 0) (match-end 0))
3000 "0")))))
3001 ;;(message "%S:%S:%S:%s" fn tok txt dent)
3002 (and fn (funcall fn txt tok dent)))
3003 (speedbar-position-cursor-on-line))
3004\f
3005;;; Reading info from the speedbar buffer
3006;;
3007(defun speedbar-line-file (&optional p)
3008 "Retrieve the file or whatever from the line at P point.
3009The return value is a string representing the file. If it is a
3010directory, then it is the directory name."
3011 (save-excursion
3012 (save-match-data
3013 (beginning-of-line)
3014 (if (looking-at (concat
3015 "\\([0-9]+\\): *[[<][-+?][]>] \\([^ \n]+\\)\\("
59588cd4 3016 speedbar-indicator-regex "\\)?"))
6b3eac8d
DN
3017 (let* ((depth (string-to-int (match-string 1)))
3018 (path (speedbar-line-path depth))
3019 (f (match-string 2)))
3020 (concat path f))
3021 nil))))
3022
3023(defun speedbar-goto-this-file (file)
3024 "If FILE is displayed, goto this line and return t.
3025Otherwise do not move and return nil."
3026 (let ((path (substring (file-name-directory (expand-file-name file))
3027 (length (expand-file-name default-directory))))
3028 (dest (point)))
3029 (save-match-data
3030 (goto-char (point-min))
3031 ;; scan all the directories
3032 (while (and path (not (eq path t)))
3033 (if (string-match "^/?\\([^/]+\\)" path)
3034 (let ((pp (match-string 1 path)))
3035 (if (save-match-data
3036 (re-search-forward (concat "> " (regexp-quote pp) "$")
3037 nil t))
3038 (setq path (substring path (match-end 1)))
3039 (setq path nil)))
3040 (setq path t)))
3041 ;; find the file part
3042 (if (or (not path) (string= (file-name-nondirectory file) ""))
3043 ;; only had a dir part
3044 (if path
3045 (progn
3046 (speedbar-position-cursor-on-line)
3047 t)
3048 (goto-char dest) nil)
3049 ;; find the file part
3050 (let ((nd (file-name-nondirectory file)))
3051 (if (re-search-forward
3052 (concat "] \\(" (regexp-quote nd)
59588cd4 3053 "\\)\\(" speedbar-indicator-regex "\\)$")
6b3eac8d
DN
3054 nil t)
3055 (progn
3056 (speedbar-position-cursor-on-line)
3057 t)
3058 (goto-char dest)
3059 nil))))))
3060
3061(defun speedbar-line-path (depth)
3062 "Retrieve the pathname associated with the current line.
3063This may require traversing backwards from DEPTH and combining the default
3064directory with these items."
59588cd4
KH
3065 (cond
3066 ((string= speedbar-initial-expansion-list-name "files")
3067 (save-excursion
3068 (save-match-data
3069 (let ((path nil))
3070 (setq depth (1- depth))
3071 (while (/= depth -1)
3072 (if (not (re-search-backward (format "^%d:" depth) nil t))
3073 (error "Error building path of tag")
3074 (cond ((looking-at "[0-9]+:\\s-*<->\\s-+\\([^\n]+\\)$")
3075 (setq path (concat (buffer-substring-no-properties
3076 (match-beginning 1) (match-end 1))
3077 "/"
3078 path)))
3079 ((looking-at "[0-9]+:\\s-*[-]\\s-+\\([^\n]+\\)$")
3080 ;; This is the start of our path.
3081 (setq path (buffer-substring-no-properties
3082 (match-beginning 1) (match-end 1))))))
3083 (setq depth (1- depth)))
3084 (if (and path
3085 (string-match (concat speedbar-indicator-regex "$")
3086 path))
3087 (setq path (substring path 0 (match-beginning 0))))
3088 (concat default-directory path)))))
3089 (t
3090 ;; If we aren't in file mode, then return an empty string to make
3091 ;; sure that we can still get some stuff done.
3092 "")))
6b3eac8d
DN
3093
3094(defun speedbar-path-line (path)
3095 "Position the cursor on the line specified by PATH."
3096 (save-match-data
3097 (if (string-match "/$" path)
3098 (setq path (substring path 0 (match-beginning 0))))
3099 (let ((nomatch t) (depth 0)
3100 (fname (file-name-nondirectory path))
3101 (pname (file-name-directory path)))
3102 (if (not (member pname speedbar-shown-directories))
59588cd4 3103 (error "Internal Error: File %s not shown in speedbar" path))
6b3eac8d
DN
3104 (goto-char (point-min))
3105 (while (and nomatch
3106 (re-search-forward
3107 (concat "[]>] \\(" (regexp-quote fname)
59588cd4 3108 "\\)\\(" speedbar-indicator-regex "\\)?$")
6b3eac8d
DN
3109 nil t))
3110 (beginning-of-line)
3111 (looking-at "\\([0-9]+\\):")
3112 (setq depth (string-to-int (match-string 0))
3113 nomatch (not (string= pname (speedbar-line-path depth))))
3114 (end-of-line))
3115 (beginning-of-line)
3116 (not nomatch))))
3117
3118(defun speedbar-edit-line ()
3119 "Edit whatever tag or file is on the current speedbar line."
3120 (interactive)
3121 (or (save-excursion
3122 (beginning-of-line)
3123 ;; If this fails, then it is a non-standard click, and as such,
3124 ;; perfectly allowed.
fd7bd989 3125 (if (re-search-forward "[]>?}] [^ ]"
6b3eac8d
DN
3126 (save-excursion (end-of-line) (point))
3127 t)
3128 (speedbar-do-function-pointer)
3129 nil))
3130 (speedbar-do-function-pointer)))
3131
3132(defun speedbar-expand-line ()
3133 "Expand the line under the cursor."
3134 (interactive)
3135 (beginning-of-line)
3136 (re-search-forward ":\\s-*.\\+. " (save-excursion (end-of-line) (point)))
3137 (forward-char -2)
3138 (speedbar-do-function-pointer))
3139
3140(defun speedbar-contract-line ()
3141 "Contract the line under the cursor."
3142 (interactive)
3143 (beginning-of-line)
3144 (re-search-forward ":\\s-*.-. " (save-excursion (end-of-line) (point)))
3145 (forward-char -2)
3146 (speedbar-do-function-pointer))
3147
3148(if speedbar-xemacsp
3149 (defalias 'speedbar-mouse-event-p 'button-press-event-p)
3150 (defun speedbar-mouse-event-p (event)
3151 "Return t if the event is a mouse related event"
3152 ;; And Emacs does it this way
3153 (if (and (listp event)
3154 (member (event-basic-type event)
3155 '(mouse-1 mouse-2 mouse-3)))
3156 t
3157 nil)))
3158
3159(defun speedbar-maybee-jump-to-attached-frame ()
3160 "Jump to the attached frame ONLY if this was not a mouse event."
3161 (if (or (not (speedbar-mouse-event-p last-input-event))
3162 speedbar-activity-change-focus-flag)
3163 (progn
3164 (select-frame speedbar-attached-frame)
3165 (other-frame 0))))
3166
3167(defun speedbar-find-file (text token indent)
3168 "Speedbar click handler for filenames.
3169TEXT, the file will be displayed in the attached frame.
3170TOKEN is unused, but required by the click handler. INDENT is the
3171current indentation level."
3172 (let ((cdd (speedbar-line-path indent)))
3173 (speedbar-find-file-in-frame (concat cdd text))
3174 (speedbar-stealthy-updates)
3175 (run-hooks 'speedbar-visiting-file-hook)
3176 ;; Reset the timer with a new timeout when cliking a file
3177 ;; in case the user was navigating directories, we can cancel
3178 ;; that other timer.
3179 (speedbar-set-timer speedbar-update-speed))
3180 (speedbar-maybee-jump-to-attached-frame))
3181
3182(defun speedbar-dir-follow (text token indent)
3183 "Speedbar click handler for directory names.
3184Clicking a directory will cause the speedbar to list files in the
3185the subdirectory TEXT. TOKEN is an unused requirement. The
3186subdirectory chosen will be at INDENT level."
3187 (setq default-directory
3188 (concat (expand-file-name (concat (speedbar-line-path indent) text))
3189 "/"))
3190 ;; Because we leave speedbar as the current buffer,
3191 ;; update contents will change directory without
59588cd4
KH
3192 ;; having to touch the attached frame. Turn off smart expand just
3193 ;; in case.
3194 (let ((speedbar-smart-directory-expand-flag nil))
3195 (speedbar-update-contents))
6b3eac8d
DN
3196 (speedbar-set-timer speedbar-navigating-speed)
3197 (setq speedbar-last-selected-file nil)
3198 (speedbar-stealthy-updates))
3199
3200(defun speedbar-delete-subblock (indent)
3201 "Delete text from point to indentation level INDENT or greater.
3202Handles end-of-sublist smartly."
3203 (speedbar-with-writable
fd7bd989
EL
3204 (save-excursion
3205 (end-of-line) (forward-char 1)
3206 (let ((start (point)))
3207 (while (and (looking-at "^\\([0-9]+\\):")
3208 (> (string-to-int (match-string 1)) indent)
3209 (not (eobp)))
3210 (forward-line 1)
3211 (beginning-of-line))
3212 (delete-region start (point))))))
6b3eac8d
DN
3213
3214(defun speedbar-dired (text token indent)
3215 "Speedbar click handler for directory expand button.
3216Clicking this button expands or contracts a directory. TEXT is the
3217button clicked which has either a + or -. TOKEN is the directory to be
3218expanded. INDENT is the current indentation level."
3219 (cond ((string-match "+" text) ;we have to expand this dir
3220 (setq speedbar-shown-directories
3221 (cons (expand-file-name
3222 (concat (speedbar-line-path indent) token "/"))
3223 speedbar-shown-directories))
3224 (speedbar-change-expand-button-char ?-)
3225 (speedbar-reset-scanners)
3226 (save-excursion
3227 (end-of-line) (forward-char 1)
3228 (speedbar-with-writable
3229 (speedbar-default-directory-list
3230 (concat (speedbar-line-path indent) token "/")
3231 (1+ indent)))))
3232 ((string-match "-" text) ;we have to contract this node
3233 (speedbar-reset-scanners)
3234 (let ((oldl speedbar-shown-directories)
3235 (newl nil)
3236 (td (expand-file-name
3237 (concat (speedbar-line-path indent) token))))
3238 (while oldl
3239 (if (not (string-match (concat "^" (regexp-quote td)) (car oldl)))
3240 (setq newl (cons (car oldl) newl)))
3241 (setq oldl (cdr oldl)))
3242 (setq speedbar-shown-directories newl))
3243 (speedbar-change-expand-button-char ?+)
3244 (speedbar-delete-subblock indent)
3245 )
59588cd4 3246 (t (error "Ooops... not sure what to do")))
6b3eac8d
DN
3247 (speedbar-center-buffer-smartly)
3248 (setq speedbar-last-selected-file nil)
3249 (save-excursion (speedbar-stealthy-updates)))
3250
3251(defun speedbar-directory-buttons-follow (text token indent)
3252 "Speedbar click handler for default directory buttons.
3253TEXT is the button clicked on. TOKEN is the directory to follow.
3254INDENT is the current indentation level and is unused."
0e596101
EL
3255 (if (string-match "^[A-z]:$" token)
3256 (setq default-directory (concat token (char-to-string directory-sep-char)))
59588cd4 3257 (setq default-directory token))
6b3eac8d
DN
3258 ;; Because we leave speedbar as the current buffer,
3259 ;; update contents will change directory without
3260 ;; having to touch the attached frame.
3261 (speedbar-update-contents)
3262 (speedbar-set-timer speedbar-navigating-speed))
3263
3264(defun speedbar-tag-file (text token indent)
3265 "The cursor is on a selected line. Expand the tags in the specified file.
3266The parameter TEXT and TOKEN are required, where TEXT is the button
3267clicked, and TOKEN is the file to expand. INDENT is the current
3268indentation level."
3269 (cond ((string-match "+" text) ;we have to expand this file
3270 (let* ((fn (expand-file-name (concat (speedbar-line-path indent)
3271 token)))
3272 (lst (if speedbar-use-imenu-flag
3273 (let ((tim (speedbar-fetch-dynamic-imenu fn)))
3274 (if (eq tim t)
3275 (speedbar-fetch-dynamic-etags fn)
3276 tim))
3277 (speedbar-fetch-dynamic-etags fn))))
3278 ;; if no list, then remove expando button
3279 (if (not lst)
3280 (speedbar-change-expand-button-char ??)
3281 (speedbar-change-expand-button-char ?-)
3282 (speedbar-with-writable
3283 (save-excursion
3284 (end-of-line) (forward-char 1)
3285 (speedbar-insert-generic-list indent
3286 lst 'speedbar-tag-expand
3287 'speedbar-tag-find))))))
3288 ((string-match "-" text) ;we have to contract this node
3289 (speedbar-change-expand-button-char ?+)
3290 (speedbar-delete-subblock indent))
59588cd4 3291 (t (error "Ooops... not sure what to do")))
6b3eac8d
DN
3292 (speedbar-center-buffer-smartly))
3293
3294(defun speedbar-tag-find (text token indent)
3295 "For the tag TEXT in a file TOKEN, goto that position.
3296INDENT is the current indentation level."
3297 (let ((file (speedbar-line-path indent)))
3298 (speedbar-find-file-in-frame file)
3299 (save-excursion (speedbar-stealthy-updates))
3300 ;; Reset the timer with a new timeout when cliking a file
3301 ;; in case the user was navigating directories, we can cancel
3302 ;; that other timer.
3303 (speedbar-set-timer speedbar-update-speed)
3304 (goto-char token)
3305 (run-hooks 'speedbar-visiting-tag-hook)
3306 ;;(recenter)
3307 (speedbar-maybee-jump-to-attached-frame)
3308 ))
3309
3310(defun speedbar-tag-expand (text token indent)
3311 "Expand a tag sublist. Imenu will return sub-lists of specialized tag types.
3312Etags does not support this feature. TEXT will be the button
3313string. TOKEN will be the list, and INDENT is the current indentation
3314level."
3315 (cond ((string-match "+" text) ;we have to expand this file
3316 (speedbar-change-expand-button-char ?-)
3317 (speedbar-with-writable
3318 (save-excursion
3319 (end-of-line) (forward-char 1)
59588cd4 3320 (speedbar-insert-generic-list indent token 'speedbar-tag-expand
6b3eac8d
DN
3321 'speedbar-tag-find))))
3322 ((string-match "-" text) ;we have to contract this node
3323 (speedbar-change-expand-button-char ?+)
3324 (speedbar-delete-subblock indent))
59588cd4 3325 (t (error "Ooops... not sure what to do")))
6b3eac8d
DN
3326 (speedbar-center-buffer-smartly))
3327\f
3328;;; Loading files into the attached frame.
3329;;
3330(defun speedbar-find-file-in-frame (file)
3331 "This will load FILE into the speedbar attached frame.
3332If the file is being displayed in a different frame already, then raise that
3333frame instead."
3334 (let* ((buff (find-file-noselect file))
3335 (bwin (get-buffer-window buff 0)))
3336 (if bwin
3337 (progn
3338 (select-window bwin)
3339 (raise-frame (window-frame bwin)))
3340 (if speedbar-power-click
3341 (let ((pop-up-frames t)) (select-window (display-buffer buff)))
3342 (select-frame speedbar-attached-frame)
3343 (switch-to-buffer buff))))
59588cd4 3344 )
6b3eac8d
DN
3345
3346;;; Centering Utility
3347;;
3348(defun speedbar-center-buffer-smartly ()
3349 "Recenter a speedbar buffer so the current indentation level is all visible.
3350This assumes that the cursor is on a file, or tag of a file which the user is
3351interested in."
3352 (if (<= (count-lines (point-min) (point-max))
0e596101 3353 (1- (window-height (selected-window))))
6b3eac8d
DN
3354 ;; whole buffer fits
3355 (let ((cp (point)))
3356 (goto-char (point-min))
3357 (recenter 0)
3358 (goto-char cp))
3359 ;; too big
3360 (let (depth start end exp p)
3361 (save-excursion
3362 (beginning-of-line)
3363 (setq depth (if (looking-at "[0-9]+")
3364 (string-to-int (buffer-substring-no-properties
3365 (match-beginning 0) (match-end 0)))
3366 0))
3367 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3368 (save-excursion
3369 (end-of-line)
3370 (if (re-search-backward exp nil t)
3371 (setq start (point))
fd7bd989 3372 (setq start (point-min)))
6b3eac8d
DN
3373 (save-excursion ;Not sure about this part.
3374 (end-of-line)
3375 (setq p (point))
3376 (while (and (not (re-search-forward exp nil t))
3377 (>= depth 0))
3378 (setq depth (1- depth))
3379 (setq exp (format "^%d:\\s-*[[{<]\\([?+-]\\)[]>}]" depth)))
3380 (if (/= (point) p)
3381 (setq end (point))
3382 (setq end (point-max)))))
3383 ;; Now work out the details of centering
3384 (let ((nl (count-lines start end))
3385 (cp (point)))
3386 (if (> nl (window-height (selected-window)))
3387 ;; We can't fit it all, so just center on cursor
3388 (progn (goto-char start)
3389 (recenter 1))
3390 ;; we can fit everything on the screen, but...
3391 (if (and (pos-visible-in-window-p start (selected-window))
3392 (pos-visible-in-window-p end (selected-window)))
3393 ;; we are all set!
3394 nil
3395 ;; we need to do something...
3396 (goto-char start)
3397 (let ((newcent (/ (- (window-height (selected-window)) nl) 2))
3398 (lte (count-lines start (point-max))))
3399 (if (and (< (+ newcent lte) (window-height (selected-window)))
3400 (> (- (window-height (selected-window)) lte 1)
3401 newcent))
3402 (setq newcent (- (window-height (selected-window))
3403 lte 1)))
3404 (recenter newcent))))
3405 (goto-char cp)))))
3406
3407\f
3408;;; Tag Management -- Imenu
3409;;
3410(if (not speedbar-use-imenu-flag)
3411
3412 nil
3413
3414(eval-when-compile (if (locate-library "imenu") (require 'imenu)))
3415
3416(defun speedbar-fetch-dynamic-imenu (file)
3417 "Load FILE into a buffer, and generate tags using Imenu.
3418Returns the tag list, or t for an error."
3419 ;; Load this AND compile it in
3420 (require 'imenu)
3421 (save-excursion
3422 (set-buffer (find-file-noselect file))
3423 (if speedbar-power-click (setq imenu--index-alist nil))
3424 (condition-case nil
3425 (let ((index-alist (imenu--make-index-alist t)))
3426 (if speedbar-sort-tags
3427 (sort (copy-alist index-alist)
3428 (lambda (a b) (string< (car a) (car b))))
3429 index-alist))
3430 (error t))))
3431)
3432\f
3433;;; Tag Management -- etags (old XEmacs compatibility part)
3434;;
3435(defvar speedbar-fetch-etags-parse-list
3436 '(;; Note that java has the same parse-group as c
3437 ("\\.\\([cChH]\\|c\\+\\+\\|cpp\\|cc\\|hh\\|java\\)\\'" .
3438 speedbar-parse-c-or-c++tag)
3439 ("^\\.emacs$\\|.\\(el\\|l\\|lsp\\)\\'" .
3440 "def[^i]+\\s-+\\(\\(\\w\\|[-_]\\)+\\)\\s-*\C-?")
59588cd4
KH
3441; ("\\.\\([fF]\\|for\\|FOR\\|77\\|90\\)\\'" .
3442; speedbar-parse-fortran77-tag)
6b3eac8d
DN
3443 ("\\.tex\\'" . speedbar-parse-tex-string)
3444 ("\\.p\\'" .
3445 "\\(\\(FUNCTION\\|function\\|PROCEDURE\\|procedure\\)\\s-+\\([a-zA-Z0-9_.:]+\\)\\)\\s-*(?^?")
3446 )
3447 "Associations of file extensions and expressions for extracting tags.
3448To add a new file type, you would want to add a new association to the
3449list, where the car is the file match, and the cdr is the way to
3450extract an element from the tags output. If the output is complex,
3451use a function symbol instead of regexp. The function should expect
3452to be at the beginning of a line in the etags buffer.
3453
3454This variable is ignored if `speedbar-use-imenu-flag' is non-nil.")
3455
3456(defvar speedbar-fetch-etags-command "etags"
3457 "*Command used to create an etags file.
3458
3459This variable is ignored if `speedbar-use-imenu-flag' is t")
3460
3461(defvar speedbar-fetch-etags-arguments '("-D" "-I" "-o" "-")
3462 "*List of arguments to use with `speedbar-fetch-etags-command'.
3463This creates an etags output buffer. Use `speedbar-toggle-etags' to
3464modify this list conveniently.
3465
3466This variable is ignored if `speedbar-use-imenu-flag' is t")
3467
3468(defun speedbar-toggle-etags (flag)
3469 "Toggle FLAG in `speedbar-fetch-etags-arguments'.
3470FLAG then becomes a member of etags command line arguments. If flag
3471is \"sort\", then toggle the value of `speedbar-sort-tags'. If its
3472value is \"show\" then toggle the value of
3473`speedbar-show-unknown-files'.
3474
3475 This function is a convenience function for XEmacs menu created by
3476Farzin Guilak <farzin@protocol.com>"
3477 (interactive)
3478 (cond
3479 ((equal flag "sort")
3480 (setq speedbar-sort-tags (not speedbar-sort-tags)))
3481 ((equal flag "show")
3482 (setq speedbar-show-unknown-files (not speedbar-show-unknown-files)))
3483 ((or (equal flag "-C")
3484 (equal flag "-S")
3485 (equal flag "-D"))
3486 (if (member flag speedbar-fetch-etags-arguments)
3487 (setq speedbar-fetch-etags-arguments
3488 (delete flag speedbar-fetch-etags-arguments))
3489 (add-to-list 'speedbar-fetch-etags-arguments flag)))
3490 (t nil)))
3491
3492(defun speedbar-fetch-dynamic-etags (file)
3493 "For FILE, run etags and create a list of symbols extracted.
3494Each symbol will be associated with its line position in FILE."
3495 (let ((newlist nil))
3496 (unwind-protect
3497 (save-excursion
3498 (if (get-buffer "*etags tmp*")
3499 (kill-buffer "*etags tmp*")) ;kill to clean it up
3500 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3501 (set-buffer (get-buffer-create "*etags tmp*"))
3502 (apply 'call-process speedbar-fetch-etags-command nil
3503 (current-buffer) nil
3504 (append speedbar-fetch-etags-arguments (list file)))
3505 (goto-char (point-min))
3506 (if (<= 1 speedbar-verbosity-level) (message "Fetching etags..."))
3507 (let ((expr
3508 (let ((exprlst speedbar-fetch-etags-parse-list)
3509 (ans nil))
3510 (while (and (not ans) exprlst)
3511 (if (string-match (car (car exprlst)) file)
3512 (setq ans (car exprlst)))
3513 (setq exprlst (cdr exprlst)))
3514 (cdr ans))))
3515 (if expr
3516 (let (tnl)
3517 (while (not (save-excursion (end-of-line) (eobp)))
3518 (save-excursion
3519 (setq tnl (speedbar-extract-one-symbol expr)))
3520 (if tnl (setq newlist (cons tnl newlist)))
3521 (forward-line 1)))
3522 (message "Sorry, no support for a file of that extension"))))
3523 )
3524 (if speedbar-sort-tags
3525 (sort newlist (lambda (a b) (string< (car a) (car b))))
3526 (reverse newlist))))
3527
3528;; This bit donated by Farzin Guilak <farzin@protocol.com> but I'm not
3529;; sure it's needed with the different sorting method.
3530;;
3531;(defun speedbar-clean-etags()
3532; "Removes spaces before the ^? character, and removes `#define',
3533;return types, etc. preceding tags. This ensures that the sort operation
3534;works on the tags, not the return types."
3535; (save-excursion
3536; (goto-char (point-min))
3537; (while
3538; (re-search-forward "(?[ \t](?\C-?" nil t)
3539; (replace-match "\C-?" nil nil))
3540; (goto-char (point-min))
3541; (while
3542; (re-search-forward "\\(.*[ \t]+\\)\\([^ \t\n]+.*\C-?\\)" nil t)
3543; (delete-region (match-beginning 1) (match-end 1)))))
3544
3545(defun speedbar-extract-one-symbol (expr)
59588cd4 3546 "At point, return nil, or one alist in the form: (SYMBOL . POSITION)
6b3eac8d
DN
3547The line should contain output from etags. Parse the output using the
3548regular expression EXPR"
3549 (let* ((sym (if (stringp expr)
3550 (if (save-excursion
3551 (re-search-forward expr (save-excursion
3552 (end-of-line)
3553 (point)) t))
3554 (buffer-substring-no-properties (match-beginning 1)
3555 (match-end 1)))
3556 (funcall expr)))
3557 (pos (let ((j (re-search-forward "[\C-?\C-a]\\([0-9]+\\),\\([0-9]+\\)"
3558 (save-excursion
3559 (end-of-line)
3560 (point))
3561 t)))
3562 (if (and j sym)
3563 (1+ (string-to-int (buffer-substring-no-properties
3564 (match-beginning 2)
3565 (match-end 2))))
3566 0))))
3567 (if (/= pos 0)
3568 (cons sym pos)
3569 nil)))
3570
3571(defun speedbar-parse-c-or-c++tag ()
3572 "Parse a c or c++ tag, which tends to be a little complex."
3573 (save-excursion
3574 (let ((bound (save-excursion (end-of-line) (point))))
3575 (cond ((re-search-forward "\C-?\\([^\C-a]+\\)\C-a" bound t)
3576 (buffer-substring-no-properties (match-beginning 1)
3577 (match-end 1)))
3578 ((re-search-forward "\\<\\([^ \t]+\\)\\s-+new(" bound t)
3579 (buffer-substring-no-properties (match-beginning 1)
3580 (match-end 1)))
3581 ((re-search-forward "\\<\\([^ \t(]+\\)\\s-*(\C-?" bound t)
3582 (buffer-substring-no-properties (match-beginning 1)
3583 (match-end 1)))
3584 (t nil))
3585 )))
3586
3587(defun speedbar-parse-tex-string ()
3588 "Parse a Tex string. Only find data which is relevant."
3589 (save-excursion
3590 (let ((bound (save-excursion (end-of-line) (point))))
3591 (cond ((re-search-forward "\\(\\(sub\\)*section\\|chapter\\|cite\\)\\s-*{[^\C-?}]*}?" bound t)
3592 (buffer-substring-no-properties (match-beginning 0)
3593 (match-end 0)))
3594 (t nil)))))
3595
3596\f
59588cd4
KH
3597;;; BUFFER DISPLAY mode.
3598;;
3599(defvar speedbar-buffers-key-map nil
3600 "Keymap used when in the buffers display mode.")
3601
3602(if speedbar-buffers-key-map
3603 nil
3604 (setq speedbar-buffers-key-map (speedbar-make-specialized-keymap))
3605
3606 ;; Basic tree features
3607 (define-key speedbar-buffers-key-map "e" 'speedbar-edit-line)
3608 (define-key speedbar-buffers-key-map "\C-m" 'speedbar-edit-line)
3609 (define-key speedbar-buffers-key-map "+" 'speedbar-expand-line)
3610 (define-key speedbar-buffers-key-map "-" 'speedbar-contract-line)
3611
3612 ;; Buffer specific keybindings
3613 (define-key speedbar-buffers-key-map "k" 'speedbar-buffer-kill-buffer)
3614 (define-key speedbar-buffers-key-map "r" 'speedbar-buffer-revert-buffer)
3615
3616 )
3617
3618(defvar speedbar-buffer-easymenu-definition
3619 '(["Jump to buffer" speedbar-edit-line t]
3620 ["Expand File Tags" speedbar-expand-line
3621 (save-excursion (beginning-of-line)
3622 (looking-at "[0-9]+: *.\\+. "))]
3623 ["Contract File Tags" speedbar-contract-line
3624 (save-excursion (beginning-of-line)
3625 (looking-at "[0-9]+: *.-. "))]
3626 )
3627 "Menu item elements shown when displaying a buffer list.")
3628
3629(defun speedbar-buffer-buttons (directory zero)
3630 "Create speedbar buttons based on the buffers currently loaded.
3631DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3632 (speedbar-buffer-buttons-engine nil))
3633
3634(defun speedbar-buffer-buttons-temp (directory zero)
3635 "Create speedbar buttons based on the buffers currently loaded.
3636DIRECTORY is the path to the currently active buffer, and ZERO is 0."
3637 (speedbar-buffer-buttons-engine t))
3638
3639(defun speedbar-buffer-buttons-engine (temp)
3640 "Create speedbar buffer buttons.
3641If TEMP is non-nil, then clicking on a buffer restores the previous display."
3642 (insert "Active Buffers:\n")
3643 (let ((bl (buffer-list)))
3644 (while bl
3645 (if (string-match "^[ *]" (buffer-name (car bl)))
3646 nil
3647 (let* ((known (string-match speedbar-file-regexp
3648 (buffer-name (car bl))))
3649 (expchar (if known ?+ ??))
3650 (fn (if known 'speedbar-tag-file nil))
3651 (fname (save-excursion (set-buffer (car bl))
3652 (buffer-file-name))))
3653 (speedbar-make-tag-line 'bracket expchar fn fname
3654 (buffer-name (car bl))
3655 'speedbar-buffer-click temp
3656 'speedbar-file-face 0)))
3657 (setq bl (cdr bl)))
3658 (setq bl (buffer-list))
3659 (insert "Scratch Buffers:\n")
3660 (while bl
3661 (if (not (string-match "^\\*" (buffer-name (car bl))))
3662 nil
3663 (if (eq (car bl) speedbar-buffer)
3664 nil
3665 (speedbar-make-tag-line 'bracket ?? nil nil
3666 (buffer-name (car bl))
3667 'speedbar-buffer-click temp
3668 'speedbar-file-face 0)))
3669 (setq bl (cdr bl)))
3670 (setq bl (buffer-list))
3671 (insert "Hidden Buffers:\n")
3672 (while bl
3673 (if (not (string-match "^ " (buffer-name (car bl))))
3674 nil
3675 (if (eq (car bl) speedbar-buffer)
3676 nil
3677 (speedbar-make-tag-line 'bracket ?? nil nil
3678 (buffer-name (car bl))
3679 'speedbar-buffer-click temp
3680 'speedbar-file-face 0)))
3681 (setq bl (cdr bl)))))
3682
3683(defun speedbar-buffer-click (text token indent)
3684 "When the users clicks on a buffer-button in speedbar.
3685TEXT is the buffer's name, TOKEN and INDENT are unused."
3686 (if speedbar-power-click
3687 (let ((pop-up-frames t)) (select-window (display-buffer text)))
3688 (select-frame speedbar-attached-frame)
3689 (switch-to-buffer text)
3690 (if token (speedbar-change-initial-expansion-list
3691 speedbar-previously-used-expansion-list-name))))
3692
3693(defun speedbar-buffer-kill-buffer ()
3694 "Kill the buffer the cursor is on in the speedbar buffer."
3695 (interactive)
3696 (or (save-excursion
3697 (beginning-of-line)
3698 ;; If this fails, then it is a non-standard click, and as such,
3699 ;; perfectly allowed.
fd7bd989 3700 (if (re-search-forward "[]>?}] [^ ]"
59588cd4
KH
3701 (save-excursion (end-of-line) (point))
3702 t)
3703 (let ((text (progn
3704 (forward-char -1)
3705 (buffer-substring (point) (save-excursion
3706 (end-of-line)
3707 (point))))))
3708 (if (and (get-buffer text)
3709 (y-or-n-p (format "Kill buffer %s? " text)))
0e596101
EL
3710 (kill-buffer text))
3711 (speedbar-refresh))))))
59588cd4
KH
3712
3713(defun speedbar-buffer-revert-buffer ()
3714 "Revert the buffer the cursor is on in the speedbar buffer."
3715 (interactive)
3716 (save-excursion
3717 (beginning-of-line)
3718 ;; If this fails, then it is a non-standard click, and as such,
3719 ;; perfectly allowed
fd7bd989 3720 (if (re-search-forward "[]>?}] [^ ]"
59588cd4
KH
3721 (save-excursion (end-of-line) (point))
3722 t)
3723 (let ((text (progn
3724 (forward-char -1)
3725 (buffer-substring (point) (save-excursion
3726 (end-of-line)
3727 (point))))))
3728 (if (get-buffer text)
3729 (progn
3730 (set-buffer text)
3731 (revert-buffer t)))))))
3732
3733
3734\f
6b3eac8d
DN
3735;;; Color loading section This is messy *Blech!*
3736;;
3737(defface speedbar-button-face '((((class color) (background light))
3738 (:foreground "green4"))
3739 (((class color) (background dark))
3740 (:foreground "green3")))
3741 "Face used for +/- buttons."
3742 :group 'speedbar-faces)
3743
3744(defface speedbar-file-face '((((class color) (background light))
3745 (:foreground "cyan4"))
3746 (((class color) (background dark))
3747 (:foreground "cyan"))
3748 (t (:bold t)))
3749 "Face used for file names."
3750 :group 'speedbar-faces)
3751
3752(defface speedbar-directory-face '((((class color) (background light))
3753 (:foreground "blue4"))
3754 (((class color) (background dark))
3755 (:foreground "light blue")))
3756 "Faced used for directory names."
3757 :group 'speedbar-faces)
3758(defface speedbar-tag-face '((((class color) (background light))
3759 (:foreground "brown"))
3760 (((class color) (background dark))
3761 (:foreground "yellow")))
3762 "Face used for displaying tags."
3763 :group 'speedbar-faces)
3764
3765(defface speedbar-selected-face '((((class color) (background light))
3766 (:foreground "red" :underline t))
3767 (((class color) (background dark))
3768 (:foreground "red" :underline t))
3769 (t (:underline t)))
3770 "Face used to underline the file in the active window."
3771 :group 'speedbar-faces)
3772
3773(defface speedbar-highlight-face '((((class color) (background light))
3774 (:background "green"))
3775 (((class color) (background dark))
3776 (:background "sea green"))
3777 (((class grayscale monochrome)
3778 (background light))
3779 (:background "black"))
3780 (((class grayscale monochrome)
3781 (background dark))
3782 (:background "white")))
3783 "Face used for highlighting buttons with the mouse."
3784 :group 'speedbar-faces)
3785
3786;; some edebug hooks
3787(add-hook 'edebug-setup-hook
3788 (lambda ()
3789 (def-edebug-spec speedbar-with-writable def-body)))
3790
3791(provide 'speedbar)
3792;;; speedbar ends here
3793
3794;; run load-time hooks
3795(run-hooks 'speedbar-load-hook)