Subject: bug#23707: 25.0.94; Regression in mouse-set-region

From: martin rudalics <[email-masked]>
Date: Tue, 07 Jun 2016 11:10:04 +0200

> When using the mouse to set a region outside of the current window, the
 > region is now created improperly. There are two basic cases:
 >
 > a) dragging the mouse and ending on or outside the Emacs frame causes no
 > region to be created
 >
 > Steps to reproduce:
 >
 > 1. emacs -Q
 > 2. In the scratch buffer with <mouse-1>, drag the region and end off in
 > the menu-bar area.
 > 3. The region is not created.
 >
 > b) dragging the mouse and ending in another Emacs window causes a region
 > to be created between the starting point and the point corresponding to
 > the ending point *in the other buffer*.
 >
 > 1. emacs -Q
 > 2. C-x 2
 > 3. C-x 0
 > 4. In the bottom scratch window, drag with your mouse and end somewhere
 > in the scratch message int he top window
 > 5. The region is created, but ends prematurely at whatever point you
 > ended at in the top window.
 >
 >
 > Emacs 24.5 had the correct behaviour of creating a region between the
 > starting point and one of the ends of the buffer when dragging outside
 > of the current window.
 >
 > In GNU Emacs 25.0.94.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.18.9)
 >   of 2016-05-17 built on lylat
 > Windowing system distributor 'Fedora Project', version 11.0.11803000
 > Configured using:
 >   'configure --with-gif=no'

Both scenarios are easily reproducible on Windows.  Would the attached
patch fix it for you?

Thanks, martin
diff --git a/lisp/mouse.el b/lisp/mouse.el
index 592338a..64ee796 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -566,7 +566,12 @@ mouse-set-region
   (mouse-minibuffer-check click)
   (select-window (posn-window (event-start click)))
   (let ((beg (posn-point (event-start click)))
-	(end (posn-point (event-end click)))
+        (end
+         (if (eq (posn-window (event-end click)) (selected-window))
+             (posn-point (event-end click))
+           ;; If the mouse ends up in any other window or on the menu
+           ;; bar, use `window-point' of selected window (Bug#23707).
+           (window-point)))
         (click-count (event-click-count click)))
     (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
       (when drag-start