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