Commit | Line | Data |
---|---|---|
aae56ea7 | 1 | ;;; scroll-bar.el --- window system-independent scroll bar support. |
6d62a90e | 2 | |
b578f267 | 3 | ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. |
6d62a90e JB |
4 | |
5 | ;; Maintainer: FSF | |
6 | ;; Keywords: hardware | |
7 | ||
b578f267 | 8 | ;; This file is part of GNU Emacs. |
6d62a90e | 9 | |
b578f267 EN |
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. | |
6d62a90e | 14 | |
b578f267 EN |
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. | |
6d62a90e | 19 | |
b578f267 EN |
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. | |
76550a57 | 24 | |
d9ecc911 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; Window-system-independent bindings of mouse clicks on the scroll bar. | |
28 | ;; Presently emulates the scroll-bar behavior of xterm. | |
b578f267 | 29 | |
d9ecc911 ER |
30 | ;;; Code: |
31 | ||
dbc4e1c1 JB |
32 | (require 'mouse) |
33 | ||
6d62a90e JB |
34 | \f |
35 | ;;;; Utilities. | |
36 | ||
86828678 RS |
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 | ||
bf3c8a70 | 44 | (defun scroll-bar-scale (num-denom whole) |
6d62a90e | 45 | "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM). |
bf3c8a70 JB |
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 | |
6d62a90e | 49 | \(buffer-size)) is the position in the current buffer corresponding to |
bf3c8a70 | 50 | that scroll bar position." |
6d62a90e JB |
51 | ;; We multiply before we divide to maintain precision. |
52 | ;; We use floating point because the product of a large buffer size | |
bf3c8a70 | 53 | ;; with a large scroll bar portion can easily overflow a lisp int. |
6d62a90e JB |
54 | (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom)))) |
55 | ||
56 | \f | |
fe48f821 | 57 | ;;;; Helpful functions for enabling and disabling scroll bars. |
fe48f821 JB |
58 | |
59 | (defun scroll-bar-mode (flag) | |
60 | "Toggle display of vertical scroll bars on each frame. | |
61 | This command applies to all frames that exist and frames to be | |
62 | created in the future. | |
63 | With a numeric argument, if the argument is negative, | |
64 | turn off scroll bars; otherwise, turn on scroll bars." | |
65 | (interactive "P") | |
6ee9f953 | 66 | (if flag (setq flag (prefix-numeric-value flag))) |
ada464a7 JB |
67 | |
68 | ;; Obtain the current setting by looking at default-frame-alist. | |
69 | (let ((scroll-bar-mode | |
70 | (let ((assq (assq 'vertical-scroll-bars default-frame-alist))) | |
71 | (if assq (cdr assq) t)))) | |
72 | ||
73 | ;; Tweedle it according to the argument. | |
74 | (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode) | |
75 | (or (not (numberp flag)) (>= flag 0)))) | |
76 | ||
77 | ;; Apply it to default-frame-alist. | |
78 | (mapcar | |
79 | (function | |
80 | (lambda (param-name) | |
81 | (let ((parameter (assq param-name default-frame-alist))) | |
82 | (if (consp parameter) | |
83 | (setcdr parameter scroll-bar-mode) | |
84 | (setq default-frame-alist | |
85 | (cons (cons param-name scroll-bar-mode) | |
86 | default-frame-alist)))))) | |
87 | '(vertical-scroll-bars horizontal-scroll-bars)) | |
88 | ||
89 | ;; Apply it to existing frames. | |
90 | (let ((frames (frame-list))) | |
91 | (while frames | |
92 | (modify-frame-parameters | |
93 | (car frames) | |
94 | (list (cons 'vertical-scroll-bars scroll-bar-mode) | |
95 | (cons 'horizontal-scroll-bars scroll-bar-mode))) | |
96 | (setq frames (cdr frames)))))) | |
fe48f821 | 97 | \f |
bf3c8a70 | 98 | ;;;; Buffer navigation using the scroll bar. |
6d62a90e | 99 | |
e532b016 | 100 | ;;; This was used for up-events on button 2, but no longer. |
bf3c8a70 JB |
101 | (defun scroll-bar-set-window-start (event) |
102 | "Set the window start according to where the scroll bar is dragged. | |
103 | EVENT should be a scroll bar click or drag event." | |
6d62a90e | 104 | (interactive "e") |
dbc4e1c1 | 105 | (let* ((end-position (event-end event)) |
6d62a90e JB |
106 | (window (nth 0 end-position)) |
107 | (portion-whole (nth 2 end-position))) | |
108 | (save-excursion | |
109 | (set-buffer (window-buffer window)) | |
110 | (save-excursion | |
4cad38d5 JB |
111 | (goto-char (+ (point-min) |
112 | (scroll-bar-scale portion-whole | |
113 | (- (point-max) (point-min))))) | |
6d62a90e JB |
114 | (beginning-of-line) |
115 | (set-window-start window (point)))))) | |
116 | ||
aeee66be RS |
117 | (defun scroll-bar-drag-position (portion-whole) |
118 | "Calculate new window start for drag event." | |
119 | (save-excursion | |
120 | (goto-char (+ (point-min) | |
121 | (scroll-bar-scale portion-whole | |
122 | (- (point-max) (point-min))))) | |
123 | (beginning-of-line) | |
124 | (point))) | |
125 | ||
126 | (defun scroll-bar-maybe-set-window-start (event) | |
127 | "Set the window start according to where the scroll bar is dragged. | |
128 | Only change window start if the new start is substantially different. | |
129 | EVENT should be a scroll bar click or drag event." | |
130 | (interactive "e") | |
131 | (let* ((end-position (event-end event)) | |
132 | (window (nth 0 end-position)) | |
133 | (portion-whole (nth 2 end-position)) | |
134 | (next-portion-whole (cons (1+ (car portion-whole)) | |
135 | (cdr portion-whole))) | |
136 | portion-start | |
137 | next-portion-start | |
138 | (current-start (window-start window))) | |
139 | (save-excursion | |
140 | (set-buffer (window-buffer window)) | |
141 | (setq portion-start (scroll-bar-drag-position portion-whole)) | |
142 | (setq next-portion-start (max | |
143 | (scroll-bar-drag-position next-portion-whole) | |
144 | (1+ portion-start))) | |
145 | (if (or (> current-start next-portion-start) | |
146 | (< current-start portion-start)) | |
9c005c65 KH |
147 | (set-window-start window portion-start) |
148 | ;; Always set window start, to ensure scroll bar position is updated. | |
149 | (set-window-start window current-start))))) | |
aeee66be | 150 | |
e532b016 RS |
151 | ;; Scroll the window to the proper position for EVENT. |
152 | (defun scroll-bar-drag-1 (event) | |
153 | (let* ((start-position (event-start event)) | |
154 | (window (nth 0 start-position)) | |
155 | (portion-whole (nth 2 start-position))) | |
156 | (save-excursion | |
157 | (set-buffer (window-buffer window)) | |
d2ae6f7e RS |
158 | ;; Calculate position relative to the accessible part of the buffer. |
159 | (goto-char (+ (point-min) | |
160 | (scroll-bar-scale portion-whole | |
161 | (- (point-max) (point-min))))) | |
e532b016 RS |
162 | (beginning-of-line) |
163 | (set-window-start window (point))))) | |
164 | ||
165 | (defun scroll-bar-drag (event) | |
166 | "Scroll the window by dragging the scroll bar slider. | |
167 | If you click outside the slider, the window scrolls to bring the slider there." | |
168 | (interactive "e") | |
c782bea5 RS |
169 | (let* (done |
170 | (echo-keystrokes 0)) | |
8f37c4f3 RS |
171 | (or point-before-scroll |
172 | (setq point-before-scroll (point))) | |
6538766a | 173 | ;; Our scrolling can move point; don't let that clear point-before-scroll. |
8f37c4f3 | 174 | (let (point-before-scroll) |
6538766a | 175 | (scroll-bar-drag-1 event) |
8f37c4f3 RS |
176 | (track-mouse |
177 | (while (not done) | |
178 | (setq event (read-event)) | |
179 | (if (eq (car-safe event) 'mouse-movement) | |
180 | (setq event (read-event))) | |
181 | (cond ((eq (car-safe event) 'scroll-bar-movement) | |
182 | (scroll-bar-drag-1 event)) | |
183 | (t | |
184 | ;; Exit when we get the drag event; ignore that event. | |
6538766a KH |
185 | (setq done t))))) |
186 | (sit-for 0)))) | |
e532b016 | 187 | |
bf3c8a70 JB |
188 | (defun scroll-bar-scroll-down (event) |
189 | "Scroll the window's top line down to the location of the scroll bar click. | |
190 | EVENT should be a scroll bar click." | |
6d62a90e JB |
191 | (interactive "e") |
192 | (let ((old-selected-window (selected-window))) | |
193 | (unwind-protect | |
194 | (progn | |
dbc4e1c1 | 195 | (let* ((end-position (event-end event)) |
6d62a90e JB |
196 | (window (nth 0 end-position)) |
197 | (portion-whole (nth 2 end-position))) | |
8f37c4f3 RS |
198 | (let (point-before-scroll) |
199 | (select-window window)) | |
200 | (or point-before-scroll | |
201 | (setq point-before-scroll (point))) | |
202 | (let (point-before-scroll) | |
203 | (scroll-down | |
204 | (scroll-bar-scale portion-whole (1- (window-height))))))) | |
6d62a90e JB |
205 | (select-window old-selected-window)))) |
206 | ||
bf3c8a70 JB |
207 | (defun scroll-bar-scroll-up (event) |
208 | "Scroll the line next to the scroll bar click to the top of the window. | |
209 | EVENT should be a scroll bar click." | |
6d62a90e JB |
210 | (interactive "e") |
211 | (let ((old-selected-window (selected-window))) | |
212 | (unwind-protect | |
213 | (progn | |
dbc4e1c1 | 214 | (let* ((end-position (event-end event)) |
6d62a90e JB |
215 | (window (nth 0 end-position)) |
216 | (portion-whole (nth 2 end-position))) | |
8f37c4f3 RS |
217 | (let (point-before-scroll) |
218 | (select-window window)) | |
219 | (or point-before-scroll | |
220 | (setq point-before-scroll (point))) | |
221 | (let (point-before-scroll) | |
222 | (scroll-up | |
223 | (scroll-bar-scale portion-whole (1- (window-height))))))) | |
6d62a90e JB |
224 | (select-window old-selected-window)))) |
225 | ||
226 | \f | |
227 | ;;;; Bindings. | |
228 | ||
229 | ;;; For now, we'll set things up to work like xterm. | |
bf3c8a70 JB |
230 | (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up) |
231 | (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up) | |
6d62a90e | 232 | |
e532b016 RS |
233 | (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag) |
234 | ||
bf3c8a70 JB |
235 | (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down) |
236 | (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down) | |
6d62a90e JB |
237 | |
238 | \f | |
dc14eed2 | 239 | (provide 'scroll-bar) |
6d62a90e | 240 | |
bf3c8a70 | 241 | ;;; scroll-bar.el ends here |