2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1995, by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * doupdate.c
2N/A *
2N/A * XCurses Library
2N/A *
2N/A * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
2N/A *
2N/A */
2N/A
2N/A#ifdef M_RCSID
2N/A#ifndef lint
2N/Astatic char const rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/doupdate.c 1.9 1995/07/26 17:45:06 ant Exp $";
2N/A#endif
2N/A#endif
2N/A
2N/A#include <private.h>
2N/A#include <string.h>
2N/A#include <setjmp.h>
2N/A#include <signal.h>
2N/A
2N/A#undef SIGTSTP
2N/A
2N/A/*
2N/A * Disable typeahead trapping because it slow down updated dramatically
2N/A * on MPE/iX.
2N/A */
2N/A#ifdef MPE_STUB
2N/A#undef M_CURSES_TYPEAHEAD
2N/A#endif
2N/A
2N/A/*
2N/A * This value is the ideal length for the cursor addressing sequence
2N/A * being four bytes long, ie. "<escape><cursor addressing code><row><col>".
2N/A * eg. VT52 - "\EYrc" or ADM3A - "\E=rc"
2N/A */
2N/A#define JUMP_SIZE 4
2N/A
2N/A/*
2N/A * This value is the ideal length for the clear-to-eol sequence
2N/A * being two bytes long, ie "<escape><clear eol code>".
2N/A */
2N/A#define CEOL_SIZE 2
2N/A
2N/A#define GOTO(r,c) (__m_mvcur(curscr->_cury, curscr->_curx,r,c,__m_outc),\
2N/A curscr->_cury = r, curscr->_curx = c)
2N/A
2N/Atypedef struct cost_op {
2N/A short cost;
2N/A short op;
2N/A} lcost;
2N/A
2N/Atypedef void (*t_action)(int, int);
2N/A
2N/Astatic jmp_buf breakout;
2N/A
2N/A#define LC(i,j) (lc[(i) * (LINES + 1) + (j)])
2N/A
2N/Astatic lcost *lc = (lcost *) 0;
2N/Astatic unsigned long *nhash = (unsigned long *) 0;
2N/Astatic t_action *del = (t_action *) 0;
2N/Astatic t_action *ins_rep = (t_action *) 0;
2N/A
2N/Astatic WINDOW *newscr;
2N/A
2N/ASTATIC void erase_bottom(int);
2N/ASTATIC void clear_bottom(int);
2N/ASTATIC void complex(void);
2N/ASTATIC int cost(int, int);
2N/ASTATIC void lines_delete(int, int);
2N/ASTATIC void lines_insert(int, int);
2N/ASTATIC void lines_replace(int, int);
2N/ASTATIC void script(int, int);
2N/ASTATIC int scroll_dn(int);
2N/ASTATIC int scroll_up(int);
2N/ASTATIC void simple(void);
2N/ASTATIC void text_replace(int);
2N/ASTATIC void block_over(int, int, int);
2N/A
2N/A/*f
2N/A * Wrapper that streams Curses output.
2N/A *
2N/A * All escape sequences going to the screen come through here.
2N/A * All ordinary characters go to the screen via the putc in doupdate.c
2N/A */
2N/Aint
2N/A__m_outc(ch)
2N/Aint ch;
2N/A{
2N/A return putc(ch, __m_screen->_of);
2N/A}
2N/A
2N/A/*
2N/A * Allocate or grow doupdate() structures.
2N/A */
2N/Aint
2N/A__m_doupdate_init()
2N/A{
2N/A void *new;
2N/A static short nlines = 0;
2N/A
2N/A if (lines <= 0)
2N/A return -1;
2N/A
2N/A if (lines <= nlines)
2N/A return 0;
2N/A
2N/A new = m_malloc((lines+1) * (lines+1) * sizeof *lc);
2N/A if (new == (void *) 0)
2N/A return -1;
2N/A if (lc != (lcost *) 0)
2N/A free(lc);
2N/A lc = (lcost *) new;
2N/A
2N/A new = m_malloc((lines + lines) * sizeof *del);
2N/A if (new == (void *) 0)
2N/A return -1;
2N/A if (del != (t_action *) 0)
2N/A free(del);
2N/A del = (t_action *) new;
2N/A ins_rep = del + lines;
2N/A
2N/A new = m_malloc(lines * sizeof *nhash);
2N/A if (new == (void *) 0)
2N/A return -1;
2N/A if (nhash != (unsigned long *) 0)
2N/A free(nhash);
2N/A nhash = (unsigned long *) new;
2N/A
2N/A nlines = lines;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/ASTATIC void
2N/Aerase_bottom(y)
2N/Aint y;
2N/A{
2N/A int i;
2N/A
2N/A for (i = y; i < LINES; ++i) {
2N/A (void) __m_cc_erase(curscr, i, 0, i, curscr->_maxx-1);
2N/A __m_cc_hash(curscr, __m_screen->_hash, i);
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * Clear from the start of the current row to bottom of screen.
2N/A */
2N/ASTATIC void
2N/Aclear_bottom(y)
2N/Aint y;
2N/A{
2N/A erase_bottom(y);
2N/A
2N/A /* Restore default color pair before doing area clears. */
2N/A if (back_color_erase)
2N/A (void) vid_puts(WA_NORMAL, 0, (void *) 0, __m_outc);
2N/A
2N/A if (y == 0 && clear_screen != (char *) 0) {
2N/A (void) tputs(clear_screen, 1, __m_outc);
2N/A } else {
2N/A (void) __m_mvcur(-1, -1, y, 0, __m_outc);
2N/A if (clr_eos != (char *) 0) {
2N/A (void) tputs(clr_eos, 1, __m_outc);
2N/A } else if (clr_eol != (char *) 0) {
2N/A for (;;) {
2N/A (void) tputs(clr_eol, 1, __m_outc);
2N/A if (LINES <= y)
2N/A break;
2N/A (void) __m_mvcur(y, 0, y+1, 0, __m_outc);
2N/A ++y;
2N/A }
2N/A }
2N/A }
2N/A
2N/A curscr->_cury = y;
2N/A curscr->_curx = 0;
2N/A}
2N/A
2N/A/*f
2N/A * Replace a line of text.
2N/A *
2N/A * The principal scheme is to overwrite the region of a line between
2N/A * the first and last differing characters. A clear-eol is used to
2N/A * optimise an update region that consist largely of blanks. This can
2N/A * happen fairly often in the case of scrolled lines or full redraws.
2N/A *
2N/A * Miller's line redraw algorithm, used in the 'S' editor [Mil87],
2N/A * should be re-investigated to see if it is simple and fast enough for
2N/A * our needs, and if it can be adapted to handle the ceol_standout_glitch
2N/A * (HP 2392A terminals) and multibyte character sequences.
2N/A *
2N/A * Very early versions of this code applied a Gosling algorithm column
2N/A * wise in addition to the row-wise used in complex(). It was removed
2N/A * in favour of both computation and transmission speed. The assumption
2N/A * being that overwrites of a line region occured far more frequently
2N/A * than the need to insert/delete several isolated characters.
2N/A *
2N/A * References:
2N/A * [Mil87] W. Miller, A Software Tools Sampler, Prentice-Hall, 1987
2N/A */
2N/ASTATIC void
2N/Atext_replace(row)
2N/Aint row;
2N/A{
2N/A short npair;
2N/A attr_t cookie, nattr;
2N/A cchar_t *optr, *nptr;
2N/A int col, last, tail, jump, count;
2N/A
2N/A#ifdef M_CURSES_TYPEAHEAD
2N/A /* Before replacing a line of text, check for type-ahead. */
2N/A if (__m_screen->_flags & S_ISATTY) {
2N/A unsigned char cc;
2N/A
2N/A if (read(__m_screen->_kfd, &cc, sizeof cc) == sizeof cc) {
2N/A (void) ungetch(cc);
2N/A longjmp(breakout, 1);
2N/A }
2N/A }
2N/A#endif /* M_CURSES_TYPEAHEAD */
2N/A
2N/A col = newscr->_first[row];
2N/A if (col < 0)
2N/A col = 0;
2N/A
2N/A last = newscr->_last[row];
2N/A if (COLS < last)
2N/A last = COLS;
2N/A
2N/A if (clr_eol != (char *) 0) {
2N/A /* Find start of blank tail region. */
2N/A nptr = &newscr->_line[row][COLS];
2N/A for (tail = COLS; 0 < tail; --tail) {
2N/A if (!__m_cc_compare(--nptr, &newscr->_bg, 1))
2N/A break;
2N/A }
2N/A
2N/A /* Only consider clear-to-end-of-line optimization if the
2N/A * blank tail falls within the end of the dirty region by
2N/A * more than ideal length of clear-to-end-of-line sequence.
2N/A * Else disable the check by forcing tail to be at the
2N/A * end-of-line.
2N/A */
2N/A if (last < tail + CEOL_SIZE)
2N/A tail = COLS;
2N/A }
2N/A
2N/A optr = &curscr->_line[row][col];
2N/A nptr = &newscr->_line[row][col];
2N/A
2N/A for (jump = -1; col < last; ) {
2N/A /* Skip common regions. */
2N/A for (count = 0; __m_cc_compare(optr, nptr, 1); ++count) {
2N/A /* Advance before possible goto. */
2N/A ++optr;
2N/A ++nptr;
2N/A
2N/A if (last <= ++col)
2N/A goto done;
2N/A }
2N/A
2N/A /* Move the cursor by redrawing characters or using
2N/A * cursor motion commands. The first time that we
2N/A * address this row, jump equals -1, so that the cursor
2N/A * will be forced to the correct screen line. Once
2N/A * there, we should be able to track the cursor motion
2N/A * along the line and jump only when the cost of redrawing
2N/A * to column N is more expensive than a jump to column N.
2N/A */
2N/A if (jump < count) {
2N/A /* First time addressing this row or cost of
2N/A * jumping cheaper than redrawing.
2N/A */
2N/A jump = JUMP_SIZE;
2N/A GOTO(row, col);
2N/A count = 0;
2N/A
2N/A /* If attributes at start of field are different
2N/A * force an attribute cookie to be dropped.
2N/A */
2N/A if (ceol_standout_glitch
2N/A && (optr->_at != nptr->_at || optr->_co != nptr->_co))
2N/A ATTR_STATE |= WA_COOKIE;
2N/A } else {
2N/A /* Redraw to move short distance. */
2N/A optr -= count;
2N/A nptr -= count;
2N/A col -= count;
2N/A }
2N/A
2N/A /* Write difference region. */
2N/A while (col < last
2N/A && (!__m_cc_compare(optr, nptr, 1) || 0 < count--)) {
2N/Awrite_loop:
2N/A /* Check for clear-to-end-of-line optimization. */
2N/A if (clr_eol != (char *) 0 && tail <= col) {
2N/A /* For HP terminals, only clear-to-end-of-line
2N/A * once the attributes have been turned off.
2N/A * Other terminals, we can proceed normally.
2N/A */
2N/A if (!ceol_standout_glitch
2N/A || ATTR_STATE == WA_NORMAL) {
2N/A curscr->_curx = col;
2N/A goto done;
2N/A }
2N/A }
2N/A
2N/A ++col;
2N/A
2N/A /* Make sure we don't scroll the screen by writing
2N/A * to the bottom right corner.
2N/A */
2N/A if (COLS <= col && LINES-1 <= row
2N/A && auto_right_margin && !eat_newline_glitch) {
2N/A /*** TODO
2N/A *** Insert character/auto_right_margin
2N/A *** hacks for writting into the last
2N/A *** column of the last line so as not
2N/A *** to scroll.
2N/A ***/
2N/A curscr->_curx = col;
2N/A goto done;
2N/A }
2N/A
2N/A /* Remember any existing attribute cookie. */
2N/A cookie = optr->_at & WA_COOKIE;
2N/A
2N/A nattr = nptr->_at;
2N/A npair = nptr->_co;
2N/A
2N/A /* Change attribute state. On HP terminals we also
2N/A * have to check for attribute cookies that may need
2N/A * to be changed.
2N/A */
2N/A if (ATTR_STATE != nattr
2N/A || optr->_at != nattr || optr->_co != npair) {
2N/A (void) vid_puts(
2N/A nattr, npair, (void *) 0, __m_outc
2N/A );
2N/A
2N/A /* Remember new or existing cookie. */
2N/A cookie = WA_COOKIE;
2N/A }
2N/A
2N/A /* Don't display internal characters. */
2N/A if (nptr->_f)
2N/A (void) __m_cc_write(nptr);
2N/A
2N/A /* Update copy of screen image. */
2N/A *optr++ = *nptr++;
2N/A optr->_at |= cookie;
2N/A }
2N/A
2N/A curscr->_curx = col;
2N/A
2N/A /* Check the attributes at the end of the field with
2N/A * those of start of the next common region. If they
2N/A * differ, force another iteration of the write-loop
2N/A * that will change the attribute state.
2N/A */
2N/A if (ceol_standout_glitch && col < COLS
2N/A && ATTR_STATE != (optr->_at & ~WA_COOKIE))
2N/A goto write_loop;
2N/A }
2N/Adone:
2N/A /* Before leaving this line, check if we have to turn off
2N/A * attributes and record a cookie.
2N/A */
2N/A if (!move_standout_mode && ATTR_STATE != WA_NORMAL) {
2N/A /* ceol_standout_glitch, which affects HP terminals,
2N/A * drops hidden cookies on the screen where ever the
2N/A * cursor is, so disabling attributes before a cursor
2N/A * motion operation could disturb existing highlights.
2N/A */
2N/A if (ceol_standout_glitch)
2N/A /* Attributes on an HP terminal do not cross lines. */
2N/A ATTR_STATE = A_NORMAL;
2N/A else
2N/A (void) vid_puts(WA_NORMAL, 0, (void *) 0, __m_outc);
2N/A }
2N/A
2N/A /* Re-check for clear to end-of-line optimization. */
2N/A if (clr_eol != (char *) 0 && tail <= col && col < last) {
2N/A /* Is the tail of the current screen image non-blank? */
2N/A for (tail = col; tail < COLS; ++tail, ++optr)
2N/A if (!__m_cc_compare(optr, &newscr->_bg, 1))
2N/A break;
2N/A
2N/A /* If tail didn't reach the right margin of
2N/A * the current screen image, then we will
2N/A * make it look like the new image with a
2N/A * clear to end-of-line.
2N/A */
2N/A if (tail < COLS) {
2N/A /* Restore default color pair before area clear. */
2N/A if (back_color_erase)
2N/A (void) vid_puts(
2N/A WA_NORMAL, 0, (void *) 0, __m_outc
2N/A );
2N/A
2N/A (void) tputs(clr_eol, 1, __m_outc);
2N/A __m_cc_erase(curscr, row, tail, row, COLS-1);
2N/A }
2N/A }
2N/A
2N/A /* Line wrapping checks. */
2N/A if (COLS <= curscr->_curx) {
2N/A --curscr->_curx;
2N/A if (auto_right_margin && row < LINES-1) {
2N/A if (eat_newline_glitch) {
2N/A __m_outc('\r');
2N/A __m_outc('\n');
2N/A }
2N/A ++curscr->_cury;
2N/A curscr->_curx = 0;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * Replace a block of lines.
2N/A * Only ever used for complex().
2N/A */
2N/ASTATIC void
2N/Alines_replace(from, to_1)
2N/Aint from, to_1;
2N/A{
2N/A for (; from < to_1; ++from)
2N/A text_replace(from);
2N/A}
2N/A
2N/A/*f
2N/A * Delete a block of lines.
2N/A * Only ever used for complex().
2N/A */
2N/ASTATIC void
2N/Alines_delete(from, to_1)
2N/Aint from, to_1;
2N/A{
2N/A int count = to_1 - from;
2N/A
2N/A if (LINES <= to_1) {
2N/A clear_bottom(from);
2N/A } else {
2N/A GOTO(from, 0);
2N/A (void) winsdelln(curscr, -count);
2N/A
2N/A if (parm_delete_line != (char *) 0) {
2N/A /* Assume that the sequence to delete more than one
2N/A * line is faster than repeated single delete_lines.
2N/A */
2N/A (void) tputs(
2N/A tparm(
2N/A parm_delete_line, (long) count,
2N/A 0, 0, 0, 0, 0, 0, 0, 0
2N/A ), count, __m_outc
2N/A );
2N/A } else if (delete_line != (char *) 0) {
2N/A while (from++ < to_1)
2N/A (void) tputs(delete_line, 1, __m_outc);
2N/A } else {
2N/A /* Error -- what to do. */
2N/A return;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * Insert a block of lines.
2N/A * Only ever used for complex().
2N/A *
2N/A * We must assume that insert_line and parm_insert_line reset the
2N/A * cursor column to zero. Therefore it is text_replace() responsiblity
2N/A * to move the cursor to the correct column to begin the update.
2N/A */
2N/ASTATIC void
2N/Alines_insert(from, to_1)
2N/Aint from, to_1;
2N/A{
2N/A int row, count = to_1 - from;
2N/A
2N/A /* Position the cursor and insert a block of lines into the screen
2N/A * image now, insert lines into the physical screen, then draw the
2N/A * new screen lines.
2N/A */
2N/A GOTO(from, 0);
2N/A (void) winsdelln(curscr, count);
2N/A
2N/A if (parm_insert_line != (char *) 0) {
2N/A /* Assume that the sequence to insert more than one line is
2N/A * faster than repeated single insert_lines.
2N/A */
2N/A (void) tputs(
2N/A tparm(
2N/A parm_insert_line, (long) count,
2N/A 0, 0, 0, 0, 0, 0, 0, 0
2N/A ), count, __m_outc
2N/A );
2N/A } else if (insert_line != (char *) 0) {
2N/A /* For the single line insert we use to iterate moving
2N/A * the cursor, inserting, and then drawing a line. That
2N/A * would appear to be slow but visually appealing. However,
2N/A * people on slow terminals want speed and those on fast
2N/A * terminal won't see it.
2N/A */
2N/A for (row = from; row < to_1; ++row)
2N/A (void) tputs(insert_line, 1, __m_outc);
2N/A } else {
2N/A /* Error -- what to do. */
2N/A return;
2N/A }
2N/A
2N/A for (row = from; row < to_1; ++row)
2N/A text_replace(row);
2N/A}
2N/A
2N/ASTATIC int
2N/Ascroll_up(n)
2N/Aint n;
2N/A{
2N/A int count = n;
2N/A int start, finish, to, row;
2N/A
2N/A if (scroll_forward != (char *) 0) {
2N/A GOTO(LINES-1, 0);
2N/A while (0 < n--)
2N/A (void) tputs(scroll_forward, 1, __m_outc);
2N/A } else if (parm_delete_line != (char *) 0 && 1 < n) {
2N/A GOTO(0, 0);
2N/A (void) tputs(
2N/A tparm(
2N/A parm_delete_line, (long) n,
2N/A 0, 0, 0, 0, 0, 0, 0, 0
2N/A ), n, __m_outc
2N/A );
2N/A } else if (delete_line != (char *) 0) {
2N/A GOTO(0, 0);
2N/A while (0 < n--)
2N/A (void) tputs(delete_line, 1, __m_outc);
2N/A } else {
2N/A return 0;
2N/A }
2N/A
2N/A /* Scroll recorded image. */
2N/A start = 0;
2N/A finish = count-1;
2N/A to = lines;
2N/A
2N/A (void) __m_cc_erase(curscr, start, 0, finish, curscr->_maxx-1);
2N/A (void) __m_ptr_move(
2N/A (void **) curscr->_line, curscr->_maxy, start, finish, to
2N/A );
2N/A
2N/A simple();
2N/A
2N/A return 1;
2N/A}
2N/A
2N/ASTATIC int
2N/Ascroll_dn(n)
2N/Aint n;
2N/A{
2N/A int count = n;
2N/A int start, finish, to, row;
2N/A
2N/A if (LINES < n)
2N/A return 0;
2N/A
2N/A if (scroll_reverse != (char *) 0) {
2N/A GOTO(0, 0);
2N/A while (0 < n--)
2N/A (void) tputs(scroll_reverse, 1, __m_outc);
2N/A } else if (parm_insert_line != (char *) 0 && 1 < n) {
2N/A GOTO(0, 0);
2N/A (void) tputs(
2N/A tparm(
2N/A parm_insert_line, (long) n,
2N/A 0, 0, 0, 0, 0, 0, 0, 0
2N/A ), n, __m_outc
2N/A );
2N/A } else if (insert_line != (char *) 0) {
2N/A GOTO(0, 0);
2N/A while (0 < n--)
2N/A (void) tputs(insert_line, 1, __m_outc);
2N/A } else {
2N/A return 0;
2N/A }
2N/A
2N/A /* Scroll recorded image. */
2N/A start = lines - count;
2N/A finish = lines - 1;
2N/A to = 0;
2N/A
2N/A (void) __m_cc_erase(curscr, start, 0, finish, curscr->_maxx-1);
2N/A (void) __m_ptr_move(
2N/A (void **) curscr->_line, curscr->_maxy, start, finish, to
2N/A );
2N/A
2N/A simple();
2N/A
2N/A return 1;
2N/A}
2N/A
2N/A#ifdef NEVER
2N/ASTATIC int
2N/Ais_same_line(old, new, count)
2N/Acchar_t *old, *new;
2N/Aint count;
2N/A{
2N/A while (0 < count--)
2N/A if (!__m_cc_compare(old, new, 1))
2N/A return 0;
2N/A
2N/A return 1;
2N/A}
2N/A#endif /* NEVER */
2N/A
2N/A/*f
2N/A * Dynamic programming algorithm for the string edit problem.
2N/A *
2N/A * This is a modified Gosling cost algorithm that takes into account
2N/A * null/move operations.
2N/A *
2N/A * Costs for move, delete, replace, and insert are 0, 1, 2, and 3
2N/A * repectively.
2N/A */
2N/ASTATIC int
2N/Acost(fr, lr)
2N/Aint fr, lr;
2N/A{
2N/A register lcost *lcp;
2N/A register int or, nr, cc;
2N/A register unsigned long *ohash = __m_screen->_hash;
2N/A cchar_t **oline = curscr->_line;
2N/A cchar_t **nline = newscr->_line;
2N/A int linesz = COLS * sizeof **oline;
2N/A
2N/A /* Prepare initial row and column of cost matrix.
2N/A *
2N/A * 0 3 6 9 ...
2N/A * 1
2N/A * 2
2N/A * 3
2N/A * :
2N/A */
2N/A LC(fr,fr).cost = 0;
2N/A for (cc = 1, ++lr, nr = fr+1; nr <= lr; ++nr, ++cc) {
2N/A /* Top row is 3, 6, 9, ... */
2N/A LC(fr,nr).cost = cc * 3;
2N/A LC(fr,nr).op = 'i';
2N/A
2N/A /* Left column is 1, 2, 3, ... */
2N/A LC(nr,fr).cost = cc;
2N/A LC(nr,fr).op = 'd';
2N/A }
2N/A
2N/A for (--lr, or = fr; or <= lr; ++or) {
2N/A for (nr = fr; nr <= lr; ++nr) {
2N/A lcp = &LC(or+1,nr+1);
2N/A
2N/A /* Assume move op. */
2N/A lcp->cost = LC(or,nr).cost;
2N/A lcp->op = 'm';
2N/A
2N/A if (ohash[or] != nhash[nr]
2N/A#ifdef NEVER
2N/A/* Should no longer require this code. Using the POSIX 32-bit CRC to
2N/A * generate a hash value should be sufficient now, since text_replace()
2N/A * will compare the contents of a line and output only the dirty regions.
2N/A */
2N/A || !is_same_line(oline[or], nline[nr], linesz)
2N/A#endif
2N/A ) {
2N/A /* Lines are different, assume replace op. */
2N/A lcp->cost += 2;
2N/A lcp->op = 'r';
2N/A }
2N/A
2N/A /* Compare insert op. */
2N/A if ((cc = LC(or+1,nr).cost + 3) < lcp->cost) {
2N/A lcp->cost = cc;
2N/A lcp->op = 'i';
2N/A }
2N/A
2N/A /* Compare delete op. */
2N/A if ((cc = LC(or,nr+1).cost + 1) < lcp->cost) {
2N/A lcp->cost = cc;
2N/A lcp->op = 'd';
2N/A }
2N/A }
2N/A }
2N/A
2N/A return LC(lr+1,lr+1).cost;
2N/A}
2N/A
2N/A/*f
2N/A * Build edit script.
2N/A *
2N/A * Normally this would be a recursve routine doing the deletes, inserts,
2N/A * and replaces on individual lines. Instead we build the script so that
2N/A * we can later do the operations on a block basis. For terminals with
2N/A * parm_delete or parm_insert strings this will be better in terms of the
2N/A * number of characters sent to delete and insert a block of lines.
2N/A *
2N/A * Also we can optimize the script so that tail inserts become replaces.
2N/A * This saves unnecessary inserts operations when the tail can just be
2N/A * overwritten.
2N/A */
2N/ASTATIC void
2N/Ascript(fr, lr)
2N/Aint fr, lr;
2N/A{
2N/A int i, j;
2N/A cchar_t *cp;
2N/A
2N/A i = j = lr + 1;
2N/A
2N/A memset(del, 0, sizeof *del * LINES);
2N/A memset(ins_rep, 0, sizeof *ins_rep * LINES);
2N/A
2N/A do {
2N/A /* We don't have to bounds check i or j becuase row fr and
2N/A * column fr of lc have been preset in order to guarantee the
2N/A * correct motion.
2N/A */
2N/A switch (LC(i,j).op) {
2N/A case 'i':
2N/A --j;
2N/A ins_rep[j] = lines_insert;
2N/A break;
2N/A case 'd':
2N/A --i;
2N/A del[i] = lines_delete;
2N/A break;
2N/A case 'm':
2N/A --i;
2N/A --j;
2N/A break;
2N/A case 'r':
2N/A --i;
2N/A --j;
2N/A ins_rep[j] = lines_replace;
2N/A break;
2N/A }
2N/A } while (fr < i || fr < j);
2N/A
2N/A /* Optimize Tail Inserts */
2N/A for (i = LINES-1; 0 <= i && ins_rep[i] == lines_insert; --i) {
2N/A /* Make each character in the screen line image invalid. */
2N/A for (cp = curscr->_line[i], j = 0; j < COLS; ++j, ++cp)
2N/A cp->_n = -1;
2N/A ins_rep[i] = lines_replace;
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * Complex update algorithm using insert/delete line operations.
2N/A *
2N/A * References:
2N/A * [MyM86] E.W. Myers & W. Miller, Row Replacement Algorithms for
2N/A * Screen Editors, TR 86-19, Dept. Computer Science, U. of Arizona
2N/A * [MyM87] E.W. Myers & W. Miller, A Simple Row Replacement Method,
2N/A * TR 86-28, Dept. Computer Science, U. of Arizona
2N/A * [Mil87] W. Miller, A Software Tools Sampler, Prentice-Hall, 1987
2N/A * [Gos81] James Gosling, A redisplay algorithm, Proceedings of the
2N/A * ACM Symposium on Text Manipulation, SIGPLAN Notices,
2N/A * 16(6) June 1981, pg 123-129
2N/A *
2N/A * All the above were reviewed and experimented with. Due to the nature of
2N/A * Curses' having to handling overlapping WINDOWs, the only suitable
2N/A * algorithum is [Gos81]. The others are better suited to editor type
2N/A * applications that have one window being the entire terminal screen.
2N/A *
2N/A */
2N/ASTATIC void
2N/Acomplex()
2N/A{
2N/A int fr = -1;
2N/A int i, j, lr;
2N/A t_action func;
2N/A
2N/A /* Find block of lines to change */
2N/A for (i = 0; i < LINES; ++i) {
2N/A if (newscr->_first[i] < newscr->_last[i]) {
2N/A /* Compute new hash. */
2N/A __m_cc_hash(newscr, nhash, i);
2N/A if (fr == -1)
2N/A fr = i;
2N/A lr = i;
2N/A } else {
2N/A /* Line not dirty so hash same as before. */
2N/A nhash[i] = __m_screen->_hash[i];
2N/A }
2N/A }
2N/A
2N/A if (fr != -1) {
2N/A /* Gosling */
2N/A cost(fr, lr);
2N/A script(fr, lr);
2N/A
2N/A /* Do deletes first in reverse order. */
2N/A for (j = lr; fr <= j; --j) {
2N/A if (del[j] != (t_action) 0) {
2N/A for (i = j-1; fr <= i; --i)
2N/A if (del[i] == (t_action) 0)
2N/A break;
2N/A
2N/A lines_delete(i+1, j+1);
2N/A j = i;
2N/A }
2N/A }
2N/A
2N/A /* Do insert/replace in forward order. */
2N/A for (i = fr; i <= lr; ++i) {
2N/A if ((func = ins_rep[i]) != (t_action) 0) {
2N/A /* Find size of block */
2N/A for (j = i; j <= lr && ins_rep[j] == func; ++j)
2N/A ;
2N/A (*func)(i, j);
2N/A i = j-1;
2N/A }
2N/A }
2N/Arecord:
2N/A /* _line[], which contains pointers to screen lines,
2N/A * may be shuffled.
2N/A */
2N/A for (i = fr; i <= lr; ++i) {
2N/A /* Save new hash for next update. */
2N/A __m_screen->_hash[i] = nhash[i];
2N/A
2N/A /* Mark line as untouched. */
2N/A newscr->_first[i] = newscr->_maxx;
2N/A newscr->_last[i] = -1;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*f
2N/A * Simple screen update algorithm
2N/A *
2N/A * We perform a simple incremental update of the terminal screen.
2N/A * Only the segment of a line that was touched is replaced on the
2N/A * line.
2N/A */
2N/ASTATIC void
2N/Asimple()
2N/A{
2N/A int row;
2N/A
2N/A for (row = 0; row < LINES; ++row) {
2N/A if (newscr->_first[row] < newscr->_last[row]) {
2N/A text_replace(row);
2N/A
2N/A /* Mark line as untouched. */
2N/A newscr->_first[row] = newscr->_maxx;
2N/A newscr->_last[row] = -1;
2N/A
2N/A if (__m_screen->_flags & S_INS_DEL_LINE)
2N/A __m_cc_hash(newscr, nhash, row);
2N/A }
2N/A }
2N/A
2N/A newscr->_flags &= ~W_REDRAW_WINDOW;
2N/A}
2N/A
2N/A/*f
2N/A * Send all changes made to _newscr to the physical terminal.
2N/A *
2N/A * If idlok() is set TRUE then doupdate will try and use hardware insert
2N/A * and delete line sequences in an effort to optimize output. idlok()
2N/A * should really only be used in applications that want a proper scrolling
2N/A * effect.
2N/A *
2N/A * Added scroll heuristic to handle special case where a full size window
2N/A * with full size scroll region, will scroll the window and replace dirty
2N/A * lines instead of performing usual cost/script operations.
2N/A */
2N/Aint
2N/Adoupdate()
2N/A{
2N/A#ifdef SIGTSTP
2N/A int (*oldsig)(int) = signal(SIGTSTP, SIG_IGN);
2N/A#endif
2N/A
2N/A#ifdef M_CURSES_TYPEAHEAD
2N/A unsigned char cc;
2N/A int min, time, icanon;
2N/A
2N/A if (__m_screen->_flags & S_ISATTY) {
2N/A /* Set up non-blocking input for typeahead trapping. */
2N/A min = cur_term->_prog.c_cc[VMIN];
2N/A time = cur_term->_prog.c_cc[VTIME];
2N/A icanon = cur_term->_prog.c_lflag & ICANON;
2N/A
2N/A cur_term->_prog.c_cc[VMIN] = 0;
2N/A cur_term->_prog.c_cc[VTIME] = 0;
2N/A cur_term->_prog.c_lflag &= ~ICANON;
2N/A
2N/A (void) tcsetattr(__m_screen->_kfd, TCSANOW, &cur_term->_prog);
2N/A }
2N/A#endif /* M_CURSES_TYPEAHEAD */
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace(
2N/A "doupdate(void) using %s algorithm.",
2N/A (__m_screen->_flags & S_INS_DEL_LINE) ? "complex" : "simple"
2N/A );
2N/A#endif
2N/A
2N/A newscr = __m_screen->_newscr;
2N/A
2N/A if (__m_screen->_flags & S_ENDWIN) {
2N/A /* Return from temporary escape done with endwin(). */
2N/A __m_screen->_flags &= ~S_ENDWIN;
2N/A
2N/A (void) reset_prog_mode();
2N/A if (enter_ca_mode != (char *) 0)
2N/A (void) tputs(enter_ca_mode, 1, __m_outc);
2N/A if (keypad_xmit != (char *) 0)
2N/A (void) tputs(keypad_xmit, 1, __m_outc);
2N/A if (ena_acs != (char *) 0)
2N/A (void) tputs(ena_acs, 1, __m_outc);
2N/A
2N/A /* Force redraw of screen. */
2N/A newscr->_flags |= W_CLEAR_WINDOW;
2N/A }
2N/A
2N/A#ifdef M_CURSES_TYPEAHEAD
2N/A if (setjmp(breakout) == 0) {
2N/A if ((__m_screen->_flags & S_ISATTY)
2N/A && read(__m_screen->_kfd, &cc, sizeof cc) == sizeof cc) {
2N/A (void) ungetch(cc);
2N/A longjmp(breakout, 1);
2N/A }
2N/A#endif /* M_CURSES_TYPEAHEAD */
2N/A
2N/A /* When redrawwing a window, we not only assume that line
2N/A * noise may have lost characters, but line noise may have
2N/A * generated bogus characters on the screen outside the
2N/A * the window in question, in which case redraw the entire
2N/A * screen to be sure.
2N/A */
2N/A if (newscr->_flags & (W_CLEAR_WINDOW | W_REDRAW_WINDOW)) {
2N/A clear_bottom(0);
2N/A newscr->_flags &= ~W_CLEAR_WINDOW;
2N/A (void) wtouchln(newscr, 0, newscr->_maxy, 1);
2N/A }
2N/A
2N/A if (newscr->_flags & W_REDRAW_WINDOW)
2N/A simple();
2N/A#if 0 /* This first expression, of undefined section, is useless
2N/A * since newscr->_scroll is unsigned and never LT zero.
2N/A */
2N/A else if (newscr->_scroll < 0 && scroll_dn(-newscr->_scroll))
2N/A#else
2N/A else if (scroll_dn(-newscr->_scroll))
2N/A#endif
2N/A ;
2N/A else if (0 < newscr->_scroll && scroll_up(newscr->_scroll))
2N/A ;
2N/A else if (__m_screen->_flags & S_INS_DEL_LINE)
2N/A complex();
2N/A else
2N/A simple();
2N/A
2N/A if (!(newscr->_flags & W_LEAVE_CURSOR))
2N/A GOTO(newscr->_cury, newscr->_curx);
2N/A
2N/A if (!(curscr->_flags & W_FLUSH))
2N/A (void) fflush(__m_screen->_of);
2N/A#ifdef M_CURSES_TYPEAHEAD
2N/A }
2N/A
2N/A if (__m_screen->_flags & S_ISATTY) {
2N/A /* Restore previous input mode. */
2N/A cur_term->_prog.c_cc[VMIN] = min;
2N/A cur_term->_prog.c_cc[VTIME] = time;
2N/A cur_term->_prog.c_lflag |= icanon;
2N/A
2N/A (void) tcsetattr(__m_screen->_kfd,TCSANOW,&cur_term->_prog);
2N/A }
2N/A#endif /* M_CURSES_TYPEAHEAD */
2N/A
2N/A newscr->_scroll = curscr->_scroll = 0;
2N/A#ifdef SIGTSTP
2N/A signal(SIGTSTP, oldsig);
2N/A#endif
2N/A
2N/A return __m_return_code("doupdate", OK);
2N/A}
2N/A
2N/A/*
2N/A * If true, the implementation may use hardware insert and delete,
2N/A * character features of the terminal. The window parameter
2N/A * is ignored.
2N/A */
2N/Avoid
2N/Aidcok(WINDOW *w, bool bf)
2N/A{
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("idcok(%p, %d)", w, bf);
2N/A#endif
2N/A
2N/A __m_screen->_flags &= ~S_INS_DEL_CHAR;
2N/A if (bf)
2N/A __m_screen->_flags |= S_INS_DEL_CHAR;
2N/A
2N/A __m_return_void("idcok");
2N/A}
2N/A
2N/A/*
2N/A * If true, the implementation may use hardware insert, delete,
2N/A * and scroll line features of the terminal. The window parameter
2N/A * is ignored.
2N/A */
2N/Aint
2N/Aidlok(WINDOW *w, bool bf)
2N/A{
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("idlok(%p, %d)", w, bf);
2N/A#endif
2N/A
2N/A __m_screen->_flags &= ~S_INS_DEL_LINE;
2N/A if (bf && has_il())
2N/A __m_screen->_flags |= S_INS_DEL_LINE;
2N/A
2N/A return __m_return_code("idlok", OK);
2N/A}
2N/A
2N/A/*
2N/A * Use the POSIX 32-bit CRC function to compute a hash value
2N/A * for the window line.
2N/A */
2N/Avoid
2N/A__m_cc_hash(w, array, y)
2N/AWINDOW *w;
2N/Aunsigned long *array;
2N/Aint y;
2N/A{
2N/A array[y] = 0;
2N/A m_crcposix(
2N/A &array[y], (unsigned char *) w->_line[y],
2N/A (size_t) (w->_maxx * sizeof **w->_line)
2N/A );
2N/A}
2N/A
2N/A