[Rd] writing Unicode text to the Windows clipboard

Jennifer Bryan jenny@|@bry@n @end|ng |rom gm@||@com
Fri May 24 08:16:15 CEST 2019


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]]



More information about the R-devel mailing list