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