-static char *getseltext() {
- char *str, *ptr;
- int ls, x, y, sz;
- if(selbx==-1)
- return NULL;
- sz = ((xw.w/xw.ch) * (se[1]-sb[1]+2));
- ptr = str = malloc (sz);
- for(y = 0; y < term.row; y++) {
- for(x = 0; x < term.col; x++) {
- if(term.line[y][x].c && (ls=selected(x, y))) {
- *ptr = term.line[y][x].c;
- ptr++;
- }
+int
+utf8size(char *s) {
+ unsigned char c = *s;
+
+ if(~c&B7)
+ return 1;
+ else if((c&(B7|B6|B5)) == (B7|B6))
+ return 2;
+ else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
+ return 3;
+ else
+ return 4;
+}
+
+void
+selinit(void) {
+ sel.tclick1.tv_sec = 0;
+ sel.tclick1.tv_usec = 0;
+ sel.mode = 0;
+ sel.bx = -1;
+ sel.clip = NULL;
+ sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
+ if(sel.xtarget == None)
+ sel.xtarget = XA_STRING;
+}
+
+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));
+}
+
+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;