I want to change the mouse behaviour in Emacs:

When I click between windows (i.e. from one window into another)), I only want to select the new window and not change point. This is the behaviour globally enabled through:

  (global-set-key [mouse-1] 'mouse-select-window)
  (global-unset-key [down-mouse-1])
  (global-unset-key [drag-mouse-1]))

When I stay with my mouse click in the same window I want Emacs' out-of the box behaviour with moving point and selecting on drag, i.e.

  (global-set-key [mouse-1] 'mouse-set-point)
  (global-set-key [down-mouse-1]    'mouse-drag-region)
  (global-set-key [drag-mouse-1]    'mouse-set-region))

Is there a standard way to do this? I have tried to provide my own customised function for [mouse-1] but somehow this is not enough.

  • Show us your custom function.
    – phils
    Oct 10, 2020 at 0:47

2 Answers 2


Here’s what i came up with.

The trick is that, as you already seem to figured it out, [down-mouse-1] will cause probles, as it will also select the window, so when it comes to [mouse-1], the new window is already selected.

I first wrote a handy utility function:

(defun gpolonkai/event-in-current-window-p (event)
  "Check if EVENT happened in the current window."
  (eq (selected-window) (posn-window (event-start event))))

Then, in all mouse button handling function, you can check if the event happened in the currently active window:

(defun gpolonkai/mouse-set-point (click &optional promote-to-region)
  "Set point based on click location.

If CLICK happened in an inactive window, select that window without setting point"
  (interactive "e\np")
  (if (gpolonkai/event-in-current-window-p click)
      (call-interactively 'mouse-set-point)
    (call-interactively 'mouse-select-window)))

Now unbind [down-mouse-1] and bind this function to [mouse-1], and you are all set! Also, don’t forget to (require 'mouse) to be on the safe side.


Here is a solution that uses the advice mechanism instead. It doesn't affect clicks on links and buttons, though, which would require advising push-button, widget-button-click and potentially other functions.

(define-advice mouse-set-point (:around (f event &optional promote-to-region) disable-clickthrough)
  (let ((window (posn-window (event-start event))))
    (if (eq window (selected-window))
        (funcall f event promote-to-region)
      (select-window window))))

(define-advice mouse-set-region (:around (f start-event) disable-clickthrough)
  (let ((window (posn-window (event-start start-event))))
    (if (eq window (selected-window))
        (funcall f start-event)
      (select-window window))))

(define-advice mouse-drag-region (:around (f start-event) disable-clickthrough)
  (let ((window (posn-window (event-start start-event))))
    (when (eq window (selected-window))
      (funcall f start-event))))

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.