In window--min-size-1 set WINDOW arg when calling window-min-pixel-height/-width.
[emacs.git] / lisp / scroll-bar.el
blob739670cb1c93437acaf193562f828aa3317799de
1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993-1995, 1999-2014 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: hardware
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
27 ;; Presently emulates the scroll-bar behavior of xterm.
29 ;;; Code:
31 (require 'mouse)
32 (eval-when-compile (require 'cl-lib))
35 ;;;; Utilities.
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))
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))
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))))
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 width in columns is 14 pixels rounded up.
83 (ceiling (or (frame-parameter nil 'scroll-bar-height) 14)
84 (frame-char-width)))
85 (0))))
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)
93 (defvar previous-horizontal-scroll-bar-mode nil)
95 (defvar scroll-bar-mode-explicit nil
96 "Non-nil means `set-scroll-bar-mode' should really do something.
97 This is nil while loading `scroll-bar.el', and t afterward.")
99 (defvar horizontal-scroll-bar-mode-explicit nil
100 "Non-nil means `set-horizontal-scroll-bar-mode' should really do something.
101 This is nil while loading `scroll-bar.el', and t afterward.")
103 (defun set-scroll-bar-mode (value)
104 "Set the scroll bar mode to VALUE and put the new value into effect.
105 See the `scroll-bar-mode' variable for possible values to use."
106 (if scroll-bar-mode
107 (setq previous-scroll-bar-mode scroll-bar-mode))
109 (setq scroll-bar-mode value)
111 (when scroll-bar-mode-explicit
112 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
113 scroll-bar-mode)))))
115 (defun set-horizontal-scroll-bar-mode (value)
116 "Set the horizontal scroll bar mode to VALUE and put the new value into effect.
117 See the `horizontal-scroll-bar-mode' variable for possible values to use."
118 (if horizontal-scroll-bar-mode
119 (setq previous-horizontal-scroll-bar-mode horizontal-scroll-bar-mode))
121 (setq horizontal-scroll-bar-mode value)
123 (when horizontal-scroll-bar-mode-explicit
124 (modify-all-frames-parameters (list (cons 'horizontal-scroll-bars
125 horizontal-scroll-bar-mode)))))
127 (defcustom scroll-bar-mode default-frame-scroll-bars
128 "Specify whether to have vertical scroll bars, and on which side.
129 Possible values are nil (no scroll bars), `left' (scroll bars on left)
130 and `right' (scroll bars on right).
131 To set this variable in a Lisp program, use `set-scroll-bar-mode'
132 to make it take real effect.
133 Setting the variable with a customization buffer also takes effect."
134 :type '(choice (const :tag "none (nil)" nil)
135 (const left)
136 (const right))
137 :group 'frames
138 ;; The default value for :initialize would try to use :set
139 ;; when processing the file in cus-dep.el.
140 :initialize 'custom-initialize-default
141 :set (lambda (_sym val) (set-scroll-bar-mode val)))
143 (defcustom horizontal-scroll-bar-mode default-frame-horizontal-scroll-bars
144 "Specify whether to have horizontal scroll bars, and on which side.
145 To set this variable in a Lisp program, use `set-horizontal-scroll-bar-mode'
146 to make it take real effect.
147 Setting the variable with a customization buffer also takes effect."
148 :type '(choice (const :tag "none (nil)" nil)
149 (const t))
150 :group 'frames
151 ;; The default value for :initialize would try to use :set
152 ;; when processing the file in cus-dep.el.
153 :initialize 'custom-initialize-default
154 :set (lambda (_sym val) (set-horizontal-scroll-bar-mode val)))
156 ;; We just set scroll-bar-mode, but that was the default.
157 ;; If it is set again, that is for real.
158 (setq scroll-bar-mode-explicit t)
159 (setq horizontal-scroll-bar-mode-explicit t)
161 (defun get-scroll-bar-mode ()
162 (declare (gv-setter set-scroll-bar-mode))
163 scroll-bar-mode)
165 (defun get-horizontal-scroll-bar-mode ()
166 (declare (gv-setter set-horizontal-scroll-bar-mode))
167 horizontal-scroll-bar-mode)
169 (define-minor-mode scroll-bar-mode
170 "Toggle vertical scroll bars on all frames (Scroll Bar mode).
171 With a prefix argument ARG, enable Scroll Bar mode if ARG is
172 positive, and disable it otherwise. If called from Lisp, enable
173 the mode if ARG is omitted or nil.
175 This command applies to all frames that exist and frames to be
176 created in the future."
177 :variable ((get-scroll-bar-mode)
178 . (lambda (v) (set-scroll-bar-mode
179 (if v (or previous-scroll-bar-mode
180 default-frame-scroll-bars))))))
182 (define-minor-mode horizontal-scroll-bar-mode
183 "Toggle horizontal scroll bars on all frames (Horizontal Scroll Bar mode).
184 With a prefix argument ARG, enable Horizontal Scroll Bar mode if
185 ARG is positive, and disable it otherwise. If called from Lisp,
186 enable the mode if ARG is omitted or nil.
188 This command applies to all frames that exist and frames to be
189 created in the future."
190 :variable ((get-horizontal-scroll-bar-mode)
191 . (lambda (v) (set-horizontal-scroll-bar-mode
192 (if v (or previous-scroll-bar-mode
193 default-frame-horizontal-scroll-bars))))))
195 (defun toggle-scroll-bar (arg)
196 "Toggle whether or not the selected frame has vertical scroll bars.
197 With arg, turn vertical scroll bars on if and only if arg is positive.
198 The variable `scroll-bar-mode' controls which side the scroll bars are on
199 when they are turned on; if it is nil, they go on the left."
200 (interactive "P")
201 (if (null arg)
202 (setq arg
203 (if (cdr (assq 'vertical-scroll-bars
204 (frame-parameters (selected-frame))))
205 -1 1))
206 (setq arg (prefix-numeric-value arg)))
207 (modify-frame-parameters
208 (selected-frame)
209 (list (cons 'vertical-scroll-bars
210 (if (> arg 0)
211 (or scroll-bar-mode default-frame-scroll-bars))))))
213 (defun toggle-horizontal-scroll-bar (arg)
214 "Toggle whether or not the selected frame has horizontal scroll bars.
215 With arg, turn horizontal scroll bars on if and only if arg is positive."
216 (interactive "P")
217 (if (null arg)
218 (setq arg
219 (if (cdr (assq 'horizontal-scroll-bars
220 (frame-parameters (selected-frame))))
221 -1 1))
222 (setq arg (prefix-numeric-value arg)))
223 (modify-frame-parameters
224 (selected-frame)
225 (list (cons 'horizontal-scroll-bars
226 (if (> arg 0)
227 (or horizontal-scroll-bar-mode default-frame-horizontal-scroll-bars))))))
229 ;;;; Buffer navigation using the scroll bar.
231 ;; This was used for up-events on button 2, but no longer.
232 (defun scroll-bar-set-window-start (event)
233 "Set the window start according to where the scroll bar is dragged.
234 EVENT should be a scroll bar click or drag event."
235 (interactive "e")
236 (let* ((end-position (event-end event))
237 (window (nth 0 end-position))
238 (portion-whole (nth 2 end-position)))
239 (with-current-buffer (window-buffer window)
240 (save-excursion
241 (goto-char (+ (point-min)
242 (scroll-bar-scale portion-whole
243 (- (point-max) (point-min)))))
244 (beginning-of-line)
245 (set-window-start window (point))))))
247 (defun scroll-bar-drag-position (portion-whole)
248 "Calculate new window start for drag event."
249 (save-excursion
250 (goto-char (+ (point-min)
251 (scroll-bar-scale portion-whole
252 (- (point-max) (point-min)))))
253 (beginning-of-line)
254 (point)))
256 (defun scroll-bar-maybe-set-window-start (event)
257 "Set the window start according to where the scroll bar is dragged.
258 Only change window start if the new start is substantially different.
259 EVENT should be a scroll bar click or drag event."
260 (interactive "e")
261 (let* ((end-position (event-end event))
262 (window (nth 0 end-position))
263 (portion-whole (nth 2 end-position))
264 (next-portion-whole (cons (1+ (car portion-whole))
265 (cdr portion-whole)))
266 portion-start
267 next-portion-start
268 (current-start (window-start window)))
269 (with-current-buffer (window-buffer window)
270 (setq portion-start (scroll-bar-drag-position portion-whole))
271 (setq next-portion-start (max
272 (scroll-bar-drag-position next-portion-whole)
273 (1+ portion-start)))
274 (if (or (>= current-start next-portion-start)
275 (< current-start portion-start))
276 (set-window-start window portion-start)
277 ;; Always set window start, to ensure scroll bar position is updated.
278 (set-window-start window current-start)))))
280 ;; Scroll the window to the proper position for EVENT.
281 (defun scroll-bar-drag-1 (event)
282 (let* ((start-position (event-start event))
283 (window (nth 0 start-position))
284 (portion-whole (nth 2 start-position)))
285 (save-excursion
286 (with-current-buffer (window-buffer window)
287 ;; Calculate position relative to the accessible part of the buffer.
288 (goto-char (+ (point-min)
289 (scroll-bar-scale portion-whole
290 (- (point-max) (point-min)))))
291 (vertical-motion 0 window)
292 (set-window-start window (point))))))
294 (defun scroll-bar-drag (event)
295 "Scroll the window by dragging the scroll bar slider.
296 If you click outside the slider, the window scrolls to bring the slider there."
297 (interactive "e")
298 (let* (done
299 (echo-keystrokes 0)
300 (end-position (event-end event))
301 (window (nth 0 end-position))
302 (before-scroll))
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
308 (or before-scroll (point))))
309 (scroll-bar-drag-1 event)
310 (track-mouse
311 (while (not done)
312 (setq event (read-event))
313 (if (eq (car-safe event) 'mouse-movement)
314 (setq event (read-event)))
315 (cond ((eq (car-safe event) 'scroll-bar-movement)
316 (scroll-bar-drag-1 event))
318 ;; Exit when we get the drag event; ignore that event.
319 (setq done t)))))
320 (sit-for 0)
321 (with-current-buffer (window-buffer window)
322 (setq point-before-scroll before-scroll))))
324 ;; Scroll the window to the proper position for EVENT.
325 (defun scroll-bar-horizontal-drag-1 (event)
326 (let* ((start-position (event-start event))
327 (window (nth 0 start-position))
328 (portion-whole (nth 2 start-position))
329 (unit (frame-char-width (window-frame window))))
330 (set-window-hscroll
331 window (/ (1- (+ (car portion-whole) unit)) unit))))
333 (defun scroll-bar-horizontal-drag (event)
334 "Scroll the window horizontally by dragging the scroll bar slider.
335 If you click outside the slider, the window scrolls to bring the slider there."
336 (interactive "e")
337 (let* (done
338 (echo-keystrokes 0)
339 (end-position (event-end event))
340 (window (nth 0 end-position))
341 (before-scroll))
342 (with-current-buffer (window-buffer window)
343 (setq before-scroll point-before-scroll))
344 (save-selected-window
345 (select-window window)
346 (setq before-scroll
347 (or before-scroll (point))))
348 (scroll-bar-horizontal-drag-1 event)
349 (track-mouse
350 (while (not done)
351 (setq event (read-event))
352 (if (eq (car-safe event) 'mouse-movement)
353 (setq event (read-event)))
354 (cond ((eq (car-safe event) 'scroll-bar-movement)
355 (scroll-bar-horizontal-drag-1 event))
357 ;; Exit when we get the drag event; ignore that event.
358 (setq done t)))))
359 (sit-for 0)
360 (with-current-buffer (window-buffer window)
361 (setq point-before-scroll before-scroll))))
363 (defun scroll-bar-scroll-down (event)
364 "Scroll the window's top line down to the location of the scroll bar click.
365 EVENT should be a scroll bar click."
366 (interactive "e")
367 (let* ((end-position (event-end event))
368 (window (nth 0 end-position))
369 (before-scroll))
370 (with-current-buffer (window-buffer window)
371 (setq before-scroll point-before-scroll))
372 (unwind-protect
373 (save-selected-window
374 (let ((portion-whole (nth 2 end-position)))
375 (select-window window)
376 (setq before-scroll
377 (or before-scroll (point)))
378 (scroll-down
379 (scroll-bar-scale portion-whole (1- (window-height)))))
380 (sit-for 0))
381 (with-current-buffer (window-buffer window)
382 (setq point-before-scroll before-scroll)))))
384 (defun scroll-bar-scroll-up (event)
385 "Scroll the line next to the scroll bar click to the top of the window.
386 EVENT should be a scroll bar click."
387 (interactive "e")
388 (let* ((end-position (event-end event))
389 (window (nth 0 end-position))
390 (before-scroll))
391 (with-current-buffer (window-buffer window)
392 (setq before-scroll point-before-scroll))
393 (unwind-protect
394 (save-selected-window
395 (let ((portion-whole (nth 2 end-position)))
396 (select-window window)
397 (setq before-scroll
398 (or before-scroll (point)))
399 (scroll-up
400 (scroll-bar-scale portion-whole (1- (window-height)))))
401 (sit-for 0))
402 (with-current-buffer (window-buffer window)
403 (setq point-before-scroll before-scroll)))))
406 ;;; Tookit scroll bars.
408 (defun scroll-bar-toolkit-scroll (event)
409 (interactive "e")
410 (let* ((end-position (event-end event))
411 (window (nth 0 end-position))
412 (part (nth 4 end-position))
413 before-scroll)
414 (cond
415 ((eq part 'end-scroll))
417 (with-current-buffer (window-buffer window)
418 (setq before-scroll point-before-scroll))
419 (save-selected-window
420 (select-window window)
421 (setq before-scroll (or before-scroll (point)))
422 (cond
423 ((eq part 'above-handle)
424 (scroll-up '-))
425 ((eq part 'below-handle)
426 (scroll-up nil))
427 ((eq part 'ratio)
428 (let* ((portion-whole (nth 2 end-position))
429 (lines (scroll-bar-scale portion-whole
430 (1- (window-height)))))
431 (scroll-up (cond ((not (zerop lines)) lines)
432 ((< (car portion-whole) 0) -1)
433 (t 1)))))
434 ((eq part 'up)
435 (scroll-up -1))
436 ((eq part 'down)
437 (scroll-up 1))
438 ((eq part 'top)
439 (set-window-start window (point-min)))
440 ((eq part 'bottom)
441 (goto-char (point-max))
442 (recenter))
443 ((eq part 'handle)
444 (scroll-bar-drag-1 event))))
445 (sit-for 0)
446 (with-current-buffer (window-buffer window)
447 (setq point-before-scroll before-scroll))))))
449 (defun scroll-bar-toolkit-horizontal-scroll (event)
450 (interactive "e")
451 (let* ((end-position (event-end event))
452 (window (nth 0 end-position))
453 (part (nth 4 end-position))
454 (bidi-factor (if (eq (current-bidi-paragraph-direction) 'left-to-right)
456 -1))
457 before-scroll)
458 (cond
459 ((eq part 'end-scroll))
461 (with-current-buffer (window-buffer window)
462 (setq before-scroll point-before-scroll))
463 (save-selected-window
464 (select-window window)
465 (setq before-scroll (or before-scroll (point)))
466 (cond
467 ((eq part 'before-handle)
468 (scroll-right (* bidi-factor 4)))
469 ((eq part 'after-handle)
470 (scroll-left (* bidi-factor 4)))
471 ((eq part 'ratio)
472 (let* ((portion-whole (nth 2 end-position))
473 (columns (scroll-bar-scale portion-whole
474 (1- (window-width)))))
475 (scroll-right
476 (* (cond
477 ((not (zerop columns))
478 columns)
479 ((< (car portion-whole) 0) -1)
480 (t 1))
481 bidi-factor))))
482 ((eq part 'left)
483 (scroll-right (* bidi-factor 1)))
484 ((eq part 'right)
485 (scroll-left (* bidi-factor 1)))
486 ((eq part 'leftmost)
487 (goto-char (if (eq bidi-factor 1)
488 (line-beginning-position)
489 (line-end-position))))
490 ((eq part 'rightmost)
491 (goto-char (if (eq bidi-factor 1)
492 (line-end-position)
493 (line-beginning-position))))
494 ((eq part 'horizontal-handle)
495 (scroll-bar-horizontal-drag-1 event))))
496 (sit-for 0)
497 (with-current-buffer (window-buffer window)
498 (setq point-before-scroll before-scroll))))))
500 ;;;; Bindings.
502 ;; For now, we'll set things up to work like xterm.
503 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
504 (global-set-key [vertical-scroll-bar mouse-1]
505 'scroll-bar-toolkit-scroll)
506 (global-set-key [horizontal-scroll-bar mouse-1]
507 'scroll-bar-toolkit-horizontal-scroll))
509 (global-set-key [vertical-scroll-bar mouse-1]
510 'scroll-bar-scroll-up)
511 (global-set-key [vertical-scroll-bar drag-mouse-1]
512 'scroll-bar-scroll-up)
513 (global-set-key [vertical-scroll-bar down-mouse-2]
514 'scroll-bar-drag)
515 (global-set-key [vertical-scroll-bar mouse-3]
516 'scroll-bar-scroll-down)
517 (global-set-key [vertical-scroll-bar drag-mouse-3]
518 'scroll-bar-scroll-down)))
521 (provide 'scroll-bar)
523 ;;; scroll-bar.el ends here