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