Currently, I have text modes configured with wide margins for readability with

(defun text-margins ()
  (setq left-margin-width 16)
  (setq right-margin-width 16))
(add-hook 'text-mode-hook 'text-margins)

in my init.

And this is great, except that sometimes I want to use the mouse to move point to the beginning of a line (in the GUI, on MacOS).

Clicking on a line inside the text moves point to the location clicked, as expected.

However, clicking on my nice wide margin next to a line doesn't move point. Instead, it beeps at me with an error and leaves point where it started.

Is there any way to catch a mouse event inside a margin, so that I can give it a function to move point rather than just yell at me?

1 Answer 1


Ok y'all, I figured it out myself. The following code works even on the bottom half of split windows and on inactive windows.

  (defun set-point-to-margin-click (event) 
    (interactive "e")
    (let ((position (cddr (mouse-position)))
         (clicked-window (posn-window (event-start event))))
      (select-window clicked-window)
      (let ((topline (car (cdr (window-body-edges)))))
        (move-to-window-line 0)
        (line-move-visual (- position topline)))))
    (global-set-key (kbd "<left-margin> <mouse-1>") 'set-point-to-margin-click)
    (global-set-key (kbd "<right-margin> <mouse-1>") 'set-point-to-margin-click)

Your Answer

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

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