Occasionally I found myself wanting to paste stuff from an st instance I
just closed, so I wrote the attached. I'll clean it up later and post to
the wiki, probably use fork() and pipe() directly instead of popen so
that I can bypass the shell, maybe #ifdef instead of using an int.

I'm sure people'll object to using posix_spawn and linking against
librt, so I'll leave it at fork.

Cheers,

Alex Pilon
diff --git a/config.def.h b/config.def.h
index fd09d72..a43d7de 100644
--- a/config.def.h
+++ b/config.def.h
@@ -19,6 +19,8 @@ static int borderpx = 2;
 static char shell[] = "/bin/sh";
 static char *utmp = NULL;
 static char stty_args[] = "stty raw pass8 nl -echo -iexten -cstopb 38400";
+static int persistentclip = 1;
+static char clip[] = "xclip -selection clipboard";
 
 /* identification sequence returned in DA and DECID */
 static char vtiden[] = "\033[?6c";
diff --git a/st.c b/st.c
index 98622ec..805a04c 100644
--- a/st.c
+++ b/st.c
@@ -1164,14 +1164,30 @@ selpaste(const Arg *dummy)
 void
 clipcopy(const Arg *dummy)
 {
-       Atom clipboard;
 
        if (sel.clipboard != NULL)
                free(sel.clipboard);
 
-       if (sel.primary != NULL) {
+       if (sel.primary != NULL)
                sel.clipboard = xstrdup(sel.primary);
-               clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
+       else
+               /* never clear the clipboard just because you <C-S-c> while
+                * nothing was selected */
+               return;
+
+       if (persistentclip) {
+               /* TODO: Something cleaner. */
+               /* FIXME: Causes "Couldn't read from shell"s? */
+               FILE* clippipe = popen(clip, "w");
+               if (clippipe == NULL) {
+                       perror("Couldn't start persistent clipboard");
+                       return;
+               }
+               if (fputs(sel.clipboard, clippipe) < 0)
+                       perror("Couldn't copy to persistent clipboard");
+               pclose(clippipe);
+       } else {
+               Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
                XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
        }
 }

Reply via email to