*** empty log message ***
[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,
aaef169d 4;; 2004, 2005, 2006 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
13;; the Free Software Foundation; either version 2, or (at your option)
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
RS
77(defvar scroll-bar-mode)
78
2124851f
RS
79(defvar scroll-bar-mode-explicit nil
80 "Non-nil means `set-scroll-bar-mode' should really do something.
81This is nil while loading `scroll-bar.el', and t afterward.")
82
e4b201bb
KH
83(defun set-scroll-bar-mode-1 (ignore value)
84 (set-scroll-bar-mode value))
85
86(defun set-scroll-bar-mode (value)
04c34f2e
RS
87 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
88 (setq scroll-bar-mode value)
89
2124851f
RS
90 (when scroll-bar-mode-explicit
91 ;; Apply it to default-frame-alist.
92 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
93 (if (consp parameter)
94 (setcdr parameter scroll-bar-mode)
95 (setq default-frame-alist
96 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
97 default-frame-alist))))
98
99 ;; Apply it to existing frames.
100 (let ((frames (frame-list)))
101 (while frames
102 (modify-frame-parameters
103 (car frames)
104 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
105 (setq frames (cdr frames))))))
04c34f2e 106
61f2fe7b 107(defcustom scroll-bar-mode default-frame-scroll-bars
04c34f2e
RS
108 "*Specify whether to have vertical scroll bars, and on which side.
109Possible values are nil (no scroll bars), `left' (scroll bars on left)
110and `right' (scroll bars on right).
463deb66
RS
111To set this variable in a Lisp program, use `set-scroll-bar-mode'
112to make it take real effect.
113Setting the variable with a customization buffer also takes effect."
64453f32 114 :type '(choice (const :tag "none (nil)" nil)
04c34f2e
RS
115 (const left)
116 (const right))
117 :group 'frames
2669fbfb
RS
118 ;; The default value for :initialize would try to use :set
119 ;; when processing the file in cus-dep.el.
120 :initialize 'custom-initialize-default
e4b201bb 121 :set 'set-scroll-bar-mode-1)
04c34f2e 122
2124851f
RS
123;; We just set scroll-bar-mode, but that was the default.
124;; If it is set again, that is for real.
125(setq scroll-bar-mode-explicit t)
126
b1ad7c13 127(defun scroll-bar-mode (&optional flag)
04c34f2e 128 "Toggle display of vertical scroll bars on all frames.
fe48f821
JB
129This command applies to all frames that exist and frames to be
130created in the future.
131With a numeric argument, if the argument is negative,
132turn off scroll bars; otherwise, turn on scroll bars."
133 (interactive "P")
ada464a7 134
04c34f2e 135 ;; Tweedle the variable according to the argument.
61f2fe7b
KS
136 (set-scroll-bar-mode (if (if (null flag)
137 (not scroll-bar-mode)
138 (setq flag (prefix-numeric-value flag))
139 (or (not (numberp flag)) (>= flag 0)))
140 default-frame-scroll-bars)))
04c34f2e
RS
141
142(defun toggle-scroll-bar (arg)
143 "Toggle whether or not the selected frame has vertical scroll bars.
144With arg, turn vertical scroll bars on if and only if arg is positive.
145The variable `scroll-bar-mode' controls which side the scroll bars are on
146when they are turned on; if it is nil, they go on the left."
147 (interactive "P")
148 (if (null arg)
149 (setq arg
150 (if (cdr (assq 'vertical-scroll-bars
151 (frame-parameters (selected-frame))))
19c6feac
KH
152 -1 1))
153 (setq arg (prefix-numeric-value arg)))
1032e793
GV
154 (modify-frame-parameters
155 (selected-frame)
156 (list (cons 'vertical-scroll-bars
157 (if (> arg 0)
61f2fe7b 158 (or scroll-bar-mode default-frame-scroll-bars))))))
04c34f2e
RS
159
160(defun toggle-horizontal-scroll-bar (arg)
161 "Toggle whether or not the selected frame has horizontal scroll bars.
162With arg, turn horizontal scroll bars on if and only if arg is positive.
163Horizontal scroll bars aren't implemented yet."
164 (interactive "P")
165 (error "Horizontal scroll bars aren't implemented yet"))
fe48f821 166\f
bf3c8a70 167;;;; Buffer navigation using the scroll bar.
6d62a90e 168
e532b016 169;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
170(defun scroll-bar-set-window-start (event)
171 "Set the window start according to where the scroll bar is dragged.
172EVENT should be a scroll bar click or drag event."
6d62a90e 173 (interactive "e")
dbc4e1c1 174 (let* ((end-position (event-end event))
6d62a90e
JB
175 (window (nth 0 end-position))
176 (portion-whole (nth 2 end-position)))
177 (save-excursion
178 (set-buffer (window-buffer window))
179 (save-excursion
4cad38d5
JB
180 (goto-char (+ (point-min)
181 (scroll-bar-scale portion-whole
182 (- (point-max) (point-min)))))
6d62a90e
JB
183 (beginning-of-line)
184 (set-window-start window (point))))))
185
aeee66be
RS
186(defun scroll-bar-drag-position (portion-whole)
187 "Calculate new window start for drag event."
188 (save-excursion
189 (goto-char (+ (point-min)
190 (scroll-bar-scale portion-whole
191 (- (point-max) (point-min)))))
192 (beginning-of-line)
193 (point)))
194
195(defun scroll-bar-maybe-set-window-start (event)
196 "Set the window start according to where the scroll bar is dragged.
197Only change window start if the new start is substantially different.
198EVENT should be a scroll bar click or drag event."
199 (interactive "e")
200 (let* ((end-position (event-end event))
201 (window (nth 0 end-position))
202 (portion-whole (nth 2 end-position))
203 (next-portion-whole (cons (1+ (car portion-whole))
204 (cdr portion-whole)))
205 portion-start
206 next-portion-start
207 (current-start (window-start window)))
208 (save-excursion
209 (set-buffer (window-buffer window))
210 (setq portion-start (scroll-bar-drag-position portion-whole))
211 (setq next-portion-start (max
212 (scroll-bar-drag-position next-portion-whole)
213 (1+ portion-start)))
15655694 214 (if (or (>= current-start next-portion-start)
aeee66be 215 (< current-start portion-start))
9c005c65
KH
216 (set-window-start window portion-start)
217 ;; Always set window start, to ensure scroll bar position is updated.
218 (set-window-start window current-start)))))
aeee66be 219
e532b016
RS
220;; Scroll the window to the proper position for EVENT.
221(defun scroll-bar-drag-1 (event)
222 (let* ((start-position (event-start event))
223 (window (nth 0 start-position))
224 (portion-whole (nth 2 start-position)))
225 (save-excursion
226 (set-buffer (window-buffer window))
d2ae6f7e
RS
227 ;; Calculate position relative to the accessible part of the buffer.
228 (goto-char (+ (point-min)
229 (scroll-bar-scale portion-whole
230 (- (point-max) (point-min)))))
9d7ab3f0 231 (vertical-motion 0 window)
e532b016
RS
232 (set-window-start window (point)))))
233
234(defun scroll-bar-drag (event)
235 "Scroll the window by dragging the scroll bar slider.
236If you click outside the slider, the window scrolls to bring the slider there."
237 (interactive "e")
c782bea5 238 (let* (done
cb183ca0
RS
239 (echo-keystrokes 0)
240 (end-position (event-end event))
241 (window (nth 0 end-position))
242 (before-scroll))
243 (with-current-buffer (window-buffer window)
244 (setq before-scroll point-before-scroll))
245 (save-selected-window
246 (select-window window)
247 (setq before-scroll
248 (or before-scroll (point))))
249 (scroll-bar-drag-1 event)
250 (track-mouse
251 (while (not done)
252 (setq event (read-event))
253 (if (eq (car-safe event) 'mouse-movement)
254 (setq event (read-event)))
255 (cond ((eq (car-safe event) 'scroll-bar-movement)
256 (scroll-bar-drag-1 event))
257 (t
258 ;; Exit when we get the drag event; ignore that event.
259 (setq done t)))))
260 (sit-for 0)
261 (with-current-buffer (window-buffer window)
262 (setq point-before-scroll before-scroll))))
e532b016 263
bf3c8a70
JB
264(defun scroll-bar-scroll-down (event)
265 "Scroll the window's top line down to the location of the scroll bar click.
266EVENT should be a scroll bar click."
6d62a90e 267 (interactive "e")
cb183ca0
RS
268 (let* ((end-position (event-end event))
269 (window (nth 0 end-position))
270 (before-scroll))
271 (with-current-buffer (window-buffer window)
272 (setq before-scroll point-before-scroll))
def7db1d
KH
273 (unwind-protect
274 (save-selected-window
275 (let ((portion-whole (nth 2 end-position)))
276 (select-window window)
277 (setq before-scroll
278 (or before-scroll (point)))
279 (scroll-down
280 (scroll-bar-scale portion-whole (1- (window-height)))))
281 (sit-for 0))
282 (with-current-buffer (window-buffer window)
283 (setq point-before-scroll before-scroll)))))
6d62a90e 284
bf3c8a70
JB
285(defun scroll-bar-scroll-up (event)
286 "Scroll the line next to the scroll bar click to the top of the window.
287EVENT should be a scroll bar click."
6d62a90e 288 (interactive "e")
cb183ca0
RS
289 (let* ((end-position (event-end event))
290 (window (nth 0 end-position))
291 (before-scroll))
292 (with-current-buffer (window-buffer window)
293 (setq before-scroll point-before-scroll))
def7db1d
KH
294 (unwind-protect
295 (save-selected-window
296 (let ((portion-whole (nth 2 end-position)))
297 (select-window window)
298 (setq before-scroll
299 (or before-scroll (point)))
300 (scroll-up
301 (scroll-bar-scale portion-whole (1- (window-height)))))
302 (sit-for 0))
303 (with-current-buffer (window-buffer window)
304 (setq point-before-scroll before-scroll)))))
6d62a90e
JB
305
306\f
cf4eb316 307;;; Tookit scroll bars.
6d62a90e 308
cf4eb316
GM
309(defun scroll-bar-toolkit-scroll (event)
310 (interactive "e")
311 (let* ((end-position (event-end event))
312 (window (nth 0 end-position))
313 (part (nth 4 end-position))
314 before-scroll)
2088cd64 315 (cond ((eq part 'end-scroll))
cf4eb316
GM
316 (t
317 (with-current-buffer (window-buffer window)
318 (setq before-scroll point-before-scroll))
319 (save-selected-window
320 (select-window window)
321 (setq before-scroll (or before-scroll (point)))
322 (cond ((eq part 'above-handle)
323 (scroll-up '-))
324 ((eq part 'below-handle)
325 (scroll-up nil))
d294c01f
SM
326 ((eq part 'ratio)
327 (let* ((portion-whole (nth 2 end-position))
328 (lines (scroll-bar-scale portion-whole
329 (1- (window-height)))))
330 (scroll-up (cond ((not (zerop lines)) lines)
331 ((< (car portion-whole) 0) -1)
332 (t 1)))))
cf4eb316
GM
333 ((eq part 'up)
334 (scroll-up -1))
335 ((eq part 'down)
336 (scroll-up 1))
337 ((eq part 'top)
338 (set-window-start window (point-min)))
339 ((eq part 'bottom)
340 (goto-char (point-max))
341 (recenter))
342 ((eq part 'handle)
343 (scroll-bar-drag-1 event))))
344 (sit-for 0)
cf4eb316
GM
345 (with-current-buffer (window-buffer window)
346 (setq point-before-scroll before-scroll))))))
e532b016 347
cf4eb316
GM
348
349\f
350;;;; Bindings.
351
352;;; For now, we'll set things up to work like xterm.
d08947c5 353(cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
cf4eb316
GM
354 (global-set-key [vertical-scroll-bar mouse-1]
355 'scroll-bar-toolkit-scroll))
356 (t
357 (global-set-key [vertical-scroll-bar mouse-1]
358 'scroll-bar-scroll-up)
359 (global-set-key [vertical-scroll-bar drag-mouse-1]
360 'scroll-bar-scroll-up)
361 (global-set-key [vertical-scroll-bar down-mouse-2]
362 'scroll-bar-drag)
363 (global-set-key [vertical-scroll-bar mouse-3]
364 'scroll-bar-scroll-down)
365 (global-set-key [vertical-scroll-bar drag-mouse-3]
366 'scroll-bar-scroll-down)))
6d62a90e
JB
367
368\f
dc14eed2 369(provide 'scroll-bar)
6d62a90e 370
ab5796a9 371;;; arch-tag: 6f1d01d0-0b1e-4bf8-86db-d491e0f399f3
bf3c8a70 372;;; scroll-bar.el ends here