Home | History | Annotate | Line # | Download | only in ksh
vi.c revision 1.2
      1  1.2  tls /*	$NetBSD: vi.c,v 1.2 1997/01/12 19:12:28 tls Exp $	*/
      2  1.2  tls 
      3  1.1  jtc /*
      4  1.1  jtc  *	vi command editing
      5  1.1  jtc  *	written by John Rochester (initially for nsh)
      6  1.1  jtc  *	bludgeoned to fit pdksh by Larry Bouzane, Jeff Sparkes & Eric Gisin
      7  1.1  jtc  *
      8  1.1  jtc  */
      9  1.1  jtc #include "config.h"
     10  1.1  jtc #ifdef VI
     11  1.1  jtc 
     12  1.1  jtc #include "sh.h"
     13  1.1  jtc #include <ctype.h>
     14  1.1  jtc #include "ksh_stat.h"		/* completion */
     15  1.1  jtc #include "edit.h"
     16  1.1  jtc 
     17  1.1  jtc #define CMDLEN		1024
     18  1.1  jtc #define Ctrl(c)		(c&0x1f)
     19  1.1  jtc #define	is_wordch(c)	(letnum(c))
     20  1.1  jtc 
     21  1.1  jtc struct edstate {
     22  1.1  jtc 	int	winleft;
     23  1.1  jtc 	char	*cbuf;
     24  1.1  jtc 	int	cbufsize;
     25  1.1  jtc 	int	linelen;
     26  1.1  jtc 	int	cursor;
     27  1.1  jtc };
     28  1.1  jtc 
     29  1.1  jtc 
     30  1.1  jtc static int	vi_hook	ARGS((int ch));
     31  1.1  jtc static void 	vi_reset ARGS((char *buf, size_t len));
     32  1.1  jtc static int	nextstate ARGS((int ch));
     33  1.1  jtc static int	vi_insert ARGS((int ch));
     34  1.1  jtc static int	vi_cmd ARGS((int argcnt, const char *cmd));
     35  1.1  jtc static int	domove ARGS((int argcnt, const char *cmd, int sub));
     36  1.1  jtc static int	redo_insert ARGS((int count));
     37  1.1  jtc static void	yank_range ARGS((int a, int b));
     38  1.1  jtc static int	bracktype ARGS((int ch));
     39  1.1  jtc static void	save_cbuf ARGS((void));
     40  1.1  jtc static void	restore_cbuf ARGS((void));
     41  1.1  jtc static void	edit_reset ARGS((char *buf, size_t len));
     42  1.1  jtc static int	putbuf ARGS((const char *buf, int len, int repl));
     43  1.1  jtc static void	del_range ARGS((int a, int b));
     44  1.1  jtc static int	findch ARGS((int ch, int cnt, int forw, int incl));
     45  1.1  jtc static int	forwword ARGS((int argcnt));
     46  1.1  jtc static int	backword ARGS((int argcnt));
     47  1.1  jtc static int	endword ARGS((int argcnt));
     48  1.1  jtc static int	Forwword ARGS((int argcnt));
     49  1.1  jtc static int	Backword ARGS((int argcnt));
     50  1.1  jtc static int	Endword ARGS((int argcnt));
     51  1.1  jtc static int	grabhist ARGS((int save, int n));
     52  1.1  jtc static int	grabsearch ARGS((int save, int start, int fwd, char *pat));
     53  1.1  jtc static void	redraw_line ARGS((int newline));
     54  1.1  jtc static void	refresh ARGS((int leftside));
     55  1.1  jtc static int	outofwin ARGS((void));
     56  1.1  jtc static void	rewindow ARGS((void));
     57  1.1  jtc static int	newcol ARGS((int ch, int col));
     58  1.1  jtc static void	display ARGS((char *wb1, char *wb2, int leftside));
     59  1.1  jtc static void	ed_mov_opt ARGS((int col, char *wb));
     60  1.1  jtc static int	expand_word ARGS((int command));
     61  1.1  jtc static int	complete_word ARGS((int command, int count));
     62  1.1  jtc static int	print_expansions ARGS((struct edstate *e, int command));
     63  1.1  jtc static int 	char_len ARGS((int c));
     64  1.1  jtc static void 	x_vi_zotc ARGS((int c));
     65  1.1  jtc static void	vi_pprompt ARGS((int full));
     66  1.1  jtc static void	vi_error ARGS((void));
     67  1.1  jtc static void	vi_macro_reset ARGS((void));
     68  1.1  jtc 
     69  1.1  jtc #define C_	0x1		/* a valid command that isn't a M_, E_, U_ */
     70  1.1  jtc #define M_	0x2		/* movement command (h, l, etc.) */
     71  1.1  jtc #define E_	0x4		/* extended command (c, d, y) */
     72  1.1  jtc #define X_	0x8		/* long command (@, f, F, t, T, etc.) */
     73  1.1  jtc #define U_	0x10		/* an UN-undoable command (that isn't a M_) */
     74  1.1  jtc #define B_	0x20		/* bad command (^@) */
     75  1.1  jtc #define Z_	0x40		/* repeat count defaults to 0 (not 1) */
     76  1.1  jtc #define S_	0x80		/* search (/, ?) */
     77  1.1  jtc 
     78  1.1  jtc #define is_bad(c)	(classify[(c)&0x7f]&B_)
     79  1.1  jtc #define is_cmd(c)	(classify[(c)&0x7f]&(M_|E_|C_|U_))
     80  1.1  jtc #define is_move(c)	(classify[(c)&0x7f]&M_)
     81  1.1  jtc #define is_extend(c)	(classify[(c)&0x7f]&E_)
     82  1.1  jtc #define is_long(c)	(classify[(c)&0x7f]&X_)
     83  1.1  jtc #define is_undoable(c)	(!(classify[(c)&0x7f]&U_))
     84  1.1  jtc #define is_srch(c)	(classify[(c)&0x7f]&S_)
     85  1.1  jtc #define is_zerocount(c)	(classify[(c)&0x7f]&Z_)
     86  1.1  jtc 
     87  1.1  jtc const unsigned char	classify[128] = {
     88  1.1  jtc    /*       0       1       2       3       4       5       6       7        */
     89  1.1  jtc    /*   0   ^@     ^A      ^B      ^C      ^D      ^E      ^F      ^G        */
     90  1.1  jtc 	    B_,     0,      0,      0,      0,      C_|U_,  C_|Z_,  0,
     91  1.1  jtc    /*  01   ^H     ^I      ^J      ^K      ^L      ^M      ^N      ^O        */
     92  1.1  jtc 	    M_,     C_|Z_,  0,      0,      C_|U_,  0,      C_,     0,
     93  1.1  jtc    /*  02   ^P     ^Q      ^R      ^S      ^T      ^U      ^V      ^W        */
     94  1.1  jtc 	    C_,     0,      C_|U_,  0,      0,      0,      C_,     0,
     95  1.1  jtc    /*  03   ^X     ^Y      ^Z      ^[      ^\      ^]      ^^      ^_        */
     96  1.1  jtc 	    C_,     0,      0,      C_|Z_,  0,      0,      0,      0,
     97  1.1  jtc    /*  04  <space>  !       "       #       $       %       &       '        */
     98  1.1  jtc 	    M_,     0,      0,      C_,     M_,     M_,     0,      0,
     99  1.1  jtc    /*  05   (       )       *       +       ,       -       .       /        */
    100  1.1  jtc 	    0,      0,      C_,     C_,     M_,     C_,     0,      C_|S_,
    101  1.1  jtc    /*  06   0       1       2       3       4       5       6       7        */
    102  1.1  jtc 	    M_,     0,      0,      0,      0,      0,      0,      0,
    103  1.1  jtc    /*  07   8       9       :       ;       <       =       >       ?        */
    104  1.1  jtc 	    0,      0,      0,      M_,     0,      C_,     0,      C_|S_,
    105  1.1  jtc    /* 010   @       A       B       C       D       E       F       G        */
    106  1.1  jtc 	    C_|X_,  C_,     M_,     C_,     C_,     M_,     M_|X_,  C_|U_|Z_,
    107  1.1  jtc    /* 011   H       I       J       K       L       M       N       O        */
    108  1.1  jtc 	    0,      C_,     0,      0,      0,      0,      C_|U_,  0,
    109  1.1  jtc    /* 012   P       Q       R       S       T       U       V       W        */
    110  1.1  jtc 	    C_,     0,      C_,     C_,     M_|X_,  C_,     0,      M_,
    111  1.1  jtc    /* 013   X       Y       Z       [       \       ]       ^       _        */
    112  1.1  jtc 	    C_,     C_|U_,  0,      0,      C_|Z_,  0,      M_,     C_|Z_,
    113  1.1  jtc    /* 014   `       a       b       c       d       e       f       g        */
    114  1.1  jtc 	    0,      C_,     M_,     E_,     E_,     M_,     M_|X_,  C_|Z_,
    115  1.1  jtc    /* 015   h       i       j       k       l       m       n       o        */
    116  1.1  jtc 	    M_,     C_,     C_|U_,  C_|U_,  M_,     0,      C_|U_,  0,
    117  1.1  jtc    /* 016   p       q       r       s       t       u       v       w        */
    118  1.1  jtc 	    C_,     0,      X_,     C_,     M_|X_,  C_|U_,  C_|U_|Z_,M_,
    119  1.1  jtc    /* 017   x       y       z       {       |       }       ~      ^?        */
    120  1.1  jtc 	    C_,     E_|U_,  0,      0,      M_|Z_,  0,      C_,     0
    121  1.1  jtc };
    122  1.1  jtc 
    123  1.1  jtc #define MAXVICMD	3
    124  1.1  jtc #define SRCHLEN		40
    125  1.1  jtc 
    126  1.1  jtc #define INSERT		1
    127  1.1  jtc #define REPLACE		2
    128  1.1  jtc 
    129  1.1  jtc #define VNORMAL		0		/* command, insert or replace mode */
    130  1.1  jtc #define VARG1		1		/* digit prefix (first, eg, 5l) */
    131  1.1  jtc #define VEXTCMD		2		/* cmd + movement (eg, cl) */
    132  1.1  jtc #define VARG2		3		/* digit prefix (second, eg, 2c3l) */
    133  1.1  jtc #define VXCH		4		/* f, F, t, T, @ */
    134  1.1  jtc #define VFAIL		5		/* bad command */
    135  1.1  jtc #define VCMD		6		/* single char command (eg, X) */
    136  1.1  jtc #define VREDO		7		/* . */
    137  1.1  jtc #define VLIT		8		/* ^V */
    138  1.1  jtc #define VSEARCH		9		/* /, ? */
    139  1.1  jtc #define VVERSION	10		/* <ESC> ^V */
    140  1.1  jtc 
    141  1.1  jtc static char		undocbuf[CMDLEN];
    142  1.1  jtc 
    143  1.1  jtc static struct edstate 	*save_edstate ARGS((struct edstate *old));
    144  1.1  jtc static void		restore_edstate ARGS((struct edstate *old, struct edstate *new));
    145  1.1  jtc static void 		free_edstate ARGS((struct edstate *old));
    146  1.1  jtc 
    147  1.1  jtc static struct edstate	ebuf;
    148  1.1  jtc static struct edstate	undobuf = { 0, undocbuf, CMDLEN, 0, 0 };
    149  1.1  jtc 
    150  1.1  jtc static struct edstate	*es;			/* current editor state */
    151  1.1  jtc static struct edstate	*undo;
    152  1.1  jtc 
    153  1.1  jtc static char	ibuf[CMDLEN];		/* input buffer */
    154  1.1  jtc static int	first_insert;		/* set when starting in insert mode */
    155  1.1  jtc static int	saved_inslen;		/* saved inslen for first insert */
    156  1.1  jtc static int	inslen;			/* length of input buffer */
    157  1.1  jtc static int	srchlen;		/* length of current search pattern */
    158  1.1  jtc static char	ybuf[CMDLEN];		/* yank buffer */
    159  1.1  jtc static int	yanklen;		/* length of yank buffer */
    160  1.1  jtc static int	fsavecmd = ' ';		/* last find command */
    161  1.1  jtc static int	fsavech;		/* character to find */
    162  1.1  jtc static char	lastcmd[MAXVICMD];	/* last non-move command */
    163  1.1  jtc static int	lastac;			/* argcnt for lastcmd */
    164  1.1  jtc static int	lastsearch = ' ';	/* last search command */
    165  1.1  jtc static char	srchpat[SRCHLEN];	/* last search pattern */
    166  1.1  jtc static int	insert;			/* non-zero in insert mode */
    167  1.1  jtc static int	hnum;			/* position in history */
    168  1.1  jtc static int	ohnum;			/* history line copied (after mod) */
    169  1.1  jtc static int	hlast;			/* 1 past last position in history */
    170  1.1  jtc static int	modified;		/* buffer has been "modified" */
    171  1.1  jtc static int	state;
    172  1.1  jtc 
    173  1.1  jtc /* Information for keeping track of macros that are being expanded.
    174  1.1  jtc  * The format of buf is the alias contents followed by a null byte followed
    175  1.1  jtc  * by the name (letter) of the alias.  The end of the buffer is marked by
    176  1.1  jtc  * a double null.  The name of the alias is stored so recursive macros can
    177  1.1  jtc  * be detected.
    178  1.1  jtc  */
    179  1.1  jtc struct macro_state {
    180  1.1  jtc     unsigned char	*p;	/* current position in buf */
    181  1.1  jtc     unsigned char	*buf;	/* pointer to macro(s) being expanded */
    182  1.1  jtc     int			len;	/* how much data in buffer */
    183  1.1  jtc };
    184  1.1  jtc static struct macro_state macro;
    185  1.1  jtc 
    186  1.1  jtc enum expand_mode { NONE, EXPAND, COMPLETE, PRINT };
    187  1.1  jtc static enum expand_mode expanded = NONE;/* last input was expanded */
    188  1.1  jtc 
    189  1.1  jtc int
    190  1.1  jtc x_vi(buf, len)
    191  1.1  jtc 	char	*buf;
    192  1.1  jtc 	size_t	len;
    193  1.1  jtc {
    194  1.1  jtc 	int	c;
    195  1.1  jtc 
    196  1.1  jtc 	vi_reset(buf, len > CMDLEN ? CMDLEN : len);
    197  1.1  jtc 	vi_pprompt(1);
    198  1.1  jtc 	x_flush();
    199  1.1  jtc 	while (1) {
    200  1.1  jtc 		if (macro.p) {
    201  1.1  jtc 			c = *macro.p++;
    202  1.1  jtc 			/* end of current macro? */
    203  1.1  jtc 			if (!c) {
    204  1.1  jtc 				/* more macros left to finish? */
    205  1.1  jtc 				if (*macro.p++)
    206  1.1  jtc 					continue;
    207  1.1  jtc 				/* must be the end of all the macros */
    208  1.1  jtc 				vi_macro_reset();
    209  1.1  jtc 				c = x_getc();
    210  1.1  jtc 			}
    211  1.1  jtc 		} else
    212  1.1  jtc 			c = x_getc();
    213  1.1  jtc 		if (c == -1)
    214  1.1  jtc 			break;
    215  1.1  jtc 		if (state != VLIT) {
    216  1.1  jtc 			if (c == edchars.intr || c == edchars.quit) {
    217  1.1  jtc 				/* pretend we got an interrupt */
    218  1.1  jtc 				x_vi_zotc(c);
    219  1.1  jtc 				x_flush();
    220  1.1  jtc 				trapsig(c == edchars.intr ? SIGINT : SIGQUIT);
    221  1.1  jtc 				x_mode(FALSE);
    222  1.1  jtc 				unwind(LSHELL);
    223  1.1  jtc 			} else if (c == edchars.eof && state != VVERSION) {
    224  1.1  jtc 				if (es->linelen == 0) {
    225  1.1  jtc 					x_vi_zotc(edchars.eof);
    226  1.1  jtc 					c = -1;
    227  1.1  jtc 					break;
    228  1.1  jtc 				}
    229  1.1  jtc 				continue;
    230  1.1  jtc 			}
    231  1.1  jtc 		}
    232  1.1  jtc 		if (vi_hook(c))
    233  1.1  jtc 			break;
    234  1.1  jtc 		x_flush();
    235  1.1  jtc 	}
    236  1.1  jtc 
    237  1.1  jtc 	x_putc('\r'); x_putc('\n'); x_flush();
    238  1.1  jtc 
    239  1.1  jtc 	if (c == -1)
    240  1.1  jtc 		return -1;
    241  1.1  jtc 
    242  1.1  jtc 	if (es->cbuf != buf)
    243  1.1  jtc 		memmove(buf, es->cbuf, es->linelen);
    244  1.1  jtc 
    245  1.1  jtc 	buf[es->linelen++] = '\n';
    246  1.1  jtc 
    247  1.1  jtc 	return es->linelen;
    248  1.1  jtc }
    249  1.1  jtc 
    250  1.1  jtc static int
    251  1.1  jtc vi_hook(ch)
    252  1.1  jtc 	int		ch;
    253  1.1  jtc {
    254  1.1  jtc 	static char	curcmd[MAXVICMD];
    255  1.1  jtc 	static char	locpat[SRCHLEN];
    256  1.1  jtc 	static int	cmdlen;
    257  1.1  jtc 	static int	argc1, argc2;
    258  1.1  jtc 
    259  1.1  jtc 	switch (state) {
    260  1.1  jtc 
    261  1.1  jtc 	case VNORMAL:
    262  1.1  jtc 		if (insert != 0) {
    263  1.1  jtc 			if (ch == Ctrl('v')) {
    264  1.1  jtc 				state = VLIT;
    265  1.1  jtc 				ch = '^';
    266  1.1  jtc 			}
    267  1.1  jtc 			switch (vi_insert(ch)) {
    268  1.1  jtc 			case -1:
    269  1.1  jtc #ifdef OS2
    270  1.1  jtc 				/* Arrow keys generate 0xe0X, where X is H.. */
    271  1.1  jtc 				state = VCMD;
    272  1.1  jtc 				argc1 = 1;
    273  1.1  jtc 				switch (x_getc()) {
    274  1.1  jtc 				  case 'H':
    275  1.1  jtc 					*curcmd='k';
    276  1.1  jtc 					break;
    277  1.1  jtc 				  case 'K':
    278  1.1  jtc 					*curcmd='h';
    279  1.1  jtc 					break;
    280  1.1  jtc 				  case 'P':
    281  1.1  jtc 					*curcmd='j';
    282  1.1  jtc 					break;
    283  1.1  jtc 				  case 'M':
    284  1.1  jtc 					*curcmd='l';
    285  1.1  jtc 					break;
    286  1.1  jtc 				  default:
    287  1.1  jtc 					vi_error();
    288  1.1  jtc 					state = VNORMAL;
    289  1.1  jtc 				}
    290  1.1  jtc 				break;
    291  1.1  jtc #else /* OS2 */
    292  1.1  jtc 				vi_error();
    293  1.1  jtc 				state = VNORMAL;
    294  1.1  jtc #endif /* OS2 */
    295  1.1  jtc 				break;
    296  1.1  jtc 			case 0:
    297  1.1  jtc 				if (state == VLIT) {
    298  1.1  jtc 					es->cursor--;
    299  1.1  jtc 					refresh(0);
    300  1.1  jtc 				} else
    301  1.1  jtc 					refresh(insert != 0);
    302  1.1  jtc 				break;
    303  1.1  jtc 			case 1:
    304  1.1  jtc 				return 1;
    305  1.1  jtc 			}
    306  1.1  jtc 		} else {
    307  1.1  jtc 			if (ch == '\r' || ch == '\n')
    308  1.1  jtc 				return 1;
    309  1.1  jtc 			cmdlen = 0;
    310  1.1  jtc 			argc1 = 0;
    311  1.1  jtc 			if (ch >= '1' && ch <= '9') {
    312  1.1  jtc 				argc1 = ch - '0';
    313  1.1  jtc 				state = VARG1;
    314  1.1  jtc 			} else {
    315  1.1  jtc 				curcmd[cmdlen++] = ch;
    316  1.1  jtc 				state = nextstate(ch);
    317  1.1  jtc 				if (state == VSEARCH) {
    318  1.1  jtc 					save_cbuf();
    319  1.1  jtc 					es->cursor = 0;
    320  1.1  jtc 					es->linelen = 0;
    321  1.1  jtc 					if (ch == '/') {
    322  1.1  jtc 						if (putbuf("/", 1, 0) != 0) {
    323  1.1  jtc 							return -1;
    324  1.1  jtc 						}
    325  1.1  jtc 					} else if (putbuf("?", 1, 0) != 0)
    326  1.1  jtc 							return -1;
    327  1.1  jtc 					refresh(0);
    328  1.1  jtc 				}
    329  1.1  jtc 				if (state == VVERSION) {
    330  1.1  jtc 					save_cbuf();
    331  1.1  jtc 					es->cursor = 0;
    332  1.1  jtc 					es->linelen = 0;
    333  1.1  jtc 					putbuf(ksh_version + 4,
    334  1.1  jtc 						strlen(ksh_version + 4), 0);
    335  1.1  jtc 					refresh(0);
    336  1.1  jtc 				}
    337  1.1  jtc 			}
    338  1.1  jtc 		}
    339  1.1  jtc 		break;
    340  1.1  jtc 
    341  1.1  jtc 	case VLIT:
    342  1.1  jtc 		if (is_bad(ch)) {
    343  1.1  jtc 			del_range(es->cursor, es->cursor + 1);
    344  1.1  jtc 			vi_error();
    345  1.1  jtc 		} else
    346  1.1  jtc 			es->cbuf[es->cursor++] = ch;
    347  1.1  jtc 		refresh(1);
    348  1.1  jtc 		state = VNORMAL;
    349  1.1  jtc 		break;
    350  1.1  jtc 
    351  1.1  jtc 	case VVERSION:
    352  1.1  jtc 		restore_cbuf();
    353  1.1  jtc 		state = VNORMAL;
    354  1.1  jtc 		refresh(0);
    355  1.1  jtc 		break;
    356  1.1  jtc 
    357  1.1  jtc 	case VARG1:
    358  1.1  jtc 		if (isdigit(ch))
    359  1.1  jtc 			argc1 = argc1 * 10 + ch - '0';
    360  1.1  jtc 		else {
    361  1.1  jtc 			curcmd[cmdlen++] = ch;
    362  1.1  jtc 			state = nextstate(ch);
    363  1.1  jtc 		}
    364  1.1  jtc 		break;
    365  1.1  jtc 
    366  1.1  jtc 	case VEXTCMD:
    367  1.1  jtc 		argc2 = 0;
    368  1.1  jtc 		if (ch >= '1' && ch <= '9') {
    369  1.1  jtc 			argc2 = ch - '0';
    370  1.1  jtc 			state = VARG2;
    371  1.1  jtc 			return 0;
    372  1.1  jtc 		} else {
    373  1.1  jtc 			curcmd[cmdlen++] = ch;
    374  1.1  jtc 			if (ch == curcmd[0])
    375  1.1  jtc 				state = VCMD;
    376  1.1  jtc 			else if (is_move(ch))
    377  1.1  jtc 				state = nextstate(ch);
    378  1.1  jtc 			else
    379  1.1  jtc 				state = VFAIL;
    380  1.1  jtc 		}
    381  1.1  jtc 		break;
    382  1.1  jtc 
    383  1.1  jtc 	case VARG2:
    384  1.1  jtc 		if (isdigit(ch))
    385  1.1  jtc 			argc2 = argc2 * 10 + ch - '0';
    386  1.1  jtc 		else {
    387  1.1  jtc 			if (argc1 == 0)
    388  1.1  jtc 				argc1 = argc2;
    389  1.1  jtc 			else
    390  1.1  jtc 				argc1 *= argc2;
    391  1.1  jtc 			curcmd[cmdlen++] = ch;
    392  1.1  jtc 			if (ch == curcmd[0])
    393  1.1  jtc 				state = VCMD;
    394  1.1  jtc 			else if (is_move(ch))
    395  1.1  jtc 				state = nextstate(ch);
    396  1.1  jtc 			else
    397  1.1  jtc 				state = VFAIL;
    398  1.1  jtc 		}
    399  1.1  jtc 		break;
    400  1.1  jtc 
    401  1.1  jtc 	case VXCH:
    402  1.1  jtc 		if (ch == Ctrl('['))
    403  1.1  jtc 			state = VNORMAL;
    404  1.1  jtc 		else {
    405  1.1  jtc 			curcmd[cmdlen++] = ch;
    406  1.1  jtc 			state = VCMD;
    407  1.1  jtc 		}
    408  1.1  jtc 		break;
    409  1.1  jtc 
    410  1.1  jtc 	case VSEARCH:
    411  1.1  jtc 		if (ch == '\r' || ch == '\n' /*|| ch == Ctrl('[')*/ ) {
    412  1.1  jtc 			restore_cbuf();
    413  1.1  jtc 			/* Repeat last search? */
    414  1.1  jtc 			if (srchlen == 0) {
    415  1.1  jtc 				if (!srchpat[0]) {
    416  1.1  jtc 					vi_error();
    417  1.1  jtc 					state = VNORMAL;
    418  1.1  jtc 					refresh(0);
    419  1.1  jtc 					return 0;
    420  1.1  jtc 				}
    421  1.1  jtc 			} else {
    422  1.1  jtc 				locpat[srchlen] = '\0';
    423  1.1  jtc 				(void) strcpy(srchpat, locpat);
    424  1.1  jtc 			}
    425  1.1  jtc 			state = VCMD;
    426  1.1  jtc 		} else if (ch == edchars.erase || ch == Ctrl('h')) {
    427  1.1  jtc 			if (srchlen != 0) {
    428  1.1  jtc 				srchlen--;
    429  1.1  jtc 				es->linelen -= char_len((unsigned char) locpat[srchlen]);
    430  1.1  jtc 				es->cursor = es->linelen;
    431  1.1  jtc 				refresh(0);
    432  1.1  jtc 				return 0;
    433  1.1  jtc 			}
    434  1.1  jtc 			restore_cbuf();
    435  1.1  jtc 			state = VNORMAL;
    436  1.1  jtc 			refresh(0);
    437  1.1  jtc 		} else if (ch == edchars.kill) {
    438  1.1  jtc 			srchlen = 0;
    439  1.1  jtc 			es->linelen = 1;
    440  1.1  jtc 			es->cursor = 1;
    441  1.1  jtc 			refresh(0);
    442  1.1  jtc 			return 0;
    443  1.1  jtc 		} else if (ch == edchars.werase) {
    444  1.1  jtc 			int i;
    445  1.1  jtc 			int n = srchlen;
    446  1.1  jtc 
    447  1.1  jtc 			while (n > 0 && isspace(locpat[n - 1]))
    448  1.1  jtc 				n--;
    449  1.1  jtc 			while (n > 0 && !isspace(locpat[n - 1]))
    450  1.1  jtc 				n--;
    451  1.1  jtc 			for (i = srchlen; --i >= n; )
    452  1.1  jtc 				es->linelen -= char_len((unsigned char) locpat[i]);
    453  1.1  jtc 			srchlen = n;
    454  1.1  jtc 			es->cursor = es->linelen;
    455  1.1  jtc 			refresh(0);
    456  1.1  jtc 			return 0;
    457  1.1  jtc 		} else {
    458  1.1  jtc 			if (srchlen == SRCHLEN - 1)
    459  1.1  jtc 				vi_error();
    460  1.1  jtc 			else {
    461  1.1  jtc 				locpat[srchlen++] = ch;
    462  1.1  jtc 				if ((ch & 0x80) && Flag(FVISHOW8)) {
    463  1.1  jtc 					es->cbuf[es->linelen++] = 'M';
    464  1.1  jtc 					es->cbuf[es->linelen++] = '-';
    465  1.1  jtc 					ch &= 0x7f;
    466  1.1  jtc 				}
    467  1.1  jtc 				if (ch < ' ' || ch == 0x7f) {
    468  1.1  jtc 					es->cbuf[es->linelen++] = '^';
    469  1.1  jtc 					es->cbuf[es->linelen++] = ch ^ '@';
    470  1.1  jtc 				} else
    471  1.1  jtc 					es->cbuf[es->linelen++] = ch;
    472  1.1  jtc 				es->cursor = es->linelen;
    473  1.1  jtc 				refresh(0);
    474  1.1  jtc 			}
    475  1.1  jtc 			return 0;
    476  1.1  jtc 		}
    477  1.1  jtc 		break;
    478  1.1  jtc 	}
    479  1.1  jtc 
    480  1.1  jtc 	switch (state) {
    481  1.1  jtc 	case VCMD:
    482  1.1  jtc 		state = VNORMAL;
    483  1.1  jtc 		switch (vi_cmd(argc1, curcmd)) {
    484  1.1  jtc 		case -1:
    485  1.1  jtc 			vi_error();
    486  1.1  jtc 			refresh(0);
    487  1.1  jtc 			break;
    488  1.1  jtc 		case 0:
    489  1.1  jtc 			if (insert != 0)
    490  1.1  jtc 				inslen = 0;
    491  1.1  jtc 			refresh(insert != 0);
    492  1.1  jtc 			break;
    493  1.1  jtc 		case 1:
    494  1.1  jtc 			refresh(0);
    495  1.1  jtc 			return 1;
    496  1.1  jtc 		case 2:
    497  1.1  jtc 			/* back from a 'v' command - don't redraw the screen */
    498  1.1  jtc 			return 1;
    499  1.1  jtc 		}
    500  1.1  jtc 		break;
    501  1.1  jtc 
    502  1.1  jtc 	case VREDO:
    503  1.1  jtc 		state = VNORMAL;
    504  1.1  jtc 		if (argc1 != 0)
    505  1.1  jtc 			lastac = argc1;
    506  1.1  jtc 		switch (vi_cmd(lastac, lastcmd) != 0) {
    507  1.1  jtc 		case -1:
    508  1.1  jtc 			vi_error();
    509  1.1  jtc 			refresh(0);
    510  1.1  jtc 			break;
    511  1.1  jtc 		case 0:
    512  1.1  jtc 			if (insert != 0) {
    513  1.1  jtc 				if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
    514  1.1  jtc 						lastcmd[0] == 'C') {
    515  1.1  jtc 					if (redo_insert(1) != 0)
    516  1.1  jtc 						vi_error();
    517  1.1  jtc 				} else {
    518  1.1  jtc 					if (redo_insert(lastac) != 0)
    519  1.1  jtc 						vi_error();
    520  1.1  jtc 				}
    521  1.1  jtc 			}
    522  1.1  jtc 			refresh(0);
    523  1.1  jtc 			break;
    524  1.1  jtc 		case 1:
    525  1.1  jtc 			refresh(0);
    526  1.1  jtc 			return 1;
    527  1.1  jtc 		case 2:
    528  1.1  jtc 			/* back from a 'v' command - don't redraw the screen */
    529  1.1  jtc 			return 1;
    530  1.1  jtc 		}
    531  1.1  jtc 		break;
    532  1.1  jtc 
    533  1.1  jtc 	case VFAIL:
    534  1.1  jtc 		state = VNORMAL;
    535  1.1  jtc 		vi_error();
    536  1.1  jtc 		break;
    537  1.1  jtc 	}
    538  1.1  jtc 	return 0;
    539  1.1  jtc }
    540  1.1  jtc 
    541  1.1  jtc static void
    542  1.1  jtc vi_reset(buf, len)
    543  1.1  jtc 	char	*buf;
    544  1.1  jtc 	size_t	len;
    545  1.1  jtc {
    546  1.1  jtc 	state = VNORMAL;
    547  1.1  jtc 	ohnum = hnum = hlast = histnum(-1) + 1;
    548  1.1  jtc 	insert = INSERT;
    549  1.1  jtc 	saved_inslen = inslen;
    550  1.1  jtc 	first_insert = 1;
    551  1.1  jtc 	inslen = 0;
    552  1.1  jtc 	modified = 1;
    553  1.1  jtc 	vi_macro_reset();
    554  1.1  jtc 	edit_reset(buf, len);
    555  1.1  jtc }
    556  1.1  jtc 
    557  1.1  jtc static int
    558  1.1  jtc nextstate(ch)
    559  1.1  jtc 	int	ch;
    560  1.1  jtc {
    561  1.1  jtc 	if (is_extend(ch))
    562  1.1  jtc 		return VEXTCMD;
    563  1.1  jtc 	else if (is_srch(ch))
    564  1.1  jtc 		return VSEARCH;
    565  1.1  jtc 	else if (is_long(ch))
    566  1.1  jtc 		return VXCH;
    567  1.1  jtc 	else if (ch == '.')
    568  1.1  jtc 		return VREDO;
    569  1.1  jtc 	else if (ch == Ctrl('v'))
    570  1.1  jtc 		return VVERSION;
    571  1.1  jtc 	else if (is_cmd(ch))
    572  1.1  jtc 		return VCMD;
    573  1.1  jtc 	else
    574  1.1  jtc 		return VFAIL;
    575  1.1  jtc }
    576  1.1  jtc 
    577  1.1  jtc static int
    578  1.1  jtc vi_insert(ch)
    579  1.1  jtc 	int	ch;
    580  1.1  jtc {
    581  1.1  jtc 	int	tcursor;
    582  1.1  jtc 
    583  1.1  jtc 	if (ch == edchars.erase || ch == Ctrl('h')) {
    584  1.1  jtc 		if (insert == REPLACE) {
    585  1.1  jtc 			if (es->cursor == undo->cursor) {
    586  1.1  jtc 				vi_error();
    587  1.1  jtc 				return 0;
    588  1.1  jtc 			}
    589  1.1  jtc 			if (inslen > 0)
    590  1.1  jtc 				inslen--;
    591  1.1  jtc 			es->cursor--;
    592  1.1  jtc 			if (es->cursor >= undo->linelen)
    593  1.1  jtc 				es->linelen--;
    594  1.1  jtc 			else
    595  1.1  jtc 				es->cbuf[es->cursor] = undo->cbuf[es->cursor];
    596  1.1  jtc 		} else {
    597  1.1  jtc 			if (es->cursor == 0) {
    598  1.1  jtc 				/* x_putc(BEL); no annoying bell here */
    599  1.1  jtc 				return 0;
    600  1.1  jtc 			}
    601  1.1  jtc 			if (inslen > 0)
    602  1.1  jtc 				inslen--;
    603  1.1  jtc 			es->cursor--;
    604  1.1  jtc 			es->linelen--;
    605  1.1  jtc 			memmove(&es->cbuf[es->cursor], &es->cbuf[es->cursor+1],
    606  1.1  jtc 					es->linelen - es->cursor + 1);
    607  1.1  jtc 		}
    608  1.1  jtc 		expanded = NONE;
    609  1.1  jtc 		return 0;
    610  1.1  jtc 	}
    611  1.1  jtc 	if (ch == edchars.kill) {
    612  1.1  jtc 		if (es->cursor != 0) {
    613  1.1  jtc 			inslen = 0;
    614  1.1  jtc 			memmove(es->cbuf, &es->cbuf[es->cursor],
    615  1.1  jtc 						es->linelen - es->cursor);
    616  1.1  jtc 			es->linelen -= es->cursor;
    617  1.1  jtc 			es->cursor = 0;
    618  1.1  jtc 		}
    619  1.1  jtc 		expanded = NONE;
    620  1.1  jtc 		return 0;
    621  1.1  jtc 	}
    622  1.1  jtc 	if (ch == edchars.werase) {
    623  1.1  jtc 		if (es->cursor != 0) {
    624  1.1  jtc 			tcursor = Backword(1);
    625  1.1  jtc 			memmove(&es->cbuf[tcursor], &es->cbuf[es->cursor],
    626  1.1  jtc 						es->linelen - es->cursor);
    627  1.1  jtc 			es->linelen -= es->cursor - tcursor;
    628  1.1  jtc 			if (inslen < es->cursor - tcursor)
    629  1.1  jtc 				inslen = 0;
    630  1.1  jtc 			else
    631  1.1  jtc 				inslen -= es->cursor - tcursor;
    632  1.1  jtc 			es->cursor = tcursor;
    633  1.1  jtc 		}
    634  1.1  jtc 		expanded = NONE;
    635  1.1  jtc 		return 0;
    636  1.1  jtc 	}
    637  1.1  jtc 	/* If any chars are entered before escape, trash the saved insert
    638  1.1  jtc 	 * buffer (if user inserts & deletes char, ibuf gets trashed and
    639  1.1  jtc 	 * we don't want to use it)
    640  1.1  jtc 	 */
    641  1.1  jtc 	if (first_insert && ch != Ctrl('['))
    642  1.1  jtc 		saved_inslen = 0;
    643  1.1  jtc 	switch (ch) {
    644  1.1  jtc 
    645  1.1  jtc #ifdef OS2
    646  1.1  jtc 	case 224:	 /* function key prefix */
    647  1.1  jtc #endif /* OS2 */
    648  1.1  jtc 	case '\0':
    649  1.1  jtc 		return -1;
    650  1.1  jtc 
    651  1.1  jtc 	case '\r':
    652  1.1  jtc 	case '\n':
    653  1.1  jtc 		return 1;
    654  1.1  jtc 
    655  1.1  jtc 	case Ctrl('['):
    656  1.1  jtc 		expanded = NONE;
    657  1.1  jtc 		if (first_insert) {
    658  1.1  jtc 			first_insert = 0;
    659  1.1  jtc 			if (inslen == 0) {
    660  1.1  jtc 				inslen = saved_inslen;
    661  1.1  jtc 				return redo_insert(0);
    662  1.1  jtc 			}
    663  1.1  jtc 			lastcmd[0] = 'a';
    664  1.1  jtc 			lastac = 1;
    665  1.1  jtc 		}
    666  1.1  jtc 		if (lastcmd[0] == 's' || lastcmd[0] == 'c' ||
    667  1.1  jtc 				lastcmd[0] == 'C')
    668  1.1  jtc 			return redo_insert(0);
    669  1.1  jtc 		else
    670  1.1  jtc 			return redo_insert(lastac - 1);
    671  1.1  jtc 
    672  1.1  jtc 	/* { Begin nonstandard vi commands */
    673  1.1  jtc 	case Ctrl('x'):
    674  1.1  jtc 		expand_word(0);
    675  1.1  jtc 		break;
    676  1.1  jtc 
    677  1.1  jtc 	case Ctrl('f'):
    678  1.1  jtc 		complete_word(0, 0);
    679  1.1  jtc 		break;
    680  1.1  jtc 
    681  1.1  jtc 	case Ctrl('e'):
    682  1.1  jtc 		print_expansions(es, 0);
    683  1.1  jtc 		break;
    684  1.1  jtc 
    685  1.1  jtc 	case Ctrl('i'):
    686  1.1  jtc 		if (Flag(FVITABCOMPLETE)) {
    687  1.1  jtc 			complete_word(0, 0);
    688  1.1  jtc 			break;
    689  1.1  jtc 		}
    690  1.1  jtc 		/* FALLTHROUGH */
    691  1.1  jtc 	/* End nonstandard vi commands } */
    692  1.1  jtc 
    693  1.1  jtc 	default:
    694  1.1  jtc 		if (es->linelen == es->cbufsize - 1)
    695  1.1  jtc 			return -1;
    696  1.1  jtc 		ibuf[inslen++] = ch;
    697  1.1  jtc 		if (insert == INSERT) {
    698  1.1  jtc 			memmove(&es->cbuf[es->cursor+1], &es->cbuf[es->cursor],
    699  1.1  jtc 					es->linelen - es->cursor);
    700  1.1  jtc 			es->linelen++;
    701  1.1  jtc 		}
    702  1.1  jtc 		es->cbuf[es->cursor++] = ch;
    703  1.1  jtc 		if (insert == REPLACE && es->cursor > es->linelen)
    704  1.1  jtc 			es->linelen++;
    705  1.1  jtc 		expanded = NONE;
    706  1.1  jtc 	}
    707  1.1  jtc 	return 0;
    708  1.1  jtc }
    709  1.1  jtc 
    710  1.1  jtc static int
    711  1.1  jtc vi_cmd(argcnt, cmd)
    712  1.1  jtc 	int		argcnt;
    713  1.1  jtc 	const char	*cmd;
    714  1.1  jtc {
    715  1.1  jtc 	int		ncursor;
    716  1.1  jtc 	int		cur, c1, c2, c3 = 0;
    717  1.1  jtc 	int		any;
    718  1.1  jtc 	struct edstate	*t;
    719  1.1  jtc 
    720  1.1  jtc 	if (argcnt == 0 && !is_zerocount(*cmd))
    721  1.1  jtc 		argcnt = 1;
    722  1.1  jtc 
    723  1.1  jtc 	if (is_move(*cmd)) {
    724  1.1  jtc 		if ((cur = domove(argcnt, cmd, 0)) >= 0) {
    725  1.1  jtc 			if (cur == es->linelen && cur != 0)
    726  1.1  jtc 				cur--;
    727  1.1  jtc 			es->cursor = cur;
    728  1.1  jtc 		} else
    729  1.1  jtc 			return -1;
    730  1.1  jtc 	} else {
    731  1.1  jtc 		/* Don't save state in middle of macro.. */
    732  1.1  jtc 		if (is_undoable(*cmd) && !macro.p) {
    733  1.1  jtc 			undo->winleft = es->winleft;
    734  1.1  jtc 			memmove(undo->cbuf, es->cbuf, es->linelen);
    735  1.1  jtc 			undo->linelen = es->linelen;
    736  1.1  jtc 			undo->cursor = es->cursor;
    737  1.1  jtc 			lastac = argcnt;
    738  1.1  jtc 			memmove(lastcmd, cmd, MAXVICMD);
    739  1.1  jtc 		}
    740  1.1  jtc 		switch (*cmd) {
    741  1.1  jtc 
    742  1.1  jtc 		case Ctrl('l'):
    743  1.1  jtc 		case Ctrl('r'):
    744  1.1  jtc 			redraw_line(1);
    745  1.1  jtc 			break;
    746  1.1  jtc 
    747  1.1  jtc 		case '@':
    748  1.1  jtc 			{
    749  1.1  jtc 				static char alias[] = "_\0";
    750  1.1  jtc 				struct tbl *ap;
    751  1.1  jtc 				int	olen, nlen;
    752  1.1  jtc 				char	*p, *nbuf;
    753  1.1  jtc 
    754  1.1  jtc 				/* lookup letter in alias list... */
    755  1.1  jtc 				alias[1] = cmd[1];
    756  1.1  jtc 				ap = tsearch(&aliases, alias, hash(alias));
    757  1.1  jtc 				if (!cmd[1] || !ap || !(ap->flag & ISSET))
    758  1.1  jtc 					return -1;
    759  1.1  jtc 				/* check if this is a recursive call... */
    760  1.1  jtc 				if ((p = (char *) macro.p))
    761  1.1  jtc 					while ((p = strchr(p, '\0')) && p[1])
    762  1.1  jtc 						if (*++p == cmd[1])
    763  1.1  jtc 							return -1;
    764  1.1  jtc 				/* insert alias into macro buffer */
    765  1.1  jtc 				nlen = strlen(ap->val.s) + 1;
    766  1.1  jtc 				olen = !macro.p ? 2
    767  1.1  jtc 					: macro.len - (macro.p - macro.buf);
    768  1.1  jtc 				nbuf = alloc(nlen + 1 + olen, APERM);
    769  1.1  jtc 				memcpy(nbuf, ap->val.s, nlen);
    770  1.1  jtc 				nbuf[nlen++] = cmd[1];
    771  1.1  jtc 				if (macro.p) {
    772  1.1  jtc 					memcpy(nbuf + nlen, macro.p, olen);
    773  1.1  jtc 					afree(macro.buf, APERM);
    774  1.1  jtc 					nlen += olen;
    775  1.1  jtc 				} else {
    776  1.1  jtc 					nbuf[nlen++] = '\0';
    777  1.1  jtc 					nbuf[nlen++] = '\0';
    778  1.1  jtc 				}
    779  1.1  jtc 				macro.p = macro.buf = (unsigned char *) nbuf;
    780  1.1  jtc 				macro.len = nlen;
    781  1.1  jtc 			}
    782  1.1  jtc 			break;
    783  1.1  jtc 
    784  1.1  jtc 		case 'a':
    785  1.1  jtc 			modified = 1; hnum = hlast;
    786  1.1  jtc 			if (es->linelen != 0)
    787  1.1  jtc 				es->cursor++;
    788  1.1  jtc 			insert = INSERT;
    789  1.1  jtc 			break;
    790  1.1  jtc 
    791  1.1  jtc 		case 'A':
    792  1.1  jtc 			modified = 1; hnum = hlast;
    793  1.1  jtc 			del_range(0, 0);
    794  1.1  jtc 			es->cursor = es->linelen;
    795  1.1  jtc 			insert = INSERT;
    796  1.1  jtc 			break;
    797  1.1  jtc 
    798  1.1  jtc 		case 'S':
    799  1.1  jtc 			es->cursor = domove(1, "^", 1);
    800  1.1  jtc 			del_range(es->cursor, es->linelen);
    801  1.1  jtc 			modified = 1; hnum = hlast;
    802  1.1  jtc 			insert = INSERT;
    803  1.1  jtc 			break;
    804  1.1  jtc 
    805  1.1  jtc 		case 'Y':
    806  1.1  jtc 			cmd = "y$";
    807  1.1  jtc 			/* ahhhhhh... */
    808  1.1  jtc 		case 'c':
    809  1.1  jtc 		case 'd':
    810  1.1  jtc 		case 'y':
    811  1.1  jtc 			if (*cmd == cmd[1]) {
    812  1.1  jtc 				c1 = *cmd == 'c' ? domove(1, "^", 1) : 0;
    813  1.1  jtc 				c2 = es->linelen;
    814  1.1  jtc 			} else if (!is_move(cmd[1]))
    815  1.1  jtc 				return -1;
    816  1.1  jtc 			else {
    817  1.1  jtc 				if ((ncursor = domove(argcnt, &cmd[1], 1)) < 0)
    818  1.1  jtc 					return -1;
    819  1.1  jtc 				if (*cmd == 'c' &&
    820  1.1  jtc 						(cmd[1]=='w' || cmd[1]=='W') &&
    821  1.1  jtc 						!isspace(es->cbuf[es->cursor])) {
    822  1.1  jtc 					while (isspace(es->cbuf[--ncursor]))
    823  1.1  jtc 						;
    824  1.1  jtc 					ncursor++;
    825  1.1  jtc 				}
    826  1.1  jtc 				if (ncursor > es->cursor) {
    827  1.1  jtc 					c1 = es->cursor;
    828  1.1  jtc 					c2 = ncursor;
    829  1.1  jtc 				} else {
    830  1.1  jtc 					c1 = ncursor;
    831  1.1  jtc 					c2 = es->cursor;
    832  1.1  jtc 					if (cmd[1] == '%')
    833  1.1  jtc 						c2++;
    834  1.1  jtc 				}
    835  1.1  jtc 			}
    836  1.1  jtc 			if (*cmd != 'c' && c1 != c2)
    837  1.1  jtc 				yank_range(c1, c2);
    838  1.1  jtc 			if (*cmd != 'y') {
    839  1.1  jtc 				del_range(c1, c2);
    840  1.1  jtc 				es->cursor = c1;
    841  1.1  jtc 			}
    842  1.1  jtc 			if (*cmd == 'c') {
    843  1.1  jtc 				modified = 1; hnum = hlast;
    844  1.1  jtc 				insert = INSERT;
    845  1.1  jtc 			}
    846  1.1  jtc 			break;
    847  1.1  jtc 
    848  1.1  jtc 		case 'p':
    849  1.1  jtc 			modified = 1; hnum = hlast;
    850  1.1  jtc 			if (es->linelen != 0)
    851  1.1  jtc 				es->cursor++;
    852  1.1  jtc 			while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
    853  1.1  jtc 				;
    854  1.1  jtc 			if (es->cursor != 0)
    855  1.1  jtc 				es->cursor--;
    856  1.1  jtc 			if (argcnt != 0)
    857  1.1  jtc 				return -1;
    858  1.1  jtc 			break;
    859  1.1  jtc 
    860  1.1  jtc 		case 'P':
    861  1.1  jtc 			modified = 1; hnum = hlast;
    862  1.1  jtc 			any = 0;
    863  1.1  jtc 			while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0)
    864  1.1  jtc 				any = 1;
    865  1.1  jtc 			if (any && es->cursor != 0)
    866  1.1  jtc 				es->cursor--;
    867  1.1  jtc 			if (argcnt != 0)
    868  1.1  jtc 				return -1;
    869  1.1  jtc 			break;
    870  1.1  jtc 
    871  1.1  jtc 		case 'C':
    872  1.1  jtc 			modified = 1; hnum = hlast;
    873  1.1  jtc 			del_range(es->cursor, es->linelen);
    874  1.1  jtc 			insert = INSERT;
    875  1.1  jtc 			break;
    876  1.1  jtc 
    877  1.1  jtc 		case 'D':
    878  1.1  jtc 			yank_range(es->cursor, es->linelen);
    879  1.1  jtc 			del_range(es->cursor, es->linelen);
    880  1.1  jtc 			if (es->cursor != 0)
    881  1.1  jtc 				es->cursor--;
    882  1.1  jtc 			break;
    883  1.1  jtc 
    884  1.1  jtc 		case 'g':
    885  1.1  jtc 			if (!argcnt)
    886  1.1  jtc 				argcnt = hlast + 1;
    887  1.1  jtc 			/* fall through */
    888  1.1  jtc 		case 'G':
    889  1.1  jtc 			if (!argcnt)
    890  1.1  jtc 				argcnt = 1;
    891  1.1  jtc 			else
    892  1.1  jtc 				argcnt = hlast - (source->line - argcnt);
    893  1.1  jtc 			if (grabhist(modified, argcnt - 1) < 0)
    894  1.1  jtc 				return -1;
    895  1.1  jtc 			else {
    896  1.1  jtc 				modified = 0;
    897  1.1  jtc 				hnum = argcnt - 1;
    898  1.1  jtc 			}
    899  1.1  jtc 			break;
    900  1.1  jtc 
    901  1.1  jtc 		case 'i':
    902  1.1  jtc 			modified = 1; hnum = hlast;
    903  1.1  jtc 			insert = INSERT;
    904  1.1  jtc 			break;
    905  1.1  jtc 
    906  1.1  jtc 		case 'I':
    907  1.1  jtc 			modified = 1; hnum = hlast;
    908  1.1  jtc 			es->cursor = domove(1, "^", 1);
    909  1.1  jtc 			insert = INSERT;
    910  1.1  jtc 			break;
    911  1.1  jtc 
    912  1.1  jtc 		case 'j':
    913  1.1  jtc 		case '+':
    914  1.1  jtc 		case Ctrl('n'):
    915  1.1  jtc 			if (grabhist(modified, hnum + argcnt) < 0)
    916  1.1  jtc 				return -1;
    917  1.1  jtc 			else {
    918  1.1  jtc 				modified = 0;
    919  1.1  jtc 				hnum += argcnt;
    920  1.1  jtc 			}
    921  1.1  jtc 			break;
    922  1.1  jtc 
    923  1.1  jtc 		case 'k':
    924  1.1  jtc 		case '-':
    925  1.1  jtc 		case Ctrl('p'):
    926  1.1  jtc 			if (grabhist(modified, hnum - argcnt) < 0)
    927  1.1  jtc 				return -1;
    928  1.1  jtc 			else {
    929  1.1  jtc 				modified = 0;
    930  1.1  jtc 				hnum -= argcnt;
    931  1.1  jtc 			}
    932  1.1  jtc 			break;
    933  1.1  jtc 
    934  1.1  jtc 		case 'r':
    935  1.1  jtc 			if (es->linelen == 0)
    936  1.1  jtc 				return -1;
    937  1.1  jtc 			modified = 1; hnum = hlast;
    938  1.1  jtc 			if (cmd[1] == 0)
    939  1.1  jtc 				vi_error();
    940  1.1  jtc 			else
    941  1.1  jtc 				es->cbuf[es->cursor] = cmd[1];
    942  1.1  jtc 			break;
    943  1.1  jtc 
    944  1.1  jtc 		case 'R':
    945  1.1  jtc 			modified = 1; hnum = hlast;
    946  1.1  jtc 			insert = REPLACE;
    947  1.1  jtc 			break;
    948  1.1  jtc 
    949  1.1  jtc 		case 's':
    950  1.1  jtc 			if (es->linelen == 0)
    951  1.1  jtc 				return -1;
    952  1.1  jtc 			modified = 1; hnum = hlast;
    953  1.1  jtc 			if (es->cursor + argcnt > es->linelen)
    954  1.1  jtc 				argcnt = es->linelen - es->cursor;
    955  1.1  jtc 			del_range(es->cursor, es->cursor + argcnt);
    956  1.1  jtc 			insert = INSERT;
    957  1.1  jtc 			break;
    958  1.1  jtc 
    959  1.1  jtc 		case 'v':
    960  1.1  jtc 			if (es->linelen == 0)
    961  1.1  jtc 				return -1;
    962  1.1  jtc 			if (!argcnt) {
    963  1.1  jtc 				if (modified) {
    964  1.1  jtc 					es->cbuf[es->linelen] = '\0';
    965  1.1  jtc 					source->line++;
    966  1.1  jtc 					histsave(source->line, es->cbuf, 1);
    967  1.1  jtc 				} else
    968  1.1  jtc 					argcnt = source->line + 1
    969  1.1  jtc 						- (hlast - hnum);
    970  1.1  jtc 			}
    971  1.1  jtc 			shf_snprintf(es->cbuf, es->cbufsize,
    972  1.1  jtc 					argcnt ? "%s %d" : "%s",
    973  1.1  jtc 					"fc -e ${VISUAL:-${EDITOR:-vi}} --",
    974  1.1  jtc 					argcnt);
    975  1.1  jtc 			es->linelen = strlen(es->cbuf);
    976  1.1  jtc 			return 2;
    977  1.1  jtc 
    978  1.1  jtc 		case 'x':
    979  1.1  jtc 			if (es->linelen == 0)
    980  1.1  jtc 				return -1;
    981  1.1  jtc 			modified = 1; hnum = hlast;
    982  1.1  jtc 			if (es->cursor + argcnt > es->linelen)
    983  1.1  jtc 				argcnt = es->linelen - es->cursor;
    984  1.1  jtc 			yank_range(es->cursor, es->cursor + argcnt);
    985  1.1  jtc 			del_range(es->cursor, es->cursor + argcnt);
    986  1.1  jtc 			break;
    987  1.1  jtc 
    988  1.1  jtc 		case 'X':
    989  1.1  jtc 			if (es->cursor > 0) {
    990  1.1  jtc 				modified = 1; hnum = hlast;
    991  1.1  jtc 				if (es->cursor < argcnt)
    992  1.1  jtc 					argcnt = es->cursor;
    993  1.1  jtc 				yank_range(es->cursor - argcnt, es->cursor);
    994  1.1  jtc 				del_range(es->cursor - argcnt, es->cursor);
    995  1.1  jtc 				es->cursor -= argcnt;
    996  1.1  jtc 			} else
    997  1.1  jtc 				return -1;
    998  1.1  jtc 			break;
    999  1.1  jtc 
   1000  1.1  jtc 		case 'u':
   1001  1.1  jtc 			t = es;
   1002  1.1  jtc 			es = undo;
   1003  1.1  jtc 			undo = t;
   1004  1.1  jtc 			break;
   1005  1.1  jtc 
   1006  1.1  jtc 		case 'U':
   1007  1.1  jtc 			if (!modified)
   1008  1.1  jtc 				return -1;
   1009  1.1  jtc 			if (grabhist(modified, ohnum) < 0)
   1010  1.1  jtc 				return -1;
   1011  1.1  jtc 			modified = 0;
   1012  1.1  jtc 			hnum = ohnum;
   1013  1.1  jtc 			break;
   1014  1.1  jtc 
   1015  1.1  jtc 		case '?':
   1016  1.1  jtc 			if (hnum == hlast)
   1017  1.1  jtc 				hnum = -1;
   1018  1.1  jtc 			/* ahhh */
   1019  1.1  jtc 		case '/':
   1020  1.1  jtc 			c3 = 1;
   1021  1.1  jtc 			srchlen = 0;
   1022  1.1  jtc 			lastsearch = *cmd;
   1023  1.1  jtc 			/* fall through */
   1024  1.1  jtc 		case 'n':
   1025  1.1  jtc 		case 'N':
   1026  1.1  jtc 			if (lastsearch == ' ')
   1027  1.1  jtc 				return -1;
   1028  1.1  jtc 			if (lastsearch == '?')
   1029  1.1  jtc 				c1 = 1;
   1030  1.1  jtc 			else
   1031  1.1  jtc 				c1 = 0;
   1032  1.1  jtc 			if (*cmd == 'N')
   1033  1.1  jtc 				c1 = !c1;
   1034  1.1  jtc 			if ((c2 = grabsearch(modified, hnum,
   1035  1.1  jtc 							c1, srchpat)) < 0) {
   1036  1.1  jtc 				if (c3) {
   1037  1.1  jtc 					restore_cbuf();
   1038  1.1  jtc 					refresh(0);
   1039  1.1  jtc 				}
   1040  1.1  jtc 				return -1;
   1041  1.1  jtc 			} else {
   1042  1.1  jtc 				modified = 0;
   1043  1.1  jtc 				hnum = c2;
   1044  1.1  jtc 				ohnum = hnum;
   1045  1.1  jtc 			}
   1046  1.1  jtc 			break;
   1047  1.1  jtc 		case '_': {
   1048  1.1  jtc 			int	inspace;
   1049  1.1  jtc 			char	*p, *sp;
   1050  1.1  jtc 
   1051  1.1  jtc 			if (histnum(-1) < 0)
   1052  1.1  jtc 				return -1;
   1053  1.1  jtc 			p = *histpos();
   1054  1.1  jtc #define issp(c)		(isspace((c)) || (c) == '\n')
   1055  1.1  jtc 			if (argcnt) {
   1056  1.1  jtc 				while (*p && issp(*p))
   1057  1.1  jtc 					p++;
   1058  1.1  jtc 				while (*p && --argcnt) {
   1059  1.1  jtc 					while (*p && !issp(*p))
   1060  1.1  jtc 						p++;
   1061  1.1  jtc 					while (*p && issp(*p))
   1062  1.1  jtc 						p++;
   1063  1.1  jtc 				}
   1064  1.1  jtc 				if (!*p)
   1065  1.1  jtc 					return -1;
   1066  1.1  jtc 				sp = p;
   1067  1.1  jtc 			} else {
   1068  1.1  jtc 				sp = p;
   1069  1.1  jtc 				inspace = 0;
   1070  1.1  jtc 				while (*p) {
   1071  1.1  jtc 					if (issp(*p))
   1072  1.1  jtc 						inspace = 1;
   1073  1.1  jtc 					else if (inspace) {
   1074  1.1  jtc 						inspace = 0;
   1075  1.1  jtc 						sp = p;
   1076  1.1  jtc 					}
   1077  1.1  jtc 					p++;
   1078  1.1  jtc 				}
   1079  1.1  jtc 				p = sp;
   1080  1.1  jtc 			}
   1081  1.1  jtc 			modified = 1; hnum = hlast;
   1082  1.1  jtc 			if (es->cursor != es->linelen)
   1083  1.1  jtc 				es->cursor++;
   1084  1.1  jtc 			while (*p && !issp(*p)) {
   1085  1.1  jtc 				argcnt++;
   1086  1.1  jtc 				p++;
   1087  1.1  jtc 			}
   1088  1.1  jtc 			if (putbuf(space, 1, 0) != 0)
   1089  1.1  jtc 				argcnt = -1;
   1090  1.1  jtc 			else if (putbuf(sp, argcnt, 0) != 0)
   1091  1.1  jtc 				argcnt = -1;
   1092  1.1  jtc 			if (argcnt < 0) {
   1093  1.1  jtc 				if (es->cursor != 0)
   1094  1.1  jtc 					es->cursor--;
   1095  1.1  jtc 				return -1;
   1096  1.1  jtc 			}
   1097  1.1  jtc 			insert = INSERT;
   1098  1.1  jtc 			}
   1099  1.1  jtc 			break;
   1100  1.1  jtc 
   1101  1.1  jtc 		case '~': {
   1102  1.1  jtc 			char	*p;
   1103  1.1  jtc 			int	i;
   1104  1.1  jtc 
   1105  1.1  jtc 			if (es->linelen == 0)
   1106  1.1  jtc 				return -1;
   1107  1.1  jtc 			for (i = 0; i < argcnt; i++) {
   1108  1.1  jtc 				p = &es->cbuf[es->cursor];
   1109  1.1  jtc 				if (islower(*p)) {
   1110  1.1  jtc 					modified = 1; hnum = hlast;
   1111  1.1  jtc 					*p = toupper(*p);
   1112  1.1  jtc 				} else if (isupper(*p)) {
   1113  1.1  jtc 					modified = 1; hnum = hlast;
   1114  1.1  jtc 					*p = tolower(*p);
   1115  1.1  jtc 				}
   1116  1.1  jtc 				if (es->cursor < es->linelen - 1)
   1117  1.1  jtc 					es->cursor++;
   1118  1.1  jtc 			}
   1119  1.1  jtc 			break;
   1120  1.1  jtc 			}
   1121  1.1  jtc 
   1122  1.1  jtc 		case '#':
   1123  1.1  jtc 		    {
   1124  1.1  jtc 			int ret = x_do_comment(es->cbuf, es->cbufsize,
   1125  1.1  jtc 					    &es->linelen);
   1126  1.1  jtc 			if (ret >= 0)
   1127  1.1  jtc 				es->cursor = 0;
   1128  1.1  jtc 			return ret;
   1129  1.1  jtc 		    }
   1130  1.1  jtc 
   1131  1.1  jtc 		case '=': 			/* at&t ksh */
   1132  1.1  jtc 		case Ctrl('e'):			/* Nonstandard vi/ksh */
   1133  1.1  jtc 			print_expansions(es, 1);
   1134  1.1  jtc 			break;
   1135  1.1  jtc 
   1136  1.1  jtc 
   1137  1.1  jtc 		case Ctrl('i'):			/* Nonstandard vi/ksh */
   1138  1.1  jtc 			if (!Flag(FVITABCOMPLETE))
   1139  1.1  jtc 				return -1;
   1140  1.2  tls 			complete_word(1, argcnt);
   1141  1.2  tls 			break;
   1142  1.1  jtc 
   1143  1.1  jtc 		case Ctrl('['):			/* some annoying at&t ksh's */
   1144  1.1  jtc 			if (!Flag(FVIESCCOMPLETE))
   1145  1.1  jtc 				return -1;
   1146  1.1  jtc 		case '\\':			/* at&t ksh */
   1147  1.1  jtc 		case Ctrl('f'):			/* Nonstandard vi/ksh */
   1148  1.1  jtc 			complete_word(1, argcnt);
   1149  1.1  jtc 			break;
   1150  1.1  jtc 
   1151  1.1  jtc 
   1152  1.1  jtc 		case '*':			/* at&t ksh */
   1153  1.1  jtc 		case Ctrl('x'):			/* Nonstandard vi/ksh */
   1154  1.1  jtc 			expand_word(1);
   1155  1.1  jtc 			break;
   1156  1.1  jtc 		}
   1157  1.1  jtc 		if (insert == 0 && es->cursor != 0 && es->cursor >= es->linelen)
   1158  1.1  jtc 			es->cursor--;
   1159  1.1  jtc 	}
   1160  1.1  jtc 	return 0;
   1161  1.1  jtc }
   1162  1.1  jtc 
   1163  1.1  jtc static int
   1164  1.1  jtc domove(argcnt, cmd, sub)
   1165  1.1  jtc 	int	argcnt;
   1166  1.1  jtc 	const char *cmd;
   1167  1.1  jtc 	int	sub;
   1168  1.1  jtc {
   1169  1.1  jtc 	int	bcount, UNINITIALIZED(i), t;
   1170  1.1  jtc 	int	UNINITIALIZED(ncursor);
   1171  1.1  jtc 
   1172  1.1  jtc 	switch (*cmd) {
   1173  1.1  jtc 
   1174  1.1  jtc 	case 'b':
   1175  1.1  jtc 		if (!sub && es->cursor == 0)
   1176  1.1  jtc 			return -1;
   1177  1.1  jtc 		ncursor = backword(argcnt);
   1178  1.1  jtc 		break;
   1179  1.1  jtc 
   1180  1.1  jtc 	case 'B':
   1181  1.1  jtc 		if (!sub && es->cursor == 0)
   1182  1.1  jtc 			return -1;
   1183  1.1  jtc 		ncursor = Backword(argcnt);
   1184  1.1  jtc 		break;
   1185  1.1  jtc 
   1186  1.1  jtc 	case 'e':
   1187  1.1  jtc 		if (!sub && es->cursor + 1 >= es->linelen)
   1188  1.1  jtc 			return -1;
   1189  1.1  jtc 		ncursor = endword(argcnt);
   1190  1.1  jtc 		if (sub && ncursor < es->linelen)
   1191  1.1  jtc 			ncursor++;
   1192  1.1  jtc 		break;
   1193  1.1  jtc 
   1194  1.1  jtc 	case 'E':
   1195  1.1  jtc 		if (!sub && es->cursor + 1 >= es->linelen)
   1196  1.1  jtc 			return -1;
   1197  1.1  jtc 		ncursor = Endword(argcnt);
   1198  1.1  jtc 		if (sub && ncursor < es->linelen)
   1199  1.1  jtc 			ncursor++;
   1200  1.1  jtc 		break;
   1201  1.1  jtc 
   1202  1.1  jtc 	case 'f':
   1203  1.1  jtc 	case 'F':
   1204  1.1  jtc 	case 't':
   1205  1.1  jtc 	case 'T':
   1206  1.1  jtc 		fsavecmd = *cmd;
   1207  1.1  jtc 		fsavech = cmd[1];
   1208  1.1  jtc 		/* drop through */
   1209  1.1  jtc 
   1210  1.1  jtc 	case ',':
   1211  1.1  jtc 	case ';':
   1212  1.1  jtc 		if (fsavecmd == ' ')
   1213  1.1  jtc 			return -1;
   1214  1.1  jtc 		i = fsavecmd == 'f' || fsavecmd == 'F';
   1215  1.1  jtc 		t = fsavecmd > 'a';
   1216  1.1  jtc 		if (*cmd == ',')
   1217  1.1  jtc 			t = !t;
   1218  1.1  jtc 		if ((ncursor = findch(fsavech, argcnt, t, i)) < 0)
   1219  1.1  jtc 			return -1;
   1220  1.1  jtc 		if (sub && t)
   1221  1.1  jtc 			ncursor++;
   1222  1.1  jtc 		break;
   1223  1.1  jtc 
   1224  1.1  jtc 	case 'h':
   1225  1.1  jtc 	case Ctrl('h'):
   1226  1.1  jtc 		if (!sub && es->cursor == 0)
   1227  1.1  jtc 			return -1;
   1228  1.1  jtc 		ncursor = es->cursor - argcnt;
   1229  1.1  jtc 		if (ncursor < 0)
   1230  1.1  jtc 			ncursor = 0;
   1231  1.1  jtc 		break;
   1232  1.1  jtc 
   1233  1.1  jtc 	case ' ':
   1234  1.1  jtc 	case 'l':
   1235  1.1  jtc 		if (!sub && es->cursor + 1 >= es->linelen)
   1236  1.1  jtc 			return -1;
   1237  1.1  jtc 		if (es->linelen != 0) {
   1238  1.1  jtc 			ncursor = es->cursor + argcnt;
   1239  1.1  jtc 			if (ncursor > es->linelen)
   1240  1.1  jtc 				ncursor = es->linelen;
   1241  1.1  jtc 		}
   1242  1.1  jtc 		break;
   1243  1.1  jtc 
   1244  1.1  jtc 	case 'w':
   1245  1.1  jtc 		if (!sub && es->cursor + 1 >= es->linelen)
   1246  1.1  jtc 			return -1;
   1247  1.1  jtc 		ncursor = forwword(argcnt);
   1248  1.1  jtc 		break;
   1249  1.1  jtc 
   1250  1.1  jtc 	case 'W':
   1251  1.1  jtc 		if (!sub && es->cursor + 1 >= es->linelen)
   1252  1.1  jtc 			return -1;
   1253  1.1  jtc 		ncursor = Forwword(argcnt);
   1254  1.1  jtc 		break;
   1255  1.1  jtc 
   1256  1.1  jtc 	case '0':
   1257  1.1  jtc 		ncursor = 0;
   1258  1.1  jtc 		break;
   1259  1.1  jtc 
   1260  1.1  jtc 	case '^':
   1261  1.1  jtc 		ncursor = 0;
   1262  1.1  jtc 		while (ncursor < es->linelen - 1 && isspace(es->cbuf[ncursor]))
   1263  1.1  jtc 			ncursor++;
   1264  1.1  jtc 		break;
   1265  1.1  jtc 
   1266  1.1  jtc 	case '|':
   1267  1.1  jtc 		ncursor = argcnt;
   1268  1.1  jtc 		if (ncursor > es->linelen)
   1269  1.1  jtc 			ncursor = es->linelen;
   1270  1.1  jtc 		if (ncursor)
   1271  1.1  jtc 			ncursor--;
   1272  1.1  jtc 		break;
   1273  1.1  jtc 
   1274  1.1  jtc 	case '$':
   1275  1.1  jtc 		if (es->linelen != 0)
   1276  1.1  jtc 			ncursor = es->linelen;
   1277  1.1  jtc 		else
   1278  1.1  jtc 			ncursor = 0;
   1279  1.1  jtc 		break;
   1280  1.1  jtc 
   1281  1.1  jtc 	case '%':
   1282  1.1  jtc 		ncursor = es->cursor;
   1283  1.1  jtc 		while (ncursor < es->linelen &&
   1284  1.1  jtc 				(i = bracktype(es->cbuf[ncursor])) == 0)
   1285  1.1  jtc 			ncursor++;
   1286  1.1  jtc 		if (ncursor == es->linelen)
   1287  1.1  jtc 			return -1;
   1288  1.1  jtc 		bcount = 1;
   1289  1.1  jtc 		do {
   1290  1.1  jtc 			if (i > 0) {
   1291  1.1  jtc 				if (++ncursor >= es->linelen)
   1292  1.1  jtc 					return -1;
   1293  1.1  jtc 			} else {
   1294  1.1  jtc 				if (--ncursor < 0)
   1295  1.1  jtc 					return -1;
   1296  1.1  jtc 			}
   1297  1.1  jtc 			t = bracktype(es->cbuf[ncursor]);
   1298  1.1  jtc 			if (t == i)
   1299  1.1  jtc 				bcount++;
   1300  1.1  jtc 			else if (t == -i)
   1301  1.1  jtc 				bcount--;
   1302  1.1  jtc 		} while (bcount != 0);
   1303  1.1  jtc 		if (sub && i > 0)
   1304  1.1  jtc 			ncursor++;
   1305  1.1  jtc 		break;
   1306  1.1  jtc 
   1307  1.1  jtc 	default:
   1308  1.1  jtc 		return -1;
   1309  1.1  jtc 	}
   1310  1.1  jtc 	return ncursor;
   1311  1.1  jtc }
   1312  1.1  jtc 
   1313  1.1  jtc static int
   1314  1.1  jtc redo_insert(count)
   1315  1.1  jtc 	int	count;
   1316  1.1  jtc {
   1317  1.1  jtc 	while (count-- > 0)
   1318  1.1  jtc 		if (putbuf(ibuf, inslen, insert==REPLACE) != 0)
   1319  1.1  jtc 			return -1;
   1320  1.1  jtc 	if (es->cursor > 0)
   1321  1.1  jtc 		es->cursor--;
   1322  1.1  jtc 	insert = 0;
   1323  1.1  jtc 	return 0;
   1324  1.1  jtc }
   1325  1.1  jtc 
   1326  1.1  jtc static void
   1327  1.1  jtc yank_range(a, b)
   1328  1.1  jtc 	int	a, b;
   1329  1.1  jtc {
   1330  1.1  jtc 	yanklen = b - a;
   1331  1.1  jtc 	if (yanklen != 0)
   1332  1.1  jtc 		memmove(ybuf, &es->cbuf[a], yanklen);
   1333  1.1  jtc }
   1334  1.1  jtc 
   1335  1.1  jtc static int
   1336  1.1  jtc bracktype(ch)
   1337  1.1  jtc 	int	ch;
   1338  1.1  jtc {
   1339  1.1  jtc 	switch (ch) {
   1340  1.1  jtc 
   1341  1.1  jtc 	case '(':
   1342  1.1  jtc 		return 1;
   1343  1.1  jtc 
   1344  1.1  jtc 	case '[':
   1345  1.1  jtc 		return 2;
   1346  1.1  jtc 
   1347  1.1  jtc 	case '{':
   1348  1.1  jtc 		return 3;
   1349  1.1  jtc 
   1350  1.1  jtc 	case ')':
   1351  1.1  jtc 		return -1;
   1352  1.1  jtc 
   1353  1.1  jtc 	case ']':
   1354  1.1  jtc 		return -2;
   1355  1.1  jtc 
   1356  1.1  jtc 	case '}':
   1357  1.1  jtc 		return -3;
   1358  1.1  jtc 
   1359  1.1  jtc 	default:
   1360  1.1  jtc 		return 0;
   1361  1.1  jtc 	}
   1362  1.1  jtc }
   1363  1.1  jtc 
   1364  1.1  jtc /*
   1365  1.1  jtc  *	Non user interface editor routines below here
   1366  1.1  jtc  */
   1367  1.1  jtc 
   1368  1.1  jtc static int	cur_col;		/* current column on line */
   1369  1.1  jtc static int	pwidth;			/* width of prompt */
   1370  1.1  jtc static int	prompt_trunc;		/* how much of prompt to truncate */
   1371  1.1  jtc static int	prompt_skip;		/* how much of prompt to skip */
   1372  1.1  jtc static int	winwidth;		/* width of window */
   1373  1.1  jtc static char	*wbuf[2];		/* window buffers */
   1374  1.1  jtc static int	wbuf_len;		/* length of window buffers (x_cols-3)*/
   1375  1.1  jtc static int	win;			/* window buffer in use */
   1376  1.1  jtc static char	morec;			/* more character at right of window */
   1377  1.1  jtc static int	lastref;		/* argument to last refresh() */
   1378  1.1  jtc static char	holdbuf[CMDLEN];	/* place to hold last edit buffer */
   1379  1.1  jtc static int	holdlen;		/* length of holdbuf */
   1380  1.1  jtc 
   1381  1.1  jtc static void
   1382  1.1  jtc save_cbuf()
   1383  1.1  jtc {
   1384  1.1  jtc 	memmove(holdbuf, es->cbuf, es->linelen);
   1385  1.1  jtc 	holdlen = es->linelen;
   1386  1.1  jtc 	holdbuf[holdlen] = '\0';
   1387  1.1  jtc }
   1388  1.1  jtc 
   1389  1.1  jtc static void
   1390  1.1  jtc restore_cbuf()
   1391  1.1  jtc {
   1392  1.1  jtc 	es->cursor = 0;
   1393  1.1  jtc 	es->linelen = holdlen;
   1394  1.1  jtc 	memmove(es->cbuf, holdbuf, holdlen);
   1395  1.1  jtc }
   1396  1.1  jtc 
   1397  1.1  jtc /* return a new edstate */
   1398  1.1  jtc static struct edstate *
   1399  1.1  jtc save_edstate(old)
   1400  1.1  jtc 	struct edstate *old;
   1401  1.1  jtc {
   1402  1.1  jtc 	struct edstate *new;
   1403  1.1  jtc 
   1404  1.1  jtc 	new = (struct edstate *)alloc(sizeof(struct edstate), APERM);
   1405  1.1  jtc 	new->cbuf = alloc(old->cbufsize, APERM);
   1406  1.1  jtc 	new->cbufsize = old->cbufsize;
   1407  1.1  jtc 	strcpy(new->cbuf, old->cbuf);
   1408  1.1  jtc 	new->linelen = old->linelen;
   1409  1.1  jtc 	new->cursor = old->cursor;
   1410  1.1  jtc 	new->winleft = old->winleft;
   1411  1.1  jtc 	return new;
   1412  1.1  jtc }
   1413  1.1  jtc 
   1414  1.1  jtc static void
   1415  1.1  jtc restore_edstate(new, old)
   1416  1.1  jtc 	struct edstate *old, *new;
   1417  1.1  jtc {
   1418  1.1  jtc 	strncpy(new->cbuf, old->cbuf, old->linelen);
   1419  1.1  jtc 	new->linelen = old->linelen;
   1420  1.1  jtc 	new->cursor = old->cursor;
   1421  1.1  jtc 	new->winleft = old->winleft;
   1422  1.1  jtc 	free_edstate(old);
   1423  1.1  jtc }
   1424  1.1  jtc 
   1425  1.1  jtc static void
   1426  1.1  jtc free_edstate(old)
   1427  1.1  jtc 	struct edstate *old;
   1428  1.1  jtc {
   1429  1.1  jtc 	afree(old->cbuf, APERM);
   1430  1.1  jtc 	afree((char *)old, APERM);
   1431  1.1  jtc }
   1432  1.1  jtc 
   1433  1.1  jtc 
   1434  1.1  jtc 
   1435  1.1  jtc static void
   1436  1.1  jtc edit_reset(buf, len)
   1437  1.1  jtc 	char	*buf;
   1438  1.1  jtc 	size_t	len;
   1439  1.1  jtc {
   1440  1.1  jtc 	const char *p;
   1441  1.1  jtc 
   1442  1.1  jtc 	es = &ebuf;
   1443  1.1  jtc 	es->cbuf = buf;
   1444  1.1  jtc 	es->cbufsize = len;
   1445  1.1  jtc 	undo = &undobuf;
   1446  1.1  jtc 	undo->cbufsize = len;
   1447  1.1  jtc 
   1448  1.1  jtc 	es->linelen = undo->linelen = 0;
   1449  1.1  jtc 	es->cursor = undo->cursor = 0;
   1450  1.1  jtc 	es->winleft = undo->winleft = 0;
   1451  1.1  jtc 
   1452  1.1  jtc 	cur_col = pwidth = promptlen(prompt, &p);
   1453  1.1  jtc 	prompt_skip = p - prompt;
   1454  1.1  jtc 	if (pwidth > x_cols - 3 - MIN_EDIT_SPACE) {
   1455  1.1  jtc 		cur_col = x_cols - 3 - MIN_EDIT_SPACE;
   1456  1.1  jtc 		prompt_trunc = pwidth - cur_col;
   1457  1.1  jtc 		pwidth -= prompt_trunc;
   1458  1.1  jtc 	} else
   1459  1.1  jtc 		prompt_trunc = 0;
   1460  1.1  jtc 	if (!wbuf_len || wbuf_len != x_cols - 3) {
   1461  1.1  jtc 		wbuf_len = x_cols - 3;
   1462  1.1  jtc 		wbuf[0] = aresize(wbuf[0], wbuf_len, APERM);
   1463  1.1  jtc 		wbuf[1] = aresize(wbuf[1], wbuf_len, APERM);
   1464  1.1  jtc 	}
   1465  1.1  jtc 	(void) memset(wbuf[0], ' ', wbuf_len);
   1466  1.1  jtc 	(void) memset(wbuf[1], ' ', wbuf_len);
   1467  1.1  jtc 	winwidth = x_cols - pwidth - 3;
   1468  1.1  jtc 	win = 0;
   1469  1.1  jtc 	morec = ' ';
   1470  1.1  jtc 	lastref = 1;
   1471  1.1  jtc 	holdlen = 0;
   1472  1.1  jtc }
   1473  1.1  jtc 
   1474  1.1  jtc static int
   1475  1.1  jtc putbuf(buf, len, repl)
   1476  1.1  jtc 	const char *buf;
   1477  1.1  jtc 	int	len;
   1478  1.1  jtc 	int	repl;
   1479  1.1  jtc {
   1480  1.1  jtc 	if (len == 0)
   1481  1.1  jtc 		return 0;
   1482  1.1  jtc 	if (repl) {
   1483  1.1  jtc 		if (es->cursor + len >= es->cbufsize)
   1484  1.1  jtc 			return -1;
   1485  1.1  jtc 		if (es->cursor + len > es->linelen)
   1486  1.1  jtc 			es->linelen = es->cursor + len;
   1487  1.1  jtc 	} else {
   1488  1.1  jtc 		if (es->linelen + len >= es->cbufsize)
   1489  1.1  jtc 			return -1;
   1490  1.1  jtc 		memmove(&es->cbuf[es->cursor + len], &es->cbuf[es->cursor],
   1491  1.1  jtc 			es->linelen - es->cursor);
   1492  1.1  jtc 		es->linelen += len;
   1493  1.1  jtc 	}
   1494  1.1  jtc 	memmove(&es->cbuf[es->cursor], buf, len);
   1495  1.1  jtc 	es->cursor += len;
   1496  1.1  jtc 	return 0;
   1497  1.1  jtc }
   1498  1.1  jtc 
   1499  1.1  jtc static void
   1500  1.1  jtc del_range(a, b)
   1501  1.1  jtc 	int	a, b;
   1502  1.1  jtc {
   1503  1.1  jtc 	if (es->linelen != b)
   1504  1.1  jtc 		memmove(&es->cbuf[a], &es->cbuf[b], es->linelen - b);
   1505  1.1  jtc 	es->linelen -= b - a;
   1506  1.1  jtc }
   1507  1.1  jtc 
   1508  1.1  jtc static int
   1509  1.1  jtc findch(ch, cnt, forw, incl)
   1510  1.1  jtc 	int	ch;
   1511  1.1  jtc 	int	cnt;
   1512  1.1  jtc 	int	forw;
   1513  1.1  jtc 	int	incl;
   1514  1.1  jtc {
   1515  1.1  jtc 	int	ncursor;
   1516  1.1  jtc 
   1517  1.1  jtc 	if (es->linelen == 0)
   1518  1.1  jtc 		return -1;
   1519  1.1  jtc 	ncursor = es->cursor;
   1520  1.1  jtc 	while (cnt--) {
   1521  1.1  jtc 		do {
   1522  1.1  jtc 			if (forw) {
   1523  1.1  jtc 				if (++ncursor == es->linelen)
   1524  1.1  jtc 					return -1;
   1525  1.1  jtc 			} else {
   1526  1.1  jtc 				if (--ncursor < 0)
   1527  1.1  jtc 					return -1;
   1528  1.1  jtc 			}
   1529  1.1  jtc 		} while (es->cbuf[ncursor] != ch);
   1530  1.1  jtc 	}
   1531  1.1  jtc 	if (!incl) {
   1532  1.1  jtc 		if (forw)
   1533  1.1  jtc 			ncursor--;
   1534  1.1  jtc 		else
   1535  1.1  jtc 			ncursor++;
   1536  1.1  jtc 	}
   1537  1.1  jtc 	return ncursor;
   1538  1.1  jtc }
   1539  1.1  jtc 
   1540  1.1  jtc static int
   1541  1.1  jtc forwword(argcnt)
   1542  1.1  jtc 	int	argcnt;
   1543  1.1  jtc {
   1544  1.1  jtc 	int	ncursor;
   1545  1.1  jtc 
   1546  1.1  jtc 	ncursor = es->cursor;
   1547  1.1  jtc 	while (ncursor < es->linelen && argcnt--) {
   1548  1.1  jtc 		if (is_wordch(es->cbuf[ncursor]))
   1549  1.1  jtc 			while (is_wordch(es->cbuf[ncursor]) &&
   1550  1.1  jtc 					ncursor < es->linelen)
   1551  1.1  jtc 				ncursor++;
   1552  1.1  jtc 		else if (!isspace(es->cbuf[ncursor]))
   1553  1.1  jtc 			while (!is_wordch(es->cbuf[ncursor]) &&
   1554  1.1  jtc 					!isspace(es->cbuf[ncursor]) &&
   1555  1.1  jtc 					ncursor < es->linelen)
   1556  1.1  jtc 				ncursor++;
   1557  1.1  jtc 		while (isspace(es->cbuf[ncursor]) && ncursor < es->linelen)
   1558  1.1  jtc 			ncursor++;
   1559  1.1  jtc 	}
   1560  1.1  jtc 	return ncursor;
   1561  1.1  jtc }
   1562  1.1  jtc 
   1563  1.1  jtc static int
   1564  1.1  jtc backword(argcnt)
   1565  1.1  jtc 	int	argcnt;
   1566  1.1  jtc {
   1567  1.1  jtc 	int	ncursor;
   1568  1.1  jtc 
   1569  1.1  jtc 	ncursor = es->cursor;
   1570  1.1  jtc 	while (ncursor > 0 && argcnt--) {
   1571  1.1  jtc 		while (--ncursor > 0 && isspace(es->cbuf[ncursor]))
   1572  1.1  jtc 			;
   1573  1.1  jtc 		if (ncursor > 0) {
   1574  1.1  jtc 			if (is_wordch(es->cbuf[ncursor]))
   1575  1.1  jtc 				while (--ncursor >= 0 &&
   1576  1.1  jtc 				   is_wordch(es->cbuf[ncursor]))
   1577  1.1  jtc 					;
   1578  1.1  jtc 			else
   1579  1.1  jtc 				while (--ncursor >= 0 &&
   1580  1.1  jtc 				   !is_wordch(es->cbuf[ncursor]) &&
   1581  1.1  jtc 				   !isspace(es->cbuf[ncursor]))
   1582  1.1  jtc 					;
   1583  1.1  jtc 			ncursor++;
   1584  1.1  jtc 		}
   1585  1.1  jtc 	}
   1586  1.1  jtc 	return ncursor;
   1587  1.1  jtc }
   1588  1.1  jtc 
   1589  1.1  jtc static int
   1590  1.1  jtc endword(argcnt)
   1591  1.1  jtc 	int	argcnt;
   1592  1.1  jtc {
   1593  1.1  jtc 	int	ncursor;
   1594  1.1  jtc 
   1595  1.1  jtc 	ncursor = es->cursor;
   1596  1.1  jtc 	while (ncursor < es->linelen && argcnt--) {
   1597  1.1  jtc 		while (++ncursor < es->linelen - 1 &&
   1598  1.1  jtc 				isspace(es->cbuf[ncursor]))
   1599  1.1  jtc 			;
   1600  1.1  jtc 		if (ncursor < es->linelen - 1) {
   1601  1.1  jtc 			if (is_wordch(es->cbuf[ncursor]))
   1602  1.1  jtc 				while (++ncursor < es->linelen &&
   1603  1.1  jtc 					  is_wordch(es->cbuf[ncursor]))
   1604  1.1  jtc 					;
   1605  1.1  jtc 			else
   1606  1.1  jtc 				while (++ncursor < es->linelen &&
   1607  1.1  jtc 				   !is_wordch(es->cbuf[ncursor]) &&
   1608  1.1  jtc 				   !isspace(es->cbuf[ncursor]))
   1609  1.1  jtc 					;
   1610  1.1  jtc 			ncursor--;
   1611  1.1  jtc 		}
   1612  1.1  jtc 	}
   1613  1.1  jtc 	return ncursor;
   1614  1.1  jtc }
   1615  1.1  jtc 
   1616  1.1  jtc static int
   1617  1.1  jtc Forwword(argcnt)
   1618  1.1  jtc 	int	argcnt;
   1619  1.1  jtc {
   1620  1.1  jtc 	int	ncursor;
   1621  1.1  jtc 
   1622  1.1  jtc 	ncursor = es->cursor;
   1623  1.1  jtc 	while (ncursor < es->linelen && argcnt--) {
   1624  1.1  jtc 		while (!isspace(es->cbuf[ncursor]) && ncursor < es->linelen)
   1625  1.1  jtc 			ncursor++;
   1626  1.1  jtc 		while (isspace(es->cbuf[ncursor]) && ncursor < es->linelen)
   1627  1.1  jtc 			ncursor++;
   1628  1.1  jtc 	}
   1629  1.1  jtc 	return ncursor;
   1630  1.1  jtc }
   1631  1.1  jtc 
   1632  1.1  jtc static int
   1633  1.1  jtc Backword(argcnt)
   1634  1.1  jtc 	int	argcnt;
   1635  1.1  jtc {
   1636  1.1  jtc 	int	ncursor;
   1637  1.1  jtc 
   1638  1.1  jtc 	ncursor = es->cursor;
   1639  1.1  jtc 	while (ncursor > 0 && argcnt--) {
   1640  1.1  jtc 		while (--ncursor >= 0 && isspace(es->cbuf[ncursor]))
   1641  1.1  jtc 			;
   1642  1.1  jtc 		while (ncursor >= 0 && !isspace(es->cbuf[ncursor]))
   1643  1.1  jtc 			ncursor--;
   1644  1.1  jtc 		ncursor++;
   1645  1.1  jtc 	}
   1646  1.1  jtc 	return ncursor;
   1647  1.1  jtc }
   1648  1.1  jtc 
   1649  1.1  jtc static int
   1650  1.1  jtc Endword(argcnt)
   1651  1.1  jtc 	int	argcnt;
   1652  1.1  jtc {
   1653  1.1  jtc 	int	ncursor;
   1654  1.1  jtc 
   1655  1.1  jtc 	ncursor = es->cursor;
   1656  1.1  jtc 	while (ncursor < es->linelen - 1 && argcnt--) {
   1657  1.1  jtc 		while (++ncursor < es->linelen - 1 &&
   1658  1.1  jtc 				isspace(es->cbuf[ncursor]))
   1659  1.1  jtc 			;
   1660  1.1  jtc 		if (ncursor < es->linelen - 1) {
   1661  1.1  jtc 			while (++ncursor < es->linelen &&
   1662  1.1  jtc 					!isspace(es->cbuf[ncursor]))
   1663  1.1  jtc 				;
   1664  1.1  jtc 			ncursor--;
   1665  1.1  jtc 		}
   1666  1.1  jtc 	}
   1667  1.1  jtc 	return ncursor;
   1668  1.1  jtc }
   1669  1.1  jtc 
   1670  1.1  jtc static int
   1671  1.1  jtc grabhist(save, n)
   1672  1.1  jtc 	int	save;
   1673  1.1  jtc 	int	n;
   1674  1.1  jtc {
   1675  1.1  jtc 	char	*hptr;
   1676  1.1  jtc 
   1677  1.1  jtc 	if (n < 0 || n > hlast)
   1678  1.1  jtc 		return -1;
   1679  1.1  jtc 	if (n == hlast) {
   1680  1.1  jtc 		restore_cbuf();
   1681  1.1  jtc 		ohnum = n;
   1682  1.1  jtc 		return 0;
   1683  1.1  jtc 	}
   1684  1.1  jtc 	(void) histnum(n);
   1685  1.1  jtc 	if ((hptr = *histpos()) == NULL) {
   1686  1.1  jtc 		internal_errorf(0, "grabhist: bad history array");
   1687  1.1  jtc 		return -1;
   1688  1.1  jtc 	}
   1689  1.1  jtc 	if (save)
   1690  1.1  jtc 		save_cbuf();
   1691  1.1  jtc 	if ((es->linelen = strlen(hptr)) >= es->cbufsize)
   1692  1.1  jtc 		es->linelen = es->cbufsize - 1;
   1693  1.1  jtc 	memmove(es->cbuf, hptr, es->linelen);
   1694  1.1  jtc 	es->cursor = 0;
   1695  1.1  jtc 	ohnum = n;
   1696  1.1  jtc 	return 0;
   1697  1.1  jtc }
   1698  1.1  jtc 
   1699  1.1  jtc static int
   1700  1.1  jtc grabsearch(save, start, fwd, pat)
   1701  1.1  jtc 	int	save, start, fwd;
   1702  1.1  jtc 	char	*pat;
   1703  1.1  jtc {
   1704  1.1  jtc 	char	*hptr;
   1705  1.1  jtc 	int	hist;
   1706  1.1  jtc 	int	anchored;
   1707  1.1  jtc 
   1708  1.1  jtc 	if ((start == 0 && fwd == 0) || (start >= hlast-1 && fwd == 1))
   1709  1.1  jtc 		return -1;
   1710  1.1  jtc 	if (fwd)
   1711  1.1  jtc 		start++;
   1712  1.1  jtc 	else
   1713  1.1  jtc 		start--;
   1714  1.1  jtc 	anchored = *pat == '^' ? (++pat, 1) : 0;
   1715  1.1  jtc 	if ((hist = findhist(start, fwd, pat, anchored)) < 0) {
   1716  1.1  jtc 		/* if (start != 0 && fwd && match(holdbuf, pat) >= 0) { */
   1717  1.1  jtc 		/* XXX should FILECMP be strncmp? */
   1718  1.1  jtc 		if (start != 0 && fwd && FILECMP(holdbuf, pat) >= 0) {
   1719  1.1  jtc 			restore_cbuf();
   1720  1.1  jtc 			return 0;
   1721  1.1  jtc 		} else
   1722  1.1  jtc 			return -1;
   1723  1.1  jtc 	}
   1724  1.1  jtc 	if (save)
   1725  1.1  jtc 		save_cbuf();
   1726  1.1  jtc 	histnum(hist);
   1727  1.1  jtc 	hptr = *histpos();
   1728  1.1  jtc 	if ((es->linelen = strlen(hptr)) >= es->cbufsize)
   1729  1.1  jtc 		es->linelen = es->cbufsize - 1;
   1730  1.1  jtc 	memmove(es->cbuf, hptr, es->linelen);
   1731  1.1  jtc 	es->cursor = 0;
   1732  1.1  jtc 	return hist;
   1733  1.1  jtc }
   1734  1.1  jtc 
   1735  1.1  jtc static void
   1736  1.1  jtc redraw_line(newline)
   1737  1.1  jtc 	int newline;
   1738  1.1  jtc {
   1739  1.1  jtc 	(void) memset(wbuf[win], ' ', wbuf_len);
   1740  1.1  jtc 	if (newline) {
   1741  1.1  jtc 		x_putc('\r');
   1742  1.1  jtc 		x_putc('\n');
   1743  1.1  jtc 	}
   1744  1.1  jtc 	vi_pprompt(0);
   1745  1.1  jtc 	cur_col = pwidth;
   1746  1.1  jtc 	morec = ' ';
   1747  1.1  jtc }
   1748  1.1  jtc 
   1749  1.1  jtc static void
   1750  1.1  jtc refresh(leftside)
   1751  1.1  jtc 	int		leftside;
   1752  1.1  jtc {
   1753  1.1  jtc 	if (leftside < 0)
   1754  1.1  jtc 		leftside = lastref;
   1755  1.1  jtc 	else
   1756  1.1  jtc 		lastref = leftside;
   1757  1.1  jtc 	if (outofwin())
   1758  1.1  jtc 		rewindow();
   1759  1.1  jtc 	display(wbuf[1 - win], wbuf[win], leftside);
   1760  1.1  jtc 	win = 1 - win;
   1761  1.1  jtc }
   1762  1.1  jtc 
   1763  1.1  jtc static int
   1764  1.1  jtc outofwin()
   1765  1.1  jtc {
   1766  1.1  jtc 	int	cur, col;
   1767  1.1  jtc 
   1768  1.1  jtc 	if (es->cursor < es->winleft)
   1769  1.1  jtc 		return 1;
   1770  1.1  jtc 	col = 0;
   1771  1.1  jtc 	cur = es->winleft;
   1772  1.1  jtc 	while (cur < es->cursor)
   1773  1.1  jtc 		col = newcol((unsigned char) es->cbuf[cur++], col);
   1774  1.1  jtc 	if (col >= winwidth)
   1775  1.1  jtc 		return 1;
   1776  1.1  jtc 	return 0;
   1777  1.1  jtc }
   1778  1.1  jtc 
   1779  1.1  jtc static void
   1780  1.1  jtc rewindow()
   1781  1.1  jtc {
   1782  1.1  jtc 	register int	tcur, tcol;
   1783  1.1  jtc 	int		holdcur1, holdcol1;
   1784  1.1  jtc 	int		holdcur2, holdcol2;
   1785  1.1  jtc 
   1786  1.1  jtc 	holdcur1 = holdcur2 = tcur = 0;
   1787  1.1  jtc 	holdcol1 = holdcol2 = tcol = 0;
   1788  1.1  jtc 	while (tcur < es->cursor) {
   1789  1.1  jtc 		if (tcol - holdcol2 > winwidth / 2) {
   1790  1.1  jtc 			holdcur1 = holdcur2;
   1791  1.1  jtc 			holdcol1 = holdcol2;
   1792  1.1  jtc 			holdcur2 = tcur;
   1793  1.1  jtc 			holdcol2 = tcol;
   1794  1.1  jtc 		}
   1795  1.1  jtc 		tcol = newcol((unsigned char) es->cbuf[tcur++], tcol);
   1796  1.1  jtc 	}
   1797  1.1  jtc 	while (tcol - holdcol1 > winwidth / 2)
   1798  1.1  jtc 		holdcol1 = newcol((unsigned char) es->cbuf[holdcur1++],
   1799  1.1  jtc 				  holdcol1);
   1800  1.1  jtc 	es->winleft = holdcur1;
   1801  1.1  jtc }
   1802  1.1  jtc 
   1803  1.1  jtc static int
   1804  1.1  jtc newcol(ch, col)
   1805  1.1  jtc 	int	ch, col;
   1806  1.1  jtc {
   1807  1.1  jtc 	if (ch == '\t')
   1808  1.1  jtc 		return (col | 7) + 1;
   1809  1.1  jtc 	return col + char_len(ch);
   1810  1.1  jtc }
   1811  1.1  jtc 
   1812  1.1  jtc static void
   1813  1.1  jtc display(wb1, wb2, leftside)
   1814  1.1  jtc 	char	*wb1, *wb2;
   1815  1.1  jtc 	int	leftside;
   1816  1.1  jtc {
   1817  1.1  jtc 	unsigned char ch;
   1818  1.1  jtc 	char	*twb1, *twb2, mc;
   1819  1.1  jtc 	int	cur, col, cnt;
   1820  1.1  jtc 	int	UNINITIALIZED(ncol);
   1821  1.1  jtc 	int	moreright;
   1822  1.1  jtc 
   1823  1.1  jtc 	col = 0;
   1824  1.1  jtc 	cur = es->winleft;
   1825  1.1  jtc 	moreright = 0;
   1826  1.1  jtc 	twb1 = wb1;
   1827  1.1  jtc 	while (col < winwidth && cur < es->linelen) {
   1828  1.1  jtc 		if (cur == es->cursor && leftside)
   1829  1.1  jtc 			ncol = col + pwidth;
   1830  1.1  jtc 		if ((ch = es->cbuf[cur]) == '\t') {
   1831  1.1  jtc 			do {
   1832  1.1  jtc 				*twb1++ = ' ';
   1833  1.1  jtc 			} while (++col < winwidth && (col & 7) != 0);
   1834  1.1  jtc 		} else {
   1835  1.1  jtc 			if ((ch & 0x80) && Flag(FVISHOW8)) {
   1836  1.1  jtc 				*twb1++ = 'M';
   1837  1.1  jtc 				if (++col < winwidth) {
   1838  1.1  jtc 					*twb1++ = '-';
   1839  1.1  jtc 					col++;
   1840  1.1  jtc 				}
   1841  1.1  jtc 				ch &= 0x7f;
   1842  1.1  jtc 			}
   1843  1.1  jtc 			if (col < winwidth) {
   1844  1.1  jtc 				if (ch < ' ' || ch == 0x7f) {
   1845  1.1  jtc 					*twb1++ = '^';
   1846  1.1  jtc 					if (++col < winwidth) {
   1847  1.2  tls 						*twb1++ = ch ^ '@';
   1848  1.1  jtc 						col++;
   1849  1.1  jtc 					}
   1850  1.1  jtc 				} else {
   1851  1.2  tls 					*twb1++ = ch;
   1852  1.1  jtc 					col++;
   1853  1.1  jtc 				}
   1854  1.1  jtc 			}
   1855  1.1  jtc 		}
   1856  1.1  jtc 		if (cur == es->cursor && !leftside)
   1857  1.1  jtc 			ncol = col + pwidth - 1;
   1858  1.1  jtc 		cur++;
   1859  1.1  jtc 	}
   1860  1.1  jtc 	if (cur == es->cursor)
   1861  1.1  jtc 		ncol = col + pwidth;
   1862  1.1  jtc 	if (col < winwidth) {
   1863  1.1  jtc 		while (col < winwidth) {
   1864  1.1  jtc 			*twb1++ = ' ';
   1865  1.1  jtc 			col++;
   1866  1.1  jtc 		}
   1867  1.1  jtc 	} else
   1868  1.1  jtc 		moreright++;
   1869  1.1  jtc 	*twb1 = ' ';
   1870  1.1  jtc 
   1871  1.1  jtc 	col = pwidth;
   1872  1.1  jtc 	cnt = winwidth;
   1873  1.1  jtc 	twb1 = wb1;
   1874  1.1  jtc 	twb2 = wb2;
   1875  1.1  jtc 	while (cnt--) {
   1876  1.1  jtc 		if (*twb1 != *twb2) {
   1877  1.1  jtc 			if (cur_col != col)
   1878  1.1  jtc 				ed_mov_opt(col, wb1);
   1879  1.1  jtc 			x_putc(*twb1);
   1880  1.1  jtc 			cur_col++;
   1881  1.1  jtc 		}
   1882  1.1  jtc 		twb1++;
   1883  1.1  jtc 		twb2++;
   1884  1.1  jtc 		col++;
   1885  1.1  jtc 	}
   1886  1.1  jtc 	if (es->winleft > 0 && moreright)
   1887  1.1  jtc 		/* POSIX says to use * for this but that is a globbing
   1888  1.1  jtc 		 * character and may confuse people; + is more innocuous
   1889  1.1  jtc 		 */
   1890  1.1  jtc 		mc = '+';
   1891  1.1  jtc 	else if (es->winleft > 0)
   1892  1.1  jtc 		mc = '<';
   1893  1.1  jtc 	else if (moreright)
   1894  1.1  jtc 		mc = '>';
   1895  1.1  jtc 	else
   1896  1.1  jtc 		mc = ' ';
   1897  1.1  jtc 	if (mc != morec) {
   1898  1.1  jtc 		ed_mov_opt(pwidth + winwidth + 1, wb1);
   1899  1.1  jtc 		x_putc(mc);
   1900  1.1  jtc 		cur_col++;
   1901  1.1  jtc 		morec = mc;
   1902  1.1  jtc 	}
   1903  1.1  jtc 	if (cur_col != ncol)
   1904  1.1  jtc 		ed_mov_opt(ncol, wb1);
   1905  1.1  jtc }
   1906  1.1  jtc 
   1907  1.1  jtc static void
   1908  1.1  jtc ed_mov_opt(col, wb)
   1909  1.1  jtc 	int	col;
   1910  1.1  jtc 	char	*wb;
   1911  1.1  jtc {
   1912  1.1  jtc 	if (col < cur_col) {
   1913  1.1  jtc 		if (col + 1 < cur_col - col) {
   1914  1.1  jtc 			x_putc('\r');
   1915  1.1  jtc 			vi_pprompt(0);
   1916  1.1  jtc 			cur_col = pwidth;
   1917  1.1  jtc 			while (cur_col++ < col)
   1918  1.1  jtc 				x_putc(*wb++);
   1919  1.1  jtc 		} else {
   1920  1.1  jtc 			while (cur_col-- > col)
   1921  1.1  jtc 				x_putc('\b');
   1922  1.1  jtc 		}
   1923  1.1  jtc 	} else {
   1924  1.1  jtc 		wb = &wb[cur_col - pwidth];
   1925  1.1  jtc 		while (cur_col++ < col)
   1926  1.1  jtc 			x_putc(*wb++);
   1927  1.1  jtc 	}
   1928  1.1  jtc 	cur_col = col;
   1929  1.1  jtc }
   1930  1.1  jtc 
   1931  1.1  jtc 
   1932  1.1  jtc /* replace word with all expansions (ie, expand word*) */
   1933  1.1  jtc static int
   1934  1.1  jtc expand_word(command)
   1935  1.1  jtc 	int command;
   1936  1.1  jtc {
   1937  1.1  jtc 	static struct edstate *buf;
   1938  1.1  jtc 	int rval = 0;
   1939  1.1  jtc 	int nwords;
   1940  1.1  jtc 	int start, end;
   1941  1.1  jtc 	char **words;
   1942  1.1  jtc 	int i;
   1943  1.1  jtc 
   1944  1.1  jtc 	/* Undo previous expansion */
   1945  1.1  jtc 	if (command == 0 && expanded == EXPAND && buf) {
   1946  1.1  jtc 		restore_edstate(es, buf);
   1947  1.1  jtc 		buf = 0;
   1948  1.1  jtc 		expanded = NONE;
   1949  1.1  jtc 		return 0;
   1950  1.1  jtc 	}
   1951  1.1  jtc 	if (buf) {
   1952  1.1  jtc 		free_edstate(buf);
   1953  1.1  jtc 		buf = 0;
   1954  1.1  jtc 	}
   1955  1.1  jtc 
   1956  1.1  jtc 	nwords = x_cf_glob(XCF_COMMAND_FILE|XCF_FULLPATH,
   1957  1.1  jtc 		es->cbuf, es->linelen, es->cursor,
   1958  1.1  jtc 		&start, &end, &words, (int *) 0);
   1959  1.1  jtc 	if (nwords == 0) {
   1960  1.1  jtc 		vi_error();
   1961  1.1  jtc 		return -1;
   1962  1.1  jtc 	}
   1963  1.1  jtc 
   1964  1.1  jtc 	buf = save_edstate(es);
   1965  1.1  jtc 	expanded = EXPAND;
   1966  1.1  jtc 	del_range(start, end);
   1967  1.1  jtc 	es->cursor = start;
   1968  1.1  jtc 	for (i = 0; i < nwords; ) {
   1969  1.1  jtc 		if (putbuf(words[i], (int) strlen(words[i]), 0) != 0) {
   1970  1.1  jtc 			rval = -1;
   1971  1.1  jtc 			break;
   1972  1.1  jtc 		}
   1973  1.1  jtc 		if (++i < nwords && putbuf(space, 1, 0) != 0) {
   1974  1.1  jtc 			rval = -1;
   1975  1.1  jtc 			break;
   1976  1.1  jtc 		}
   1977  1.1  jtc 	}
   1978  1.1  jtc 	i = buf->cursor - end;
   1979  1.1  jtc 	if (rval == 0 && i > 0)
   1980  1.1  jtc 		es->cursor += i;
   1981  1.1  jtc 	modified = 1; hnum = hlast;
   1982  1.1  jtc 	insert = INSERT;
   1983  1.1  jtc 	lastac = 0;
   1984  1.1  jtc 	refresh(0);
   1985  1.1  jtc 	return rval;
   1986  1.1  jtc }
   1987  1.1  jtc 
   1988  1.1  jtc static int
   1989  1.1  jtc complete_word(command, count)
   1990  1.1  jtc 	int command;
   1991  1.1  jtc 	int count;
   1992  1.1  jtc {
   1993  1.1  jtc 	static struct edstate *buf;
   1994  1.1  jtc 	int rval = 0;
   1995  1.1  jtc 	int nwords;
   1996  1.1  jtc 	int start, end;
   1997  1.1  jtc 	char **words;
   1998  1.1  jtc 	char *match;
   1999  1.1  jtc 	int match_len;
   2000  1.1  jtc 	int is_unique;
   2001  1.1  jtc 	int is_command;
   2002  1.1  jtc 
   2003  1.1  jtc 	/* Undo previous completion */
   2004  1.1  jtc 	if (command == 0 && expanded == COMPLETE && buf) {
   2005  1.1  jtc 		print_expansions(buf, 0);
   2006  1.1  jtc 		expanded = PRINT;
   2007  1.1  jtc 		return 0;
   2008  1.1  jtc 	}
   2009  1.1  jtc 	if (command == 0 && expanded == PRINT && buf) {
   2010  1.1  jtc 		restore_edstate(es, buf);
   2011  1.1  jtc 		buf = 0;
   2012  1.1  jtc 		expanded = NONE;
   2013  1.1  jtc 		return 0;
   2014  1.1  jtc 	}
   2015  1.1  jtc 	if (buf) {
   2016  1.1  jtc 		free_edstate(buf);
   2017  1.1  jtc 		buf = 0;
   2018  1.1  jtc 	}
   2019  1.1  jtc 
   2020  1.1  jtc 	/* XCF_FULLPATH for count 'cause the menu printed by print_expansions()
   2021  1.1  jtc 	 * was done this way.
   2022  1.1  jtc 	 */
   2023  1.1  jtc 	nwords = x_cf_glob(XCF_COMMAND_FILE | (count ? XCF_FULLPATH : 0),
   2024  1.1  jtc 		es->cbuf, es->linelen, es->cursor,
   2025  1.1  jtc 		&start, &end, &words, &is_command);
   2026  1.1  jtc 	if (nwords == 0) {
   2027  1.1  jtc 		vi_error();
   2028  1.1  jtc 		return -1;
   2029  1.1  jtc 	}
   2030  1.1  jtc 	if (count) {
   2031  1.1  jtc 		int i;
   2032  1.1  jtc 
   2033  1.1  jtc 		count--;
   2034  1.1  jtc 		if (count >= nwords) {
   2035  1.1  jtc 			vi_error();
   2036  1.1  jtc 			x_print_expansions(nwords, words, is_command);
   2037  1.1  jtc 			x_free_words(nwords, words);
   2038  1.1  jtc 			redraw_line(0);
   2039  1.1  jtc 			return -1;
   2040  1.1  jtc 		}
   2041  1.1  jtc 		/*
   2042  1.1  jtc 		 * Expand the count'th word to its basename
   2043  1.1  jtc 		 */
   2044  1.1  jtc 		if (is_command) {
   2045  1.1  jtc 			match = words[count]
   2046  1.1  jtc 				+ x_basename(words[count], (char *) 0);
   2047  1.1  jtc 			/* If more than one possible match, use full path */
   2048  1.1  jtc 			for (i = 0; i < nwords; i++)
   2049  1.1  jtc 				if (i != count &&
   2050  1.1  jtc 				    FILECMP(words[i]
   2051  1.1  jtc 					    + x_basename(words[i], (char *) 0),
   2052  1.1  jtc 					    match) == 0)
   2053  1.1  jtc 				{
   2054  1.1  jtc 					match = words[count];
   2055  1.1  jtc 					break;
   2056  1.1  jtc 				}
   2057  1.1  jtc 		} else
   2058  1.1  jtc 			match = words[count];
   2059  1.1  jtc 		match_len = strlen(match);
   2060  1.1  jtc 		is_unique = 1;
   2061  1.1  jtc 		/* expanded = PRINT;	next call undo */
   2062  1.1  jtc 	} else {
   2063  1.1  jtc 		match = words[0];
   2064  1.1  jtc 		match_len = x_longest_prefix(nwords, words);
   2065  1.1  jtc 		expanded = COMPLETE;	/* next call will list completions */
   2066  1.1  jtc 		is_unique = nwords == 1;
   2067  1.1  jtc 	}
   2068  1.1  jtc 
   2069  1.1  jtc 	buf = save_edstate(es);
   2070  1.1  jtc 	del_range(start, end);
   2071  1.1  jtc 	es->cursor = start;
   2072  1.1  jtc 	if (putbuf(match, match_len, 0) != 0)
   2073  1.1  jtc 		rval = -1;
   2074  1.1  jtc 	else if (is_unique) {
   2075  1.1  jtc 		/* If exact match, don't undo.  Allows directory completions
   2076  1.1  jtc 		 * to be used (ie, complete the next portion of the path).
   2077  1.1  jtc 		 */
   2078  1.1  jtc 		expanded = NONE;
   2079  1.1  jtc 
   2080  1.1  jtc 		/* If not a directory, add a space to the end... */
   2081  1.1  jtc 		if (match_len > 0 && !ISDIRSEP(match[match_len - 1]))
   2082  1.1  jtc 			rval = putbuf(space, 1, 0);
   2083  1.1  jtc 	}
   2084  1.1  jtc 	x_free_words(nwords, words);
   2085  1.1  jtc 
   2086  1.1  jtc 	modified = 1; hnum = hlast;
   2087  1.1  jtc 	insert = INSERT;
   2088  1.1  jtc 	lastac = 0;	 /* prevent this from being redone... */
   2089  1.1  jtc 	refresh(0);
   2090  1.1  jtc 
   2091  1.1  jtc 	return rval;
   2092  1.1  jtc }
   2093  1.1  jtc 
   2094  1.1  jtc static int
   2095  1.1  jtc print_expansions(e, command)
   2096  1.1  jtc 	struct edstate *e;
   2097  1.1  jtc 	int	command;
   2098  1.1  jtc {
   2099  1.1  jtc 	int nwords;
   2100  1.1  jtc 	int start, end;
   2101  1.1  jtc 	char **words;
   2102  1.1  jtc 	int is_command;
   2103  1.1  jtc 
   2104  1.1  jtc 	nwords = x_cf_glob(XCF_COMMAND_FILE|XCF_FULLPATH,
   2105  1.1  jtc 		e->cbuf, e->linelen, e->cursor,
   2106  1.1  jtc 		&start, &end, &words, &is_command);
   2107  1.1  jtc 	if (nwords == 0) {
   2108  1.1  jtc 		vi_error();
   2109  1.1  jtc 		return -1;
   2110  1.1  jtc 	}
   2111  1.1  jtc 	x_print_expansions(nwords, words, is_command);
   2112  1.1  jtc 	x_free_words(nwords, words);
   2113  1.1  jtc 	redraw_line(0);
   2114  1.1  jtc 	return 0;
   2115  1.1  jtc }
   2116  1.1  jtc 
   2117  1.1  jtc /* How long is char when displayed (not counting tabs) */
   2118  1.1  jtc static int
   2119  1.1  jtc char_len(c)
   2120  1.1  jtc 	int c;
   2121  1.1  jtc {
   2122  1.1  jtc 	int len = 1;
   2123  1.1  jtc 
   2124  1.1  jtc 	if ((c & 0x80) && Flag(FVISHOW8)) {
   2125  1.1  jtc 		len += 2;
   2126  1.1  jtc 		c &= 0x7f;
   2127  1.1  jtc 	}
   2128  1.1  jtc 	if (c < ' ' || c == 0x7f)
   2129  1.1  jtc 		len++;
   2130  1.1  jtc 	return len;
   2131  1.1  jtc }
   2132  1.1  jtc 
   2133  1.1  jtc /* Similar to x_zotc(emacs.c), but no tab wierdness */
   2134  1.1  jtc static void
   2135  1.1  jtc x_vi_zotc(c)
   2136  1.1  jtc 	int c;
   2137  1.1  jtc {
   2138  1.1  jtc 	if (Flag(FVISHOW8) && (c & 0x80)) {
   2139  1.1  jtc 		x_puts("M-");
   2140  1.1  jtc 		c &= 0x7f;
   2141  1.1  jtc 	}
   2142  1.1  jtc 	if (c < ' ' || c == 0x7f) {
   2143  1.1  jtc 		x_putc('^');
   2144  1.1  jtc 		c ^= '@';
   2145  1.1  jtc 	}
   2146  1.1  jtc 	x_putc(c);
   2147  1.1  jtc }
   2148  1.1  jtc 
   2149  1.1  jtc static void
   2150  1.1  jtc vi_pprompt(full)
   2151  1.1  jtc 	int full;
   2152  1.1  jtc {
   2153  1.1  jtc 	pprompt(prompt + (full ? 0 : prompt_skip), prompt_trunc);
   2154  1.1  jtc }
   2155  1.1  jtc 
   2156  1.1  jtc static void
   2157  1.1  jtc vi_error()
   2158  1.1  jtc {
   2159  1.1  jtc 	/* Beem out of any macros as soon as an error occurs */
   2160  1.1  jtc 	vi_macro_reset();
   2161  1.1  jtc 	x_putc(BEL);
   2162  1.1  jtc 	x_flush();
   2163  1.1  jtc }
   2164  1.1  jtc 
   2165  1.1  jtc static void
   2166  1.1  jtc vi_macro_reset()
   2167  1.1  jtc {
   2168  1.1  jtc 	if (macro.p) {
   2169  1.1  jtc 		afree(macro.buf, APERM);
   2170  1.1  jtc 		memset((char *) &macro, 0, sizeof(macro));
   2171  1.1  jtc 	}
   2172  1.1  jtc }
   2173  1.1  jtc 
   2174  1.1  jtc #endif	/* VI */
   2175