From m@ech|er @end|ng |rom @t@t@m@th@ethz@ch  Mon May 27 10:06:58 2019
From: m@ech|er @end|ng |rom @t@t@m@th@ethz@ch (Martin Maechler)
Date: Mon, 27 May 2019 10:06:58 +0200
Subject: [Rd] writing Unicode text to the Windows clipboard
In-Reply-To: <CANe9BHG6LFE2kKRJ=xB8Tpz-krDw0WiSS0n+25po9f-2FxZkHw@mail.gmail.com>
References: <CANe9BHG6LFE2kKRJ=xB8Tpz-krDw0WiSS0n+25po9f-2FxZkHw@mail.gmail.com>
Message-ID: <23787.39586.941043.193318@stat.math.ethz.ch>

>>>>> Jennifer Bryan 
>>>>>     on Thu, 23 May 2019 00:03:05 -0400 writes:

    > Hello, I'm interested in moving text from and to the
    > clipboard that cannot necessarily be represented in the
    > native encoding. So, really, this is about Windows.

    > I can successfully read from the clipboard by specifying
    > the format that corresponds to unicode text.

    >> From R >=2.7.0, it seems you should also be able to write
    >> unicode text
    > to the Windows clipboard.

    > https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/gnuwin32/CHANGES0#L535-L536

    > However, in my hands, this does not seem to be true. I can
    > make it work with this change:

> diff --git a/src/library/utils/src/windows/util.c
> b/src/library/utils/src/windows/util.c
> index 373049495dd..fc3dc39e3a7 100644
> --- a/src/library/utils/src/windows/util.c
> +++ b/src/library/utils/src/windows/util.c
> @@ -318,7 +318,7 @@ SEXP writeClipboard(SEXP text, SEXP sformat)
>   warning(_("unable to open the clipboard"));
>   GlobalFree(hglb);
>       } else {
> - success = SetClipboardData(CF_TEXT, hglb) != 0;
> + success = SetClipboardData(format, hglb) != 0;
>   if(!success) {
>       warning(_("unable to write to the clipboard"));
>       GlobalFree(hglb);
> 
> Example:
> 
> "?" is "GREATER-THAN OVER EQUAL TO", which is unicode <U+2267>, has
> UTF-16LE bytes 67 22, and is not representable in latin1.
> 
> I copy ? to the Windows clipboard and attempt a round trip. I see:
> 
> x <- readClipboard(format = 13, raw = TRUE) # 13 <--> "Unicode text"
> #> [1] 67 22 00 00
> writeClipboard(x, format = 13L)
> readClipboard(format = 13, raw = TRUE)
> #> [1] 67 00 22 00 00 00 00 00
> 
> and, literally, pasting yields: g"
> 
> If I build r-devel with the patch, the same process yields
> 
> x <- readClipboard(format = 13, raw = TRUE)
> #> [1] 67 22 00 00
> writeClipboard(x, format = 13)
> readClipboard(format = 13, raw = TRUE)
> #> [1] 67 22 00 00
> 
> and pasting returns the original input: ?
> 
> Passing the `format` to SetClipboardData() instead of hard-wiring
> "CF_TEXT" brings behaviour in line with the docs.
> 
> -- Jenny
> 
> 	[[alternative HTML version deleted]]

Thank you, Jenny -- and Jeroen for confirmation!

I've now found the time to read up a bit on this, notably ?writeClipboard
and the underlying source code,
and just from that reading I'd agree that the change seems a
clear improvement and does what indeed the documentation had
suggested all along.

I'll commit the change to R-devel .. and plan to port it to 'R
3.6.0 patched' in a few days.

Martin



[Rd] writing Unicode text to the Windows clipboard
Martin Maechler