]> code.delx.au - gnu-emacs/blob - lisp/scroll-bar.el
838f9bf80cd1d717a4ea7974bf2cf61904e52e23
[gnu-emacs] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support
2
3 ;; Copyright (C) 1993-1995, 1999-2016 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 \f
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 (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 (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 (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
73 (defun scroll-bar-lines ()
74 "Return the height, measured in lines, of the horizontal scrollbar."
75 (let* ((wsb (window-scroll-bars))
76 (htype (nth 5 wsb))
77 (lines (nth 4 wsb)))
78 (cond
79 (htype lines)
80 ((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 (ceiling (or (frame-parameter nil 'scroll-bar-height) 14)
84 (frame-char-width)))
85 (0))))
86
87 \f
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 (if scroll-bar-mode
102 (setq previous-scroll-bar-mode scroll-bar-mode))
103
104 (setq scroll-bar-mode value)
105
106 (when scroll-bar-mode-explicit
107 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
108 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 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 (and (display-graphic-p)
150 (boundp 'x-toolkit-scroll-bars)
151 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 (if (and horizontal-scroll-bar-mode
165 (not (horizontal-scroll-bars-available-p)))
166 (progn
167 (setq horizontal-scroll-bar-mode nil)
168 (message "Horizontal scroll bars are not implemented on this system"))
169 (dolist (frame (frame-list))
170 (set-frame-parameter
171 frame 'horizontal-scroll-bars horizontal-scroll-bar-mode))
172 ;; Handle `default-frame-alist' entry.
173 (setq default-frame-alist
174 (cons (cons 'horizontal-scroll-bars horizontal-scroll-bar-mode)
175 (assq-delete-all 'horizontal-scroll-bars
176 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 (if (null arg)
185 (setq arg
186 (if (cdr (assq 'vertical-scroll-bars
187 (frame-parameters (selected-frame))))
188 -1 1))
189 (setq arg (prefix-numeric-value arg)))
190 (modify-frame-parameters
191 (selected-frame)
192 (list (cons 'vertical-scroll-bars
193 (if (> arg 0)
194 (or scroll-bar-mode default-frame-scroll-bars))))))
195
196 (defun toggle-horizontal-scroll-bar (arg)
197 "Toggle whether or not the selected frame has horizontal scroll bars.
198 With ARG, turn vertical scroll bars on if and only if ARG is positive."
199 (interactive "P")
200 (if (null arg)
201 (setq arg
202 (if (cdr (assq 'horizontal-scroll-bars
203 (frame-parameters (selected-frame))))
204 -1 1))
205 (setq arg (prefix-numeric-value arg)))
206 (modify-frame-parameters
207 (selected-frame)
208 (list (cons 'horizontal-scroll-bars
209 (when (> arg 0) 'bottom)))))
210 \f
211 ;;;; Buffer navigation using the scroll bar.
212
213 ;; This was used for up-events on button 2, but no longer.
214 (defun scroll-bar-set-window-start (event)
215 "Set the window start according to where the scroll bar is dragged.
216 EVENT should be a scroll bar click or drag event."
217 (interactive "e")
218 (let* ((end-position (event-end event))
219 (window (nth 0 end-position))
220 (portion-whole (nth 2 end-position)))
221 (with-current-buffer (window-buffer window)
222 (save-excursion
223 (goto-char (+ (point-min)
224 (scroll-bar-scale portion-whole
225 (- (point-max) (point-min)))))
226 (beginning-of-line)
227 (set-window-start window (point))))))
228
229 (defun scroll-bar-drag-position (portion-whole)
230 "Calculate new window start for drag event."
231 (save-excursion
232 (goto-char (+ (point-min)
233 (scroll-bar-scale portion-whole
234 (- (point-max) (point-min)))))
235 (beginning-of-line)
236 (point)))
237
238 (defun scroll-bar-maybe-set-window-start (event)
239 "Set the window start according to where the scroll bar is dragged.
240 Only change window start if the new start is substantially different.
241 EVENT should be a scroll bar click or drag event."
242 (interactive "e")
243 (let* ((end-position (event-end event))
244 (window (nth 0 end-position))
245 (portion-whole (nth 2 end-position))
246 (next-portion-whole (cons (1+ (car portion-whole))
247 (cdr portion-whole)))
248 portion-start
249 next-portion-start
250 (current-start (window-start window)))
251 (with-current-buffer (window-buffer window)
252 (setq portion-start (scroll-bar-drag-position portion-whole))
253 (setq next-portion-start (max
254 (scroll-bar-drag-position next-portion-whole)
255 (1+ portion-start)))
256 (if (or (>= current-start next-portion-start)
257 (< current-start portion-start))
258 (set-window-start window portion-start)
259 ;; Always set window start, to ensure scroll bar position is updated.
260 (set-window-start window current-start)))))
261
262 ;; Scroll the window to the proper position for EVENT.
263 (defun scroll-bar-drag-1 (event)
264 (let* ((start-position (event-start event))
265 (window (nth 0 start-position))
266 (portion-whole (nth 2 start-position)))
267 (save-excursion
268 (with-current-buffer (window-buffer window)
269 ;; Calculate position relative to the accessible part of the buffer.
270 (goto-char (+ (point-min)
271 (scroll-bar-scale portion-whole
272 (- (point-max) (point-min)))))
273 (vertical-motion 0 window)
274 (set-window-start window (point))))))
275
276 (defun scroll-bar-drag (event)
277 "Scroll the window by dragging the scroll bar slider.
278 If you click outside the slider, the window scrolls to bring the slider there."
279 (interactive "e")
280 (let* (done
281 (echo-keystrokes 0)
282 (end-position (event-end event))
283 (window (nth 0 end-position))
284 (before-scroll))
285 (with-current-buffer (window-buffer window)
286 (setq before-scroll point-before-scroll))
287 (save-selected-window
288 (select-window window)
289 (setq before-scroll
290 (or before-scroll (point))))
291 (scroll-bar-drag-1 event)
292 (track-mouse
293 (while (not done)
294 (setq event (read-event))
295 (if (eq (car-safe event) 'mouse-movement)
296 (setq event (read-event)))
297 (cond ((eq (car-safe event) 'scroll-bar-movement)
298 (scroll-bar-drag-1 event))
299 (t
300 ;; Exit when we get the drag event; ignore that event.
301 (setq done t)))))
302 (sit-for 0)
303 (with-current-buffer (window-buffer window)
304 (setq point-before-scroll before-scroll))))
305
306 ;; Scroll the window to the proper position for EVENT.
307 (defun scroll-bar-horizontal-drag-1 (event)
308 (let* ((start-position (event-start event))
309 (window (nth 0 start-position))
310 (portion-whole (nth 2 start-position))
311 (unit (frame-char-width (window-frame window))))
312 (if (eq (current-bidi-paragraph-direction (window-buffer window))
313 'left-to-right)
314 (set-window-hscroll
315 window (/ (+ (car portion-whole) (1- unit)) unit))
316 (set-window-hscroll
317 window (/ (+ (- (cdr portion-whole) (car portion-whole))
318 (1- unit))
319 unit)))))
320
321 (defun scroll-bar-horizontal-drag (event)
322 "Scroll the window horizontally by dragging the scroll bar slider.
323 If you click outside the slider, the window scrolls to bring the slider there."
324 (interactive "e")
325 (let* (done
326 (echo-keystrokes 0)
327 (end-position (event-end event))
328 (window (nth 0 end-position))
329 (before-scroll))
330 (with-current-buffer (window-buffer window)
331 (setq before-scroll point-before-scroll))
332 (save-selected-window
333 (select-window window)
334 (setq before-scroll
335 (or before-scroll (point))))
336 (scroll-bar-horizontal-drag-1 event)
337 (track-mouse
338 (while (not done)
339 (setq event (read-event))
340 (if (eq (car-safe event) 'mouse-movement)
341 (setq event (read-event)))
342 (cond ((eq (car-safe event) 'scroll-bar-movement)
343 (scroll-bar-horizontal-drag-1 event))
344 (t
345 ;; Exit when we get the drag event; ignore that event.
346 (setq done t)))))
347 (sit-for 0)
348 (with-current-buffer (window-buffer window)
349 (setq point-before-scroll before-scroll))))
350
351 (defun scroll-bar-scroll-down (event)
352 "Scroll the window's top line down to the location of the scroll bar click.
353 EVENT should be a scroll bar click."
354 (interactive "e")
355 (let* ((end-position (event-end event))
356 (window (nth 0 end-position))
357 (before-scroll))
358 (with-current-buffer (window-buffer window)
359 (setq before-scroll point-before-scroll))
360 (unwind-protect
361 (save-selected-window
362 (let ((portion-whole (nth 2 end-position)))
363 (select-window window)
364 (setq before-scroll
365 (or before-scroll (point)))
366 (scroll-down
367 (scroll-bar-scale portion-whole (1- (window-height)))))
368 (sit-for 0))
369 (with-current-buffer (window-buffer window)
370 (setq point-before-scroll before-scroll)))))
371
372 (defun scroll-bar-scroll-up (event)
373 "Scroll the line next to the scroll bar click to the top of the window.
374 EVENT should be a scroll bar click."
375 (interactive "e")
376 (let* ((end-position (event-end event))
377 (window (nth 0 end-position))
378 (before-scroll))
379 (with-current-buffer (window-buffer window)
380 (setq before-scroll point-before-scroll))
381 (unwind-protect
382 (save-selected-window
383 (let ((portion-whole (nth 2 end-position)))
384 (select-window window)
385 (setq before-scroll
386 (or before-scroll (point)))
387 (scroll-up
388 (scroll-bar-scale portion-whole (1- (window-height)))))
389 (sit-for 0))
390 (with-current-buffer (window-buffer window)
391 (setq point-before-scroll before-scroll)))))
392
393 \f
394 ;;; Tookit scroll bars.
395
396 (defun scroll-bar-toolkit-scroll (event)
397 "Handle event EVENT on vertical scroll bar."
398 (interactive "e")
399 (let* ((end-position (event-end event))
400 (window (nth 0 end-position))
401 (part (nth 4 end-position))
402 before-scroll)
403 (cond
404 ((eq part 'end-scroll))
405 (t
406 (with-current-buffer (window-buffer window)
407 (setq before-scroll point-before-scroll))
408 (save-selected-window
409 (select-window window)
410 (setq before-scroll (or before-scroll (point)))
411 (cond
412 ((eq part 'above-handle)
413 (scroll-up '-))
414 ((eq part 'below-handle)
415 (scroll-up nil))
416 ((eq part 'ratio)
417 (let* ((portion-whole (nth 2 end-position))
418 (lines (scroll-bar-scale portion-whole
419 (1- (window-height)))))
420 (scroll-up (cond ((not (zerop lines)) lines)
421 ((< (car portion-whole) 0) -1)
422 (t 1)))))
423 ((eq part 'up)
424 (scroll-up -1))
425 ((eq part 'down)
426 (scroll-up 1))
427 ((eq part 'top)
428 (set-window-start window (point-min)))
429 ((eq part 'bottom)
430 (goto-char (point-max))
431 (recenter))
432 ((eq part 'handle)
433 (scroll-bar-drag-1 event))))
434 (sit-for 0)
435 (with-current-buffer (window-buffer window)
436 (setq point-before-scroll before-scroll))))))
437
438 (defun scroll-bar-toolkit-horizontal-scroll (event)
439 "Handle event EVENT on horizontal scroll bar."
440 (interactive "e")
441 (let* ((end-position (event-end event))
442 (window (nth 0 end-position))
443 (part (nth 4 end-position))
444 (bidi-factor
445 (if (eq (current-bidi-paragraph-direction (window-buffer window))
446 'left-to-right)
447 1
448 -1))
449 before-scroll)
450 (cond
451 ((eq part 'end-scroll))
452 (t
453 (with-current-buffer (window-buffer window)
454 (setq before-scroll point-before-scroll))
455 (save-selected-window
456 (select-window window)
457 (setq before-scroll (or before-scroll (point)))
458 (cond
459 ((eq part 'before-handle)
460 (scroll-right (* bidi-factor 4)))
461 ((eq part 'after-handle)
462 (scroll-left (* bidi-factor 4)))
463 ((eq part 'ratio)
464 (let* ((portion-whole (nth 2 end-position))
465 (columns (scroll-bar-scale portion-whole
466 (1- (window-width)))))
467 (scroll-right
468 (* (cond
469 ((not (zerop columns))
470 columns)
471 ((< (car portion-whole) 0) -1)
472 (t 1))
473 bidi-factor))))
474 ((eq part 'left)
475 (scroll-right (* bidi-factor 1)))
476 ((eq part 'right)
477 (scroll-left (* bidi-factor 1)))
478 ((eq part 'leftmost)
479 (goto-char (if (eq bidi-factor 1)
480 (line-beginning-position)
481 (line-end-position))))
482 ((eq part 'rightmost)
483 (goto-char (if (eq bidi-factor 1)
484 (line-end-position)
485 (line-beginning-position))))
486 ((eq part 'horizontal-handle)
487 (scroll-bar-horizontal-drag-1 event))))
488 (sit-for 0)
489 (with-current-buffer (window-buffer window)
490 (setq point-before-scroll before-scroll))))))
491 \f
492 ;;;; Bindings.
493
494 ;; For now, we'll set things up to work like xterm.
495 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
496 (global-set-key [vertical-scroll-bar mouse-1]
497 'scroll-bar-toolkit-scroll)
498 (global-set-key [horizontal-scroll-bar mouse-1]
499 'scroll-bar-toolkit-horizontal-scroll))
500 (t
501 (global-set-key [vertical-scroll-bar mouse-1]
502 'scroll-bar-scroll-up)
503 (global-set-key [vertical-scroll-bar drag-mouse-1]
504 'scroll-bar-scroll-up)
505 (global-set-key [vertical-scroll-bar down-mouse-2]
506 'scroll-bar-drag)
507 (global-set-key [vertical-scroll-bar mouse-3]
508 'scroll-bar-scroll-down)
509 (global-set-key [vertical-scroll-bar drag-mouse-3]
510 'scroll-bar-scroll-down)))
511
512 \f
513 (provide 'scroll-bar)
514
515 ;;; scroll-bar.el ends here