comparison lisp/mouse-drag.el @ 16319:8bd3981bd342

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Wed, 25 Sep 1996 03:30:55 +0000
parents
children 56f494ed6b13
comparison
equal deleted inserted replaced
16318:86f1da650461 16319:8bd3981bd342
1 ;;; mouse-drag.el
2 ;;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;;; Author: John Heidemann <johnh@ISI.EDU>
5 ;;; Keywords: mouse
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; What is ``mouse-drag.el''?
27 ;;;
28 ;;; Doesn't that scroll bar seem far away when you want to scroll?
29 ;;; This module overloads mouse-2 to do ``throw'' scrolling. You
30 ;;; click and drag. The distance you move from your original click
31 ;;; turns into a scroll amount. The scroll amount is scaled
32 ;;; exponentially to make both large moves and short adjustments easy.
33 ;;; What this boils down to is that you can easily scroll around the
34 ;;; buffer without much mouse movement. Finally, clicks which aren't
35 ;;; drags are passed off to the old mouse-2 binding, so old mouse-2
36 ;;; operations (find-file in dired-mode, yanking in most other modes)
37 ;;; still work.
38 ;;;
39 ;;; There is an alternative way to scroll, ``drag'' scrolling. You
40 ;;; can click on a character and then drag it around, scrolling the
41 ;;; buffer with you. The character always stays under the mouse.
42 ;;; Compared to throw-scrolling, this approach provides direct
43 ;;; manipulation (nice) but requires more mouse movement
44 ;;; (unfortunate). It is offered as an alternative for those who
45 ;;; prefer it.
46 ;;;
47 ;;; If you like mouse-drag, you should also check out mouse-copy
48 ;;; for ``one-click text copy and move''.
49 ;;;
50 ;;; To use mouse-drag, place the following in your .emacs file:
51 ;;; (require 'mouse-drag)
52 ;;; -and either-
53 ;;; (global-set-key [down-mouse-2] 'mouse-drag-throw)
54 ;;; -or-
55 ;;; (global-set-key [down-mouse-2] 'mouse-drag-drag)
56 ;;;
57 ;;;
58 ;;;
59 ;;; Options:
60 ;;;
61 ;;; - reverse the throw-scroll direction with \\[mouse-throw-with-scroll-bar]
62 ;;; - work around a bug with \\[mouse-extras-work-around-drag-bug]
63 ;;;
64 ;;;
65 ;;; History and related work:
66 ;;;
67 ;;; One-click copying and moving was inspired by lemacs-19.8.
68 ;;; Throw-scrolling was inspired by MacPaint's ``hand'' and by Tk's
69 ;;; mouse-2 scrolling. The package mouse-scroll.el by Tom Wurgler
70 ;;; <twurgler@goodyear.com> is similar to mouse-drag-throw, but
71 ;;; doesn't pass clicks through.
72 ;;;
73 ;;; These functions have been tested in emacs version 19.30,
74 ;;; and this package has run in the past on 19.25-19.29.
75 ;;;
76 ;;; Originally mouse-drag was part of a larger package.
77 ;;; As of 11 July 96 the scrolling functions were split out
78 ;;; in preparation for incorporation into (the future) emacs-19.32.
79 ;;;
80 ;;;
81 ;;; Thanks:
82 ;;;
83 ;;; Thanks to Kai Grossjohann
84 ;;; <grossjoh@dusty.informatik.uni-dortmund.de> for reporting bugs, to
85 ;;; Tom Wurgler <twurgler@goodyear.com> for reporting bugs and
86 ;;; suggesting fixes, and to Joel Graber <jgraber@ti.com> for
87 ;;; prompting me to do drag-scrolling and for an initial
88 ;;; implementation of horizontal drag-scrolling.
89 ;;;
90 ;;; -johnh@isi.edu, 11-Jul-96
91 ;;;
92 ;;;
93 ;;; Old changes, for reference:
94 ;;;
95 ;;; What's new with mouse-extras 2.21?
96 ;;;
97 ;;; - support for emacs-19.{29,30}
98 ;;; - point now stays on the visible screen during horizontal scrolling
99 ;;; (bug identified and fix suggested by Tom Wurgler <twurgler@goodyear.com>)
100 ;;; - better work-around for lost-mouse-events bug (supports double/triple
101 ;;; clicks), see \\[mouse-extras-work-around-drag-bug] for details.
102 ;;; - work-around for lost-mouse-events bug now is OFF by default;
103 ;;; enable it if you have problems
104 ;;;
105
106
107
108 ;;; Code:
109
110 ;;
111 ;; scrolling code
112 ;;
113
114 (defun mouse-drag-safe-scroll (row-delta &optional col-delta)
115 "* Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
116 Keep the cursor on the screen as needed."
117 (if (and row-delta
118 (/= 0 row-delta))
119 (condition-case nil ;; catch and ignore movement errors
120 (scroll-down row-delta)
121 (beginning-of-buffer (message "Beginning of buffer"))
122 (end-of-buffer (message "End of buffer"))))
123 (if (and col-delta
124 (/= 0 col-delta))
125 (progn
126 (scroll-right col-delta)
127 ;; Make sure that the point stays on the visible screen
128 ;; (if truncation-lines in set).
129 ;; This code mimics the behavior we automatically get
130 ;; when doing vertical scrolling.
131 ;; Problem identified and a fix suggested by Tom Wurgler.
132 (cond
133 ((< (current-column) (window-hscroll))
134 (move-to-column (window-hscroll))) ; make on left column
135 ((> (- (current-column) (window-hscroll) (window-width) -2) 0)
136 (move-to-column (+ (window-width) (window-hscroll) -3)))))))
137
138 (defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
139 "* Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
140 (while (sit-for mouse-scroll-delay)
141 (mouse-drag-safe-scroll row-delta col-delta)))
142
143 (defun mouse-drag-events-are-point-events-p (start-posn end-posn)
144 "* Determine if START-POSN and END-POSN are \"close\"."
145 (let*
146 ((start-col-row (posn-col-row start-posn))
147 (end-col-row (posn-col-row end-posn)))
148 (and
149 ;; We no longer exclude things by time.
150 ;; (< (- (posn-timestamp end-posn) (posn-timestamp start-posn))
151 ;; (if (numberp double-click-time)
152 ;; (* 2 double-click-time) ;; stretch it a little
153 ;; 999999)) ;; non-numeric => check by position alone
154 (= (car start-col-row) (car end-col-row))
155 (= (cdr start-col-row) (cdr end-col-row)))))
156
157 (defun mouse-drag-should-do-col-scrolling ()
158 "* Determine if it's wise to enable col-scrolling for the current window."
159 (or truncate-lines
160 (> (window-hscroll (selected-window)) 0)
161 (< (window-width) (screen-width))))
162
163 (defvar mouse-throw-with-scroll-bar nil
164 "* Set direction of mouse-throwing.
165 If nil, the text moves in the direction the mouse moves.
166 If t, the scroll bar moves in the direction the mouse moves.")
167 (defconst mouse-throw-magnifier-with-scroll-bar
168 [-16 -8 -4 -2 -1 0 0 0 1 2 4 8 16])
169 (defconst mouse-throw-magnifier-with-mouse-movement
170 [ 16 8 4 2 1 0 0 0 -1 -2 -4 -8 -16])
171 (defconst mouse-throw-magnifier-min -6)
172 (defconst mouse-throw-magnifier-max 6)
173
174 (defun mouse-drag-throw (start-event)
175 "\"Throw\" the page according to a mouse drag.
176
177 A \"throw\" is scrolling the page at a speed relative to the distance
178 from the original mouse click to the current mouse location. Try it;
179 you'll like it. It's easier to observe than to explain.
180
181 If the mouse is clicked and released in the same place of time we
182 assume that the user didn't want to scdebugroll but wanted to whatever
183 mouse-2 used to do, so we pass it through.
184
185 Throw scrolling was inspired (but is not identical to) the \"hand\"
186 option in MacPaint, or the middle button in Tk text widgets.
187
188 If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
189 in the opposite direction. (Different people have different ideas
190 about which direction is natural. Perhaps it has to do with which
191 hemisphere you're in.)
192
193 To test this function, evaluate:
194 (global-set-key [down-mouse-2] 'mouse-drag-throw)"
195 (interactive "e")
196 ;; we want to do save-selected-window, but that requires 19.29
197 (let* ((start-posn (event-start start-event))
198 (start-window (posn-window start-posn))
199 (start-row (cdr (posn-col-row start-posn)))
200 (start-col (car (posn-col-row start-posn)))
201 (old-selected-window (selected-window))
202 event end row mouse-delta scroll-delta
203 have-scrolled point-event-p old-binding
204 window-last-row
205 col mouse-col-delta window-last-col
206 (scroll-col-delta 0)
207 adjusted-mouse-col-delta
208 ;; be conservative about allowing horizontal scrolling
209 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
210 (select-window start-window)
211 (track-mouse
212 (while (progn
213 (setq event (read-event)
214 end (event-end event)
215 row (cdr (posn-col-row end))
216 col (car (posn-col-row end)))
217 (or (mouse-movement-p event)
218 (eq (car-safe event) 'switch-frame)))
219 (if (eq start-window (posn-window end))
220 (progn
221 (setq mouse-delta (- start-row row)
222 adjusted-mouse-delta
223 (- (cond
224 ((<= mouse-delta mouse-throw-magnifier-min)
225 mouse-throw-magnifier-min)
226 ((>= mouse-delta mouse-throw-magnifier-max)
227 mouse-throw-magnifier-max)
228 (t mouse-delta))
229 mouse-throw-magnifier-min)
230 scroll-delta (aref (if mouse-throw-with-scroll-bar
231 mouse-throw-magnifier-with-scroll-bar
232 mouse-throw-magnifier-with-mouse-movement)
233 adjusted-mouse-delta))
234 (if col-scrolling-p
235 (setq mouse-col-delta (- start-col col)
236 adjusted-mouse-col-delta
237 (- (cond
238 ((<= mouse-col-delta mouse-throw-magnifier-min)
239 mouse-throw-magnifier-min)
240 ((>= mouse-col-delta mouse-throw-magnifier-max)
241 mouse-throw-magnifier-max)
242 (t mouse-col-delta))
243 mouse-throw-magnifier-min)
244 scroll-col-delta (aref (if mouse-throw-with-scroll-bar
245 mouse-throw-magnifier-with-scroll-bar
246 mouse-throw-magnifier-with-mouse-movement)
247 adjusted-mouse-col-delta)))))
248 (if (or (/= 0 scroll-delta)
249 (/= 0 scroll-col-delta))
250 (progn
251 (setq have-scrolled t)
252 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
253 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
254 ;; If it was a click and not a drag, prepare to pass the event on.
255 ;; Note: We must determine the pass-through event before restoring
256 ;; the window, but invoke it after. Sigh.
257 (if (and (not have-scrolled)
258 (mouse-drag-events-are-point-events-p start-posn end))
259 (setq point-event-p t
260 old-binding (key-binding
261 (vector (event-basic-type start-event)))))
262 ;; Now restore the old window.
263 (select-window old-selected-window)
264 ;; For clicks, call the old function.
265 (if point-event-p
266 (call-interactively old-binding))))
267
268 (defun mouse-drag-drag (start-event)
269 "\"Drag\" the page according to a mouse drag.
270
271 Drag scrolling moves the page according to the movement of the mouse.
272 You \"grab\" the character under the mouse and move it around.
273
274 If the mouse is clicked and released in the same place of time we
275 assume that the user didn't want to scroll but wanted to whatever
276 mouse-2 used to do, so we pass it through.
277
278 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
279 middle button in Tk text widgets.
280
281 To test this function, evaluate:
282 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
283 (interactive "e")
284 ;; we want to do save-selected-window, but that requires 19.29
285 (let* ((start-posn (event-start start-event))
286 (start-window (posn-window start-posn))
287 (start-row (cdr (posn-col-row start-posn)))
288 (start-col (car (posn-col-row start-posn)))
289 (old-selected-window (selected-window))
290 event end row mouse-delta scroll-delta
291 have-scrolled point-event-p old-binding
292 window-last-row
293 col mouse-col-delta window-last-col
294 (scroll-col-delta 0)
295 ;; be conservative about allowing horizontal scrolling
296 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
297 (select-window start-window)
298 (setq window-last-row (- (window-height) 2)
299 window-last-col (- (window-width) 2))
300 (track-mouse
301 (while (progn
302 (setq event (read-event)
303 end (event-end event)
304 row (cdr (posn-col-row end))
305 col (car (posn-col-row end)))
306 (or (mouse-movement-p event)
307 (eq (car-safe event) 'switch-frame)))
308 ;; Scroll if see if we're on the edge.
309 ;; NEEDSWORK: should handle mouse-in-other window.
310 (cond
311 ((not (eq start-window (posn-window end)))
312 t) ; wait for return to original window
313 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
314 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
315 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
316 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
317 (t
318 (setq scroll-delta (- row start-row)
319 start-row row)
320 (if col-scrolling-p
321 (setq scroll-col-delta (- col start-col)
322 start-col col))
323 (if (or (/= 0 scroll-delta)
324 (/= 0 scroll-col-delta))
325 (progn
326 (setq have-scrolled t)
327 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
328 ;; If it was a click and not a drag, prepare to pass the event on.
329 ;; Note: We must determine the pass-through event before restoring
330 ;; the window, but invoke it after. Sigh.
331 (if (and (not have-scrolled)
332 (mouse-drag-events-are-point-events-p start-posn end))
333 (setq point-event-p t
334 old-binding (key-binding
335 (vector (event-basic-type start-event)))))
336 ;; Now restore the old window.
337 (select-window old-selected-window)
338 ;; For clicks, call the old function.
339 (if point-event-p
340 (call-interactively old-binding))))
341
342 (provide 'mouse-drag)
343
344 ;;; mouse-drag.el ends here