2

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:

(progn
  (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.

(progn
  (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.

1
  • Show us your custom function.
    – phils
    Oct 10 '20 at 0:47
0

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.

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.