Line data Source code
1 : ;;; scroll-bar.el --- window system-independent scroll bar support
2 :
3 : ;; Copyright (C) 1993-1995, 1999-2017 Free Software Foundation, Inc.
4 :
5 : ;; Maintainer: emacs-devel@gnu.org
6 : ;; Keywords: hardware
7 : ;; Package: emacs
8 :
9 : ;; This file is part of GNU Emacs.
10 :
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 3 of the License, or
14 : ;; (at your option) any later version.
15 :
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.
20 :
21 : ;; You should have received a copy of the GNU General Public License
22 : ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 :
24 : ;;; Commentary:
25 :
26 : ;; Window-system-independent bindings of mouse clicks on the scroll bar.
27 : ;; Presently emulates the scroll-bar behavior of xterm.
28 :
29 : ;;; Code:
30 :
31 : (require 'mouse)
32 : (eval-when-compile (require 'cl-lib))
33 :
34 :
35 : ;;;; Utilities.
36 :
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 0 : (nth 2 event))
43 :
44 : (defun scroll-bar-scale (num-denom whole)
45 : "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
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
49 : \(buffer-size)) is the position in the current buffer corresponding to
50 : that scroll bar position."
51 : ;; We multiply before we divide to maintain precision.
52 : ;; We use floating point because the product of a large buffer size
53 : ;; with a large scroll bar portion can easily overflow a lisp int.
54 0 : (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
55 :
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 0 : (let* ((wsb (window-scroll-bars))
60 0 : (vtype (nth 2 wsb))
61 0 : (cols (nth 1 wsb)))
62 0 : (cond
63 0 : ((not (memq side '(left right)))
64 0 : (error "`left' or `right' expected instead of %S" side))
65 0 : ((and (eq vtype side) cols))
66 0 : ((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 0 : (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
70 0 : (frame-char-width)))
71 0 : (0))))
72 :
73 : (defun scroll-bar-lines ()
74 : "Return the height, measured in lines, of the horizontal scrollbar."
75 0 : (let* ((wsb (window-scroll-bars))
76 0 : (htype (nth 5 wsb))
77 0 : (lines (nth 4 wsb)))
78 0 : (cond
79 0 : (htype lines)
80 0 : ((frame-parameter nil 'horizontal-scroll-bars)
81 : ;; nil means it's a non-toolkit scroll bar (which is currently
82 : ;; impossible), and its height in lines is 14 pixels rounded up.
83 0 : (ceiling (or (frame-parameter nil 'scroll-bar-height) 14)
84 0 : (frame-char-width)))
85 0 : (0))))
86 :
87 :
88 : ;;;; Helpful functions for enabling and disabling scroll bars.
89 :
90 : (defvar scroll-bar-mode)
91 : (defvar horizontal-scroll-bar-mode)
92 : (defvar previous-scroll-bar-mode nil)
93 :
94 : (defvar scroll-bar-mode-explicit nil
95 : "Non-nil means `set-scroll-bar-mode' should really do something.
96 : This is nil while loading `scroll-bar.el', and t afterward.")
97 :
98 : (defun set-scroll-bar-mode (value)
99 : "Set the scroll bar mode to VALUE and put the new value into effect.
100 : See the `scroll-bar-mode' variable for possible values to use."
101 0 : (if scroll-bar-mode
102 0 : (setq previous-scroll-bar-mode scroll-bar-mode))
103 :
104 0 : (setq scroll-bar-mode value)
105 :
106 0 : (when scroll-bar-mode-explicit
107 0 : (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
108 0 : scroll-bar-mode)))))
109 :
110 : (defcustom scroll-bar-mode default-frame-scroll-bars
111 : "Specify whether to have vertical scroll bars, and on which side.
112 : Possible values are nil (no scroll bars), `left' (scroll bars on left)
113 : and `right' (scroll bars on right).
114 : To set this variable in a Lisp program, use `set-scroll-bar-mode'
115 : to make it take real effect.
116 : Setting the variable with a customization buffer also takes effect."
117 : :type '(choice (const :tag "none (nil)" nil)
118 : (const left)
119 : (const right))
120 : :group 'frames
121 : ;; The default value for :initialize would try to use :set
122 : ;; when processing the file in cus-dep.el.
123 : :initialize 'custom-initialize-default
124 : :set (lambda (_sym val) (set-scroll-bar-mode val)))
125 :
126 : ;; We just set scroll-bar-mode, but that was the default.
127 : ;; If it is set again, that is for real.
128 : (setq scroll-bar-mode-explicit t)
129 :
130 : (defun get-scroll-bar-mode ()
131 : (declare (gv-setter set-scroll-bar-mode))
132 0 : scroll-bar-mode)
133 :
134 : (define-minor-mode scroll-bar-mode
135 : "Toggle vertical scroll bars on all frames (Scroll Bar mode).
136 : With a prefix argument ARG, enable Scroll Bar mode if ARG is
137 : positive, and disable it otherwise. If called from Lisp, enable
138 : the mode if ARG is omitted or nil.
139 :
140 : This command applies to all frames that exist and frames to be
141 : created in the future."
142 : :variable ((get-scroll-bar-mode)
143 : . (lambda (v) (set-scroll-bar-mode
144 : (if v (or previous-scroll-bar-mode
145 : default-frame-scroll-bars))))))
146 :
147 : (defun horizontal-scroll-bars-available-p ()
148 : "Return non-nil when horizontal scroll bars are available on this system."
149 0 : (and (display-graphic-p)
150 0 : (boundp 'x-toolkit-scroll-bars)
151 0 : x-toolkit-scroll-bars))
152 :
153 : (define-minor-mode horizontal-scroll-bar-mode
154 : "Toggle horizontal scroll bars on all frames (Horizontal Scroll Bar mode).
155 : With a prefix argument ARG, enable Horizontal Scroll Bar mode if
156 : ARG is positive, and disable it otherwise. If called from Lisp,
157 : enable the mode if ARG is omitted or nil.
158 :
159 : This command applies to all frames that exist and frames to be
160 : created in the future."
161 : :init-value nil
162 : :global t
163 : :group 'frames
164 0 : (if (and horizontal-scroll-bar-mode
165 0 : (not (horizontal-scroll-bars-available-p)))
166 0 : (progn
167 0 : (setq horizontal-scroll-bar-mode nil)
168 0 : (message "Horizontal scroll bars are not implemented on this system"))
169 0 : (dolist (frame (frame-list))
170 0 : (set-frame-parameter
171 0 : frame 'horizontal-scroll-bars horizontal-scroll-bar-mode))
172 : ;; Handle `default-frame-alist' entry.
173 0 : (setq default-frame-alist
174 0 : (cons (cons 'horizontal-scroll-bars horizontal-scroll-bar-mode)
175 0 : (assq-delete-all 'horizontal-scroll-bars
176 0 : default-frame-alist)))))
177 :
178 : (defun toggle-scroll-bar (arg)
179 : "Toggle whether or not the selected frame has vertical scroll bars.
180 : With ARG, turn vertical scroll bars on if and only if ARG is positive.
181 : The variable `scroll-bar-mode' controls which side the scroll bars are on
182 : when they are turned on; if it is nil, they go on the left."
183 : (interactive "P")
184 0 : (if (null arg)
185 0 : (setq arg
186 0 : (if (frame-parameter nil 'vertical-scroll-bars) -1 1))
187 0 : (setq arg (prefix-numeric-value arg)))
188 0 : (modify-frame-parameters
189 0 : (selected-frame)
190 0 : (list (cons 'vertical-scroll-bars
191 0 : (if (> arg 0)
192 0 : (or scroll-bar-mode default-frame-scroll-bars))))))
193 :
194 : (defun toggle-horizontal-scroll-bar (arg)
195 : "Toggle whether or not the selected frame has horizontal scroll bars.
196 : With ARG, turn vertical scroll bars on if and only if ARG is positive."
197 : (interactive "P")
198 0 : (if (null arg)
199 0 : (setq arg
200 0 : (if (frame-parameter nil 'horizontal-scroll-bars) -1 1))
201 0 : (setq arg (prefix-numeric-value arg)))
202 0 : (modify-frame-parameters
203 0 : (selected-frame)
204 0 : (list (cons 'horizontal-scroll-bars
205 0 : (when (> arg 0) 'bottom)))))
206 :
207 : ;;;; Buffer navigation using the scroll bar.
208 :
209 : ;; This was used for up-events on button 2, but no longer.
210 : (defun scroll-bar-set-window-start (event)
211 : "Set the window start according to where the scroll bar is dragged.
212 : EVENT should be a scroll bar click or drag event."
213 : (interactive "e")
214 0 : (let* ((end-position (event-end event))
215 0 : (window (nth 0 end-position))
216 0 : (portion-whole (nth 2 end-position)))
217 0 : (with-current-buffer (window-buffer window)
218 0 : (save-excursion
219 0 : (goto-char (+ (point-min)
220 0 : (scroll-bar-scale portion-whole
221 0 : (- (point-max) (point-min)))))
222 0 : (beginning-of-line)
223 0 : (set-window-start window (point))))))
224 :
225 : (defun scroll-bar-drag-position (portion-whole)
226 : "Calculate new window start for drag event."
227 0 : (save-excursion
228 0 : (goto-char (+ (point-min)
229 0 : (scroll-bar-scale portion-whole
230 0 : (- (point-max) (point-min)))))
231 0 : (beginning-of-line)
232 0 : (point)))
233 :
234 : (defun scroll-bar-maybe-set-window-start (event)
235 : "Set the window start according to where the scroll bar is dragged.
236 : Only change window start if the new start is substantially different.
237 : EVENT should be a scroll bar click or drag event."
238 : (interactive "e")
239 0 : (let* ((end-position (event-end event))
240 0 : (window (nth 0 end-position))
241 0 : (portion-whole (nth 2 end-position))
242 0 : (next-portion-whole (cons (1+ (car portion-whole))
243 0 : (cdr portion-whole)))
244 : portion-start
245 : next-portion-start
246 0 : (current-start (window-start window)))
247 0 : (with-current-buffer (window-buffer window)
248 0 : (setq portion-start (scroll-bar-drag-position portion-whole))
249 0 : (setq next-portion-start (max
250 0 : (scroll-bar-drag-position next-portion-whole)
251 0 : (1+ portion-start)))
252 0 : (if (or (>= current-start next-portion-start)
253 0 : (< current-start portion-start))
254 0 : (set-window-start window portion-start)
255 : ;; Always set window start, to ensure scroll bar position is updated.
256 0 : (set-window-start window current-start)))))
257 :
258 : ;; Scroll the window to the proper position for EVENT.
259 : (defun scroll-bar-drag-1 (event)
260 0 : (let* ((start-position (event-start event))
261 0 : (window (nth 0 start-position))
262 0 : (portion-whole (nth 2 start-position)))
263 0 : (save-excursion
264 0 : (with-current-buffer (window-buffer window)
265 : ;; Calculate position relative to the accessible part of the buffer.
266 0 : (goto-char (+ (point-min)
267 0 : (scroll-bar-scale portion-whole
268 0 : (- (point-max) (point-min)))))
269 0 : (vertical-motion 0 window)
270 0 : (set-window-start window (point))))))
271 :
272 : (defun scroll-bar-drag (event)
273 : "Scroll the window by dragging the scroll bar slider.
274 : If you click outside the slider, the window scrolls to bring the slider there."
275 : (interactive "e")
276 0 : (let* (done
277 : (echo-keystrokes 0)
278 0 : (end-position (event-end event))
279 0 : (window (nth 0 end-position))
280 : (before-scroll))
281 0 : (with-current-buffer (window-buffer window)
282 0 : (setq before-scroll point-before-scroll))
283 0 : (save-selected-window
284 0 : (select-window window 'mark-for-redisplay)
285 0 : (setq before-scroll
286 0 : (or before-scroll (point))))
287 0 : (scroll-bar-drag-1 event)
288 0 : (track-mouse
289 0 : (while (not done)
290 0 : (setq event (read-event))
291 0 : (if (eq (car-safe event) 'mouse-movement)
292 0 : (setq event (read-event)))
293 0 : (cond ((eq (car-safe event) 'scroll-bar-movement)
294 0 : (scroll-bar-drag-1 event))
295 : (t
296 : ;; Exit when we get the drag event; ignore that event.
297 0 : (setq done t)))))
298 0 : (sit-for 0)
299 0 : (with-current-buffer (window-buffer window)
300 0 : (setq point-before-scroll before-scroll))))
301 :
302 : ;; Scroll the window to the proper position for EVENT.
303 : (defun scroll-bar-horizontal-drag-1 (event)
304 0 : (let* ((start-position (event-start event))
305 0 : (window (nth 0 start-position))
306 0 : (portion-whole (nth 2 start-position))
307 0 : (unit (frame-char-width (window-frame window))))
308 0 : (if (eq (current-bidi-paragraph-direction (window-buffer window))
309 0 : 'left-to-right)
310 0 : (set-window-hscroll
311 0 : window (/ (+ (car portion-whole) (1- unit)) unit))
312 0 : (set-window-hscroll
313 0 : window (/ (+ (- (cdr portion-whole) (car portion-whole))
314 0 : (1- unit))
315 0 : unit)))))
316 :
317 : (defun scroll-bar-horizontal-drag (event)
318 : "Scroll the window horizontally by dragging the scroll bar slider.
319 : If you click outside the slider, the window scrolls to bring the slider there."
320 : (interactive "e")
321 0 : (let* (done
322 : (echo-keystrokes 0)
323 0 : (end-position (event-end event))
324 0 : (window (nth 0 end-position))
325 : (before-scroll))
326 0 : (with-current-buffer (window-buffer window)
327 0 : (setq before-scroll point-before-scroll))
328 0 : (save-selected-window
329 0 : (select-window window 'mark-for-redisplay)
330 0 : (setq before-scroll
331 0 : (or before-scroll (point))))
332 0 : (scroll-bar-horizontal-drag-1 event)
333 0 : (track-mouse
334 0 : (while (not done)
335 0 : (setq event (read-event))
336 0 : (if (eq (car-safe event) 'mouse-movement)
337 0 : (setq event (read-event)))
338 0 : (cond ((eq (car-safe event) 'scroll-bar-movement)
339 0 : (scroll-bar-horizontal-drag-1 event))
340 : (t
341 : ;; Exit when we get the drag event; ignore that event.
342 0 : (setq done t)))))
343 0 : (sit-for 0)
344 0 : (with-current-buffer (window-buffer window)
345 0 : (setq point-before-scroll before-scroll))))
346 :
347 : (defun scroll-bar-scroll-down (event)
348 : "Scroll the window's top line down to the location of the scroll bar click.
349 : EVENT should be a scroll bar click."
350 : (interactive "e")
351 0 : (let* ((end-position (event-end event))
352 0 : (window (nth 0 end-position))
353 : (before-scroll))
354 0 : (with-current-buffer (window-buffer window)
355 0 : (setq before-scroll point-before-scroll))
356 0 : (unwind-protect
357 0 : (save-selected-window
358 0 : (let ((portion-whole (nth 2 end-position)))
359 0 : (select-window window 'mark-for-redisplay)
360 0 : (setq before-scroll
361 0 : (or before-scroll (point)))
362 0 : (scroll-down
363 0 : (scroll-bar-scale portion-whole (1- (window-height)))))
364 0 : (sit-for 0))
365 0 : (with-current-buffer (window-buffer window)
366 0 : (setq point-before-scroll before-scroll)))))
367 :
368 : (defun scroll-bar-scroll-up (event)
369 : "Scroll the line next to the scroll bar click to the top of the window.
370 : EVENT should be a scroll bar click."
371 : (interactive "e")
372 0 : (let* ((end-position (event-end event))
373 0 : (window (nth 0 end-position))
374 : (before-scroll))
375 0 : (with-current-buffer (window-buffer window)
376 0 : (setq before-scroll point-before-scroll))
377 0 : (unwind-protect
378 0 : (save-selected-window
379 0 : (let ((portion-whole (nth 2 end-position)))
380 0 : (select-window window 'mark-for-redisplay)
381 0 : (setq before-scroll
382 0 : (or before-scroll (point)))
383 0 : (scroll-up
384 0 : (scroll-bar-scale portion-whole (1- (window-height)))))
385 0 : (sit-for 0))
386 0 : (with-current-buffer (window-buffer window)
387 0 : (setq point-before-scroll before-scroll)))))
388 :
389 :
390 : ;;; Tookit scroll bars.
391 :
392 : (defun scroll-bar-toolkit-scroll (event)
393 : "Handle event EVENT on vertical scroll bar."
394 : (interactive "e")
395 0 : (let* ((end-position (event-end event))
396 0 : (window (nth 0 end-position))
397 0 : (part (nth 4 end-position))
398 : before-scroll)
399 0 : (cond
400 0 : ((eq part 'end-scroll))
401 : (t
402 0 : (with-current-buffer (window-buffer window)
403 0 : (setq before-scroll point-before-scroll))
404 0 : (save-selected-window
405 0 : (select-window window 'mark-for-redisplay)
406 0 : (setq before-scroll (or before-scroll (point)))
407 0 : (cond
408 0 : ((eq part 'above-handle)
409 0 : (scroll-up '-))
410 0 : ((eq part 'below-handle)
411 0 : (scroll-up nil))
412 0 : ((eq part 'ratio)
413 0 : (let* ((portion-whole (nth 2 end-position))
414 0 : (lines (scroll-bar-scale portion-whole
415 0 : (1- (window-height)))))
416 0 : (scroll-up (cond ((not (zerop lines)) lines)
417 0 : ((< (car portion-whole) 0) -1)
418 0 : (t 1)))))
419 0 : ((eq part 'up)
420 0 : (scroll-up -1))
421 0 : ((eq part 'down)
422 0 : (scroll-up 1))
423 0 : ((eq part 'top)
424 0 : (set-window-start window (point-min)))
425 0 : ((eq part 'bottom)
426 0 : (goto-char (point-max))
427 0 : (recenter))
428 0 : ((eq part 'handle)
429 0 : (scroll-bar-drag-1 event))))
430 0 : (sit-for 0)
431 0 : (with-current-buffer (window-buffer window)
432 0 : (setq point-before-scroll before-scroll))))))
433 :
434 : (defun scroll-bar-toolkit-horizontal-scroll (event)
435 : "Handle event EVENT on horizontal scroll bar."
436 : (interactive "e")
437 0 : (let* ((end-position (event-end event))
438 0 : (window (nth 0 end-position))
439 0 : (part (nth 4 end-position))
440 : (bidi-factor
441 0 : (if (eq (current-bidi-paragraph-direction (window-buffer window))
442 0 : 'left-to-right)
443 : 1
444 0 : -1))
445 : before-scroll)
446 0 : (cond
447 0 : ((eq part 'end-scroll))
448 : (t
449 0 : (with-current-buffer (window-buffer window)
450 0 : (setq before-scroll point-before-scroll))
451 0 : (save-selected-window
452 0 : (select-window window 'mark-for-redisplay)
453 0 : (setq before-scroll (or before-scroll (point)))
454 0 : (cond
455 0 : ((eq part 'before-handle)
456 0 : (scroll-right (* bidi-factor 4)))
457 0 : ((eq part 'after-handle)
458 0 : (scroll-left (* bidi-factor 4)))
459 0 : ((eq part 'ratio)
460 0 : (let* ((portion-whole (nth 2 end-position))
461 0 : (columns (scroll-bar-scale portion-whole
462 0 : (1- (window-width)))))
463 0 : (scroll-right
464 0 : (* (cond
465 0 : ((not (zerop columns))
466 0 : columns)
467 0 : ((< (car portion-whole) 0) -1)
468 0 : (t 1))
469 0 : bidi-factor))))
470 0 : ((eq part 'left)
471 0 : (scroll-right (* bidi-factor 1)))
472 0 : ((eq part 'right)
473 0 : (scroll-left (* bidi-factor 1)))
474 0 : ((eq part 'leftmost)
475 0 : (goto-char (if (eq bidi-factor 1)
476 0 : (line-beginning-position)
477 0 : (line-end-position))))
478 0 : ((eq part 'rightmost)
479 0 : (goto-char (if (eq bidi-factor 1)
480 0 : (line-end-position)
481 0 : (line-beginning-position))))
482 0 : ((eq part 'horizontal-handle)
483 0 : (scroll-bar-horizontal-drag-1 event))))
484 0 : (sit-for 0)
485 0 : (with-current-buffer (window-buffer window)
486 0 : (setq point-before-scroll before-scroll))))))
487 :
488 : ;;;; Bindings.
489 :
490 : ;; For now, we'll set things up to work like xterm.
491 : (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
492 : (global-set-key [vertical-scroll-bar mouse-1]
493 : 'scroll-bar-toolkit-scroll)
494 : (global-set-key [horizontal-scroll-bar mouse-1]
495 : 'scroll-bar-toolkit-horizontal-scroll))
496 : (t
497 : (global-set-key [vertical-scroll-bar mouse-1]
498 : 'scroll-bar-scroll-up)
499 : (global-set-key [vertical-scroll-bar drag-mouse-1]
500 : 'scroll-bar-scroll-up)
501 : (global-set-key [vertical-scroll-bar down-mouse-2]
502 : 'scroll-bar-drag)
503 : (global-set-key [vertical-scroll-bar mouse-3]
504 : 'scroll-bar-scroll-down)
505 : (global-set-key [vertical-scroll-bar drag-mouse-3]
506 : 'scroll-bar-scroll-down)))
507 :
508 :
509 : (provide 'scroll-bar)
510 :
511 : ;;; scroll-bar.el ends here
|