(texinfo-section-types-regexp): Define here.
[bpt/emacs.git] / lisp / scroll-bar.el
CommitLineData
aae56ea7 1;;; scroll-bar.el --- window system-independent scroll bar support.
6d62a90e
JB
2
3;;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5;; Maintainer: FSF
6;; Keywords: hardware
7
8;;; This file is part of GNU Emacs.
9
10;;; GNU Emacs is free software; you can redistribute it and/or modify
11;;; it under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 2, or (at your option)
13;;; any later version.
14
15;;; GNU Emacs is distributed in the hope that it will be useful,
16;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Emacs; see the file COPYING. If not, write to
22;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
76550a57
ER
24;;; Code:
25
d9ecc911
ER
26;;; Commentary:
27
28;; Window-system-independent bindings of mouse clicks on the scroll bar.
29;; Presently emulates the scroll-bar behavior of xterm.
30;;; Code:
31
dbc4e1c1
JB
32(require 'mouse)
33
6d62a90e
JB
34\f
35;;;; Utilities.
36
bf3c8a70 37(defun scroll-bar-scale (num-denom whole)
6d62a90e 38 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
bf3c8a70
JB
39This is handy for scaling a position on a scroll bar into real units,
40like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
41from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
6d62a90e 42\(buffer-size)) is the position in the current buffer corresponding to
bf3c8a70 43that scroll bar position."
6d62a90e
JB
44 ;; We multiply before we divide to maintain precision.
45 ;; We use floating point because the product of a large buffer size
bf3c8a70 46 ;; with a large scroll bar portion can easily overflow a lisp int.
6d62a90e
JB
47 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
48
49\f
fe48f821
JB
50;;;; Helpful functions for enabling and disabling scroll bars.
51(defvar scroll-bar-mode nil)
52
53(defun scroll-bar-mode (flag)
54 "Toggle display of vertical scroll bars on each frame.
55This command applies to all frames that exist and frames to be
56created in the future.
57With a numeric argument, if the argument is negative,
58turn off scroll bars; otherwise, turn on scroll bars."
59 (interactive "P")
60 (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
61 (or (not (numberp flag)) (>= flag 0))))
62 (mapcar
63 (function
64 (lambda (param-name)
65 (let ((parameter (assq param-name default-frame-alist)))
66 (if (consp parameter)
67 (setcdr parameter scroll-bar-mode)
68 (setq default-frame-alist
69 (cons (cons param-name scroll-bar-mode)
70 default-frame-alist))))))
bf3c8a70 71 '(vertical-scroll-bars horizontal-scroll-bars))
fe48f821
JB
72 (let ((frames (frame-list)))
73 (while frames
74 (modify-frame-parameters
75 (car frames)
bf3c8a70
JB
76 (list (cons 'vertical-scroll-bars scroll-bar-mode)
77 (cons 'horizontal-scroll-bars scroll-bar-mode)))
fe48f821
JB
78 (setq frames (cdr frames)))))
79\f
bf3c8a70 80;;;; Buffer navigation using the scroll bar.
6d62a90e 81
e532b016 82;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
83(defun scroll-bar-set-window-start (event)
84 "Set the window start according to where the scroll bar is dragged.
85EVENT should be a scroll bar click or drag event."
6d62a90e 86 (interactive "e")
dbc4e1c1 87 (let* ((end-position (event-end event))
6d62a90e
JB
88 (window (nth 0 end-position))
89 (portion-whole (nth 2 end-position)))
90 (save-excursion
91 (set-buffer (window-buffer window))
92 (save-excursion
bf3c8a70 93 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
6d62a90e
JB
94 (beginning-of-line)
95 (set-window-start window (point))))))
96
e532b016
RS
97;; Scroll the window to the proper position for EVENT.
98(defun scroll-bar-drag-1 (event)
99 (let* ((start-position (event-start event))
100 (window (nth 0 start-position))
101 (portion-whole (nth 2 start-position)))
102 (save-excursion
103 (set-buffer (window-buffer window))
104 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
105 (beginning-of-line)
106 (set-window-start window (point)))))
107
108(defun scroll-bar-drag (event)
109 "Scroll the window by dragging the scroll bar slider.
110If you click outside the slider, the window scrolls to bring the slider there."
111 (interactive "e")
112 (let* (done)
113 (scroll-bar-drag-1 event)
114 (track-mouse
115 (while (not done)
116 (setq event (read-event))
117 (if (eq (car-safe event) 'mouse-movement)
118 (setq event (read-event)))
119 (cond ((eq (car-safe event) 'scroll-bar-movement)
120 (scroll-bar-drag-1 event))
121 (t
122 ;; Exit when we get the drag event; ignore that event.
123 (setq done t)))))))
124
bf3c8a70
JB
125(defun scroll-bar-scroll-down (event)
126 "Scroll the window's top line down to the location of the scroll bar click.
127EVENT should be a scroll bar click."
6d62a90e
JB
128 (interactive "e")
129 (let ((old-selected-window (selected-window)))
130 (unwind-protect
131 (progn
dbc4e1c1 132 (let* ((end-position (event-end event))
6d62a90e
JB
133 (window (nth 0 end-position))
134 (portion-whole (nth 2 end-position)))
135 (select-window window)
136 (scroll-down
bf3c8a70 137 (scroll-bar-scale portion-whole (1- (window-height))))))
6d62a90e
JB
138 (select-window old-selected-window))))
139
bf3c8a70
JB
140(defun scroll-bar-scroll-up (event)
141 "Scroll the line next to the scroll bar click to the top of the window.
142EVENT should be a scroll bar click."
6d62a90e
JB
143 (interactive "e")
144 (let ((old-selected-window (selected-window)))
145 (unwind-protect
146 (progn
dbc4e1c1 147 (let* ((end-position (event-end event))
6d62a90e
JB
148 (window (nth 0 end-position))
149 (portion-whole (nth 2 end-position)))
150 (select-window window)
151 (scroll-up
bf3c8a70 152 (scroll-bar-scale portion-whole (1- (window-height))))))
6d62a90e
JB
153 (select-window old-selected-window))))
154
155\f
156;;;; Bindings.
157
158;;; For now, we'll set things up to work like xterm.
bf3c8a70
JB
159(global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
160(global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
6d62a90e 161
e532b016
RS
162(global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
163
bf3c8a70
JB
164(global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
165(global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
6d62a90e
JB
166
167\f
dc14eed2 168(provide 'scroll-bar)
6d62a90e 169
bf3c8a70 170;;; scroll-bar.el ends here