mouse-copy.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ;;; mouse-copy.el --- one-click text copy and move
  2. ;; Copyright (C) 1996, 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-copy.el''?
  18. ;;
  19. ;; It provides one-click text copy and move. Rather than the
  20. ;; standard stroke-out-a-region (down-mouse-1, up-mouse-1) followed
  21. ;; by a yank (down-mouse-2, up-mouse-2 or C-y), you can now stroke
  22. ;; out a region and have it automatically pasted at the current
  23. ;; point. You can also move text just as easily. Although the
  24. ;; difference may not sound like much, it does make mousing text
  25. ;; around a lot easier, IMHO.
  26. ;;
  27. ;; If you like mouse-copy, you should also check out mouse-drag
  28. ;; for ``one-click scrolling''.
  29. ;;
  30. ;; To use mouse-copy, place the following in your .emacs file:
  31. ;; (require 'mouse-copy)
  32. ;; (global-set-key [M-down-mouse-1] 'mouse-drag-secondary-pasting)
  33. ;; (global-set-key [M-S-down-mouse-1] 'mouse-drag-secondary-moving)
  34. ;;
  35. ;; (These definitions override the old binding of M-mouse-1 to
  36. ;; mouse-drag-secondary. I find I don't use that command much so its
  37. ;; loss is not important, and it can be made up with a M-mouse-1
  38. ;; followed by a M-mouse-3. I personally reserve M-mouse bindings
  39. ;; for my window manager and bind everything to C-mouse.)
  40. ;;
  41. ;;
  42. ;; History and related work:
  43. ;;
  44. ;; One-click copying and moving was inspired by lemacs-19.8.
  45. ;; Throw-scrolling was inspired by MacPaint's ``hand'' and by Tk's
  46. ;; mouse-2 scrolling. The package mouse-scroll.el by Tom Wurgler
  47. ;; <twurgler@goodyear.com> is similar to mouse-drag-throw, but
  48. ;; doesn't pass clicks through.
  49. ;;
  50. ;; These functions have been tested in emacs version 19.30,
  51. ;; and this package has run in the past on 19.25-19.29.
  52. ;;
  53. ;; Originally mouse-copy was part of a larger package.
  54. ;; As of 11 July 96 the scrolling functions were split out
  55. ;; in preparation for incorporation into (the future) emacs-19.32.
  56. ;;
  57. ;;
  58. ;; Known Bugs:
  59. ;;
  60. ;; - Highlighting is sub-optimal under 19.29 and XFree86-3.1.1
  61. ;; (see \\[mouse-copy-work-around-drag-bug] for details).
  62. ;; - mouse-drag-secondary-pasting and mouse-drag-secondary-moving
  63. ;; require X11R5 (or better) and so fail under older versions
  64. ;; of Open Windows (like that present in Solaris/x86 2.1).
  65. ;;
  66. ;;
  67. ;; Future plans:
  68. ;;
  69. ;; I read about the chording features of Plan-9's Acme environment at
  70. ;; <http://www.zip.com.au/~cs/app/wily/auug.html>. I'd like
  71. ;; to incorporate some of these ideas into mouse-copy. The only
  72. ;; lose is that this is not the current Emacs Way Of Doing Things, so
  73. ;; there would be a learning curve for existing emacs users.
  74. ;;
  75. ;;
  76. ;; Thanks:
  77. ;;
  78. ;; Thanks to Kai Grossjohann
  79. ;; <grossjoh@dusty.informatik.uni-dortmund.de> for reporting bugs, to
  80. ;; Tom Wurgler <twurgler@goodyear.com> for reporting bugs and
  81. ;; suggesting fixes, and to Joel Graber <jgraber@ti.com> for
  82. ;; prompting me to do drag-scrolling and for an initial
  83. ;; implementation of horizontal drag-scrolling.
  84. ;;
  85. ;; -johnh, 11-Jul-96
  86. ;;; Code:
  87. ;;
  88. ;; move/paste code
  89. ;;
  90. (defvar mouse-copy-last-paste-start nil
  91. "Internal to `mouse-drag-secondary-pasting'.")
  92. (defvar mouse-copy-last-paste-end nil
  93. "Internal to `mouse-drag-secondary-pasting'.")
  94. (defvar mouse-copy-have-drag-bug nil
  95. "Set to enable mouse-copy-work-around-drag-bug.
  96. See `mouse-copy-work-around-drag-bug' for details.")
  97. (defun mouse-copy-work-around-drag-bug (start-event end-event)
  98. "Code to work around a bug in post-19.29 Emacs: it drops mouse-drag events.
  99. The problem occurs under XFree86-3.1.1 (X11R6pl11) but not under X11R5,
  100. and under post-19.29 but not early versions of Emacs.
  101. 19.29 and 19.30 seems to drop mouse drag events
  102. sometimes. (Reproducible under XFree86-3.1.1 (X11R6pl11) and
  103. XFree86-3.1.2 under Linux 1.2.x. Doesn't occur under X11R5 and SunOS
  104. 4.1.1.)
  105. To see if you have the problem:
  106. Disable this routine (with (setq mouse-copy-have-drag-bug nil)).
  107. Click and drag for a while.
  108. If highlighting stops tracking, you have the bug.
  109. If you have the bug (or the real fix :-), please let me know."
  110. ;; To work-around, call mouse-set-secondary with a fake
  111. ;; drag event to set the overlay,
  112. ;; the load the x-selection.
  113. (save-excursion
  114. (let*
  115. ((start-posn (event-start start-event))
  116. (end-posn (event-end end-event))
  117. (end-buffer (window-buffer (posn-window end-posn)))
  118. ;; First, figure out the region (left as point/mark).
  119. (range (progn
  120. (set-buffer end-buffer)
  121. (mouse-start-end (posn-point start-posn)
  122. (posn-point end-posn)
  123. (1- (event-click-count start-event)))))
  124. (beg (car range))
  125. (end (car (cdr range))))
  126. ;; Second, set the overlay.
  127. (if mouse-secondary-overlay
  128. (move-overlay mouse-secondary-overlay beg end)
  129. (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
  130. (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
  131. ;; Third, set the selection.
  132. ;; (setq me-beg beg me-end end me-range range) ; for debugging
  133. (set-buffer end-buffer)
  134. (x-set-selection 'SECONDARY (buffer-substring beg end)))))
  135. (defun mouse-drag-secondary-pasting (start-event)
  136. "Drag out a secondary selection, then paste it at the current point.
  137. To test this function, evaluate:
  138. (global-set-key [M-down-mouse-1] 'mouse-drag-secondary-pasting)
  139. put the point at one place, then click and drag over some other region."
  140. (interactive "e")
  141. ;; Work-around: We see and react to each part of a multi-click event
  142. ;; as it proceeds. For a triple-event, this means the double-event
  143. ;; has already copied something that the triple-event will re-copy
  144. ;; (a Bad Thing). We therefore undo the prior insertion if we're on
  145. ;; a multiple event.
  146. (if (and mouse-copy-last-paste-start
  147. (>= (event-click-count start-event) 2))
  148. (delete-region mouse-copy-last-paste-start
  149. mouse-copy-last-paste-end))
  150. ;; HACK: We assume that mouse-drag-secondary returns nil if
  151. ;; there's no secondary selection. This assumption holds as of
  152. ;; emacs-19.22 but is not documented. It's not clear that there's
  153. ;; any other way to get this information.
  154. (if (mouse-drag-secondary start-event)
  155. (progn
  156. (if mouse-copy-have-drag-bug
  157. (mouse-copy-work-around-drag-bug start-event last-input-event))
  158. ;; Remember what we do so we can undo it, if necessary.
  159. (setq mouse-copy-last-paste-start (point))
  160. (insert (x-get-selection 'SECONDARY))
  161. (setq mouse-copy-last-paste-end (point)))
  162. (setq mouse-copy-last-paste-start nil)))
  163. (defun mouse-kill-preserving-secondary ()
  164. "Kill the text in the secondary selection, but leave the selection set.
  165. This command is like \\[mouse-kill-secondary] (that is, the secondary
  166. selection is deleted and placed in the kill ring), except that it also
  167. leaves the secondary buffer active on exit.
  168. This command was derived from mouse-kill-secondary in emacs-19.28
  169. by johnh@ficus.cs.ucla.edu."
  170. (interactive)
  171. (let* ((keys (this-command-keys))
  172. (click (elt keys (1- (length keys)))))
  173. (or (eq (overlay-buffer mouse-secondary-overlay)
  174. (if (listp click)
  175. (window-buffer (posn-window (event-start click)))
  176. (current-buffer)))
  177. (error "Select or click on the buffer where the secondary selection is")))
  178. (with-current-buffer (overlay-buffer mouse-secondary-overlay)
  179. (kill-region (overlay-start mouse-secondary-overlay)
  180. (overlay-end mouse-secondary-overlay)))
  181. ;; (delete-overlay mouse-secondary-overlay)
  182. ;; (x-set-selection 'SECONDARY nil)
  183. ;; (setq mouse-secondary-overlay nil)
  184. )
  185. (defun mouse-drag-secondary-moving (start-event)
  186. "Sweep out a secondary selection, then move it to the current point."
  187. (interactive "e")
  188. ;; HACK: We assume that mouse-drag-secondary returns nil if
  189. ;; there's no secondary selection. This works as of emacs-19.22.
  190. ;; It's not clear that there's any other way to get this information.
  191. (if (mouse-drag-secondary start-event)
  192. (progn
  193. (mouse-kill-preserving-secondary)
  194. (insert (x-get-selection 'SECONDARY))))
  195. )
  196. (provide 'mouse-copy)
  197. ;;; mouse-copy.el ends here