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