speedbar.el 145 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1
;;; speedbar --- quick access to files and tags in a frame
Dan Nicolaescu's avatar
Dan Nicolaescu committed
2

3
;;; Copyright (C) 1996, 97, 98, 99 Free Software Foundation
Karl Heuer's avatar
Karl Heuer committed
4 5

;; Author: Eric M. Ludlam <zappo@gnu.org>
6
;; Version: 0.8.1
Karl Heuer's avatar
Karl Heuer committed
7
;; Keywords: file, tags, tools
8
;; X-RCS: $Id: speedbar.el,v 1.18 1998/12/19 14:01:53 zappo Exp $
Karl Heuer's avatar
Karl Heuer committed
9

Dan Nicolaescu's avatar
Dan Nicolaescu committed
10
;; This file is part of GNU Emacs.
Karl Heuer's avatar
Karl Heuer committed
11

Dan Nicolaescu's avatar
Dan Nicolaescu committed
12 13 14 15
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
Karl Heuer's avatar
Karl Heuer committed
16

Dan Nicolaescu's avatar
Dan Nicolaescu committed
17 18 19 20
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Karl Heuer's avatar
Karl Heuer committed
21

Dan Nicolaescu's avatar
Dan Nicolaescu committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:
;;
;;   The speedbar provides a frame in which files, and locations in
;; files are displayed.  These items can be clicked on with mouse-2
;; in order to make the last active frame display that file location.
;;
;; Starting Speedbar:
;;
;;   If speedbar came to you as a part of Emacs, simply type
Karl Heuer's avatar
Karl Heuer committed
36
;; `M-x speedbar', and it will be autoloaded for you.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
37 38 39 40 41 42 43
;;
;;   If speedbar is not a part of your distribution, then add
;; this to your .emacs file:
;;
;;   (autoload 'speedbar-frame-mode "speedbar" "Popup a speedbar frame" t)
;;   (autoload 'speedbar-get-focus "speedbar" "Jump to speedbar frame" t)
;;
Karl Heuer's avatar
Karl Heuer committed
44
;;   If you want to choose it from a menu, such as "Tools", you can do this:
Dan Nicolaescu's avatar
Dan Nicolaescu committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
;;
;;   Emacs:
;;   (define-key-after (lookup-key global-map [menu-bar tools])
;;      [speedbar] '("Speedbar" . speedbar-frame-mode) [calendar])
;;
;;   XEmacs:
;;   (add-menu-button '("Tools")
;;		      ["Speedbar" speedbar-frame-mode
;;		       :style toggle
;;		       :selected (and (boundp 'speedbar-frame)
;;				      (frame-live-p speedbar-frame)
;;				      (frame-visible-p speedbar-frame))]
;;		      "--")
;;
;;   If you want to access speedbar using only the keyboard, do this:
;;
;;   (global-set-key [(f4)] 'speedbar-get-focus)
;;
;;   This will let you hit f4 (or whatever key you choose) to jump
;; focus to the speedbar frame.  Pressing it again will bring you back
;; to the attached frame.  Pressing RET or e to jump to a file
;; or tag will move you back to the attached frame.  The command
;; `speedbar-get-focus' will also create a speedbar frame if it does
;; not exist.
;;
;; Customizing Speedbar:
;;
;;   Once a speedbar frame is active, it takes advantage of idle time
;; to keep its contents updated.  The contents is usually a list of
;; files in the directory of the currently active buffer.  When
;; applicable, tags in the active file can be expanded.
;;
;;   To add new supported files types into speedbar, use the function
;; `speedbar-add-supported-extension' If speedbar complains that the
;; file type is not supported, that means there is no built in
;; support from imenu, and the etags part wasn't set up correctly.  You
;; may add elements to `speedbar-supported-extension-expressions' as long
;; as it is done before speedbar is loaded.
;;
;;   To prevent speedbar from following you into certain directories
;; use the function `speedbar-add-ignored-path-regexp' too add a new
;; regular expression matching a type of path.  You may add list
;; elements to `speedbar-ignored-path-expressions' as long as it is
;; done before speedbar is loaded.
;;
;;   To add new file types to imenu, see the documentation in the
Karl Heuer's avatar
Karl Heuer committed
91
;; file imenu.el that comes with Emacs.  To add new file types which
Dan Nicolaescu's avatar
Dan Nicolaescu committed
92 93 94 95 96 97 98 99 100 101 102 103
;; etags supports, you need to modify the variable
;; `speedbar-fetch-etags-parse-list'.
;;
;;    If the updates are going too slow for you, modify the variable
;; `speedbar-update-speed' to a longer idle time before updates.
;;
;;    If you navigate directories, you will probably notice that you
;; will navigate to a directory which is eventually replaced after
;; you go back to editing a file (unless you pull up a new file.)
;; The delay time before this happens is in
;; `speedbar-navigating-speed', and defaults to 10 seconds.
;;
Karl Heuer's avatar
Karl Heuer committed
104 105 106 107 108 109 110 111 112 113
;;    To enable mouse tracking with information in the minibuffer of
;; the attached frame, use the variable `speedbar-track-mouse-flag'.
;;
;;    Tag layout can be modified through `speedbar-tag-hierarchy-method',
;; which controls how tags are layed out.  It is actually a list of
;; functions that filter the data.  The default groups large tag lists
;; into sub-lists.  A long flat list can be used instead if needed.
;; Other filters could be easily added.
;;
;;    Users of XEmacs previous to 20 may want to change the default
Dan Nicolaescu's avatar
Dan Nicolaescu committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
;; timeouts for `speedbar-update-speed' to something longer as XEmacs
;; doesn't have idle timers, the speedbar timer keeps going off
;; arbitrarily while you're typing.  It's quite pesky.
;;
;;    Users of really old emacsen without the needed timers will not
;; have speedbar updating automatically.  Use "r" to refresh the
;; display after changing directories.  Remember, do not interrupt the
;; stealthy updates or your display may not be completely refreshed.
;;
;;    AUC-TEX users: The imenu tags for AUC-TEX mode don't work very
;; well.  Use the imenu keywords from tex-mode.el for better results.
;;
;; This file requires the library package assoc (association lists)
;; and the package custom (for easy configuration of speedbar)
;;     http://www.dina.kvl.dk/~abraham/custom/
;;
Karl Heuer's avatar
Karl Heuer committed
130
;;; Developing for speedbar
Dan Nicolaescu's avatar
Dan Nicolaescu committed
131
;;
Karl Heuer's avatar
Karl Heuer committed
132
;; Adding a speedbar specialized display mode:
Dan Nicolaescu's avatar
Dan Nicolaescu committed
133
;;
Karl Heuer's avatar
Karl Heuer committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
;; Speedbar can be configured to create a special display for certain
;; modes that do not display tradition file/tag data.  Rmail, Info,
;; and the debugger are examples.  These modes can, however, benefit
;; from a speedbar style display in their own way.
;;
;; If your `major-mode' is `foo-mode', the only requirement is to
;; create a function called `foo-speedbar-buttons' which takes one
;; argument, BUFFER.  BUFFER will be the buffer speedbar wants filled.
;; In `foo-speedbar-buttons' there are several functions that make
;; building a speedbar display easy.  See the documentation for
;; `speedbar-with-writable' (needed because the buffer is usually
;; read-only) `speedbar-make-tag-line', `speedbar-insert-button', and
;; `speedbar-insert-generic-list'.  If you use
;; `speedbar-insert-generic-list', also read the doc for
;; `speedbar-tag-hierarchy-method' in case you wish to override it.
;; The function `speedbar-with-attached-buffer' brings you back to the
;; buffer speedbar is displaying for.
;;
;; For those functions that make buttons, the "function" should be a
;; symbol that is the function to call when clicked on.  The "token"
;; is extra data you can pass along.  The "function" must take three
;; parameters.  They are (TEXT TOKEN INDENT).  TEXT is the text of the
;; button clicked on.  TOKEN is the data passed in when you create the
;; button.  INDENT is an indentation level, or 0.  You can store
;; indentation levels with `speedbar-make-tag-line' which creates a
;; line with an expander (eg.  [+]) and a text button.
;;
;; Some useful functions when writing expand functions, and click
;; functions are `speedbar-change-expand-button-char',
;; `speedbar-delete-subblock', and `speedbar-center-buffer-smartly'.
;; The variable `speedbar-power-click' is set to t in your functions
;; when the user shift-clicks.  This indications anything from
;; refreshing cached data to making a buffer appear in a new frame.
;;
;; If you wish to add to the default speedbar menu for the case of
;; `foo-mode', create a variable `foo-speedbar-menu-items'.  This
;; should be a list compatible with the `easymenu' package.  It will
;; be spliced into the main menu.  (Available with click-mouse-3).  If
;; you wish to have extra key bindings in your special mode, create a
;; variable `foo-speedbar-key-map'.  Instead of using `make-keymap',
;; or `make-sparse-keymap', use the function
;; `speedbar-make-specialized-keymap'.  This lets you inherit all of
;; speedbar's default bindings with low overhead.
;;
;; Adding a speedbar top-level display mode:
;;
;; Unlike the specialized modes, there are no name requirements,
;; however the methods for writing a button display, menu, and keymap
;; are the same.  Once you create these items, you can call the
;; function `speedbar-add-expansion-list'.  It takes one parameter
;; which is a list element of the form (NAME MENU KEYMAP &rest
;; BUTTON-FUNCTIONS).  NAME is a string that will show up in the
;; Displays menu item.  MENU is a symbol containing the menu items to
;; splice in.  KEYMAP is a symbol holding the keymap to use, and
;; BUTTON-FUNCTIONS are the function names to call, in order, to create
;; the display.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
190 191 192 193 194 195 196 197 198 199

;;; TODO:
;; - More functions to create buttons and options
;; - Timeout directories we haven't visited in a while.
;; - Remeber tags when refreshing the display.  (Refresh tags too?)
;; - More 'special mode support.

(require 'assoc)
(require 'easymenu)

Karl Heuer's avatar
Karl Heuer committed
200 201 202 203 204
(defvar speedbar-xemacsp (string-match "XEmacs" emacs-version)
  "Non-nil if we are running in the XEmacs environment.")
(defvar speedbar-xemacs20p (and speedbar-xemacsp
				(= emacs-major-version 20)))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
205 206 207 208 209
;; From custom web page for compatibility between versions of custom:
(eval-and-compile
  (condition-case ()
      (require 'custom)
    (error nil))
Karl Heuer's avatar
Karl Heuer committed
210 211 212 213
  (if (and (featurep 'custom) (fboundp 'custom-declare-variable)
	   ;; Some XEmacsen w/ custom don't have :set keyword.
	   ;; This protects them against custom.
	   (fboundp 'custom-initialize-set))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      nil ;; We've got what we needed
    ;; We have the old custom-library, hack around it!
    (defmacro defgroup (&rest args)
      nil)
    (defmacro defface (var values doc &rest args)
      (` (progn
	   (defvar (, var) (quote (, var)))
	   ;; To make colors for your faces you need to set your .Xdefaults
	   ;; or set them up ahead of time in your .emacs file.
	   (make-face (, var))
	   )))
    (defmacro defcustom (var value doc &rest args)
      (` (defvar (, var) (, value) (, doc))))))

;; customization stuff
(defgroup speedbar nil
  "File and tag browser frame."
  :group 'tags
Dan Nicolaescu's avatar
Dan Nicolaescu committed
232
  :group 'tools
Dan Nicolaescu's avatar
Dan Nicolaescu committed
233
  :group 'convenience
Dan Nicolaescu's avatar
Dan Nicolaescu committed
234
  :version "20.3")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
235 236 237 238 239 240 241 242 243 244 245 246 247

(defgroup speedbar-faces nil
  "Faces used in speedbar."
  :prefix "speedbar-"
  :group 'speedbar
  :group 'faces)

(defgroup speedbar-vc nil
  "Version control display in speedbar."
  :prefix "speedbar-"
  :group 'speedbar)

;;; Code:
Karl Heuer's avatar
Karl Heuer committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
(defvar speedbar-initial-expansion-mode-alist
  '(("buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
     speedbar-buffer-buttons)
    ("quick buffers" speedbar-buffer-easymenu-definition speedbar-buffers-key-map
     speedbar-buffer-buttons-temp)
    ;; Files last, means first in the Displays menu
    ("files" speedbar-easymenu-definition-special speedbar-file-key-map
     speedbar-directory-buttons speedbar-default-directory-list)
    )
  "List of named expansion elements for filling the speedbar frame.
These expansion lists are only valid for regular files.  Special modes
still get to override this list on a mode-by-mode basis.  This list of
lists is of the form (NAME MENU KEYMAP FN1 FN2 ...).  NAME is a string
representing the types of things to be displayed.  MENU is an easymenu
structure used when in this mode.  KEYMAP is a local keymap to install
over the regular speedbar keymap.  FN1 ...  are functions that will be
called in order.  These functions will always get the default
directory to use passed in as the first parameter, and a 0 as the
second parameter.  The 0 indicates the uppermost indentation level.
They must assume that the cursor is at the position where they start
inserting buttons.")

270
(defvar speedbar-initial-expansion-list-name "files"
Karl Heuer's avatar
Karl Heuer committed
271 272
  "A symbol name representing the expansion list to use.
The expansion list `speedbar-initial-expansion-mode-alist' contains
273
the names and associated functions to use for buttons in speedbar.")
Karl Heuer's avatar
Karl Heuer committed
274 275 276 277 278

(defvar speedbar-previously-used-expansion-list-name "files"
  "Save the last expansion list method.
This is used for returning to a previous expansion list method when
the user is done with the current expansion list.")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
279 280

(defvar speedbar-stealthy-function-list
Karl Heuer's avatar
Karl Heuer committed
281 282 283
  '(("files"
     speedbar-update-current-file speedbar-check-vc speedbar-check-objects)
    )
Dan Nicolaescu's avatar
Dan Nicolaescu committed
284
  "List of functions to periodically call stealthily.
Karl Heuer's avatar
Karl Heuer committed
285 286 287 288 289
This list is of the form:
 '( (\"NAME\" FUNCTION ...)
    ...)
where NAME is the name of the major display mode these functions are
for, and the remaining elements FUNCTION are functions to call in order.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
290 291 292 293 294 295
Each function must return nil if interrupted, or t if completed.
Stealthy functions which have a single operation should always return
t.  Functions which take a long time should maintain a state (where
they are in their speedbar related calculations) and permit
interruption.  See `speedbar-check-vc' as a good example.")

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
(defvar speedbar-mode-functions-list
  '(("files" (speedbar-item-info . speedbar-files-item-info)
     (speedbar-line-path . speedbar-files-line-path))
    ("buffers" (speedbar-item-info . speedbar-buffers-item-info)
     (speedbar-line-path . speedbar-buffers-line-path))
    ("quick buffers" (speedbar-item-info . speedbar-buffers-item-info)
     (speedbar-line-path . speedbar-buffers-line-path))
    )
  "List of function tables to use for different major display modes.
It is not necessary to define any functions for a specialized mode.
This just provides a simple way of adding lots of customizations.
Each sublist is of the form:
  (\"NAME\" (FUNCTIONSYMBOL . REPLACEMENTFUNCTION) ...)
Where NAME is the name of the specialized mode.  The rest of the list
is a set of dotted pairs of the form FUNCTIONSYMBOL, which is the name
of a function you would like to replace, and REPLACEMENTFUNCTION,
which is a function you can call instead.  Not all functions can be
replaced this way.  Replaceable functions must provide that
functionality individually.")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
316 317 318 319 320 321 322 323
(defcustom speedbar-mode-specific-contents-flag t
  "*Non-nil means speedbar will show special mode contents.
This permits some modes to create customized contents for the speedbar
frame."
  :group 'speedbar
  :type 'boolean)

(defvar speedbar-special-mode-expansion-list nil
Karl Heuer's avatar
Karl Heuer committed
324 325 326 327 328
  "Default function list for creating specialized button lists.
This list is set by modes that wish to have special speedbar displays.
The list is of function names.  Each function is called with one
parameter BUFFER, the originating buffer.  The current buffer is the
speedbar buffer.")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
329

Karl Heuer's avatar
Karl Heuer committed
330 331 332 333
(defvar speedbar-special-mode-key-map nil
  "Default keymap used when identifying a specialized display mode.
This keymap is local to each buffer that wants to define special keybindings
effective when it's display is shown.")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

(defcustom speedbar-visiting-file-hook nil
  "Hooks run when speedbar visits a file in the selected frame."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-visiting-tag-hook nil
  "Hooks run when speedbar visits a tag in the selected frame."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-load-hook nil
  "Hooks run when speedbar is loaded."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-show-unknown-files nil
  "*Non-nil show files we can't expand with a ? in the expand button.
nil means don't show the file in the list."
  :group 'speedbar
  :type 'boolean)

(defcustom speedbar-update-speed
  (if speedbar-xemacsp
      (if speedbar-xemacs20p
	  2				; 1 is too obrusive in XEmacs
	5)				; when no idleness, need long delay
    1)
  "*Idle time in seconds needed before speedbar will update itself.
Updates occur to allow speedbar to display directory information
relevant to the buffer you are currently editing."
  :group 'speedbar
  :type 'integer)

Karl Heuer's avatar
Karl Heuer committed
368 369 370 371
;; When I moved to a repeating timer, I had the horrible missfortune
;; of loosing the ability for adaptive speed choice.  This update
;; speed currently causes long delays when it should have been turned off.
(defcustom speedbar-navigating-speed speedbar-update-speed
Dan Nicolaescu's avatar
Dan Nicolaescu committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  "*Idle time to wait after navigation commands in speedbar are executed.
Navigation commands included expanding/contracting nodes, and moving
between different directories."
  :group 'speedbar
  :type 'integer)

(defcustom speedbar-frame-parameters '((minibuffer . nil)
				       (width . 20)
				       (border-width . 0)
				       (menu-bar-lines . 0)
				       (unsplittable . t))
  "*Parameters to use when creating the speedbar frame in Emacs.
Parameters not listed here which will be added automatically are
`height' which will be initialized to the height of the frame speedbar
is attached to."
  :group 'speedbar
  :type '(repeat (sexp :tag "Parameter:")))

;; These values by Hrvoje Niksic <hniksic@srce.hr>
(defcustom speedbar-frame-plist
  '(minibuffer nil width 20 border-width 0
	       internal-border-width 0 unsplittable t
	       default-toolbar-visible-p nil has-modeline-p nil
395
	       menubar-visible-p nil)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  "*Parameters to use when creating the speedbar frame in XEmacs.
Parameters not listed here which will be added automatically are
`height' which will be initialized to the height of the frame speedbar
is attached to."
  :group 'speedbar
  :type '(repeat (group :inline t
			(symbol :tag "Property")
			(sexp :tag "Value"))))

(defcustom speedbar-use-imenu-flag (stringp (locate-library "imenu"))
  "*Non-nil means use imenu for file parsing.  nil to use etags.
XEmacs prior to 20.4 doesn't support imenu, therefore the default is to
use etags instead.  Etags support is not as robust as imenu support."
  :tag "User Imenu"
  :group 'speedbar
  :type 'boolean)

Karl Heuer's avatar
Karl Heuer committed
413 414 415 416 417
(defcustom speedbar-track-mouse-flag t
  "*Non-nil means to display info about the line under the mouse."
  :group 'speedbar
  :type 'boolean)

Dan Nicolaescu's avatar
Dan Nicolaescu committed
418
(defcustom speedbar-sort-tags nil
Karl Heuer's avatar
Karl Heuer committed
419
  "*If Non-nil, sort tags in the speedbar display.  *Obsolete*."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
420 421 422
  :group 'speedbar
  :type 'boolean)

Karl Heuer's avatar
Karl Heuer committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
(defcustom speedbar-tag-hierarchy-method
  '(prefix-group trim-words)
  "*List of methods which speedbar will use to organize tags into groups.
Groups are defined as expandable meta-tags.  Imenu supports such
things in some languages, such as separating variables from functions.
Available methods are:
  sort         - Sort tags.  (sometimes unnecessary)
  trim-words   - Trim all tags by a common prefix, broken @ word sections.
  prefix-group - Try to guess groups by prefix.
  simple-group - If imenu already returned some meta groups, stick all
                 tags that are not in a group into a sub-group."
  :group 'speedbar
  :type '(repeat
	  (radio
	   (const :tag "Sort the tags." sort)
	   (const :tag "Trim words to common prefix." trim-words)
	   (const :tag "Create groups from common prefixes." prefix-group)
	   (const :tag "Group loose tags into their own group." simple-group))
	  ))

443 444 445 446 447 448 449 450 451 452 453 454
(defcustom speedbar-tag-group-name-minimum-length 4
  "*The minimum length of a prefix group name before expanding.
Thus, if the `speedbar-tag-hierarchy-method' includes `prefix-group'
and one such groups common characters is less than this number of
characters, then the group name will be changed to the form of:
  worda to wordb
instead of just
  word
This way we won't get silly looking listings."
  :group 'speedbar
  :type 'integer)

Karl Heuer's avatar
Karl Heuer committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
(defcustom speedbar-tag-split-minimum-length 20
  "*Minimum length before we stop trying to create sub-lists in tags.
This is used by all tag-hierarchy methods that break large lists into
sub-lists."
  :group 'speedbar
  :type 'integer)

(defcustom speedbar-tag-regroup-maximum-length 10
  "*Maximum length of submenus that are regrouped.
If the regrouping option is used, then if two or more short subgroups
are next to each other, then they are combined until this number of
items is reached."
  :group 'speedbar
  :type 'integer)

Dan Nicolaescu's avatar
Dan Nicolaescu committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
(defcustom speedbar-activity-change-focus-flag nil
  "*Non-nil means the selected frame will change based on activity.
Thus, if a file is selected for edit, the buffer will appear in the
selected frame and the focus will change to that frame."
  :group 'speedbar
  :type 'boolean)

(defcustom speedbar-directory-button-trim-method 'span
  "*Indicates how the directory button will be displayed.
Possible values are:
 'span - span large directories over multiple lines.
 'trim - trim large directories to only show the last few.
 nil   - no trimming."
  :group 'speedbar
  :type '(radio (const :tag "Span large directories over mutiple lines."
		       span)
		(const :tag "Trim large directories to only show the last few."
		       trim)
		(const :tag "No trimming." nil)))

(defcustom speedbar-smart-directory-expand-flag t
  "*Non-nil means speedbar should use smart expansion.
Smart expansion only affects when speedbar wants to display a
directory for a file in the attached frame.  When smart expansion is
enabled, new directories which are children of a displayed directory
are expanded in the current framework.  If nil, then the current
hierarchy would be replaced with the new directory."
  :group 'speedbar
  :type 'boolean)

Karl Heuer's avatar
Karl Heuer committed
500 501 502
(defvar speedbar-hide-button-brackets-flag nil
  "*Non-nil means speedbar will hide the brackets around the + or -.")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
(defcustom speedbar-before-popup-hook nil
  "*Hooks called before popping up the speedbar frame."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-before-delete-hook nil
  "*Hooks called before deleting the speedbar frame."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-mode-hook nil
  "*Hooks called after creating a speedbar buffer."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-timer-hook nil
  "*Hooks called after running the speedbar timer function."
  :group 'speedbar
  :type 'hook)

(defcustom speedbar-verbosity-level 1
  "*Verbosity level of the speedbar.  0 means say nothing.
1 means medium level verbosity.  2 and higher are higher levels of
verbosity."
  :group 'speedbar
  :type 'integer)

Karl Heuer's avatar
Karl Heuer committed
530 531 532
(defvar speedbar-indicator-separator " "
  "String separating file text from indicator characters.")

Dan Nicolaescu's avatar
Dan Nicolaescu committed
533 534 535 536 537 538
(defcustom speedbar-vc-do-check t
  "*Non-nil check all files in speedbar to see if they have been checked out.
Any file checked out is marked with `speedbar-vc-indicator'"
  :group 'speedbar-vc
  :type 'boolean)

Karl Heuer's avatar
Karl Heuer committed
539
(defvar speedbar-vc-indicator "*"
Dan Nicolaescu's avatar
Dan Nicolaescu committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  "Text used to mark files which are currently checked out.
Currently only RCS is supported.  Other version control systems can be
added by examining the function `speedbar-this-file-in-vc' and
`speedbar-vc-check-dir-p'")

(defcustom speedbar-vc-path-enable-hook nil
  "*Return non-nil if the current path should be checked for Version Control.
Functions in this hook must accept one parameter which is the path
being checked."
  :group 'speedbar-vc
  :type 'hook)

(defcustom speedbar-vc-in-control-hook nil
  "*Return non-nil if the specified file is under Version Control.
Functions in this hook must accept two parameters.  The PATH of the
current file, and the FILENAME of the file being checked."
  :group 'speedbar-vc
  :type 'hook)

(defvar speedbar-vc-to-do-point nil
  "Local variable maintaining the current version control check position.")

Karl Heuer's avatar
Karl Heuer committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
(defcustom speedbar-obj-do-check t
  "*Non-nil check all files in speedbar to see if they have an object file.
Any file checked out is marked with `speedbar-obj-indicator', and the
marking is based on  `speedbar-obj-alist'"
  :group 'speedbar-vc
  :type 'boolean)

(defvar speedbar-obj-to-do-point nil
  "Local variable maintaining the current version control check position.")

(defvar speedbar-obj-indicator '("#" . "!")
  "Text used to mark files that have a corresponding hidden object file.
The car is for an up-to-date object.  The cdr is for an out of date object.
The expression `speedbar-obj-alist' defines who gets tagged.")

(defvar speedbar-obj-alist
  '(("\\.\\([cpC]\\|cpp\\|cc\\)$" . ".o")
    ("\\.el$" . ".elc")
    ("\\.java$" . ".class")
    ("\\.f\\(or\\|90\\|77\\)?$" . ".o")
    ("\\.tex$" . ".dvi")
    ("\\.texi$" . ".info"))
  "Alist of file extensions, and their corresponding object file type.")

(defvar speedbar-indicator-regex
  (concat (regexp-quote speedbar-indicator-separator)
	  "\\("
	  (regexp-quote speedbar-vc-indicator)
	  "\\|"
	  (regexp-quote (car speedbar-obj-indicator))
	  "\\|"
	  (regexp-quote (cdr speedbar-obj-indicator))
	  "\\)*")
  "Regular expression used when identifying files.
Permits stripping of indicator characters from a line.")

(defcustom speedbar-scanner-reset-hook nil
  "*Hook called whenever generic scanners are reset.
Set this to implement your own scanning / rescan safe functions with
state data."
  :group 'speedbar
  :type 'hook)

Dan Nicolaescu's avatar
Dan Nicolaescu committed
605 606 607 608 609
(defvar speedbar-ignored-modes nil
  "*List of major modes which speedbar will not switch directories for.")

(defun speedbar-extension-list-to-regex (extlist)
  "Takes EXTLIST, a list of extensions and transforms it into regexp.
Karl Heuer's avatar
Karl Heuer committed
610 611
All the preceding `.' are stripped for an optimized expression starting
with `.' followed by extensions, followed by full-filenames."
Dan Nicolaescu's avatar
Dan Nicolaescu committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  (let ((regex1 nil) (regex2 nil))
    (while extlist
      (if (= (string-to-char (car extlist)) ?.)
	  (setq regex1 (concat regex1 (if regex1 "\\|" "")
			       (substring (car extlist) 1)))
	(setq regex2 (concat regex2 (if regex2 "\\|" "") (car extlist))))
      (setq extlist (cdr extlist)))
    ;; concat all the sub-exressions together, making sure all types
    ;; of parts exist during concatination.
    (concat "\\("
	    (if regex1 (concat "\\(\\.\\(" regex1 "\\)\\)") "")
	    (if (and regex1 regex2) "\\|" "")
	    (if regex2 (concat "\\(" regex2 "\\)") "")
	    "\\)$")))

(defvar speedbar-ignored-path-regexp nil
  "Regular expression matching paths speedbar will not switch to.
Created from `speedbar-ignored-path-expressions' with the function
`speedbar-extension-list-to-regex' (A misnamed function in this case.)
Use the function `speedbar-add-ignored-path-regexp', or customize the
variable `speedbar-ignored-path-expressions' to modify this variable.")

(defcustom speedbar-ignored-path-expressions
  '("/logs?/\\'")
  "*List of regular expressions matching directories speedbar will ignore.
They should included paths to directories which are notoriously very
large and take a long time to load in.  Use the function
`speedbar-add-ignored-path-regexp' to add new items to this list after
speedbar is loaded.  You may place anything you like in this list
before speedbar has been loaded."
  :group 'speedbar
  :type '(repeat (regexp :tag "Path Regexp"))
  :set (lambda (sym val)
	 (setq speedbar-ignored-path-expressions val
	       speedbar-ignored-path-regexp
	       (speedbar-extension-list-to-regex val))))

Karl Heuer's avatar
Karl Heuer committed
649 650 651 652 653 654 655
(defcustom speedbar-directory-unshown-regexp "^\\(CVS\\|RCS\\|SCCS\\)\\'"
  "*Regular expression matching directories not to show in speedbar.
They should include commonly existing directories which are not
useful, such as version control."
  :group 'speedbar
  :type 'string)

Dan Nicolaescu's avatar
Dan Nicolaescu committed
656 657 658 659 660 661
(defvar speedbar-file-unshown-regexp
  (let ((nstr "") (noext completion-ignored-extensions))
    (while noext
      (setq nstr (concat nstr (regexp-quote (car noext)) "\\'"
			 (if (cdr noext) "\\|" ""))
	    noext (cdr noext)))
662 663
    ;;               backup      refdir      lockfile
    (concat nstr "\\|#[^#]+#$\\|\\.\\.?\\'\\|\\.#"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
664 665 666 667 668 669
  "*Regexp matching files we don't want displayed in a speedbar buffer.
It is generated from the variable `completion-ignored-extensions'")

;; this is dangerous to customize, because the defaults will probably
;; change in the future.
(defcustom speedbar-supported-extension-expressions
Karl Heuer's avatar
Karl Heuer committed
670 671
  (append '(".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?" ".tex\\(i\\(nfo\\)?\\)?"
	    ".el" ".emacs" ".l" ".lsp" ".p" ".java" ".f\\(90\\|77\\|or\\)?")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
672
	  (if speedbar-use-imenu-flag
Karl Heuer's avatar
Karl Heuer committed
673 674 675 676
	      '(".ada" ".pl" ".tcl" ".m" ".scm" ".pm" ".py"
		;; html is not supported by default, but an imenu tags package
		;; is available.  Also, html files are nice to be able to see.
		".s?html"
Dan Nicolaescu's avatar
Dan Nicolaescu committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		"Makefile\\(\\.in\\)?")))
  "*List of regular expressions which will match files supported by tagging.
Do not prefix the `.' char with a double \\ to quote it, as the period
will be stripped by a simplified optimizer when compiled into a
singular expression.  This variable will be turned into
`speedbar-file-regexp' for use with speedbar.  You should use the
function `speedbar-add-supported-extension' to add a new extension at
runtime, or use the configuration dialog to set it in your .emacs
file."
  :group 'speedbar
  :type '(repeat (regexp :tag "Extension Regexp"))
  :set (lambda (sym val)
	 (setq speedbar-supported-extension-expressions val
	       speedbar-file-regexp (speedbar-extension-list-to-regex val)))
  )

(defvar speedbar-file-regexp
  (speedbar-extension-list-to-regex speedbar-supported-extension-expressions)
  "Regular expression matching files we know how to expand.
Created from `speedbar-supported-extension-expression' with the
function `speedbar-extension-list-to-regex'")

(defun speedbar-add-supported-extension (extension)
  "Add EXTENSION as a new supported extension for speedbar tagging.
This should start with a `.' if it is not a complete file name, and
the dot should NOT be quoted in with \\.  Other regular expression
matchers are allowed however.  EXTENSION may be a single string or a
list of strings."
Karl Heuer's avatar
Karl Heuer committed
705
  (interactive "sExtionsion: ")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719
  (if (not (listp extension)) (setq extension (list extension)))
  (while extension
    (if (member (car extension) speedbar-supported-extension-expressions)
	nil
      (setq speedbar-supported-extension-expressions
	    (cons (car extension) speedbar-supported-extension-expressions)))
    (setq extension (cdr extension)))
  (setq speedbar-file-regexp (speedbar-extension-list-to-regex
			      speedbar-supported-extension-expressions)))

(defun speedbar-add-ignored-path-regexp (path-expression)
  "Add PATH-EXPRESSION as a new ignored path for speedbar tracking.
This function will modify `speedbar-ignored-path-regexp' and add
PATH-EXPRESSION to `speedbar-ignored-path-expressions'."
Karl Heuer's avatar
Karl Heuer committed
720
  (interactive "sPath regex: ")
Dan Nicolaescu's avatar
Dan Nicolaescu committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  (if (not (listp path-expression))
      (setq path-expression (list path-expression)))
  (while path-expression
    (if (member (car path-expression) speedbar-ignored-path-expressions)
	nil
      (setq speedbar-ignored-path-expressions
	    (cons (car path-expression) speedbar-ignored-path-expressions)))
    (setq path-expression (cdr path-expression)))
  (setq speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
				      speedbar-ignored-path-expressions)))

;; If we don't have custom, then we set it here by hand.
(if (not (fboundp 'custom-declare-variable))
    (setq speedbar-file-regexp (speedbar-extension-list-to-regex
				speedbar-supported-extension-expressions)
	  speedbar-ignored-path-regexp (speedbar-extension-list-to-regex
					speedbar-ignored-path-expressions)))

Karl Heuer's avatar
Karl Heuer committed
739 740 741 742 743
(defvar speedbar-update-flag (and
			      (or (fboundp 'run-with-idle-timer)
				  (fboundp 'start-itimer)
				  (boundp 'post-command-idle-hook))
			      window-system)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  "*Non-nil means to automatically update the display.
When this is nil then speedbar will not follow the attached frame's path.
When speedbar is active, use:

\\<speedbar-key-map> `\\[speedbar-toggle-updates]'

to toggle this value.")

(defvar speedbar-syntax-table nil
  "Syntax-table used on the speedbar.")

(if speedbar-syntax-table
    nil
  (setq speedbar-syntax-table (make-syntax-table))
  ;; turn off paren matching around here.
  (modify-syntax-entry ?\' " " speedbar-syntax-table)
  (modify-syntax-entry ?\" " " speedbar-syntax-table)
  (modify-syntax-entry ?( " " speedbar-syntax-table)
  (modify-syntax-entry ?) " " speedbar-syntax-table)
  (modify-syntax-entry ?[ " " speedbar-syntax-table)
  (modify-syntax-entry ?] " " speedbar-syntax-table))

(defvar speedbar-key-map nil
  "Keymap used in speedbar buffer.")

(if speedbar-key-map
    nil
  (setq speedbar-key-map (make-keymap))
  (suppress-keymap speedbar-key-map t)

  ;; control
  (define-key speedbar-key-map "g" 'speedbar-refresh)
  (define-key speedbar-key-map "t" 'speedbar-toggle-updates)
  (define-key speedbar-key-map "q" 'speedbar-close-frame)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
778
  (define-key speedbar-key-map "Q" 'delete-frame)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
779 780 781 782

  ;; navigation
  (define-key speedbar-key-map "n" 'speedbar-next)
  (define-key speedbar-key-map "p" 'speedbar-prev)
Karl Heuer's avatar
Karl Heuer committed
783 784 785 786
  (define-key speedbar-key-map "\M-n" 'speedbar-restricted-next)
  (define-key speedbar-key-map "\M-p" 'speedbar-restricted-prev)
  (define-key speedbar-key-map "\C-\M-n" 'speedbar-forward-list)
  (define-key speedbar-key-map "\C-\M-p" 'speedbar-backward-list)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
787 788 789
  (define-key speedbar-key-map " " 'speedbar-scroll-up)
  (define-key speedbar-key-map [delete] 'speedbar-scroll-down)

Karl Heuer's avatar
Karl Heuer committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  ;; Short cuts I happen to find useful
  (define-key speedbar-key-map "r"
    (lambda () (interactive)
      (speedbar-change-initial-expansion-list
       speedbar-previously-used-expansion-list-name)))
  (define-key speedbar-key-map "b"
    (lambda () (interactive)
      (speedbar-change-initial-expansion-list "quick buffers")))
  (define-key speedbar-key-map "f"
    (lambda () (interactive)
      (speedbar-change-initial-expansion-list "files")))

  ;; Overrides
  (substitute-key-definition 'switch-to-buffer
			     'speedbar-switch-buffer-attached-frame
			     speedbar-key-map global-map)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
806 807 808 809 810 811

  (if speedbar-xemacsp
      (progn
	;; mouse bindings so we can manipulate the items on each line
	(define-key speedbar-key-map 'button2 'speedbar-click)
	(define-key speedbar-key-map '(shift button2) 'speedbar-power-click)
Karl Heuer's avatar
Karl Heuer committed
812 813 814 815 816 817 818
	;; Info doc fix from Bob Weiner
	(if (featurep 'infodoc)
	    nil
	  (define-key speedbar-key-map 'button3 'speedbar-xemacs-popup-kludge))
	(define-key speedbar-key-map '(meta button3) 'speedbar-mouse-item-info)
	)

Dan Nicolaescu's avatar
Dan Nicolaescu committed
819 820 821 822 823 824 825 826 827 828 829 830 831
    ;; mouse bindings so we can manipulate the items on each line
    (define-key speedbar-key-map [down-mouse-1] 'speedbar-double-click)
    (define-key speedbar-key-map [mouse-2] 'speedbar-click)
    ;; This is the power click for new frames, or refreshing a cache
    (define-key speedbar-key-map [S-mouse-2] 'speedbar-power-click)
    ;; This adds a small unecessary visual effect
    ;;(define-key speedbar-key-map [down-mouse-2] 'speedbar-quick-mouse)
    (define-key speedbar-key-map [M-mouse-2] 'speedbar-mouse-item-info)

    (define-key speedbar-key-map [down-mouse-3] 'speedbar-emacs-popup-kludge)

    ;; This lets the user scroll as if we had a scrollbar... well maybe not
    (define-key speedbar-key-map [mode-line mouse-2] 'speedbar-mouse-hscroll)
Karl Heuer's avatar
Karl Heuer committed
832 833 834 835
    ;; another handy place users might click to get our menu.
    (define-key speedbar-key-map [mode-line down-mouse-1]
      'speedbar-emacs-popup-kludge)

836 837 838
    ;; We can't switch buffers with the buffer mouse menu.  Lets hack it.
    (define-key speedbar-key-map [C-down-mouse-1] 'speedbar-hack-buffer-menu)

Karl Heuer's avatar
Karl Heuer committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    ;; Lastly, we want to track the mouse.  Play here
    (define-key speedbar-key-map [mouse-movement] 'speedbar-track-mouse)
   ))

(defun speedbar-make-specialized-keymap ()
  "Create a keymap for use w/ a speedbar major or minor display mode.
This basically creates a sparse keymap, and makes it's parent be
`speedbar-key-map'."
  (let ((k (make-sparse-keymap)))
    (set-keymap-parent k speedbar-key-map)
    k))

(defvar speedbar-file-key-map nil
  "Keymap used in speedbar buffer while files are displayed.")

(if speedbar-file-key-map
    nil
  (setq speedbar-file-key-map (speedbar-make-specialized-keymap))

  ;; Basic tree features
  (define-key speedbar-file-key-map "e" 'speedbar-edit-line)
  (define-key speedbar-file-key-map "\C-m" 'speedbar-edit-line)
  (define-key speedbar-file-key-map "+" 'speedbar-expand-line)
  (define-key speedbar-file-key-map "-" 'speedbar-contract-line)

  ;; file based commands
  (define-key speedbar-file-key-map "U" 'speedbar-up-directory)
  (define-key speedbar-file-key-map "I" 'speedbar-item-info)
  (define-key speedbar-file-key-map "B" 'speedbar-item-byte-compile)
  (define-key speedbar-file-key-map "L" 'speedbar-item-load)
  (define-key speedbar-file-key-map "C" 'speedbar-item-copy)
  (define-key speedbar-file-key-map "D" 'speedbar-item-delete)
  (define-key speedbar-file-key-map "O" 'speedbar-item-object-delete)
  (define-key speedbar-file-key-map "R" 'speedbar-item-rename)
  )
Dan Nicolaescu's avatar
Dan Nicolaescu committed
874 875 876 877 878 879 880 881 882 883 884 885 886

(defvar speedbar-easymenu-definition-base
  '("Speedbar"
    ["Update" speedbar-refresh t]
    ["Auto Update" speedbar-toggle-updates
     :style toggle :selected speedbar-update-flag]
    )
  "Base part of the speedbar menu.")

(defvar speedbar-easymenu-definition-special
  '(["Edit Item On Line" speedbar-edit-line t]
    ["Show All Files" speedbar-toggle-show-all-files
     :style toggle :selected speedbar-show-unknown-files]
Karl Heuer's avatar
Karl Heuer committed
887
    ["Expand File Tags" speedbar-expand-line
Dan Nicolaescu's avatar
Dan Nicolaescu committed
888 889
     (save-excursion (beginning-of-line)
		     (looking-at "[0-9]+: *.\\+. "))]
Karl Heuer's avatar
Karl Heuer committed
890
    ["Contract File Tags" speedbar-contract-line
Dan Nicolaescu's avatar
Dan Nicolaescu committed
891 892
     (save-excursion (beginning-of-line)
		     (looking-at "[0-9]+: *.-. "))]
Karl Heuer's avatar
Karl Heuer committed
893 894
;    ["Sort Tags" speedbar-toggle-sorting
;     :style toggle :selected speedbar-sort-tags]
Dan Nicolaescu's avatar
Dan Nicolaescu committed
895
    "----"
Karl Heuer's avatar
Karl Heuer committed
896
    ["File/Tag Information" speedbar-item-info t]
Dan Nicolaescu's avatar
Dan Nicolaescu committed
897 898 899
    ["Load Lisp File" speedbar-item-load
     (save-excursion
       (beginning-of-line)
Karl Heuer's avatar
Karl Heuer committed
900
       (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
Dan Nicolaescu's avatar
Dan Nicolaescu committed
901 902 903
    ["Byte Compile File" speedbar-item-byte-compile
     (save-excursion
       (beginning-of-line)
Karl Heuer's avatar
Karl Heuer committed
904 905
       (looking-at "[0-9]+: *\\[[+-]\\] .+\\(\\.el\\)\\( \\|$\\)"))]
    ["Copy File" speedbar-item-copy
Dan Nicolaescu's avatar
Dan Nicolaescu committed
906
     (save-excursion (beginning-of-line) (looking-at "[0-9]+: *\\["))]
Karl Heuer's avatar
Karl Heuer committed
907
    ["Rename File" speedbar-item-rename
Dan Nicolaescu's avatar
Dan Nicolaescu committed
908
     (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
Karl Heuer's avatar
Karl Heuer committed
909 910 911 912 913 914
    ["Delete File" speedbar-item-delete
     (save-excursion (beginning-of-line) (looking-at "[0-9]+: *[[<]"))]
    ["Delete Object" speedbar-item-object-delete
     (save-excursion (beginning-of-line)
		     (looking-at "[0-9]+: *\\[[+-]\\] [^ \n]+ \\*?[!#]$"))]
    )
Dan Nicolaescu's avatar
Dan Nicolaescu committed
915 916 917
  "Additional menu items while in file-mode.")
 
(defvar speedbar-easymenu-definition-trailer
918
  (append
Karl Heuer's avatar
Karl Heuer committed
919
   (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
920 921 922 923
       (list ["Customize..." speedbar-customize t]))
   (list
    ["Close" speedbar-close-frame t]
    ["Quit" delete-frame t] ))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  "Menu items appearing at the end of the speedbar menu.")

(defvar speedbar-desired-buffer nil
  "Non-nil when speedbar is showing buttons specific a special mode.
In this case it is the originating buffer.")
(defvar speedbar-buffer nil
  "The buffer displaying the speedbar.")
(defvar speedbar-frame nil
  "The frame displaying speedbar.")
(defvar speedbar-cached-frame nil
  "The frame that was last created, then removed from the display.")
(defvar speedbar-full-text-cache nil
  "The last open directory is saved in its entirety for ultra-fast switching.")
(defvar speedbar-timer nil
  "The speedbar timer used for updating the buffer.")
(defvar speedbar-attached-frame nil
  "The frame which started speedbar mode.
This is the frame from which all data displayed in the speedbar is
gathered, and in which files and such are displayed.")

(defvar speedbar-last-selected-file nil
  "The last file which was selected in speedbar buffer.")

(defvar speedbar-shown-directories nil
  "Maintain list of directories simultaneously open in the current speedbar.")

(defvar speedbar-directory-contents-alist nil
  "An association list of directories and their contents.
Each sublist was returned by `speedbar-file-lists'.  This list is
maintained to speed up the refresh rate when switching between
directories.")

(defvar speedbar-power-click nil
  "Never set this by hand.  Value is t when S-mouse activity occurs.")


960 961 962 963 964 965 966 967 968 969
;;; Compatibility
;;
(if (fboundp 'frame-parameter)

    (defalias 'speedbar-frame-parameter 'frame-parameter)
  
  (defun speedbar-frame-parameter (frame parameter)
    "Return FRAME's PARAMETER value."
    (cdr (assoc parameter (frame-parameters frame)))))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
;;; Mode definitions/ user commands
;;

;;;###autoload
(defalias 'speedbar 'speedbar-frame-mode)
;;;###autoload
(defun speedbar-frame-mode (&optional arg)
  "Enable or disable speedbar.  Positive ARG means turn on, negative turn off.
nil means toggle.  Once the speedbar frame is activated, a buffer in
`speedbar-mode' will be displayed.  Currently, only one speedbar is
supported at a time.
`speedbar-before-popup-hook' is called before popping up the speedbar frame.
`speedbar-before-delete-hook' is called before the frame is deleted."
  (interactive "P")
  ;; toggle frame on and off.
  (if (not arg) (if (and (frame-live-p speedbar-frame)
			 (frame-visible-p speedbar-frame))
		    (setq arg -1) (setq arg 1)))
  ;; turn the frame off on neg number
  (if (and (numberp arg) (< arg 0))
      (progn
	(run-hooks 'speedbar-before-delete-hook)
	(if (and speedbar-frame (frame-live-p speedbar-frame))
	    (progn
	      (setq speedbar-cached-frame speedbar-frame)
	      (make-frame-invisible speedbar-frame)))
	(setq speedbar-frame nil)
	(speedbar-set-timer nil)
	;; Used to delete the buffer.  This has the annoying affect of
	;; preventing whatever took its place from ever appearing
	;; as the default after a C-x b was typed
	;;(if (bufferp speedbar-buffer)
	;;    (kill-buffer speedbar-buffer))
	)
    ;; Set this as our currently attached frame
    (setq speedbar-attached-frame (selected-frame))
    (run-hooks 'speedbar-before-popup-hook)
    ;; Get the frame to work in
    (if (frame-live-p speedbar-cached-frame)
	(progn
	  (setq speedbar-frame speedbar-cached-frame)
	  (make-frame-visible speedbar-frame)
	  ;; Get the buffer to play with
	  (speedbar-mode)
	  (select-frame speedbar-frame)
	  (if (not (eq (current-buffer) speedbar-buffer))
	      (switch-to-buffer speedbar-buffer))
	  (set-window-dedicated-p (selected-window) t)
	  (raise-frame speedbar-frame)
	  (speedbar-set-timer speedbar-update-speed)
	  )
      (if (frame-live-p speedbar-frame)
	  (raise-frame speedbar-frame)
	(setq speedbar-frame
	      (if speedbar-xemacsp
1025 1026 1027 1028 1029 1030 1031 1032 1033
		  ;; Only guess height if it is not specified.
		  (if (member 'height speedbar-frame-plist)
		      (make-frame speedbar-frame-plist)
		    (make-frame (nconc (list 'height
					     (speedbar-needed-height))
				       speedbar-frame-plist)))
		(let* ((mh (speedbar-frame-parameter nil 'menu-bar-lines))
		       (cfx (speedbar-frame-parameter nil 'left))
		       (cfy (speedbar-frame-parameter nil 'top))
1034 1035
		       (cfw (frame-pixel-width))
		       (params
1036 1037 1038 1039 1040 1041 1042
			;; Only add a guessed height if one is not specified
			;; in the input parameters.
			(if (assoc 'height speedbar-frame-parameters)
			    speedbar-frame-parameters
			  (append
			   speedbar-frame-parameters
			   (list (cons 'height (+ mh (frame-height)))))))
1043 1044 1045 1046 1047 1048 1049 1050
		       (frame
			(if (or (< emacs-major-version 20)
				(not (eq window-system 'x)))
			    (make-frame params)
			  (let ((x-pointer-shape x-pointer-top-left-arrow)
				(x-sensitive-text-pointer-shape
				 x-pointer-hand2))
			    (make-frame params)))))
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
		  ;; Position speedbar frame.
		  (if (or (not window-system) (eq window-system 'pc)
			  (assoc 'left speedbar-frame-parameters)
			  (assoc 'top speedbar-frame-parameters))
		      ;; Do no positioning if not on a windowing system,
		      ;; or if left/top were specified in the parameters.
		      frame
		    (let ((cfx
			   (if (not (consp cfx))
			       cfx
			     ;; If cfx is a list, that means we grow
			     ;; from a specific edge of the display.
			     ;; Convert that to the distance from the
			     ;; left side of the display.
			     (if (eq (car cfx) '-)
				 ;; A - means distance from the right edge
				 ;; of the display, or DW - cfx - framewidth
				 (- (x-display-pixel-width) (car (cdr cfx))
				    (frame-pixel-width))
			       (car (cdr cfx))))))
		      (modify-frame-parameters
		       frame
		       (list
			(cons
			 'left
			 ;; Decide which side to put it
			 ;; on.  200 is just a buffer
			 ;; for the left edge of the
			 ;; screen.  The extra 10 is just
			 ;; dressings for window decorations.
			 (let ((sfw (frame-pixel-width frame)))
			   (let ((left-guess (- cfx 10 sfw))
				 (right-guess (+ cfx cfw 5)))
			     (let ((left-margin left-guess)
				   (right-margin
				    (- (x-display-pixel-width)
				       right-guess 5 sfw)))
			       (cond ((>= left-margin 0) left-guess)
				     ((>= right-margin 0) right-guess)
				     ;; otherwise choose side we overlap less
				     ((> left-margin right-margin) 0)
				     (t (- (x-display-pixel-width) sfw 5)))))))
			(cons 'top cfy)))
		      frame)))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1095 1096 1097 1098 1099 1100 1101 1102 1103
	;; reset the selection variable
	(setq speedbar-last-selected-file nil)
	;; Put the buffer into the frame
	(save-window-excursion
	  ;; Get the buffer to play with
	  (speedbar-mode)
	  (select-frame speedbar-frame)
	  (switch-to-buffer speedbar-buffer)
	  (set-window-dedicated-p (selected-window) t))
1104 1105
	(if (and (or (null window-system) (eq window-system 'pc))
		 (fboundp 'set-frame-name))
1106 1107 1108
	    (progn
	      (select-frame speedbar-frame)
	      (set-frame-name "Speedbar")))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	(speedbar-set-timer speedbar-update-speed)))))

;;;###autoload
(defun speedbar-get-focus ()
  "Change frame focus to or from the speedbar frame.
If the selected frame is not speedbar, then speedbar frame is
selected.  If the speedbar frame is active, then select the attached frame."
  (interactive)
  (if (eq (selected-frame) speedbar-frame)
      (if (frame-live-p speedbar-attached-frame)
	  (select-frame speedbar-attached-frame))
Karl Heuer's avatar
Karl Heuer committed
1120 1121 1122 1123
    ;; If updates are off, then refresh the frame (they want it now...)
    (if (not speedbar-update-flag)
	(let ((speedbar-update-flag t))
	  (speedbar-timer-fn)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1124 1125 1126
    ;; make sure we have a frame
    (if (not (frame-live-p speedbar-frame)) (speedbar-frame-mode 1))
    ;; go there
Karl Heuer's avatar
Karl Heuer committed
1127 1128
    (select-frame speedbar-frame)
    )
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1129 1130 1131 1132 1133 1134 1135 1136 1137
  (other-frame 0))

(defun speedbar-close-frame ()
  "Turn off a currently active speedbar."
  (interactive)
  (speedbar-frame-mode -1)
  (select-frame speedbar-attached-frame)
  (other-frame 0))

Karl Heuer's avatar
Karl Heuer committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
(defun speedbar-switch-buffer-attached-frame (&optional buffer)
  "Switch to BUFFER in speedbar's attached frame, and raise that frame.
This overrides the default behavior of `switch-to-buffer' which is
broken because of the dedicated speedbar frame."
  (interactive)
  ;; Assume we are in the speedbar frame.
  (speedbar-get-focus)
  ;; Now switch buffers
  (if buffer
      (switch-to-buffer buffer)
    (call-interactively 'switch-to-buffer nil nil)))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
(defmacro speedbar-frame-width ()
  "Return the width of the speedbar frame in characters.
nil if it doesn't exist."
  '(frame-width speedbar-frame))

;; XEmacs function only.
(defun speedbar-needed-height (&optional frame)
  "The needed height for the tool bar FRAME (in characters)."
  (or frame (setq frame (selected-frame)))
  ;; The 1 is the missing modeline/minibuffer
  (+ 1 (/ (frame-pixel-height frame)
	  (face-height 'default frame))))

(defun speedbar-mode ()
  "Major mode for managing a display of directories and tags.
\\<speedbar-key-map>
The first line represents the default path of the speedbar frame.
Each directory segment is a button which jumps speedbar's default
directory to that path.  Buttons are activated by clicking `\\[speedbar-click]'.
In some situations using `\\[speedbar-power-click]' is a `power click' which will
rescan cached items, or pop up new frames.

Each line starting with <+> represents a directory.  Click on the <+>
to insert the directory listing into the current tree.  Click on the
<-> to retract that list.  Click on the directory name to go to that
directory as the default.

Each line starting with [+] is a file.  If the variable
`speedbar-show-unknown-files' is t, the lines starting with [?] are
files which don't have imenu support, but are not expressly ignored.
Files are completely ignored if they match `speedbar-file-unshown-regexp'
which is generated from `completion-ignored-extensions'.

Files with a `*' character after their name are files checked out of a
version control system.  (currently only RCS is supported.)  New
version control systems can be added by examining the documentation
for `speedbar-this-file-in-vc' and `speedbar-vc-check-dir-p'

Karl Heuer's avatar
Karl Heuer committed
1188 1189 1190 1191 1192
Files with a `#' or `!' character after them are source files that
have an object file associated with them.  The `!' indicates that the
files is out of date.   You can control what source/object associations
exist through the variable `speedbar-obj-alist'.

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
Click on the [+] to display a list of tags from that file.  Click on
the [-] to retract the list.  Click on the file name to edit the file
in the attached frame.

If you open tags, you might find a node starting with {+}, which is a
category of tags.  Click the {+} to expand the category.  Jump-able
tags start with >.  Click the name of the tag to go to that position
in the selected file.

\\{speedbar-key-map}"
  ;; NOT interactive
  (save-excursion
    (setq speedbar-buffer (set-buffer (get-buffer-create " SPEEDBAR")))
    (kill-all-local-variables)
    (setq major-mode 'speedbar-mode)
    (setq mode-name "Speedbar")
    (set-syntax-table speedbar-syntax-table)
    (setq font-lock-keywords nil) ;; no font-locking please
    (setq truncate-lines t)
    (make-local-variable 'frame-title-format)
    (setq frame-title-format "Speedbar")
    ;; Set this up special just for the speedbar buffer
Karl Heuer's avatar
Karl Heuer committed
1215
    ;; Terminal minibuffer stuff does not require this.
1216 1217
    (if (and window-system (not (eq window-system 'pc))
	     (null default-minibuffer-frame))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1218 1219 1220
	(progn
	  (make-local-variable 'default-minibuffer-frame)
	  (setq default-minibuffer-frame speedbar-attached-frame)))
Karl Heuer's avatar
Karl Heuer committed
1221 1222 1223 1224 1225
    ;; Correct use of `temp-buffer-show-function': Bob Weiner
    (if (and (boundp 'temp-buffer-show-hook)
	     (boundp 'temp-buffer-show-function))
	(progn (make-local-variable 'temp-buffer-show-hook)
	       (setq temp-buffer-show-hook temp-buffer-show-function)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    (make-local-variable 'temp-buffer-show-function)
    (setq temp-buffer-show-function 'speedbar-temp-buffer-show-function)
    (if speedbar-xemacsp
	(progn
	  ;; Argh!  mouse-track-click-hook doesn't understand the
	  ;; make-local-hook conventions.
	  (make-local-variable 'mouse-track-click-hook)
	  (add-hook 'mouse-track-click-hook
		    (lambda (event count)
		      (if (/= (event-button event) 1)
			  nil		; Do normal operations.
			(cond ((eq count 1)
			       (speedbar-quick-mouse event))
			      ((or (eq count 2)
				   (eq count 3))
			       (mouse-set-point event)
			       (speedbar-do-function-pointer)
			       (speedbar-quick-mouse event)))
			;; Don't do normal operations.
			t)))))
    (make-local-hook 'kill-buffer-hook)
    (add-hook 'kill-buffer-hook (lambda () (let ((skilling (boundp 'skilling)))
					     (if skilling
						 nil
					       (if (eq (current-buffer)
						       speedbar-buffer)
						   (speedbar-frame-mode -1)))))
	      t t)
1254
    (toggle-read-only 1)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1255
    (speedbar-set-mode-line-format)
Karl Heuer's avatar
Karl Heuer committed
1256 1257 1258 1259 1260 1261 1262 1263 1264
    (if speedbar-xemacsp
	(progn
	  (make-local-variable 'mouse-motion-handler)
	  (setq mouse-motion-handler 'speedbar-track-mouse-xemacs))
      (if speedbar-track-mouse-flag
	  (progn
	    (make-local-variable 'track-mouse)
	    (setq track-mouse t)))	;this could be messy.
      (setq auto-show-mode nil))	;no auto-show for Emacs
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1265 1266 1267 1268
    (run-hooks 'speedbar-mode-hook))
  (speedbar-update-contents)
  speedbar-buffer)

Karl Heuer's avatar
Karl Heuer committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
(defun speedbar-show-info-under-mouse (&optional event)
  "Call the info function for the line under the mouse.
Optional EVENT is currently not used."
  (let ((pos (mouse-position)))  ; we ignore event until I use it later.
    (if (equal (car pos) speedbar-frame)
	(save-excursion
	  (save-window-excursion
	    (apply 'set-mouse-position pos)
	    (speedbar-item-info))))))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
(defun speedbar-set-mode-line-format ()
  "Set the format of the mode line based on the current speedbar environment.
This gives visual indications of what is up.  It EXPECTS the speedbar
frame and window to be the currently active frame and window."
  (if (and (frame-live-p speedbar-frame)
	   (or (not speedbar-xemacsp)
	       (specifier-instance has-modeline-p)))
      (save-excursion
	(set-buffer speedbar-buffer)
	(let* ((w (or (speedbar-frame-width) 20))
	       (p1 "<<")
	       (p5 ">>")
	       (p3 (if speedbar-update-flag "SPEEDBAR" "SLOWBAR"))
	       (blank (- w (length p1) (length p3) (length p5)
			 (if line-number-mode 4 0)))
	       (p2 (if (> blank 0)
		       (make-string (/ blank 2) ? )
		     ""))
	       (p4 (if (> blank 0)
		       (make-string (+ (/ blank 2) (% blank 2)) ? )
		     ""))
	       (tf
		(if line-number-mode
		    (list (concat p1 p2 p3) '(line-number-mode " %3l")
			  (concat p4 p5))
		  (list (concat p1 p2 p3 p4 p5)))))
	  (if (not (equal mode-line-format tf))
	      (progn
		(setq mode-line-format tf)
		(force-mode-line-update)))))))

(defun speedbar-temp-buffer-show-function (buffer)
  "Placed in the variable `temp-buffer-show-function' in `speedbar-mode'.
If a user requests help using \\[help-command] <Key> the temp BUFFER will be
redirected into a window on the attached frame."
  (if speedbar-attached-frame (select-frame speedbar-attached-frame))
  (pop-to-buffer buffer nil)
  (other-window -1)
1317 1318 1319 1320
  ;; Fix for using this hook on some platforms: Bob Weiner
  (cond ((not speedbar-xemacsp)
	 (run-hooks 'temp-buffer-show-hook))
	((fboundp 'run-hook-with-args)
Karl Heuer's avatar
Karl Heuer committed
1321 1322 1323 1324 1325 1326
	 (run-hook-with-args 'temp-buffer-show-hook buffer))
	((and (boundp 'temp-buffer-show-hook)
	      (listp temp-buffer-show-hook))
	 (mapcar (function (lambda (hook) (funcall hook buffer)))
		 temp-buffer-show-hook))))

1327 1328 1329
(defvar speedbar-previous-menu nil
  "The menu before the last `speedbar-reconfigure-keymaps' was called.")

Karl Heuer's avatar
Karl Heuer committed
1330
(defun speedbar-reconfigure-keymaps ()
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1331 1332 1333
  "Reconfigure the menu-bar in a speedbar frame.
Different menu items are displayed depending on the current display mode
and the existence of packages."
Karl Heuer's avatar
Karl Heuer committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
  (let ((md (append
	     speedbar-easymenu-definition-base
	     (if speedbar-shown-directories
		 ;; file display mode version
		 (speedbar-initial-menu)
	       (save-excursion
		 (select-frame speedbar-attached-frame)
		 (if (local-variable-p
		      'speedbar-easymenu-definition-special
		      (current-buffer))
		     ;; If bound locally, we can use it
		     speedbar-easymenu-definition-special)))
	     ;; Dynamic menu stuff
	     '("-")
	    (list (cons "Displays"
			(let ((displays nil)
			      (alist speedbar-initial-expansion-mode-alist))
			  (while alist
			    (setq displays
				  (cons
				   (vector
				    (capitalize (car (car alist)))
				    (list
				     'speedbar-change-initial-expansion-list
				     (car (car alist)))
				    t)
				   displays))
			    (setq alist (cdr alist)))
			  displays)))
	    ;; The trailer
	    speedbar-easymenu-definition-trailer))
	(localmap (save-excursion
		    (let ((cf (selected-frame)))
		      (prog2
			  (select-frame speedbar-attached-frame)
			  (if (local-variable-p
			       'speedbar-special-mode-key-map
			       (current-buffer))
			      speedbar-special-mode-key-map)
			(select-frame cf))))))
    (save-excursion
      (set-buffer speedbar-buffer)
      (use-local-map (or localmap
			 (speedbar-initial-keymap)
			 ;; This creates a small keymap we can glom the
			 ;; menu adjustments into.
			 (speedbar-make-specialized-keymap)))
1381 1382 1383 1384
      ;; Delete the old menu if applicable.
      (if speedbar-previous-menu (easy-menu-remove speedbar-previous-menu))
      (setq speedbar-previous-menu md)
      ;; Now add the new menu
Karl Heuer's avatar
Karl Heuer committed
1385 1386 1387
      (if (not speedbar-xemacsp)
	  (easy-menu-define speedbar-menu-map (current-local-map)
			    "Speedbar menu" md)
1388
	(easy-menu-add md (current-local-map))
Karl Heuer's avatar
Karl Heuer committed
1389
	(set-buffer-menubar (list md))))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424


;;; User Input stuff
;;

;; XEmacs: this can be implemented using modeline keymaps, but there
;; is no use, as we have horizontal scrollbar (as the docstring
;; hints.)
(defun speedbar-mouse-hscroll (e)
  "Read a mouse event E from the mode line, and horizontally scroll.
If the mouse is being clicked on the far left, or far right of the
mode-line.  This is only useful for non-XEmacs"
  (interactive "e")
  (let* ((xp (car (nth 2 (car (cdr e)))))
	 (cpw (/ (frame-pixel-width)
		 (frame-width)))
	 (oc (1+ (/ xp cpw)))
	 )
    (cond ((< oc 3)
	   (scroll-left 2))
	  ((> oc (- (window-width) 3))
	   (scroll-right 2))
	  (t (message "Click on the edge of the modeline to scroll left/right")))
    ;;(message "X: Pixel %d Char Pixels %d On char %d" xp cpw oc)
    ))

(defun speedbar-customize ()
  "Customize speedbar using the Custom package."
  (interactive)
  (let ((sf (selected-frame)))
    (select-frame speedbar-attached-frame)
    (customize-group 'speedbar)
    (select-frame sf))
  (speedbar-maybee-jump-to-attached-frame))

Karl Heuer's avatar
Karl Heuer committed
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
(defun speedbar-track-mouse (event)
  "For motion EVENT, display info about the current line."
  (interactive "e")
  (if (not speedbar-track-mouse-flag)
      nil
    (save-excursion
      (let ((char (nth 1 (car (cdr event)))))
	(if (not (numberp char))
	    (message nil)
	  (goto-char char)
	  ;; (message "%S" event)
	  (speedbar-item-info)
	  )))))

(defun speedbar-track-mouse-xemacs (event)
  "For motion EVENT, display info about the current line."
  (if (functionp (default-value 'mouse-motion-handler))
      (funcall (default-value 'mouse-motion-handler) event))
  (if speedbar-track-mouse-flag
      (save-excursion
	(save-window-excursion
	  (condition-case ()
	      (progn (mouse-set-point event)
		     ;; Prevent focus-related bugs.
		     (if (eq major-mode 'speedbar-mode)
			 (speedbar-item-info)))
	    (error nil))))))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1453 1454 1455 1456 1457 1458 1459 1460
;; In XEmacs, we make popup menus work on the item over mouse (as
;; opposed to where the point happens to be.)  We attain this by
;; temporarily moving the point to that place.
;;    Hrvoje Niksic <hniksic@srce.hr>
(defun speedbar-xemacs-popup-kludge (event)
  "Pop up a menu related to the clicked on item.
Must be bound to EVENT."
  (interactive "e")
Karl Heuer's avatar
Karl Heuer committed
1461
  (select-frame speedbar-frame)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  (save-excursion
    (goto-char (event-closest-point event))
    (beginning-of-line)
    (forward-char (min 5 (- (save-excursion (end-of-line) (point))
			    (save-excursion (beginning-of-line) (point)))))
    (popup-mode-menu)
    ;; Wait for menu to bail out.  `popup-mode-menu' (and other popup
    ;; menu functions) return immediately.
    (let (new)
      (while (not (misc-user-event-p (setq new (next-event))))
	(dispatch-event new))
      (dispatch-event new))))

(defun speedbar-emacs-popup-kludge (e)
  "Pop up a menu related to the clicked on item.
Must be bound to event E."
  (interactive "e")
  (save-excursion
    (mouse-set-point e)
    ;; This gets the cursor where the user can see it.
    (if (not (bolp)) (forward-char -1))
    (sit-for 0)
    (if (< emacs-major-version 20)
	(mouse-major-mode-menu e)
      (mouse-major-mode-menu e nil))))

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
(defun speedbar-hack-buffer-menu (e)
  "Control mouse 1 is buffer menu.
This hack overrides it so that the right thing happens in the main
Emacs frame, not in the speedbar frame.
Argument E is the event causing this activity."
  (interactive "e")
  (let ((fn (lookup-key global-map (if speedbar-xemacsp
				              '(control button1)
				     [C-down-mouse-1])))
	(newbuff nil))
    (unwind-protect
	(save-excursion
	  (set-window-dedicated-p (selected-window) nil)
	  (call-interactively fn)
	  (setq newbuff (current-buffer)))
      (switch-to-buffer " SPEEDBAR")
      (set-window-dedicated-p (selected-window) t))
    (speedbar-with-attached-buffer
     (switch-to-buffer newbuff))))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
(defun speedbar-next (arg)
  "Move to the next ARGth line in a speedbar buffer."
  (interactive "p")
  (forward-line (or arg 1))
  (speedbar-item-info)
  (speedbar-position-cursor-on-line))

(defun speedbar-prev (arg)
  "Move to the previous ARGth line in a speedbar buffer."
  (interactive "p")
  (speedbar-next (if arg (- arg) -1)))

Karl Heuer's avatar
Karl Heuer committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
(defun speedbar-restricted-move (arg)
  "Move to the next ARGth line in a speedbar buffer at the same depth.
This means that movement is restricted to a subnode, and that siblings
of intermediate nodes are skipped."
  (if (not (numberp arg)) (signal 'wrong-type-argument (list arg 'numberp)))
  ;; First find the extent for which we are allowed to move.
  (let ((depth (save-excursion (beginning-of-line)
			       (if (looking-at "[0-9]+:")
				   (string-to-int (match-string 0))
				 0)))
	(crement (if (< arg 0) 1 -1)) ; decrement or increment
	(lastmatch (point)))
    (while (/= arg 0)
      (forward-line (- crement))
      (let ((subdepth (save-excursion (beginning-of-line)
			       (if (looking-at "[0-9]+:")
				   (string-to-int (match-string 0))
				 0))))
	(cond ((or (< subdepth depth)
		   (progn (end-of-line) (eobp))
		   (progn (beginning-of-line) (bobp)))
	       ;; We have reached the end of this block.
	       (goto-char lastmatch)
	       (setq arg 0)
	       (error "End of sub-list"))
	      ((= subdepth depth)
	       (setq lastmatch (point)
		     arg (+ arg crement))))))
    (speedbar-position-cursor-on-line)))

(defun speedbar-restricted-next (arg)
  "Move to the next ARGth line in a speedbar buffer at the same depth.
This means that movement is restricted to a subnode, and that siblings
of intermediate nodes are skipped."
  (interactive "p")
  (speedbar-restricted-move (or arg 1))
  (speedbar-item-info))


(defun speedbar-restricted-prev (arg)
  "Move to the previous ARGth line in a speedbar buffer at the same depth.
This means that movement is restricted to a subnode, and that siblings
of intermediate nodes are skipped."
  (interactive "p")
  (speedbar-restricted-move (if arg (- arg) -1))
  (speedbar-item-info))

(defun speedbar-navigate-list (arg)
  "Move across ARG groups of similarly typed items in speedbar.
Stop on the first line of the next type of item, or on the last or first item
if we reach a buffer boundary."
  (interactive "p")
  (beginning-of-line)
  (if (looking-at "[0-9]+: *[[<{][-+?][]>}] ")
      (let ((str (regexp-quote (match-string 0))))
	(while (looking-at str)
	  (speedbar-restricted-move arg)
	  (beginning-of-line))))
  (speedbar-position-cursor-on-line))

(defun speedbar-forward-list ()
  "Move forward over the current list.
A LIST in speedbar is a group of similarly typed items, such as directories,
files, or the directory button."
  (interactive)
  (speedbar-navigate-list 1)
  (speedbar-item-info))

(defun speedbar-backward-list ()
  "Move backward over the current list.
A LIST in speedbar is a group of similarly typed items, such as directories,
files, or the directory button."
  (interactive)
  (speedbar-navigate-list -1)
  (speedbar-item-info))

Dan Nicolaescu's avatar
Dan Nicolaescu committed
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
(defun speedbar-scroll-up (&optional arg)
  "Page down one screen-full of the speedbar, or ARG lines."
  (interactive "P")
  (scroll-up arg)
  (speedbar-position-cursor-on-line))

(defun speedbar-scroll-down (&optional arg)
  "Page up one screen-full of the speedbar, or ARG lines."
  (interactive "P")
  (scroll-down arg)
  (speedbar-position-cursor-on-line))

(defun speedbar-up-directory ()
  "Keyboard accelerator for moving the default directory up one.
Assumes that the current buffer is the speedbar buffer"
  (interactive)
  (setq default-directory (expand-file-name (concat default-directory "../")))
  (speedbar-update-contents))

;;; Speedbar file activity (aka creeping featurism)
;;
(defun speedbar-refresh ()
  "Refresh the current speedbar display, disposing of any cached data."
  (interactive)
1620 1621
  (let ((dl speedbar-shown-directories)
	(dm (and (boundp 'deactivate-mark) deactivate-mark)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1622 1623
    (while dl
      (adelete 'speedbar-directory-contents-alist (car dl))
1624
      (setq dl (cdr dl)))
1625 1626
    (if (<= 1 speedbar-verbosity-level)
	(message "Refreshing speedbar..."))
1627 1628 1629 1630 1631
    (speedbar-update-contents</