1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 /*-----------------------------------------------------------------
39 * The routine doupdate() and its dependents. Also _nc_outstr(),
40 * so all physical output is concentrated here (except _nc_outch()
43 *-----------------------------------------------------------------*/
49 #include <curses.priv.h>
51 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
52 #define USE_TRACE_TIMES 1
54 #define USE_TRACE_TIMES 0
57 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
62 #include <sys/times.h>
70 #include <sys/select.h>
76 MODULE_ID("$Id: tty_update.c,v 1.117 1999/10/22 23:28:46 tom Exp $")
79 * This define controls the line-breakout optimization. Every once in a
80 * while during screen refresh, we want to check for input and abort the
81 * update if there's some waiting. CHECK_INTERVAL controls the number of
82 * changed lines to be emitted between input checks.
84 * Note: Input-check-and-abort is no longer done if the screen is being
85 * updated from scratch. This is a feature, not a bug.
87 #define CHECK_INTERVAL 5
90 * Enable checking to see if doupdate and friends are tracking the true
91 * cursor position correctly. NOTE: this is a debugging hack which will
92 * work ONLY on ANSI-compatible terminals!
94 /* #define POSITION_DEBUG */
96 static inline chtype ClrBlank ( WINDOW *win );
97 static int ClrBottom(int total);
98 static void ClearScreen( chtype blank );
99 static void ClrUpdate( void );
100 static void DelChar( int count );
101 static void InsStr( chtype *line, int count );
102 static void TransformLine( int const lineno );
104 #ifdef POSITION_DEBUG
105 /****************************************************************************
107 * Debugging code. Only works on ANSI-standard terminals.
109 ****************************************************************************/
111 static void position_check(int expected_y, int expected_x, char *legend)
112 /* check to see if the real cursor position matches the virtual */
117 if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
120 memset(buf, '\0', sizeof(buf));
121 putp("\033[6n"); /* only works on ANSI-compatibles */
123 (void) read(0, buf, sizeof(buf)-1);
124 _tracef("probe returned %s", _nc_visbuf(buf));
126 /* try to interpret as a position report */
127 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
128 _tracef("position probe failed in %s", legend);
134 if (y - 1 != expected_y || x - 1 != expected_x) {
136 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
137 y-1, x-1, expected_y, expected_x, legend);
139 _tracef("position matches OK in %s", legend);
144 #define position_check(expected_y, expected_x, legend) /* nothing */
145 #endif /* POSITION_DEBUG */
147 /****************************************************************************
149 * Optimized update code
151 ****************************************************************************/
153 static inline void GoTo(int const row, int const col)
155 chtype oldattr = SP->_current_attr;
157 TR(TRACE_MOVE, ("GoTo(%d, %d) from (%d, %d)",
158 row, col, SP->_cursrow, SP->_curscol));
160 position_check(SP->_cursrow, SP->_curscol, "GoTo");
163 * Force restore even if msgr is on when we're in an alternate
164 * character set -- these have a strong tendency to screw up the
165 * CR & LF used for local character motions!
167 if ((oldattr & A_ALTCHARSET)
168 || (oldattr && !move_standout_mode))
170 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before move",
171 oldattr, _traceattr(oldattr)));
175 mvcur(SP->_cursrow, SP->_curscol, row, col);
178 position_check(SP->_cursrow, SP->_curscol, "GoTo2");
181 static inline void PutAttrChar(chtype ch)
183 if (tilde_glitch && (TextOf(ch) == '~'))
184 ch = ('`' | AttrOf(ch));
186 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
188 SP->_cursrow, SP->_curscol));
191 _nc_outch((int)TextOf(ch));
193 putc((int)TextOf(ch), SP->_ofp); /* macro's fastest... */
200 TPUTS_TRACE("char_padding");
205 static bool check_pending(void)
206 /* check for pending input */
208 bool have_pending = FALSE;
211 * Only carry out this check when the flag is zero, otherwise we'll
212 * have the refreshing slow down drastically (or stop) if there's an
213 * unread character available.
215 if(SP->_fifohold != 0)
218 if (SP->_checkfd >= 0) {
220 struct pollfd fds[1];
221 fds[0].fd = SP->_checkfd;
222 fds[0].events = POLLIN;
223 if (poll(fds, 1, 0) > 0)
227 #elif defined(__BEOS__)
229 * BeOS's select() is declared in socket.h, so the configure script does
230 * not see it. That's just as well, since that function works only for
231 * sockets. This (using snooze and ioctl) was distilled from Be's patch
232 * for ncurses which uses a separate thread to simulate select().
234 * FIXME: the return values from the ioctl aren't very clear if we get
238 int howmany = ioctl(0, 'ichr', &n);
239 if (howmany >= 0 && n > 0) {
244 struct timeval ktimeout;
247 ktimeout.tv_usec = 0;
250 FD_SET(SP->_checkfd, &fdset);
251 if (select(SP->_checkfd+1, &fdset, NULL, NULL, &ktimeout) != 0)
265 * No one supports recursive inline functions. However, gcc is quieter if we
266 * instantiate the recursive part separately.
268 #if CC_HAS_INLINE_FUNCS
269 static void callPutChar(chtype const);
271 #define callPutChar(ch) PutChar(ch)
274 static inline void PutChar(chtype const ch); /* forward declaration */
276 /* put char at lower right corner */
277 static void PutCharLR(chtype const ch)
279 if (!auto_right_margin)
281 /* we can put the char directly */
284 else if (enter_am_mode && exit_am_mode)
286 /* we can suppress automargin */
287 TPUTS_TRACE("exit_am_mode");
292 position_check(SP->_cursrow, SP->_curscol, "exit_am_mode");
294 TPUTS_TRACE("enter_am_mode");
297 else if ((enter_insert_mode && exit_insert_mode)
298 || insert_character || parm_ich)
300 GoTo(screen_lines-1,screen_columns-2);
302 GoTo(screen_lines-1,screen_columns-2);
303 InsStr(newscr->_line[screen_lines-1].text+screen_columns-2,1);
307 static void wrap_cursor(void)
309 if (eat_newline_glitch)
312 * xenl can manifest two different ways. The vt100
313 * way is that, when you'd expect the cursor to wrap,
314 * it stays hung at the right margin (on top of the
315 * character just emitted) and doesn't wrap until the
316 * *next* graphic char is emitted. The c100 way is
317 * to ignore LF received just after an am wrap.
319 * An aggressive way to handle this would be to
320 * emit CR/LF after the char and then assume the wrap
321 * is done, you're on the first position of the next
322 * line, and the terminal out of its weird state.
323 * Here it's safe to just tell the code that the
324 * cursor is in hyperspace and let the next mvcur()
325 * call straighten things out.
330 else if (auto_right_margin)
339 position_check(SP->_cursrow, SP->_curscol, "wrap_cursor");
342 static inline void PutChar(chtype const ch)
343 /* insert character, handling automargin stuff */
345 if (SP->_cursrow == screen_lines-1 && SP->_curscol == screen_columns-1)
350 if (SP->_curscol >= screen_columns)
353 position_check(SP->_cursrow, SP->_curscol, "PutChar");
357 * Issue a given span of characters from an array.
358 * Must be functionally equivalent to:
359 * for (i = 0; i < num; i++)
361 * but can leave the cursor positioned at the middle of the interval.
363 * Returns: 0 - cursor is at the end of interval
364 * 1 - cursor is somewhere in the middle
366 * This code is optimized using ech and rep.
368 static int EmitRange(const chtype *ntext, int num)
372 if (erase_chars || repeat_char)
379 while (num>1 && ntext[0]!=ntext[1])
393 while (runcount < num && ntext[runcount] == ntext0)
397 * The cost expression in the middle isn't exactly right.
398 * _cup_cost is an upper bound on the cost for moving to the
399 * end of the erased area, but not the cost itself (which we
400 * can't compute without emitting the move). This may result
401 * in erase_chars not getting used in some situations for
402 * which it would be marginally advantageous.
405 && runcount > SP->_ech_cost + SP->_cup_cost
406 && can_clear_with(ntext0))
409 putp(tparm(erase_chars, runcount));
412 * If this is the last part of the given interval,
413 * don't bother moving cursor, since it can be the
414 * last update on the line.
417 GoTo(SP->_cursrow, SP->_curscol + runcount);
419 return 1; /* cursor stays in the middle */
421 else if (repeat_char && runcount > SP->_rep_cost)
423 bool wrap_possible = (SP->_curscol + runcount >= screen_columns);
424 int rep_count = runcount;
430 putp(tparm(repeat_char, TextOf(ntext0), rep_count));
431 SP->_curscol += rep_count;
438 for (i = 0; i < runcount; i++)
447 for (i = 0; i < num; i++)
453 * Output the line in the given range [first .. last]
455 * If there's a run of identical characters that's long enough to justify
456 * cursor movement, use that also.
458 * Returns: same as EmitRange
467 int cost = min(SP->_cup_ch_cost, SP->_hpa_ch_cost);
469 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %d, %d, %d)",
470 otext, ntext, row, first, last));
473 && (last-first+1) > cost) {
474 for (j = first, run = 0; j <= last; j++) {
475 if (otext[j] == ntext[j]) {
479 int before_run = (j - run);
480 EmitRange(ntext+first, before_run-first);
481 GoTo(row, first = j);
487 return EmitRange(ntext + first, last-first+1);
490 #if CC_HAS_INLINE_FUNCS
491 static void callPutChar(chtype const ch)
497 #define MARK_NOCHANGE(win,row) \
499 win->_line[row].firstchar = _NOCHANGE; \
500 win->_line[row].lastchar = _NOCHANGE; \
501 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row); \
509 struct tms before, after;
510 #endif /* USE_TRACE_TIMES */
512 T((T_CALLED("doupdate()")));
515 if (_nc_tracing & TRACE_UPDATE)
518 _tracef("curscr is clear");
520 _tracedump("curscr", curscr);
521 _tracedump("newscr", newscr);
525 _nc_signal_handler(FALSE);
531 if (SP->_endwin || SP->_sig_winch)
534 * This is a transparent extension: XSI does not address it,
535 * and applications need not know that ncurses can do it.
537 * Check if the terminal size has changed while curses was off
538 * (this can happen in an xterm, for example), and resize the
539 * ncurses data structures accordingly.
541 _nc_update_screensize();
547 T(("coming back from shell mode"));
552 SP->_mouse_resume(SP);
558 /* zero the metering machinery */
560 (void) times(&before);
561 #endif /* USE_TRACE_TIMES */
564 * This is the support for magic-cookie terminals. The
565 * theory: we scan the virtual screen looking for attribute
566 * turnons. Where we find one, check to make sure it's
567 * realizable by seeing if the required number of
568 * un-attributed blanks are present before and after the
569 * attributed range; try to shift the range boundaries over
570 * blanks (not changing the screen display) so this becomes
571 * true. If it is, shift the beginning attribute change
572 * appropriately (the end one, if we've gotten this far, is
573 * guaranteed room for its cookie). If not, nuke the added
574 * attributes out of the span.
577 if (magic_cookie_glitch > 0) {
579 attr_t rattr = A_NORMAL;
581 for (i = 0; i < screen_lines; i++)
582 for (j = 0; j < screen_columns; j++)
585 chtype turnon = AttrOf(newscr->_line[i].text[j]) & ~rattr;
587 /* is an attribute turned on here? */
589 rattr = AttrOf(newscr->_line[i].text[j]);
593 T(("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
594 T(("...to %s",_traceattr(turnon)));
597 * If the attribute change location is a blank with a
598 * "safe" attribute, undo the attribute turnon. This may
599 * ensure there's enough room to set the attribute before
600 * the first non-blank in the run.
602 #define SAFE(a) !((a) & (chtype)~NONBLANK_ATTR)
603 if (TextOf(newscr->_line[i].text[j])==' ' && SAFE(turnon))
605 newscr->_line[i].text[j] &= ~turnon;
609 /* check that there's enough room at start of span */
610 for (k = 1; k <= magic_cookie_glitch; k++)
612 || TextOf(newscr->_line[i].text[j-k]) != ' '
613 || !SAFE(AttrOf(newscr->_line[i].text[j-k])))
617 bool end_onscreen = FALSE;
620 /* find end of span, if it's onscreen */
621 for (m = i; m < screen_lines; m++)
623 for ( ; n < screen_columns; n++)
625 if (AttrOf(newscr->_line[m].text[n]) == rattr)
628 T(("Range attributed with %s ends at (%d, %d)",
629 _traceattr(turnon),m,n));
635 T(("Range attributed with %s ends offscreen",
636 _traceattr(turnon)));
641 chtype *lastline = newscr->_line[m].text;
644 * If there are safely-attributed blanks at the
645 * end of the range, shorten the range. This will
646 * help ensure that there is enough room at end
650 && TextOf(lastline[n]) == ' '
651 && SAFE(AttrOf(lastline[n])))
652 lastline[n--] &= ~turnon;
654 /* check that there's enough room at end of span */
655 for (k = 1; k <= magic_cookie_glitch; k++)
656 if (n + k >= screen_columns
657 || TextOf(lastline[n + k]) != ' '
658 || !SAFE(AttrOf(lastline[n+k])))
667 T(("Clearing %s beginning at (%d, %d)",
668 _traceattr(turnon), i, j));
670 /* turn off new attributes over span */
671 for (p = i; p < screen_lines; p++)
673 for ( ; q < screen_columns; q++)
675 if (AttrOf(newscr->_line[p].text[q]) == rattr)
677 newscr->_line[p].text[q] &= ~turnon;
685 T(("Cookie space for %s found before (%d, %d)",
686 _traceattr(turnon), i, j));
689 * back up the start of range so there's room
690 * for cookies before the first nonblank character
692 for (k = 1; k <= magic_cookie_glitch; k++)
693 newscr->_line[i].text[j-k] |= turnon;
696 rattr = AttrOf(newscr->_line[i].text[j]);
700 /* show altered highlights after magic-cookie check */
701 if (_nc_tracing & TRACE_UPDATE)
703 _tracef("After magic-cookie check...");
704 _tracedump("newscr", newscr);
708 #endif /* USE_XMC_SUPPORT */
711 if (curscr->_clear || newscr->_clear) { /* force refresh ? */
712 T(("clearing and updating from scratch"));
714 curscr->_clear = FALSE; /* reset flag */
715 newscr->_clear = FALSE; /* reset flag */
717 int changedlines = CHECK_INTERVAL;
722 nonempty = min(screen_lines, newscr->_maxy+1);
724 if (SP->_scrolling) {
725 _nc_scroll_optimize();
728 nonempty = ClrBottom(nonempty);
730 T(("Transforming lines, nonempty %d", nonempty));
731 for (i = 0; i < nonempty; i++) {
733 * Here is our line-breakout optimization.
735 if (changedlines == CHECK_INTERVAL)
743 * newscr->line[i].firstchar is normally set
744 * by wnoutrefresh. curscr->line[i].firstchar
745 * is normally set by _nc_scroll_window in the
746 * vertical-movement optimization code,
748 if (newscr->_line[i].firstchar != _NOCHANGE
749 || curscr->_line[i].firstchar != _NOCHANGE)
755 /* mark line changed successfully */
756 if (i <= newscr->_maxy)
757 MARK_NOCHANGE(newscr,i)
758 if (i <= curscr->_maxy)
759 MARK_NOCHANGE(curscr,i)
763 /* put everything back in sync */
764 for (i = nonempty; i <= newscr->_maxy; i++)
765 MARK_NOCHANGE(newscr,i)
766 for (i = nonempty; i <= curscr->_maxy; i++)
767 MARK_NOCHANGE(curscr,i)
769 if (!newscr->_leaveok)
771 curscr->_curx = newscr->_curx;
772 curscr->_cury = newscr->_cury;
774 GoTo(curscr->_cury, curscr->_curx);
779 * Keep the physical screen in normal mode in case we get other
780 * processes writing to the screen.
782 UpdateAttrs(A_NORMAL);
785 curscr->_attrs = newscr->_attrs;
786 /* curscr->_bkgd = newscr->_bkgd; */
789 (void) times(&after);
790 TR(TRACE_TIMES, ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
792 after.tms_stime-before.tms_stime,
793 after.tms_utime-before.tms_utime));
794 #endif /* USE_TRACE_TIMES */
796 _nc_signal_handler(TRUE);
804 * Returns the attributed character that corresponds to the "cleared"
805 * screen. If the terminal has the back-color-erase feature, this will be
806 * colored according to the wbkgd() call.
808 * We treat 'curscr' specially because it isn't supposed to be set directly
809 * in the wbkgd() call. Assume 'stdscr' for this case.
811 #define BCE_ATTRS (A_NORMAL|A_COLOR)
812 #define BCE_BKGD(win) (((win) == curscr ? stdscr : (win))->_bkgd)
814 static inline chtype ClrBlank (WINDOW *win)
816 chtype blank = BLANK;
817 if (back_color_erase)
818 blank |= (BCE_BKGD(win) & BCE_ATTRS);
825 ** Update by clearing and redrawing the entire screen.
829 static void ClrUpdate(void)
832 chtype blank = ClrBlank(stdscr);
833 int nonempty = min(screen_lines, newscr->_maxy+1);
835 T(("ClrUpdate() called"));
839 T(("updating screen from scratch"));
841 nonempty = ClrBottom(nonempty);
843 for (i = 0; i < nonempty; i++)
850 ** Clear to end of current line, starting at the cursor position
853 static void ClrToEOL(chtype blank)
856 bool needclear = FALSE;
858 for (j = SP->_curscol; j < screen_columns; j++)
860 chtype *cp = &(curscr->_line[SP->_cursrow].text[j]);
872 TPUTS_TRACE("clr_eol");
873 if (SP->_el_cost > (screen_columns - SP->_curscol))
875 int count = (screen_columns - SP->_curscol);
887 ** Clear to end of screen, starting at the cursor position
890 static void ClrToEOS(chtype blank)
895 TPUTS_TRACE("clr_eos");
897 tputs(clr_eos, screen_lines-row, _nc_outch);
899 for (col = SP->_curscol; col < screen_columns; col++)
900 curscr->_line[row].text[col] = blank;
902 for (row++; row < screen_lines; row++)
904 for (col = 0; col < screen_columns; col++)
905 curscr->_line[row].text[col] = blank;
912 * Test if clearing the end of the screen would satisfy part of the
913 * screen-update. Do this by scanning backwards through the lines in the
914 * screen, checking if each is blank, and one or more are changed.
916 static int ClrBottom(int total)
918 static chtype *tstLine;
919 static size_t lenLine;
923 int last = min(screen_columns, newscr->_maxx+1);
924 size_t length = sizeof(chtype) * last;
925 chtype blank = newscr->_line[total-1].text[last-1]; /* lower right char */
927 if(!clr_eos || !can_clear_with(blank))
930 if ((tstLine == 0) || (last > (int)lenLine)) {
931 tstLine = typeRealloc(chtype, last, tstLine);
934 for (col = 0; col < last; col++)
935 tstLine[col] = blank;
940 for (row = total-1; row >= 0; row--) {
941 if (memcmp(tstLine, newscr->_line[row].text, length))
943 if (memcmp(tstLine, curscr->_line[row].text, length))
947 /* don't use clr_eos for just one line if clr_eol available */
948 if (top < total-1 || (top < total && !clr_eol && !clr_bol)) {
952 if (SP->oldhash && SP->newhash)
954 for (row = top; row < screen_lines; row++)
955 SP->oldhash[row] = SP->newhash[row];
961 FreeAndNull(tstLine);
968 ** TransformLine(lineno)
970 ** Transform the given line in curscr to the one in newscr, using
971 ** Insert/Delete Character if _nc_idcok && has_ic().
973 ** firstChar = position of first different character in line
974 ** oLastChar = position of last different character in old line
975 ** nLastChar = position of last different character in new line
978 ** overwrite chars up to min(oLastChar, nLastChar)
979 ** if oLastChar < nLastChar
980 ** insert newLine[oLastChar+1..nLastChar]
982 ** delete oLastChar - nLastChar spaces
985 static void TransformLine(int const lineno)
987 int firstChar, oLastChar, nLastChar;
988 chtype *newLine = newscr->_line[lineno].text;
989 chtype *oldLine = curscr->_line[lineno].text;
991 bool attrchanged = FALSE;
993 T(("TransformLine(%d) called", lineno));
995 /* copy new hash value to old one */
996 if (SP->oldhash && SP->newhash)
997 SP->oldhash[lineno] = SP->newhash[lineno];
999 if(ceol_standout_glitch && clr_eol) {
1001 while(firstChar < screen_columns) {
1002 if(AttrOf(newLine[firstChar]) != AttrOf(oldLine[firstChar]))
1010 if (attrchanged) { /* we may have to disregard the whole line */
1011 GoTo(lineno, firstChar);
1012 ClrToEOL(ClrBlank(curscr));
1013 PutRange(oldLine, newLine, lineno, 0, (screen_columns-1));
1016 #define NEW(r,c) newscr->_line[r].text[c]
1017 #define xmc_turn_on(a,b) ((((a)^(b)) & ~(a) & SP->_xmc_triggers) != 0)
1018 #define xmc_turn_off(a,b) xmc_turn_on(b,a)
1021 * This is a very simple loop to paint characters which may have the
1022 * magic cookie glitch embedded. It doesn't know much about video
1023 * attributes which are continued from one line to the next. It
1024 * assumes that we have filtered out requests for attribute changes
1025 * that do not get mapped to blank positions.
1027 * FIXME: we are not keeping track of where we put the cookies, so this
1028 * will work properly only once, since we may overwrite a cookie in a
1029 * following operation.
1031 } else if (magic_cookie_glitch > 0) {
1032 GoTo(lineno, firstChar);
1033 for (n = 0; n < screen_columns; n++) {
1034 int m = n + magic_cookie_glitch;
1036 /* check for turn-on:
1037 * If we are writing an attributed blank, where the
1038 * previous cell is not attributed.
1040 if (TextOf(newLine[n]) == ' '
1042 && xmc_turn_on(newLine[n-1], newLine[n]))
1045 && xmc_turn_on(NEW(lineno-1,screen_columns-1), newLine[n])))) {
1049 PutChar(newLine[n]);
1051 /* check for turn-off:
1052 * If we are writing an attributed non-blank, where the
1053 * next cell is blank, and not attributed.
1055 if (TextOf(newLine[n]) != ' '
1056 && ((n+1 < screen_columns
1057 && xmc_turn_off(newLine[n], newLine[n+1]))
1058 || (n+1 >= screen_columns
1059 && lineno+1 < screen_lines
1060 && xmc_turn_off(newLine[n], NEW(lineno+1,0))))) {
1070 /* find the first differing character */
1071 while (firstChar < screen_columns &&
1072 newLine[firstChar] == oldLine[firstChar])
1075 /* if there wasn't one, we're done */
1076 if (firstChar >= screen_columns)
1079 /* it may be cheap to clear leading whitespace with clr_bol */
1080 if (clr_bol && can_clear_with(blank=newLine[0]))
1082 int oFirstChar, nFirstChar;
1084 for (oFirstChar = 0; oFirstChar < screen_columns; oFirstChar++)
1085 if (oldLine[oFirstChar] != blank)
1087 for (nFirstChar = 0; nFirstChar < screen_columns; nFirstChar++)
1088 if (newLine[nFirstChar] != blank)
1091 if (nFirstChar > oFirstChar + SP->_el1_cost)
1093 if (nFirstChar >= screen_columns && SP->_el_cost <= SP->_el1_cost)
1097 TPUTS_TRACE("clr_eol");
1102 GoTo(lineno, nFirstChar - 1);
1104 TPUTS_TRACE("clr_bol");
1108 while (firstChar < nFirstChar)
1109 oldLine[firstChar++] = blank;
1111 if (firstChar >= screen_columns)
1116 blank = newLine[screen_columns-1];
1118 if(!can_clear_with(blank))
1120 /* find the last differing character */
1121 nLastChar = screen_columns - 1;
1123 while (nLastChar > firstChar
1124 && newLine[nLastChar] == oldLine[nLastChar])
1127 if (nLastChar >= firstChar) {
1128 GoTo(lineno, firstChar);
1129 PutRange(oldLine, newLine, lineno, firstChar, nLastChar);
1130 memcpy( oldLine + firstChar,
1131 newLine + firstChar,
1132 (nLastChar - firstChar + 1) * sizeof(chtype));
1137 /* find last non-blank character on old line */
1138 oLastChar = screen_columns - 1;
1139 while (oLastChar > firstChar && oldLine[oLastChar] == blank)
1142 /* find last non-blank character on new line */
1143 nLastChar = screen_columns - 1;
1144 while (nLastChar > firstChar && newLine[nLastChar] == blank)
1147 if((nLastChar == firstChar)
1148 && (SP->_el_cost < (oLastChar - nLastChar))) {
1149 GoTo(lineno, firstChar);
1150 if(newLine[firstChar] != blank )
1151 PutChar(newLine[firstChar]);
1153 } else if( (nLastChar != oLastChar)
1154 && (newLine[nLastChar] != oldLine[oLastChar]
1155 || !(_nc_idcok && has_ic())) ) {
1156 GoTo(lineno, firstChar);
1157 if ((oLastChar - nLastChar) > SP->_el_cost) {
1158 if(PutRange(oldLine, newLine, lineno, firstChar, nLastChar))
1159 GoTo(lineno, nLastChar+1);
1162 n = max( nLastChar , oLastChar );
1163 PutRange(oldLine, newLine, lineno, firstChar, n);
1166 int nLastNonblank = nLastChar;
1167 int oLastNonblank = oLastChar;
1169 /* find the last characters that really differ */
1170 while (newLine[nLastChar] == oldLine[oLastChar]) {
1172 && oLastChar != 0) {
1180 n = min(oLastChar, nLastChar);
1181 if (n >= firstChar) {
1182 GoTo(lineno, firstChar);
1183 PutRange(oldLine, newLine, lineno, firstChar, n);
1186 if (oLastChar < nLastChar) {
1187 int m = max(nLastNonblank, oLastNonblank);
1189 if (InsCharCost(nLastChar - oLastChar)
1191 PutRange(oldLine, newLine, lineno, n+1, m);
1193 InsStr(&newLine[n+1], nLastChar - oLastChar);
1195 } else if (oLastChar > nLastChar ) {
1197 if (DelCharCost(oLastChar - nLastChar)
1198 > SP->_el_cost + nLastNonblank - (n+1)) {
1199 if(PutRange(oldLine, newLine, lineno,
1200 n+1, nLastNonblank))
1201 GoTo(lineno, nLastNonblank+1);
1205 * The delete-char sequence will
1206 * effectively shift in blanks from the
1207 * right margin of the screen. Ensure
1208 * that they are the right color by
1209 * setting the video attributes from
1210 * the last character on the row.
1213 DelChar(oLastChar - nLastChar);
1219 /* update the code's internal representation */
1220 if (screen_columns > firstChar)
1221 memcpy( oldLine + firstChar,
1222 newLine + firstChar,
1223 (screen_columns - firstChar) * sizeof(chtype));
1227 ** ClearScreen(blank)
1229 ** Clear the physical screen and put cursor at home
1233 static void ClearScreen(chtype blank)
1237 T(("ClearScreen() called"));
1241 TPUTS_TRACE("clear_screen");
1243 SP->_cursrow = SP->_curscol = 0;
1244 position_check(SP->_cursrow, SP->_curscol, "ClearScreen");
1245 } else if (clr_eos) {
1246 SP->_cursrow = SP->_curscol = -1;
1250 TPUTS_TRACE("clr_eos");
1252 } else if (clr_eol) {
1253 SP->_cursrow = SP->_curscol = -1;
1255 for (i = 0; i < screen_lines; i++) {
1258 TPUTS_TRACE("clr_eol");
1263 T(("cannot clear screen"));
1267 for (i = 0; i < screen_lines; i++) {
1268 for (j = 0; j < screen_columns; j++)
1269 curscr->_line[i].text[j] = blank;
1272 T(("screen cleared"));
1276 ** InsStr(line, count)
1278 ** Insert the count characters pointed to by line.
1282 static void InsStr(chtype *line, int count)
1284 T(("InsStr(%p,%d) called", line, count));
1286 /* Prefer parm_ich as it has the smallest cost - no need to shift
1287 * the whole line on each character. */
1288 /* The order must match that of InsCharCost. */
1290 TPUTS_TRACE("parm_ich");
1291 tputs(tparm(parm_ich, count), count, _nc_outch);
1297 } else if (enter_insert_mode && exit_insert_mode) {
1298 TPUTS_TRACE("enter_insert_mode");
1299 putp(enter_insert_mode);
1304 TPUTS_TRACE("insert_padding");
1305 putp(insert_padding);
1310 TPUTS_TRACE("exit_insert_mode");
1311 putp(exit_insert_mode);
1314 TPUTS_TRACE("insert_character");
1315 putp(insert_character);
1319 TPUTS_TRACE("insert_padding");
1320 putp(insert_padding);
1326 position_check(SP->_cursrow, SP->_curscol, "InsStr");
1332 ** Delete count characters at current position
1336 static void DelChar(int count)
1338 T(("DelChar(%d) called, position = (%d,%d)", count, newscr->_cury, newscr->_curx));
1341 TPUTS_TRACE("parm_dch");
1342 tputs(tparm(parm_dch, count), count, _nc_outch);
1346 TPUTS_TRACE("delete_character");
1347 putp(delete_character);
1353 ** _nc_outstr(char *str)
1355 ** Emit a string without waiting for update.
1358 void _nc_outstr(const char *str)
1365 * Physical-scrolling support
1367 * This code was adapted from Keith Bostic's hardware scrolling
1368 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1369 * capabilities, narrowed the call interface slightly, and cleaned
1370 * up some convoluted tests. I also added support for the memory_above
1371 * memory_below, and non_dest_scroll_region capabilities.
1373 * For this code to work, we must have either
1374 * change_scroll_region and scroll forward/reverse commands, or
1375 * insert and delete line capabilities.
1376 * When the scrolling region has been set, the cursor has to
1377 * be at the last line of the region to make the scroll up
1378 * happen, or on the first line of region to scroll down.
1380 * This code makes one aesthetic decision in the opposite way from
1381 * BSD curses. BSD curses preferred pairs of il/dl operations
1382 * over scrolls, allegedly because il/dl looked faster. We, on
1383 * the other hand, prefer scrolls because (a) they're just as fast
1384 * on many terminals and (b) using them avoids bouncing an
1385 * unchanged bottom section of the screen up and down, which is
1388 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1390 * I used assumption that capabilities il/il1/dl/dl1 work inside
1391 * changed scroll region not shifting screen contents outside of it.
1392 * If there are any terminals behaving different way, it would be
1393 * necessary to add some conditions to scroll_csr_forward/backward.
1396 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1397 static int scroll_csr_forward(int n, int top, int bot, int miny, int maxy, chtype blank)
1401 if (n == 1 && scroll_forward && top == miny && bot == maxy)
1405 TPUTS_TRACE("scroll_forward");
1406 tputs(scroll_forward, 0, _nc_outch);
1408 else if (n == 1 && delete_line && bot == maxy)
1412 TPUTS_TRACE("delete_line");
1413 tputs(delete_line, 0, _nc_outch);
1415 else if (parm_index && top == miny && bot == maxy)
1419 TPUTS_TRACE("parm_index");
1420 tputs(tparm(parm_index, n, 0), n, _nc_outch);
1422 else if (parm_delete_line && bot == maxy)
1426 TPUTS_TRACE("parm_delete_line");
1427 tputs(tparm(parm_delete_line, n, 0), n, _nc_outch);
1429 else if (scroll_forward && top == miny && bot == maxy)
1433 for (i = 0; i < n; i++)
1435 TPUTS_TRACE("scroll_forward");
1436 tputs(scroll_forward, 0, _nc_outch);
1439 else if (delete_line && bot == maxy)
1443 for (i = 0; i < n; i++)
1445 TPUTS_TRACE("delete_line");
1446 tputs(delete_line, 0, _nc_outch);
1455 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1457 static int scroll_csr_backward(int n, int top, int bot, int miny, int maxy, chtype blank)
1461 if (n == 1 && scroll_reverse && top == miny && bot == maxy)
1465 TPUTS_TRACE("scroll_reverse");
1466 tputs(scroll_reverse, 0, _nc_outch);
1468 else if (n == 1 && insert_line && bot == maxy)
1472 TPUTS_TRACE("insert_line");
1473 tputs(insert_line, 0, _nc_outch);
1475 else if (parm_rindex && top == miny && bot == maxy)
1479 TPUTS_TRACE("parm_rindex");
1480 tputs(tparm(parm_rindex, n, 0), n, _nc_outch);
1482 else if (parm_insert_line && bot == maxy)
1486 TPUTS_TRACE("parm_insert_line");
1487 tputs(tparm(parm_insert_line, n, 0), n, _nc_outch);
1489 else if (scroll_reverse && top == miny && bot == maxy)
1493 for (i = 0; i < n; i++)
1495 TPUTS_TRACE("scroll_reverse");
1496 tputs(scroll_reverse, 0, _nc_outch);
1499 else if (insert_line && bot == maxy)
1503 for (i = 0; i < n; i++)
1505 TPUTS_TRACE("insert_line");
1506 tputs(insert_line, 0, _nc_outch);
1515 /* scroll by using delete_line at del and insert_line at ins */
1517 static int scroll_idl(int n, int del, int ins, chtype blank)
1521 if(!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1526 if (n == 1 && delete_line)
1528 TPUTS_TRACE("delete_line");
1529 tputs(delete_line, 0, _nc_outch);
1531 else if (parm_delete_line)
1533 TPUTS_TRACE("parm_delete_line");
1534 tputs(tparm(parm_delete_line, n, 0), n, _nc_outch);
1536 else /* if (delete_line) */
1538 for (i = 0; i < n; i++)
1540 TPUTS_TRACE("delete_line");
1541 tputs(delete_line, 0, _nc_outch);
1547 if (n == 1 && insert_line)
1549 TPUTS_TRACE("insert_line");
1550 tputs(insert_line, 0, _nc_outch);
1552 else if (parm_insert_line)
1554 TPUTS_TRACE("parm_insert_line");
1555 tputs(tparm(parm_insert_line, n, 0), n, _nc_outch);
1557 else /* if (insert_line) */
1559 for (i = 0; i < n; i++)
1561 TPUTS_TRACE("insert_line");
1562 tputs(insert_line, 0, _nc_outch);
1569 int _nc_scrolln(int n, int top, int bot, int maxy)
1570 /* scroll region from top to bot by n lines */
1572 chtype blank=ClrBlank(stdscr);
1574 bool cursor_saved=FALSE;
1577 TR(TRACE_MOVE, ("mvcur_scrolln(%d, %d, %d, %d)", n, top, bot, maxy));
1581 * If we scroll, we might remove a cookie.
1583 if (magic_cookie_glitch > 0) {
1588 if (n > 0) /* scroll up (forward) */
1591 * Explicitly clear if stuff pushed off top of region might
1592 * be saved by the terminal.
1594 if (non_dest_scroll_region || (memory_above && top == 0)) {
1595 for (i = 0; i < n; i++)
1602 res = scroll_csr_forward(n, top, bot, 0, maxy, blank);
1604 if (res == ERR && change_scroll_region)
1606 if ((((n==1 && scroll_forward) || parm_index)
1607 && (SP->_cursrow == bot || SP->_cursrow == bot-1))
1608 && save_cursor && restore_cursor)
1611 TPUTS_TRACE("save_cursor");
1612 tputs(save_cursor, 0, _nc_outch);
1614 TPUTS_TRACE("change_scroll_region");
1615 tputs(tparm(change_scroll_region, top, bot), 0, _nc_outch);
1618 TPUTS_TRACE("restore_cursor");
1619 tputs(restore_cursor, 0, _nc_outch);
1623 SP->_cursrow = SP->_curscol = -1;
1626 res = scroll_csr_forward(n, top, bot, top, bot, blank);
1628 TPUTS_TRACE("change_scroll_region");
1629 tputs(tparm(change_scroll_region, 0, maxy), 0, _nc_outch);
1630 SP->_cursrow = SP->_curscol = -1;
1633 if (res == ERR && _nc_idlok)
1634 res = scroll_idl(n, top, bot-n+1, blank);
1636 else /* (n < 0) - scroll down (backward) */
1639 * Do explicit clear to end of region if it's possible that the
1640 * terminal might hold on to stuff we push off the end.
1642 if (non_dest_scroll_region || (memory_below && bot == maxy))
1644 if (bot == maxy && clr_eos)
1651 for (i = 0; i < -n; i++)
1653 GoTo(maxy + n + i, 0);
1659 res = scroll_csr_backward(-n, top, bot, 0, maxy, blank);
1661 if (res == ERR && change_scroll_region)
1663 if (top != 0 && (SP->_cursrow == top || SP->_cursrow == top-1)
1664 && save_cursor && restore_cursor)
1667 TPUTS_TRACE("save_cursor");
1668 tputs(save_cursor, 0, _nc_outch);
1670 TPUTS_TRACE("change_scroll_region");
1671 tputs(tparm(change_scroll_region, top, bot), 0, _nc_outch);
1674 TPUTS_TRACE("restore_cursor");
1675 tputs(restore_cursor, 0, _nc_outch);
1679 SP->_cursrow = SP->_curscol = -1;
1682 res = scroll_csr_backward(-n, top, bot, top, bot, blank);
1684 TPUTS_TRACE("change_scroll_region");
1685 tputs(tparm(change_scroll_region, 0, maxy), 0, _nc_outch);
1686 SP->_cursrow = SP->_curscol = -1;
1689 if (res == ERR && _nc_idlok)
1690 res = scroll_idl(-n, bot+n+1, top, blank);
1696 _nc_scroll_window(curscr, n, top, bot, blank);
1698 /* shift hash values too - they can be reused */
1699 _nc_scroll_oldhash(n, top, bot);
1705 void _nc_screen_resume()
1707 /* make sure terminal is in a sane known state */
1708 SP->_current_attr = A_NORMAL;
1709 newscr->_clear = TRUE;
1711 if (SP->_coloron == TRUE && orig_pair)
1713 if (exit_attribute_mode)
1714 putp(exit_attribute_mode);
1717 /* turn off attributes */
1718 if (exit_alt_charset_mode)
1719 putp(exit_alt_charset_mode);
1720 if (exit_standout_mode)
1721 putp(exit_standout_mode);
1722 if (exit_underline_mode)
1723 putp(exit_underline_mode);
1725 if (exit_insert_mode)
1726 putp(exit_insert_mode);
1727 if (enter_am_mode && exit_am_mode)
1728 putp(auto_right_margin ? enter_am_mode : exit_am_mode);
1731 void _nc_screen_init()
1733 _nc_screen_resume();
1736 /* wrap up screen handling */
1737 void _nc_screen_wrap()
1739 UpdateAttrs(A_NORMAL);
1743 void _nc_do_xmc_glitch(attr_t previous)
1745 attr_t chg = XMC_CHANGES(previous ^ SP->_current_attr);
1749 SP->_curscol += magic_cookie_glitch;
1750 if (SP->_curscol >= SP->_columns)
1752 T(("bumped to %d,%d after cookie", SP->_cursrow, SP->_curscol));
1757 #endif /* USE_XMC_SUPPORT */