Bug fix for vc-dispatcher split.
[bpt/emacs.git] / lisp / scroll-bar.el
CommitLineData
55535639 1;;; scroll-bar.el --- window system-independent scroll bar support
6d62a90e 2
0d30b337 3;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
409cc4a3 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6d62a90e
JB
5
6;; Maintainer: FSF
7;; Keywords: hardware
8
b578f267 9;; This file is part of GNU Emacs.
6d62a90e 10
b578f267
EN
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
b578f267 14;; any later version.
6d62a90e 15
b578f267
EN
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
6d62a90e 20
b578f267
EN
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
76550a57 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.
b578f267 30
d9ecc911
ER
31;;; Code:
32
dbc4e1c1
JB
33(require 'mouse)
34
6d62a90e
JB
35\f
36;;;; Utilities.
37
86828678
RS
38(defun scroll-bar-event-ratio (event)
39 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
40The value is a cons cell (PORTION . WHOLE) containing two integers
41whose ratio gives the event's vertical position in the scroll bar, with 0
42referring to the top and 1 to the bottom."
43 (nth 2 event))
44
bf3c8a70 45(defun scroll-bar-scale (num-denom whole)
6d62a90e 46 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
bf3c8a70
JB
47This is handy for scaling a position on a scroll bar into real units,
48like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
49from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
6d62a90e 50\(buffer-size)) is the position in the current buffer corresponding to
bf3c8a70 51that scroll bar position."
6d62a90e
JB
52 ;; We multiply before we divide to maintain precision.
53 ;; We use floating point because the product of a large buffer size
bf3c8a70 54 ;; with a large scroll bar portion can easily overflow a lisp int.
6d62a90e
JB
55 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
56
9fd76d04
MY
57(defun scroll-bar-columns (side)
58 "Return the width, measured in columns, of the vertical scrollbar on SIDE.
59SIDE must be the symbol `left' or `right'."
60 (let* ((wsb (window-scroll-bars))
61 (vtype (nth 2 wsb))
62 (cols (nth 1 wsb)))
63 (cond
64 ((not (memq side '(left right)))
65 (error "`left' or `right' expected instead of %S" side))
66 ((and (eq vtype side) cols))
67 ((eq (frame-parameter nil 'vertical-scroll-bars) side)
68 ;; nil means it's a non-toolkit scroll bar, and its width in
69 ;; columns is 14 pixels rounded up.
70 (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
71 (frame-char-width)))
72 (0))))
73
6d62a90e 74\f
fe48f821 75;;;; Helpful functions for enabling and disabling scroll bars.
fe48f821 76
1064fe38 77(defvar scroll-bar-mode)
f62c3ee2 78(defvar previous-scroll-bar-mode nil)
1064fe38 79
2124851f
RS
80(defvar scroll-bar-mode-explicit nil
81 "Non-nil means `set-scroll-bar-mode' should really do something.
82This is nil while loading `scroll-bar.el', and t afterward.")
83
e4b201bb
KH
84(defun set-scroll-bar-mode-1 (ignore value)
85 (set-scroll-bar-mode value))
86
87(defun set-scroll-bar-mode (value)
04c34f2e 88 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
f62c3ee2
JD
89 (if scroll-bar-mode
90 (setq previous-scroll-bar-mode scroll-bar-mode))
91
04c34f2e
RS
92 (setq scroll-bar-mode value)
93
2124851f 94 (when scroll-bar-mode-explicit
095fe281
KL
95 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
96 scroll-bar-mode)))))
04c34f2e 97
61f2fe7b 98(defcustom scroll-bar-mode default-frame-scroll-bars
04c34f2e
RS
99 "*Specify whether to have vertical scroll bars, and on which side.
100Possible values are nil (no scroll bars), `left' (scroll bars on left)
101and `right' (scroll bars on right).
463deb66
RS
102To set this variable in a Lisp program, use `set-scroll-bar-mode'
103to make it take real effect.
104Setting the variable with a customization buffer also takes effect."
64453f32 105 :type '(choice (const :tag "none (nil)" nil)
04c34f2e
RS
106 (const left)
107 (const right))
108 :group 'frames
2669fbfb
RS
109 ;; The default value for :initialize would try to use :set
110 ;; when processing the file in cus-dep.el.
111 :initialize 'custom-initialize-default
e4b201bb 112 :set 'set-scroll-bar-mode-1)
04c34f2e 113
2124851f
RS
114;; We just set scroll-bar-mode, but that was the default.
115;; If it is set again, that is for real.
116(setq scroll-bar-mode-explicit t)
117
b1ad7c13 118(defun scroll-bar-mode (&optional flag)
04c34f2e 119 "Toggle display of vertical scroll bars on all frames.
fe48f821
JB
120This command applies to all frames that exist and frames to be
121created in the future.
122With a numeric argument, if the argument is negative,
123turn off scroll bars; otherwise, turn on scroll bars."
124 (interactive "P")
ada464a7 125
04c34f2e 126 ;; Tweedle the variable according to the argument.
61f2fe7b
KS
127 (set-scroll-bar-mode (if (if (null flag)
128 (not scroll-bar-mode)
129 (setq flag (prefix-numeric-value flag))
130 (or (not (numberp flag)) (>= flag 0)))
f62c3ee2
JD
131 (or previous-scroll-bar-mode
132 default-frame-scroll-bars))))
04c34f2e
RS
133
134(defun toggle-scroll-bar (arg)
135 "Toggle whether or not the selected frame has vertical scroll bars.
136With arg, turn vertical scroll bars on if and only if arg is positive.
137The variable `scroll-bar-mode' controls which side the scroll bars are on
138when they are turned on; if it is nil, they go on the left."
139 (interactive "P")
140 (if (null arg)
141 (setq arg
142 (if (cdr (assq 'vertical-scroll-bars
143 (frame-parameters (selected-frame))))
19c6feac
KH
144 -1 1))
145 (setq arg (prefix-numeric-value arg)))
1032e793
GV
146 (modify-frame-parameters
147 (selected-frame)
148 (list (cons 'vertical-scroll-bars
149 (if (> arg 0)
61f2fe7b 150 (or scroll-bar-mode default-frame-scroll-bars))))))
04c34f2e
RS
151
152(defun toggle-horizontal-scroll-bar (arg)
153 "Toggle whether or not the selected frame has horizontal scroll bars.
154With arg, turn horizontal scroll bars on if and only if arg is positive.
155Horizontal scroll bars aren't implemented yet."
156 (interactive "P")
157 (error "Horizontal scroll bars aren't implemented yet"))
fe48f821 158\f
bf3c8a70 159;;;; Buffer navigation using the scroll bar.
6d62a90e 160
e532b016 161;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
162(defun scroll-bar-set-window-start (event)
163 "Set the window start according to where the scroll bar is dragged.
164EVENT should be a scroll bar click or drag event."
6d62a90e 165 (interactive "e")
dbc4e1c1 166 (let* ((end-position (event-end event))
6d62a90e
JB
167 (window (nth 0 end-position))
168 (portion-whole (nth 2 end-position)))
169 (save-excursion
170 (set-buffer (window-buffer window))
171 (save-excursion
4cad38d5
JB
172 (goto-char (+ (point-min)
173 (scroll-bar-scale portion-whole
174 (- (point-max) (point-min)))))
6d62a90e
JB
175 (beginning-of-line)
176 (set-window-start window (point))))))
177
aeee66be
RS
178(defun scroll-bar-drag-position (portion-whole)
179 "Calculate new window start for drag event."
180 (save-excursion
181 (goto-char (+ (point-min)
182 (scroll-bar-scale portion-whole
183 (- (point-max) (point-min)))))
184 (beginning-of-line)
185 (point)))
186
187(defun scroll-bar-maybe-set-window-start (event)
188 "Set the window start according to where the scroll bar is dragged.
189Only change window start if the new start is substantially different.
190EVENT should be a scroll bar click or drag event."
191 (interactive "e")
192 (let* ((end-position (event-end event))
193 (window (nth 0 end-position))
194 (portion-whole (nth 2 end-position))
195 (next-portion-whole (cons (1+ (car portion-whole))
196 (cdr portion-whole)))
197 portion-start
198 next-portion-start
199 (current-start (window-start window)))
200 (save-excursion
201 (set-buffer (window-buffer window))
202 (setq portion-start (scroll-bar-drag-position portion-whole))
203 (setq next-portion-start (max
204 (scroll-bar-drag-position next-portion-whole)
205 (1+ portion-start)))
15655694 206 (if (or (>= current-start next-portion-start)
aeee66be 207 (< current-start portion-start))
9c005c65
KH
208 (set-window-start window portion-start)
209 ;; Always set window start, to ensure scroll bar position is updated.
210 (set-window-start window current-start)))))
aeee66be 211
e532b016
RS
212;; Scroll the window to the proper position for EVENT.
213(defun scroll-bar-drag-1 (event)
214 (let* ((start-position (event-start event))
215 (window (nth 0 start-position))
216 (portion-whole (nth 2 start-position)))
217 (save-excursion
218 (set-buffer (window-buffer window))
d2ae6f7e
RS
219 ;; Calculate position relative to the accessible part of the buffer.
220 (goto-char (+ (point-min)
221 (scroll-bar-scale portion-whole
222 (- (point-max) (point-min)))))
9d7ab3f0 223 (vertical-motion 0 window)
e532b016
RS
224 (set-window-start window (point)))))
225
226(defun scroll-bar-drag (event)
227 "Scroll the window by dragging the scroll bar slider.
228If you click outside the slider, the window scrolls to bring the slider there."
229 (interactive "e")
c782bea5 230 (let* (done
cb183ca0
RS
231 (echo-keystrokes 0)
232 (end-position (event-end event))
233 (window (nth 0 end-position))
234 (before-scroll))
235 (with-current-buffer (window-buffer window)
236 (setq before-scroll point-before-scroll))
237 (save-selected-window
238 (select-window window)
239 (setq before-scroll
240 (or before-scroll (point))))
241 (scroll-bar-drag-1 event)
242 (track-mouse
243 (while (not done)
244 (setq event (read-event))
245 (if (eq (car-safe event) 'mouse-movement)
246 (setq event (read-event)))
247 (cond ((eq (car-safe event) 'scroll-bar-movement)
248 (scroll-bar-drag-1 event))
249 (t
250 ;; Exit when we get the drag event; ignore that event.
251 (setq done t)))))
252 (sit-for 0)
253 (with-current-buffer (window-buffer window)
254 (setq point-before-scroll before-scroll))))
e532b016 255
bf3c8a70
JB
256(defun scroll-bar-scroll-down (event)
257 "Scroll the window's top line down to the location of the scroll bar click.
258EVENT should be a scroll bar click."
6d62a90e 259 (interactive "e")
cb183ca0
RS
260 (let* ((end-position (event-end event))
261 (window (nth 0 end-position))
262 (before-scroll))
263 (with-current-buffer (window-buffer window)
264 (setq before-scroll point-before-scroll))
def7db1d
KH
265 (unwind-protect
266 (save-selected-window
267 (let ((portion-whole (nth 2 end-position)))
268 (select-window window)
269 (setq before-scroll
270 (or before-scroll (point)))
271 (scroll-down
272 (scroll-bar-scale portion-whole (1- (window-height)))))
273 (sit-for 0))
274 (with-current-buffer (window-buffer window)
275 (setq point-before-scroll before-scroll)))))
6d62a90e 276
bf3c8a70
JB
277(defun scroll-bar-scroll-up (event)
278 "Scroll the line next to the scroll bar click to the top of the window.
279EVENT should be a scroll bar click."
6d62a90e 280 (interactive "e")
cb183ca0
RS
281 (let* ((end-position (event-end event))
282 (window (nth 0 end-position))
283 (before-scroll))
284 (with-current-buffer (window-buffer window)
285 (setq before-scroll point-before-scroll))
def7db1d
KH
286 (unwind-protect
287 (save-selected-window
288 (let ((portion-whole (nth 2 end-position)))
289 (select-window window)
290 (setq before-scroll
291 (or before-scroll (point)))
292 (scroll-up
293 (scroll-bar-scale portion-whole (1- (window-height)))))
294 (sit-for 0))
295 (with-current-buffer (window-buffer window)
296 (setq point-before-scroll before-scroll)))))
6d62a90e
JB
297
298\f
cf4eb316 299;;; Tookit scroll bars.
6d62a90e 300
cf4eb316
GM
301(defun scroll-bar-toolkit-scroll (event)
302 (interactive "e")
303 (let* ((end-position (event-end event))
304 (window (nth 0 end-position))
305 (part (nth 4 end-position))
306 before-scroll)
2088cd64 307 (cond ((eq part 'end-scroll))
cf4eb316
GM
308 (t
309 (with-current-buffer (window-buffer window)
310 (setq before-scroll point-before-scroll))
311 (save-selected-window
312 (select-window window)
313 (setq before-scroll (or before-scroll (point)))
314 (cond ((eq part 'above-handle)
315 (scroll-up '-))
316 ((eq part 'below-handle)
317 (scroll-up nil))
d294c01f
SM
318 ((eq part 'ratio)
319 (let* ((portion-whole (nth 2 end-position))
320 (lines (scroll-bar-scale portion-whole
321 (1- (window-height)))))
322 (scroll-up (cond ((not (zerop lines)) lines)
323 ((< (car portion-whole) 0) -1)
324 (t 1)))))
cf4eb316
GM
325 ((eq part 'up)
326 (scroll-up -1))
327 ((eq part 'down)
328 (scroll-up 1))
329 ((eq part 'top)
330 (set-window-start window (point-min)))
331 ((eq part 'bottom)
332 (goto-char (point-max))
333 (recenter))
334 ((eq part 'handle)
335 (scroll-bar-drag-1 event))))
336 (sit-for 0)
cf4eb316
GM
337 (with-current-buffer (window-buffer window)
338 (setq point-before-scroll before-scroll))))))
e532b016 339
cf4eb316
GM
340
341\f
342;;;; Bindings.
343
344;;; For now, we'll set things up to work like xterm.
d08947c5 345(cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
cf4eb316
GM
346 (global-set-key [vertical-scroll-bar mouse-1]
347 'scroll-bar-toolkit-scroll))
348 (t
349 (global-set-key [vertical-scroll-bar mouse-1]
350 'scroll-bar-scroll-up)
351 (global-set-key [vertical-scroll-bar drag-mouse-1]
352 'scroll-bar-scroll-up)
353 (global-set-key [vertical-scroll-bar down-mouse-2]
354 'scroll-bar-drag)
355 (global-set-key [vertical-scroll-bar mouse-3]
356 'scroll-bar-scroll-down)
357 (global-set-key [vertical-scroll-bar drag-mouse-3]
358 'scroll-bar-scroll-down)))
6d62a90e
JB
359
360\f
dc14eed2 361(provide 'scroll-bar)
6d62a90e 362
cbee283d 363;; arch-tag: 6f1d01d0-0b1e-4bf8-86db-d491e0f399f3
bf3c8a70 364;;; scroll-bar.el ends here