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