shithub: drawterm

Download patch

ref: 658bdec9e954fdfdfe7a9a72939dd62014d2edc1
parent: ee260887863628ae373d9fabcddeac3b0071237a
author: aiju <devnull@localhost>
date: Mon May 14 10:57:11 EDT 2018

win32: convert clipboard to and from CR+LF

--- a/gui-win32/screen.c	Tue Apr 17 05:45:42 2018
+++ b/gui-win32/screen.c	Mon May 14 10:57:11 2018
@@ -594,7 +594,7 @@
 clipread(void)
 {
 	HANDLE h;
-	char *p;
+	char *p, *q, *r;
 
 	if(!OpenClipboard(window)){
 		oserror();
@@ -609,18 +609,50 @@
 		oserror();
 		return strdup("");
 	}
+
+	for(q = r = p; *q != 0; q++)
+		if(*q != '\r')
+			*r++ = *q;
+	*r = 0;
 	
 	CloseClipboard();
 	return p;
 }
 
+static char *
+addcr(char *buf, int *lp)
+{
+	int nlen;
+	char *r, *p, *q;
+
+	nlen = 0;
+	for(p = buf; *p != 0; p++){
+		if(*p == '\n')
+			nlen++;
+		nlen++;
+	}
+	*lp = nlen;
+	r = malloc(nlen + 1);
+	if(r == nil)
+		panic("malloc: %r");
+	q = r;
+	for(p = buf; *p != 0; p++){
+		if(*p == '\n')
+			*q++ = '\r';
+		*q++ = *p;
+	}
+	*q = 0;
+	return r;
+}
+
 int
 clipwrite(char *buf)
 {
 	HANDLE h;
 	char *p;
 	Rune16 *rp;
-	int n = strlen(buf);
+	char *crbuf;
+	int n;
 
 	if(!OpenClipboard(window)) {
 		oserror();
@@ -633,11 +665,13 @@
 		return -1;
 	}
 
+	crbuf = addcr(buf, &n);
+
 	h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));
 	if(h == NULL)
 		panic("out of memory");
 	rp = GlobalLock(h);
-	utftorunes16(rp, buf, n+1);
+	utftorunes16(rp, crbuf, n+1);
 	GlobalUnlock(h);
 
 	SetClipboardData(CF_UNICODETEXT, h);
@@ -646,13 +680,14 @@
 	if(h == NULL)
 		panic("out of memory");
 	p = GlobalLock(h);
-	memcpy(p, buf, n);
+	memcpy(p, crbuf, n);
 	p[n] = 0;
 	GlobalUnlock(h);
 	
 	SetClipboardData(CF_TEXT, h);
 
 	CloseClipboard();
+	free(crbuf);
 	return n;
 }