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