> On 03/10/2015 10:49 PM, Alex Pilon wrote:
> > Are you thinking of something like the attached?
>
On Wed, Mar 11, 2015 at 01:01:11PM -0400, Greg Reagle wrote:
> That looks fine to me, looking at it briefly, but I haven't tested it (yet).

Sorry about the noise. It *seemed* to work before; serves me right. I
completely misunderstood X selection management. Here's what I think is
a fixed patch.

Also, I removed the selection clear event handler registration. I just
didn't like the visual selection being cleared just because I set it in
another program. Remove that hunk if you disagree.
diff --git a/config.def.h b/config.def.h
index cd9292a..667f334 100644
--- a/config.def.h
+++ b/config.def.h
@@ -120,7 +120,8 @@ static Shortcut shortcuts[] = {
 	{ MODKEY|ShiftMask,     XK_Next,        xzoom,          {.i = -1} },
 	{ MODKEY|ShiftMask,     XK_Home,        xzoomreset,     {.i =  0} },
 	{ ShiftMask,            XK_Insert,      selpaste,       {.i =  0} },
-	{ MODKEY|ShiftMask,     XK_Insert,      clippaste,      {.i =  0} },
+	{ ControlMask|ShiftMask,XK_C,           clipcopy,       {.i =  0} },
+	{ ControlMask|ShiftMask,XK_V,           clippaste,      {.i =  0} },
 	{ MODKEY,               XK_Num_Lock,    numlock,        {.i =  0} },
 };
 
diff --git a/st.c b/st.c
index 595a955..a429103 100644
--- a/st.c
+++ b/st.c
@@ -290,7 +290,7 @@ typedef struct {
 		int x, y;
 	} nb, ne, ob, oe;
 
-	char *clip;
+	char *primary, *clipboard;
 	Atom xtarget;
 	bool alt;
 	struct timespec tclick1;
@@ -312,6 +312,7 @@ typedef struct {
 } Shortcut;
 
 /* function definitions used in config.h */
+static void clipcopy(const Arg *);
 static void clippaste(const Arg *);
 static void numlock(const Arg *);
 static void selpaste(const Arg *);
@@ -479,7 +480,6 @@ static void (*handler[LASTEvent])(XEvent *) = {
 	[MotionNotify] = bmotion,
 	[ButtonPress] = bpress,
 	[ButtonRelease] = brelease,
-	[SelectionClear] = selclear,
 	[SelectionNotify] = selnotify,
 	[SelectionRequest] = selrequest,
 };
@@ -493,6 +493,7 @@ static STREscape strescseq;
 static int cmdfd;
 static pid_t pid;
 static Selection sel;
+static Atom XA_CLIPBOARD;
 static int iofd = STDOUT_FILENO;
 static char **opt_cmd = NULL;
 static char *opt_io = NULL;
@@ -640,10 +641,12 @@ selinit(void) {
 	memset(&sel.tclick2, 0, sizeof(sel.tclick2));
 	sel.mode = 0;
 	sel.ob.x = -1;
-	sel.clip = NULL;
+	sel.primary = NULL;
+	sel.clipboard = NULL;
 	sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
 	if(sel.xtarget == None)
 		sel.xtarget = XA_STRING;
+	XA_CLIPBOARD = XInternAtom(xw.dpy, "CLIPBOARD", 0);
 }
 
 static int
@@ -985,10 +988,12 @@ selnotify(XEvent *e) {
 	int format;
 	uchar *data, *last, *repl;
 	Atom type;
+	XSelectionEvent *xsev;
 
 	ofs = 0;
+	xsev = (XSelectionEvent*) e;
 	do {
-		if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
+		if(XGetWindowProperty(xw.dpy, xw.win, xsev->property, ofs, BUFSIZ/4,
 					False, AnyPropertyType, &type, &format,
 					&nitems, &rem, &data)) {
 			fprintf(stderr, "Clipboard allocation failed\n");
@@ -1027,10 +1032,7 @@ selpaste(const Arg *dummy) {
 
 void
 clippaste(const Arg *dummy) {
-	Atom clipboard;
-
-	clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
-	XConvertSelection(xw.dpy, clipboard, sel.xtarget, XA_PRIMARY,
+	XConvertSelection(xw.dpy, XA_CLIPBOARD, sel.xtarget, XA_CLIPBOARD,
 			xw.win, CurrentTime);
 }
 
@@ -1047,6 +1049,7 @@ selrequest(XEvent *e) {
 	XSelectionRequestEvent *xsre;
 	XSelectionEvent xev;
 	Atom xa_targets, string;
+	char* seltext;
 
 	xsre = (XSelectionRequestEvent *) e;
 	xev.type = SelectionNotify;
@@ -1065,11 +1068,23 @@ selrequest(XEvent *e) {
 				XA_ATOM, 32, PropModeReplace,
 				(uchar *) &string, 1);
 		xev.property = xsre->property;
-	} else if(xsre->target == sel.xtarget && sel.clip != NULL) {
-		XChangeProperty(xsre->display, xsre->requestor, xsre->property,
-				xsre->target, 8, PropModeReplace,
-				(uchar *) sel.clip, strlen(sel.clip));
-		xev.property = xsre->property;
+	} else if(xsre->target == sel.xtarget) {
+		if (xsre->selection == XA_PRIMARY) {
+			seltext = sel.primary;
+		} else if (xsre->selection == XA_CLIPBOARD) {
+			seltext = sel.clipboard;
+		} else {
+			fprintf(stderr,
+					"Unhandled clipboard selection 0x%lx\n",
+					xsre->selection);
+			return;
+		}
+		if (seltext != NULL) {
+			XChangeProperty(xsre->display, xsre->requestor, xsre->property,
+					xsre->target, 8, PropModeReplace,
+					(uchar *) seltext, strlen(seltext));
+			xev.property = xsre->property;
+		}
 	}
 
 	/* all done, send a notification to the listener */
@@ -1079,16 +1094,18 @@ selrequest(XEvent *e) {
 
 void
 xsetsel(char *str) {
-	/* register the selection for both the clipboard and the primary */
-	Atom clipboard;
-
-	free(sel.clip);
-	sel.clip = str;
+	free(sel.primary);
+	sel.primary = str;
 
 	XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
+}
 
-	clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
-	XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
+void
+clipcopy(const Arg *arg) {
+	if (sel.clipboard != NULL)
+		free(sel.clipboard);
+	sel.clipboard = strdup(sel.primary);
+	XSetSelectionOwner(xw.dpy, XA_CLIPBOARD, xw.win, CurrentTime);
 }
 
 void
@@ -4051,4 +4068,3 @@ run:
 
 	return 0;
 }
-

Attachment: pgpNvsD9_AGrI.pgp
Description: PGP signature

Reply via email to