Search code examples
emacselispusability

Elisp: simple wrapper over a «mouse-set-point» changes a behavior


The «mouse-set-point» is a function that is being called in Emacs with "mouse double-click" on a word to set a region around it and activate. I made a simple wrapper over it, and bound instead of the default one, like this:

(defun mouse-set-point-highlight-occurs (EVENT)
  (interactive "e")
  (mouse-set-point EVENT))

(global-set-key (kbd "<double-mouse-1>") 'mouse-set-point-highlight-occurs)

As you see it does nothing except for plain transfer of an argument, so the behavior shouldn't be changed. But now with double click the region appears just for a moment, and then fades. What could be wrong with it?


Solution

  • The short answer: you want to call mouse-set-region instead of mouse-set-point.

    The longer answer: part of what you're seeing is a long standing misfeature of the way the mouse region selection code works. If you look at the code of mouse-drag-track (which is the workhorse of mouse-drag-region bound to down-mouse-1) you'll see that this function is the one that implements the mouse-set-point behavior in case of double-mouse-1 (i.e. it checks if the binding is mouse-set-point and if it is, it runs its own code instead of mouse-set-point).

    In Emacs's trunk (i.e. not what will be released as 24.4 but the next one), this code has been modified to work "more normally". But even in Emacs's trunk, your code won't work right: you'll need to additionally pass a non-nil second argument to mouse-set-point in order to indicate that you don't just want to set point, but to actually set the region.