Home | History | Annotate | Line # | Download | only in vi
vs_smap.c revision 1.2
      1  1.2  christos /*	$NetBSD: vs_smap.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
      2  1.1  christos /*-
      3  1.1  christos  * Copyright (c) 1993, 1994
      4  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      5  1.1  christos  * Copyright (c) 1993, 1994, 1995, 1996
      6  1.1  christos  *	Keith Bostic.  All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * See the LICENSE file for redistribution information.
      9  1.1  christos  */
     10  1.1  christos 
     11  1.1  christos #include "config.h"
     12  1.1  christos 
     13  1.1  christos #ifndef lint
     14  1.1  christos static const char sccsid[] = "Id: vs_smap.c,v 10.30 2002/01/19 21:59:07 skimo Exp  (Berkeley) Date: 2002/01/19 21:59:07 ";
     15  1.1  christos #endif /* not lint */
     16  1.1  christos 
     17  1.1  christos #include <sys/types.h>
     18  1.1  christos #include <sys/queue.h>
     19  1.1  christos #include <sys/time.h>
     20  1.1  christos 
     21  1.1  christos #include <bitstring.h>
     22  1.1  christos #include <limits.h>
     23  1.1  christos #include <stdio.h>
     24  1.1  christos #include <stdlib.h>
     25  1.1  christos #include <string.h>
     26  1.1  christos 
     27  1.1  christos #include "../common/common.h"
     28  1.1  christos #include "vi.h"
     29  1.1  christos 
     30  1.1  christos static int	vs_deleteln __P((SCR *, int));
     31  1.1  christos static int	vs_insertln __P((SCR *, int));
     32  1.1  christos static int	vs_sm_delete __P((SCR *, db_recno_t));
     33  1.1  christos static int	vs_sm_down __P((SCR *, MARK *, db_recno_t, scroll_t, SMAP *));
     34  1.1  christos static int	vs_sm_erase __P((SCR *));
     35  1.1  christos static int	vs_sm_insert __P((SCR *, db_recno_t));
     36  1.1  christos static int	vs_sm_reset __P((SCR *, db_recno_t));
     37  1.1  christos static int	vs_sm_up __P((SCR *, MARK *, db_recno_t, scroll_t, SMAP *));
     38  1.1  christos 
     39  1.1  christos /*
     40  1.1  christos  * vs_change --
     41  1.1  christos  *	Make a change to the screen.
     42  1.1  christos  *
     43  1.1  christos  * PUBLIC: int vs_change __P((SCR *, db_recno_t, lnop_t));
     44  1.1  christos  */
     45  1.1  christos int
     46  1.1  christos vs_change(SCR *sp, db_recno_t lno, lnop_t op)
     47  1.1  christos {
     48  1.1  christos 	VI_PRIVATE *vip;
     49  1.1  christos 	SMAP *p;
     50  1.1  christos 	size_t cnt, oldy, oldx;
     51  1.1  christos 
     52  1.1  christos 	vip = VIP(sp);
     53  1.1  christos 
     54  1.1  christos 	/*
     55  1.1  christos 	 * XXX
     56  1.1  christos 	 * Very nasty special case.  The historic vi code displays a single
     57  1.1  christos 	 * space (or a '$' if the list option is set) for the first line in
     58  1.1  christos 	 * an "empty" file.  If we "insert" a line, that line gets scrolled
     59  1.1  christos 	 * down, not repainted, so it's incorrect when we refresh the screen.
     60  1.1  christos 	 * The vi text input functions detect it explicitly and don't insert
     61  1.1  christos 	 * a new line.
     62  1.1  christos 	 *
     63  1.1  christos 	 * Check for line #2 before going to the end of the file.
     64  1.1  christos 	 */
     65  1.1  christos 	if (((op == LINE_APPEND && lno == 0) ||
     66  1.1  christos 	    (op == LINE_INSERT && lno == 1)) &&
     67  1.1  christos 	    !db_exist(sp, 2)) {
     68  1.1  christos 		lno = 1;
     69  1.1  christos 		op = LINE_RESET;
     70  1.1  christos 	}
     71  1.1  christos 
     72  1.1  christos 	/* Appending is the same as inserting, if the line is incremented. */
     73  1.1  christos 	if (op == LINE_APPEND) {
     74  1.1  christos 		++lno;
     75  1.1  christos 		op = LINE_INSERT;
     76  1.1  christos 	}
     77  1.1  christos 
     78  1.1  christos 	/* Ignore the change if the line is after the map. */
     79  1.1  christos 	if (lno > TMAP->lno)
     80  1.1  christos 		return (0);
     81  1.1  christos 
     82  1.1  christos 	/*
     83  1.1  christos 	 * If the line is before the map, and it's a decrement, decrement
     84  1.1  christos 	 * the map.  If it's an increment, increment the map.  Otherwise,
     85  1.1  christos 	 * ignore it.
     86  1.1  christos 	 */
     87  1.1  christos 	if (lno < HMAP->lno) {
     88  1.1  christos 		switch (op) {
     89  1.1  christos 		case LINE_APPEND:
     90  1.1  christos 			abort();
     91  1.1  christos 			/* NOTREACHED */
     92  1.1  christos 		case LINE_DELETE:
     93  1.1  christos 			for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
     94  1.1  christos 				--p->lno;
     95  1.1  christos 			if (sp->lno >= lno)
     96  1.1  christos 				--sp->lno;
     97  1.1  christos 			F_SET(vip, VIP_N_RENUMBER);
     98  1.1  christos 			break;
     99  1.1  christos 		case LINE_INSERT:
    100  1.1  christos 			for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
    101  1.1  christos 				++p->lno;
    102  1.1  christos 			if (sp->lno >= lno)
    103  1.1  christos 				++sp->lno;
    104  1.1  christos 			F_SET(vip, VIP_N_RENUMBER);
    105  1.1  christos 			break;
    106  1.1  christos 		case LINE_RESET:
    107  1.1  christos 			break;
    108  1.1  christos 		}
    109  1.1  christos 		return (0);
    110  1.1  christos 	}
    111  1.1  christos 
    112  1.1  christos 	F_SET(vip, VIP_N_REFRESH);
    113  1.1  christos 
    114  1.1  christos 	/*
    115  1.1  christos 	 * Invalidate the line size cache, and invalidate the cursor if it's
    116  1.1  christos 	 * on this line,
    117  1.1  christos 	 */
    118  1.1  christos 	VI_SCR_CFLUSH(vip);
    119  1.1  christos 	if (sp->lno == lno)
    120  1.1  christos 		F_SET(vip, VIP_CUR_INVALID);
    121  1.1  christos 
    122  1.1  christos 	/*
    123  1.1  christos 	 * If ex modifies the screen after ex output is already on the screen
    124  1.1  christos 	 * or if we've switched into ex canonical mode, don't touch it -- we'll
    125  1.1  christos 	 * get scrolling wrong, at best.
    126  1.1  christos 	 */
    127  1.1  christos 	if (!F_ISSET(sp, SC_TINPUT_INFO) &&
    128  1.1  christos 	    (F_ISSET(sp, SC_SCR_EXWROTE) || VIP(sp)->totalcount > 1)) {
    129  1.1  christos 		F_SET(vip, VIP_N_EX_REDRAW);
    130  1.1  christos 		return (0);
    131  1.1  christos 	}
    132  1.1  christos 
    133  1.1  christos 	/* Save and restore the cursor for these routines. */
    134  1.1  christos 	(void)sp->gp->scr_cursor(sp, &oldy, &oldx);
    135  1.1  christos 
    136  1.1  christos 	switch (op) {
    137  1.1  christos 	case LINE_DELETE:
    138  1.1  christos 		if (vs_sm_delete(sp, lno))
    139  1.1  christos 			return (1);
    140  1.1  christos 		if (sp->lno > lno)
    141  1.1  christos 			--sp->lno;
    142  1.1  christos 		F_SET(vip, VIP_N_RENUMBER);
    143  1.1  christos 		break;
    144  1.1  christos 	case LINE_INSERT:
    145  1.1  christos 		if (vs_sm_insert(sp, lno))
    146  1.1  christos 			return (1);
    147  1.1  christos 		if (sp->lno > lno)
    148  1.1  christos 			++sp->lno;
    149  1.1  christos 		F_SET(vip, VIP_N_RENUMBER);
    150  1.1  christos 		break;
    151  1.1  christos 	case LINE_RESET:
    152  1.1  christos 		if (vs_sm_reset(sp, lno))
    153  1.1  christos 			return (1);
    154  1.1  christos 		break;
    155  1.1  christos 	default:
    156  1.1  christos 		abort();
    157  1.1  christos 	}
    158  1.1  christos 
    159  1.1  christos 	(void)sp->gp->scr_move(sp, oldy, oldx);
    160  1.1  christos 	return (0);
    161  1.1  christos }
    162  1.1  christos 
    163  1.1  christos /*
    164  1.1  christos  * vs_sm_fill --
    165  1.1  christos  *	Fill in the screen map, placing the specified line at the
    166  1.1  christos  *	right position.  There isn't any way to tell if an SMAP
    167  1.1  christos  *	entry has been filled in, so this routine had better be
    168  1.1  christos  *	called with P_FILL set before anything else is done.
    169  1.1  christos  *
    170  1.1  christos  * !!!
    171  1.1  christos  * Unexported interface: if lno is OOBLNO, P_TOP means that the HMAP
    172  1.1  christos  * slot is already filled in, P_BOTTOM means that the TMAP slot is
    173  1.1  christos  * already filled in, and we just finish up the job.
    174  1.1  christos  *
    175  1.1  christos  * PUBLIC: int vs_sm_fill __P((SCR *, db_recno_t, pos_t));
    176  1.1  christos  */
    177  1.1  christos int
    178  1.1  christos vs_sm_fill(SCR *sp, db_recno_t lno, pos_t pos)
    179  1.1  christos {
    180  1.1  christos 	SMAP *p, tmp;
    181  1.1  christos 	size_t cnt;
    182  1.1  christos 
    183  1.1  christos 	/* Flush all cached information from the SMAP. */
    184  1.1  christos 	for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
    185  1.1  christos 		SMAP_FLUSH(p);
    186  1.1  christos 
    187  1.1  christos 	/*
    188  1.1  christos 	 * If the map is filled, the screen must be redrawn.
    189  1.1  christos 	 *
    190  1.1  christos 	 * XXX
    191  1.1  christos 	 * This is a bug.  We should try and figure out if the desired line
    192  1.1  christos 	 * is already in the map or close by -- scrolling the screen would
    193  1.1  christos 	 * be a lot better than redrawing.
    194  1.1  christos 	 */
    195  1.1  christos 	F_SET(sp, SC_SCR_REDRAW);
    196  1.1  christos 
    197  1.1  christos 	switch (pos) {
    198  1.1  christos 	case P_FILL:
    199  1.1  christos 		tmp.lno = 1;
    200  1.1  christos 		tmp.coff = 0;
    201  1.1  christos 		tmp.soff = 1;
    202  1.1  christos 
    203  1.1  christos 		/* See if less than half a screen from the top. */
    204  1.1  christos 		if (vs_sm_nlines(sp,
    205  1.1  christos 		    &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
    206  1.1  christos 			lno = 1;
    207  1.1  christos 			goto top;
    208  1.1  christos 		}
    209  1.1  christos 
    210  1.1  christos 		/* See if less than half a screen from the bottom. */
    211  1.1  christos 		if (db_last(sp, &tmp.lno))
    212  1.1  christos 			return (1);
    213  1.1  christos 		tmp.coff = 0;
    214  1.1  christos 		tmp.soff = vs_screens(sp, tmp.lno, NULL);
    215  1.1  christos 		if (vs_sm_nlines(sp,
    216  1.1  christos 		    &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
    217  1.1  christos 			TMAP->lno = tmp.lno;
    218  1.1  christos 			TMAP->coff = tmp.coff;
    219  1.1  christos 			TMAP->soff = tmp.soff;
    220  1.1  christos 			goto bottom;
    221  1.1  christos 		}
    222  1.1  christos 		goto middle;
    223  1.1  christos 	case P_TOP:
    224  1.1  christos 		if (lno != OOBLNO) {
    225  1.1  christos top:			HMAP->lno = lno;
    226  1.1  christos 			HMAP->coff = 0;
    227  1.1  christos 			HMAP->soff = 1;
    228  1.1  christos 		} else {
    229  1.1  christos 			/*
    230  1.1  christos 			 * If number of lines HMAP->lno (top line) spans
    231  1.1  christos 			 * changed due to, say reformatting, and now is
    232  1.1  christos 			 * fewer than HMAP->soff, reset so the line is
    233  1.1  christos 			 * redrawn at the top of the screen.
    234  1.1  christos 			 */
    235  1.1  christos 			cnt = vs_screens(sp, HMAP->lno, NULL);
    236  1.1  christos 			if (cnt < HMAP->soff)
    237  1.1  christos 				HMAP->soff = 1;
    238  1.1  christos 		}
    239  1.1  christos 		/* If we fail, just punt. */
    240  1.1  christos 		for (p = HMAP, cnt = sp->t_rows; --cnt; ++p)
    241  1.1  christos 			if (vs_sm_next(sp, p, p + 1))
    242  1.1  christos 				goto err;
    243  1.1  christos 		break;
    244  1.1  christos 	case P_MIDDLE:
    245  1.1  christos 		/* If we fail, guess that the file is too small. */
    246  1.1  christos middle:		p = HMAP + sp->t_rows / 2;
    247  1.1  christos 		p->lno = lno;
    248  1.1  christos 		p->coff = 0;
    249  1.1  christos 		p->soff = 1;
    250  1.1  christos 		for (; p > HMAP; --p)
    251  1.1  christos 			if (vs_sm_prev(sp, p, p - 1)) {
    252  1.1  christos 				lno = 1;
    253  1.1  christos 				goto top;
    254  1.1  christos 			}
    255  1.1  christos 
    256  1.1  christos 		/* If we fail, just punt. */
    257  1.1  christos 		p = HMAP + sp->t_rows / 2;
    258  1.1  christos 		for (; p < TMAP; ++p)
    259  1.1  christos 			if (vs_sm_next(sp, p, p + 1))
    260  1.1  christos 				goto err;
    261  1.1  christos 		break;
    262  1.1  christos 	case P_BOTTOM:
    263  1.1  christos 		if (lno != OOBLNO) {
    264  1.1  christos 			TMAP->lno = lno;
    265  1.1  christos 			TMAP->coff = 0;
    266  1.1  christos 			TMAP->soff = vs_screens(sp, lno, NULL);
    267  1.1  christos 		}
    268  1.1  christos 		/* If we fail, guess that the file is too small. */
    269  1.1  christos bottom:		for (p = TMAP; p > HMAP; --p)
    270  1.1  christos 			if (vs_sm_prev(sp, p, p - 1)) {
    271  1.1  christos 				lno = 1;
    272  1.1  christos 				goto top;
    273  1.1  christos 			}
    274  1.1  christos 		break;
    275  1.1  christos 	default:
    276  1.1  christos 		abort();
    277  1.1  christos 	}
    278  1.1  christos 	return (0);
    279  1.1  christos 
    280  1.1  christos 	/*
    281  1.1  christos 	 * Try and put *something* on the screen.  If this fails, we have a
    282  1.1  christos 	 * serious hard error.
    283  1.1  christos 	 */
    284  1.1  christos err:	HMAP->lno = 1;
    285  1.1  christos 	HMAP->coff = 0;
    286  1.1  christos 	HMAP->soff = 1;
    287  1.1  christos 	for (p = HMAP; p < TMAP; ++p)
    288  1.1  christos 		if (vs_sm_next(sp, p, p + 1))
    289  1.1  christos 			return (1);
    290  1.1  christos 	return (0);
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos /*
    294  1.1  christos  * For the routines vs_sm_reset, vs_sm_delete and vs_sm_insert: if the
    295  1.1  christos  * screen contains only a single line (whether because the screen is small
    296  1.1  christos  * or the line large), it gets fairly exciting.  Skip the fun, set a flag
    297  1.1  christos  * so the screen map is refilled and the screen redrawn, and return.  This
    298  1.1  christos  * is amazingly slow, but it's not clear that anyone will care.
    299  1.1  christos  */
    300  1.1  christos #define	HANDLE_WEIRDNESS(cnt) {						\
    301  1.1  christos 	if (cnt >= sp->t_rows) {					\
    302  1.1  christos 		F_SET(sp, SC_SCR_REFORMAT);				\
    303  1.1  christos 		return (0);						\
    304  1.1  christos 	}								\
    305  1.1  christos }
    306  1.1  christos 
    307  1.1  christos /*
    308  1.1  christos  * vs_sm_delete --
    309  1.1  christos  *	Delete a line out of the SMAP.
    310  1.1  christos  */
    311  1.1  christos static int
    312  1.1  christos vs_sm_delete(SCR *sp, db_recno_t lno)
    313  1.1  christos {
    314  1.1  christos 	SMAP *p, *t;
    315  1.1  christos 	size_t cnt_orig;
    316  1.1  christos 
    317  1.1  christos 	/*
    318  1.1  christos 	 * Find the line in the map, and count the number of screen lines
    319  1.1  christos 	 * which display any part of the deleted line.
    320  1.1  christos 	 */
    321  1.1  christos 	for (p = HMAP; p->lno != lno; ++p);
    322  1.1  christos 	if (O_ISSET(sp, O_LEFTRIGHT))
    323  1.1  christos 		cnt_orig = 1;
    324  1.1  christos 	else
    325  1.1  christos 		for (cnt_orig = 1, t = p + 1;
    326  1.1  christos 		    t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
    327  1.1  christos 
    328  1.1  christos 	HANDLE_WEIRDNESS(cnt_orig);
    329  1.1  christos 
    330  1.1  christos 	/* Delete that many lines from the screen. */
    331  1.1  christos 	(void)sp->gp->scr_move(sp, p - HMAP, 0);
    332  1.1  christos 	if (vs_deleteln(sp, cnt_orig))
    333  1.1  christos 		return (1);
    334  1.1  christos 
    335  1.1  christos 	/* Shift the screen map up. */
    336  1.1  christos 	memmove(p, p + cnt_orig, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
    337  1.1  christos 
    338  1.1  christos 	/* Decrement the line numbers for the rest of the map. */
    339  1.1  christos 	for (t = TMAP - cnt_orig; p <= t; ++p)
    340  1.1  christos 		--p->lno;
    341  1.1  christos 
    342  1.1  christos 	/* Display the new lines. */
    343  1.1  christos 	for (p = TMAP - cnt_orig;;) {
    344  1.1  christos 		if (p < TMAP && vs_sm_next(sp, p, p + 1))
    345  1.1  christos 			return (1);
    346  1.1  christos 		/* vs_sm_next() flushed the cache. */
    347  1.1  christos 		if (vs_line(sp, ++p, NULL, NULL))
    348  1.1  christos 			return (1);
    349  1.1  christos 		if (p == TMAP)
    350  1.1  christos 			break;
    351  1.1  christos 	}
    352  1.1  christos 	return (0);
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos /*
    356  1.1  christos  * vs_sm_insert --
    357  1.1  christos  *	Insert a line into the SMAP.
    358  1.1  christos  */
    359  1.1  christos static int
    360  1.1  christos vs_sm_insert(SCR *sp, db_recno_t lno)
    361  1.1  christos {
    362  1.1  christos 	SMAP *p, *t;
    363  1.1  christos 	size_t cnt_orig, cnt, coff;
    364  1.1  christos 
    365  1.1  christos 	/* Save the offset. */
    366  1.1  christos 	coff = HMAP->coff;
    367  1.1  christos 
    368  1.1  christos 	/*
    369  1.1  christos 	 * Find the line in the map, find out how many screen lines
    370  1.1  christos 	 * needed to display the line.
    371  1.1  christos 	 */
    372  1.1  christos 	for (p = HMAP; p->lno != lno; ++p);
    373  1.1  christos 
    374  1.1  christos 	cnt_orig = vs_screens(sp, lno, NULL);
    375  1.1  christos 	HANDLE_WEIRDNESS(cnt_orig);
    376  1.1  christos 
    377  1.1  christos 	/*
    378  1.1  christos 	 * The lines left in the screen override the number of screen
    379  1.1  christos 	 * lines in the inserted line.
    380  1.1  christos 	 */
    381  1.1  christos 	cnt = (TMAP - p) + 1;
    382  1.1  christos 	if (cnt_orig > cnt)
    383  1.1  christos 		cnt_orig = cnt;
    384  1.1  christos 
    385  1.1  christos 	/* Push down that many lines. */
    386  1.1  christos 	(void)sp->gp->scr_move(sp, p - HMAP, 0);
    387  1.1  christos 	if (vs_insertln(sp, cnt_orig))
    388  1.1  christos 		return (1);
    389  1.1  christos 
    390  1.1  christos 	/* Shift the screen map down. */
    391  1.1  christos 	memmove(p + cnt_orig, p, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
    392  1.1  christos 
    393  1.1  christos 	/* Increment the line numbers for the rest of the map. */
    394  1.1  christos 	for (t = p + cnt_orig; t <= TMAP; ++t)
    395  1.1  christos 		++t->lno;
    396  1.1  christos 
    397  1.1  christos 	/* Fill in the SMAP for the new lines, and display. */
    398  1.1  christos 	for (cnt = 1, t = p; cnt <= cnt_orig; ++t, ++cnt) {
    399  1.1  christos 		t->lno = lno;
    400  1.1  christos 		t->coff = coff;
    401  1.1  christos 		t->soff = cnt;
    402  1.1  christos 		SMAP_FLUSH(t);
    403  1.1  christos 		if (vs_line(sp, t, NULL, NULL))
    404  1.1  christos 			return (1);
    405  1.1  christos 	}
    406  1.1  christos 	return (0);
    407  1.1  christos }
    408  1.1  christos 
    409  1.1  christos /*
    410  1.1  christos  * vs_sm_reset --
    411  1.1  christos  *	Reset a line in the SMAP.
    412  1.1  christos  */
    413  1.1  christos static int
    414  1.1  christos vs_sm_reset(SCR *sp, db_recno_t lno)
    415  1.1  christos {
    416  1.1  christos 	SMAP *p, *t;
    417  1.1  christos 	size_t cnt_orig, cnt_new, cnt, diff;
    418  1.1  christos 
    419  1.1  christos 	/*
    420  1.1  christos 	 * See if the number of on-screen rows taken up by the old display
    421  1.1  christos 	 * for the line is the same as the number needed for the new one.
    422  1.1  christos 	 * If so, repaint, otherwise do it the hard way.
    423  1.1  christos 	 */
    424  1.1  christos 	for (p = HMAP; p->lno != lno; ++p);
    425  1.1  christos 	if (O_ISSET(sp, O_LEFTRIGHT)) {
    426  1.1  christos 		t = p;
    427  1.1  christos 		cnt_orig = cnt_new = 1;
    428  1.1  christos 	} else {
    429  1.1  christos 		for (cnt_orig = 0,
    430  1.1  christos 		    t = p; t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
    431  1.1  christos 		cnt_new = vs_screens(sp, lno, NULL);
    432  1.1  christos 	}
    433  1.1  christos 
    434  1.1  christos 	HANDLE_WEIRDNESS(cnt_orig);
    435  1.1  christos 
    436  1.1  christos 	if (cnt_orig == cnt_new) {
    437  1.1  christos 		do {
    438  1.1  christos 			SMAP_FLUSH(p);
    439  1.1  christos 			if (vs_line(sp, p, NULL, NULL))
    440  1.1  christos 				return (1);
    441  1.1  christos 		} while (++p < t);
    442  1.1  christos 		return (0);
    443  1.1  christos 	}
    444  1.1  christos 
    445  1.1  christos 	if (cnt_orig < cnt_new) {
    446  1.1  christos 		/* Get the difference. */
    447  1.1  christos 		diff = cnt_new - cnt_orig;
    448  1.1  christos 
    449  1.1  christos 		/*
    450  1.1  christos 		 * The lines left in the screen override the number of screen
    451  1.1  christos 		 * lines in the inserted line.
    452  1.1  christos 		 */
    453  1.1  christos 		cnt = (TMAP - p) + 1;
    454  1.1  christos 		if (diff > cnt)
    455  1.1  christos 			diff = cnt;
    456  1.1  christos 
    457  1.1  christos 		/* If there are any following lines, push them down. */
    458  1.1  christos 		if (cnt > 1) {
    459  1.1  christos 			(void)sp->gp->scr_move(sp, p - HMAP, 0);
    460  1.1  christos 			if (vs_insertln(sp, diff))
    461  1.1  christos 				return (1);
    462  1.1  christos 
    463  1.1  christos 			/* Shift the screen map down. */
    464  1.1  christos 			memmove(p + diff, p,
    465  1.1  christos 			    (((TMAP - p) - diff) + 1) * sizeof(SMAP));
    466  1.1  christos 		}
    467  1.1  christos 
    468  1.1  christos 		/* Fill in the SMAP for the replaced line, and display. */
    469  1.1  christos 		for (cnt = 1, t = p; cnt_new-- && t <= TMAP; ++t, ++cnt) {
    470  1.1  christos 			t->lno = lno;
    471  1.1  christos 			t->soff = cnt;
    472  1.1  christos 			SMAP_FLUSH(t);
    473  1.1  christos 			if (vs_line(sp, t, NULL, NULL))
    474  1.1  christos 				return (1);
    475  1.1  christos 		}
    476  1.1  christos 	} else {
    477  1.1  christos 		/* Get the difference. */
    478  1.1  christos 		diff = cnt_orig - cnt_new;
    479  1.1  christos 
    480  1.1  christos 		/* Delete that many lines from the screen. */
    481  1.1  christos 		(void)sp->gp->scr_move(sp, p - HMAP, 0);
    482  1.1  christos 		if (vs_deleteln(sp, diff))
    483  1.1  christos 			return (1);
    484  1.1  christos 
    485  1.1  christos 		/* Shift the screen map up. */
    486  1.1  christos 		memmove(p, p + diff, (((TMAP - p) - diff) + 1) * sizeof(SMAP));
    487  1.1  christos 
    488  1.1  christos 		/* Fill in the SMAP for the replaced line, and display. */
    489  1.1  christos 		for (cnt = 1, t = p; cnt_new--; ++t, ++cnt) {
    490  1.1  christos 			t->lno = lno;
    491  1.1  christos 			t->soff = cnt;
    492  1.1  christos 			SMAP_FLUSH(t);
    493  1.1  christos 			if (vs_line(sp, t, NULL, NULL))
    494  1.1  christos 				return (1);
    495  1.1  christos 		}
    496  1.1  christos 
    497  1.1  christos 		/* Display the new lines at the bottom of the screen. */
    498  1.1  christos 		for (t = TMAP - diff;;) {
    499  1.1  christos 			if (t < TMAP && vs_sm_next(sp, t, t + 1))
    500  1.1  christos 				return (1);
    501  1.1  christos 			/* vs_sm_next() flushed the cache. */
    502  1.1  christos 			if (vs_line(sp, ++t, NULL, NULL))
    503  1.1  christos 				return (1);
    504  1.1  christos 			if (t == TMAP)
    505  1.1  christos 				break;
    506  1.1  christos 		}
    507  1.1  christos 	}
    508  1.1  christos 	return (0);
    509  1.1  christos }
    510  1.1  christos 
    511  1.1  christos /*
    512  1.1  christos  * vs_sm_scroll
    513  1.1  christos  *	Scroll the SMAP up/down count logical lines.  Different
    514  1.1  christos  *	semantics based on the vi command, *sigh*.
    515  1.1  christos  *
    516  1.1  christos  * PUBLIC: int vs_sm_scroll __P((SCR *, MARK *, db_recno_t, scroll_t));
    517  1.1  christos  */
    518  1.1  christos int
    519  1.1  christos vs_sm_scroll(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd)
    520  1.1  christos {
    521  1.1  christos 	SMAP *smp;
    522  1.1  christos 
    523  1.1  christos 	/*
    524  1.1  christos 	 * Invalidate the cursor.  The line is probably going to change,
    525  1.1  christos 	 * (although for ^E and ^Y it may not).  In any case, the scroll
    526  1.1  christos 	 * routines move the cursor to draw things.
    527  1.1  christos 	 */
    528  1.1  christos 	F_SET(VIP(sp), VIP_CUR_INVALID);
    529  1.1  christos 
    530  1.1  christos 	/* Find the cursor in the screen. */
    531  1.1  christos 	if (vs_sm_cursor(sp, &smp))
    532  1.1  christos 		return (1);
    533  1.1  christos 
    534  1.1  christos 	switch (scmd) {
    535  1.1  christos 	case CNTRL_B:
    536  1.1  christos 	case CNTRL_U:
    537  1.1  christos 	case CNTRL_Y:
    538  1.1  christos 	case Z_CARAT:
    539  1.1  christos 		if (vs_sm_down(sp, rp, count, scmd, smp))
    540  1.1  christos 			return (1);
    541  1.1  christos 		break;
    542  1.1  christos 	case CNTRL_D:
    543  1.1  christos 	case CNTRL_E:
    544  1.1  christos 	case CNTRL_F:
    545  1.1  christos 	case Z_PLUS:
    546  1.1  christos 		if (vs_sm_up(sp, rp, count, scmd, smp))
    547  1.1  christos 			return (1);
    548  1.1  christos 		break;
    549  1.1  christos 	default:
    550  1.1  christos 		abort();
    551  1.1  christos 	}
    552  1.1  christos 
    553  1.1  christos 	/*
    554  1.1  christos 	 * !!!
    555  1.1  christos 	 * If we're at the start of a line, go for the first non-blank.
    556  1.1  christos 	 * This makes it look like the old vi, even though we're moving
    557  1.1  christos 	 * around by logical lines, not physical ones.
    558  1.1  christos 	 *
    559  1.1  christos 	 * XXX
    560  1.1  christos 	 * In the presence of a long line, which has more than a screen
    561  1.1  christos 	 * width of leading spaces, this code can cause a cursor warp.
    562  1.1  christos 	 * Live with it.
    563  1.1  christos 	 */
    564  1.1  christos 	if (scmd != CNTRL_E && scmd != CNTRL_Y &&
    565  1.1  christos 	    rp->cno == 0 && nonblank(sp, rp->lno, &rp->cno))
    566  1.1  christos 		return (1);
    567  1.1  christos 
    568  1.1  christos 	return (0);
    569  1.1  christos }
    570  1.1  christos 
    571  1.1  christos /*
    572  1.1  christos  * vs_sm_up --
    573  1.1  christos  *	Scroll the SMAP up count logical lines.
    574  1.1  christos  */
    575  1.1  christos static int
    576  1.1  christos vs_sm_up(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd, SMAP *smp)
    577  1.1  christos {
    578  1.1  christos 	int cursor_set, echanged, zset;
    579  1.1  christos 	SMAP *ssmp, s1, s2;
    580  1.1  christos 
    581  1.1  christos 	/*
    582  1.1  christos 	 * Check to see if movement is possible.
    583  1.1  christos 	 *
    584  1.1  christos 	 * Get the line after the map.  If that line is a new one (and if
    585  1.1  christos 	 * O_LEFTRIGHT option is set, this has to be true), and the next
    586  1.1  christos 	 * line doesn't exist, and the cursor doesn't move, or the cursor
    587  1.1  christos 	 * isn't even on the screen, or the cursor is already at the last
    588  1.1  christos 	 * line in the map, it's an error.  If that test succeeded because
    589  1.1  christos 	 * the cursor wasn't at the end of the map, test to see if the map
    590  1.1  christos 	 * is mostly empty.
    591  1.1  christos 	 */
    592  1.1  christos 	if (vs_sm_next(sp, TMAP, &s1))
    593  1.1  christos 		return (1);
    594  1.1  christos 	if (s1.lno > TMAP->lno && !db_exist(sp, s1.lno)) {
    595  1.1  christos 		if (scmd == CNTRL_E || scmd == Z_PLUS || smp == TMAP) {
    596  1.1  christos 			v_eof(sp, NULL);
    597  1.1  christos 			return (1);
    598  1.1  christos 		}
    599  1.1  christos 		if (vs_sm_next(sp, smp, &s1))
    600  1.1  christos 			return (1);
    601  1.1  christos 		if (s1.lno > smp->lno && !db_exist(sp, s1.lno)) {
    602  1.1  christos 			v_eof(sp, NULL);
    603  1.1  christos 			return (1);
    604  1.1  christos 		}
    605  1.1  christos 	}
    606  1.1  christos 
    607  1.1  christos 	/*
    608  1.1  christos 	 * Small screens: see vs_refresh.c section 6a.
    609  1.1  christos 	 *
    610  1.1  christos 	 * If it's a small screen, and the movement isn't larger than a
    611  1.1  christos 	 * screen, i.e some context will remain, open up the screen and
    612  1.1  christos 	 * display by scrolling.  In this case, the cursor moves down one
    613  1.1  christos 	 * line for each line displayed.  Otherwise, erase/compress and
    614  1.1  christos 	 * repaint, and move the cursor to the first line in the screen.
    615  1.1  christos 	 * Note, the ^F command is always in the latter case, for historical
    616  1.1  christos 	 * reasons.
    617  1.1  christos 	 */
    618  1.1  christos 	cursor_set = 0;
    619  1.1  christos 	if (IS_SMALL(sp)) {
    620  1.1  christos 		if (count >= sp->t_maxrows || scmd == CNTRL_F) {
    621  1.1  christos 			s1 = TMAP[0];
    622  1.1  christos 			if (vs_sm_erase(sp))
    623  1.1  christos 				return (1);
    624  1.1  christos 			for (; count--; s1 = s2) {
    625  1.1  christos 				if (vs_sm_next(sp, &s1, &s2))
    626  1.1  christos 					return (1);
    627  1.1  christos 				if (s2.lno != s1.lno && !db_exist(sp, s2.lno))
    628  1.1  christos 					break;
    629  1.1  christos 			}
    630  1.1  christos 			TMAP[0] = s2;
    631  1.1  christos 			if (vs_sm_fill(sp, OOBLNO, P_BOTTOM))
    632  1.1  christos 				return (1);
    633  1.1  christos 			return (vs_sm_position(sp, rp, 0, P_TOP));
    634  1.1  christos 		}
    635  1.1  christos 		cursor_set = scmd == CNTRL_E || vs_sm_cursor(sp, &ssmp);
    636  1.1  christos 		for (; count &&
    637  1.1  christos 		    sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
    638  1.1  christos 			if (vs_sm_next(sp, TMAP, &s1))
    639  1.1  christos 				return (1);
    640  1.1  christos 			if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
    641  1.1  christos 				break;
    642  1.1  christos 			*++TMAP = s1;
    643  1.1  christos 			/* vs_sm_next() flushed the cache. */
    644  1.1  christos 			if (vs_line(sp, TMAP, NULL, NULL))
    645  1.1  christos 				return (1);
    646  1.1  christos 
    647  1.1  christos 			if (!cursor_set)
    648  1.1  christos 				++ssmp;
    649  1.1  christos 		}
    650  1.1  christos 		if (!cursor_set) {
    651  1.1  christos 			rp->lno = ssmp->lno;
    652  1.1  christos 			rp->cno = ssmp->c_sboff;
    653  1.1  christos 		}
    654  1.1  christos 		if (count == 0)
    655  1.1  christos 			return (0);
    656  1.1  christos 	}
    657  1.1  christos 
    658  1.1  christos 	for (echanged = zset = 0; count; --count) {
    659  1.1  christos 		/* Decide what would show up on the screen. */
    660  1.1  christos 		if (vs_sm_next(sp, TMAP, &s1))
    661  1.1  christos 			return (1);
    662  1.1  christos 
    663  1.1  christos 		/* If the line doesn't exist, we're done. */
    664  1.1  christos 		if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
    665  1.1  christos 			break;
    666  1.1  christos 
    667  1.1  christos 		/* Scroll the screen cursor up one logical line. */
    668  1.1  christos 		if (vs_sm_1up(sp))
    669  1.1  christos 			return (1);
    670  1.1  christos 		switch (scmd) {
    671  1.1  christos 		case CNTRL_E:
    672  1.1  christos 			if (smp > HMAP)
    673  1.1  christos 				--smp;
    674  1.1  christos 			else
    675  1.1  christos 				echanged = 1;
    676  1.1  christos 			break;
    677  1.1  christos 		case Z_PLUS:
    678  1.1  christos 			if (zset) {
    679  1.1  christos 				if (smp > HMAP)
    680  1.1  christos 					--smp;
    681  1.1  christos 			} else {
    682  1.1  christos 				smp = TMAP;
    683  1.1  christos 				zset = 1;
    684  1.1  christos 			}
    685  1.1  christos 			/* FALLTHROUGH */
    686  1.1  christos 		default:
    687  1.1  christos 			break;
    688  1.1  christos 		}
    689  1.1  christos 	}
    690  1.1  christos 
    691  1.1  christos 	if (cursor_set)
    692  1.1  christos 		return(0);
    693  1.1  christos 
    694  1.1  christos 	switch (scmd) {
    695  1.1  christos 	case CNTRL_E:
    696  1.1  christos 		/*
    697  1.1  christos 		 * On a ^E that was forced to change lines, try and keep the
    698  1.1  christos 		 * cursor as close as possible to the last position, but also
    699  1.1  christos 		 * set it up so that the next "real" movement will return the
    700  1.1  christos 		 * cursor to the closest position to the last real movement.
    701  1.1  christos 		 */
    702  1.1  christos 		if (echanged) {
    703  1.1  christos 			rp->lno = smp->lno;
    704  1.1  christos 			rp->cno = vs_colpos(sp, smp->lno,
    705  1.1  christos 			    (O_ISSET(sp, O_LEFTRIGHT) ?
    706  1.1  christos 			    smp->coff : (smp->soff - 1) * sp->cols) +
    707  1.1  christos 			    sp->rcm % sp->cols);
    708  1.1  christos 		}
    709  1.1  christos 		return (0);
    710  1.1  christos 	case CNTRL_F:
    711  1.1  christos 		/*
    712  1.1  christos 		 * If there are more lines, the ^F command is positioned at
    713  1.1  christos 		 * the first line of the screen.
    714  1.1  christos 		 */
    715  1.1  christos 		if (!count) {
    716  1.1  christos 			smp = HMAP;
    717  1.1  christos 			break;
    718  1.1  christos 		}
    719  1.1  christos 		/* FALLTHROUGH */
    720  1.1  christos 	case CNTRL_D:
    721  1.1  christos 		/*
    722  1.1  christos 		 * The ^D and ^F commands move the cursor towards EOF
    723  1.1  christos 		 * if there are more lines to move.  Check to be sure
    724  1.1  christos 		 * the lines actually exist.  (They may not if the
    725  1.1  christos 		 * file is smaller than the screen.)
    726  1.1  christos 		 */
    727  1.1  christos 		for (; count; --count, ++smp)
    728  1.1  christos 			if (smp == TMAP || !db_exist(sp, smp[1].lno))
    729  1.1  christos 				break;
    730  1.1  christos 		break;
    731  1.1  christos 	case Z_PLUS:
    732  1.1  christos 		 /* The z+ command moves the cursor to the first new line. */
    733  1.1  christos 		break;
    734  1.1  christos 	default:
    735  1.1  christos 		abort();
    736  1.1  christos 	}
    737  1.1  christos 
    738  1.1  christos 	if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
    739  1.1  christos 		return (1);
    740  1.1  christos 	rp->lno = smp->lno;
    741  1.1  christos 	rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
    742  1.1  christos 	return (0);
    743  1.1  christos }
    744  1.1  christos 
    745  1.1  christos /*
    746  1.1  christos  * vs_sm_1up --
    747  1.1  christos  *	Scroll the SMAP up one.
    748  1.1  christos  *
    749  1.1  christos  * PUBLIC: int vs_sm_1up __P((SCR *));
    750  1.1  christos  */
    751  1.1  christos int
    752  1.1  christos vs_sm_1up(SCR *sp)
    753  1.1  christos {
    754  1.1  christos 	/*
    755  1.1  christos 	 * Delete the top line of the screen.  Shift the screen map
    756  1.1  christos 	 * up and display a new line at the bottom of the screen.
    757  1.1  christos 	 */
    758  1.1  christos 	(void)sp->gp->scr_move(sp, 0, 0);
    759  1.1  christos 	if (vs_deleteln(sp, 1))
    760  1.1  christos 		return (1);
    761  1.1  christos 
    762  1.1  christos 	/* One-line screens can fail. */
    763  1.1  christos 	if (IS_ONELINE(sp)) {
    764  1.1  christos 		if (vs_sm_next(sp, TMAP, TMAP))
    765  1.1  christos 			return (1);
    766  1.1  christos 	} else {
    767  1.1  christos 		memmove(HMAP, HMAP + 1, (sp->rows - 1) * sizeof(SMAP));
    768  1.1  christos 		if (vs_sm_next(sp, TMAP - 1, TMAP))
    769  1.1  christos 			return (1);
    770  1.1  christos 	}
    771  1.1  christos 	/* vs_sm_next() flushed the cache. */
    772  1.1  christos 	return (vs_line(sp, TMAP, NULL, NULL));
    773  1.1  christos }
    774  1.1  christos 
    775  1.1  christos /*
    776  1.1  christos  * vs_deleteln --
    777  1.1  christos  *	Delete a line a la curses, make sure to put the information
    778  1.1  christos  *	line and other screens back.
    779  1.1  christos  */
    780  1.1  christos static int
    781  1.1  christos vs_deleteln(SCR *sp, int cnt)
    782  1.1  christos {
    783  1.1  christos 	GS *gp;
    784  1.1  christos 	size_t oldy, oldx;
    785  1.1  christos 
    786  1.1  christos 	gp = sp->gp;
    787  1.1  christos 
    788  1.1  christos 	/* If the screen is vertically split, we can't scroll it. */
    789  1.1  christos 	if (IS_VSPLIT(sp)) {
    790  1.1  christos 		F_SET(sp, SC_SCR_REDRAW);
    791  1.1  christos 		return (0);
    792  1.1  christos 	}
    793  1.1  christos 
    794  1.1  christos 	if (IS_ONELINE(sp))
    795  1.1  christos 		(void)gp->scr_clrtoeol(sp);
    796  1.1  christos 	else {
    797  1.1  christos 		(void)gp->scr_cursor(sp, &oldy, &oldx);
    798  1.1  christos 		while (cnt--) {
    799  1.1  christos 			(void)gp->scr_deleteln(sp);
    800  1.1  christos 			(void)gp->scr_move(sp, LASTLINE(sp), 0);
    801  1.1  christos 			(void)gp->scr_insertln(sp);
    802  1.1  christos 			(void)gp->scr_move(sp, oldy, oldx);
    803  1.1  christos 		}
    804  1.1  christos 	}
    805  1.1  christos 	return (0);
    806  1.1  christos }
    807  1.1  christos 
    808  1.1  christos /*
    809  1.1  christos  * vs_sm_down --
    810  1.1  christos  *	Scroll the SMAP down count logical lines.
    811  1.1  christos  */
    812  1.1  christos static int
    813  1.1  christos vs_sm_down(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd, SMAP *smp)
    814  1.1  christos {
    815  1.1  christos 	SMAP *ssmp, s1, s2;
    816  1.1  christos 	int cursor_set, ychanged, zset;
    817  1.1  christos 
    818  1.1  christos 	/* Check to see if movement is possible. */
    819  1.1  christos 	if (HMAP->lno == 1 &&
    820  1.1  christos 	    (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1) &&
    821  1.1  christos 	    (scmd == CNTRL_Y || scmd == Z_CARAT || smp == HMAP)) {
    822  1.1  christos 		v_sof(sp, NULL);
    823  1.1  christos 		return (1);
    824  1.1  christos 	}
    825  1.1  christos 
    826  1.1  christos 	/*
    827  1.1  christos 	 * Small screens: see vs_refresh.c section 6a.
    828  1.1  christos 	 *
    829  1.1  christos 	 * If it's a small screen, and the movement isn't larger than a
    830  1.1  christos 	 * screen, i.e some context will remain, open up the screen and
    831  1.1  christos 	 * display by scrolling.  In this case, the cursor moves up one
    832  1.1  christos 	 * line for each line displayed.  Otherwise, erase/compress and
    833  1.1  christos 	 * repaint, and move the cursor to the first line in the screen.
    834  1.1  christos 	 * Note, the ^B command is always in the latter case, for historical
    835  1.1  christos 	 * reasons.
    836  1.1  christos 	 */
    837  1.1  christos 	cursor_set = scmd == CNTRL_Y;
    838  1.1  christos 	if (IS_SMALL(sp)) {
    839  1.1  christos 		if (count >= sp->t_maxrows || scmd == CNTRL_B) {
    840  1.1  christos 			s1 = HMAP[0];
    841  1.1  christos 			if (vs_sm_erase(sp))
    842  1.1  christos 				return (1);
    843  1.1  christos 			for (; count--; s1 = s2) {
    844  1.1  christos 				if (vs_sm_prev(sp, &s1, &s2))
    845  1.1  christos 					return (1);
    846  1.1  christos 				if (s2.lno == 1 &&
    847  1.1  christos 				    (O_ISSET(sp, O_LEFTRIGHT) || s2.soff == 1))
    848  1.1  christos 					break;
    849  1.1  christos 			}
    850  1.1  christos 			HMAP[0] = s2;
    851  1.1  christos 			if (vs_sm_fill(sp, OOBLNO, P_TOP))
    852  1.1  christos 				return (1);
    853  1.1  christos 			return (vs_sm_position(sp, rp, 0, P_BOTTOM));
    854  1.1  christos 		}
    855  1.1  christos 		cursor_set = scmd == CNTRL_Y || vs_sm_cursor(sp, &ssmp);
    856  1.1  christos 		for (; count &&
    857  1.1  christos 		    sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
    858  1.1  christos 			if (HMAP->lno == 1 &&
    859  1.1  christos 			    (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
    860  1.1  christos 				break;
    861  1.1  christos 			++TMAP;
    862  1.1  christos 			if (vs_sm_1down(sp))
    863  1.1  christos 				return (1);
    864  1.1  christos 		}
    865  1.1  christos 		if (!cursor_set) {
    866  1.1  christos 			rp->lno = ssmp->lno;
    867  1.1  christos 			rp->cno = ssmp->c_sboff;
    868  1.1  christos 		}
    869  1.1  christos 		if (count == 0)
    870  1.1  christos 			return (0);
    871  1.1  christos 	}
    872  1.1  christos 
    873  1.1  christos 	for (ychanged = zset = 0; count; --count) {
    874  1.1  christos 		/* If the line doesn't exist, we're done. */
    875  1.1  christos 		if (HMAP->lno == 1 &&
    876  1.1  christos 		    (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
    877  1.1  christos 			break;
    878  1.1  christos 
    879  1.1  christos 		/* Scroll the screen and cursor down one logical line. */
    880  1.1  christos 		if (vs_sm_1down(sp))
    881  1.1  christos 			return (1);
    882  1.1  christos 		switch (scmd) {
    883  1.1  christos 		case CNTRL_Y:
    884  1.1  christos 			if (smp < TMAP)
    885  1.1  christos 				++smp;
    886  1.1  christos 			else
    887  1.1  christos 				ychanged = 1;
    888  1.1  christos 			break;
    889  1.1  christos 		case Z_CARAT:
    890  1.1  christos 			if (zset) {
    891  1.1  christos 				if (smp < TMAP)
    892  1.1  christos 					++smp;
    893  1.1  christos 			} else {
    894  1.1  christos 				smp = HMAP;
    895  1.1  christos 				zset = 1;
    896  1.1  christos 			}
    897  1.1  christos 			/* FALLTHROUGH */
    898  1.1  christos 		default:
    899  1.1  christos 			break;
    900  1.1  christos 		}
    901  1.1  christos 	}
    902  1.1  christos 
    903  1.1  christos 	if (scmd != CNTRL_Y && cursor_set)
    904  1.1  christos 		return(0);
    905  1.1  christos 
    906  1.1  christos 	switch (scmd) {
    907  1.1  christos 	case CNTRL_B:
    908  1.1  christos 		/*
    909  1.1  christos 		 * If there are more lines, the ^B command is positioned at
    910  1.1  christos 		 * the last line of the screen.  However, the line may not
    911  1.1  christos 		 * exist.
    912  1.1  christos 		 */
    913  1.1  christos 		if (!count) {
    914  1.1  christos 			for (smp = TMAP; smp > HMAP; --smp)
    915  1.1  christos 				if (db_exist(sp, smp->lno))
    916  1.1  christos 					break;
    917  1.1  christos 			break;
    918  1.1  christos 		}
    919  1.1  christos 		/* FALLTHROUGH */
    920  1.1  christos 	case CNTRL_U:
    921  1.1  christos 		/*
    922  1.1  christos 		 * The ^B and ^U commands move the cursor towards SOF
    923  1.1  christos 		 * if there are more lines to move.
    924  1.1  christos 		 */
    925  1.2  christos 		if (count < (db_recno_t)(smp - HMAP))
    926  1.1  christos 			smp -= count;
    927  1.1  christos 		else
    928  1.1  christos 			smp = HMAP;
    929  1.1  christos 		break;
    930  1.1  christos 	case CNTRL_Y:
    931  1.1  christos 		/*
    932  1.1  christos 		 * On a ^Y that was forced to change lines, try and keep the
    933  1.1  christos 		 * cursor as close as possible to the last position, but also
    934  1.1  christos 		 * set it up so that the next "real" movement will return the
    935  1.1  christos 		 * cursor to the closest position to the last real movement.
    936  1.1  christos 		 */
    937  1.1  christos 		if (ychanged) {
    938  1.1  christos 			rp->lno = smp->lno;
    939  1.1  christos 			rp->cno = vs_colpos(sp, smp->lno,
    940  1.1  christos 			    (O_ISSET(sp, O_LEFTRIGHT) ?
    941  1.1  christos 			    smp->coff : (smp->soff - 1) * sp->cols) +
    942  1.1  christos 			    sp->rcm % sp->cols);
    943  1.1  christos 		}
    944  1.1  christos 		return (0);
    945  1.1  christos 	case Z_CARAT:
    946  1.1  christos 		 /* The z^ command moves the cursor to the first new line. */
    947  1.1  christos 		break;
    948  1.1  christos 	default:
    949  1.1  christos 		abort();
    950  1.1  christos 	}
    951  1.1  christos 
    952  1.1  christos 	if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
    953  1.1  christos 		return (1);
    954  1.1  christos 	rp->lno = smp->lno;
    955  1.1  christos 	rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
    956  1.1  christos 	return (0);
    957  1.1  christos }
    958  1.1  christos 
    959  1.1  christos /*
    960  1.1  christos  * vs_sm_erase --
    961  1.1  christos  *	Erase the small screen area for the scrolling functions.
    962  1.1  christos  */
    963  1.1  christos static int
    964  1.1  christos vs_sm_erase(SCR *sp)
    965  1.1  christos {
    966  1.1  christos 	GS *gp;
    967  1.1  christos 
    968  1.1  christos 	gp = sp->gp;
    969  1.1  christos 	(void)gp->scr_move(sp, LASTLINE(sp), 0);
    970  1.1  christos 	(void)gp->scr_clrtoeol(sp);
    971  1.1  christos 	for (; sp->t_rows > sp->t_minrows; --sp->t_rows, --TMAP) {
    972  1.1  christos 		(void)gp->scr_move(sp, TMAP - HMAP, 0);
    973  1.1  christos 		(void)gp->scr_clrtoeol(sp);
    974  1.1  christos 	}
    975  1.1  christos 	return (0);
    976  1.1  christos }
    977  1.1  christos 
    978  1.1  christos /*
    979  1.1  christos  * vs_sm_1down --
    980  1.1  christos  *	Scroll the SMAP down one.
    981  1.1  christos  *
    982  1.1  christos  * PUBLIC: int vs_sm_1down __P((SCR *));
    983  1.1  christos  */
    984  1.1  christos int
    985  1.1  christos vs_sm_1down(SCR *sp)
    986  1.1  christos {
    987  1.1  christos 	/*
    988  1.1  christos 	 * Insert a line at the top of the screen.  Shift the screen map
    989  1.1  christos 	 * down and display a new line at the top of the screen.
    990  1.1  christos 	 */
    991  1.1  christos 	(void)sp->gp->scr_move(sp, 0, 0);
    992  1.1  christos 	if (vs_insertln(sp, 1))
    993  1.1  christos 		return (1);
    994  1.1  christos 
    995  1.1  christos 	/* One-line screens can fail. */
    996  1.1  christos 	if (IS_ONELINE(sp)) {
    997  1.1  christos 		if (vs_sm_prev(sp, HMAP, HMAP))
    998  1.1  christos 			return (1);
    999  1.1  christos 	} else {
   1000  1.1  christos 		memmove(HMAP + 1, HMAP, (sp->rows - 1) * sizeof(SMAP));
   1001  1.1  christos 		if (vs_sm_prev(sp, HMAP + 1, HMAP))
   1002  1.1  christos 			return (1);
   1003  1.1  christos 	}
   1004  1.1  christos 	/* vs_sm_prev() flushed the cache. */
   1005  1.1  christos 	return (vs_line(sp, HMAP, NULL, NULL));
   1006  1.1  christos }
   1007  1.1  christos 
   1008  1.1  christos /*
   1009  1.1  christos  * vs_insertln --
   1010  1.1  christos  *	Insert a line a la curses, make sure to put the information
   1011  1.1  christos  *	line and other screens back.
   1012  1.1  christos  */
   1013  1.1  christos static int
   1014  1.1  christos vs_insertln(SCR *sp, int cnt)
   1015  1.1  christos {
   1016  1.1  christos 	GS *gp;
   1017  1.1  christos 	size_t oldy, oldx;
   1018  1.1  christos 
   1019  1.1  christos 	gp = sp->gp;
   1020  1.1  christos 
   1021  1.1  christos 	/* If the screen is vertically split, we can't scroll it. */
   1022  1.1  christos 	if (IS_VSPLIT(sp)) {
   1023  1.1  christos 		F_SET(sp, SC_SCR_REDRAW);
   1024  1.1  christos 		return (0);
   1025  1.1  christos 	}
   1026  1.1  christos 
   1027  1.1  christos 	if (IS_ONELINE(sp)) {
   1028  1.1  christos 		(void)gp->scr_move(sp, LASTLINE(sp), 0);
   1029  1.1  christos 		(void)gp->scr_clrtoeol(sp);
   1030  1.1  christos 	} else {
   1031  1.1  christos 		(void)gp->scr_cursor(sp, &oldy, &oldx);
   1032  1.1  christos 		while (cnt--) {
   1033  1.1  christos 			(void)gp->scr_move(sp, LASTLINE(sp) - 1, 0);
   1034  1.1  christos 			(void)gp->scr_deleteln(sp);
   1035  1.1  christos 			(void)gp->scr_move(sp, oldy, oldx);
   1036  1.1  christos 			(void)gp->scr_insertln(sp);
   1037  1.1  christos 		}
   1038  1.1  christos 	}
   1039  1.1  christos 	return (0);
   1040  1.1  christos }
   1041  1.1  christos 
   1042  1.1  christos /*
   1043  1.1  christos  * vs_sm_next --
   1044  1.1  christos  *	Fill in the next entry in the SMAP.
   1045  1.1  christos  *
   1046  1.1  christos  * PUBLIC: int vs_sm_next __P((SCR *, SMAP *, SMAP *));
   1047  1.1  christos  */
   1048  1.1  christos int
   1049  1.1  christos vs_sm_next(SCR *sp, SMAP *p, SMAP *t)
   1050  1.1  christos {
   1051  1.1  christos 	size_t lcnt;
   1052  1.1  christos 
   1053  1.1  christos 	SMAP_FLUSH(t);
   1054  1.1  christos 	if (O_ISSET(sp, O_LEFTRIGHT)) {
   1055  1.1  christos 		t->lno = p->lno + 1;
   1056  1.1  christos 		t->coff = p->coff;
   1057  1.1  christos 	} else {
   1058  1.1  christos 		lcnt = vs_screens(sp, p->lno, NULL);
   1059  1.1  christos 		if (lcnt == p->soff) {
   1060  1.1  christos 			t->lno = p->lno + 1;
   1061  1.1  christos 			t->soff = 1;
   1062  1.1  christos 		} else {
   1063  1.1  christos 			t->lno = p->lno;
   1064  1.1  christos 			t->soff = p->soff + 1;
   1065  1.1  christos 		}
   1066  1.1  christos 	}
   1067  1.1  christos 	return (0);
   1068  1.1  christos }
   1069  1.1  christos 
   1070  1.1  christos /*
   1071  1.1  christos  * vs_sm_prev --
   1072  1.1  christos  *	Fill in the previous entry in the SMAP.
   1073  1.1  christos  *
   1074  1.1  christos  * PUBLIC: int vs_sm_prev __P((SCR *, SMAP *, SMAP *));
   1075  1.1  christos  */
   1076  1.1  christos int
   1077  1.1  christos vs_sm_prev(SCR *sp, SMAP *p, SMAP *t)
   1078  1.1  christos {
   1079  1.1  christos 	SMAP_FLUSH(t);
   1080  1.1  christos 	if (O_ISSET(sp, O_LEFTRIGHT)) {
   1081  1.1  christos 		t->lno = p->lno - 1;
   1082  1.1  christos 		t->coff = p->coff;
   1083  1.1  christos 	} else {
   1084  1.1  christos 		if (p->soff != 1) {
   1085  1.1  christos 			t->lno = p->lno;
   1086  1.1  christos 			t->soff = p->soff - 1;
   1087  1.1  christos 		} else {
   1088  1.1  christos 			t->lno = p->lno - 1;
   1089  1.1  christos 			t->soff = vs_screens(sp, t->lno, NULL);
   1090  1.1  christos 		}
   1091  1.1  christos 	}
   1092  1.1  christos 	return (t->lno == 0);
   1093  1.1  christos }
   1094  1.1  christos 
   1095  1.1  christos /*
   1096  1.1  christos  * vs_sm_cursor --
   1097  1.1  christos  *	Return the SMAP entry referenced by the cursor.
   1098  1.1  christos  *
   1099  1.1  christos  * PUBLIC: int vs_sm_cursor __P((SCR *, SMAP **));
   1100  1.1  christos  */
   1101  1.1  christos int
   1102  1.1  christos vs_sm_cursor(SCR *sp, SMAP **smpp)
   1103  1.1  christos {
   1104  1.1  christos 	SMAP *p;
   1105  1.1  christos 
   1106  1.1  christos 	/* See if the cursor is not in the map. */
   1107  1.1  christos 	if (sp->lno < HMAP->lno || sp->lno > TMAP->lno)
   1108  1.1  christos 		return (1);
   1109  1.1  christos 
   1110  1.1  christos 	/* Find the first occurence of the line. */
   1111  1.1  christos 	for (p = HMAP; p->lno != sp->lno; ++p);
   1112  1.1  christos 
   1113  1.1  christos 	/* Fill in the map information until we find the right line. */
   1114  1.1  christos 	for (; p <= TMAP; ++p) {
   1115  1.1  christos 		/* Short lines are common and easy to detect. */
   1116  1.1  christos 		if (p != TMAP && (p + 1)->lno != p->lno) {
   1117  1.1  christos 			*smpp = p;
   1118  1.1  christos 			return (0);
   1119  1.1  christos 		}
   1120  1.1  christos 		if (!SMAP_CACHE(p) && vs_line(sp, p, NULL, NULL))
   1121  1.1  christos 			return (1);
   1122  1.1  christos 		if (p->c_eboff >= sp->cno) {
   1123  1.1  christos 			*smpp = p;
   1124  1.1  christos 			return (0);
   1125  1.1  christos 		}
   1126  1.1  christos 	}
   1127  1.1  christos 
   1128  1.1  christos 	/* It was past the end of the map after all. */
   1129  1.1  christos 	return (1);
   1130  1.1  christos }
   1131  1.1  christos 
   1132  1.1  christos /*
   1133  1.1  christos  * vs_sm_position --
   1134  1.1  christos  *	Return the line/column of the top, middle or last line on the screen.
   1135  1.1  christos  *	(The vi H, M and L commands.)  Here because only the screen routines
   1136  1.1  christos  *	know what's really out there.
   1137  1.1  christos  *
   1138  1.1  christos  * PUBLIC: int vs_sm_position __P((SCR *, MARK *, u_long, pos_t));
   1139  1.1  christos  */
   1140  1.1  christos int
   1141  1.1  christos vs_sm_position(SCR *sp, MARK *rp, u_long cnt, pos_t pos)
   1142  1.1  christos {
   1143  1.1  christos 	SMAP *smp;
   1144  1.1  christos 	db_recno_t last;
   1145  1.1  christos 
   1146  1.1  christos 	switch (pos) {
   1147  1.1  christos 	case P_TOP:
   1148  1.1  christos 		/*
   1149  1.1  christos 		 * !!!
   1150  1.1  christos 		 * Historically, an invalid count to the H command failed.
   1151  1.1  christos 		 * We do nothing special here, just making sure that H in
   1152  1.1  christos 		 * an empty screen works.
   1153  1.1  christos 		 */
   1154  1.2  christos 		if (cnt > (u_long)(TMAP - HMAP))
   1155  1.1  christos 			goto sof;
   1156  1.1  christos 		smp = HMAP + cnt;
   1157  1.1  christos 		if (cnt && !db_exist(sp, smp->lno)) {
   1158  1.1  christos sof:			msgq(sp, M_BERR, "220|Movement past the end-of-screen");
   1159  1.1  christos 			return (1);
   1160  1.1  christos 		}
   1161  1.1  christos 		break;
   1162  1.1  christos 	case P_MIDDLE:
   1163  1.1  christos 		/*
   1164  1.1  christos 		 * !!!
   1165  1.1  christos 		 * Historically, a count to the M command was ignored.
   1166  1.1  christos 		 * If the screen isn't filled, find the middle of what's
   1167  1.1  christos 		 * real and move there.
   1168  1.1  christos 		 */
   1169  1.1  christos 		if (!db_exist(sp, TMAP->lno)) {
   1170  1.1  christos 			if (db_last(sp, &last))
   1171  1.1  christos 				return (1);
   1172  1.1  christos 			for (smp = TMAP; smp->lno > last && smp > HMAP; --smp);
   1173  1.1  christos 			if (smp > HMAP)
   1174  1.1  christos 				smp -= (smp - HMAP) / 2;
   1175  1.1  christos 		} else
   1176  1.1  christos 			smp = (HMAP + (TMAP - HMAP) / 2) + cnt;
   1177  1.1  christos 		break;
   1178  1.1  christos 	case P_BOTTOM:
   1179  1.1  christos 		/*
   1180  1.1  christos 		 * !!!
   1181  1.1  christos 		 * Historically, an invalid count to the L command failed.
   1182  1.1  christos 		 * If the screen isn't filled, find the bottom of what's
   1183  1.1  christos 		 * real and try to offset from there.
   1184  1.1  christos 		 */
   1185  1.2  christos 		if (cnt > (u_long)(TMAP - HMAP))
   1186  1.1  christos 			goto eof;
   1187  1.1  christos 		smp = TMAP - cnt;
   1188  1.1  christos 		if (!db_exist(sp, smp->lno)) {
   1189  1.1  christos 			if (db_last(sp, &last))
   1190  1.1  christos 				return (1);
   1191  1.1  christos 			for (; smp->lno > last && smp > HMAP; --smp);
   1192  1.2  christos 			if (cnt > (u_long)(smp - HMAP)) {
   1193  1.1  christos eof:				msgq(sp, M_BERR,
   1194  1.1  christos 			    "221|Movement past the beginning-of-screen");
   1195  1.1  christos 				return (1);
   1196  1.1  christos 			}
   1197  1.1  christos 			smp -= cnt;
   1198  1.1  christos 		}
   1199  1.1  christos 		break;
   1200  1.1  christos 	default:
   1201  1.1  christos 		abort();
   1202  1.1  christos 	}
   1203  1.1  christos 
   1204  1.1  christos 	/* Make sure that the cached information is valid. */
   1205  1.1  christos 	if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
   1206  1.1  christos 		return (1);
   1207  1.1  christos 	rp->lno = smp->lno;
   1208  1.1  christos 	rp->cno = smp->c_sboff;
   1209  1.1  christos 
   1210  1.1  christos 	return (0);
   1211  1.1  christos }
   1212  1.1  christos 
   1213  1.1  christos /*
   1214  1.1  christos  * vs_sm_nlines --
   1215  1.1  christos  *	Return the number of screen lines from an SMAP entry to the
   1216  1.1  christos  *	start of some file line, less than a maximum value.
   1217  1.1  christos  *
   1218  1.1  christos  * PUBLIC: db_recno_t vs_sm_nlines __P((SCR *, SMAP *, db_recno_t, size_t));
   1219  1.1  christos  */
   1220  1.1  christos db_recno_t
   1221  1.1  christos vs_sm_nlines(SCR *sp, SMAP *from_sp, db_recno_t to_lno, size_t max)
   1222  1.1  christos {
   1223  1.1  christos 	db_recno_t lno, lcnt;
   1224  1.1  christos 
   1225  1.1  christos 	if (O_ISSET(sp, O_LEFTRIGHT))
   1226  1.1  christos 		return (from_sp->lno > to_lno ?
   1227  1.1  christos 		    from_sp->lno - to_lno : to_lno - from_sp->lno);
   1228  1.1  christos 
   1229  1.1  christos 	if (from_sp->lno == to_lno)
   1230  1.1  christos 		return (from_sp->soff - 1);
   1231  1.1  christos 
   1232  1.1  christos 	if (from_sp->lno > to_lno) {
   1233  1.1  christos 		lcnt = from_sp->soff - 1;	/* Correct for off-by-one. */
   1234  1.1  christos 		for (lno = from_sp->lno; --lno >= to_lno && lcnt <= max;)
   1235  1.1  christos 			lcnt += vs_screens(sp, lno, NULL);
   1236  1.1  christos 	} else {
   1237  1.1  christos 		lno = from_sp->lno;
   1238  1.1  christos 		lcnt = (vs_screens(sp, lno, NULL) - from_sp->soff) + 1;
   1239  1.1  christos 		for (; ++lno < to_lno && lcnt <= max;)
   1240  1.1  christos 			lcnt += vs_screens(sp, lno, NULL);
   1241  1.1  christos 	}
   1242  1.1  christos 	return (lcnt);
   1243  1.1  christos }
   1244