insert mode bug fix and cleanup
[unix-history] / usr / src / usr.bin / window / wwupdate.c
index 45743f3..5fda014 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)wwupdate.c 3.22 (Berkeley) %G%";
+static char sccsid[] = "@(#)wwupdate.c 3.26 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "ww.h"
 #endif /* not lint */
 
 #include "ww.h"
@@ -28,7 +28,6 @@ wwupdate1(top, bot)
        register j;
        char *touched;
        struct ww_update *upd;
        register j;
        char *touched;
        struct ww_update *upd;
-       char didit;
        char check_clreos = 0;
        int scan_top, scan_bot;
 
        char check_clreos = 0;
        int scan_top, scan_bot;
 
@@ -48,9 +47,17 @@ wwupdate1(top, bot)
                    (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
                        int st = tt.tt_clreos != 0 ? scan_top : 0;
 
                    (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
                        int st = tt.tt_clreos != 0 ? scan_top : 0;
 
-                       for (n = 1; t1 < t2;)
+                       /*
+                        * t1 is one past the first touched row,
+                        * t2 is on the last touched row.
+                        */
+                       for (t1--, n = 1; t1 < t2;)
                                if (*t1++)
                                        n++;
                                if (*t1++)
                                        n++;
+                       /*
+                        * If we can't clreos then we try for clearing
+                        * the whole screen.
+                        */
                        if (check_clreos = n * 10 > (wwnrow - st) * 9) {
                                scan_top = st;
                                scan_bot = wwnrow;
                        if (check_clreos = n * 10 > (wwnrow - st) * 9) {
                                scan_top = st;
                                scan_bot = wwnrow;
@@ -108,10 +115,18 @@ wwupdate1(top, bot)
                register simple_gain = 0;
                char didit = 0;
 
                register simple_gain = 0;
                char didit = 0;
 
+               /*
+                * gain is the advantage of clearing all the lines.
+                * best_gain is the advantage of clearing to eos
+                * at best_row and u->best_col.
+                * simple_gain is the advantage of using only clreol.
+                * We use g > best_gain because u->best_col can be
+                * undefined when u->best_gain is 0 so we can't use it.
+                */
                for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
                        register g = gain + u->best_gain;
 
                for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
                        register g = gain + u->best_gain;
 
-                       if (g >= best_gain) {
+                       if (g > best_gain) {
                                best_gain = g;
                                best_row = j;
                        }
                                best_gain = g;
                                best_row = j;
                        }
@@ -121,7 +136,7 @@ wwupdate1(top, bot)
                }
                if (tt.tt_clreos == 0) {
                        if (gain > simple_gain && gain > 4) {
                }
                if (tt.tt_clreos == 0) {
                        if (gain > simple_gain && gain > 4) {
-                               (*tt.tt_clear)();
+                               xxclear();
                                i = top = scan_top;
                                bot = scan_bot;
                                j = 0;
                                i = top = scan_top;
                                bot = scan_bot;
                                j = 0;
@@ -130,8 +145,7 @@ wwupdate1(top, bot)
                } else
                        if (best_gain > simple_gain && best_gain > 4) {
                                i = best_row;
                } else
                        if (best_gain > simple_gain && best_gain > 4) {
                                i = best_row;
-                               (*tt.tt_move)(i, j = wwupd[i].best_col);
-                               (*tt.tt_clreos)();
+                               xxclreos(i, j = wwupd[i].best_col);
                                bot = scan_bot;
                                didit = 1;
                        }
                                bot = scan_bot;
                                didit = 1;
                        }
@@ -164,8 +178,7 @@ simple:
                didit = 0;
                if (tt.tt_clreol != 0 && upd->best_gain > 4) {
                        wwnupdclreol++;
                didit = 0;
                if (tt.tt_clreol != 0 && upd->best_gain > 4) {
                        wwnupdclreol++;
-                       (*tt.tt_move)(i, j = upd->best_col);
-                       (*tt.tt_clreol)();
+                       xxclreol(i, j = upd->best_col);
                        for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
                                os++->c_w = ' ';
                        didit = 1;
                        for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
                                os++->c_w = ' ';
                        didit = 1;
@@ -206,39 +219,39 @@ simple:
                                }
                                j++;
                        }
                                }
                                j++;
                        }
-                       tt.tt_nmodes = m;
                        if (wwwrap
                            && i == wwnrow - 1 && q - buf + c == wwncol) {
                        if (wwwrap
                            && i == wwnrow - 1 && q - buf + c == wwncol) {
-                               if (tt.tt_hasinsert) {
+                               if (tt.tt_inschar) {
                                        if (q - buf != 1) {
                                        if (q - buf != 1) {
-                                               (*tt.tt_move)(i, c);
-                                               (*tt.tt_write)(buf + 1,
-                                                       q - buf - 1);
-                                               (*tt.tt_move)(i, c);
-                                               tt.tt_ninsert = 1;
-                                               (*tt.tt_write)(buf, 1);
-                                               tt.tt_ninsert = 0;
+                                               xxwrite(i, c, buf + 1,
+                                                       q - buf - 1, m);
+                                               xxinschar(i, c, *buf, m);
                                        } else {
                                        } else {
-                                               (*tt.tt_move)(i, c - 1);
-                                               (*tt.tt_write)(buf, 1);
-                                               tt.tt_nmodes = ns[-2].c_m;
-                                               (*tt.tt_move)(i, c - 1);
-                                               tt.tt_ninsert = 1;
-                                               (*tt.tt_write)(&ns[-2].c_c, 1);
-                                               tt.tt_ninsert = 0;
+                                               xxwrite(i, c - 1, buf, 1, m);
+                                               xxinschar(i, c - 1,
+                                                       ns[-2].c_c, ns[-2].c_m);
                                        }
                                        }
-                               } else {
-                                       if (q - buf > 1) {
-                                               (*tt.tt_move)(i, c);
-                                               (*tt.tt_write)(buf, q-buf-1);
+                               } else if (tt.tt_insspace) {
+                                       if (q - buf != 1) {
+                                               xxwrite(i, c, buf + 1,
+                                                       q - buf - 1, m);
+                                               xxinsspace(i, c);
+                                               xxwrite(i, c, buf, 1, m);
+                                       } else {
+                                               xxwrite(i, c - 1, buf, 1, m);
+                                               xxinsspace(i, c - 1);
+                                               xxwrite(i, c - 1, &ns[-2].c_c,
+                                                       1, ns[-2].c_m);
                                        }
                                        }
+                               } else {
+                                       if (q - buf > 1)
+                                               xxwrite(i, c, buf,
+                                                       q - buf - 1, m);
                                        os[-1] = lastc;
                                        *touched = WWU_TOUCHED;
                                }
                                        os[-1] = lastc;
                                        *touched = WWU_TOUCHED;
                                }
-                       } else {
-                               (*tt.tt_move)(i, c);
-                               (*tt.tt_write)(buf, q - buf);
-                       }
+                       } else
+                               xxwrite(i, c, buf, q - buf, m);
                        didit = 1;
                }
                if (!didit)
                        didit = 1;
                }
                if (!didit)