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