1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993-1995, 1999-2016 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
))
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.
159 This command applies to all frames that exist and frames to be
160 created in the future."
164 (if (and horizontal-scroll-bar-mode
165 (not (horizontal-scroll-bars-available-p)))
167 (setq horizontal-scroll-bar-mode nil
)
168 (message "Horizontal scroll bars are not implemented on this system"))
169 (dolist (frame (frame-list))
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
)))))
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."
186 (if (cdr (assq 'vertical-scroll-bars
187 (frame-parameters (selected-frame))))
189 (setq arg
(prefix-numeric-value arg
)))
190 (modify-frame-parameters
192 (list (cons 'vertical-scroll-bars
194 (or scroll-bar-mode default-frame-scroll-bars
))))))
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."
202 (if (cdr (assq 'horizontal-scroll-bars
203 (frame-parameters (selected-frame))))
205 (setq arg
(prefix-numeric-value arg
)))
206 (modify-frame-parameters
208 (list (cons 'horizontal-scroll-bars
209 (when (> arg
0) 'bottom
)))))
211 ;;;; Buffer navigation using the scroll bar.
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."
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
)
223 (goto-char (+ (point-min)
224 (scroll-bar-scale portion-whole
225 (- (point-max) (point-min)))))
227 (set-window-start window
(point))))))
229 (defun scroll-bar-drag-position (portion-whole)
230 "Calculate new window start for drag event."
232 (goto-char (+ (point-min)
233 (scroll-bar-scale portion-whole
234 (- (point-max) (point-min)))))
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."
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
)))
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
)
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
)))))
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
)))
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))))))
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."
282 (end-position (event-end event
))
283 (window (nth 0 end-position
))
285 (with-current-buffer (window-buffer window
)
286 (setq before-scroll point-before-scroll
))
287 (save-selected-window
288 (select-window window
)
290 (or before-scroll
(point))))
291 (scroll-bar-drag-1 event
)
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
))
300 ;; Exit when we get the drag event; ignore that event.
303 (with-current-buffer (window-buffer window
)
304 (setq point-before-scroll before-scroll
))))
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
))
315 window
(/ (+ (car portion-whole
) (1- unit
)) unit
))
317 window
(/ (+ (- (cdr portion-whole
) (car portion-whole
))
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."
327 (end-position (event-end event
))
328 (window (nth 0 end-position
))
330 (with-current-buffer (window-buffer window
)
331 (setq before-scroll point-before-scroll
))
332 (save-selected-window
333 (select-window window
)
335 (or before-scroll
(point))))
336 (scroll-bar-horizontal-drag-1 event
)
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
))
345 ;; Exit when we get the drag event; ignore that event.
348 (with-current-buffer (window-buffer window
)
349 (setq point-before-scroll before-scroll
))))
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."
355 (let* ((end-position (event-end event
))
356 (window (nth 0 end-position
))
358 (with-current-buffer (window-buffer window
)
359 (setq before-scroll point-before-scroll
))
361 (save-selected-window
362 (let ((portion-whole (nth 2 end-position
)))
363 (select-window window
)
365 (or before-scroll
(point)))
367 (scroll-bar-scale portion-whole
(1- (window-height)))))
369 (with-current-buffer (window-buffer window
)
370 (setq point-before-scroll before-scroll
)))))
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."
376 (let* ((end-position (event-end event
))
377 (window (nth 0 end-position
))
379 (with-current-buffer (window-buffer window
)
380 (setq before-scroll point-before-scroll
))
382 (save-selected-window
383 (let ((portion-whole (nth 2 end-position
)))
384 (select-window window
)
386 (or before-scroll
(point)))
388 (scroll-bar-scale portion-whole
(1- (window-height)))))
390 (with-current-buffer (window-buffer window
)
391 (setq point-before-scroll before-scroll
)))))
394 ;;; Tookit scroll bars.
396 (defun scroll-bar-toolkit-scroll (event)
397 "Handle event EVENT on vertical scroll bar."
399 (let* ((end-position (event-end event
))
400 (window (nth 0 end-position
))
401 (part (nth 4 end-position
))
404 ((eq part
'end-scroll
))
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)))
412 ((eq part
'above-handle
)
414 ((eq part
'below-handle
)
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)
428 (set-window-start window
(point-min)))
430 (goto-char (point-max))
433 (scroll-bar-drag-1 event
))))
435 (with-current-buffer (window-buffer window
)
436 (setq point-before-scroll before-scroll
))))))
438 (defun scroll-bar-toolkit-horizontal-scroll (event)
439 "Handle event EVENT on horizontal scroll bar."
441 (let* ((end-position (event-end event
))
442 (window (nth 0 end-position
))
443 (part (nth 4 end-position
))
445 (if (eq (current-bidi-paragraph-direction (window-buffer window
))
451 ((eq part
'end-scroll
))
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)))
459 ((eq part
'before-handle
)
460 (scroll-right (* bidi-factor
4)))
461 ((eq part
'after-handle
)
462 (scroll-left (* bidi-factor
4)))
464 (let* ((portion-whole (nth 2 end-position
))
465 (columns (scroll-bar-scale portion-whole
466 (1- (window-width)))))
469 ((not (zerop columns
))
471 ((< (car portion-whole
) 0) -
1)
475 (scroll-right (* bidi-factor
1)))
477 (scroll-left (* bidi-factor
1)))
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)
485 (line-beginning-position))))
486 ((eq part
'horizontal-handle
)
487 (scroll-bar-horizontal-drag-1 event
))))
489 (with-current-buffer (window-buffer window
)
490 (setq point-before-scroll before-scroll
))))))
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
))
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
]
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
)))
513 (provide 'scroll-bar
)
515 ;;; scroll-bar.el ends here