lisp/*.el: Lexical-binding cleanup.
[emacs.git] / lisp / scroll-bar.el
blob54f2ba765b590f9617776d61ba9ccb4b3e5b77d5
1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993-1995, 1999-2011 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
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))
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))))
74 ;;;; Helpful functions for enabling and disabling scroll bars.
76 (defvar scroll-bar-mode)
77 (defvar previous-scroll-bar-mode nil)
79 (defvar scroll-bar-mode-explicit nil
80 "Non-nil means `set-scroll-bar-mode' should really do something.
81 This is nil while loading `scroll-bar.el', and t afterward.")
83 (defun set-scroll-bar-mode (value)
84 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
85 (if scroll-bar-mode
86 (setq previous-scroll-bar-mode scroll-bar-mode))
88 (setq scroll-bar-mode value)
90 (when scroll-bar-mode-explicit
91 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
92 scroll-bar-mode)))))
94 (defcustom scroll-bar-mode default-frame-scroll-bars
95 "Specify whether to have vertical scroll bars, and on which side.
96 Possible values are nil (no scroll bars), `left' (scroll bars on left)
97 and `right' (scroll bars on right).
98 To set this variable in a Lisp program, use `set-scroll-bar-mode'
99 to make it take real effect.
100 Setting the variable with a customization buffer also takes effect."
101 :type '(choice (const :tag "none (nil)" nil)
102 (const left)
103 (const right))
104 :group 'frames
105 ;; The default value for :initialize would try to use :set
106 ;; when processing the file in cus-dep.el.
107 :initialize 'custom-initialize-default
108 :set (lambda (_sym val) (set-scroll-bar-mode val)))
110 ;; We just set scroll-bar-mode, but that was the default.
111 ;; If it is set again, that is for real.
112 (setq scroll-bar-mode-explicit t)
114 (defun get-scroll-bar-mode () scroll-bar-mode)
115 (defsetf get-scroll-bar-mode set-scroll-bar-mode)
116 (define-minor-mode scroll-bar-mode
117 "Toggle display of vertical scroll bars on all frames.
118 This command applies to all frames that exist and frames to be
119 created in the future.
120 With a numeric argument, if the argument is positive
121 turn on scroll bars; otherwise turn off scroll bars."
122 :variable (eq (get-scroll-bar-mode)
123 (or previous-scroll-bar-mode
124 default-frame-scroll-bars)))
126 (defun toggle-scroll-bar (arg)
127 "Toggle whether or not the selected frame has vertical scroll bars.
128 With arg, turn vertical scroll bars on if and only if arg is positive.
129 The variable `scroll-bar-mode' controls which side the scroll bars are on
130 when they are turned on; if it is nil, they go on the left."
131 (interactive "P")
132 (if (null arg)
133 (setq arg
134 (if (cdr (assq 'vertical-scroll-bars
135 (frame-parameters (selected-frame))))
136 -1 1))
137 (setq arg (prefix-numeric-value arg)))
138 (modify-frame-parameters
139 (selected-frame)
140 (list (cons 'vertical-scroll-bars
141 (if (> arg 0)
142 (or scroll-bar-mode default-frame-scroll-bars))))))
144 (defun toggle-horizontal-scroll-bar (_arg)
145 "Toggle whether or not the selected frame has horizontal scroll bars.
146 With arg, turn horizontal scroll bars on if and only if arg is positive.
147 Horizontal scroll bars aren't implemented yet."
148 (interactive "P")
149 (error "Horizontal scroll bars aren't implemented yet"))
151 ;;;; Buffer navigation using the scroll bar.
153 ;; This was used for up-events on button 2, but no longer.
154 (defun scroll-bar-set-window-start (event)
155 "Set the window start according to where the scroll bar is dragged.
156 EVENT should be a scroll bar click or drag event."
157 (interactive "e")
158 (let* ((end-position (event-end event))
159 (window (nth 0 end-position))
160 (portion-whole (nth 2 end-position)))
161 (with-current-buffer (window-buffer window)
162 (save-excursion
163 (goto-char (+ (point-min)
164 (scroll-bar-scale portion-whole
165 (- (point-max) (point-min)))))
166 (beginning-of-line)
167 (set-window-start window (point))))))
169 (defun scroll-bar-drag-position (portion-whole)
170 "Calculate new window start for drag event."
171 (save-excursion
172 (goto-char (+ (point-min)
173 (scroll-bar-scale portion-whole
174 (- (point-max) (point-min)))))
175 (beginning-of-line)
176 (point)))
178 (defun scroll-bar-maybe-set-window-start (event)
179 "Set the window start according to where the scroll bar is dragged.
180 Only change window start if the new start is substantially different.
181 EVENT should be a scroll bar click or drag event."
182 (interactive "e")
183 (let* ((end-position (event-end event))
184 (window (nth 0 end-position))
185 (portion-whole (nth 2 end-position))
186 (next-portion-whole (cons (1+ (car portion-whole))
187 (cdr portion-whole)))
188 portion-start
189 next-portion-start
190 (current-start (window-start window)))
191 (with-current-buffer (window-buffer window)
192 (setq portion-start (scroll-bar-drag-position portion-whole))
193 (setq next-portion-start (max
194 (scroll-bar-drag-position next-portion-whole)
195 (1+ portion-start)))
196 (if (or (>= current-start next-portion-start)
197 (< current-start portion-start))
198 (set-window-start window portion-start)
199 ;; Always set window start, to ensure scroll bar position is updated.
200 (set-window-start window current-start)))))
202 ;; Scroll the window to the proper position for EVENT.
203 (defun scroll-bar-drag-1 (event)
204 (let* ((start-position (event-start event))
205 (window (nth 0 start-position))
206 (portion-whole (nth 2 start-position)))
207 (save-excursion
208 (with-current-buffer (window-buffer window)
209 ;; Calculate position relative to the accessible part of the buffer.
210 (goto-char (+ (point-min)
211 (scroll-bar-scale portion-whole
212 (- (point-max) (point-min)))))
213 (vertical-motion 0 window)
214 (set-window-start window (point))))))
216 (defun scroll-bar-drag (event)
217 "Scroll the window by dragging the scroll bar slider.
218 If you click outside the slider, the window scrolls to bring the slider there."
219 (interactive "e")
220 (let* (done
221 (echo-keystrokes 0)
222 (end-position (event-end event))
223 (window (nth 0 end-position))
224 (before-scroll))
225 (with-current-buffer (window-buffer window)
226 (setq before-scroll point-before-scroll))
227 (save-selected-window
228 (select-window window)
229 (setq before-scroll
230 (or before-scroll (point))))
231 (scroll-bar-drag-1 event)
232 (track-mouse
233 (while (not done)
234 (setq event (read-event))
235 (if (eq (car-safe event) 'mouse-movement)
236 (setq event (read-event)))
237 (cond ((eq (car-safe event) 'scroll-bar-movement)
238 (scroll-bar-drag-1 event))
240 ;; Exit when we get the drag event; ignore that event.
241 (setq done t)))))
242 (sit-for 0)
243 (with-current-buffer (window-buffer window)
244 (setq point-before-scroll before-scroll))))
246 (defun scroll-bar-scroll-down (event)
247 "Scroll the window's top line down to the location of the scroll bar click.
248 EVENT should be a scroll bar click."
249 (interactive "e")
250 (let* ((end-position (event-end event))
251 (window (nth 0 end-position))
252 (before-scroll))
253 (with-current-buffer (window-buffer window)
254 (setq before-scroll point-before-scroll))
255 (unwind-protect
256 (save-selected-window
257 (let ((portion-whole (nth 2 end-position)))
258 (select-window window)
259 (setq before-scroll
260 (or before-scroll (point)))
261 (scroll-down
262 (scroll-bar-scale portion-whole (1- (window-height)))))
263 (sit-for 0))
264 (with-current-buffer (window-buffer window)
265 (setq point-before-scroll before-scroll)))))
267 (defun scroll-bar-scroll-up (event)
268 "Scroll the line next to the scroll bar click to the top of the window.
269 EVENT should be a scroll bar click."
270 (interactive "e")
271 (let* ((end-position (event-end event))
272 (window (nth 0 end-position))
273 (before-scroll))
274 (with-current-buffer (window-buffer window)
275 (setq before-scroll point-before-scroll))
276 (unwind-protect
277 (save-selected-window
278 (let ((portion-whole (nth 2 end-position)))
279 (select-window window)
280 (setq before-scroll
281 (or before-scroll (point)))
282 (scroll-up
283 (scroll-bar-scale portion-whole (1- (window-height)))))
284 (sit-for 0))
285 (with-current-buffer (window-buffer window)
286 (setq point-before-scroll before-scroll)))))
289 ;;; Tookit scroll bars.
291 (defun scroll-bar-toolkit-scroll (event)
292 (interactive "e")
293 (let* ((end-position (event-end event))
294 (window (nth 0 end-position))
295 (part (nth 4 end-position))
296 before-scroll)
297 (cond ((eq part 'end-scroll))
299 (with-current-buffer (window-buffer window)
300 (setq before-scroll point-before-scroll))
301 (save-selected-window
302 (select-window window)
303 (setq before-scroll (or before-scroll (point)))
304 (cond ((eq part 'above-handle)
305 (scroll-up '-))
306 ((eq part 'below-handle)
307 (scroll-up nil))
308 ((eq part 'ratio)
309 (let* ((portion-whole (nth 2 end-position))
310 (lines (scroll-bar-scale portion-whole
311 (1- (window-height)))))
312 (scroll-up (cond ((not (zerop lines)) lines)
313 ((< (car portion-whole) 0) -1)
314 (t 1)))))
315 ((eq part 'up)
316 (scroll-up -1))
317 ((eq part 'down)
318 (scroll-up 1))
319 ((eq part 'top)
320 (set-window-start window (point-min)))
321 ((eq part 'bottom)
322 (goto-char (point-max))
323 (recenter))
324 ((eq part 'handle)
325 (scroll-bar-drag-1 event))))
326 (sit-for 0)
327 (with-current-buffer (window-buffer window)
328 (setq point-before-scroll before-scroll))))))
332 ;;;; Bindings.
334 ;; For now, we'll set things up to work like xterm.
335 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
336 (global-set-key [vertical-scroll-bar mouse-1]
337 'scroll-bar-toolkit-scroll))
339 (global-set-key [vertical-scroll-bar mouse-1]
340 'scroll-bar-scroll-up)
341 (global-set-key [vertical-scroll-bar drag-mouse-1]
342 'scroll-bar-scroll-up)
343 (global-set-key [vertical-scroll-bar down-mouse-2]
344 'scroll-bar-drag)
345 (global-set-key [vertical-scroll-bar mouse-3]
346 'scroll-bar-scroll-down)
347 (global-set-key [vertical-scroll-bar drag-mouse-3]
348 'scroll-bar-scroll-down)))
351 (provide 'scroll-bar)
353 ;;; scroll-bar.el ends here