Add Mod + Shift + c/v and no selclear.

Thanks to Alex Pilon <alp@alexpilon.ca>!

Now there is a distinction between the primary and clipboard selection. With
Mod + Shift + c/v the clipboard is handled. The old Insert behavious does
reside.
This commit is contained in:
Christoph Lohmann 2015-03-14 07:41:59 +01:00
parent b746816b78
commit 2fcfea1bf1
2 changed files with 56 additions and 15 deletions

View file

@ -119,6 +119,8 @@ static Shortcut shortcuts[] = {
{ MODKEY|ShiftMask, XK_Home, xzoomreset, {.i = 0} }, { MODKEY|ShiftMask, XK_Home, xzoomreset, {.i = 0} },
{ ShiftMask, XK_Insert, selpaste, {.i = 0} }, { ShiftMask, XK_Insert, selpaste, {.i = 0} },
{ MODKEY|ShiftMask, XK_Insert, clippaste, {.i = 0} }, { MODKEY|ShiftMask, XK_Insert, clippaste, {.i = 0} },
{ MODKEY|ShiftMask, XK_C, clipcopy, {.i = 0} },
{ MODKEY|ShiftMask, XK_V, clippaste, {.i = 0} },
{ MODKEY, XK_Num_Lock, numlock, {.i = 0} }, { MODKEY, XK_Num_Lock, numlock, {.i = 0} },
}; };

69
st.c
View file

@ -290,7 +290,7 @@ typedef struct {
int x, y; int x, y;
} nb, ne, ob, oe; } nb, ne, ob, oe;
char *clip; char *primary, *clipboard;
Atom xtarget; Atom xtarget;
bool alt; bool alt;
struct timespec tclick1; struct timespec tclick1;
@ -312,6 +312,7 @@ typedef struct {
} Shortcut; } Shortcut;
/* function definitions used in config.h */ /* function definitions used in config.h */
static void clipcopy(const Arg *);
static void clippaste(const Arg *); static void clippaste(const Arg *);
static void numlock(const Arg *); static void numlock(const Arg *);
static void selpaste(const Arg *); static void selpaste(const Arg *);
@ -479,7 +480,11 @@ static void (*handler[LASTEvent])(XEvent *) = {
[MotionNotify] = bmotion, [MotionNotify] = bmotion,
[ButtonPress] = bpress, [ButtonPress] = bpress,
[ButtonRelease] = brelease, [ButtonRelease] = brelease,
[SelectionClear] = selclear, /*
* Uncomment if you want the selection to disappear when you select something
* different in another window.
*/
/* [SelectionClear] = selclear, */
[SelectionNotify] = selnotify, [SelectionNotify] = selnotify,
[SelectionRequest] = selrequest, [SelectionRequest] = selrequest,
}; };
@ -640,7 +645,8 @@ selinit(void) {
memset(&sel.tclick2, 0, sizeof(sel.tclick2)); memset(&sel.tclick2, 0, sizeof(sel.tclick2));
sel.mode = 0; sel.mode = 0;
sel.ob.x = -1; sel.ob.x = -1;
sel.clip = NULL; sel.primary = NULL;
sel.clipboard = NULL;
sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0); sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
if(sel.xtarget == None) if(sel.xtarget == None)
sel.xtarget = XA_STRING; sel.xtarget = XA_STRING;
@ -985,12 +991,15 @@ selnotify(XEvent *e) {
int format; int format;
uchar *data, *last, *repl; uchar *data, *last, *repl;
Atom type; Atom type;
XSelectionEvent *xsev;
ofs = 0; ofs = 0;
xsev = (XSelectionEvent *)e;
do { do {
if(XGetWindowProperty(xw.dpy, xw.win, XA_PRIMARY, ofs, BUFSIZ/4, if(XGetWindowProperty(xw.dpy, xw.win, xsev->property, ofs,
False, AnyPropertyType, &type, &format, BUFSIZ/4, False, AnyPropertyType,
&nitems, &rem, &data)) { &type, &format, &nitems, &rem,
&data)) {
fprintf(stderr, "Clipboard allocation failed\n"); fprintf(stderr, "Clipboard allocation failed\n");
return; return;
} }
@ -1025,12 +1034,26 @@ selpaste(const Arg *dummy) {
xw.win, CurrentTime); xw.win, CurrentTime);
} }
void
clipcopy(const Arg *dummy) {
Atom clipboard;
if(sel.clipboard != NULL)
free(sel.clipboard);
if(sel.primary != NULL) {
sel.clipboard = xstrdup(sel.primary);
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
}
}
void void
clippaste(const Arg *dummy) { clippaste(const Arg *dummy) {
Atom clipboard; Atom clipboard;
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
XConvertSelection(xw.dpy, clipboard, sel.xtarget, XA_PRIMARY, XConvertSelection(xw.dpy, clipboard, sel.xtarget, clipboard,
xw.win, CurrentTime); xw.win, CurrentTime);
} }
@ -1046,7 +1069,8 @@ void
selrequest(XEvent *e) { selrequest(XEvent *e) {
XSelectionRequestEvent *xsre; XSelectionRequestEvent *xsre;
XSelectionEvent xev; XSelectionEvent xev;
Atom xa_targets, string; Atom xa_targets, string, clipboard;
char *seltext;
xsre = (XSelectionRequestEvent *) e; xsre = (XSelectionRequestEvent *) e;
xev.type = SelectionNotify; xev.type = SelectionNotify;
@ -1065,11 +1089,25 @@ selrequest(XEvent *e) {
XA_ATOM, 32, PropModeReplace, XA_ATOM, 32, PropModeReplace,
(uchar *) &string, 1); (uchar *) &string, 1);
xev.property = xsre->property; xev.property = xsre->property;
} else if(xsre->target == sel.xtarget && sel.clip != NULL) { } else if(xsre->target == sel.xtarget) {
XChangeProperty(xsre->display, xsre->requestor, xsre->property, clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
xsre->target, 8, PropModeReplace, if(xsre->selection == XA_PRIMARY) {
(uchar *) sel.clip, strlen(sel.clip)); seltext = sel.primary;
xev.property = xsre->property; } else if(xsre->selection == 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 */ /* all done, send a notification to the listener */
@ -1079,8 +1117,9 @@ selrequest(XEvent *e) {
void void
xsetsel(char *str) { xsetsel(char *str) {
free(sel.clip); free(sel.primary);
sel.clip = str; sel.primary = str;
XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime); XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
} }