jayduhon / inferno-os

Automatically exported from code.google.com/p/inferno-os
2 stars 0 forks source link

Acme: "Snarf" and "Cut" get only the first symbol from what was selected #303

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Steps to reproduce the problem:
1. Run Acme.
2. Select some text and "Snarf" (or "Cut") it.
3. "Paste".

What is the expected output?:
Selected string is being pasted at cursor position.

What do you see instead?
Only the first symbol from what was selected is being pasted.
Copying from host and pasting to Acme works as expected.

Which operating system are you using?
Windows 7 x64

Please provide any additional information below.

Original issue reported on code.google.com by Dmitry.A...@gmail.com on 14 Nov 2013 at 1:11

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
diff -r 286560f7d4d0 emu/Nt/win.c
--- a/emu/Nt/win.c  Sun Dec 22 05:22:28 2013 +0400
+++ b/emu/Nt/win.c  Mon Dec 23 00:38:42 2013 +0400
@@ -698,15 +698,24 @@
 static char*
 clipreadunicode(HANDLE h)
 {
-   Rune *p;
-   int n;
+   wchar_t *p;
+   int n, wn;
    char *q;

    p = GlobalLock(h);
-   n = runenlen(p, runestrlen(p)+1);
-   q = malloc(n);
-   if(q != nil)
-       runestoutf(q, p, n);
+   wn = wcslen(p);
+   n = WideCharToMultiByte(CP_UTF8, 0, p, wn, nil, 0, nil, nil) + 1; // calc 
buffer length
+   if(n > 0){
+       q = malloc(n);
+       n = WideCharToMultiByte(CP_UTF8, 0, p, wn+1, q, n, nil, nil);
+       if(n == 0) {
+           // todo: log error
+           *q = 0;
+       }
+   }else{
+       // todo: log error
+   }
+
    GlobalUnlock(h);

    if(q == nil)
@@ -752,9 +761,9 @@
 clipwrite(char *buf)
 {
    HANDLE h;
-   char *p, *e;
-   Rune *rp;
-   int n;
+   char *p;
+   wchar_t *rp;
+   int n, wn;

    n = 0;
    if(buf != nil)
@@ -767,15 +776,27 @@
        return -1;
    }

-   h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));
+   wn = MultiByteToWideChar(CP_UTF8, 0, buf, n, nil, 0); // get buffer size
+   if(wn==0) {
+       CloseClipboard();
+       // todo: log the error here
+       return -1;
+   }
+
+   h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (wn+1)*sizeof(wchar_t));
    if(h == NULL)
        error(Enovmem);
+
    rp = GlobalLock(h);
-   p = buf;
-   e = p+n;
-   while(p<e)
-       p += chartorune(rp++, p);
-   *rp = 0;
+
+   wn = MultiByteToWideChar(CP_UTF8, 0, buf, n, rp, wn);
+   if(wn==0){
+       CloseClipboard();
+       GlobalFree(h);
+       // todo: log the error here
+       return -1;
+   }
+   rp[wn] = 0;
    GlobalUnlock(h);

    SetClipboardData(CF_UNICODETEXT, h);

Original comment by Dmitry.A...@gmail.com on 22 Dec 2013 at 8:46