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