History

win32: convert clipboard to and from CR+LF

aiju committed 8 years ago

Commit 658bdec1 parent ee260881 file · +39 −4Browse files

Unified · Split

gui-win32/screen.c+39−4
@@ -594,7 +594,7 @@ char* · 593 unmodified lines
594594 clipread(void)
595595 {
596596 HANDLE h;
597− char *p;
597+ char *p, *q, *r;
598598
599599 if(!OpenClipboard(window)){
600600 oserror();
@@ -609,18 +609,50 @@ clipread(void) · 8 unmodified lines
609609 oserror();
610610 return strdup("");
611611 }
612+
613+ for(q = r = p; *q != 0; q++)
614+ if(*q != '\r')
615+ *r++ = *q;
616+ *r = 0;
612617
613618 CloseClipboard();
614619 return p;
615620 }
616621
622+static char *
623+addcr(char *buf, int *lp)
624+{
625+ int nlen;
626+ char *r, *p, *q;
627+
628+ nlen = 0;
629+ for(p = buf; *p != 0; p++){
630+ if(*p == '\n')
631+ nlen++;
632+ nlen++;
633+ }
634+ *lp = nlen;
635+ r = malloc(nlen + 1);
636+ if(r == nil)
637+ panic("malloc: %r");
638+ q = r;
639+ for(p = buf; *p != 0; p++){
640+ if(*p == '\n')
641+ *q++ = '\r';
642+ *q++ = *p;
643+ }
644+ *q = 0;
645+ return r;
646+}
647+
617648 int
618649 clipwrite(char *buf)
619650 {
620651 HANDLE h;
621652 char *p;
622653 Rune16 *rp;
623− int n = strlen(buf);
654+ char *crbuf;
655+ int n;
624656
625657 if(!OpenClipboard(window)) {
626658 oserror();
@@ -633,11 +665,13 @@ clipwrite(char *buf) · 6 unmodified lines
633665 return -1;
634666 }
635667
668+ crbuf = addcr(buf, &n);
669+
636670 h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));
637671 if(h == NULL)
638672 panic("out of memory");
639673 rp = GlobalLock(h);
640− utftorunes16(rp, buf, n+1);
674+ utftorunes16(rp, crbuf, n+1);
641675 GlobalUnlock(h);
642676
643677 SetClipboardData(CF_UNICODETEXT, h);
@@ -646,13 +680,14 @@ clipwrite(char *buf) · 2 unmodified lines
646680 if(h == NULL)
647681 panic("out of memory");
648682 p = GlobalLock(h);
649− memcpy(p, buf, n);
683+ memcpy(p, crbuf, n);
650684 p[n] = 0;
651685 GlobalUnlock(h);
652686
653687 SetClipboardData(CF_TEXT, h);
654688
655689 CloseClipboard();
690+ free(crbuf);
656691 return n;
657692 }
658693

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t