11

I want to prevent the "first" click on a frame in an inactive X window from having any effect, other than to make the frame active. Specifically, I don't want it to reposition point.

The problem is that I can't seem to detect that he frame was inactive when I get the mouse click. So the following code always shows "y":

;; detect if frame is active when clicking
(defun my/mouse-drag-region (event)
  (interactive "e")
  (mouse-minibuffer-check event)
  (let* ((window (caadr event))
         (frame (window-frame window)))
    (if (eql frame (selected-frame))
        (print "y")
      (print "n"))))
(global-set-key [down-mouse-1] 'my/mouse-drag-region)

How do I detect that the frame was previously inactive?

Or, has someone already achieved what I am trying to do elsewhere?

Note: I am asking for something different than this other question, which is about inactive windows, not inactive frames.

edam
  • 311
  • 1
  • 6
  • You want to modify the keyboard shortcut for `mouse-set-point` and create your own function to do whatever you want. Do the `C-h k` mouse click thing and see the **two (2) functions** that are listed there. – lawlist Jun 01 '15 at 14:17
  • Don't your frames have a title bar and border? Why don't you just click those, instead of clicking in a buffer? – Drew Jun 01 '15 at 16:08
  • @lawlist: I don't think this helps because the up event comes even later. I can't even tell that the frame was inactive during the down event! – edam Jun 01 '15 at 18:00
  • 2
    @Drew: well, I could, but it's fiddley. I'd prefer to just be able to click in the X window, like I can when emacs runs in a terminal. – edam Jun 01 '15 at 18:01
  • This is a very annoying behaviour indeed. I prevent it by not using the mouse at all, ie. using Alt+Tab and co. to switch between frames. – GergelyPolonkai Jun 03 '15 at 00:28
  • I highly recommend the `ace-window` package for switching frames and windows without the use of a mouse. – Matthew Piziak Jun 05 '15 at 14:44
  • You might be able to start a timer (running a fraction of a second) in a function added to `focus-in-hook`. You could then check this timer in your custom mouse drag function. – Lindydancer Jul 07 '15 at 06:00
  • The other (and arguably more consistent) OS-solution is to set focus-follows-mouse. – PythonNut Jul 28 '15 at 19:54
  • not answer to your question, but i activate windows (emacs frame) by mouse hovering. So perhaps this was your original intention. Also, i set f7 to switch to emacs. Both can be set in OS, except hover to activate is near impossible in Mac. PS if you go with hover, be sure the delay is at least 0.5 sec. – Xah Lee Aug 12 '15 at 20:26

2 Answers2

1

I don't think this is possible from within Emacs. The problem is that as soon as you click in the frame, the window manager selects that frame. Emacs thus sees it immediately as the selected frame.

Your own test code shows this. No advising of either mouse-drag-region or mouse-set-point (or any other Emacs function) will make any difference.

(Perhaps there is something you can do at the X11 level - dunno.)

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Couldn't you track the selected frame via the `post-command-hook`, record all the point positions and revert them when the selected frame changes? It wouldn't be efficient, but it would work. – PythonNut Jul 23 '15 at 17:48
  • @PythonNut: Ugh. Maybe. Try it. `post-command-hook` is the last resort of a scoundrel. ;-) We all use it for lots of stuff, but it is a heavy hammer. – Drew Jul 23 '15 at 21:25
1

Not exactly what you're asking for, but this works well for me:

(global-unset-key [down-mouse-1])   ; no dragging nonsense
(global-set-key [mouse-1] 'mouse-select-window)  ; no setting point

This doesn't match your request exactly in that not only is the frame made active but so is the window clicked. But, I find that to be useful. Double click moves the point, single click doesn't.

JeanPierre
  • 7,323
  • 1
  • 18
  • 37
DaveL
  • 11
  • 1