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