Subject: Re: bug#6774: Cut and paste with C-w/mouse-2 not working?

From: David De La Harpe Golden <[email-masked]>
Date: Mon, 02 Aug 2010 21:30:30 +0100

On 02/08/10 20:41, Chong Yidong wrote:
> David De La Harpe Golden<[email-masked]>  writes:
>
>> Yes, there's a problem here - I guess emacs is keeping primary bound
>> to a span that's become zero-length. Again.
>
> After thinking about this, my opinion is that the Emacs command
> loop---or rather prepare_to_modify_buffer in insdel.c---should make a
> copy of the primary selection before running before-change-functions.
> Then, deactivate-mark should check if the resulting string is empty;
> and, if so, set the primary to that saved copy.
>
> I haven't looked at your branch; is this the approach you implemented?

Not reallym, though my branch certainly ended up altering the command 
loop to save a copy of the primary selection before commands  - doing 
that where you say may be more selective to commands that are going to 
modify, and therefore more efficient.

Does your way work properly in a multi-window context on its own, 
though?  I suspect not -that it will deactivate-mark on "boring" 
restored active regions (see below) and set the selection to the 
"boring" region, confounding user expectations, because the restored 
region could be non-empty.

So I'll try and have a go explaining the key points of the branch, then, 
bearing in mind it was focussed mostly on making multi-window/frame 
stuff work, and the zero length fixes came along for the ride.

On the whole, grabbing it locally and giving it a go may be worthwhile,
might be faster than reading the below!

At this stage, the sheer length of the explanation below suggests the 
approach is just too ugly.  I may be stuck in a rut on it
(the trouble is it _does_ give quite nice results...).

Just doing it eagerly may be better. However, one of the non-efficiency 
reasons for the laziness was because certain gestures in emacs are 
really effectively "long running commands" that you can stop and switch 
to another application in the middle of (track-mouse...), and something 
happening purely before/after commands can leave the region stale, and 
the laziness skirted that.

The thing is, if you use focus-follows-mouse between frames, or mouse 
autoselect-window between emacs windows, you select_window without going 
through "normal channels", so trying to do _anything_ smart in 
deactivate-mark tends to break - e.g. the mark is being deactivated in a 
window different to the last window bound to the selection. The selx 
branch just forces the selection to a string if it's lazy-bound to a 
window and emacs still owns it on deactivate mark.

1. Introduce a save/restore of mark and mark-active state as well as 
point on select_window window switching.  The restore would be 
controversial, but could easily be made optional - the saving is the 
important bit for our purposes, the restore is a "new feature". This bit 
might be independently a nice feature, at least.

2. Allow selections to be lazy-bound to window rather than buffer. (this 
is unfinished - in both window and buffer cases should really walk the 
selection_alist and force the lazy selections before deletion of the 
window/buffer.  When the selection is bound to a window, it is the span 
between the window-mark and window-point.

3. Introduce a x-force-lazy-selection to force a selection to a string 
if it is lazy-bound to a buffer.  This is currently only a placeholder 
inefficient implementation, doing it even if unnecessary (i.e. selection 
is already a string), it could be made more efficient by checking if 
forcing is necessary - but owing to the split of the code between C and 
Lisp, that would probably mean moving it to C.

4. Introduce a notion of the region being "interesting" or "boring". If 
a region is zero-length _or_ matching the saved boring region extent, 
consider it boring.

6. make select_window set the region boring when it restores the region. 
  This preserves temporal ordering of selection with respect to user 
explicit selection actions rather than programmatic restores.

5. If select-active-region is called, test if the region is interesting. 
  If it isn't (boring/zero), don't select, just set a 
"select-active-region-maybe" flag.  If it isn't boring, set it to the 
current window.

6. Before each command in the command loop, if select-active-region 
maybe, check again if the region is interesting.

7. Force the primary selection (ideally this would be more efficient as 
in 3), effectively freezing off a string.

8. After each command, call select-active-region again to check if the 
region is interesting and "thaw".

9. In deactivate mark, force the selection in case it's lazy.  Since we 
can't actually tell if we're in the window that the selection was bound 
to, it's _un_safe to use buffer-substring, have to go through 
x-force-lazy-selection.