38412
|
1 ;;; scroll-bar.el --- window system-independent scroll bar support
|
1772
|
2
|
50398
|
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2003
|
36803
|
4 ;; Free Software Foundation, Inc.
|
1772
|
5
|
|
6 ;; Maintainer: FSF
|
|
7 ;; Keywords: hardware
|
|
8
|
14169
|
9 ;; This file is part of GNU Emacs.
|
1772
|
10
|
14169
|
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.
|
1772
|
15
|
14169
|
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.
|
1772
|
20
|
14169
|
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.
|
2233
|
25
|
2315
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
|
|
29 ;; Presently emulates the scroll-bar behavior of xterm.
|
14169
|
30
|
2315
|
31 ;;; Code:
|
|
32
|
1821
|
33 (require 'mouse)
|
|
34
|
1772
|
35
|
|
36 ;;;; Utilities.
|
|
37
|
7222
|
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))
|
|
44
|
1981
|
45 (defun scroll-bar-scale (num-denom whole)
|
1772
|
46 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
|
1981
|
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
|
1772
|
50 \(buffer-size)) is the position in the current buffer corresponding to
|
1981
|
51 that scroll bar position."
|
1772
|
52 ;; We multiply before we divide to maintain precision.
|
|
53 ;; We use floating point because the product of a large buffer size
|
1981
|
54 ;; with a large scroll bar portion can easily overflow a lisp int.
|
1772
|
55 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
|
|
56
|
|
57
|
1954
|
58 ;;;; Helpful functions for enabling and disabling scroll bars.
|
|
59
|
18480
|
60 (defvar scroll-bar-mode)
|
|
61
|
18891
|
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.")
|
|
65
|
20129
|
66 (defun set-scroll-bar-mode-1 (ignore value)
|
|
67 (set-scroll-bar-mode value))
|
|
68
|
|
69 (defun set-scroll-bar-mode (value)
|
18479
|
70 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
|
|
71 (setq scroll-bar-mode value)
|
|
72
|
18891
|
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))))
|
18479
|
81
|
18891
|
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))))))
|
18479
|
89
|
21734
|
90 (defcustom scroll-bar-mode
|
49004
|
91 (cond ((eq system-type 'windows-nt) 'right)
|
49016
|
92 ((featurep 'mac-carbon) 'right)
|
49004
|
93 (t 'left))
|
18479
|
94 "*Specify whether to have vertical scroll bars, and on which side.
|
|
95 Possible values are nil (no scroll bars), `left' (scroll bars on left)
|
|
96 and `right' (scroll bars on right).
|
22908
|
97 To set this variable in a Lisp program, use `set-scroll-bar-mode'
|
|
98 to make it take real effect.
|
|
99 Setting the variable with a customization buffer also takes effect."
|
50398
|
100 :type '(choice (const :tag "none (nil)" nil)
|
18479
|
101 (const left)
|
|
102 (const right))
|
|
103 :group 'frames
|
47494
|
104 ;; The default value for :initialize would try to use :set
|
|
105 ;; when processing the file in cus-dep.el.
|
|
106 :initialize 'custom-initialize-default
|
20129
|
107 :set 'set-scroll-bar-mode-1)
|
18479
|
108
|
18891
|
109 ;; We just set scroll-bar-mode, but that was the default.
|
|
110 ;; If it is set again, that is for real.
|
|
111 (setq scroll-bar-mode-explicit t)
|
|
112
|
42851
|
113 (defun scroll-bar-mode (&optional flag)
|
18479
|
114 "Toggle display of vertical scroll bars on all frames.
|
1954
|
115 This command applies to all frames that exist and frames to be
|
|
116 created in the future.
|
|
117 With a numeric argument, if the argument is negative,
|
|
118 turn off scroll bars; otherwise, turn on scroll bars."
|
|
119 (interactive "P")
|
4469
abb585cf9188
(scroll-bar-mode): If FLAG is non-nil, set it to its prefix-numeric-value.
Roland McGrath <roland@gnu.org>
diff
changeset
|
120 (if flag (setq flag (prefix-numeric-value flag)))
|
3613
|
121
|
18479
|
122 ;; Tweedle the variable according to the argument.
|
20129
|
123 (set-scroll-bar-mode (if (null flag) (not scroll-bar-mode)
|
18479
|
124 (and (or (not (numberp flag)) (>= flag 0))
|
49004
|
125 (cond ((eq system-type 'windows-nt) 'right)
|
49016
|
126 ((featurep 'mac-carbon) 'right)
|
49004
|
127 (t 'left))))))
|
3613
|
128
|
18479
|
129 (defun toggle-scroll-bar (arg)
|
|
130 "Toggle whether or not the selected frame has vertical scroll bars.
|
|
131 With arg, turn vertical scroll bars on if and only if arg is positive.
|
|
132 The variable `scroll-bar-mode' controls which side the scroll bars are on
|
|
133 when they are turned on; if it is nil, they go on the left."
|
|
134 (interactive "P")
|
|
135 (if (null arg)
|
|
136 (setq arg
|
|
137 (if (cdr (assq 'vertical-scroll-bars
|
|
138 (frame-parameters (selected-frame))))
|
20061
|
139 -1 1))
|
|
140 (setq arg (prefix-numeric-value arg)))
|
21734
|
141 (modify-frame-parameters
|
|
142 (selected-frame)
|
|
143 (list (cons 'vertical-scroll-bars
|
|
144 (if (> arg 0)
|
|
145 (or scroll-bar-mode
|
49004
|
146 (cond ((eq system-type 'windows-nt) 'right)
|
49016
|
147 ((featurep 'mac-carbon) 'right)
|
49004
|
148 (t 'left))))))))
|
3613
|
149
|
18479
|
150 (defun toggle-horizontal-scroll-bar (arg)
|
|
151 "Toggle whether or not the selected frame has horizontal scroll bars.
|
|
152 With arg, turn horizontal scroll bars on if and only if arg is positive.
|
|
153 Horizontal scroll bars aren't implemented yet."
|
|
154 (interactive "P")
|
|
155 (error "Horizontal scroll bars aren't implemented yet"))
|
1954
|
156
|
1981
|
157 ;;;; Buffer navigation using the scroll bar.
|
1772
|
158
|
2698
|
159 ;;; This was used for up-events on button 2, but no longer.
|
1981
|
160 (defun scroll-bar-set-window-start (event)
|
|
161 "Set the window start according to where the scroll bar is dragged.
|
|
162 EVENT should be a scroll bar click or drag event."
|
1772
|
163 (interactive "e")
|
1821
|
164 (let* ((end-position (event-end event))
|
1772
|
165 (window (nth 0 end-position))
|
|
166 (portion-whole (nth 2 end-position)))
|
|
167 (save-excursion
|
|
168 (set-buffer (window-buffer window))
|
|
169 (save-excursion
|
3588
|
170 (goto-char (+ (point-min)
|
|
171 (scroll-bar-scale portion-whole
|
|
172 (- (point-max) (point-min)))))
|
1772
|
173 (beginning-of-line)
|
|
174 (set-window-start window (point))))))
|
|
175
|
15215
|
176 (defun scroll-bar-drag-position (portion-whole)
|
|
177 "Calculate new window start for drag event."
|
|
178 (save-excursion
|
|
179 (goto-char (+ (point-min)
|
|
180 (scroll-bar-scale portion-whole
|
|
181 (- (point-max) (point-min)))))
|
|
182 (beginning-of-line)
|
|
183 (point)))
|
|
184
|
|
185 (defun scroll-bar-maybe-set-window-start (event)
|
|
186 "Set the window start according to where the scroll bar is dragged.
|
|
187 Only change window start if the new start is substantially different.
|
|
188 EVENT should be a scroll bar click or drag event."
|
|
189 (interactive "e")
|
|
190 (let* ((end-position (event-end event))
|
|
191 (window (nth 0 end-position))
|
|
192 (portion-whole (nth 2 end-position))
|
|
193 (next-portion-whole (cons (1+ (car portion-whole))
|
|
194 (cdr portion-whole)))
|
|
195 portion-start
|
|
196 next-portion-start
|
|
197 (current-start (window-start window)))
|
|
198 (save-excursion
|
|
199 (set-buffer (window-buffer window))
|
|
200 (setq portion-start (scroll-bar-drag-position portion-whole))
|
|
201 (setq next-portion-start (max
|
|
202 (scroll-bar-drag-position next-portion-whole)
|
|
203 (1+ portion-start)))
|
19683
|
204 (if (or (>= current-start next-portion-start)
|
15215
|
205 (< current-start portion-start))
|
15264
|
206 (set-window-start window portion-start)
|
|
207 ;; Always set window start, to ensure scroll bar position is updated.
|
|
208 (set-window-start window current-start)))))
|
15215
|
209
|
2698
|
210 ;; Scroll the window to the proper position for EVENT.
|
|
211 (defun scroll-bar-drag-1 (event)
|
|
212 (let* ((start-position (event-start event))
|
|
213 (window (nth 0 start-position))
|
|
214 (portion-whole (nth 2 start-position)))
|
|
215 (save-excursion
|
|
216 (set-buffer (window-buffer window))
|
3508
|
217 ;; Calculate position relative to the accessible part of the buffer.
|
|
218 (goto-char (+ (point-min)
|
|
219 (scroll-bar-scale portion-whole
|
|
220 (- (point-max) (point-min)))))
|
50717
318f8d4ecf5a
(scroll-bar-drag-1): Replace beginning-of-line with vertical-motion.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
221 (vertical-motion 0 window)
|
2698
|
222 (set-window-start window (point)))))
|
|
223
|
|
224 (defun scroll-bar-drag (event)
|
|
225 "Scroll the window by dragging the scroll bar slider.
|
|
226 If you click outside the slider, the window scrolls to bring the slider there."
|
|
227 (interactive "e")
|
11145
|
228 (let* (done
|
21427
|
229 (echo-keystrokes 0)
|
|
230 (end-position (event-end event))
|
|
231 (window (nth 0 end-position))
|
|
232 (before-scroll))
|
|
233 (with-current-buffer (window-buffer window)
|
|
234 (setq before-scroll point-before-scroll))
|
|
235 (save-selected-window
|
|
236 (select-window window)
|
|
237 (setq before-scroll
|
|
238 (or before-scroll (point))))
|
|
239 (scroll-bar-drag-1 event)
|
|
240 (track-mouse
|
|
241 (while (not done)
|
|
242 (setq event (read-event))
|
|
243 (if (eq (car-safe event) 'mouse-movement)
|
|
244 (setq event (read-event)))
|
|
245 (cond ((eq (car-safe event) 'scroll-bar-movement)
|
|
246 (scroll-bar-drag-1 event))
|
|
247 (t
|
|
248 ;; Exit when we get the drag event; ignore that event.
|
|
249 (setq done t)))))
|
|
250 (sit-for 0)
|
|
251 (with-current-buffer (window-buffer window)
|
|
252 (setq point-before-scroll before-scroll))))
|
2698
|
253
|
1981
|
254 (defun scroll-bar-scroll-down (event)
|
|
255 "Scroll the window's top line down to the location of the scroll bar click.
|
|
256 EVENT should be a scroll bar click."
|
1772
|
257 (interactive "e")
|
21427
|
258 (let* ((end-position (event-end event))
|
|
259 (window (nth 0 end-position))
|
|
260 (before-scroll))
|
|
261 (with-current-buffer (window-buffer window)
|
|
262 (setq before-scroll point-before-scroll))
|
23248
|
263 (unwind-protect
|
|
264 (save-selected-window
|
|
265 (let ((portion-whole (nth 2 end-position)))
|
|
266 (select-window window)
|
|
267 (setq before-scroll
|
|
268 (or before-scroll (point)))
|
|
269 (scroll-down
|
|
270 (scroll-bar-scale portion-whole (1- (window-height)))))
|
|
271 (sit-for 0))
|
|
272 (with-current-buffer (window-buffer window)
|
|
273 (setq point-before-scroll before-scroll)))))
|
1772
|
274
|
1981
|
275 (defun scroll-bar-scroll-up (event)
|
|
276 "Scroll the line next to the scroll bar click to the top of the window.
|
|
277 EVENT should be a scroll bar click."
|
1772
|
278 (interactive "e")
|
21427
|
279 (let* ((end-position (event-end event))
|
|
280 (window (nth 0 end-position))
|
|
281 (before-scroll))
|
|
282 (with-current-buffer (window-buffer window)
|
|
283 (setq before-scroll point-before-scroll))
|
23248
|
284 (unwind-protect
|
|
285 (save-selected-window
|
|
286 (let ((portion-whole (nth 2 end-position)))
|
|
287 (select-window window)
|
|
288 (setq before-scroll
|
|
289 (or before-scroll (point)))
|
|
290 (scroll-up
|
|
291 (scroll-bar-scale portion-whole (1- (window-height)))))
|
|
292 (sit-for 0))
|
|
293 (with-current-buffer (window-buffer window)
|
|
294 (setq point-before-scroll before-scroll)))))
|
1772
|
295
|
|
296
|
24981
|
297 ;;; Tookit scroll bars.
|
|
298
|
|
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)
|
27421
|
305 (cond ((eq part 'end-scroll))
|
24981
|
306 (t
|
|
307 (with-current-buffer (window-buffer window)
|
|
308 (setq before-scroll point-before-scroll))
|
|
309 (save-selected-window
|
|
310 (select-window window)
|
|
311 (setq before-scroll (or before-scroll (point)))
|
|
312 (cond ((eq part 'above-handle)
|
|
313 (scroll-up '-))
|
|
314 ((eq part 'below-handle)
|
|
315 (scroll-up nil))
|
26510
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
316 ((eq part 'ratio)
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
317 (let* ((portion-whole (nth 2 end-position))
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
318 (lines (scroll-bar-scale portion-whole
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
319 (1- (window-height)))))
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
320 (scroll-up (cond ((not (zerop lines)) lines)
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
321 ((< (car portion-whole) 0) -1)
|
6866d2a9a421
(scroll-bar-toolkit-scroll): add handling of the `ratio'
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
322 (t 1)))))
|
24981
|
323 ((eq part 'up)
|
|
324 (scroll-up -1))
|
|
325 ((eq part 'down)
|
|
326 (scroll-up 1))
|
|
327 ((eq part 'top)
|
|
328 (set-window-start window (point-min)))
|
|
329 ((eq part 'bottom)
|
|
330 (goto-char (point-max))
|
|
331 (recenter))
|
|
332 ((eq part 'handle)
|
|
333 (scroll-bar-drag-1 event))))
|
|
334 (sit-for 0)
|
|
335 (with-current-buffer (window-buffer window)
|
|
336 (setq point-before-scroll before-scroll))))))
|
|
337
|
|
338
|
|
339
|
1772
|
340 ;;;; Bindings.
|
|
341
|
|
342 ;;; For now, we'll set things up to work like xterm.
|
36803
|
343 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
|
24981
|
344 (global-set-key [vertical-scroll-bar mouse-1]
|
|
345 'scroll-bar-toolkit-scroll))
|
|
346 (t
|
|
347 (global-set-key [vertical-scroll-bar mouse-1]
|
|
348 'scroll-bar-scroll-up)
|
|
349 (global-set-key [vertical-scroll-bar drag-mouse-1]
|
|
350 'scroll-bar-scroll-up)
|
|
351 (global-set-key [vertical-scroll-bar down-mouse-2]
|
|
352 'scroll-bar-drag)
|
|
353 (global-set-key [vertical-scroll-bar mouse-3]
|
|
354 'scroll-bar-scroll-down)
|
|
355 (global-set-key [vertical-scroll-bar drag-mouse-3]
|
|
356 'scroll-bar-scroll-down)))
|
1772
|
357
|
|
358
|
1973
3a08dacd8bfb
These are in preparation for a more thorough renaming to occur soon.
Jim Blandy <jimb@redhat.com>
diff
changeset
|
359 (provide 'scroll-bar)
|
1772
|
360
|
1981
|
361 ;;; scroll-bar.el ends here
|