1

Much of the time when I use the mouse to click on an emacs window, the x selection which I hope to paste into the buffer is blown away. This seems to be caused by the slight shift of the hand as I apply the left click causing an inadvertant "drag copy." I do not believe I will be able to break this particular habit.

One solution I have found to prevent this is to set:

(setq mouse-drag-copy-region nil)

however this has the unfortunate side effect of preventing me from using the mouse drag functionality at all in emacs. Is there a way to control mouse drag behavior so that a click is ignored unless it spands two or more characters within the buffer?

I am coding on Centos 5.x + GNOME 2 with GNU emacs 23.3.1 in case it makes a difference.

This appears to be a GTK/X issue; I can produce similar behavior with other apps.

Setjmp
  • 27,279
  • 27
  • 74
  • 92
  • Have you tried not using your .emacs? i.e. can you reproduce the behavior when you start Emacs with the '-q' option? – Trey Jackson Jul 14 '11 at 23:31
  • Thanks for your suggestion. I can observe the behavior even with -q. Also, through experimenttaion I discovered that it is in part the (natural) movement of my hand when I make that initial click on the screen that is blowing away the copied text (the question is updated accordingly). – Setjmp Jul 18 '11 at 16:12

2 Answers2

1

The easiest solution to this is to modify the function mouse-drag-track, which can be found in mouse.el. To find the function definition, M-x find-function mouse-drag-track RET.

Copy that into your .emacs file and make one small change. Find the and statement that looks like:

(and mouse-drag-copy-region
     do-mouse-drag-region-post-process
     (let (deactivate-mark)
       (copy-region-as-kill region-commencement
                            region-termination)))

And modify it to have the check to ensure the region is at least 2 characters. I've made this check to be 10 characters for easier testing:

(and mouse-drag-copy-region
     (>= (abs (- region-commencement region-termination)) 10) ;; THIS IS NEW
     do-mouse-drag-region-post-process
     (let (deactivate-mark)
       (copy-region-as-kill region-commencement
                            region-termination)))

I don't see a clean way to do this via advice or hooks or variable settings. Be sure to have a (require 'mouse) before your definition of mouse-drag-track to ensure you override the built-in definition, as opposed to it overriding yours. You also might want to add a check to your .emacs right above the re-definition to remind you to check for new versions of the library/function you're overwriting:

(unless (eq emacs-major-version 23)
  (error "check for new mouse-drag-track"))
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • Thanks for your suggestion. After instrumenting your solution, I have determined that mouse-drag-track is not the appropriate entry point. I have created a bug: https://lists.gnu.org/archive/html/bug-gnu-emacs/2011-07/msg02003.html. Basically, I nolonger think it is my unsteady hands that are the problem but rather emacs behavior in an X window environment. – Setjmp Aug 01 '11 at 16:46
0

This is a bug in emacs that was introduced into the source code history in 2001. I have posted a patch that can be applied to local installations. Hopefully the emacs maintainers will investigate further.

Here is my patch and discussion:

http://lists.gnu.org/archive/html/emacs-devel/2011-08/msg00818.html

Setjmp
  • 27,279
  • 27
  • 74
  • 92