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