(byte-compile-warning-prefix): Correctly compute line number.
[emacs.git] / lisp / scroll-bar.el
blobccda2a56b07512992a0fa94d410b09bbb5b203b3
1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: hardware
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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
31 ;;; Code:
33 (require 'mouse)
36 ;;;; Utilities.
38 (defun scroll-bar-event-ratio (event)
39 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
40 The value is a cons cell (PORTION . WHOLE) containing two integers
41 whose ratio gives the event's vertical position in the scroll bar, with 0
42 referring to the top and 1 to the bottom."
43 (nth 2 event))
45 (defun scroll-bar-scale (num-denom whole)
46 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
47 This is handy for scaling a position on a scroll bar into real units,
48 like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
49 from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
50 \(buffer-size)) is the position in the current buffer corresponding to
51 that scroll bar position."
52 ;; We multiply before we divide to maintain precision.
53 ;; We use floating point because the product of a large buffer size
54 ;; with a large scroll bar portion can easily overflow a lisp int.
55 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
57 (defun scroll-bar-columns (side)
58 "Return the width, measured in columns, of the vertical scrollbar on SIDE.
59 SIDE must be the symbol `left' or `right'."
60 (let* ((wsb (window-scroll-bars))
61 (vtype (nth 2 wsb))
62 (cols (nth 1 wsb)))
63 (cond
64 ((not (memq side '(left right)))
65 (error "`left' or `right' expected instead of %S" side))
66 ((and (eq vtype side) cols))
67 ((eq (frame-parameter nil 'vertical-scroll-bars) side)
68 ;; nil means it's a non-toolkit scroll bar, and its width in
69 ;; columns is 14 pixels rounded up.
70 (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
71 (frame-char-width)))
72 (0))))
75 ;;;; Helpful functions for enabling and disabling scroll bars.
77 (defvar scroll-bar-mode)
78 (defvar previous-scroll-bar-mode nil)
80 (defvar scroll-bar-mode-explicit nil
81 "Non-nil means `set-scroll-bar-mode' should really do something.
82 This is nil while loading `scroll-bar.el', and t afterward.")
84 (defun set-scroll-bar-mode-1 (ignore value)
85 (set-scroll-bar-mode value))
87 (defun set-scroll-bar-mode (value)
88 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
89 (if scroll-bar-mode
90 (setq previous-scroll-bar-mode scroll-bar-mode))
92 (setq scroll-bar-mode value)
94 (when scroll-bar-mode-explicit
95 ;; Apply it to default-frame-alist.
96 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
97 (if (consp parameter)
98 (setcdr parameter scroll-bar-mode)
99 (setq default-frame-alist
100 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
101 default-frame-alist))))
103 ;; Apply it to existing frames.
104 (let ((frames (frame-list)))
105 (while frames
106 (modify-frame-parameters
107 (car frames)
108 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
109 (setq frames (cdr frames))))))
111 (defcustom scroll-bar-mode default-frame-scroll-bars
112 "*Specify whether to have vertical scroll bars, and on which side.
113 Possible values are nil (no scroll bars), `left' (scroll bars on left)
114 and `right' (scroll bars on right).
115 To set this variable in a Lisp program, use `set-scroll-bar-mode'
116 to make it take real effect.
117 Setting the variable with a customization buffer also takes effect."
118 :type '(choice (const :tag "none (nil)" nil)
119 (const left)
120 (const right))
121 :group 'frames
122 ;; The default value for :initialize would try to use :set
123 ;; when processing the file in cus-dep.el.
124 :initialize 'custom-initialize-default
125 :set 'set-scroll-bar-mode-1)
127 ;; We just set scroll-bar-mode, but that was the default.
128 ;; If it is set again, that is for real.
129 (setq scroll-bar-mode-explicit t)
131 (defun scroll-bar-mode (&optional flag)
132 "Toggle display of vertical scroll bars on all frames.
133 This command applies to all frames that exist and frames to be
134 created in the future.
135 With a numeric argument, if the argument is negative,
136 turn off scroll bars; otherwise, turn on scroll bars."
137 (interactive "P")
139 ;; Tweedle the variable according to the argument.
140 (set-scroll-bar-mode (if (if (null flag)
141 (not scroll-bar-mode)
142 (setq flag (prefix-numeric-value flag))
143 (or (not (numberp flag)) (>= flag 0)))
144 (or previous-scroll-bar-mode
145 default-frame-scroll-bars))))
147 (defun toggle-scroll-bar (arg)
148 "Toggle whether or not the selected frame has vertical scroll bars.
149 With arg, turn vertical scroll bars on if and only if arg is positive.
150 The variable `scroll-bar-mode' controls which side the scroll bars are on
151 when they are turned on; if it is nil, they go on the left."
152 (interactive "P")
153 (if (null arg)
154 (setq arg
155 (if (cdr (assq 'vertical-scroll-bars
156 (frame-parameters (selected-frame))))
157 -1 1))
158 (setq arg (prefix-numeric-value arg)))
159 (modify-frame-parameters
160 (selected-frame)
161 (list (cons 'vertical-scroll-bars
162 (if (> arg 0)
163 (or scroll-bar-mode default-frame-scroll-bars))))))
165 (defun toggle-horizontal-scroll-bar (arg)
166 "Toggle whether or not the selected frame has horizontal scroll bars.
167 With arg, turn horizontal scroll bars on if and only if arg is positive.
168 Horizontal scroll bars aren't implemented yet."
169 (interactive "P")
170 (error "Horizontal scroll bars aren't implemented yet"))
172 ;;;; Buffer navigation using the scroll bar.
174 ;;; This was used for up-events on button 2, but no longer.
175 (defun scroll-bar-set-window-start (event)
176 "Set the window start according to where the scroll bar is dragged.
177 EVENT should be a scroll bar click or drag event."
178 (interactive "e")
179 (let* ((end-position (event-end event))
180 (window (nth 0 end-position))
181 (portion-whole (nth 2 end-position)))
182 (save-excursion
183 (set-buffer (window-buffer window))
184 (save-excursion
185 (goto-char (+ (point-min)
186 (scroll-bar-scale portion-whole
187 (- (point-max) (point-min)))))
188 (beginning-of-line)
189 (set-window-start window (point))))))
191 (defun scroll-bar-drag-position (portion-whole)
192 "Calculate new window start for drag event."
193 (save-excursion
194 (goto-char (+ (point-min)
195 (scroll-bar-scale portion-whole
196 (- (point-max) (point-min)))))
197 (beginning-of-line)
198 (point)))
200 (defun scroll-bar-maybe-set-window-start (event)
201 "Set the window start according to where the scroll bar is dragged.
202 Only change window start if the new start is substantially different.
203 EVENT should be a scroll bar click or drag event."
204 (interactive "e")
205 (let* ((end-position (event-end event))
206 (window (nth 0 end-position))
207 (portion-whole (nth 2 end-position))
208 (next-portion-whole (cons (1+ (car portion-whole))
209 (cdr portion-whole)))
210 portion-start
211 next-portion-start
212 (current-start (window-start window)))
213 (save-excursion
214 (set-buffer (window-buffer window))
215 (setq portion-start (scroll-bar-drag-position portion-whole))
216 (setq next-portion-start (max
217 (scroll-bar-drag-position next-portion-whole)
218 (1+ portion-start)))
219 (if (or (>= current-start next-portion-start)
220 (< current-start portion-start))
221 (set-window-start window portion-start)
222 ;; Always set window start, to ensure scroll bar position is updated.
223 (set-window-start window current-start)))))
225 ;; Scroll the window to the proper position for EVENT.
226 (defun scroll-bar-drag-1 (event)
227 (let* ((start-position (event-start event))
228 (window (nth 0 start-position))
229 (portion-whole (nth 2 start-position)))
230 (save-excursion
231 (set-buffer (window-buffer window))
232 ;; Calculate position relative to the accessible part of the buffer.
233 (goto-char (+ (point-min)
234 (scroll-bar-scale portion-whole
235 (- (point-max) (point-min)))))
236 (vertical-motion 0 window)
237 (set-window-start window (point)))))
239 (defun scroll-bar-drag (event)
240 "Scroll the window by dragging the scroll bar slider.
241 If you click outside the slider, the window scrolls to bring the slider there."
242 (interactive "e")
243 (let* (done
244 (echo-keystrokes 0)
245 (end-position (event-end event))
246 (window (nth 0 end-position))
247 (before-scroll))
248 (with-current-buffer (window-buffer window)
249 (setq before-scroll point-before-scroll))
250 (save-selected-window
251 (select-window window)
252 (setq before-scroll
253 (or before-scroll (point))))
254 (scroll-bar-drag-1 event)
255 (track-mouse
256 (while (not done)
257 (setq event (read-event))
258 (if (eq (car-safe event) 'mouse-movement)
259 (setq event (read-event)))
260 (cond ((eq (car-safe event) 'scroll-bar-movement)
261 (scroll-bar-drag-1 event))
263 ;; Exit when we get the drag event; ignore that event.
264 (setq done t)))))
265 (sit-for 0)
266 (with-current-buffer (window-buffer window)
267 (setq point-before-scroll before-scroll))))
269 (defun scroll-bar-scroll-down (event)
270 "Scroll the window's top line down to the location of the scroll bar click.
271 EVENT should be a scroll bar click."
272 (interactive "e")
273 (let* ((end-position (event-end event))
274 (window (nth 0 end-position))
275 (before-scroll))
276 (with-current-buffer (window-buffer window)
277 (setq before-scroll point-before-scroll))
278 (unwind-protect
279 (save-selected-window
280 (let ((portion-whole (nth 2 end-position)))
281 (select-window window)
282 (setq before-scroll
283 (or before-scroll (point)))
284 (scroll-down
285 (scroll-bar-scale portion-whole (1- (window-height)))))
286 (sit-for 0))
287 (with-current-buffer (window-buffer window)
288 (setq point-before-scroll before-scroll)))))
290 (defun scroll-bar-scroll-up (event)
291 "Scroll the line next to the scroll bar click to the top of the window.
292 EVENT should be a scroll bar click."
293 (interactive "e")
294 (let* ((end-position (event-end event))
295 (window (nth 0 end-position))
296 (before-scroll))
297 (with-current-buffer (window-buffer window)
298 (setq before-scroll point-before-scroll))
299 (unwind-protect
300 (save-selected-window
301 (let ((portion-whole (nth 2 end-position)))
302 (select-window window)
303 (setq before-scroll
304 (or before-scroll (point)))
305 (scroll-up
306 (scroll-bar-scale portion-whole (1- (window-height)))))
307 (sit-for 0))
308 (with-current-buffer (window-buffer window)
309 (setq point-before-scroll before-scroll)))))
312 ;;; Tookit scroll bars.
314 (defun scroll-bar-toolkit-scroll (event)
315 (interactive "e")
316 (let* ((end-position (event-end event))
317 (window (nth 0 end-position))
318 (part (nth 4 end-position))
319 before-scroll)
320 (cond ((eq part 'end-scroll))
322 (with-current-buffer (window-buffer window)
323 (setq before-scroll point-before-scroll))
324 (save-selected-window
325 (select-window window)
326 (setq before-scroll (or before-scroll (point)))
327 (cond ((eq part 'above-handle)
328 (scroll-up '-))
329 ((eq part 'below-handle)
330 (scroll-up nil))
331 ((eq part 'ratio)
332 (let* ((portion-whole (nth 2 end-position))
333 (lines (scroll-bar-scale portion-whole
334 (1- (window-height)))))
335 (scroll-up (cond ((not (zerop lines)) lines)
336 ((< (car portion-whole) 0) -1)
337 (t 1)))))
338 ((eq part 'up)
339 (scroll-up -1))
340 ((eq part 'down)
341 (scroll-up 1))
342 ((eq part 'top)
343 (set-window-start window (point-min)))
344 ((eq part 'bottom)
345 (goto-char (point-max))
346 (recenter))
347 ((eq part 'handle)
348 (scroll-bar-drag-1 event))))
349 (sit-for 0)
350 (with-current-buffer (window-buffer window)
351 (setq point-before-scroll before-scroll))))))
355 ;;;; Bindings.
357 ;;; For now, we'll set things up to work like xterm.
358 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
359 (global-set-key [vertical-scroll-bar mouse-1]
360 'scroll-bar-toolkit-scroll))
362 (global-set-key [vertical-scroll-bar mouse-1]
363 'scroll-bar-scroll-up)
364 (global-set-key [vertical-scroll-bar drag-mouse-1]
365 'scroll-bar-scroll-up)
366 (global-set-key [vertical-scroll-bar down-mouse-2]
367 'scroll-bar-drag)
368 (global-set-key [vertical-scroll-bar mouse-3]
369 'scroll-bar-scroll-down)
370 (global-set-key [vertical-scroll-bar drag-mouse-3]
371 'scroll-bar-scroll-down)))
374 (provide 'scroll-bar)
376 ;;; arch-tag: 6f1d01d0-0b1e-4bf8-86db-d491e0f399f3
377 ;;; scroll-bar.el ends here