(outline-next-visible-heading):
[emacs.git] / lisp / scroll-bar.el
blob7230ca046a08fdedac46b04cc24e7e676ee43f43
1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001
4 ;; 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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))))
58 ;;;; Helpful functions for enabling and disabling scroll bars.
60 (defvar scroll-bar-mode)
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.")
66 (defun set-scroll-bar-mode-1 (ignore value)
67 (set-scroll-bar-mode value))
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)
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))))
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))))))
90 (defcustom scroll-bar-mode
91 (if (eq system-type 'windows-nt) 'right 'left)
92 "*Specify whether to have vertical scroll bars, and on which side.
93 Possible values are nil (no scroll bars), `left' (scroll bars on left)
94 and `right' (scroll bars on right).
95 To set this variable in a Lisp program, use `set-scroll-bar-mode'
96 to make it take real effect.
97 Setting the variable with a customization buffer also takes effect."
98 :type '(choice (const :tag "none (nil)")
99 (const left)
100 (const right))
101 :group 'frames
102 ;; The default value for :initialize would try to use :set
103 ;; when processing the file in cus-dep.el.
104 :initialize 'custom-initialize-default
105 :set 'set-scroll-bar-mode-1)
107 ;; We just set scroll-bar-mode, but that was the default.
108 ;; If it is set again, that is for real.
109 (setq scroll-bar-mode-explicit t)
111 (defun scroll-bar-mode (&optional flag)
112 "Toggle display of vertical scroll bars on all frames.
113 This command applies to all frames that exist and frames to be
114 created in the future.
115 With a numeric argument, if the argument is negative,
116 turn off scroll bars; otherwise, turn on scroll bars."
117 (interactive "P")
118 (if flag (setq flag (prefix-numeric-value flag)))
120 ;; Tweedle the variable according to the argument.
121 (set-scroll-bar-mode (if (null flag) (not scroll-bar-mode)
122 (and (or (not (numberp flag)) (>= flag 0))
123 (if (eq system-type 'windows-nt) 'right 'left)))))
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
142 (if (eq system-type 'windows-nt) 'right 'left)))))))
144 (defun toggle-horizontal-scroll-bar (arg)
145 "Toggle whether or not the selected frame has horizontal scroll bars.
146 With arg, turn horizontal scroll bars on if and only if arg is positive.
147 Horizontal scroll bars aren't implemented yet."
148 (interactive "P")
149 (error "Horizontal scroll bars aren't implemented yet"))
151 ;;;; Buffer navigation using the scroll bar.
153 ;;; This was used for up-events on button 2, but no longer.
154 (defun scroll-bar-set-window-start (event)
155 "Set the window start according to where the scroll bar is dragged.
156 EVENT should be a scroll bar click or drag event."
157 (interactive "e")
158 (let* ((end-position (event-end event))
159 (window (nth 0 end-position))
160 (portion-whole (nth 2 end-position)))
161 (save-excursion
162 (set-buffer (window-buffer window))
163 (save-excursion
164 (goto-char (+ (point-min)
165 (scroll-bar-scale portion-whole
166 (- (point-max) (point-min)))))
167 (beginning-of-line)
168 (set-window-start window (point))))))
170 (defun scroll-bar-drag-position (portion-whole)
171 "Calculate new window start for drag event."
172 (save-excursion
173 (goto-char (+ (point-min)
174 (scroll-bar-scale portion-whole
175 (- (point-max) (point-min)))))
176 (beginning-of-line)
177 (point)))
179 (defun scroll-bar-maybe-set-window-start (event)
180 "Set the window start according to where the scroll bar is dragged.
181 Only change window start if the new start is substantially different.
182 EVENT should be a scroll bar click or drag event."
183 (interactive "e")
184 (let* ((end-position (event-end event))
185 (window (nth 0 end-position))
186 (portion-whole (nth 2 end-position))
187 (next-portion-whole (cons (1+ (car portion-whole))
188 (cdr portion-whole)))
189 portion-start
190 next-portion-start
191 (current-start (window-start window)))
192 (save-excursion
193 (set-buffer (window-buffer window))
194 (setq portion-start (scroll-bar-drag-position portion-whole))
195 (setq next-portion-start (max
196 (scroll-bar-drag-position next-portion-whole)
197 (1+ portion-start)))
198 (if (or (>= current-start next-portion-start)
199 (< current-start portion-start))
200 (set-window-start window portion-start)
201 ;; Always set window start, to ensure scroll bar position is updated.
202 (set-window-start window current-start)))))
204 ;; Scroll the window to the proper position for EVENT.
205 (defun scroll-bar-drag-1 (event)
206 (let* ((start-position (event-start event))
207 (window (nth 0 start-position))
208 (portion-whole (nth 2 start-position)))
209 (save-excursion
210 (set-buffer (window-buffer window))
211 ;; Calculate position relative to the accessible part of the buffer.
212 (goto-char (+ (point-min)
213 (scroll-bar-scale portion-whole
214 (- (point-max) (point-min)))))
215 (beginning-of-line)
216 (set-window-start window (point)))))
218 (defun scroll-bar-drag (event)
219 "Scroll the window by dragging the scroll bar slider.
220 If you click outside the slider, the window scrolls to bring the slider there."
221 (interactive "e")
222 (let* (done
223 (echo-keystrokes 0)
224 (end-position (event-end event))
225 (window (nth 0 end-position))
226 (before-scroll))
227 (with-current-buffer (window-buffer window)
228 (setq before-scroll point-before-scroll))
229 (save-selected-window
230 (select-window window)
231 (setq before-scroll
232 (or before-scroll (point))))
233 (scroll-bar-drag-1 event)
234 (track-mouse
235 (while (not done)
236 (setq event (read-event))
237 (if (eq (car-safe event) 'mouse-movement)
238 (setq event (read-event)))
239 (cond ((eq (car-safe event) 'scroll-bar-movement)
240 (scroll-bar-drag-1 event))
242 ;; Exit when we get the drag event; ignore that event.
243 (setq done t)))))
244 (sit-for 0)
245 (with-current-buffer (window-buffer window)
246 (setq point-before-scroll before-scroll))))
248 (defun scroll-bar-scroll-down (event)
249 "Scroll the window's top line down to the location of the scroll bar click.
250 EVENT should be a scroll bar click."
251 (interactive "e")
252 (let* ((end-position (event-end event))
253 (window (nth 0 end-position))
254 (before-scroll))
255 (with-current-buffer (window-buffer window)
256 (setq before-scroll point-before-scroll))
257 (unwind-protect
258 (save-selected-window
259 (let ((portion-whole (nth 2 end-position)))
260 (select-window window)
261 (setq before-scroll
262 (or before-scroll (point)))
263 (scroll-down
264 (scroll-bar-scale portion-whole (1- (window-height)))))
265 (sit-for 0))
266 (with-current-buffer (window-buffer window)
267 (setq point-before-scroll before-scroll)))))
269 (defun scroll-bar-scroll-up (event)
270 "Scroll the line next to the scroll bar click to the top of the window.
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-up
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)))))
291 ;;; Tookit scroll bars.
293 (defun scroll-bar-toolkit-scroll (event)
294 (interactive "e")
295 (let* ((end-position (event-end event))
296 (window (nth 0 end-position))
297 (part (nth 4 end-position))
298 before-scroll)
299 (cond ((eq part 'end-scroll))
301 (with-current-buffer (window-buffer window)
302 (setq before-scroll point-before-scroll))
303 (save-selected-window
304 (select-window window)
305 (setq before-scroll (or before-scroll (point)))
306 (cond ((eq part 'above-handle)
307 (scroll-up '-))
308 ((eq part 'below-handle)
309 (scroll-up nil))
310 ((eq part 'ratio)
311 (let* ((portion-whole (nth 2 end-position))
312 (lines (scroll-bar-scale portion-whole
313 (1- (window-height)))))
314 (scroll-up (cond ((not (zerop lines)) lines)
315 ((< (car portion-whole) 0) -1)
316 (t 1)))))
317 ((eq part 'up)
318 (scroll-up -1))
319 ((eq part 'down)
320 (scroll-up 1))
321 ((eq part 'top)
322 (set-window-start window (point-min)))
323 ((eq part 'bottom)
324 (goto-char (point-max))
325 (recenter))
326 ((eq part 'handle)
327 (scroll-bar-drag-1 event))))
328 (sit-for 0)
329 (with-current-buffer (window-buffer window)
330 (setq point-before-scroll before-scroll))))))
334 ;;;; Bindings.
336 ;;; For now, we'll set things up to work like xterm.
337 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
338 (global-set-key [vertical-scroll-bar mouse-1]
339 'scroll-bar-toolkit-scroll))
341 (global-set-key [vertical-scroll-bar mouse-1]
342 'scroll-bar-scroll-up)
343 (global-set-key [vertical-scroll-bar drag-mouse-1]
344 'scroll-bar-scroll-up)
345 (global-set-key [vertical-scroll-bar down-mouse-2]
346 'scroll-bar-drag)
347 (global-set-key [vertical-scroll-bar mouse-3]
348 'scroll-bar-scroll-down)
349 (global-set-key [vertical-scroll-bar drag-mouse-3]
350 'scroll-bar-scroll-down)))
353 (provide 'scroll-bar)
355 ;;; scroll-bar.el ends here