Subject: Re: bug#8996: Set PRIMARY from last selection, not last selected window

From: David De La Harpe Golden <[email-masked]>
Date: Mon, 11 Jul 2011 03:57:13 +0100

On 08/07/11 06:54, David De La Harpe Golden wrote:
> On 05/07/11 04:26, Stefan Monnier wrote:
>
>
>> The window manager is ctwm rather than Metacity, in case it matters.
>>
>
> I can replicate this (not just with ctwm)
>
> The issue I was thinking of is still present too, and is likely
> closely related / really the same thing.

Hmm. bug #6872 was addressed by trunk r101176, which suppresses the 
primary update on handle_switch_frame, but, well, that only covers that 
immediate frame switch case.  Kinda need to address switching between 
individual windows too (or actually instead since handle_switch_frame 
will always wind up calling select_window itself, I think).  However, 
select-window itself is not a command afaiui, and enumerating all 
potential window-switching commands is probably not viable.  I can't say 
I like the attached solution (modelled on the deactivate-mark variable) 
very much, though nor did I have any especially better ideas.

=== modified file 'lisp/simple.el'
--- lisp/simple.el	2011-07-06 19:44:09 +0000
+++ lisp/simple.el	2011-07-11 02:44:14 +0000
@@ -3701,6 +3701,7 @@
     (when (and (if (eq select-active-regions 'only)
 		   (eq (car-safe transient-mark-mode) 'only)
 		 select-active-regions)
+	       (not skip-active-region-selection)
 	       (region-active-p)
 	       (display-selections-p))
       ;; The var `saved-region-selection', if non-nil, is the text in

=== modified file 'src/keyboard.c'
--- src/keyboard.c	2011-07-02 23:40:04 +0000
+++ src/keyboard.c	2011-07-11 02:43:15 +0000
@@ -1337,6 +1337,7 @@
   KVAR (current_kboard, Vprefix_arg) = Qnil;
   KVAR (current_kboard, Vlast_prefix_arg) = Qnil;
   Vdeactivate_mark = Qnil;
+  Vskip_active_region_selection = Qnil;
   waiting_for_input = 0;
   cancel_echoing ();
 
@@ -1388,6 +1389,7 @@
 	display_malloc_warning ();
 
       Vdeactivate_mark = Qnil;
+      Vskip_active_region_selection = Qnil;
 
       /* If minibuffer on and echo area in use,
 	 wait a short time and redraw minibuffer.  */
@@ -1512,6 +1514,7 @@
       /* Process filters and timers may have messed with deactivate-mark.
 	 reset it before we execute the command. */
       Vdeactivate_mark = Qnil;
+      Vskip_active_region_selection = Qnil;
 
       /* Remap command through active keymaps */
       Vthis_original_command = cmd;
@@ -1648,7 +1651,7 @@
 		      ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
 		      : (!NILP (Vselect_active_regions)
 			 && !NILP (Vtransient_mark_mode)))
-		  && !EQ (Vthis_command, Qhandle_switch_frame))
+                  && NILP (Vskip_active_region_selection))
 		{
 		  EMACS_INT beg =
 		    XINT (Fmarker_position (BVAR (current_buffer, mark)));
@@ -12212,6 +12215,16 @@
 `deactivate-mark' call uses this to set the window selection.  */);
   Vsaved_region_selection = Qnil;
 
+  DEFVAR_LISP ("skip-active-region-selection", Vskip_active_region_selection,
+	       doc: /* If an editing command sets this to t, don't update the primary selection.
+The command loop sets this to nil before each command,
+and tests the value when the command returns. Certain atypical commands
+(e.g. window and frame switching) may change the current active region,
+yet it is incorrect to update the system selection from that active
+region at that time, as would normally be done if select-active-regions
+is non-nil */);
+  Vskip_active_region_selection = Qnil;
+
   DEFVAR_LISP ("debug-on-event",
                Vdebug_on_event,
                doc: /* Enter debugger on this event.  When Emacs

=== modified file 'src/window.c'
--- src/window.c	2011-07-02 10:36:48 +0000
+++ src/window.c	2011-07-11 02:23:31 +0000
@@ -339,6 +339,9 @@
   if (EQ (window, selected_window) && !inhibit_point_swap)
     return window;
 
+  /* frame/window switches are not grounds to update the primary selection */
+  Vskip_active_region_selection = Qt;
+
   sf = SELECTED_FRAME ();
   if (XFRAME (WINDOW_FRAME (w)) != sf)
     {