mouse-drag.el 12 KB

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