Update copyright notices for 2013.
[bpt/emacs.git] / lisp / mouse-drag.el
CommitLineData
092af6d8
RS
1;;; mouse-drag.el --- use mouse-2 to do a new style of scrolling
2
ab422c4d 3;; Copyright (C) 1996-1997, 2001-2013 Free Software Foundation, Inc.
84f19a49 4
bc28b212
RS
5;; Author: John Heidemann <johnh@ISI.EDU>
6;; Keywords: mouse
84f19a49
RS
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
84f19a49 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
84f19a49
RS
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
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
84f19a49
RS
22
23;;; Commentary:
24
ea89d720
SM
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;;
865fe16f 49;; To use mouse-drag, place the following in your init file:
1bd38124 50;; -either-
ea89d720
SM
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;;
ea89d720
SM
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
84f19a49
RS
102;;; Code:
103
104;;
105;; scrolling code
106;;
107
108(defun mouse-drag-safe-scroll (row-delta &optional col-delta)
8c398124 109 "Scroll down ROW-DELTA lines and right COL-DELTA, ignoring buffer edge errors.
84f19a49 110Keep the cursor on the screen as needed."
f1ade7d3
GM
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))))))))
84f19a49
RS
132
133(defun mouse-drag-repeatedly-safe-scroll (row-delta &optional col-delta)
8c398124 134 "Scroll ROW-DELTA rows and COL-DELTA cols until an event happens."
84f19a49
RS
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)
8c398124 139 "Determine if START-POSN and END-POSN are \"close\"."
84f19a49
RS
140 (let*
141 ((start-col-row (posn-col-row start-posn))
142 (end-col-row (posn-col-row end-posn)))
143 (and
ea89d720
SM
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
84f19a49
RS
149 (= (car start-col-row) (car end-col-row))
150 (= (cdr start-col-row) (cdr end-col-row)))))
151
351d52f7
KH
152(defvar mouse-drag-electric-col-scrolling t
153 "If non-nil, mouse-drag on a long line enables truncate-lines.")
154
84f19a49 155(defun mouse-drag-should-do-col-scrolling ()
8c398124 156 "Determine if it's wise to enable col-scrolling for the current window.
351d52f7 157Basically, we check for existing horizontal scrolling."
84f19a49
RS
158 (or truncate-lines
159 (> (window-hscroll (selected-window)) 0)
70ad3da9 160 (not (window-full-width-p))
351d52f7
KH
161 (and
162 mouse-drag-electric-col-scrolling
163 (save-excursion ;; on a long line?
164 (let
9b026d9f 165 ((beg (line-beginning-position))
351d52f7
KH
166 (end (progn (end-of-line) (point))))
167 (if (> (- end beg) (window-width))
168 (setq truncate-lines t)
169 nil))))))
84f19a49
RS
170
171(defvar mouse-throw-with-scroll-bar nil
fb7ada5f 172 "Set direction of mouse-throwing.
84f19a49
RS
173If nil, the text moves in the direction the mouse moves.
174If t, the scroll bar moves in the direction the mouse moves.")
84f19a49
RS
175(defconst mouse-throw-magnifier-min -6)
176(defconst mouse-throw-magnifier-max 6)
ea89d720
SM
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
1bd38124 188;;;###autoload
84f19a49
RS
189(defun mouse-drag-throw (start-event)
190 "\"Throw\" the page according to a mouse drag.
191
192A \"throw\" is scrolling the page at a speed relative to the distance
193from the original mouse click to the current mouse location. Try it;
194you'll like it. It's easier to observe than to explain.
195
196If the mouse is clicked and released in the same place of time we
6470c3c6 197assume that the user didn't want to scroll but wanted to whatever
84f19a49
RS
198mouse-2 used to do, so we pass it through.
199
200Throw scrolling was inspired (but is not identical to) the \"hand\"
201option in MacPaint, or the middle button in Tk text widgets.
202
203If `mouse-throw-with-scroll-bar' is non-nil, then this command scrolls
204in the opposite direction. (Different people have different ideas
205about which direction is natural. Perhaps it has to do with which
206hemisphere you're in.)
207
208To 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))
06b60517 217 event end row scroll-delta
e35afc97 218 have-scrolled
06b60517 219 col
84f19a49 220 (scroll-col-delta 0)
84f19a49
RS
221 ;; be conservative about allowing horizontal scrolling
222 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
223 (select-window start-window)
224 (track-mouse
225 (while (progn
226 (setq event (read-event)
227 end (event-end event)
228 row (cdr (posn-col-row end))
229 col (car (posn-col-row end)))
230 (or (mouse-movement-p event)
231 (eq (car-safe event) 'switch-frame)))
ea89d720
SM
232 (when (eq start-window (posn-window end))
233 (when col-scrolling-p
234 (setq scroll-col-delta (mouse-drag-scroll-delta (- start-col col))))
235 (setq scroll-delta (mouse-drag-scroll-delta (- start-row row))))
236
84f19a49
RS
237 (if (or (/= 0 scroll-delta)
238 (/= 0 scroll-col-delta))
239 (progn
240 (setq have-scrolled t)
241 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)
242 (mouse-drag-repeatedly-safe-scroll scroll-delta scroll-col-delta))))) ;xxx
243 ;; If it was a click and not a drag, prepare to pass the event on.
e35afc97 244 ;; Is there a more correct way to reconstruct the event?
84f19a49
RS
245 (if (and (not have-scrolled)
246 (mouse-drag-events-are-point-events-p start-posn end))
e35afc97
RS
247 (push (cons (event-basic-type start-event) (cdr start-event))
248 unread-command-events))
84f19a49 249 ;; Now restore the old window.
e35afc97 250 (select-window old-selected-window)))
84f19a49 251
1bd38124 252;;;###autoload
84f19a49
RS
253(defun mouse-drag-drag (start-event)
254 "\"Drag\" the page according to a mouse drag.
255
256Drag scrolling moves the page according to the movement of the mouse.
257You \"grab\" the character under the mouse and move it around.
258
259If the mouse is clicked and released in the same place of time we
260assume that the user didn't want to scroll but wanted to whatever
261mouse-2 used to do, so we pass it through.
262
263Drag scrolling is identical to the \"hand\" option in MacPaint, or the
264middle button in Tk text widgets.
265
266To test this function, evaluate:
267 (global-set-key [down-mouse-2] 'mouse-drag-drag)"
268 (interactive "e")
269 ;; we want to do save-selected-window, but that requires 19.29
270 (let* ((start-posn (event-start start-event))
271 (start-window (posn-window start-posn))
272 (start-row (cdr (posn-col-row start-posn)))
273 (start-col (car (posn-col-row start-posn)))
274 (old-selected-window (selected-window))
06b60517 275 event end row scroll-delta
e35afc97 276 have-scrolled
84f19a49 277 window-last-row
06b60517 278 col window-last-col
84f19a49
RS
279 (scroll-col-delta 0)
280 ;; be conservative about allowing horizontal scrolling
281 (col-scrolling-p (mouse-drag-should-do-col-scrolling)))
282 (select-window start-window)
283 (setq window-last-row (- (window-height) 2)
284 window-last-col (- (window-width) 2))
285 (track-mouse
286 (while (progn
287 (setq event (read-event)
288 end (event-end event)
289 row (cdr (posn-col-row end))
290 col (car (posn-col-row end)))
291 (or (mouse-movement-p event)
292 (eq (car-safe event) 'switch-frame)))
293 ;; Scroll if see if we're on the edge.
294 ;; NEEDSWORK: should handle mouse-in-other window.
295 (cond
296 ((not (eq start-window (posn-window end)))
297 t) ; wait for return to original window
298 ((<= row 0) (mouse-drag-repeatedly-safe-scroll -1 0))
299 ((>= row window-last-row) (mouse-drag-repeatedly-safe-scroll 1 0))
300 ((and col-scrolling-p (<= col 1)) (mouse-drag-repeatedly-safe-scroll 0 -1))
301 ((and col-scrolling-p (>= col window-last-col)) (mouse-drag-repeatedly-safe-scroll 0 1))
302 (t
303 (setq scroll-delta (- row start-row)
304 start-row row)
305 (if col-scrolling-p
306 (setq scroll-col-delta (- col start-col)
307 start-col col))
308 (if (or (/= 0 scroll-delta)
309 (/= 0 scroll-col-delta))
310 (progn
311 (setq have-scrolled t)
312 (mouse-drag-safe-scroll scroll-delta scroll-col-delta)))))))
313 ;; If it was a click and not a drag, prepare to pass the event on.
e35afc97 314 ;; Is there a more correct way to reconstruct the event?
84f19a49
RS
315 (if (and (not have-scrolled)
316 (mouse-drag-events-are-point-events-p start-posn end))
e35afc97
RS
317 (push (cons (event-basic-type start-event) (cdr start-event))
318 unread-command-events))
84f19a49 319 ;; Now restore the old window.
e35afc97
RS
320 (select-window old-selected-window)))
321
84f19a49
RS
322
323(provide 'mouse-drag)
324
325;;; mouse-drag.el ends here