4

I'm using GUI emacs 24.4.9.0, so I can click to move the cursor, and double-click to select a word under the mouse pointer as a region.

How can I configure emacs so that right-click selects a character under the mouse pointer? I know that I can assign a function by writing

(global-set-key (kbd "<mouse-3>") 'foobar) 

in the init file, but when I wrote a function for this purpose a while ago, it didn't put the cursor under the place I right-clicked, it just started marking a character forward from where the mouse pointer is currently at. What kind of function I can use for placing the cursor right under the mouse pointer?

1 Answer 1

4

You can do it:

(defun select-char-at-click (event)
  "Select char at EVENT position.
EVENT should be a mouse-click event."
  (interactive "e")
  (run-hooks 'mouse-leave-buffer-hook) ; Give Isearch etc. a chance to turn off.
  (let ((pos  (cadr (event-start event))))
    (unless (>= pos (point-max))
      (push-mark pos)
      (goto-char (1+ (mark)))))
  (setq mark-active      t
    deactivate-mark  nil))

(global-set-key [mouse-3] 'select-char-at-click)
(global-set-key [down-mouse-3] 'ignore)

But you really do not want to do that. Mouse-3 (right button) is used in Emacs to extend the region from the point you set using mouse-1. It is also used in several contexts to pop up a right-click contextual menu.

2
  • Thank you! I could manage to find the function that's originally assigned to mouse-3, which is mouse-save-then-kill, and bound it to S-mouse-1 so that it behaves like the Mac OS system keyboard shortcut, so it won't be a problem. As for the contextual menu, I've almost never found a chance to use it except on the mode line. Am I missing some really convenient uses of it? Assigning the command you wrote to mouse-3 didn't affect the behavior of mouse-3 on the mode line.
    – stacko
    Jan 17, 2016 at 9:08
  • Wrt useful mouse-3 contextual menus: you might want to take a look at my library mouse3.el. It is described here.
    – Drew
    Jan 17, 2016 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.