new version
[emacs.git] / lisp / scroll-bar.el
blob6a2660626d1fa9a8bea192b2fecff3740a73792f
1 ;;; scroll-bar.el --- window system-independent scroll bar support.
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: hardware
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
28 ;; Presently emulates the scroll-bar behavior of xterm.
30 ;;; Code:
32 (require 'mouse)
35 ;;;; Utilities.
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))
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))))
57 ;;;; Helpful functions for enabling and disabling scroll bars.
59 (defvar scroll-bar-mode)
61 (defvar scroll-bar-mode-explicit nil
62 "Non-nil means `set-scroll-bar-mode' should really do something.
63 This is nil while loading `scroll-bar.el', and t afterward.")
65 (defun set-scroll-bar-mode (ignore value)
66 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
67 (setq scroll-bar-mode value)
69 (when scroll-bar-mode-explicit
70 ;; Apply it to default-frame-alist.
71 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist)))
72 (if (consp parameter)
73 (setcdr parameter scroll-bar-mode)
74 (setq default-frame-alist
75 (cons (cons 'vertical-scroll-bars scroll-bar-mode)
76 default-frame-alist))))
78 ;; Apply it to existing frames.
79 (let ((frames (frame-list)))
80 (while frames
81 (modify-frame-parameters
82 (car frames)
83 (list (cons 'vertical-scroll-bars scroll-bar-mode)))
84 (setq frames (cdr frames))))))
86 (defcustom scroll-bar-mode 'left
87 "*Specify whether to have vertical scroll bars, and on which side.
88 Possible values are nil (no scroll bars), `left' (scroll bars on left)
89 and `right' (scroll bars on right).
90 When you set the variable in a Lisp program, it takes effect for new frames,
91 and for existing frames when `toggle-scroll-bar' is used.
92 When you set this with the customization buffer,
93 it takes effect immediately for all frames."
94 :type '(choice (const :tag "none (nil)")
95 (const left)
96 (const right))
97 :group 'frames
98 :set 'set-scroll-bar-mode)
100 ;; We just set scroll-bar-mode, but that was the default.
101 ;; If it is set again, that is for real.
102 (setq scroll-bar-mode-explicit t)
104 (defun scroll-bar-mode (flag)
105 "Toggle display of vertical scroll bars on all frames.
106 This command applies to all frames that exist and frames to be
107 created in the future.
108 With a numeric argument, if the argument is negative,
109 turn off scroll bars; otherwise, turn on scroll bars."
110 (interactive "P")
111 (if flag (setq flag (prefix-numeric-value flag)))
113 ;; Tweedle the variable according to the argument.
114 (set-scroll-bar-mode nil
115 (if (null flag) (not scroll-bar-mode)
116 (and (or (not (numberp flag)) (>= flag 0))
117 'left))))
119 (defun toggle-scroll-bar (arg)
120 "Toggle whether or not the selected frame has vertical scroll bars.
121 With arg, turn vertical scroll bars on if and only if arg is positive.
122 The variable `scroll-bar-mode' controls which side the scroll bars are on
123 when they are turned on; if it is nil, they go on the left."
124 (interactive "P")
125 (if (null arg)
126 (setq arg
127 (if (cdr (assq 'vertical-scroll-bars
128 (frame-parameters (selected-frame))))
129 -1 1)))
130 (modify-frame-parameters (selected-frame)
131 (list (cons 'vertical-scroll-bars
132 (if (> arg 0)
133 (or scroll-bar-mode 'left))))))
135 (defun toggle-horizontal-scroll-bar (arg)
136 "Toggle whether or not the selected frame has horizontal scroll bars.
137 With arg, turn horizontal scroll bars on if and only if arg is positive.
138 Horizontal scroll bars aren't implemented yet."
139 (interactive "P")
140 (error "Horizontal scroll bars aren't implemented yet"))
142 ;;;; Buffer navigation using the scroll bar.
144 ;;; This was used for up-events on button 2, but no longer.
145 (defun scroll-bar-set-window-start (event)
146 "Set the window start according to where the scroll bar is dragged.
147 EVENT should be a scroll bar click or drag event."
148 (interactive "e")
149 (let* ((end-position (event-end event))
150 (window (nth 0 end-position))
151 (portion-whole (nth 2 end-position)))
152 (save-excursion
153 (set-buffer (window-buffer window))
154 (save-excursion
155 (goto-char (+ (point-min)
156 (scroll-bar-scale portion-whole
157 (- (point-max) (point-min)))))
158 (beginning-of-line)
159 (set-window-start window (point))))))
161 (defun scroll-bar-drag-position (portion-whole)
162 "Calculate new window start for drag event."
163 (save-excursion
164 (goto-char (+ (point-min)
165 (scroll-bar-scale portion-whole
166 (- (point-max) (point-min)))))
167 (beginning-of-line)
168 (point)))
170 (defun scroll-bar-maybe-set-window-start (event)
171 "Set the window start according to where the scroll bar is dragged.
172 Only change window start if the new start is substantially different.
173 EVENT should be a scroll bar click or drag event."
174 (interactive "e")
175 (let* ((end-position (event-end event))
176 (window (nth 0 end-position))
177 (portion-whole (nth 2 end-position))
178 (next-portion-whole (cons (1+ (car portion-whole))
179 (cdr portion-whole)))
180 portion-start
181 next-portion-start
182 (current-start (window-start window)))
183 (save-excursion
184 (set-buffer (window-buffer window))
185 (setq portion-start (scroll-bar-drag-position portion-whole))
186 (setq next-portion-start (max
187 (scroll-bar-drag-position next-portion-whole)
188 (1+ portion-start)))
189 (if (or (>= current-start next-portion-start)
190 (< current-start portion-start))
191 (set-window-start window portion-start)
192 ;; Always set window start, to ensure scroll bar position is updated.
193 (set-window-start window current-start)))))
195 ;; Scroll the window to the proper position for EVENT.
196 (defun scroll-bar-drag-1 (event)
197 (let* ((start-position (event-start event))
198 (window (nth 0 start-position))
199 (portion-whole (nth 2 start-position)))
200 (save-excursion
201 (set-buffer (window-buffer window))
202 ;; Calculate position relative to the accessible part of the buffer.
203 (goto-char (+ (point-min)
204 (scroll-bar-scale portion-whole
205 (- (point-max) (point-min)))))
206 (beginning-of-line)
207 (set-window-start window (point)))))
209 (defun scroll-bar-drag (event)
210 "Scroll the window by dragging the scroll bar slider.
211 If you click outside the slider, the window scrolls to bring the slider there."
212 (interactive "e")
213 (let* (done
214 (echo-keystrokes 0))
215 (or point-before-scroll
216 (setq point-before-scroll (point)))
217 ;; Our scrolling can move point; don't let that clear point-before-scroll.
218 (let (point-before-scroll)
219 (scroll-bar-drag-1 event)
220 (track-mouse
221 (while (not done)
222 (setq event (read-event))
223 (if (eq (car-safe event) 'mouse-movement)
224 (setq event (read-event)))
225 (cond ((eq (car-safe event) 'scroll-bar-movement)
226 (scroll-bar-drag-1 event))
228 ;; Exit when we get the drag event; ignore that event.
229 (setq done t)))))
230 (sit-for 0))))
232 (defun scroll-bar-scroll-down (event)
233 "Scroll the window's top line down to the location of the scroll bar click.
234 EVENT should be a scroll bar click."
235 (interactive "e")
236 (let ((old-selected-window (selected-window)))
237 (unwind-protect
238 (progn
239 (let* ((end-position (event-end event))
240 (window (nth 0 end-position))
241 (portion-whole (nth 2 end-position)))
242 (let (point-before-scroll)
243 (select-window window))
244 (or point-before-scroll
245 (setq point-before-scroll (point)))
246 (let (point-before-scroll)
247 (scroll-down
248 (scroll-bar-scale portion-whole (1- (window-height)))))))
249 (select-window old-selected-window))))
251 (defun scroll-bar-scroll-up (event)
252 "Scroll the line next to the scroll bar click to the top of the window.
253 EVENT should be a scroll bar click."
254 (interactive "e")
255 (let ((old-selected-window (selected-window)))
256 (unwind-protect
257 (progn
258 (let* ((end-position (event-end event))
259 (window (nth 0 end-position))
260 (portion-whole (nth 2 end-position)))
261 (let (point-before-scroll)
262 (select-window window))
263 (or point-before-scroll
264 (setq point-before-scroll (point)))
265 (let (point-before-scroll)
266 (scroll-up
267 (scroll-bar-scale portion-whole (1- (window-height)))))))
268 (select-window old-selected-window))))
271 ;;;; Bindings.
273 ;;; For now, we'll set things up to work like xterm.
274 (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
275 (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
277 (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
279 (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
280 (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
283 (provide 'scroll-bar)
285 ;;; scroll-bar.el ends here