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