(set-scroll-bar-mode): Initialize to new built-in variable
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support
2
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2003
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: hardware
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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
30
31 ;;; Code:
32
33 (require 'mouse)
34
35 \f
36 ;;;; Utilities.
37
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))
44
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))))
56
57 \f
58 ;;;; Helpful functions for enabling and disabling scroll bars.
59
60 (defvar scroll-bar-mode)
61
62 (defvar scroll-bar-mode-explicit nil
63 "Non-nil means `set-scroll-bar-mode' should really do something.
64 This is nil while loading `scroll-bar.el', and t afterward.")
65
66 (defun set-scroll-bar-mode-1 (ignore value)
67 (set-scroll-bar-mode value))
68
69 (defun set-scroll-bar-mode (value)
70 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
71 (setq scroll-bar-mode value)
72
73 (when scroll-bar-mode-explicit
74 ;; Apply it to default-frame-alist.
75 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
76 (if (consp parameter)
77 (setcdr parameter scroll-bar-mode)
78 (setq default-frame-alist
79 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
80 default-frame-alist))))
81
82 ;; Apply it to existing frames.
83 (let ((frames (frame-list)))
84 (while frames
85 (modify-frame-parameters
86 (car frames)
87 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
88 (setq frames (cdr frames))))))
89
90 (defcustom scroll-bar-mode default-frame-scroll-bars
91 "*Specify whether to have vertical scroll bars, and on which side.
92 Possible values are nil (no scroll bars), `left' (scroll bars on left)
93 and `right' (scroll bars on right).
94 To set this variable in a Lisp program, use `set-scroll-bar-mode'
95 to make it take real effect.
96 Setting the variable with a customization buffer also takes effect."
97 :type '(choice (const :tag "none (nil)" nil)
98 (const left)
99 (const right))
100 :group 'frames
101 ;; The default value for :initialize would try to use :set
102 ;; when processing the file in cus-dep.el.
103 :initialize 'custom-initialize-default
104 :set 'set-scroll-bar-mode-1)
105
106 ;; We just set scroll-bar-mode, but that was the default.
107 ;; If it is set again, that is for real.
108 (setq scroll-bar-mode-explicit t)
109
110 (defun scroll-bar-mode (&optional flag)
111 "Toggle display of vertical scroll bars on all frames.
112 This command applies to all frames that exist and frames to be
113 created in the future.
114 With a numeric argument, if the argument is negative,
115 turn off scroll bars; otherwise, turn on scroll bars."
116 (interactive "P")
117
118 ;; Tweedle the variable according to the argument.
119 (set-scroll-bar-mode (if (if (null flag)
120 (not scroll-bar-mode)
121 (setq flag (prefix-numeric-value flag))
122 (or (not (numberp flag)) (>= flag 0)))
123 default-frame-scroll-bars)))
124
125 (defun toggle-scroll-bar (arg)
126 "Toggle whether or not the selected frame has vertical scroll bars.
127 With arg, turn vertical scroll bars on if and only if arg is positive.
128 The variable `scroll-bar-mode' controls which side the scroll bars are on
129 when they are turned on; if it is nil, they go on the left."
130 (interactive "P")
131 (if (null arg)
132 (setq arg
133 (if (cdr (assq 'vertical-scroll-bars
134 (frame-parameters (selected-frame))))
135 -1 1))
136 (setq arg (prefix-numeric-value arg)))
137 (modify-frame-parameters
138 (selected-frame)
139 (list (cons 'vertical-scroll-bars
140 (if (> arg 0)
141 (or scroll-bar-mode default-frame-scroll-bars))))))
142
143 (defun toggle-horizontal-scroll-bar (arg)
144 "Toggle whether or not the selected frame has horizontal scroll bars.
145 With arg, turn horizontal scroll bars on if and only if arg is positive.
146 Horizontal scroll bars aren't implemented yet."
147 (interactive "P")
148 (error "Horizontal scroll bars aren't implemented yet"))
149 \f
150 ;;;; Buffer navigation using the scroll bar.
151
152 ;;; This was used for up-events on button 2, but no longer.
153 (defun scroll-bar-set-window-start (event)
154 "Set the window start according to where the scroll bar is dragged.
155 EVENT should be a scroll bar click or drag event."
156 (interactive "e")
157 (let* ((end-position (event-end event))
158 (window (nth 0 end-position))
159 (portion-whole (nth 2 end-position)))
160 (save-excursion
161 (set-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))))))
168
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)))
177
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 (save-excursion
192 (set-buffer (window-buffer window))
193 (setq portion-start (scroll-bar-drag-position portion-whole))
194 (setq next-portion-start (max
195 (scroll-bar-drag-position next-portion-whole)
196 (1+ portion-start)))
197 (if (or (>= current-start next-portion-start)
198 (< current-start portion-start))
199 (set-window-start window portion-start)
200 ;; Always set window start, to ensure scroll bar position is updated.
201 (set-window-start window current-start)))))
202
203 ;; Scroll the window to the proper position for EVENT.
204 (defun scroll-bar-drag-1 (event)
205 (let* ((start-position (event-start event))
206 (window (nth 0 start-position))
207 (portion-whole (nth 2 start-position)))
208 (save-excursion
209 (set-buffer (window-buffer window))
210 ;; Calculate position relative to the accessible part of the buffer.
211 (goto-char (+ (point-min)
212 (scroll-bar-scale portion-whole
213 (- (point-max) (point-min)))))
214 (vertical-motion 0 window)
215 (set-window-start window (point)))))
216
217 (defun scroll-bar-drag (event)
218 "Scroll the window by dragging the scroll bar slider.
219 If you click outside the slider, the window scrolls to bring the slider there."
220 (interactive "e")
221 (let* (done
222 (echo-keystrokes 0)
223 (end-position (event-end event))
224 (window (nth 0 end-position))
225 (before-scroll))
226 (with-current-buffer (window-buffer window)
227 (setq before-scroll point-before-scroll))
228 (save-selected-window
229 (select-window window)
230 (setq before-scroll
231 (or before-scroll (point))))
232 (scroll-bar-drag-1 event)
233 (track-mouse
234 (while (not done)
235 (setq event (read-event))
236 (if (eq (car-safe event) 'mouse-movement)
237 (setq event (read-event)))
238 (cond ((eq (car-safe event) 'scroll-bar-movement)
239 (scroll-bar-drag-1 event))
240 (t
241 ;; Exit when we get the drag event; ignore that event.
242 (setq done t)))))
243 (sit-for 0)
244 (with-current-buffer (window-buffer window)
245 (setq point-before-scroll before-scroll))))
246
247 (defun scroll-bar-scroll-down (event)
248 "Scroll the window's top line down to the location of the scroll bar click.
249 EVENT should be a scroll bar click."
250 (interactive "e")
251 (let* ((end-position (event-end event))
252 (window (nth 0 end-position))
253 (before-scroll))
254 (with-current-buffer (window-buffer window)
255 (setq before-scroll point-before-scroll))
256 (unwind-protect
257 (save-selected-window
258 (let ((portion-whole (nth 2 end-position)))
259 (select-window window)
260 (setq before-scroll
261 (or before-scroll (point)))
262 (scroll-down
263 (scroll-bar-scale portion-whole (1- (window-height)))))
264 (sit-for 0))
265 (with-current-buffer (window-buffer window)
266 (setq point-before-scroll before-scroll)))))
267
268 (defun scroll-bar-scroll-up (event)
269 "Scroll the line next to the scroll bar click to the top of the window.
270 EVENT should be a scroll bar click."
271 (interactive "e")
272 (let* ((end-position (event-end event))
273 (window (nth 0 end-position))
274 (before-scroll))
275 (with-current-buffer (window-buffer window)
276 (setq before-scroll point-before-scroll))
277 (unwind-protect
278 (save-selected-window
279 (let ((portion-whole (nth 2 end-position)))
280 (select-window window)
281 (setq before-scroll
282 (or before-scroll (point)))
283 (scroll-up
284 (scroll-bar-scale portion-whole (1- (window-height)))))
285 (sit-for 0))
286 (with-current-buffer (window-buffer window)
287 (setq point-before-scroll before-scroll)))))
288
289 \f
290 ;;; Tookit scroll bars.
291
292 (defun scroll-bar-toolkit-scroll (event)
293 (interactive "e")
294 (let* ((end-position (event-end event))
295 (window (nth 0 end-position))
296 (part (nth 4 end-position))
297 before-scroll)
298 (cond ((eq part 'end-scroll))
299 (t
300 (with-current-buffer (window-buffer window)
301 (setq before-scroll point-before-scroll))
302 (save-selected-window
303 (select-window window)
304 (setq before-scroll (or before-scroll (point)))
305 (cond ((eq part 'above-handle)
306 (scroll-up '-))
307 ((eq part 'below-handle)
308 (scroll-up nil))
309 ((eq part 'ratio)
310 (let* ((portion-whole (nth 2 end-position))
311 (lines (scroll-bar-scale portion-whole
312 (1- (window-height)))))
313 (scroll-up (cond ((not (zerop lines)) lines)
314 ((< (car portion-whole) 0) -1)
315 (t 1)))))
316 ((eq part 'up)
317 (scroll-up -1))
318 ((eq part 'down)
319 (scroll-up 1))
320 ((eq part 'top)
321 (set-window-start window (point-min)))
322 ((eq part 'bottom)
323 (goto-char (point-max))
324 (recenter))
325 ((eq part 'handle)
326 (scroll-bar-drag-1 event))))
327 (sit-for 0)
328 (with-current-buffer (window-buffer window)
329 (setq point-before-scroll before-scroll))))))
330
331
332 \f
333 ;;;; Bindings.
334
335 ;;; For now, we'll set things up to work like xterm.
336 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
337 (global-set-key [vertical-scroll-bar mouse-1]
338 'scroll-bar-toolkit-scroll))
339 (t
340 (global-set-key [vertical-scroll-bar mouse-1]
341 'scroll-bar-scroll-up)
342 (global-set-key [vertical-scroll-bar drag-mouse-1]
343 'scroll-bar-scroll-up)
344 (global-set-key [vertical-scroll-bar down-mouse-2]
345 'scroll-bar-drag)
346 (global-set-key [vertical-scroll-bar mouse-3]
347 'scroll-bar-scroll-down)
348 (global-set-key [vertical-scroll-bar drag-mouse-3]
349 'scroll-bar-scroll-down)))
350
351 \f
352 (provide 'scroll-bar)
353
354 ;;; arch-tag: 6f1d01d0-0b1e-4bf8-86db-d491e0f399f3
355 ;;; scroll-bar.el ends here