]> code.delx.au - gnu-emacs/blob - lisp/scroll-bar.el
Merge from emacs-24; up to 2014-07-02T00:57:53Z!juri@jurta.org
[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 (defvar previous-horizontal-scroll-bar-mode nil)
94
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.")
98
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.")
102
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))
108
109 (setq scroll-bar-mode value)
110
111 (when scroll-bar-mode-explicit
112 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
113 scroll-bar-mode)))))
114
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))
120
121 (setq horizontal-scroll-bar-mode value)
122
123 (when horizontal-scroll-bar-mode-explicit
124 (modify-all-frames-parameters (list (cons 'horizontal-scroll-bars
125 horizontal-scroll-bar-mode)))))
126
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)))
142
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)))
155
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)
160
161 (defun get-scroll-bar-mode ()
162 (declare (gv-setter set-scroll-bar-mode))
163 scroll-bar-mode)
164
165 (defun get-horizontal-scroll-bar-mode ()
166 (declare (gv-setter set-horizontal-scroll-bar-mode))
167 horizontal-scroll-bar-mode)
168
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.
174
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))))))
181
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.
187
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))))))
194
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))))))
212
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))))))
228 \f
229 ;;;; Buffer navigation using the scroll bar.
230
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))))))
246
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)))
255
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)))))
279
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))))))
293
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))
317 (t
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))))
323
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 (if (eq (current-bidi-paragraph-direction (window-buffer window))
331 'left-to-right)
332 (set-window-hscroll
333 window (/ (1- (+ (car portion-whole) unit)) unit))
334 (set-window-hscroll
335 window (/ (1- (+ (cdr portion-whole) unit)) unit)))))
336
337 (defun scroll-bar-horizontal-drag (event)
338 "Scroll the window horizontally by dragging the scroll bar slider.
339 If you click outside the slider, the window scrolls to bring the slider there."
340 (interactive "e")
341 (let* (done
342 (echo-keystrokes 0)
343 (end-position (event-end event))
344 (window (nth 0 end-position))
345 (before-scroll))
346 (with-current-buffer (window-buffer window)
347 (setq before-scroll point-before-scroll))
348 (save-selected-window
349 (select-window window)
350 (setq before-scroll
351 (or before-scroll (point))))
352 (scroll-bar-horizontal-drag-1 event)
353 (track-mouse
354 (while (not done)
355 (setq event (read-event))
356 (if (eq (car-safe event) 'mouse-movement)
357 (setq event (read-event)))
358 (cond ((eq (car-safe event) 'scroll-bar-movement)
359 (scroll-bar-horizontal-drag-1 event))
360 (t
361 ;; Exit when we get the drag event; ignore that event.
362 (setq done t)))))
363 (sit-for 0)
364 (with-current-buffer (window-buffer window)
365 (setq point-before-scroll before-scroll))))
366
367 (defun scroll-bar-scroll-down (event)
368 "Scroll the window's top line down to the location of the scroll bar click.
369 EVENT should be a scroll bar click."
370 (interactive "e")
371 (let* ((end-position (event-end event))
372 (window (nth 0 end-position))
373 (before-scroll))
374 (with-current-buffer (window-buffer window)
375 (setq before-scroll point-before-scroll))
376 (unwind-protect
377 (save-selected-window
378 (let ((portion-whole (nth 2 end-position)))
379 (select-window window)
380 (setq before-scroll
381 (or before-scroll (point)))
382 (scroll-down
383 (scroll-bar-scale portion-whole (1- (window-height)))))
384 (sit-for 0))
385 (with-current-buffer (window-buffer window)
386 (setq point-before-scroll before-scroll)))))
387
388 (defun scroll-bar-scroll-up (event)
389 "Scroll the line next to the scroll bar click to the top of the window.
390 EVENT should be a scroll bar click."
391 (interactive "e")
392 (let* ((end-position (event-end event))
393 (window (nth 0 end-position))
394 (before-scroll))
395 (with-current-buffer (window-buffer window)
396 (setq before-scroll point-before-scroll))
397 (unwind-protect
398 (save-selected-window
399 (let ((portion-whole (nth 2 end-position)))
400 (select-window window)
401 (setq before-scroll
402 (or before-scroll (point)))
403 (scroll-up
404 (scroll-bar-scale portion-whole (1- (window-height)))))
405 (sit-for 0))
406 (with-current-buffer (window-buffer window)
407 (setq point-before-scroll before-scroll)))))
408
409 \f
410 ;;; Tookit scroll bars.
411
412 (defun scroll-bar-toolkit-scroll (event)
413 (interactive "e")
414 (let* ((end-position (event-end event))
415 (window (nth 0 end-position))
416 (part (nth 4 end-position))
417 before-scroll)
418 (cond
419 ((eq part 'end-scroll))
420 (t
421 (with-current-buffer (window-buffer window)
422 (setq before-scroll point-before-scroll))
423 (save-selected-window
424 (select-window window)
425 (setq before-scroll (or before-scroll (point)))
426 (cond
427 ((eq part 'above-handle)
428 (scroll-up '-))
429 ((eq part 'below-handle)
430 (scroll-up nil))
431 ((eq part 'ratio)
432 (let* ((portion-whole (nth 2 end-position))
433 (lines (scroll-bar-scale portion-whole
434 (1- (window-height)))))
435 (scroll-up (cond ((not (zerop lines)) lines)
436 ((< (car portion-whole) 0) -1)
437 (t 1)))))
438 ((eq part 'up)
439 (scroll-up -1))
440 ((eq part 'down)
441 (scroll-up 1))
442 ((eq part 'top)
443 (set-window-start window (point-min)))
444 ((eq part 'bottom)
445 (goto-char (point-max))
446 (recenter))
447 ((eq part 'handle)
448 (scroll-bar-drag-1 event))))
449 (sit-for 0)
450 (with-current-buffer (window-buffer window)
451 (setq point-before-scroll before-scroll))))))
452
453 (defun scroll-bar-toolkit-horizontal-scroll (event)
454 (interactive "e")
455 (let* ((end-position (event-end event))
456 (window (nth 0 end-position))
457 (part (nth 4 end-position))
458 (bidi-factor
459 (if (eq (current-bidi-paragraph-direction (window-buffer window))
460 'left-to-right)
461 1
462 -1))
463 before-scroll)
464 (cond
465 ((eq part 'end-scroll))
466 (t
467 (with-current-buffer (window-buffer window)
468 (setq before-scroll point-before-scroll))
469 (save-selected-window
470 (select-window window)
471 (setq before-scroll (or before-scroll (point)))
472 (cond
473 ((eq part 'before-handle)
474 (scroll-right (* bidi-factor 4)))
475 ((eq part 'after-handle)
476 (scroll-left (* bidi-factor 4)))
477 ((eq part 'ratio)
478 (let* ((portion-whole (nth 2 end-position))
479 (columns (scroll-bar-scale portion-whole
480 (1- (window-width)))))
481 (scroll-right
482 (* (cond
483 ((not (zerop columns))
484 columns)
485 ((< (car portion-whole) 0) -1)
486 (t 1))
487 bidi-factor))))
488 ((eq part 'left)
489 (scroll-right (* bidi-factor 1)))
490 ((eq part 'right)
491 (scroll-left (* bidi-factor 1)))
492 ((eq part 'leftmost)
493 (goto-char (if (eq bidi-factor 1)
494 (line-beginning-position)
495 (line-end-position))))
496 ((eq part 'rightmost)
497 (goto-char (if (eq bidi-factor 1)
498 (line-end-position)
499 (line-beginning-position))))
500 ((eq part 'horizontal-handle)
501 (scroll-bar-horizontal-drag-1 event))))
502 (sit-for 0)
503 (with-current-buffer (window-buffer window)
504 (setq point-before-scroll before-scroll))))))
505 \f
506 ;;;; Bindings.
507
508 ;; For now, we'll set things up to work like xterm.
509 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
510 (global-set-key [vertical-scroll-bar mouse-1]
511 'scroll-bar-toolkit-scroll)
512 (global-set-key [horizontal-scroll-bar mouse-1]
513 'scroll-bar-toolkit-horizontal-scroll))
514 (t
515 (global-set-key [vertical-scroll-bar mouse-1]
516 'scroll-bar-scroll-up)
517 (global-set-key [vertical-scroll-bar drag-mouse-1]
518 'scroll-bar-scroll-up)
519 (global-set-key [vertical-scroll-bar down-mouse-2]
520 'scroll-bar-drag)
521 (global-set-key [vertical-scroll-bar mouse-3]
522 'scroll-bar-scroll-down)
523 (global-set-key [vertical-scroll-bar drag-mouse-3]
524 'scroll-bar-scroll-down)))
525
526 \f
527 (provide 'scroll-bar)
528
529 ;;; scroll-bar.el ends here