Registering click events for emacs lisp

emacs, lisp

Solution

You may use the code letter `e' of the `interactive` declaration to access the event; e.g., the following will make left click insert the event data at the point clicked:

(define-key global-map (kbd "<down-mouse-1>")
  (lambda (event)
    (interactive "e")
    (message "%s" event)
    (let ((posn (elt event 1)))
      (with-selected-window (posn-window posn)
        (goto-char (posn-point posn))
        (insert (format "%s" event))))))

Problem

I want to register a click event and do actions based on the `position`, `window` ..etc. This site, `http://www.gnu.org/software/emacs/manual/html_node/elisp/Click-Events.html`, shows the layout of the event variables, but how do I actually attach a handler to the click events. So, basically I am looking for a function that behaves like `attach-handler` would in the below senario. ``` (attach-handler [mouse-1] `(lambda (e) (foo e)) ```

Original source