When I have Emacs (24.4, Mac OS, NS build) in the foreground, I often have many windows open in one frame. I sometimes want to switch windows by clicking in an inactive window. By default, this calls mouse-set-point
which (1) activates the window I clicked in, and (2) repositions the point to wherever I clicked. I want to change this behavior so clicking in an inactive window would only activate it, but not move the cursor. If I'm in an active window, I want it to reposition the cursor to wherever I clicked.
Is there any reasonable way built into Emacs to do this?
Edit 1: I just tried this:
(defadvice mouse-set-point (around cv/mouse-set-point (event) activate)
(let ((event-target-window (caadr event)))
(if (eql event-target-window (frame-selected-window))
ad-do-it
(set-frame-selected-window nil event-target-window))))
That doesn't work because mouse-set-point
seems to fire in the new window to position itself.
Edit 2: Updated attempt:
(defadvice mouse-set-point (around cv/mouse-set-point (event) activate)
(let ((event-name (car event))
(event-target-window (caadr event)))
(if (and (eql 'down-mouse-1 event-name)
(eql event-target-window (frame-selected-window)))
ad-do-it
(set-frame-selected-window nil event-target-window))))
This does not work in a weird way. When I click above the cursor position in the inactive window, it does what I want. When I click below the cursor position the inactive window, it moves the cursor. Two events fire consistently: down-mouse-1
and then mouse-1
. Both call mouse-set-point
.
Edit 3: Replacing mouse-set-point
does work, provided I also unset [down-mouse-1]
(normally bound to mouse-drag-region
):
(defun mouse-set-point-2 (event)
(interactive "e")
(mouse-minibuffer-check event)
(let ((event-target-window (caadr event)))
(if (eql event-target-window (frame-selected-window))
(posn-set-point (event-end event))
(set-frame-selected-window nil event-target-window))))
(global-set-key [mouse-1] 'mouse-set-point-2)
(global-unset-key [down-mouse-1])
Not good enough, I don't want to lose drag-selection with the mouse.
Edit 4: Adding advice to both mouse-set-point
and mouse-drag-region
seems to work.