⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 refresh.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
    /*     * fx is the number of characters we need to insert/delete: in the     * beginning to bring the two same begins together     */    fx = (nsb - nfd) - (osb - ofd);    /*     * sx is the number of characters we need to insert/delete: in the end to     * bring the two same last parts together     */    sx = (nls - nse) - (ols - ose);    if (!EL_CAN_INSERT) {	if (fx > 0) {	    osb = ols;	    ose = ols;	    nsb = nls;	    nse = nls;	}	if (sx > 0) {	    ols = oe;	    nls = ne;	}	if ((ols - ofd) < (nls - nfd)) {	    ols = oe;	    nls = ne;	}    }    if (!EL_CAN_DELETE) {	if (fx < 0) {	    osb = ols;	    ose = ols;	    nsb = nls;	    nse = nls;	}	if (sx < 0) {	    ols = oe;	    nls = ne;	}	if ((ols - ofd) > (nls - nfd)) {	    ols = oe;	    nls = ne;	}    }    /*     * Pragmatics III: make sure the middle shifted pointers are correct if     * they don't point to anything (we may have moved ols or nls).     */    /* if the change isn't worth it, don't bother */    /* was: if (osb == ose) */    if ((ose - osb) < MIN_END_KEEP) {	osb = ols;	ose = ols;	nsb = nls;	nse = nls;    }    /*     * Now that we are done with pragmatics we recompute fx, sx     */    fx = (nsb - nfd) - (osb - ofd);    sx = (nls - nse) - (ols - ose);    RE_DEBUG(1,(__F, "\n"),);    RE_DEBUG(1,(__F, "ofd %d, osb %d, ose %d, ols %d, oe %d\n",	    ofd - old, osb - old, ose - old, ols - old, oe - old),);    RE_DEBUG(1,(__F, "nfd %d, nsb %d, nse %d, nls %d, ne %d\n",	    nfd - new, nsb - new, nse - new, nls - new, ne - new),);    RE_DEBUG(1,(__F, 		"xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n"),);    RE_DEBUG(1,(__F,		"xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n"),);#ifdef DEBUG_REFRESH    re_printstr(el, "old- oe", old, oe);    re_printstr(el, "new- ne", new, ne);    re_printstr(el, "old-ofd", old, ofd);    re_printstr(el, "new-nfd", new, nfd);    re_printstr(el, "ofd-osb", ofd, osb);    re_printstr(el, "nfd-nsb", nfd, nsb);    re_printstr(el, "osb-ose", osb, ose);    re_printstr(el, "nsb-nse", nsb, nse);    re_printstr(el, "ose-ols", ose, ols);    re_printstr(el, "nse-nls", nse, nls);    re_printstr(el, "ols- oe", ols, oe);    re_printstr(el, "nls- ne", nls, ne);#endif /* DEBUG_REFRESH */    /*     * el_cursor.v to this line i MUST be in this routine so that if we     * don't have to change the line, we don't move to it. el_cursor.h to first     * diff char     */    term_move_to_line(el, i);    /*     * at this point we have something like this:     *      * /old                  /ofd    /osb               /ose    /ols     /oe     * v.....................v       v..................v       v........v     * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as     * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as     * ^.....................^     ^..................^       ^........^      * \new                  \nfd  \nsb               \nse     \nls    \ne     *      * fx is the difference in length between the the chars between nfd and     * nsb, and the chars between ofd and osb, and is thus the number of     * characters to delete if < 0 (new is shorter than old, as above),     * or insert (new is longer than short).     *     * sx is the same for the second differences.     */    /*     * if we have a net insert on the first difference, AND inserting the net     * amount ((nsb-nfd) - (osb-ofd)) won't push the last useful character     * (which is ne if nls != ne, otherwise is nse) off the edge of the screen     * (el->el_term.t_size.h) else we do the deletes first so that we keep everything we need     * to.     */    /*     * if the last same is the same like the end, there is no last same part,     * otherwise we want to keep the last same part set p to the last useful     * old character     */    p = (ols != oe) ? oe : ose;    /*     * if (There is a diffence in the beginning) && (we need to insert     * characters) && (the number of characters to insert is less than the term     * width) We need to do an insert! else if (we need to delete characters)     * We need to delete characters! else No insert or delete     */    if ((nsb != nfd) && fx > 0 && ((p - old) + fx <= el->el_term.t_size.h)) {	RE_DEBUG(1,(__F, "first diff insert at %d...\r\n", nfd - new),);	/*	 * Move to the first char to insert, where the first diff is.	 */	term_move_to_char(el, nfd - new);	/*	 * Check if we have stuff to keep at end	 */	if (nsb != ne) {	    RE_DEBUG(1,(__F, "with stuff to keep at end\r\n"),);	    /*	     * insert fx chars of new starting at nfd	     */	    if (fx > 0) {		RE_DEBUG(!EL_CAN_INSERT,			 (__F, "ERROR: cannot insert in early first diff\n"),);		term_insertwrite(el, nfd, fx);		re_insert(el, old, ofd - old, el->el_term.t_size.h, nfd, fx);	    }	    /*	     * write (nsb-nfd) - fx chars of new starting at (nfd + fx)	     */	    term_overwrite(el, nfd + fx, (nsb - nfd) - fx);	    re__strncopy(ofd + fx, nfd + fx, (nsb - nfd) - fx);	}	else {	    RE_DEBUG(1,(__F, "without anything to save\r\n"),);	    term_overwrite(el, nfd, (nsb - nfd));	    re__strncopy(ofd, nfd, (nsb - nfd));	    /*	     * Done	     */	    return;	}    }    else if (fx < 0) {	RE_DEBUG(1,(__F, "first diff delete at %d...\r\n", ofd - old),);	/*	 * move to the first char to delete where the first diff is	 */	term_move_to_char(el, ofd - old);	/*	 * Check if we have stuff to save	 */	if (osb != oe) {	    RE_DEBUG(1,(__F, "with stuff to save at end\r\n"),);	    /*	     * fx is less than zero *always* here but we check for code	     * symmetry	     */	    if (fx < 0) {		RE_DEBUG(!EL_CAN_DELETE,			 (__F, "ERROR: cannot delete in first diff\n"),);		term_deletechars(el, -fx);		re_delete(el, old, ofd - old, el->el_term.t_size.h, -fx);	    }	    /*	     * write (nsb-nfd) chars of new starting at nfd	     */	    term_overwrite(el, nfd, (nsb - nfd));	    re__strncopy(ofd, nfd, (nsb - nfd));	}	else {	    RE_DEBUG(1,(__F, "but with nothing left to save\r\n"),);	    /*	     * write (nsb-nfd) chars of new starting at nfd	     */	    term_overwrite(el, nfd, (nsb - nfd));	    RE_DEBUG(1,(__F, "cleareol %d\n", (oe - old) - (ne - new)),);	    term_clear_EOL(el, (oe - old) - (ne - new));	    /*	     * Done	     */	    return;	}    }    else	fx = 0;    if (sx < 0) {	RE_DEBUG(1,(__F, "second diff delete at %d...\r\n", (ose - old) + fx),);	/*	 * Check if we have stuff to delete	 */	/*	 * fx is the number of characters inserted (+) or deleted (-)	 */	term_move_to_char(el, (ose - old) + fx);	/*	 * Check if we have stuff to save	 */	if (ols != oe) {	    RE_DEBUG(1,(__F, "with stuff to save at end\r\n"),);	    /*	     * Again a duplicate test.	     */	    if (sx < 0) {		RE_DEBUG(!EL_CAN_DELETE, 			 (__F, "ERROR: cannot delete in second diff\n"),);		term_deletechars(el, -sx);	    }	    /*	     * write (nls-nse) chars of new starting at nse	     */	    term_overwrite(el, nse, (nls - nse));	}	else {	    RE_DEBUG(1,(__F, "but with nothing left to save\r\n"),);	    term_overwrite(el, nse, (nls - nse));	    RE_DEBUG(1,(__F, "cleareol %d\n", (oe - old) - (ne - new)),);	    term_clear_EOL(el, (oe - old) - (ne - new));	}    }    /*     * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...     */    if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {	RE_DEBUG(1,(__F, "late first diff insert at %d...\r\n", nfd - new),);	term_move_to_char(el, nfd - new);	/*	 * Check if we have stuff to keep at the end	 */	if (nsb != ne) {	    RE_DEBUG(1,(__F, "with stuff to keep at end\r\n"),);	    /* 	     * We have to recalculate fx here because we set it	     * to zero above as a flag saying that we hadn't done	     * an early first insert.	     */	    fx = (nsb - nfd) - (osb - ofd);	    if (fx > 0) {		/*		 * insert fx chars of new starting at nfd		 */		RE_DEBUG(!EL_CAN_INSERT,			 (__F, "ERROR: cannot insert in late first diff\n"),);		term_insertwrite(el, nfd, fx);		re_insert(el, old, ofd - old, el->el_term.t_size.h, nfd, fx);	    }	    /*	     * write (nsb-nfd) - fx chars of new starting at (nfd + fx)	     */	    term_overwrite(el, nfd + fx, (nsb - nfd) - fx);	    re__strncopy(ofd + fx, nfd + fx, (nsb - nfd) - fx);	}	else {	    RE_DEBUG(1,(__F, "without anything to save\r\n"),);	    term_overwrite(el, nfd, (nsb - nfd));	    re__strncopy(ofd, nfd, (nsb - nfd));	}    }    /*     * line is now NEW up to nse     */    if (sx >= 0) {	RE_DEBUG(1,(__F, "second diff insert at %d...\r\n", nse - new),);	term_move_to_char(el, nse - new);	if (ols != oe) {	    RE_DEBUG(1,(__F, "with stuff to keep at end\r\n"),);	    if (sx > 0) {		/* insert sx chars of new starting at nse */		RE_DEBUG(!EL_CAN_INSERT,		         (__F, "ERROR: cannot insert in second diff\n"),);		term_insertwrite(el, nse, sx);	    }	    /*	     * write (nls-nse) - sx chars of new starting at (nse + sx)	     */	    term_overwrite(el, nse + sx, (nls - nse) - sx);	}	else {	    RE_DEBUG(1,(__F, "without anything to save\r\n"),);	    term_overwrite(el, nse, (nls - nse));	    /*             * No need to do a clear-to-end here because we were doing	     * a second insert, so we will have over written all of the	     * old string.	     */	}    }    RE_DEBUG(1,(__F, "done.\r\n"),);} /* re_update_line *//* re__copy_and_pad(): *	Copy string and pad with spaces */private voidre__copy_and_pad(dst, src, width)    char *dst, *src;    size_t width;{    int i;    for (i = 0; i < width; i++) {	if (*src == '\0')	    break;	*dst++ = *src++;    }    while (i < width) {	*dst++ = ' ';	i++;    }    *dst = '\0';} /* end re__copy_and_pad *//* re_refresh_cursor(): *	Move to the new cursor position */protected voidre_refresh_cursor(el)    EditLine *el;{    char *cp, c;    int h, v, th;    /* first we must find where the cursor is... */    h  = el->el_prompt.p_pos.h;    v  = el->el_prompt.p_pos.v;    th = el->el_term.t_size.h;		/* optimize for speed 		*/    /* do input buffer to el->el_line.cursor */    for (cp = el->el_line.buffer; cp < el->el_line.cursor; cp++) {		c = *cp;	h++;			/* all chars at least this long */	if (c == '\n') {	/* handle newline in data part too */	    h = 0;	    v++;	}	else {	    if (c == '\t') {	/* if a tab, to next tab stop */		while (h & 07) {		    h++;		}	    }	    else if (iscntrl(c)) {	/* if control char */		h++;		if (h > th) {	/* if overflow, compensate */		    h = 1;		    v++;		}	    }	    else if (!isprint(c)) {		h += 3;		if (h > th) {	/* if overflow, compensate */		    h = h - th;		    v++;		}	    }	}	if (h >= th) {		/* check, extra long tabs picked up here also */	    h = 0;	    v++;	}    }    /* now go there */    term_move_to_line(el, v);    term_move_to_char(el, h);    term__flush();} /* re_refresh_cursor *//* re_fastputc(): *	Add a character fast. */private voidre_fastputc(el, c)    EditLine *el;    int    c;{    term__putc(c);    el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;    if (el->el_cursor.h >= el->el_term.t_size.h) {		/* if we must overflow */	el->el_cursor.h = 0;	el->el_cursor.v++;	el->el_refresh.r_oldcv++;	term__putc('\r');	term__putc('\n');    }} /* end re_fastputc *//* re_fastaddc(): *	we added just one char, handle it fast. *	Assumes that screen cursor == real cursor  */protected voidre_fastaddc(el)    EditLine *el;{    char c;    c = el->el_line.cursor[-1];    if (c == '\t' || el->el_line.cursor != el->el_line.lastchar) {	re_refresh(el);		/* too hard to handle */	return;    }				/* else (only do at end of line, no TAB) */    if (iscntrl(c)) {		/* if control char, do caret */	char mc = (c == '\177') ? '?' : (c | 0100);	re_fastputc(el, '^');	re_fastputc(el, mc);    }    else if (isprint(c)) {	/* normal char */	re_fastputc(el, c);    }    else {	re_fastputc(el, '\\');	re_fastputc(el, ((c >> 6) & 7) + '0');	re_fastputc(el, ((c >> 3) & 7) + '0');	re_fastputc(el, (c & 7) + '0');    }    term__flush();} /* end re_fastaddc *//* re_clear_display(): *	clear the screen buffers so that new new prompt starts fresh.  */protected voidre_clear_display(el)    EditLine *el;{    int i;    el->el_cursor.v = 0;    el->el_cursor.h = 0;    for (i = 0; i < el->el_term.t_size.v; i++)	el->el_display[i][0] = '\0';    el->el_refresh.r_oldcv = 0;} /* end re_clear_display *//* re_clear_lines(): *	Make sure all lines are *really* blank  */protected voidre_clear_lines(el)    EditLine *el;{    if (EL_CAN_CEOL) {	int i;	term_move_to_char(el, 0);	for (i = 0; i <= el->el_refresh.r_oldcv; i++) {	    /* for each line on the screen */	    term_move_to_line(el, i);	    term_clear_EOL(el, el->el_term.t_size.h);	}	term_move_to_line(el, 0);    }    else {	term_move_to_line(el, el->el_refresh.r_oldcv);	/* go to last line */	term__putc('\r');				/* go to BOL */	term__putc('\n');				/* go to new line */    }} /* end re_clear_lines */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -