document distributed with 4.2BSD
[unix-history] / usr / src / lib / libcurses / addch.c
index 2b769ac..70960fa 100644 (file)
@@ -1,56 +1,52 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)addch.c    5.1 (Berkeley) %G%";
+#endif not lint
+
 # include      "curses.ext"
 
 /*
  *     This routine adds the character to the current position
  *
 # include      "curses.ext"
 
 /*
  *     This routine adds the character to the current position
  *
- * %G% (Berkeley) @(#)addch.c  1.1
  */
 waddch(win, c)
 reg WINDOW     *win;
 char           c;
 {
        reg int         x, y;
  */
 waddch(win, c)
 reg WINDOW     *win;
 char           c;
 {
        reg int         x, y;
+       reg WINDOW      *wp;
+       reg int         newx;
 
        x = win->_curx;
        y = win->_cury;
 # ifdef FULLDEBUG
        fprintf(outf, "ADDCH('%c') at (%d, %d)\n", c, y, x);
 # endif
 
        x = win->_curx;
        y = win->_cury;
 # ifdef FULLDEBUG
        fprintf(outf, "ADDCH('%c') at (%d, %d)\n", c, y, x);
 # endif
-       if (y >= win->_maxy || x >= win->_maxx || y < 0 || x < 0)
-               return ERR;
        switch (c) {
          case '\t':
        switch (c) {
          case '\t':
-         {
-               reg int         newx;
-
-               --x;
-               for (newx = x + (8 - (x & 07)) + 1; x <= newx; x++)
+               for (newx = x + (8 - (x & 07)); x < newx; x++)
                        if (waddch(win, ' ') == ERR)
                                return ERR;
                return OK;
                        if (waddch(win, ' ') == ERR)
                                return ERR;
                return OK;
-         }
+
          default:
 # ifdef FULLDEBUG
                fprintf(outf, "ADDCH: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
 # endif
                if (win->_flags & _STANDOUT)
                        c |= _STANDOUT;
          default:
 # ifdef FULLDEBUG
                fprintf(outf, "ADDCH: 1: y = %d, x = %d, firstch = %d, lastch = %d\n", y, x, win->_firstch[y], win->_lastch[y]);
 # endif
                if (win->_flags & _STANDOUT)
                        c |= _STANDOUT;
-               if (win->_y[y][x] != c) {
-                       if (win->_firstch[y] == _NOCHANGE)
-                               win->_firstch[y] = win->_lastch[y] = x;
-                       else if (x < win->_firstch[y])
-                               win->_firstch[y] = x;
-                       else if (x > win->_lastch[y])
-                               win->_lastch[y] = x;
-               }
+               set_ch(win, y, x, c);
                win->_y[y][x++] = c;
                if (x >= win->_maxx) {
                win->_y[y][x++] = c;
                if (x >= win->_maxx) {
-newline:
                        x = 0;
                        x = 0;
-nonewline:
-                       if (++y + 1 == win->_maxy)
+newline:
+                       if (++y >= win->_maxy)
                                if (win->_scroll) {
                                if (win->_scroll) {
-                                       wrefresh(win);
                                        scroll(win);
                                        --y;
                                }
                                        scroll(win);
                                        --y;
                                }
@@ -63,10 +59,9 @@ nonewline:
                break;
          case '\n':
                wclrtoeol(win);
                break;
          case '\n':
                wclrtoeol(win);
-               if (NONL)
-                       goto nonewline;
-               else
-                       goto newline;
+               if (!NONL)
+                       x = 0;
+               goto newline;
          case '\r':
                x = 0;
                break;
          case '\r':
                x = 0;
                break;
@@ -79,3 +74,32 @@ nonewline:
        win->_cury = y;
        return OK;
 }
        win->_cury = y;
        return OK;
 }
+
+/*
+ * set_ch:
+ *     Set the first and last change flags for this window.
+ */
+static
+set_ch(win, y, x, ch)
+reg WINDOW     *win;
+int            y, x;
+{
+# ifdef        FULLDEBUG
+       fprintf(outf, "SET_CH(%0.2o, %d, %d)\n", win, y, x);
+# endif
+       if (win->_y[y][x] != ch) {
+               x += win->_ch_off;
+               if (win->_firstch[y] == _NOCHANGE)
+                       win->_firstch[y] = win->_lastch[y] = x;
+               else if (x < win->_firstch[y])
+                       win->_firstch[y] = x;
+               else if (x > win->_lastch[y])
+                       win->_lastch[y] = x;
+# ifdef FULLDEBUG
+               fprintf(outf, "SET_CH: change gives f/l: %d/%d [%d/%d]\n",
+                       win->_firstch[y], win->_lastch[y],
+                       win->_firstch[y] - win->_ch_off,
+                       win->_lastch[y] - win->_ch_off);
+# endif
+       }
+}