if (p <= data + len - sizeof(sel_nl) &&
if (p <= data + len - sel_nl_sz &&
void term_mouse(Mouse_Button b, Mouse_Action a, int x, int y,		int shift, int ctrl){    pos selpoint;    unsigned long *ldata;    if (y < 0)	y = 0;    if (y >= rows)	y = rows - 1;    if (x < 0) {	if (y > 0) {	    x = cols - 1;	    y--;	} else	    x = 0;    }    if (x >= cols)	x = cols - 1;    selpoint.y = y + disptop;    selpoint.x = x;    ldata = lineptr(selpoint.y);    if ((ldata[cols] & LATTR_MODE) != LATTR_NORM)	selpoint.x /= 2;    if (xterm_mouse) {	int encstate = 0, r, c;	char abuf[16];	static int is_down = 0;	switch (b) {	  case MBT_LEFT:	    encstate = 0x20;	       /* left button down */	    break;	  case MBT_MIDDLE:	    encstate = 0x21;	    break;	  case MBT_RIGHT:	    encstate = 0x22;	    break;	  case MBT_WHEEL_UP:	    encstate = 0x60;	    break;	  case MBT_WHEEL_DOWN:	    encstate = 0x61;	    break;	}	switch (a) {	  case MA_DRAG:	    if (xterm_mouse == 1)		return;	    encstate += 0x20;	    break;	  case MA_RELEASE:	    encstate = 0x23;	    is_down = 0;	    break;	  case MA_CLICK:	    if (is_down == b)		return;	    is_down = b;	    break;	}	if (shift)	    encstate += 0x04;	if (ctrl)	    encstate += 0x10;	r = y + 33;	c = x + 33;	sprintf(abuf, "\033[M%c%c%c", encstate, c, r);	ldisc_send(abuf, 6);	return;    }    b = translate_button(b);    if (b == MBT_SELECT && a == MA_CLICK) {	deselect();	selstate = ABOUT_TO;	selanchor = selpoint;	selmode = SM_CHAR;    } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {	deselect();	selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);	selstate = DRAGGING;	selstart = selanchor = selpoint;	selend = selstart;	incpos(selend);	sel_spread();    } else if ((b == MBT_SELECT && a == MA_DRAG) ||	       (b == MBT_EXTEND && a != MA_RELEASE)) {	if (selstate == ABOUT_TO && poseq(selanchor, selpoint))	    return;	if (b == MBT_EXTEND && a != MA_DRAG && selstate == SELECTED) {	    if (posdiff(selpoint, selstart) <		posdiff(selend, selstart) / 2) {		selanchor = selend;		decpos(selanchor);	    } else {		selanchor = selstart;	    }	    selstate = DRAGGING;	}	if (selstate != ABOUT_TO && selstate != DRAGGING)	    selanchor = selpoint;	selstate = DRAGGING;	if (poslt(selpoint, selanchor)) {	    selstart = selpoint;	    selend = selanchor;	    incpos(selend);	} else {	    selstart = selanchor;	    selend = selpoint;	    incpos(selend);	}	sel_spread();    } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {	if (selstate == DRAGGING) {	    /*	     * We've completed a selection. We now transfer the	     * data to the clipboard.	     */	    clipme(selstart, selend, selspace);	    selstate = SELECTED;	} else	    selstate = NO_SELECTION;    } else if (b == MBT_PASTE	       && (a == MA_CLICK || a == MA_2CLK || a == MA_3CLK)) {	char *data;	int len;	get_clip((void **) &data, &len);	if (data) {	    char *p, *q;	    if (paste_buffer)		sfree(paste_buffer);	    paste_pos = paste_hold = paste_len = 0;	    paste_buffer = smalloc(len);	    p = q = data;	    while (p < data + len) {		while (p < data + len &&		       !(p <= data + len - sizeof(sel_nl) &&			 !memcmp(p, sel_nl, sizeof(sel_nl))))		    p++;		{		    int i;		    unsigned char c;		    for (i = 0; i < p - q; i++) {			c = xlat_kbd2tty(q[i]);			paste_buffer[paste_len++] = c;		    }		}		if (p <= data + len - sizeof(sel_nl) &&		    !memcmp(p, sel_nl, sizeof(sel_nl))) {		    paste_buffer[paste_len++] = '\r';		    p += sizeof(sel_nl);		}		q = p;	    }	    /* Assume a small paste will be OK in one go. */	    if (paste_len < 256) {		ldisc_send(paste_buffer, paste_len);		if (paste_buffer)		    sfree(paste_buffer);		paste_buffer = 0;		paste_pos = paste_hold = paste_len = 0;	    }	}	get_clip(NULL, NULL);    }    term_update();}