1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993-1995, 1999-2015 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
27 ;; Presently emulates the scroll-bar behavior of xterm.
32 (eval-when-compile (require 'cl-lib
))
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."
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
))))
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))
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)
73 (defun scroll-bar-lines ()
74 "Return the height, measured in lines, of the horizontal scrollbar."
75 (let* ((wsb (window-scroll-bars))
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)
88 ;;;; Helpful functions for enabling and disabling scroll bars.
90 (defvar scroll-bar-mode
)
91 (defvar horizontal-scroll-bar-mode
)
92 (defvar previous-scroll-bar-mode nil
)
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.")
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."
102 (setq previous-scroll-bar-mode scroll-bar-mode
))
104 (setq scroll-bar-mode value
)
106 (when scroll-bar-mode-explicit
107 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
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
)
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
)))
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
)
130 (defun get-scroll-bar-mode ()
131 (declare (gv-setter set-scroll-bar-mode
))
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.
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
))))))
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 (not (eq (window-system) 'ns
))))
154 (define-minor-mode horizontal-scroll-bar-mode
155 "Toggle horizontal scroll bars on all frames (Horizontal Scroll Bar mode).
156 With a prefix argument ARG, enable Horizontal Scroll Bar mode if
157 ARG is positive, and disable it otherwise. If called from Lisp,
158 enable the mode if ARG is omitted or nil.
160 This command applies to all frames that exist and frames to be
161 created in the future."
165 (if (and horizontal-scroll-bar-mode
166 (not (horizontal-scroll-bars-available-p)))
168 (setq horizontal-scroll-bar-mode nil
)
169 (message "Horizontal scroll bars are not implemented on this system"))
170 (dolist (frame (frame-list))
172 frame
'horizontal-scroll-bars horizontal-scroll-bar-mode
))
173 ;; Handle `default-frame-alist' entry.
174 (setq default-frame-alist
175 (cons (cons 'horizontal-scroll-bars horizontal-scroll-bar-mode
)
176 (assq-delete-all 'horizontal-scroll-bars
177 default-frame-alist
)))))
179 (defun toggle-scroll-bar (arg)
180 "Toggle whether or not the selected frame has vertical scroll bars.
181 With ARG, turn vertical scroll bars on if and only if ARG is positive.
182 The variable `scroll-bar-mode' controls which side the scroll bars are on
183 when they are turned on; if it is nil, they go on the left."
187 (if (cdr (assq 'vertical-scroll-bars
188 (frame-parameters (selected-frame))))
190 (setq arg
(prefix-numeric-value arg
)))
191 (modify-frame-parameters
193 (list (cons 'vertical-scroll-bars
195 (or scroll-bar-mode default-frame-scroll-bars
))))))
197 (defun toggle-horizontal-scroll-bar (arg)
198 "Toggle whether or not the selected frame has horizontal scroll bars.
199 With ARG, turn vertical scroll bars on if and only if ARG is positive."
203 (if (cdr (assq 'horizontal-scroll-bars
204 (frame-parameters (selected-frame))))
206 (setq arg
(prefix-numeric-value arg
)))
207 (modify-frame-parameters
209 (list (cons 'horizontal-scroll-bars
210 (when (> arg
0) 'bottom
)))))
212 ;;;; Buffer navigation using the scroll bar.
214 ;; This was used for up-events on button 2, but no longer.
215 (defun scroll-bar-set-window-start (event)
216 "Set the window start according to where the scroll bar is dragged.
217 EVENT should be a scroll bar click or drag event."
219 (let* ((end-position (event-end event
))
220 (window (nth 0 end-position
))
221 (portion-whole (nth 2 end-position
)))
222 (with-current-buffer (window-buffer window
)
224 (goto-char (+ (point-min)
225 (scroll-bar-scale portion-whole
226 (- (point-max) (point-min)))))
228 (set-window-start window
(point))))))
230 (defun scroll-bar-drag-position (portion-whole)
231 "Calculate new window start for drag event."
233 (goto-char (+ (point-min)
234 (scroll-bar-scale portion-whole
235 (- (point-max) (point-min)))))
239 (defun scroll-bar-maybe-set-window-start (event)
240 "Set the window start according to where the scroll bar is dragged.
241 Only change window start if the new start is substantially different.
242 EVENT should be a scroll bar click or drag event."
244 (let* ((end-position (event-end event
))
245 (window (nth 0 end-position
))
246 (portion-whole (nth 2 end-position
))
247 (next-portion-whole (cons (1+ (car portion-whole
))
248 (cdr portion-whole
)))
251 (current-start (window-start window
)))
252 (with-current-buffer (window-buffer window
)
253 (setq portion-start
(scroll-bar-drag-position portion-whole
))
254 (setq next-portion-start
(max
255 (scroll-bar-drag-position next-portion-whole
)
257 (if (or (>= current-start next-portion-start
)
258 (< current-start portion-start
))
259 (set-window-start window portion-start
)
260 ;; Always set window start, to ensure scroll bar position is updated.
261 (set-window-start window current-start
)))))
263 ;; Scroll the window to the proper position for EVENT.
264 (defun scroll-bar-drag-1 (event)
265 (let* ((start-position (event-start event
))
266 (window (nth 0 start-position
))
267 (portion-whole (nth 2 start-position
)))
269 (with-current-buffer (window-buffer window
)
270 ;; Calculate position relative to the accessible part of the buffer.
271 (goto-char (+ (point-min)
272 (scroll-bar-scale portion-whole
273 (- (point-max) (point-min)))))
274 (vertical-motion 0 window
)
275 (set-window-start window
(point))))))
277 (defun scroll-bar-drag (event)
278 "Scroll the window by dragging the scroll bar slider.
279 If you click outside the slider, the window scrolls to bring the slider there."
283 (end-position (event-end event
))
284 (window (nth 0 end-position
))
286 (with-current-buffer (window-buffer window
)
287 (setq before-scroll point-before-scroll
))
288 (save-selected-window
289 (select-window window
)
291 (or before-scroll
(point))))
292 (scroll-bar-drag-1 event
)
295 (setq event
(read-event))
296 (if (eq (car-safe event
) 'mouse-movement
)
297 (setq event
(read-event)))
298 (cond ((eq (car-safe event
) 'scroll-bar-movement
)
299 (scroll-bar-drag-1 event
))
301 ;; Exit when we get the drag event; ignore that event.
304 (with-current-buffer (window-buffer window
)
305 (setq point-before-scroll before-scroll
))))
307 ;; Scroll the window to the proper position for EVENT.
308 (defun scroll-bar-horizontal-drag-1 (event)
309 (let* ((start-position (event-start event
))
310 (window (nth 0 start-position
))
311 (portion-whole (nth 2 start-position
))
312 (unit (frame-char-width (window-frame window
))))
313 (if (eq (current-bidi-paragraph-direction (window-buffer window
))
316 window
(/ (+ (car portion-whole
) (1- unit
)) unit
))
318 window
(/ (+ (- (cdr portion-whole
) (car portion-whole
))
322 (defun scroll-bar-horizontal-drag (event)
323 "Scroll the window horizontally by dragging the scroll bar slider.
324 If you click outside the slider, the window scrolls to bring the slider there."
328 (end-position (event-end event
))
329 (window (nth 0 end-position
))
331 (with-current-buffer (window-buffer window
)
332 (setq before-scroll point-before-scroll
))
333 (save-selected-window
334 (select-window window
)
336 (or before-scroll
(point))))
337 (scroll-bar-horizontal-drag-1 event
)
340 (setq event
(read-event))
341 (if (eq (car-safe event
) 'mouse-movement
)
342 (setq event
(read-event)))
343 (cond ((eq (car-safe event
) 'scroll-bar-movement
)
344 (scroll-bar-horizontal-drag-1 event
))
346 ;; Exit when we get the drag event; ignore that event.
349 (with-current-buffer (window-buffer window
)
350 (setq point-before-scroll before-scroll
))))
352 (defun scroll-bar-scroll-down (event)
353 "Scroll the window's top line down to the location of the scroll bar click.
354 EVENT should be a scroll bar click."
356 (let* ((end-position (event-end event
))
357 (window (nth 0 end-position
))
359 (with-current-buffer (window-buffer window
)
360 (setq before-scroll point-before-scroll
))
362 (save-selected-window
363 (let ((portion-whole (nth 2 end-position
)))
364 (select-window window
)
366 (or before-scroll
(point)))
368 (scroll-bar-scale portion-whole
(1- (window-height)))))
370 (with-current-buffer (window-buffer window
)
371 (setq point-before-scroll before-scroll
)))))
373 (defun scroll-bar-scroll-up (event)
374 "Scroll the line next to the scroll bar click to the top of the window.
375 EVENT should be a scroll bar click."
377 (let* ((end-position (event-end event
))
378 (window (nth 0 end-position
))
380 (with-current-buffer (window-buffer window
)
381 (setq before-scroll point-before-scroll
))
383 (save-selected-window
384 (let ((portion-whole (nth 2 end-position
)))
385 (select-window window
)
387 (or before-scroll
(point)))
389 (scroll-bar-scale portion-whole
(1- (window-height)))))
391 (with-current-buffer (window-buffer window
)
392 (setq point-before-scroll before-scroll
)))))
395 ;;; Tookit scroll bars.
397 (defun scroll-bar-toolkit-scroll (event)
398 "Handle event EVENT on vertical scroll bar."
400 (let* ((end-position (event-end event
))
401 (window (nth 0 end-position
))
402 (part (nth 4 end-position
))
405 ((eq part
'end-scroll
))
407 (with-current-buffer (window-buffer window
)
408 (setq before-scroll point-before-scroll
))
409 (save-selected-window
410 (select-window window
)
411 (setq before-scroll
(or before-scroll
(point)))
413 ((eq part
'above-handle
)
415 ((eq part
'below-handle
)
418 (let* ((portion-whole (nth 2 end-position
))
419 (lines (scroll-bar-scale portion-whole
420 (1- (window-height)))))
421 (scroll-up (cond ((not (zerop lines
)) lines
)
422 ((< (car portion-whole
) 0) -
1)
429 (set-window-start window
(point-min)))
431 (goto-char (point-max))
434 (scroll-bar-drag-1 event
))))
436 (with-current-buffer (window-buffer window
)
437 (setq point-before-scroll before-scroll
))))))
439 (defun scroll-bar-toolkit-horizontal-scroll (event)
440 "Handle event EVENT on horizontal scroll bar."
442 (let* ((end-position (event-end event
))
443 (window (nth 0 end-position
))
444 (part (nth 4 end-position
))
446 (if (eq (current-bidi-paragraph-direction (window-buffer window
))
452 ((eq part
'end-scroll
))
454 (with-current-buffer (window-buffer window
)
455 (setq before-scroll point-before-scroll
))
456 (save-selected-window
457 (select-window window
)
458 (setq before-scroll
(or before-scroll
(point)))
460 ((eq part
'before-handle
)
461 (scroll-right (* bidi-factor
4)))
462 ((eq part
'after-handle
)
463 (scroll-left (* bidi-factor
4)))
465 (let* ((portion-whole (nth 2 end-position
))
466 (columns (scroll-bar-scale portion-whole
467 (1- (window-width)))))
470 ((not (zerop columns
))
472 ((< (car portion-whole
) 0) -
1)
476 (scroll-right (* bidi-factor
1)))
478 (scroll-left (* bidi-factor
1)))
480 (goto-char (if (eq bidi-factor
1)
481 (line-beginning-position)
482 (line-end-position))))
483 ((eq part
'rightmost
)
484 (goto-char (if (eq bidi-factor
1)
486 (line-beginning-position))))
487 ((eq part
'horizontal-handle
)
488 (scroll-bar-horizontal-drag-1 event
))))
490 (with-current-buffer (window-buffer window
)
491 (setq point-before-scroll before-scroll
))))))
495 ;; For now, we'll set things up to work like xterm.
496 (cond ((and (boundp 'x-toolkit-scroll-bars
) x-toolkit-scroll-bars
)
497 (global-set-key [vertical-scroll-bar mouse-1
]
498 'scroll-bar-toolkit-scroll
)
499 (global-set-key [horizontal-scroll-bar mouse-1
]
500 'scroll-bar-toolkit-horizontal-scroll
))
502 (global-set-key [vertical-scroll-bar mouse-1
]
503 'scroll-bar-scroll-up
)
504 (global-set-key [vertical-scroll-bar drag-mouse-1
]
505 'scroll-bar-scroll-up
)
506 (global-set-key [vertical-scroll-bar down-mouse-2
]
508 (global-set-key [vertical-scroll-bar mouse-3
]
509 'scroll-bar-scroll-down
)
510 (global-set-key [vertical-scroll-bar drag-mouse-3
]
511 'scroll-bar-scroll-down
)))
514 (provide 'scroll-bar
)
516 ;;; scroll-bar.el ends here