(set-scroll-bar-mode): Take just one arg.
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support.
2
3 ;; Copyright (C) 1993, 1994, 1995 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
28 ;; Presently emulates the scroll-bar behavior of xterm.
29
30 ;;; Code:
31
32 (require 'mouse)
33
34 \f
35 ;;;; Utilities.
36
37 (defun scroll-bar-event-ratio (event)
38 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
39 The value is a cons cell (PORTION . WHOLE) containing two integers
40 whose ratio gives the event's vertical position in the scroll bar, with 0
41 referring to the top and 1 to the bottom."
42 (nth 2 event))
43
44 (defun scroll-bar-scale (num-denom whole)
45 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
46 This is handy for scaling a position on a scroll bar into real units,
47 like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
48 from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
49 \(buffer-size)) is the position in the current buffer corresponding to
50 that scroll bar position."
51 ;; We multiply before we divide to maintain precision.
52 ;; We use floating point because the product of a large buffer size
53 ;; with a large scroll bar portion can easily overflow a lisp int.
54 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
55
56 \f
57 ;;;; Helpful functions for enabling and disabling scroll bars.
58
59 (defvar scroll-bar-mode)
60
61 (defvar scroll-bar-mode-explicit nil
62 "Non-nil means `set-scroll-bar-mode' should really do something.
63 This is nil while loading `scroll-bar.el', and t afterward.")
64
65 (defun set-scroll-bar-mode-1 (ignore value)
66 (set-scroll-bar-mode value))
67
68 (defun set-scroll-bar-mode (value)
69 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
70 (setq scroll-bar-mode value)
71
72 (when scroll-bar-mode-explicit
73 ;; Apply it to default-frame-alist.
74 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
75 (if (consp parameter)
76 (setcdr parameter scroll-bar-mode)
77 (setq default-frame-alist
78 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
79 default-frame-alist))))
80
81 ;; Apply it to existing frames.
82 (let ((frames (frame-list)))
83 (while frames
84 (modify-frame-parameters
85 (car frames)
86 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
87 (setq frames (cdr frames))))))
88
89 (defcustom scroll-bar-mode 'left
90 "*Specify whether to have vertical scroll bars, and on which side.
91 Possible values are nil (no scroll bars), `left' (scroll bars on left)
92 and `right' (scroll bars on right).
93 When you set the variable in a Lisp program, it takes effect for new frames,
94 and for existing frames when `toggle-scroll-bar' is used.
95 When you set this with the customization buffer,
96 it takes effect immediately for all frames."
97 :type '(choice (const :tag "none (nil)")
98 (const left)
99 (const right))
100 :group 'frames
101 :set 'set-scroll-bar-mode-1)
102
103 ;; We just set scroll-bar-mode, but that was the default.
104 ;; If it is set again, that is for real.
105 (setq scroll-bar-mode-explicit t)
106
107 (defun scroll-bar-mode (flag)
108 "Toggle display of vertical scroll bars on all frames.
109 This command applies to all frames that exist and frames to be
110 created in the future.
111 With a numeric argument, if the argument is negative,
112 turn off scroll bars; otherwise, turn on scroll bars."
113 (interactive "P")
114 (if flag (setq flag (prefix-numeric-value flag)))
115
116 ;; Tweedle the variable according to the argument.
117 (set-scroll-bar-mode (if (null flag) (not scroll-bar-mode)
118 (and (or (not (numberp flag)) (>= flag 0))
119 'left))))
120
121 (defun toggle-scroll-bar (arg)
122 "Toggle whether or not the selected frame has vertical scroll bars.
123 With arg, turn vertical scroll bars on if and only if arg is positive.
124 The variable `scroll-bar-mode' controls which side the scroll bars are on
125 when they are turned on; if it is nil, they go on the left."
126 (interactive "P")
127 (if (null arg)
128 (setq arg
129 (if (cdr (assq 'vertical-scroll-bars
130 (frame-parameters (selected-frame))))
131 -1 1))
132 (setq arg (prefix-numeric-value arg)))
133 (modify-frame-parameters (selected-frame)
134 (list (cons 'vertical-scroll-bars
135 (if (> arg 0)
136 (or scroll-bar-mode 'left))))))
137
138 (defun toggle-horizontal-scroll-bar (arg)
139 "Toggle whether or not the selected frame has horizontal scroll bars.
140 With arg, turn horizontal scroll bars on if and only if arg is positive.
141 Horizontal scroll bars aren't implemented yet."
142 (interactive "P")
143 (error "Horizontal scroll bars aren't implemented yet"))
144 \f
145 ;;;; Buffer navigation using the scroll bar.
146
147 ;;; This was used for up-events on button 2, but no longer.
148 (defun scroll-bar-set-window-start (event)
149 "Set the window start according to where the scroll bar is dragged.
150 EVENT should be a scroll bar click or drag event."
151 (interactive "e")
152 (let* ((end-position (event-end event))
153 (window (nth 0 end-position))
154 (portion-whole (nth 2 end-position)))
155 (save-excursion
156 (set-buffer (window-buffer window))
157 (save-excursion
158 (goto-char (+ (point-min)
159 (scroll-bar-scale portion-whole
160 (- (point-max) (point-min)))))
161 (beginning-of-line)
162 (set-window-start window (point))))))
163
164 (defun scroll-bar-drag-position (portion-whole)
165 "Calculate new window start for drag event."
166 (save-excursion
167 (goto-char (+ (point-min)
168 (scroll-bar-scale portion-whole
169 (- (point-max) (point-min)))))
170 (beginning-of-line)
171 (point)))
172
173 (defun scroll-bar-maybe-set-window-start (event)
174 "Set the window start according to where the scroll bar is dragged.
175 Only change window start if the new start is substantially different.
176 EVENT should be a scroll bar click or drag event."
177 (interactive "e")
178 (let* ((end-position (event-end event))
179 (window (nth 0 end-position))
180 (portion-whole (nth 2 end-position))
181 (next-portion-whole (cons (1+ (car portion-whole))
182 (cdr portion-whole)))
183 portion-start
184 next-portion-start
185 (current-start (window-start window)))
186 (save-excursion
187 (set-buffer (window-buffer window))
188 (setq portion-start (scroll-bar-drag-position portion-whole))
189 (setq next-portion-start (max
190 (scroll-bar-drag-position next-portion-whole)
191 (1+ portion-start)))
192 (if (or (>= current-start next-portion-start)
193 (< current-start portion-start))
194 (set-window-start window portion-start)
195 ;; Always set window start, to ensure scroll bar position is updated.
196 (set-window-start window current-start)))))
197
198 ;; Scroll the window to the proper position for EVENT.
199 (defun scroll-bar-drag-1 (event)
200 (let* ((start-position (event-start event))
201 (window (nth 0 start-position))
202 (portion-whole (nth 2 start-position)))
203 (save-excursion
204 (set-buffer (window-buffer window))
205 ;; Calculate position relative to the accessible part of the buffer.
206 (goto-char (+ (point-min)
207 (scroll-bar-scale portion-whole
208 (- (point-max) (point-min)))))
209 (beginning-of-line)
210 (set-window-start window (point)))))
211
212 (defun scroll-bar-drag (event)
213 "Scroll the window by dragging the scroll bar slider.
214 If you click outside the slider, the window scrolls to bring the slider there."
215 (interactive "e")
216 (let* (done
217 (echo-keystrokes 0))
218 (or point-before-scroll
219 (setq point-before-scroll (point)))
220 ;; Our scrolling can move point; don't let that clear point-before-scroll.
221 (let (point-before-scroll)
222 (scroll-bar-drag-1 event)
223 (track-mouse
224 (while (not done)
225 (setq event (read-event))
226 (if (eq (car-safe event) 'mouse-movement)
227 (setq event (read-event)))
228 (cond ((eq (car-safe event) 'scroll-bar-movement)
229 (scroll-bar-drag-1 event))
230 (t
231 ;; Exit when we get the drag event; ignore that event.
232 (setq done t)))))
233 (sit-for 0))))
234
235 (defun scroll-bar-scroll-down (event)
236 "Scroll the window's top line down to the location of the scroll bar click.
237 EVENT should be a scroll bar click."
238 (interactive "e")
239 (let ((old-selected-window (selected-window)))
240 (unwind-protect
241 (progn
242 (let* ((end-position (event-end event))
243 (window (nth 0 end-position))
244 (portion-whole (nth 2 end-position)))
245 (let (point-before-scroll)
246 (select-window window))
247 (or point-before-scroll
248 (setq point-before-scroll (point)))
249 (let (point-before-scroll)
250 (scroll-down
251 (scroll-bar-scale portion-whole (1- (window-height)))))))
252 (select-window old-selected-window))))
253
254 (defun scroll-bar-scroll-up (event)
255 "Scroll the line next to the scroll bar click to the top of the window.
256 EVENT should be a scroll bar click."
257 (interactive "e")
258 (let ((old-selected-window (selected-window)))
259 (unwind-protect
260 (progn
261 (let* ((end-position (event-end event))
262 (window (nth 0 end-position))
263 (portion-whole (nth 2 end-position)))
264 (let (point-before-scroll)
265 (select-window window))
266 (or point-before-scroll
267 (setq point-before-scroll (point)))
268 (let (point-before-scroll)
269 (scroll-up
270 (scroll-bar-scale portion-whole (1- (window-height)))))))
271 (select-window old-selected-window))))
272
273 \f
274 ;;;; Bindings.
275
276 ;;; For now, we'll set things up to work like xterm.
277 (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
278 (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
279
280 (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
281
282 (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
283 (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
284
285 \f
286 (provide 'scroll-bar)
287
288 ;;; scroll-bar.el ends here