+static Selection sel;
+static char *opt_cmd = NULL;
+static char *opt_title = NULL;
+
+void
+selinit(void) {
+ sel.mode = 0;
+ sel.bx = -1;
+ sel.clip = NULL;
+}
+
+static inline int selected(int x, int y) {
+ if(sel.ey == y && sel.by == y) {
+ int bx = MIN(sel.bx, sel.ex);
+ int ex = MAX(sel.bx, sel.ex);
+ return BETWEEN(x, bx, ex);
+ }
+ return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x))
+ || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y));
+}
+
+static void getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
+ if(b)
+ *b = e->xbutton.button;
+
+ *x = e->xbutton.x/xw.cw;
+ *y = e->xbutton.y/xw.ch;
+ sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex;
+ sel.b.y = MIN(sel.by, sel.ey);
+ sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx;
+ sel.e.y = MAX(sel.by, sel.ey);
+}
+
+static void bpress(XEvent *e) {
+ sel.mode = 1;
+ sel.ex = sel.bx = e->xbutton.x/xw.cw;
+ sel.ey = sel.by = e->xbutton.y/xw.ch;
+}
+
+static char *getseltext() {
+ char *str, *ptr;
+ int ls, x, y, sz;
+ if(sel.bx == -1)
+ return NULL;
+ sz = (term.col+1) * (sel.e.y-sel.b.y+1);
+ ptr = str = malloc(sz);
+ for(y = 0; y < term.row; y++) {
+ for(x = 0; x < term.col; x++)
+ if(term.line[y][x].state & GLYPH_SET && (ls = selected(x, y)))
+ *ptr = term.line[y][x].c, ptr++;
+ if(ls)
+ *ptr = '\n', ptr++;
+ }
+ *ptr = 0;
+ return str;
+}
+
+static void selection_notify(XEvent *e) {
+ unsigned long nitems;
+ unsigned long ofs, rem;
+ int format;
+ unsigned char *data;
+ Atom type;
+
+ ofs = 0;
+ do {
+ if(XGetWindowProperty(xw.dis, xw.win, XA_PRIMARY, ofs, BUFSIZ/4,
+ False, AnyPropertyType, &type, &format,
+ &nitems, &rem, &data)) {
+ fprintf(stderr, "Clipboard allocation failed\n");
+ return;
+ }
+ ttywrite((const char *) data, nitems * format / 8);
+ XFree(data);
+ /* number of 32-bit chunks returned */
+ ofs += nitems * format / 32;
+ } while(rem > 0);
+}
+
+static void selpaste() {
+ XConvertSelection(xw.dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime);
+}
+
+static void selection_request(XEvent *e)
+{
+ XSelectionRequestEvent *xsre;
+ XSelectionEvent xev;
+ Atom xa_targets;
+
+ xsre = (XSelectionRequestEvent *) e;
+ xev.type = SelectionNotify;
+ xev.requestor = xsre->requestor;
+ xev.selection = xsre->selection;
+ xev.target = xsre->target;
+ xev.time = xsre->time;
+ /* reject */
+ xev.property = None;
+
+ xa_targets = XInternAtom(xw.dis, "TARGETS", 0);
+ if(xsre->target == xa_targets) {
+ /* respond with the supported type */
+ Atom string = XA_STRING;
+ XChangeProperty(xsre->display, xsre->requestor, xsre->property,
+ XA_ATOM, 32, PropModeReplace,
+ (unsigned char *) &string, 1);
+ xev.property = xsre->property;
+ } else if(xsre->target == XA_STRING) {
+ XChangeProperty(xsre->display, xsre->requestor, xsre->property,
+ xsre->target, 8, PropModeReplace,
+ (unsigned char *) sel.clip, strlen(sel.clip));
+ xev.property = xsre->property;
+ }
+
+ /* all done, send a notification to the listener */
+ if(!XSendEvent(xsre->display, xsre->requestor, True, 0, (XEvent *) &xev))
+ fprintf(stderr, "Error sending SelectionNotify event\n");
+}
+
+static void selcopy(char *str) {
+ /* register the selection for both the clipboard and the primary */
+ Atom clipboard;
+
+ free(sel.clip);
+ sel.clip = str;
+
+ XSetSelectionOwner(xw.dis, XA_PRIMARY, xw.win, CurrentTime);
+
+ clipboard = XInternAtom(xw.dis, "CLIPBOARD", 0);
+ XSetSelectionOwner(xw.dis, clipboard, xw.win, CurrentTime);
+
+ XFlush(xw.dis);
+}
+
+/* TODO: doubleclick to select word */
+static void brelease(XEvent *e) {
+ int b;
+ sel.mode = 0;
+ getbuttoninfo(e, &b, &sel.ex, &sel.ey);
+ if(sel.bx==sel.ex && sel.by==sel.ey) {
+ sel.bx = -1;
+ if(b==2)
+ selpaste();
+ } else {
+ if(b==1)
+ selcopy(getseltext());
+ }
+ draw(1);
+}
+
+static void bmotion(XEvent *e) {
+ if (sel.mode) {
+ getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
+ draw(1);
+ }
+}