+static inline bool
+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));
+}
+
+void
+getbuttoninfo(XEvent *e, int *b, int *x, int *y) {
+ if(b)
+ *b = e->xbutton.button;
+
+ *x = X2COL(e->xbutton.x);
+ *y = Y2ROW(e->xbutton.y);
+ 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);
+}
+
+void
+mousereport(XEvent *e) {
+ int x = X2COL(e->xbutton.x);
+ int y = Y2ROW(e->xbutton.y);
+ int button = e->xbutton.button;
+ int state = e->xbutton.state;
+ char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 };
+ static int ob, ox, oy;
+
+ /* from urxvt */
+ if(e->xbutton.type == MotionNotify) {
+ if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
+ return;
+ button = ob + 32;
+ ox = x, oy = y;
+ } else if(e->xbutton.type == ButtonRelease || button == AnyButton) {
+ button = 3;
+ } else {
+ button -= Button1;
+ if(button >= 3)
+ button += 64 - 3;
+ if(e->xbutton.type == ButtonPress) {
+ ob = button;
+ ox = x, oy = y;
+ }
+ }
+
+ buf[3] = 32 + button + (state & ShiftMask ? 4 : 0)
+ + (state & Mod4Mask ? 8 : 0)
+ + (state & ControlMask ? 16 : 0);
+
+ ttywrite(buf, sizeof(buf));
+}
+
+void
+bpress(XEvent *e) {
+ if(IS_SET(MODE_MOUSE))
+ mousereport(e);
+ else if(e->xbutton.button == Button1) {
+ if(sel.bx != -1)
+ for(int i=sel.b.y; i<=sel.e.y; i++)
+ term.dirty[i] = 1;
+ sel.mode = 1;
+ sel.ex = sel.bx = X2COL(e->xbutton.x);
+ sel.ey = sel.by = Y2ROW(e->xbutton.y);
+ }
+}
+
+void
+selcopy(void) {
+ char *str, *ptr;
+ int x, y, sz, sl, ls = 0;
+
+ if(sel.bx == -1)
+ str = NULL;
+ else {
+ sz = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ;
+ 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))) {
+ sl = utf8size(term.line[y][x].c);
+ memcpy(ptr, term.line[y][x].c, sl);
+ ptr += sl;
+ }
+ if(ls && y < sel.e.y)
+ *ptr++ = '\n';
+ }
+ *ptr = 0;
+ }
+ xsetsel(str);
+}
+
+void
+selnotify(XEvent *e) {
+ ulong nitems, ofs, rem;
+ int format;
+ uchar *data;
+ Atom type;
+
+ ofs = 0;
+ do {
+ if(XGetWindowProperty(xw.dpy, 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);
+}
+
+void
+selpaste() {
+ XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime);
+}
+
+void
+selrequest(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.dpy, "TARGETS", 0);
+ if(xsre->target == xa_targets) {
+ /* respond with the supported type */
+ Atom string = sel.xtarget;
+ XChangeProperty(xsre->display, xsre->requestor, xsre->property,
+ 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;
+ }
+
+ /* 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");
+}
+
+void
+xsetsel(char *str) {
+ /* register the selection for both the clipboard and the primary */
+ Atom clipboard;
+
+ free(sel.clip);
+ sel.clip = str;
+
+ XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, CurrentTime);
+
+ clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
+ XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
+
+ XFlush(xw.dpy);
+}
+
+void
+brelease(XEvent *e) {
+ if(IS_SET(MODE_MOUSE)) {
+ mousereport(e);
+ return;
+ }
+ if(e->xbutton.button == Button2)
+ selpaste();
+ else if(e->xbutton.button == Button1) {
+ sel.mode = 0;
+ getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
+ term.dirty[sel.ey] = 1;
+ if(sel.bx == sel.ex && sel.by == sel.ey) {
+ struct timeval now;
+ sel.bx = -1;
+ gettimeofday(&now, NULL);
+
+ if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) {
+ /* triple click on the line */
+ sel.b.x = sel.bx = 0;
+ sel.e.x = sel.ex = term.col;
+ sel.b.y = sel.e.y = sel.ey;
+ selcopy();
+ } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) {
+ /* double click to select word */
+ sel.bx = sel.ex;
+ while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET &&
+ term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--;
+ sel.b.x = sel.bx;
+ while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET &&
+ term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++;
+ sel.e.x = sel.ex;
+ sel.b.y = sel.e.y = sel.ey;
+ selcopy();