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