(auto-mode-alist): add patterns for diff-mode.
[bpt/emacs.git] / lisp / scroll-bar.el
CommitLineData
aae56ea7 1;;; scroll-bar.el --- window system-independent scroll bar support.
6d62a90e 2
b578f267 3;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
6d62a90e
JB
4
5;; Maintainer: FSF
6;; Keywords: hardware
7
b578f267 8;; This file is part of GNU Emacs.
6d62a90e 9
b578f267
EN
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
6d62a90e 14
b578f267
EN
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
6d62a90e 19
b578f267
EN
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
76550a57 24
d9ecc911
ER
25;;; Commentary:
26
27;; Window-system-independent bindings of mouse clicks on the scroll bar.
28;; Presently emulates the scroll-bar behavior of xterm.
b578f267 29
d9ecc911
ER
30;;; Code:
31
dbc4e1c1
JB
32(require 'mouse)
33
6d62a90e
JB
34\f
35;;;; Utilities.
36
86828678
RS
37(defun scroll-bar-event-ratio (event)
38 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
39The value is a cons cell (PORTION . WHOLE) containing two integers
40whose ratio gives the event's vertical position in the scroll bar, with 0
41referring to the top and 1 to the bottom."
42 (nth 2 event))
43
bf3c8a70 44(defun scroll-bar-scale (num-denom whole)
6d62a90e 45 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
bf3c8a70
JB
46This is handy for scaling a position on a scroll bar into real units,
47like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
48from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
6d62a90e 49\(buffer-size)) is the position in the current buffer corresponding to
bf3c8a70 50that scroll bar position."
6d62a90e
JB
51 ;; We multiply before we divide to maintain precision.
52 ;; We use floating point because the product of a large buffer size
bf3c8a70 53 ;; with a large scroll bar portion can easily overflow a lisp int.
6d62a90e
JB
54 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
55
56\f
fe48f821 57;;;; Helpful functions for enabling and disabling scroll bars.
fe48f821 58
1064fe38
RS
59(defvar scroll-bar-mode)
60
2124851f
RS
61(defvar scroll-bar-mode-explicit nil
62 "Non-nil means `set-scroll-bar-mode' should really do something.
63This is nil while loading `scroll-bar.el', and t afterward.")
64
e4b201bb
KH
65(defun set-scroll-bar-mode-1 (ignore value)
66 (set-scroll-bar-mode value))
67
68(defun set-scroll-bar-mode (value)
04c34f2e
RS
69 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
70 (setq scroll-bar-mode value)
71
2124851f
RS
72 (when scroll-bar-mode-explicit
73 ;; Apply it to default-frame-alist.
74 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
75 (if (consp parameter)
76 (setcdr parameter scroll-bar-mode)
77 (setq default-frame-alist
78 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
79 default-frame-alist))))
80
81 ;; Apply it to existing frames.
82 (let ((frames (frame-list)))
83 (while frames
84 (modify-frame-parameters
85 (car frames)
86 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
87 (setq frames (cdr frames))))))
04c34f2e 88
1032e793
GV
89(defcustom scroll-bar-mode
90 (if (eq system-type 'windows-nt) 'right 'left)
04c34f2e
RS
91 "*Specify whether to have vertical scroll bars, and on which side.
92Possible values are nil (no scroll bars), `left' (scroll bars on left)
93and `right' (scroll bars on right).
463deb66
RS
94To set this variable in a Lisp program, use `set-scroll-bar-mode'
95to make it take real effect.
96Setting the variable with a customization buffer also takes effect."
04c34f2e
RS
97 :type '(choice (const :tag "none (nil)")
98 (const left)
99 (const right))
100 :group 'frames
e4b201bb 101 :set 'set-scroll-bar-mode-1)
04c34f2e 102
2124851f
RS
103;; We just set scroll-bar-mode, but that was the default.
104;; If it is set again, that is for real.
105(setq scroll-bar-mode-explicit t)
106
fe48f821 107(defun scroll-bar-mode (flag)
04c34f2e 108 "Toggle display of vertical scroll bars on all frames.
fe48f821
JB
109This command applies to all frames that exist and frames to be
110created in the future.
111With a numeric argument, if the argument is negative,
112turn off scroll bars; otherwise, turn on scroll bars."
113 (interactive "P")
6ee9f953 114 (if flag (setq flag (prefix-numeric-value flag)))
ada464a7 115
04c34f2e 116 ;; Tweedle the variable according to the argument.
e4b201bb 117 (set-scroll-bar-mode (if (null flag) (not scroll-bar-mode)
04c34f2e 118 (and (or (not (numberp flag)) (>= flag 0))
1032e793 119 (if (eq system-type 'windows-nt) 'right 'left)))))
04c34f2e
RS
120
121(defun toggle-scroll-bar (arg)
122 "Toggle whether or not the selected frame has vertical scroll bars.
123With arg, turn vertical scroll bars on if and only if arg is positive.
124The variable `scroll-bar-mode' controls which side the scroll bars are on
125when they are turned on; if it is nil, they go on the left."
126 (interactive "P")
127 (if (null arg)
128 (setq arg
129 (if (cdr (assq 'vertical-scroll-bars
130 (frame-parameters (selected-frame))))
19c6feac
KH
131 -1 1))
132 (setq arg (prefix-numeric-value arg)))
1032e793
GV
133 (modify-frame-parameters
134 (selected-frame)
135 (list (cons 'vertical-scroll-bars
136 (if (> arg 0)
137 (or scroll-bar-mode
138 (if (eq system-type 'windows-nt) 'right 'left)))))))
04c34f2e
RS
139
140(defun toggle-horizontal-scroll-bar (arg)
141 "Toggle whether or not the selected frame has horizontal scroll bars.
142With arg, turn horizontal scroll bars on if and only if arg is positive.
143Horizontal scroll bars aren't implemented yet."
144 (interactive "P")
145 (error "Horizontal scroll bars aren't implemented yet"))
fe48f821 146\f
bf3c8a70 147;;;; Buffer navigation using the scroll bar.
6d62a90e 148
e532b016 149;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
150(defun scroll-bar-set-window-start (event)
151 "Set the window start according to where the scroll bar is dragged.
152EVENT should be a scroll bar click or drag event."
6d62a90e 153 (interactive "e")
dbc4e1c1 154 (let* ((end-position (event-end event))
6d62a90e
JB
155 (window (nth 0 end-position))
156 (portion-whole (nth 2 end-position)))
157 (save-excursion
158 (set-buffer (window-buffer window))
159 (save-excursion
4cad38d5
JB
160 (goto-char (+ (point-min)
161 (scroll-bar-scale portion-whole
162 (- (point-max) (point-min)))))
6d62a90e
JB
163 (beginning-of-line)
164 (set-window-start window (point))))))
165
aeee66be
RS
166(defun scroll-bar-drag-position (portion-whole)
167 "Calculate new window start for drag event."
168 (save-excursion
169 (goto-char (+ (point-min)
170 (scroll-bar-scale portion-whole
171 (- (point-max) (point-min)))))
172 (beginning-of-line)
173 (point)))
174
175(defun scroll-bar-maybe-set-window-start (event)
176 "Set the window start according to where the scroll bar is dragged.
177Only change window start if the new start is substantially different.
178EVENT should be a scroll bar click or drag event."
179 (interactive "e")
180 (let* ((end-position (event-end event))
181 (window (nth 0 end-position))
182 (portion-whole (nth 2 end-position))
183 (next-portion-whole (cons (1+ (car portion-whole))
184 (cdr portion-whole)))
185 portion-start
186 next-portion-start
187 (current-start (window-start window)))
188 (save-excursion
189 (set-buffer (window-buffer window))
190 (setq portion-start (scroll-bar-drag-position portion-whole))
191 (setq next-portion-start (max
192 (scroll-bar-drag-position next-portion-whole)
193 (1+ portion-start)))
15655694 194 (if (or (>= current-start next-portion-start)
aeee66be 195 (< current-start portion-start))
9c005c65
KH
196 (set-window-start window portion-start)
197 ;; Always set window start, to ensure scroll bar position is updated.
198 (set-window-start window current-start)))))
aeee66be 199
e532b016
RS
200;; Scroll the window to the proper position for EVENT.
201(defun scroll-bar-drag-1 (event)
202 (let* ((start-position (event-start event))
203 (window (nth 0 start-position))
204 (portion-whole (nth 2 start-position)))
205 (save-excursion
206 (set-buffer (window-buffer window))
d2ae6f7e
RS
207 ;; Calculate position relative to the accessible part of the buffer.
208 (goto-char (+ (point-min)
209 (scroll-bar-scale portion-whole
210 (- (point-max) (point-min)))))
e532b016
RS
211 (beginning-of-line)
212 (set-window-start window (point)))))
213
214(defun scroll-bar-drag (event)
215 "Scroll the window by dragging the scroll bar slider.
216If you click outside the slider, the window scrolls to bring the slider there."
217 (interactive "e")
c782bea5 218 (let* (done
cb183ca0
RS
219 (echo-keystrokes 0)
220 (end-position (event-end event))
221 (window (nth 0 end-position))
222 (before-scroll))
223 (with-current-buffer (window-buffer window)
224 (setq before-scroll point-before-scroll))
225 (save-selected-window
226 (select-window window)
227 (setq before-scroll
228 (or before-scroll (point))))
229 (scroll-bar-drag-1 event)
230 (track-mouse
231 (while (not done)
232 (setq event (read-event))
233 (if (eq (car-safe event) 'mouse-movement)
234 (setq event (read-event)))
235 (cond ((eq (car-safe event) 'scroll-bar-movement)
236 (scroll-bar-drag-1 event))
237 (t
238 ;; Exit when we get the drag event; ignore that event.
239 (setq done t)))))
240 (sit-for 0)
241 (with-current-buffer (window-buffer window)
242 (setq point-before-scroll before-scroll))))
e532b016 243
bf3c8a70
JB
244(defun scroll-bar-scroll-down (event)
245 "Scroll the window's top line down to the location of the scroll bar click.
246EVENT should be a scroll bar click."
6d62a90e 247 (interactive "e")
cb183ca0
RS
248 (let* ((end-position (event-end event))
249 (window (nth 0 end-position))
250 (before-scroll))
251 (with-current-buffer (window-buffer window)
252 (setq before-scroll point-before-scroll))
def7db1d
KH
253 (unwind-protect
254 (save-selected-window
255 (let ((portion-whole (nth 2 end-position)))
256 (select-window window)
257 (setq before-scroll
258 (or before-scroll (point)))
259 (scroll-down
260 (scroll-bar-scale portion-whole (1- (window-height)))))
261 (sit-for 0))
262 (with-current-buffer (window-buffer window)
263 (setq point-before-scroll before-scroll)))))
6d62a90e 264
bf3c8a70
JB
265(defun scroll-bar-scroll-up (event)
266 "Scroll the line next to the scroll bar click to the top of the window.
267EVENT should be a scroll bar click."
6d62a90e 268 (interactive "e")
cb183ca0
RS
269 (let* ((end-position (event-end event))
270 (window (nth 0 end-position))
271 (before-scroll))
272 (with-current-buffer (window-buffer window)
273 (setq before-scroll point-before-scroll))
def7db1d
KH
274 (unwind-protect
275 (save-selected-window
276 (let ((portion-whole (nth 2 end-position)))
277 (select-window window)
278 (setq before-scroll
279 (or before-scroll (point)))
280 (scroll-up
281 (scroll-bar-scale portion-whole (1- (window-height)))))
282 (sit-for 0))
283 (with-current-buffer (window-buffer window)
284 (setq point-before-scroll before-scroll)))))
6d62a90e
JB
285
286\f
cf4eb316 287;;; Tookit scroll bars.
6d62a90e 288
cf4eb316
GM
289;; Due to its event handling, Emacs is currently not able to handle Xt
290;; timeouts which toolkit scroll bars use to implement auto-repeat.
291;; As a workaround, we start a timer whenever a scroll bar action
292;; occurs, and remove it again when are notified that the user no
293;; longer interacts with the scroll bar. The timer function gives Xt
294;; the chance to call Xt timeout functions.
295
296(defvar scroll-bar-timer nil
297 "Timer running while scroll bar is active.")
6d62a90e 298
cf4eb316
GM
299(defun scroll-bar-toolkit-scroll (event)
300 (interactive "e")
301 (let* ((end-position (event-end event))
302 (window (nth 0 end-position))
303 (part (nth 4 end-position))
304 before-scroll)
305 (cond ((eq part 'end-scroll)
306 (when scroll-bar-timer
307 (cancel-timer scroll-bar-timer)
308 (setq scroll-bar-timer nil)))
309 (t
310 (with-current-buffer (window-buffer window)
311 (setq before-scroll point-before-scroll))
312 (save-selected-window
313 (select-window window)
314 (setq before-scroll (or before-scroll (point)))
315 (cond ((eq part 'above-handle)
316 (scroll-up '-))
317 ((eq part 'below-handle)
318 (scroll-up nil))
319 ((eq part 'up)
320 (scroll-up -1))
321 ((eq part 'down)
322 (scroll-up 1))
323 ((eq part 'top)
324 (set-window-start window (point-min)))
325 ((eq part 'bottom)
326 (goto-char (point-max))
327 (recenter))
328 ((eq part 'handle)
329 (scroll-bar-drag-1 event))))
330 (sit-for 0)
331 (unless scroll-bar-timer
332 (setq scroll-bar-timer
333 (run-with-timer 0.1 0.1 'xt-process-timeouts)))
334 (with-current-buffer (window-buffer window)
335 (setq point-before-scroll before-scroll))))))
e532b016 336
cf4eb316
GM
337
338\f
339;;;; Bindings.
340
341;;; For now, we'll set things up to work like xterm.
342(cond (x-toolkit-scroll-bars-p
343 (global-set-key [vertical-scroll-bar mouse-1]
344 'scroll-bar-toolkit-scroll))
345 (t
346 (global-set-key [vertical-scroll-bar mouse-1]
347 'scroll-bar-scroll-up)
348 (global-set-key [vertical-scroll-bar drag-mouse-1]
349 'scroll-bar-scroll-up)
350 (global-set-key [vertical-scroll-bar down-mouse-2]
351 'scroll-bar-drag)
352 (global-set-key [vertical-scroll-bar mouse-3]
353 'scroll-bar-scroll-down)
354 (global-set-key [vertical-scroll-bar drag-mouse-3]
355 'scroll-bar-scroll-down)))
6d62a90e
JB
356
357\f
dc14eed2 358(provide 'scroll-bar)
6d62a90e 359
bf3c8a70 360;;; scroll-bar.el ends here