I want to write some functionality that fires whenever I shift-click a point in an emacs buffer. Problem is that when shift is pressed, the point is not changing when clicking. To make things more clear :

(defun mouse-test()  (interactive)  (message (thing-at-point 'word)))

(global-set-key [mouse-1] 'mouse-test)
(global-set-key [S-mouse-1] 'mouse-test)

When clicking on a word, the word is displayed, when shift-clicking it's not..


You need to manually move the point in your function like this:

(defun mouse-test (event)
  "Move the point to the clicked position and message the thing at point."
  (interactive "e")
  (let ((es (event-start event)))
    (select-window (posn-window es))
    (goto-char (posn-point es))
    (message (thing-at-point 'word))))

Do C-hfinteractive to understand how the argument event is given.

Also look at the function event-start for documentation about what info you can retrieve from the event parameter.

A function bound to mouse events will not move the point automatically, the function needs to do it.

The reason your function works with [mouse-1] is that you are actually running two functions but don't realize it. When you do a normal mouse click both the functions bound to [down-mouse-1] and [mouse-1] are run in that order. The default function bound to [down-mouse-1] is mouse-drag-region which sets the point.

However doing a shift click ([S-mouse-1]) does not run [down-mouse-1] so it does not set the point.

Run C-hk then click the mouse and you will see the docs that describe the behavior of [down-mouse-1] and [mouse-1]

  • Unrelated note: Is there a recommended elisp coding convention where comments beginning on a new line begin with ;; and mid-line comments being with ;? Update: You removed the comment I was referencing :P – Kaushal Modi Mar 3 '15 at 14:52
  • Yes there is a convention: ;;; for doc and header comments ;; for a comment on its own line, and ; for a comment at the end of a line. However I just always use ;; I don't really understand how the convention is beneficial. – Jordon Biondo Mar 3 '15 at 14:54
  • @JordonBiondo: The convention is linked to the indentation behavior of commands such as comment-dwim and comment-region. Or rather, the reverse: the indentation behavior supports the convention. – Drew Mar 3 '15 at 15:31
  • I have noticed that use of ; vs ;; affects auto indentation. – Kaushal Modi Mar 3 '15 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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