wblen++; *wbptr++ = *p;
if (ldata->chars[x].cc_next) x += ldata->chars[x].cc_next; else break;
static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel){    wchar_t *workbuf;    wchar_t *wbptr;		       /* where next char goes within workbuf */    int old_top_x;    int wblen = 0;		       /* workbuf len */    int buflen;			       /* amount of memory allocated to workbuf */    buflen = 5120;		       /* Default size */    workbuf = snewn(buflen, wchar_t);    wbptr = workbuf;		       /* start filling here */    old_top_x = top.x;		       /* needed for rect==1 */    while (poslt(top, bottom)) {	int nl = FALSE;	termline *ldata = lineptr(top.y);	pos nlpos;	/*	 * nlpos will point at the maximum position on this line we	 * should copy up to. So we start it at the end of the	 * line...	 */	nlpos.y = top.y;	nlpos.x = term->cols;	/*	 * ... move it backwards if there's unused space at the end	 * of the line (and also set `nl' if this is the case,	 * because in normal selection mode this means we need a	 * newline at the end)...	 */	if (!(ldata->lattr & LATTR_WRAPPED)) {	    while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&		   poslt(top, nlpos))		decpos(nlpos);	    if (poslt(nlpos, bottom))		nl = TRUE;	} else if (ldata->lattr & LATTR_WRAPPED2) {	    /* Ignore the last char on the line in a WRAPPED2 line. */	    decpos(nlpos);	}	/*	 * ... and then clip it to the terminal x coordinate if	 * we're doing rectangular selection. (In this case we	 * still did the above, so that copying e.g. the right-hand	 * column from a table doesn't fill with spaces on the	 * right.)	 */	if (rect) {	    if (nlpos.x > bottom.x)		nlpos.x = bottom.x;	    nl = (top.y < bottom.y);	}	while (poslt(top, bottom) && poslt(top, nlpos)) {#if 0	    char cbuf[16], *p;	    sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));#else	    wchar_t cbuf[16], *p;	    int uc = ldata->chars[top.x].chr;	    int set, c;	    if (uc == UCSWIDE) {		top.x++;		continue;	    }	    switch (uc & CSET_MASK) {	      case CSET_LINEDRW:		if (!term->cfg.rawcnp) {		    uc = term->ucsdata->unitab_xterm[uc & 0xFF];		    break;		}	      case CSET_ASCII:		uc = term->ucsdata->unitab_line[uc & 0xFF];		break;	      case CSET_SCOACS:  		uc = term->ucsdata->unitab_scoacs[uc&0xFF]; 		break;	    }	    switch (uc & CSET_MASK) {	      case CSET_ACP:		uc = term->ucsdata->unitab_font[uc & 0xFF];		break;	      case CSET_OEMCP:		uc = term->ucsdata->unitab_oemcp[uc & 0xFF];		break;	    }	    set = (uc & CSET_MASK);	    c = (uc & ~CSET_MASK);	    cbuf[0] = uc;	    cbuf[1] = 0;	    if (DIRECT_FONT(uc)) {		if (c >= ' ' && c != 0x7F) {		    char buf[4];		    WCHAR wbuf[4];		    int rv;		    if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {			buf[0] = c;			buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);			rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);			top.x++;		    } else {			buf[0] = c;			rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);		    }		    if (rv > 0) {			memcpy(cbuf, wbuf, rv * sizeof(wchar_t));			cbuf[rv] = 0;		    }		}	    }#endif	    for (p = cbuf; *p; p++) {		/* Enough overhead for trailing NL and nul */		if (wblen >= buflen - 16) {		    buflen += 100;		    workbuf = sresize(workbuf, buflen, wchar_t);		    wbptr = workbuf + wblen;		}		wblen++;		*wbptr++ = *p;	    }	    top.x++;	}	if (nl) {	    int i;	    for (i = 0; i < sel_nl_sz; i++) {		wblen++;		*wbptr++ = sel_nl[i];	    }	}	top.y++;	top.x = rect ? old_top_x : 0;	unlineptr(ldata);    }#if SELECTION_NUL_TERMINATED    wblen++;    *wbptr++ = 0;#endif    write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */    if (buflen > 0)		       /* indicates we allocated this buffer */	sfree(workbuf);}