Subject: bug#19988: 25.0.50; Drag events ending in different frame

From: martin rudalics <[email-masked]>
Date: Wed, 04 Mar 2015 16:10:58 +0100

> Is there a reason why drag events cannot work across different frames of
 > the very same Emacs instance?

They could but it's a bit tricky when frames overlap.  And probably some
routines would have to be rewritten because they expect events to happen
on the selected frame.

 > Background: I've written a command that uses drag events to swap buffers
 > of two Emacs windows:

I'd rewrite it (sloppily) as follows:

(defun th/swap-window-buffers-by-dnd (drag-event)
   "Swaps the buffers displayed in the DRAG-EVENT's start and end
window."
   (interactive "e")
   (let ((start-win (cl-caadr drag-event))
	(end-win   (cl-caaddr drag-event)))

     (unless (window-live-p end-win)
       (let* ((selected-frame (selected-frame))
	     (frames (delq selected-frame (frame-list)))
	     position frame window)
	(unwind-protect
	    (catch 'found
	      (while frames
		(select-frame (car frames))
		(setq position (mouse-position))
		(setq window (window-at (cadr position) (cddr position)))
		(when (window-live-p window)
		  (setq end-win window)
		  (throw 'found t))
		(setq frames (cdr frames))))
	  (select-frame selected-frame))))

     (when (and (windowp start-win)
	       (windowp end-win)
	       (not (eq start-win end-win)))
       (let ((bs (window-buffer start-win))
	    (be (window-buffer end-win)))
	(set-window-buffer start-win be)
	(set-window-buffer end-win bs)))))

(global-set-key (kbd "<C-S-drag-mouse-1>")
		#'th/swap-window-buffers-by-dnd)

martin