]> code.delx.au - gnu-emacs/blob - lisp/mouse-drag.el
Merge from emacs-23; up to 2010-06-08T03:06:47Z!dann@ics.uci.edu.
[gnu-emacs] / lisp / mouse-drag.el
1 ;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
2
3 ;; Copyright (C) 1996-1997, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: John Heidemann <johnh@ISI.EDU>
6 ;; Keywords: mouse
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; What is ``mouse-drag.el''?
26 ;;
27 ;; Doesn't that scroll bar seem far away when you want to scroll?
28 ;; This module overloads mouse-2 to do ``throw'' scrolling. You
29 ;; click and drag. The distance you move from your original click
30 ;; turns into a scroll amount. The scroll amount is scaled
31 ;; exponentially to make both large moves and short adjustments easy.
32 ;; What this boils down to is that you can easily scroll around the
33 ;; buffer without much mouse movement. Finally, clicks which aren't
34 ;; drags are passed off to the old mouse-2 binding, so old mouse-2
35 ;; operations (find-file in dired-mode, yanking in most other modes)
36 ;; still work.
37 ;;
38 ;; There is an alternative way to scroll, ``drag'' scrolling. You
39 ;; can click on a character and then drag it around, scrolling the
40 ;; buffer with you. The character always stays under the mouse.
41 ;; Compared to throw-scrolling, this approach provides direct
42 ;; manipulation (nice) but requires more mouse movement
43 ;; (unfortunate). It is offered as an alternative for those who
44 ;; prefer it.
45 ;;
46 ;; If you like mouse-drag, you should also check out mouse-copy
47 ;; for ``one-click text copy and move''.
48 ;;
49 ;; To use mouse-drag, place the following in your .emacs file:
50 ;; -either-
51 ;; (global-set-key [down-mouse-2] 'mouse-drag-throw)
52 ;; -or-
53 ;; (global-set-key [down-mouse-2] 'mouse-drag-drag)
54 ;;
55 ;;
56 ;;
57 ;; Options:
58 ;;
59 ;; - reverse the throw-scroll direction with \\[mouse-throw-with-scroll-bar]
60 ;; - work around a bug with \\[mouse-extras-work-around-drag-bug]
61 ;; - auto-enable horizontal scrolling with
62 ;; \\[mouse-drag-electric-col-scrolling]
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 ;; Thanks:
81 ;;
82 ;; Thanks to Kai Grossjohann
83 ;; <grossjoh@dusty.informatik.uni-dortmund.de> for reporting bugs, to
84 ;; Tom Wurgler <twurgler@goodyear.com> for reporting bugs and
85 ;; suggesting fixes, and to Joel Graber <jgraber@ti.com> for
86 ;; prompting me to do drag-scrolling and for an initial
87 ;; implementation of horizontal drag-scrolling.
88 ;;
89 ;; -johnh@isi.edu, 11-Jul-96
90 ;;
91 ;;
92 ;; What's new with mouse-drag 2.24?
93 ;;
94 ;; - mouse-drag-electric-col-scrolling (default: on)
95 ;; auto-enables horizontal scrolling when clicks on wrapped
96 ;; lines occur
97
98 ;; TODO:
99 ;; - For mouse-drag-throw, we should try and place some visual indicator
100 ;; of the original mouse position (like Firefox does).
101
102 ;;; Code:
103
104 ;;
105 ;; scrolling code
106 ;;
107
108 (defun mouse-drag-safe-scroll (row-delta &optional col-delta)
109 "Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
110 Keep the cursor on the screen as needed."
111 (let ((scroll-preserve-screen-position nil))
112 (if (and row-delta
113 (/= 0 row-delta))
114 (condition-case nil ;; catch and ignore movement errors
115 (scroll-down row-delta)
116 (beginning-of-buffer (message "Beginning of buffer"))
117 (end-of-buffer (message "End of buffer"))))
118 (if (and col-delta
119 (/= 0 col-delta))
120 (progn
121 (scroll-right col-delta)
122 ;; Make sure that the point stays on the visible screen
123 ;; (if truncation-lines in set).
124 ;; This code mimics the behavior we automatically get
125 ;; when doing vertical scrolling.
126 ;; Problem identified and a fix suggested by Tom Wurgler.
127 (cond
128 ((< (current-column) (window-hscroll))
129 (move-to-column (window-hscroll))) ; make on left column
130 ((> (- (current-column) (window-hscroll) (window-width) -2) 0)
131 (move-to-column (+ (window-width) (window-hscroll) -3))))))))
132
133 (defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
134 "Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
135 (while (sit-for mouse-scroll-delay)
136 (mouse-drag-safe-scroll row-delta col-delta)))
137
138 (defun mouse-drag-events-are-point-events-p (start-posn end-posn)
139 "Determine if START-POSN and END-POSN are \"close\"."
140 (let*
141 ((start-col-row (posn-col-row start-posn))
142 (end-col-row (posn-col-row end-posn)))
143 (and
144 ;; ;; We no longer exclude things by time.
145 ;; (< (- (posn-timestamp end-posn) (posn-timestamp start-posn))
146 ;; (if (numberp double-click-time)
147 ;; (* 2 double-click-time) ;; stretch it a little
148 ;; 999999)) ;; non-numeric => check by position alone
149 (= (car start-col-row) (car end-col-row))
150 (= (cdr start-col-row) (cdr end-col-row)))))
151
152 (defvar mouse-drag-electric-col-scrolling t
153 "If non-nil, mouse-drag on a long line enables truncate-lines.")
154
155 (defun mouse-drag-should-do-col-scrolling ()
156 "Determine if it's wise to enable col-scrolling for the current window.
157 Basically, we check for existing horizontal scrolling."
158 (or truncate-lines
159 (> (window-hscroll (selected-window)) 0)
160 (not (window-full-width-p))
161 (and
162 mouse-drag-electric-col-scrolling
163 (save-excursion ;; on a long line?
164 (let
165 ((beg (line-beginning-position))
166 (end (progn (end-of-line) (point))))
167 (if (> (- end beg) (window-width))
168 (setq truncate-lines t)
169 nil))))))
170
171 (defvar mouse-throw-with-scroll-bar nil
172 "*Set direction of mouse-throwing.
173 If nil, the text moves in the direction the mouse moves.
174 If t, the scroll bar moves in the direction the mouse moves.")
175 (defconst mouse-throw-magnifier-min -6)
176 (defconst mouse-throw-magnifier-max 6)
177 (defconst mouse-throw-magnifier-base 1.5)
178
179 (defun mouse-drag-scroll-delta (mouse-delta)
180 ;; Limit the exponential explosion.
181 (setq mouse-delta
182 (max mouse-throw-magnifier-min
183 (min mouse-throw-magnifier-max mouse-delta)))
184 (* (round (exp (* (log mouse-throw-magnifier-base) (abs mouse-delta))))
185 (if (< mouse-delta 0) -1 1)
186 (if mouse-throw-with-scroll-bar 1 -1)))
187
188 ;;;###autoload
189 (defun mouse-drag-throw (start-event)
190 "\"Throw\" the page according to a mouse drag.
191
192 A \"throw\" is scrolling the page at a speed relative to the distance
193 from the original mouse click to the current mouse location. Try it;
194 you'll like it. It's easier to observe than to explain.
195
196 If the mouse is clicked and released in the same place of time we
197 assume that the user didn't want to scdebugroll but wanted to whatever
198 mouse-2 used to do, so we pass it through.
199
200 Throw scrolling was inspired (but is not identical to) the \"hand\"
201 option in MacPaint, or the middle button in Tk text widgets.
202
203 If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
204 in the opposite direction. (Different people have different ideas
205 about which direction is natural. Perhaps it has to do with which
206 hemisphere you're in.)
207
208 To test this function, evaluate:
209 (global-set-key [down-mouse-2] 'mouse-drag-throw)"
210 (interactive "e")
211 ;; we want to do save-selected-window, but that requires 19.29
212 (let* ((start-posn (event-start start-event))
213 (start-window (posn-window start-posn))
214 (start-row (cdr (posn-col-row start-posn)))
215 (start-col (car (posn-col-row start-posn)))
216 (old-selected-window (selected-window))
217 event end row mouse-delta scroll-delta
218 have-scrolled
219 window-last-row
220 col mouse-col-delta window-last-col
221 (scroll-col-delta 0)
222 adjusted-mouse-col-delta
223 adjusted-mouse-delta
224 ;; be conservative about allowing horizontal scrolling
225 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
226 (select-window start-window)
227 (track-mouse
228 (while (progn
229 (setq event (read-event)
230 end (event-end event)
231 row (cdr (posn-col-row end))
232 col (car (posn-col-row end)))
233 (or (mouse-movement-p event)
234 (eq (car-safe event) 'switch-frame)))
235 (when (eq start-window (posn-window end))
236 (when col-scrolling-p
237 (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
238 (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
239
240 (if (or (/= 0 scroll-delta)
241 (/= 0 scroll-col-delta))
242 (progn
243 (setq have-scrolled t)
244 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
245 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
246 ;; If it was a click and not a drag, prepare to pass the event on.
247 ;; Is there a more correct way to reconstruct the event?
248 (if (and (not have-scrolled)
249 (mouse-drag-events-are-point-events-p start-posn end))
250 (push (cons (event-basic-type start-event) (cdr start-event))
251 unread-command-events))
252 ;; Now restore the old window.
253 (select-window old-selected-window)))
254
255 ;;;###autoload
256 (defun mouse-drag-drag (start-event)
257 "\"Drag\" the page according to a mouse drag.
258
259 Drag scrolling moves the page according to the movement of the mouse.
260 You \"grab\" the character under the mouse and move it around.
261
262 If the mouse is clicked and released in the same place of time we
263 assume that the user didn't want to scroll but wanted to whatever
264 mouse-2 used to do, so we pass it through.
265
266 Drag scrolling is identical to the \"hand\" option in MacPaint, or the
267 middle button in Tk text widgets.
268
269 To test this function, evaluate:
270 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
271 (interactive "e")
272 ;; we want to do save-selected-window, but that requires 19.29
273 (let* ((start-posn (event-start start-event))
274 (start-window (posn-window start-posn))
275 (start-row (cdr (posn-col-row start-posn)))
276 (start-col (car (posn-col-row start-posn)))
277 (old-selected-window (selected-window))
278 event end row mouse-delta scroll-delta
279 have-scrolled
280 window-last-row
281 col mouse-col-delta window-last-col
282 (scroll-col-delta 0)
283 ;; be conservative about allowing horizontal scrolling
284 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
285 (select-window start-window)
286 (setq window-last-row (- (window-height) 2)
287 window-last-col (- (window-width) 2))
288 (track-mouse
289 (while (progn
290 (setq event (read-event)
291 end (event-end event)
292 row (cdr (posn-col-row end))
293 col (car (posn-col-row end)))
294 (or (mouse-movement-p event)
295 (eq (car-safe event) 'switch-frame)))
296 ;; Scroll if see if we're on the edge.
297 ;; NEEDSWORK: should handle mouse-in-other window.
298 (cond
299 ((not (eq start-window (posn-window end)))
300 t) ; wait for return to original window
301 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
302 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
303 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
304 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
305 (t
306 (setq scroll-delta (- row start-row)
307 start-row row)
308 (if col-scrolling-p
309 (setq scroll-col-delta (- col start-col)
310 start-col col))
311 (if (or (/= 0 scroll-delta)
312 (/= 0 scroll-col-delta))
313 (progn
314 (setq have-scrolled t)
315 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
316 ;; If it was a click and not a drag, prepare to pass the event on.
317 ;; Is there a more correct way to reconstruct the event?
318 (if (and (not have-scrolled)
319 (mouse-drag-events-are-point-events-p start-posn end))
320 (push (cons (event-basic-type start-event) (cdr start-event))
321 unread-command-events))
322 ;; Now restore the old window.
323 (select-window old-selected-window)))
324
325
326 (provide 'mouse-drag)
327
328 ;;; mouse-drag.el ends here