17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5bbfd0aacf * Common Development and Distribution License (the "License").
6bbfd0aacf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21f6db9f2cf/*
22bbfd0aacf * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23f6db9f2cf * Use is subject to license terms.
24f6db9f2cf */
25f6db9f2cf
267c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/* Copyright (c) 1981 Regents of the University of California */
31f6db9f2cf
327c478bdstevel@tonic-gate#include "ex.h"
337c478bdstevel@tonic-gate#include "ex_tty.h"
347c478bdstevel@tonic-gate#include "ex_vis.h"
357c478bdstevel@tonic-gate#ifndef PRESUNEUC
367c478bdstevel@tonic-gate#include <wctype.h>
377c478bdstevel@tonic-gate/* Undef putchar/getchar if they're defined. */
387c478bdstevel@tonic-gate#ifdef putchar
397c478bdstevel@tonic-gate#	undef putchar
407c478bdstevel@tonic-gate#endif
417c478bdstevel@tonic-gate#ifdef getchar
427c478bdstevel@tonic-gate#	undef getchar
437c478bdstevel@tonic-gate#endif
447c478bdstevel@tonic-gate#endif /* PRESUNEUC */
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate/*
477c478bdstevel@tonic-gate * This is the main routine for visual.
487c478bdstevel@tonic-gate * We here decode the count and possible named buffer specification
497c478bdstevel@tonic-gate * preceding a command and interpret a few of the commands.
507c478bdstevel@tonic-gate * Commands which involve a target (i.e. an operator) are decoded
517c478bdstevel@tonic-gate * in the routine operate in ex_voperate.c.
527c478bdstevel@tonic-gate */
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate#define	forbid(a)	{ if (a) goto fonfon; }
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gateextern int windowchg;
577c478bdstevel@tonic-gateextern int sigok;
58bbfd0aacf#ifdef XPG6
59bbfd0aacfint redisplay;	/* XPG6 assertion 313 [count]r\n :  Also used in ex_vops2.c */
60bbfd0aacf#endif
617c478bdstevel@tonic-gatevoid redraw(), windowinit();
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate#ifdef XPG4
647c478bdstevel@tonic-gateextern int P_cursor_offset;
657c478bdstevel@tonic-gate#endif
667c478bdstevel@tonic-gate
67f6db9f2cfvoid
68f6db9f2cfvmain(void)
697c478bdstevel@tonic-gate{
70f6db9f2cf	int c, cnt, i;
717c478bdstevel@tonic-gate	wchar_t esave[TUBECOLS];
727c478bdstevel@tonic-gate	extern wchar_t atube[];
737c478bdstevel@tonic-gate	unsigned char *oglobp;
747c478bdstevel@tonic-gate	short d;
757c478bdstevel@tonic-gate	line *addr;
767c478bdstevel@tonic-gate	int ind, nlput;
777c478bdstevel@tonic-gate	int shouldpo = 0;
787c478bdstevel@tonic-gate	int tag_reset_wrap = 0;
797c478bdstevel@tonic-gate	int onumber, olist, (*OPline)(), (*OPutchar)();
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate	vch_mac = VC_NOTINMAC;
837c478bdstevel@tonic-gate	ixlatctl(0);
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	/*
867c478bdstevel@tonic-gate	 * If we started as a vi command (on the command line)
877c478bdstevel@tonic-gate	 * then go process initial commands (recover, next or tag).
887c478bdstevel@tonic-gate	 */
897c478bdstevel@tonic-gate	if (initev) {
907c478bdstevel@tonic-gate		oglobp = globp;
917c478bdstevel@tonic-gate		globp = initev;
927c478bdstevel@tonic-gate		hadcnt = cnt = 0;
937c478bdstevel@tonic-gate		i = tchng;
947c478bdstevel@tonic-gate		addr = dot;
957c478bdstevel@tonic-gate		goto doinit;
967c478bdstevel@tonic-gate	}
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate	vshowmode("");		/* As a precaution */
997c478bdstevel@tonic-gate	/*
1007c478bdstevel@tonic-gate	 * NB:
1017c478bdstevel@tonic-gate	 *
1027c478bdstevel@tonic-gate	 * The current line is always in the line buffer linebuf,
1037c478bdstevel@tonic-gate	 * and the cursor at the position cursor.  You should do
1047c478bdstevel@tonic-gate	 * a vsave() before moving off the line to make sure the disk
1057c478bdstevel@tonic-gate	 * copy is updated if it has changed, and a getDOT() to get
1067c478bdstevel@tonic-gate	 * the line back if you mung linebuf.  The motion
1077c478bdstevel@tonic-gate	 * routines in ex_vwind.c handle most of this.
1087c478bdstevel@tonic-gate	 */
1097c478bdstevel@tonic-gate	for (;;) {
1107c478bdstevel@tonic-gate		/*
1117c478bdstevel@tonic-gate		 * Decode a visual command.
1127c478bdstevel@tonic-gate		 * First sync the temp file if there has been a reasonable
1137c478bdstevel@tonic-gate		 * amount of change.  Clear state for decoding of next
1147c478bdstevel@tonic-gate		 * command.
1157c478bdstevel@tonic-gate		 */
1167c478bdstevel@tonic-gate		TSYNC();
1177c478bdstevel@tonic-gate		vglobp = 0;
1187c478bdstevel@tonic-gate		vreg = 0;
1197c478bdstevel@tonic-gate		hold = 0;
1207c478bdstevel@tonic-gate		seenprompt = 1;
1217c478bdstevel@tonic-gate		wcursor = 0;
1227c478bdstevel@tonic-gate		Xhadcnt = hadcnt = 0;
1237c478bdstevel@tonic-gate		Xcnt = cnt = 1;
1247c478bdstevel@tonic-gate		splitw = 0;
1257c478bdstevel@tonic-gate		if (i = holdupd && !windowchg) {
1267c478bdstevel@tonic-gate			if (state == VISUAL) {
1277c478bdstevel@tonic-gate				sigok = 1;
1287c478bdstevel@tonic-gate				(void)peekkey();
1297c478bdstevel@tonic-gate				sigok = 0;
1307c478bdstevel@tonic-gate			}
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate			holdupd = 0;
1337c478bdstevel@tonic-gate/*
1347c478bdstevel@tonic-gate			if (LINE(0) < ZERO) {
1357c478bdstevel@tonic-gate				vclear();
1367c478bdstevel@tonic-gate				vcnt = 0;
1377c478bdstevel@tonic-gate				i = 3;
1387c478bdstevel@tonic-gate			}
1397c478bdstevel@tonic-gate*/
1407c478bdstevel@tonic-gate			if (state != VISUAL) {
1417c478bdstevel@tonic-gate				vcnt = 0;
1427c478bdstevel@tonic-gate				vsave();
1437c478bdstevel@tonic-gate				vrepaint(cursor);
1447c478bdstevel@tonic-gate			} else if (i == 3)
1457c478bdstevel@tonic-gate				vredraw(WTOP);
1467c478bdstevel@tonic-gate			else
1477c478bdstevel@tonic-gate				vsync(WTOP);
1487c478bdstevel@tonic-gate			vfixcurs();
1497c478bdstevel@tonic-gate		} else if(windowchg)
1507c478bdstevel@tonic-gate			redraw();
1517c478bdstevel@tonic-gate
152bbfd0aacf#ifdef XPG6
153bbfd0aacf		if (redisplay) {
154bbfd0aacf			/* XPG6 assertion 313 & 254 : after [count]r\n */
155bbfd0aacf			fixdisplay();
156bbfd0aacf		}
157bbfd0aacf		redisplay = 0;
158bbfd0aacf#endif
1597c478bdstevel@tonic-gate		/*
1607c478bdstevel@tonic-gate		 * Gobble up counts and named buffer specifications.
1617c478bdstevel@tonic-gate		 */
1627c478bdstevel@tonic-gate		for (;;) {
1637c478bdstevel@tonic-gatelooptop:
1647c478bdstevel@tonic-gate#ifdef MDEBUG
1657c478bdstevel@tonic-gate			if (trace)
1667c478bdstevel@tonic-gate				fprintf(trace, "pc=%c",peekkey());
1677c478bdstevel@tonic-gate#endif
1687c478bdstevel@tonic-gate			sigok = 1;
1697c478bdstevel@tonic-gate			c = peekkey();
1707c478bdstevel@tonic-gate			sigok = 0;
1717c478bdstevel@tonic-gate			if (isdigit(peekkey()) && peekkey() != '0') {
1727c478bdstevel@tonic-gate				hadcnt = 1;
1737c478bdstevel@tonic-gate				cnt = vgetcnt();
1747c478bdstevel@tonic-gate				forbid (cnt <= 0);
1757c478bdstevel@tonic-gate			}
1767c478bdstevel@tonic-gate			if (peekkey() != '"')
1777c478bdstevel@tonic-gate				break;
1787c478bdstevel@tonic-gate			(void)getkey(), c = getkey();
1797c478bdstevel@tonic-gate			/*
1807c478bdstevel@tonic-gate			 * Buffer names be letters or digits.
1817c478bdstevel@tonic-gate			 * But not '0' as that is the source of
1827c478bdstevel@tonic-gate			 * an 'empty' named buffer spec in the routine
1837c478bdstevel@tonic-gate			 * kshift (see ex_temp.c).
1847c478bdstevel@tonic-gate			 */
1857c478bdstevel@tonic-gate			if(!isascii(c) && MB_CUR_MAX > 1) {
1867c478bdstevel@tonic-gate				/* get rest of character */
1877c478bdstevel@tonic-gate				wchar_t wchar;
1887c478bdstevel@tonic-gate				char multic[MULTI_BYTE_MAX];
1897c478bdstevel@tonic-gate				ungetkey(c);
1907c478bdstevel@tonic-gate				(void)_mbftowc(multic, &wchar, getkey, &Peekkey);
1917c478bdstevel@tonic-gate			}
1927c478bdstevel@tonic-gate			forbid (c == '0' || !isalpha(c) && !isascii(c) && !isdigit(c));
1937c478bdstevel@tonic-gate			vreg = c;
1947c478bdstevel@tonic-gate		}
1957c478bdstevel@tonic-gatereread:
1967c478bdstevel@tonic-gate		/*
1977c478bdstevel@tonic-gate		 * Come to reread from below after some macro expansions.
1987c478bdstevel@tonic-gate		 * The call to map allows use of function key pads
1997c478bdstevel@tonic-gate		 * by performing a terminal dependent mapping of inputs.
2007c478bdstevel@tonic-gate		 */
2017c478bdstevel@tonic-gate#ifdef MDEBUG
2027c478bdstevel@tonic-gate		if (trace)
2037c478bdstevel@tonic-gate			fprintf(trace,"pcb=%c,",peekkey());
2047c478bdstevel@tonic-gate#endif
2057c478bdstevel@tonic-gate		op = getkey();
2067c478bdstevel@tonic-gate		maphopcnt = 0;
2077c478bdstevel@tonic-gate		do {
2087c478bdstevel@tonic-gate			/*
2097c478bdstevel@tonic-gate			 * Keep mapping the char as long as it changes.
2107c478bdstevel@tonic-gate			 * This allows for double mappings, e.g., q to #,
2117c478bdstevel@tonic-gate			 * #1 to something else.
2127c478bdstevel@tonic-gate			 */
2137c478bdstevel@tonic-gate			c = op;
214f6db9f2cf			op = map(c, arrows, 0);
2157c478bdstevel@tonic-gate#ifdef MDEBUG
2167c478bdstevel@tonic-gate			if (trace)
2177c478bdstevel@tonic-gate				fprintf(trace,"pca=%c,",c);
2187c478bdstevel@tonic-gate#endif
2197c478bdstevel@tonic-gate			/*
2207c478bdstevel@tonic-gate			 * Maybe the mapped to char is a count. If so, we have
2217c478bdstevel@tonic-gate			 * to go back to the "for" to interpret it. Likewise
2227c478bdstevel@tonic-gate			 * for a buffer name.
2237c478bdstevel@tonic-gate			 */
2247c478bdstevel@tonic-gate			if ((isdigit(c) && c!='0') || c == '"') {
2257c478bdstevel@tonic-gate				ungetkey(c);
2267c478bdstevel@tonic-gate				goto looptop;
2277c478bdstevel@tonic-gate			}
2287c478bdstevel@tonic-gate			if (!value(vi_REMAP)) {
2297c478bdstevel@tonic-gate				c = op;
2307c478bdstevel@tonic-gate				break;
2317c478bdstevel@tonic-gate			}
2327c478bdstevel@tonic-gate			if (++maphopcnt > 256)
2337c478bdstevel@tonic-gate				error(gettext("Infinite macro loop"));
2347c478bdstevel@tonic-gate		} while (c != op);
2357c478bdstevel@tonic-gate
2367c478bdstevel@tonic-gate		/*
2377c478bdstevel@tonic-gate		 * Begin to build an image of this command for possible
2387c478bdstevel@tonic-gate		 * later repeat in the buffer workcmd.  It will be copied
2397c478bdstevel@tonic-gate		 * to lastcmd by the routine setLAST
2407c478bdstevel@tonic-gate		 * if/when completely specified.
2417c478bdstevel@tonic-gate		 */
2427c478bdstevel@tonic-gate		lastcp = workcmd;
2437c478bdstevel@tonic-gate		if (!vglobp)
2447c478bdstevel@tonic-gate			*lastcp++ = c;
2457c478bdstevel@tonic-gate
2467c478bdstevel@tonic-gate		/*
2477c478bdstevel@tonic-gate		 * First level command decode.
2487c478bdstevel@tonic-gate		 */
2497c478bdstevel@tonic-gate		switch (c) {
2507c478bdstevel@tonic-gate
2517c478bdstevel@tonic-gate		/*
2527c478bdstevel@tonic-gate		 * ^L		Clear screen e.g. after transmission error.
2537c478bdstevel@tonic-gate		 */
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate		/*
2567c478bdstevel@tonic-gate		 * ^R		Retype screen, getting rid of @ lines.
2577c478bdstevel@tonic-gate		 *		If in open, equivalent to ^L.
2587c478bdstevel@tonic-gate		 *		On terminals where the right arrow key sends
2597c478bdstevel@tonic-gate		 *		^L we make ^R act like ^L, since there is no
2607c478bdstevel@tonic-gate		 *		way to get ^L.  These terminals (adm31, tvi)
2617c478bdstevel@tonic-gate		 *		are intelligent so ^R is useless.  Soroc
2627c478bdstevel@tonic-gate		 *		will probably foul this up, but nobody has
2637c478bdstevel@tonic-gate		 *		one of them.
2647c478bdstevel@tonic-gate		 */
2657c478bdstevel@tonic-gate		case CTRL('l'):
2667c478bdstevel@tonic-gate		case CTRL('r'):
2677c478bdstevel@tonic-gate			if (c == CTRL('l') || (key_right && *key_right==CTRL('l'))) {
2687c478bdstevel@tonic-gate				vclear();
2697c478bdstevel@tonic-gate				vdirty(0, vcnt);
2707c478bdstevel@tonic-gate			}
2717c478bdstevel@tonic-gate			if (state != VISUAL) {
2727c478bdstevel@tonic-gate				/*
2737c478bdstevel@tonic-gate				 * Get a clean line, throw away the
2747c478bdstevel@tonic-gate				 * memory of what is displayed now,
2757c478bdstevel@tonic-gate				 * and move back onto the current line.
2767c478bdstevel@tonic-gate				 */
2777c478bdstevel@tonic-gate				vclean();
2787c478bdstevel@tonic-gate				vcnt = 0;
2797c478bdstevel@tonic-gate				vmoveto(dot, cursor, 0);
2807c478bdstevel@tonic-gate				continue;
2817c478bdstevel@tonic-gate			}
2827c478bdstevel@tonic-gate			vredraw(WTOP);
2837c478bdstevel@tonic-gate			/*
2847c478bdstevel@tonic-gate			 * Weird glitch -- when we enter visual
2857c478bdstevel@tonic-gate			 * in a very small window we may end up with
2867c478bdstevel@tonic-gate			 * no lines on the screen because the line
2877c478bdstevel@tonic-gate			 * at the top is too long.  This forces the screen
2887c478bdstevel@tonic-gate			 * to be expanded to make room for it (after
2897c478bdstevel@tonic-gate			 * we have printed @'s ick showing we goofed).
2907c478bdstevel@tonic-gate			 */
2917c478bdstevel@tonic-gate			if (vcnt == 0)
2927c478bdstevel@tonic-gate				vrepaint(cursor);
2937c478bdstevel@tonic-gate			vfixcurs();
2947c478bdstevel@tonic-gate			continue;
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate		/*
2977c478bdstevel@tonic-gate		 * $		Escape just cancels the current command
2987c478bdstevel@tonic-gate		 *		with a little feedback.
2997c478bdstevel@tonic-gate		 */
3007c478bdstevel@tonic-gate		case ESCAPE:
301f6db9f2cf			(void) beep();
3027c478bdstevel@tonic-gate			continue;
3037c478bdstevel@tonic-gate
3047c478bdstevel@tonic-gate		/*
3057c478bdstevel@tonic-gate		 * @   		Macros. Bring in the macro and put it
3067c478bdstevel@tonic-gate		 *		in vmacbuf, point vglobp there and punt.
3077c478bdstevel@tonic-gate		 */
3087c478bdstevel@tonic-gate		 case '@':
3097c478bdstevel@tonic-gate			c = getesc();
3107c478bdstevel@tonic-gate			if (c == 0)
3117c478bdstevel@tonic-gate				continue;
3127c478bdstevel@tonic-gate			if (c == '@')
3137c478bdstevel@tonic-gate				c = lastmac;
3147c478bdstevel@tonic-gate			if (isupper(c))
3157c478bdstevel@tonic-gate				c = tolower(c);
3167c478bdstevel@tonic-gate			forbid(!islower(c));
3177c478bdstevel@tonic-gate			lastmac = c;
3187c478bdstevel@tonic-gate			vsave();
3197c478bdstevel@tonic-gate			CATCH
3207c478bdstevel@tonic-gate				unsigned char tmpbuf[BUFSIZE];
3217c478bdstevel@tonic-gate
322f6db9f2cf				regbuf(c, tmpbuf, sizeof (vmacbuf));
3237c478bdstevel@tonic-gate				macpush(tmpbuf, 1);
3247c478bdstevel@tonic-gate			ONERR
3257c478bdstevel@tonic-gate				lastmac = 0;
3267c478bdstevel@tonic-gate				splitw = 0;
3277c478bdstevel@tonic-gate				getDOT();
3287c478bdstevel@tonic-gate				vrepaint(cursor);
3297c478bdstevel@tonic-gate				continue;
3307c478bdstevel@tonic-gate			ENDCATCH
3317c478bdstevel@tonic-gate			vmacp = vmacbuf;
3327c478bdstevel@tonic-gate			goto reread;
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate		/*
3357c478bdstevel@tonic-gate		 * .		Repeat the last (modifying) open/visual command.
3367c478bdstevel@tonic-gate		 */
3377c478bdstevel@tonic-gate		case '.':
3387c478bdstevel@tonic-gate			/*
3397c478bdstevel@tonic-gate			 * Check that there was a last command, and
3407c478bdstevel@tonic-gate			 * take its count and named buffer unless they
3417c478bdstevel@tonic-gate			 * were given anew.  Special case if last command
3427c478bdstevel@tonic-gate			 * referenced a numeric named buffer -- increment
3437c478bdstevel@tonic-gate			 * the number and go to a named buffer again.
3447c478bdstevel@tonic-gate			 * This allows a sequence like "1pu.u.u...
3457c478bdstevel@tonic-gate			 * to successively look for stuff in the kill chain
3467c478bdstevel@tonic-gate			 * much as one does in EMACS with C-Y and M-Y.
3477c478bdstevel@tonic-gate			 */
3487c478bdstevel@tonic-gate			forbid (lastcmd[0] == 0);
3497c478bdstevel@tonic-gate			if (hadcnt)
3507c478bdstevel@tonic-gate				lastcnt = cnt;
3517c478bdstevel@tonic-gate			if (vreg)
3527c478bdstevel@tonic-gate				lastreg = vreg;
3537c478bdstevel@tonic-gate			else if (isdigit(lastreg) && lastreg < '9')
3547c478bdstevel@tonic-gate				lastreg++;
3557c478bdstevel@tonic-gate			vreg = lastreg;
3567c478bdstevel@tonic-gate			cnt = lastcnt;
3577c478bdstevel@tonic-gate			hadcnt = lasthad;
3587c478bdstevel@tonic-gate			vglobp = lastcmd;
3597c478bdstevel@tonic-gate			goto reread;
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate		/*
3627c478bdstevel@tonic-gate		 * ^U		Scroll up.  A count sticks around for
3637c478bdstevel@tonic-gate		 *		future scrolls as the scroll amount.
3647c478bdstevel@tonic-gate		 *		Attempt to hold the indentation from the
3657c478bdstevel@tonic-gate		 *		top of the screen (in logical lines).
3667c478bdstevel@tonic-gate		 *
3677c478bdstevel@tonic-gate		 * BUG:		A ^U near the bottom of the screen
3687c478bdstevel@tonic-gate		 *		on a dumb terminal (which can't roll back)
3697c478bdstevel@tonic-gate		 *		causes the screen to be cleared and then
3707c478bdstevel@tonic-gate		 *		redrawn almost as it was.  In this case
3717c478bdstevel@tonic-gate		 *		one should simply move the cursor.
3727c478bdstevel@tonic-gate		 */
3737c478bdstevel@tonic-gate		case CTRL('u'):
3747c478bdstevel@tonic-gate			if (hadcnt)
3757c478bdstevel@tonic-gate				vSCROLL = cnt;
3767c478bdstevel@tonic-gate			cnt = vSCROLL;
3777c478bdstevel@tonic-gate			if (state == VISUAL)
3787c478bdstevel@tonic-gate				ind = vcline, cnt += ind;
3797c478bdstevel@tonic-gate			else
3807c478bdstevel@tonic-gate				ind = 0;
3817c478bdstevel@tonic-gate			vmoving = 0;
3827c478bdstevel@tonic-gate			vup(cnt, ind, 1);
383f6db9f2cf			vnline((unsigned char *)NOSTR);
3847c478bdstevel@tonic-gate			continue;
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate		/*
3877c478bdstevel@tonic-gate		 * ^D		Scroll down.  Like scroll up.
3887c478bdstevel@tonic-gate		 */
3897c478bdstevel@tonic-gate		case CTRL('d'):
3907c478bdstevel@tonic-gate#ifdef TRACE
3917c478bdstevel@tonic-gate		if (trace)
3927c478bdstevel@tonic-gate			fprintf(trace, "before vdown in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol));
3937c478bdstevel@tonic-gate#endif
3947c478bdstevel@tonic-gate			if (hadcnt)
3957c478bdstevel@tonic-gate				vSCROLL = cnt;
3967c478bdstevel@tonic-gate			cnt = vSCROLL;
3977c478bdstevel@tonic-gate			if (state == VISUAL)
3987c478bdstevel@tonic-gate				ind = vcnt - vcline - 1, cnt += ind;
3997c478bdstevel@tonic-gate			else
4007c478bdstevel@tonic-gate				ind = 0;
4017c478bdstevel@tonic-gate			vmoving = 0;
4027c478bdstevel@tonic-gate			vdown(cnt, ind, 1);
4037c478bdstevel@tonic-gate#ifdef TRACE
4047c478bdstevel@tonic-gate		if (trace)
4057c478bdstevel@tonic-gate			fprintf(trace, "before vnline in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol));
4067c478bdstevel@tonic-gate#endif
407f6db9f2cf			vnline((unsigned char *)NOSTR);
4087c478bdstevel@tonic-gate#ifdef TRACE
4097c478bdstevel@tonic-gate		if (trace)
4107c478bdstevel@tonic-gate			fprintf(trace, "after vnline in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol));
4117c478bdstevel@tonic-gate#endif
4127c478bdstevel@tonic-gate			continue;
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate		/*
4157c478bdstevel@tonic-gate		 * ^E		Glitch the screen down (one) line.
4167c478bdstevel@tonic-gate		 *		Cursor left on same line in file.
4177c478bdstevel@tonic-gate		 */
4187c478bdstevel@tonic-gate		case CTRL('e'):
4197c478bdstevel@tonic-gate			if (state != VISUAL)
4207c478bdstevel@tonic-gate				continue;
4217c478bdstevel@tonic-gate			if (!hadcnt)
4227c478bdstevel@tonic-gate				cnt = 1;
4237c478bdstevel@tonic-gate			/* Bottom line of file already on screen */
4247c478bdstevel@tonic-gate			forbid(lineDOL()-lineDOT() <= vcnt-1-vcline);
4257c478bdstevel@tonic-gate			ind = vcnt - vcline - 1 + cnt;
4267c478bdstevel@tonic-gate			vdown(ind, ind, 1);
4277c478bdstevel@tonic-gate			vnline(cursor);
4287c478bdstevel@tonic-gate			continue;
4297c478bdstevel@tonic-gate
4307c478bdstevel@tonic-gate		/*
4317c478bdstevel@tonic-gate		 * ^Y		Like ^E but up
4327c478bdstevel@tonic-gate		 */
4337c478bdstevel@tonic-gate		case CTRL('y'):
4347c478bdstevel@tonic-gate			if (state != VISUAL)
4357c478bdstevel@tonic-gate				continue;
4367c478bdstevel@tonic-gate			if (!hadcnt)
4377c478bdstevel@tonic-gate				cnt = 1;
4387c478bdstevel@tonic-gate			forbid(lineDOT()-1<=vcline); /* line 1 already there */
4397c478bdstevel@tonic-gate			ind = vcline + cnt;
4407c478bdstevel@tonic-gate			vup(ind, ind, 1);
4417c478bdstevel@tonic-gate			vnline(cursor);
4427c478bdstevel@tonic-gate			continue;
4437c478bdstevel@tonic-gate
4447c478bdstevel@tonic-gate
4457c478bdstevel@tonic-gate		/*
4467c478bdstevel@tonic-gate		 * m		Mark position in mark register given
4477c478bdstevel@tonic-gate		 *		by following letter.  Return is
4487c478bdstevel@tonic-gate		 *		accomplished via ' or `; former
4497c478bdstevel@tonic-gate		 *		to beginning of line where mark
4507c478bdstevel@tonic-gate		 *		was set, latter to column where marked.
4517c478bdstevel@tonic-gate		 */
4527c478bdstevel@tonic-gate		case 'm':
4537c478bdstevel@tonic-gate			/*
4547c478bdstevel@tonic-gate			 * Getesc is generally used when a character
4557c478bdstevel@tonic-gate			 * is read as a latter part of a command
4567c478bdstevel@tonic-gate			 * to allow one to hit rubout/escape to cancel
4577c478bdstevel@tonic-gate			 * what you have typed so far.  These characters
4587c478bdstevel@tonic-gate			 * are mapped to 0 by the subroutine.
4597c478bdstevel@tonic-gate			 */
4607c478bdstevel@tonic-gate			c = getesc();
4617c478bdstevel@tonic-gate			if (c == 0)
4627c478bdstevel@tonic-gate				continue;
4637c478bdstevel@tonic-gate
4647c478bdstevel@tonic-gate			/*
4657c478bdstevel@tonic-gate			 * Markreg checks that argument is a letter
4667c478bdstevel@tonic-gate			 * and also maps ' and ` to the end of the range
4677c478bdstevel@tonic-gate			 * to allow '' or `` to reference the previous
4687c478bdstevel@tonic-gate			 * context mark.
4697c478bdstevel@tonic-gate			 */
4707c478bdstevel@tonic-gate			c = markreg(c);
4717c478bdstevel@tonic-gate			forbid (c == 0);
4727c478bdstevel@tonic-gate			vsave();
4737c478bdstevel@tonic-gate			names[c - 'a'] = (*dot &~ 01);
4747c478bdstevel@tonic-gate			ncols[c - 'a'] = cursor;
4757c478bdstevel@tonic-gate			anymarks = 1;
4767c478bdstevel@tonic-gate			continue;
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate		/*
4797c478bdstevel@tonic-gate		 * ^F		Window forwards, with 2 lines of continuity.
4807c478bdstevel@tonic-gate		 *		Count repeats.
4817c478bdstevel@tonic-gate		 */
4827c478bdstevel@tonic-gate		case CTRL('f'):
4837c478bdstevel@tonic-gate			vsave();
4847c478bdstevel@tonic-gate			if (vcnt > 2) {
4857c478bdstevel@tonic-gate				addr = dot + (vcnt - vcline) - 2 + (cnt-1)*basWLINES;
4867c478bdstevel@tonic-gate				forbid(addr > dol);
4877c478bdstevel@tonic-gate				dot = addr;
4887c478bdstevel@tonic-gate				vcnt = vcline = 0;
4897c478bdstevel@tonic-gate			}
4907c478bdstevel@tonic-gate			vzop(0, 0, '+');
4917c478bdstevel@tonic-gate			continue;
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate		/*
4947c478bdstevel@tonic-gate		 * ^B		Window backwards, with 2 lines of continuity.
4957c478bdstevel@tonic-gate		 *		Inverse of ^F.
4967c478bdstevel@tonic-gate		 */
4977c478bdstevel@tonic-gate		case CTRL('b'):
4987c478bdstevel@tonic-gate			vsave();
4997c478bdstevel@tonic-gate			if (one + vcline != dot && vcnt > 2) {
5007c478bdstevel@tonic-gate				addr = dot - vcline + 2 - (cnt-1)*basWLINES;
5017c478bdstevel@tonic-gate				forbid (addr <= zero);
5027c478bdstevel@tonic-gate				dot = addr;
5037c478bdstevel@tonic-gate				vcnt = vcline = 0;
5047c478bdstevel@tonic-gate			}
5057c478bdstevel@tonic-gate			vzop(0, 0, '^');
5067c478bdstevel@tonic-gate			continue;
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate		/*
5097c478bdstevel@tonic-gate		 * z		Screen adjustment, taking a following character:
5107c478bdstevel@tonic-gate		 *			zcarriage_return		current line to top
5117c478bdstevel@tonic-gate		 *			z<NL>		like zcarriage_return
5127c478bdstevel@tonic-gate		 *			z-		current line to bottom
5137c478bdstevel@tonic-gate		 *		also z+, z^ like ^F and ^B.
5147c478bdstevel@tonic-gate		 *		A preceding count is line to use rather
5157c478bdstevel@tonic-gate		 *		than current line.  A count between z and
5167c478bdstevel@tonic-gate		 *		specifier character changes the screen size
5177c478bdstevel@tonic-gate		 *		for the redraw.
5187c478bdstevel@tonic-gate		 *
5197c478bdstevel@tonic-gate		 */
5207c478bdstevel@tonic-gate		case 'z':
5217c478bdstevel@tonic-gate			if (state == VISUAL) {
5227c478bdstevel@tonic-gate				i = vgetcnt();
5237c478bdstevel@tonic-gate				if (i > 0)
5247c478bdstevel@tonic-gate					vsetsiz(i);
5257c478bdstevel@tonic-gate				c = getesc();
5267c478bdstevel@tonic-gate				if (c == 0)
5277c478bdstevel@tonic-gate					continue;
5287c478bdstevel@tonic-gate			}
5297c478bdstevel@tonic-gate			vsave();
5307c478bdstevel@tonic-gate			vzop(hadcnt, cnt, c);
5317c478bdstevel@tonic-gate			continue;
5327c478bdstevel@tonic-gate
5337c478bdstevel@tonic-gate		/*
5347c478bdstevel@tonic-gate		 * Y		Yank lines, abbreviation for y_ or yy.
5357c478bdstevel@tonic-gate		 *		Yanked lines can be put later if no
5367c478bdstevel@tonic-gate		 *		changes intervene, or can be put in named
5377c478bdstevel@tonic-gate		 *		buffers and put anytime in this session.
5387c478bdstevel@tonic-gate		 */
5397c478bdstevel@tonic-gate		case 'Y':
5407c478bdstevel@tonic-gate			ungetkey('_');
5417c478bdstevel@tonic-gate			c = 'y';
5427c478bdstevel@tonic-gate			break;
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gate		/*
5457c478bdstevel@tonic-gate		 * J		Join lines, 2 by default.  Count is number
5467c478bdstevel@tonic-gate		 *		of lines to join (no join operator sorry.)
5477c478bdstevel@tonic-gate		 */
5487c478bdstevel@tonic-gate		case 'J':
5497c478bdstevel@tonic-gate			forbid (dot == dol);
5507c478bdstevel@tonic-gate			if (cnt == 1)
5517c478bdstevel@tonic-gate				cnt = 2;
5527c478bdstevel@tonic-gate			if (cnt > (i = dol - dot + 1))
5537c478bdstevel@tonic-gate				cnt = i;
5547c478bdstevel@tonic-gate			vsave();
5557c478bdstevel@tonic-gate			vmacchng(1);
5567c478bdstevel@tonic-gate			setLAST();
5577c478bdstevel@tonic-gate			cursor = strend(linebuf);
5587c478bdstevel@tonic-gate			vremote(cnt, join, 0);
5597c478bdstevel@tonic-gate			notenam = (unsigned char *)"join";
5607c478bdstevel@tonic-gate			vmoving = 0;
5617c478bdstevel@tonic-gate			killU();
5627c478bdstevel@tonic-gate			vreplace(vcline, cnt, 1);
5637c478bdstevel@tonic-gate			if (!*cursor && cursor > linebuf)
5647c478bdstevel@tonic-gate				cursor--;
5657c478bdstevel@tonic-gate			if (notecnt == 2)
5667c478bdstevel@tonic-gate				notecnt = 0;
5677c478bdstevel@tonic-gate			vrepaint(cursor);
5687c478bdstevel@tonic-gate			continue;
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gate		/*
5717c478bdstevel@tonic-gate		 * S		Substitute text for whole lines, abbrev for c_.
5727c478bdstevel@tonic-gate		 *		Count is number of lines to change.
5737c478bdstevel@tonic-gate		 */
5747c478bdstevel@tonic-gate		case 'S':
5757c478bdstevel@tonic-gate			ungetkey('_');
5767c478bdstevel@tonic-gate			c = 'c';
5777c478bdstevel@tonic-gate			break;
5787c478bdstevel@tonic-gate
5797c478bdstevel@tonic-gate		/*
5807c478bdstevel@tonic-gate		 * O		Create a new line above current and accept new
5817c478bdstevel@tonic-gate		 *		input text, to an escape, there.
5827c478bdstevel@tonic-gate		 *		A count specifies, for dumb terminals when
5837c478bdstevel@tonic-gate		 *		slowopen is not set, the number of physical
5847c478bdstevel@tonic-gate		 *		line space to open on the screen.
5857c478bdstevel@tonic-gate		 *
5867c478bdstevel@tonic-gate		 * o		Like O, but opens lines below.
5877c478bdstevel@tonic-gate		 */
5887c478bdstevel@tonic-gate		case 'O':
5897c478bdstevel@tonic-gate		case 'o':
5907c478bdstevel@tonic-gate			vmacchng(1);
5917c478bdstevel@tonic-gate			voOpen(c, cnt);
5927c478bdstevel@tonic-gate			continue;
5937c478bdstevel@tonic-gate
5947c478bdstevel@tonic-gate		/*
5957c478bdstevel@tonic-gate		 * C		Change text to end of line, short for c$.
5967c478bdstevel@tonic-gate		 */
5977c478bdstevel@tonic-gate		case 'C':
5987c478bdstevel@tonic-gate			if (*cursor) {
5997c478bdstevel@tonic-gate				ungetkey('$'), c = 'c';
6007c478bdstevel@tonic-gate				break;
6017c478bdstevel@tonic-gate			}
6027c478bdstevel@tonic-gate			goto appnd;
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate		/*
6057c478bdstevel@tonic-gate		 * ~	Switch case of letter under cursor
6067c478bdstevel@tonic-gate		 */
6077c478bdstevel@tonic-gate		case '~':
6087c478bdstevel@tonic-gate			{
6097c478bdstevel@tonic-gate				unsigned char mbuf[2049];
6107c478bdstevel@tonic-gate				unsigned char *ccursor = cursor;
6117c478bdstevel@tonic-gate#ifdef PRESUNEUC
6127c478bdstevel@tonic-gate				int tmp, length;
6137c478bdstevel@tonic-gate				wchar_t wchar;
614cebd8f5Toomas Soome				unsigned char tmp1;
6157c478bdstevel@tonic-gate#else
616cebd8f5Toomas Soome				int tmp, len;
6177c478bdstevel@tonic-gate				wchar_t wc;
6187c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6197c478bdstevel@tonic-gate				setLAST();
6207c478bdstevel@tonic-gate				for (tmp = 0; tmp + 3 < 2048; ) {
6217c478bdstevel@tonic-gate				/*
6227c478bdstevel@tonic-gate				 * Use multiple 'r' commands to replace
6237c478bdstevel@tonic-gate				 * alpha with alternate case.
6247c478bdstevel@tonic-gate				 */
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate					if(cnt-- <= 0)
6277c478bdstevel@tonic-gate						break;
6287c478bdstevel@tonic-gate#ifdef PRESUNEUC
6297c478bdstevel@tonic-gate					length = mbtowc(&wchar, (char *)ccursor, MULTI_BYTE_MAX);
6307c478bdstevel@tonic-gate#else
6317c478bdstevel@tonic-gate					len = mbtowc(&wc, (char *)ccursor, MULTI_BYTE_MAX);
6327c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6337c478bdstevel@tonic-gate#ifdef PRESUNEUC
6347c478bdstevel@tonic-gate					if(length > 1) {
6357c478bdstevel@tonic-gate#else
6367c478bdstevel@tonic-gate					if(len > 1 && !iswalpha(wc)) {
6377c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6387c478bdstevel@tonic-gate						mbuf[tmp+0] = ' ';
6397c478bdstevel@tonic-gate						tmp++;
6407c478bdstevel@tonic-gate#ifdef PRESUNEUC
6417c478bdstevel@tonic-gate						ccursor += length;
6427c478bdstevel@tonic-gate#else
6437c478bdstevel@tonic-gate						ccursor += len;
6447c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6457c478bdstevel@tonic-gate						continue;
6467c478bdstevel@tonic-gate					}
6477c478bdstevel@tonic-gate					mbuf[tmp] = 'r';
6487c478bdstevel@tonic-gate#ifdef PRESUNEUC
6497c478bdstevel@tonic-gate					mbuf[tmp+1] = *ccursor++;
6507c478bdstevel@tonic-gate#else
6517c478bdstevel@tonic-gate					ccursor += ((len > 0) ? len : 1);
6527c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6537c478bdstevel@tonic-gate				/*
6547c478bdstevel@tonic-gate				 * If pointing to an alpha character,
6557c478bdstevel@tonic-gate				 * change the case.
6567c478bdstevel@tonic-gate				 */
6577c478bdstevel@tonic-gate
6587c478bdstevel@tonic-gate#ifdef PRESUNEUC
659cebd8f5Toomas Soome					tmp1 = mbuf[tmp+1];
6607c478bdstevel@tonic-gate					if (isupper((unsigned char)tmp1))
6617c478bdstevel@tonic-gate						mbuf[tmp+1] = tolower((unsigned char)tmp1);
6627c478bdstevel@tonic-gate					else
6637c478bdstevel@tonic-gate						mbuf[tmp+1] = toupper((unsigned char)tmp1);
6647c478bdstevel@tonic-gate#else
6657c478bdstevel@tonic-gate					if (iswupper(wc))
6667c478bdstevel@tonic-gate						len = wctomb((char *)(mbuf + tmp + 1),
6677c478bdstevel@tonic-gate							(wc = towlower(wc)));
6687c478bdstevel@tonic-gate					else
6697c478bdstevel@tonic-gate						len = wctomb((char *)(mbuf + tmp + 1),
6707c478bdstevel@tonic-gate							(wc = towupper(wc)));
6717c478bdstevel@tonic-gate					tmp += len - 1;
6727c478bdstevel@tonic-gate#endif /* PRESUNEUC */
6737c478bdstevel@tonic-gate					if(*ccursor)
6747c478bdstevel@tonic-gate				/*
6757c478bdstevel@tonic-gate				 * If at end of line do not advance
6767c478bdstevel@tonic-gate				 * to the next character, else use a
6777c478bdstevel@tonic-gate				 * space to advance 1 column.
6787c478bdstevel@tonic-gate				 */
6797c478bdstevel@tonic-gate						mbuf[tmp+2] = ' ';
6807c478bdstevel@tonic-gate					else {
6817c478bdstevel@tonic-gate						mbuf[tmp+2] = '\0';
6827c478bdstevel@tonic-gate						tmp +=3;
6837c478bdstevel@tonic-gate						break;
6847c478bdstevel@tonic-gate					}
6857c478bdstevel@tonic-gate					tmp += 3;
6867c478bdstevel@tonic-gate				}
6877c478bdstevel@tonic-gate
6887c478bdstevel@tonic-gate				mbuf[tmp] = 0;
6897c478bdstevel@tonic-gate				macpush(mbuf, 1);
6907c478bdstevel@tonic-gate			}
6917c478bdstevel@tonic-gate			continue;
6927c478bdstevel@tonic-gate
6937c478bdstevel@tonic-gate
6947c478bdstevel@tonic-gate		/*
6957c478bdstevel@tonic-gate		 * A		Append at end of line, short for $a.
6967c478bdstevel@tonic-gate		 */
6977c478bdstevel@tonic-gate		case 'A':
6987c478bdstevel@tonic-gate			operate('$', 1);
6997c478bdstevel@tonic-gateappnd:
7007c478bdstevel@tonic-gate			c = 'a';
7019097ca5Toomas Soome			/* FALLTHROUGH */
7027c478bdstevel@tonic-gate
7037c478bdstevel@tonic-gate		/*
7047c478bdstevel@tonic-gate		 * a		Appends text after cursor.  Text can continue
7057c478bdstevel@tonic-gate		 *		through arbitrary number of lines.
7067c478bdstevel@tonic-gate		 */
7077c478bdstevel@tonic-gate		case 'a':
7087c478bdstevel@tonic-gate			if (*cursor) {
7097c478bdstevel@tonic-gate				wchar_t wchar;
7107c478bdstevel@tonic-gate				int length = mbtowc(&wchar, (char *)cursor, MULTI_BYTE_MAX);
7117c478bdstevel@tonic-gate				if (state == HARDOPEN) {
7127c478bdstevel@tonic-gate					if(length < 0) {
7137c478bdstevel@tonic-gate						putoctal = 1;
7147c478bdstevel@tonic-gate						putchar(*cursor);
7157c478bdstevel@tonic-gate						putoctal = 0;
7167c478bdstevel@tonic-gate					} else
7177c478bdstevel@tonic-gate						putchar(wchar);
7187c478bdstevel@tonic-gate				}
7197c478bdstevel@tonic-gate				if(length < 0)
7207c478bdstevel@tonic-gate					cursor++;
7217c478bdstevel@tonic-gate				else
7227c478bdstevel@tonic-gate					cursor += length;
7237c478bdstevel@tonic-gate			}
7247c478bdstevel@tonic-gate			goto insrt;
7257c478bdstevel@tonic-gate
7267c478bdstevel@tonic-gate		/*
7277c478bdstevel@tonic-gate		 * I		Insert at beginning of whitespace of line,
7287c478bdstevel@tonic-gate		 *		short for ^i.
7297c478bdstevel@tonic-gate		 */
7307c478bdstevel@tonic-gate		case 'I':
7317c478bdstevel@tonic-gate			operate('^', 1);
7327c478bdstevel@tonic-gate			c = 'i';
7339097ca5Toomas Soome			/* FALLTHROUGH */
7347c478bdstevel@tonic-gate
7357c478bdstevel@tonic-gate		/*
7367c478bdstevel@tonic-gate		 * R		Replace characters, one for one, by input
7377c478bdstevel@tonic-gate		 *		(logically), like repeated r commands.
7387c478bdstevel@tonic-gate		 *
7397c478bdstevel@tonic-gate		 * BUG:		This is like the typeover mode of many other
7407c478bdstevel@tonic-gate		 *		editors, and is only rarely useful.  Its
7417c478bdstevel@tonic-gate		 *		implementation is a hack in a low level
7427c478bdstevel@tonic-gate		 *		routine and it doesn't work very well, e.g.
7437c478bdstevel@tonic-gate		 *		you can't move around within a R, etc.
7447c478bdstevel@tonic-gate		 */
7457c478bdstevel@tonic-gate		case 'R':
7469097ca5Toomas Soome			/* FALLTHROUGH */
7477c478bdstevel@tonic-gate
7487c478bdstevel@tonic-gate		/*
7497c478bdstevel@tonic-gate		 * i		Insert text to an escape in the buffer.
7507c478bdstevel@tonic-gate		 *		Text is arbitrary.  This command reminds of
7517c478bdstevel@tonic-gate		 *		the i command in bare teco.
7527c478bdstevel@tonic-gate		 */
7537c478bdstevel@tonic-gate		case 'i':
7547c478bdstevel@tonic-gateinsrt:
7557c478bdstevel@tonic-gate			/*
7567c478bdstevel@tonic-gate			 * Common code for all the insertion commands.
7577c478bdstevel@tonic-gate			 * Save for redo, position cursor, prepare for append
7587c478bdstevel@tonic-gate			 * at command and in visual undo.  Note that nothing
7597c478bdstevel@tonic-gate			 * is doomed, unless R when all is, and save the
7607c478bdstevel@tonic-gate			 * current line in a the undo temporary buffer.
7617c478bdstevel@tonic-gate			 */
7627c478bdstevel@tonic-gate			vmacchng(1);
7637c478bdstevel@tonic-gate			setLAST();
7647c478bdstevel@tonic-gate			vcursat(cursor);
7657c478bdstevel@tonic-gate			prepapp();
7667c478bdstevel@tonic-gate			vnoapp();
7677c478bdstevel@tonic-gate			doomed = c == 'R' ? 10000 : 0;
7687c478bdstevel@tonic-gate			if(FIXUNDO)
7697c478bdstevel@tonic-gate				vundkind = VCHNG;
7707c478bdstevel@tonic-gate			vmoving = 0;
7717c478bdstevel@tonic-gate			CP(vutmp, linebuf);
7727c478bdstevel@tonic-gate
7737c478bdstevel@tonic-gate			/*
7747c478bdstevel@tonic-gate			 * If this is a repeated command, then suppress
7757c478bdstevel@tonic-gate			 * fake insert mode on dumb terminals which looks
7767c478bdstevel@tonic-gate			 * ridiculous and wastes lots of time even at 9600B.
7777c478bdstevel@tonic-gate			 */
7787c478bdstevel@tonic-gate			if (vglobp)
7797c478bdstevel@tonic-gate				hold = HOLDQIK;
7807c478bdstevel@tonic-gate			vappend(c, cnt, 0);
7817c478bdstevel@tonic-gate			continue;
7827c478bdstevel@tonic-gate
7837c478bdstevel@tonic-gate		/*
7847c478bdstevel@tonic-gate		 * 	An attention, normally a DEL, just beeps.
7857c478bdstevel@tonic-gate		 *	If you are a vi command within ex, then
7867c478bdstevel@tonic-gate		 *	two ATTN's will drop you back to command mode.
7877c478bdstevel@tonic-gate		 */
7887c478bdstevel@tonic-gate		case ATTN:
789f6db9f2cf			(void) beep();
7907c478bdstevel@tonic-gate			if (initev || peekkey() != ATTN)
7917c478bdstevel@tonic-gate				continue;
7929097ca5Toomas Soome			/* FALLTHROUGH */
7937c478bdstevel@tonic-gate
7947c478bdstevel@tonic-gate		/*
7957c478bdstevel@tonic-gate		 * ^\		A quit always gets command mode.
7967c478bdstevel@tonic-gate		 */
7977c478bdstevel@tonic-gate		case QUIT:
7987c478bdstevel@tonic-gate			/*
7997c478bdstevel@tonic-gate			 * Have to be careful if we were called
8007c478bdstevel@tonic-gate			 *	g/xxx/vi
8017c478bdstevel@tonic-gate			 * since a return will just start up again.
8027c478bdstevel@tonic-gate			 * So we simulate an interrupt.
8037c478bdstevel@tonic-gate			 */
8047c478bdstevel@tonic-gate			if (inglobal)
8057c478bdstevel@tonic-gate				onintr(0);
8067c478bdstevel@tonic-gate			/* fall into... */
8077c478bdstevel@tonic-gate
8087c478bdstevel@tonic-gate#ifdef notdef
8097c478bdstevel@tonic-gate		/*
8107c478bdstevel@tonic-gate		 * q		Quit back to command mode, unless called as
8117c478bdstevel@tonic-gate		 *		vi on command line in which case dont do it
8127c478bdstevel@tonic-gate		 */
8137c478bdstevel@tonic-gate		case 'q':	/* quit */
8147c478bdstevel@tonic-gate			if (initev) {
8157c478bdstevel@tonic-gate				vsave();
8167c478bdstevel@tonic-gate				CATCH
8177c478bdstevel@tonic-gate					error(gettext("Q gets ex command mode, :q leaves vi"));
8187c478bdstevel@tonic-gate				ENDCATCH
8197c478bdstevel@tonic-gate				splitw = 0;
8207c478bdstevel@tonic-gate				getDOT();
8217c478bdstevel@tonic-gate				vrepaint(cursor);
8227c478bdstevel@tonic-gate				continue;
8237c478bdstevel@tonic-gate			}
8247c478bdstevel@tonic-gate#endif
8259097ca5Toomas Soome			/* FALLTHROUGH */
8267c478bdstevel@tonic-gate
8277c478bdstevel@tonic-gate		/*
8287c478bdstevel@tonic-gate		 * Q		Is like q, but always gets to command mode
8297c478bdstevel@tonic-gate		 *		even if command line invocation was as vi.
8307c478bdstevel@tonic-gate		 */
8317c478bdstevel@tonic-gate		case 'Q':
8327c478bdstevel@tonic-gate			vsave();
8337c478bdstevel@tonic-gate			/*
8347c478bdstevel@tonic-gate			 * If we are in the middle of a macro, throw away
8357c478bdstevel@tonic-gate			 * the rest and fix up undo.
8367c478bdstevel@tonic-gate			 * This code copied from getbr().
8377c478bdstevel@tonic-gate			 */
8387c478bdstevel@tonic-gate			if (vmacp) {
8397c478bdstevel@tonic-gate				vmacp = 0;
8407c478bdstevel@tonic-gate				if (inopen == -1)	/* don't mess up undo for esc esc */
8417c478bdstevel@tonic-gate					vundkind = VMANY;
8427c478bdstevel@tonic-gate				inopen = 1;	/* restore old setting now that macro done */
8437c478bdstevel@tonic-gate			}
8447c478bdstevel@tonic-gate			ixlatctl(1);
8457c478bdstevel@tonic-gate			return;
8467c478bdstevel@tonic-gate
8477c478bdstevel@tonic-gate
8487c478bdstevel@tonic-gate		/*
8497c478bdstevel@tonic-gate		 * ZZ		Like :x
8507c478bdstevel@tonic-gate		 */
8517c478bdstevel@tonic-gate		 case 'Z':
8527c478bdstevel@tonic-gate			forbid(getkey() != 'Z');
8537c478bdstevel@tonic-gate			oglobp = globp;
8547c478bdstevel@tonic-gate			globp = (unsigned char *)"x";
8557c478bdstevel@tonic-gate			vclrech(0);
8567c478bdstevel@tonic-gate			goto gogo;
8577c478bdstevel@tonic-gate
8587c478bdstevel@tonic-gate		/*
8597c478bdstevel@tonic-gate		 * P		Put back text before cursor or before current
8607c478bdstevel@tonic-gate		 *		line.  If text was whole lines goes back
8617c478bdstevel@tonic-gate		 *		as whole lines.  If part of a single line
8627c478bdstevel@tonic-gate		 *		or parts of whole lines splits up current
8637c478bdstevel@tonic-gate		 *		line to form many new lines.
8647c478bdstevel@tonic-gate		 *		May specify a named buffer, or the delete
8657c478bdstevel@tonic-gate		 *		saving buffers 1-9.
8667c478bdstevel@tonic-gate		 *
8677c478bdstevel@tonic-gate		 * p		Like P but after rather than before.
8687c478bdstevel@tonic-gate		 */
8697c478bdstevel@tonic-gate		case 'P':
8707c478bdstevel@tonic-gate		case 'p':
8717c478bdstevel@tonic-gate			vmoving = 0;
8727c478bdstevel@tonic-gate#ifdef XPG4
8737c478bdstevel@tonic-gate			P_cursor_offset = 0;
8747c478bdstevel@tonic-gate#endif
8757c478bdstevel@tonic-gate#ifdef notdef
8767c478bdstevel@tonic-gate			forbid (!vreg && value(vi_UNDOMACRO) && inopen < 0);
8777c478bdstevel@tonic-gate#endif
8787c478bdstevel@tonic-gate			/*
8797c478bdstevel@tonic-gate			 * If previous delete was partial line, use an
8807c478bdstevel@tonic-gate			 * append or insert to put it back so as to
8817c478bdstevel@tonic-gate			 * use insert mode on intelligent terminals.
8827c478bdstevel@tonic-gate			 */
8837c478bdstevel@tonic-gate			if (!vreg && DEL[0]) {
8847c478bdstevel@tonic-gate				setLAST();
8857c478bdstevel@tonic-gate				forbid ((unsigned char)DEL[128] == 0200);
8867c478bdstevel@tonic-gate				vglobp = DEL;
8877c478bdstevel@tonic-gate				ungetkey(c == 'p' ? 'a' : 'i');
8887c478bdstevel@tonic-gate				goto reread;
8897c478bdstevel@tonic-gate			}
8907c478bdstevel@tonic-gate
8917c478bdstevel@tonic-gate			/*
8927c478bdstevel@tonic-gate			 * If a register wasn't specified, then make
8937c478bdstevel@tonic-gate			 * sure there is something to put back.
8947c478bdstevel@tonic-gate			 */
8957c478bdstevel@tonic-gate			forbid (!vreg && unddol == dol);
8967c478bdstevel@tonic-gate			/*
8977c478bdstevel@tonic-gate			 * If we just did a macro the whole buffer is in
8987c478bdstevel@tonic-gate			 * the undo save area.  We don't want to put THAT.
8997c478bdstevel@tonic-gate			 */
9007c478bdstevel@tonic-gate			forbid (vundkind == VMANY && undkind==UNDALL);
9017c478bdstevel@tonic-gate			vsave();
9027c478bdstevel@tonic-gate			vmacchng(1);
9037c478bdstevel@tonic-gate			setLAST();
9047c478bdstevel@tonic-gate			i = 0;
9057c478bdstevel@tonic-gate			if (vreg && partreg(vreg) || !vreg && pkill[0]) {
9067c478bdstevel@tonic-gate				/*
9077c478bdstevel@tonic-gate				 * Restoring multiple lines which were partial
9087c478bdstevel@tonic-gate				 * lines; will leave cursor in middle
9097c478bdstevel@tonic-gate				 * of line after shoving restored text in to
9107c478bdstevel@tonic-gate				 * split the current line.
9117c478bdstevel@tonic-gate				 */
9127c478bdstevel@tonic-gate				i++;
9137c478bdstevel@tonic-gate				if (c == 'p' && *cursor)
9147c478bdstevel@tonic-gate					cursor = nextchr(cursor);
9157c478bdstevel@tonic-gate			} else {
9167c478bdstevel@tonic-gate				/*
9177c478bdstevel@tonic-gate				 * In whole line case, have to back up dot
9187c478bdstevel@tonic-gate				 * for P; also want to clear cursor so
9197c478bdstevel@tonic-gate				 * cursor will eventually be positioned
9207c478bdstevel@tonic-gate				 * at the beginning of the first put line.
9217c478bdstevel@tonic-gate				 */
9227c478bdstevel@tonic-gate				cursor = 0;
9237c478bdstevel@tonic-gate				if (c == 'P') {
9247c478bdstevel@tonic-gate					dot--, vcline--;
9257c478bdstevel@tonic-gate					c = 'p';
9267c478bdstevel@tonic-gate				}
9277c478bdstevel@tonic-gate			}
9287c478bdstevel@tonic-gate			killU();
9297c478bdstevel@tonic-gate
9307c478bdstevel@tonic-gate			/*
9317c478bdstevel@tonic-gate			 * The call to putreg can potentially
9327c478bdstevel@tonic-gate			 * bomb since there may be nothing in a named buffer.
9337c478bdstevel@tonic-gate			 * We thus put a catch in here.  If we didn't and
9347c478bdstevel@tonic-gate			 * there was an error we would end up in command mode.
9357c478bdstevel@tonic-gate			 */
9367c478bdstevel@tonic-gate			addr = dol;	/* old dol */
9377c478bdstevel@tonic-gate			CATCH
938f6db9f2cf				vremote(1,
939f6db9f2cf				    vreg ? (int (*)())putreg : put, vreg);
9407c478bdstevel@tonic-gate			ONERR
9417c478bdstevel@tonic-gate				if (vreg == -1) {
9427c478bdstevel@tonic-gate					splitw = 0;
9437c478bdstevel@tonic-gate					if (op == 'P')
9447c478bdstevel@tonic-gate						dot++, vcline++;
9457c478bdstevel@tonic-gate					goto pfixup;
9467c478bdstevel@tonic-gate				}
9477c478bdstevel@tonic-gate			ENDCATCH
9487c478bdstevel@tonic-gate			splitw = 0;
9497c478bdstevel@tonic-gate			nlput = dol - addr + 1;
9507c478bdstevel@tonic-gate			if (!i) {
9517c478bdstevel@tonic-gate				/*
9527c478bdstevel@tonic-gate				 * Increment undap1, undap2 to make up
9537c478bdstevel@tonic-gate				 * for their incorrect initialization in the
9547c478bdstevel@tonic-gate				 * routine vremote before calling put/putreg.
9557c478bdstevel@tonic-gate				 */
9567c478bdstevel@tonic-gate				if (FIXUNDO)
9577c478bdstevel@tonic-gate					undap1++, undap2++;
9587c478bdstevel@tonic-gate				vcline++;
9597c478bdstevel@tonic-gate				nlput--;
9607c478bdstevel@tonic-gate
9617c478bdstevel@tonic-gate				/*
9627c478bdstevel@tonic-gate				 * After a put want current line first line,
9637c478bdstevel@tonic-gate				 * and dot was made the last line put in code
9647c478bdstevel@tonic-gate				 * run so far.  This is why we increment vcline
9657c478bdstevel@tonic-gate				 * above and decrease dot here.
9667c478bdstevel@tonic-gate				 */
9677c478bdstevel@tonic-gate				dot -= nlput - 1;
9687c478bdstevel@tonic-gate			}
9697c478bdstevel@tonic-gate#ifdef TRACE
9707c478bdstevel@tonic-gate			if (trace)
9717c478bdstevel@tonic-gate				fprintf(trace, "vreplace(%d, %d, %d), undap1=%d, undap2=%d, dot=%d\n", vcline, i, nlput, lineno(undap1), lineno(undap2), lineno(dot));
9727c478bdstevel@tonic-gate#endif
9737c478bdstevel@tonic-gate			vreplace(vcline, i, nlput);
9747c478bdstevel@tonic-gate#ifdef XPG4
9757c478bdstevel@tonic-gate			if (op == 'P' && i > 0) {
9767c478bdstevel@tonic-gate				dot += nlput - 1;
9777c478bdstevel@tonic-gate				vcline += nlput - 1;
9787c478bdstevel@tonic-gate				cursor += P_cursor_offset;
9797c478bdstevel@tonic-gate			}
9807c478bdstevel@tonic-gate#endif
9817c478bdstevel@tonic-gate			if (state != VISUAL) {
9827c478bdstevel@tonic-gate				/*
9837c478bdstevel@tonic-gate				 * Special case in open mode.
9847c478bdstevel@tonic-gate				 * Force action on the screen when a single
9857c478bdstevel@tonic-gate				 * line is put even if it is identical to
9867c478bdstevel@tonic-gate				 * the current line, e.g. on YP; otherwise
9877c478bdstevel@tonic-gate				 * you can't tell anything happened.
9887c478bdstevel@tonic-gate				 */
9897c478bdstevel@tonic-gate				vjumpto(dot, cursor, '.');
9907c478bdstevel@tonic-gate				continue;
9917c478bdstevel@tonic-gate			}
9927c478bdstevel@tonic-gatepfixup:
9937c478bdstevel@tonic-gate			vrepaint(cursor);
9947c478bdstevel@tonic-gate			vfixcurs();
9957c478bdstevel@tonic-gate			continue;
9967c478bdstevel@tonic-gate
9977c478bdstevel@tonic-gate		/*
9987c478bdstevel@tonic-gate		 * ^^		Return to previous file.
9997c478bdstevel@tonic-gate		 *		Like a :e #, and thus can be used after a
10007c478bdstevel@tonic-gate		 *		"No Write" diagnostic.
10017c478bdstevel@tonic-gate		 */
10027c478bdstevel@tonic-gate		case CTRL('^'):
10037c478bdstevel@tonic-gate			forbid (hadcnt);
10047c478bdstevel@tonic-gate			vsave();
10057c478bdstevel@tonic-gate			ckaw();
10067c478bdstevel@tonic-gate			oglobp = globp;
10077c478bdstevel@tonic-gate			if (value(vi_AUTOWRITE) && !value(vi_READONLY))
10087c478bdstevel@tonic-gate				globp = (unsigned char *)"e! #";
10097c478bdstevel@tonic-gate			else
10107c478bdstevel@tonic-gate				globp = (unsigned char *)"e #";
10117c478bdstevel@tonic-gate			goto gogo;
10127c478bdstevel@tonic-gate
10137c478bdstevel@tonic-gate#ifdef TAG_STACK
10147c478bdstevel@tonic-gate                /*
10157c478bdstevel@tonic-gate                 * ^T           Pop the tag stack if enabled or else reset it
10167c478bdstevel@tonic-gate                 *              if not.
10177c478bdstevel@tonic-gate                 */
10187c478bdstevel@tonic-gate                case CTRL('t'):
10197c478bdstevel@tonic-gate                        forbid (hadcnt);
10207c478bdstevel@tonic-gate                        vsave();
10217c478bdstevel@tonic-gate                        oglobp = globp;
10227c478bdstevel@tonic-gate                        globp = (unsigned char *) "pop";
10237c478bdstevel@tonic-gate                        goto gogo;
10247c478bdstevel@tonic-gate#endif
10257c478bdstevel@tonic-gate		/*
10267c478bdstevel@tonic-gate		 * ^]		Takes word after cursor as tag, and then does
10277c478bdstevel@tonic-gate		 *		tag command.  Read ``go right to''.
10287c478bdstevel@tonic-gate		 *		This is not a search, so the wrapscan setting
10297c478bdstevel@tonic-gate		 *		must be ignored.  If set, then it is unset
10307c478bdstevel@tonic-gate		 *		here and restored later.
10317c478bdstevel@tonic-gate		 */
10327c478bdstevel@tonic-gate		case CTRL(']'):
10337c478bdstevel@tonic-gate			grabtag();
10347c478bdstevel@tonic-gate			oglobp = globp;
10357c478bdstevel@tonic-gate			if (value(vi_WRAPSCAN) == 0) {
10367c478bdstevel@tonic-gate				tag_reset_wrap = 1;
10377c478bdstevel@tonic-gate				value(vi_WRAPSCAN) = 1;
10387c478bdstevel@tonic-gate			}
10397c478bdstevel@tonic-gate			globp = (unsigned char *)"tag";
10407c478bdstevel@tonic-gate			goto gogo;
10417c478bdstevel@tonic-gate
10427c478bdstevel@tonic-gate		/*
10437c478bdstevel@tonic-gate		 * &		Like :&
10447c478bdstevel@tonic-gate		 */
10457c478bdstevel@tonic-gate		 case '&':
10467c478bdstevel@tonic-gate			oglobp = globp;
10477c478bdstevel@tonic-gate			globp = (unsigned char *)"&";
10487c478bdstevel@tonic-gate			goto gogo;
10497c478bdstevel@tonic-gate
10507c478bdstevel@tonic-gate		/*
10517c478bdstevel@tonic-gate		 * ^G		Bring up a status line at the bottom of
10527c478bdstevel@tonic-gate		 *		the screen, like a :file command.
10537c478bdstevel@tonic-gate		 *
10547c478bdstevel@tonic-gate		 * BUG:		Was ^S but doesn't work in cbreak mode
10557c478bdstevel@tonic-gate		 */
10567c478bdstevel@tonic-gate		case CTRL('g'):
10577c478bdstevel@tonic-gate			oglobp = globp;
10587c478bdstevel@tonic-gate			globp = (unsigned char *)"file";
10597c478bdstevel@tonic-gategogo:
10607c478bdstevel@tonic-gate			addr = dot;
10617c478bdstevel@tonic-gate			vsave();
10627c478bdstevel@tonic-gate			goto doinit;
10637c478bdstevel@tonic-gate
10647c478bdstevel@tonic-gate#ifdef SIGTSTP
10657c478bdstevel@tonic-gate		/*
10667c478bdstevel@tonic-gate		 * ^Z:	suspend editor session and temporarily return
10677c478bdstevel@tonic-gate		 * 	to shell.  Only works with Berkeley/IIASA process
10687c478bdstevel@tonic-gate		 *	control in kernel.
10697c478bdstevel@tonic-gate		 */
10707c478bdstevel@tonic-gate		case CTRL('z'):
10717c478bdstevel@tonic-gate			forbid(dosusp == 0);
10727c478bdstevel@tonic-gate			vsave();
10737c478bdstevel@tonic-gate			oglobp = globp;
10747c478bdstevel@tonic-gate			globp = (unsigned char *)"stop";
10757c478bdstevel@tonic-gate			goto gogo;
10767c478bdstevel@tonic-gate#endif
10777c478bdstevel@tonic-gate
10787c478bdstevel@tonic-gate		/*
10797c478bdstevel@tonic-gate		 * :		Read a command from the echo area and
10807c478bdstevel@tonic-gate		 *		execute it in command mode.
10817c478bdstevel@tonic-gate		 */
10827c478bdstevel@tonic-gate		case ':':
10837c478bdstevel@tonic-gate			forbid (hadcnt);
10847c478bdstevel@tonic-gate			vsave();
10857c478bdstevel@tonic-gate			i = tchng;
10867c478bdstevel@tonic-gate			addr = dot;
10877c478bdstevel@tonic-gate			if (readecho(c)) {
10887c478bdstevel@tonic-gate				esave[0] = 0;
10897c478bdstevel@tonic-gate				goto fixup;
10907c478bdstevel@tonic-gate			}
10917c478bdstevel@tonic-gate			getDOT();
10927c478bdstevel@tonic-gate			/*
10937c478bdstevel@tonic-gate			 * Use the visual undo buffer to store the global
10947c478bdstevel@tonic-gate			 * string for command mode, since it is idle right now.
10957c478bdstevel@tonic-gate			 */
10967c478bdstevel@tonic-gate			oglobp = globp; strcpy(vutmp, genbuf+1); globp = vutmp;
10977c478bdstevel@tonic-gatedoinit:
10987c478bdstevel@tonic-gate			esave[0] = 0;
10997c478bdstevel@tonic-gate			fixech();
11007c478bdstevel@tonic-gate
11017c478bdstevel@tonic-gate			/*
11027c478bdstevel@tonic-gate			 * Have to finagle around not to lose last
11037c478bdstevel@tonic-gate			 * character after this command (when run from ex
11047c478bdstevel@tonic-gate			 * command mode).  This is clumsy.
11057c478bdstevel@tonic-gate			 */
11067c478bdstevel@tonic-gate			d = peekc; ungetchar(0);
11077c478bdstevel@tonic-gate			if (shouldpo) {
11087c478bdstevel@tonic-gate				/*
11097c478bdstevel@tonic-gate				 * So after a "Hit return..." ":", we do
11107c478bdstevel@tonic-gate				 * another "Hit return..." the next time
11117c478bdstevel@tonic-gate				 */
11127c478bdstevel@tonic-gate				pofix();
11137c478bdstevel@tonic-gate				shouldpo = 0;
11147c478bdstevel@tonic-gate			}
11157c478bdstevel@tonic-gate			CATCH
11167c478bdstevel@tonic-gate				/*
11177c478bdstevel@tonic-gate				 * Save old values of options so we can
11187c478bdstevel@tonic-gate				 * notice when they change; switch into
11197c478bdstevel@tonic-gate				 * cooked mode so we are interruptible.
11207c478bdstevel@tonic-gate				 */
11217c478bdstevel@tonic-gate				onumber = value(vi_NUMBER);
11227c478bdstevel@tonic-gate				olist = value(vi_LIST);
11237c478bdstevel@tonic-gate				OPline = Pline;
11247c478bdstevel@tonic-gate				OPutchar = Putchar;
11257c478bdstevel@tonic-gate#ifndef CBREAK
11267c478bdstevel@tonic-gate				vcook();
11277c478bdstevel@tonic-gate#endif
11287c478bdstevel@tonic-gate				commands(1, 1);
11297c478bdstevel@tonic-gate				if (dot == zero && dol > zero)
11307c478bdstevel@tonic-gate					dot = one;
11317c478bdstevel@tonic-gate#ifndef CBREAK
11327c478bdstevel@tonic-gate				vraw();
11337c478bdstevel@tonic-gate#endif
11347c478bdstevel@tonic-gate			ONERR
11357c478bdstevel@tonic-gate#ifndef CBREAK
11367c478bdstevel@tonic-gate				vraw();
11377c478bdstevel@tonic-gate#endif
11387c478bdstevel@tonic-gate				copy(esave, vtube[WECHO], TUBECOLS * sizeof(wchar_t));
11397c478bdstevel@tonic-gate			ENDCATCH
11407c478bdstevel@tonic-gate			fixol();
11417c478bdstevel@tonic-gate			Pline = OPline;
11427c478bdstevel@tonic-gate			Putchar = OPutchar;
11437c478bdstevel@tonic-gate			ungetchar(d);
11447c478bdstevel@tonic-gate			globp = oglobp;
11457c478bdstevel@tonic-gate
11467c478bdstevel@tonic-gate			/*
11477c478bdstevel@tonic-gate			 * If we ended up with no lines in the buffer, make
11487c478bdstevel@tonic-gate			 * a line.
11497c478bdstevel@tonic-gate			 */
11507c478bdstevel@tonic-gate			if (dot == zero) {
11517c478bdstevel@tonic-gate				fixzero();
11527c478bdstevel@tonic-gate			}
11537c478bdstevel@tonic-gate			splitw = 0;
11547c478bdstevel@tonic-gate
11557c478bdstevel@tonic-gate			/*
11567c478bdstevel@tonic-gate			 * Special case: did list/number options change?
11577c478bdstevel@tonic-gate			 */
11587c478bdstevel@tonic-gate			if (onumber != value(vi_NUMBER))
11597c478bdstevel@tonic-gate				setnumb(value(vi_NUMBER));
11607c478bdstevel@tonic-gate			if (olist != value(vi_LIST))
11617c478bdstevel@tonic-gate				setlist(value(vi_LIST));
11627c478bdstevel@tonic-gate
11637c478bdstevel@tonic-gatefixup:
11647c478bdstevel@tonic-gate			/*
11657c478bdstevel@tonic-gate			 * If a change occurred, other than
11667c478bdstevel@tonic-gate			 * a write which clears changes, then
11677c478bdstevel@tonic-gate			 * we should allow an undo even if .
11687c478bdstevel@tonic-gate			 * didn't move.
11697c478bdstevel@tonic-gate			 *
11707c478bdstevel@tonic-gate			 * BUG: You can make this wrong by
11717c478bdstevel@tonic-gate			 * tricking around with multiple commands
11727c478bdstevel@tonic-gate			 * on one line of : escape, and including
11737c478bdstevel@tonic-gate			 * a write command there, but it's not
11747c478bdstevel@tonic-gate			 * worth worrying about.
11757c478bdstevel@tonic-gate			 */
11767c478bdstevel@tonic-gate			if (FIXUNDO && tchng && tchng != i)
11777c478bdstevel@tonic-gate				vundkind = VMANY, cursor = 0;
11787c478bdstevel@tonic-gate
11797c478bdstevel@tonic-gate			/*
11807c478bdstevel@tonic-gate			 * If we are about to do another :, hold off
11817c478bdstevel@tonic-gate			 * updating of screen.
11827c478bdstevel@tonic-gate			 */
11837c478bdstevel@tonic-gate			if (vcnt < 0 && Peekkey == ':') {
11847c478bdstevel@tonic-gate				getDOT();
11857c478bdstevel@tonic-gate				shouldpo = 1;
11867c478bdstevel@tonic-gate				continue;
11877c478bdstevel@tonic-gate			}
11887c478bdstevel@tonic-gate			shouldpo = 0;
11897c478bdstevel@tonic-gate
11907c478bdstevel@tonic-gate			/*
11917c478bdstevel@tonic-gate			 * In the case where the file being edited is
11927c478bdstevel@tonic-gate			 * new; e.g. if the initial state hasn't been
11937c478bdstevel@tonic-gate			 * saved yet, then do so now.
11947c478bdstevel@tonic-gate			 */
11957c478bdstevel@tonic-gate			if (unddol == truedol) {
11967c478bdstevel@tonic-gate				vundkind = VNONE;
11977c478bdstevel@tonic-gate				Vlines = lineDOL();
11987c478bdstevel@tonic-gate				if (!inglobal)
11997c478bdstevel@tonic-gate					savevis();
12007c478bdstevel@tonic-gate				addr = zero;
12017c478bdstevel@tonic-gate				vcnt = 0;
12027c478bdstevel@tonic-gate				if (esave[0] == 0)
12037c478bdstevel@tonic-gate					copy(esave, vtube[WECHO], TUBECOLS * sizeof(wchar_t));
12047c478bdstevel@tonic-gate			}
12057c478bdstevel@tonic-gate
12067c478bdstevel@tonic-gate			/*
12077c478bdstevel@tonic-gate			 * If the current line moved reset the cursor position.
12087c478bdstevel@tonic-gate			 */
12097c478bdstevel@tonic-gate			if (dot != addr) {
12107c478bdstevel@tonic-gate				vmoving = 0;
12117c478bdstevel@tonic-gate				cursor = 0;
12127c478bdstevel@tonic-gate			}
12137c478bdstevel@tonic-gate
12147c478bdstevel@tonic-gate			/*
12157c478bdstevel@tonic-gate			 * If current line is not on screen or if we are
12167c478bdstevel@tonic-gate			 * in open mode and . moved, then redraw.
12177c478bdstevel@tonic-gate			 */
12187c478bdstevel@tonic-gate			i = vcline + (dot - addr);
12197c478bdstevel@tonic-gate			if(windowchg)
12207c478bdstevel@tonic-gate				windowinit();
12217c478bdstevel@tonic-gate			if (i < 0 || i >= vcnt && i >= -vcnt || state != VISUAL && dot != addr) {
12227c478bdstevel@tonic-gate				if (state == CRTOPEN)
12237c478bdstevel@tonic-gate					vup1();
12247c478bdstevel@tonic-gate				if (vcnt > 0)
12257c478bdstevel@tonic-gate					vcnt = 0;
1226f6db9f2cf				vjumpto(dot, (unsigned char *) 0, '.');
12277c478bdstevel@tonic-gate			} else {
12287c478bdstevel@tonic-gate				/*
12297c478bdstevel@tonic-gate				 * Current line IS on screen.
12307c478bdstevel@tonic-gate				 * If we did a [Hit return...] then
12317c478bdstevel@tonic-gate				 * restore vcnt and clear screen if in visual
12327c478bdstevel@tonic-gate				 */
12337c478bdstevel@tonic-gate				vcline = i;
12347c478bdstevel@tonic-gate				if (vcnt < 0) {
12357c478bdstevel@tonic-gate					vcnt = -vcnt;
12367c478bdstevel@tonic-gate					if (state == VISUAL)
12377c478bdstevel@tonic-gate						vclear();
12387c478bdstevel@tonic-gate					else if (state == CRTOPEN) {
12397c478bdstevel@tonic-gate						vcnt = 0;
12407c478bdstevel@tonic-gate					}
12417c478bdstevel@tonic-gate				}
12427c478bdstevel@tonic-gate
12437c478bdstevel@tonic-gate				/*
12447c478bdstevel@tonic-gate				 * Limit max value of vcnt based on $
12457c478bdstevel@tonic-gate				 */
12467c478bdstevel@tonic-gate				i = vcline + lineDOL() - lineDOT() + 1;
12477c478bdstevel@tonic-gate				if (i < vcnt)
12487c478bdstevel@tonic-gate					vcnt = i;
12497c478bdstevel@tonic-gate
12507c478bdstevel@tonic-gate				/*
12517c478bdstevel@tonic-gate				 * Dirty and repaint.
12527c478bdstevel@tonic-gate				 */
12537c478bdstevel@tonic-gate				vdirty(0, lines);
12547c478bdstevel@tonic-gate				vrepaint(cursor);
12557c478bdstevel@tonic-gate			}
12567c478bdstevel@tonic-gate
12577c478bdstevel@tonic-gate			/*
12587c478bdstevel@tonic-gate			 * If in visual, put back the echo area
12597c478bdstevel@tonic-gate			 * if it was clobbered.
12607c478bdstevel@tonic-gate			 */
12617c478bdstevel@tonic-gate			if (state == VISUAL) {
12627c478bdstevel@tonic-gate				int sdc = destcol, sdl = destline;
12637c478bdstevel@tonic-gate
12647c478bdstevel@tonic-gate				splitw++;
12657c478bdstevel@tonic-gate				vigoto(WECHO, 0);
12667c478bdstevel@tonic-gate				for (i = 0; i < TUBECOLS - 1; i++) {
12677c478bdstevel@tonic-gate					if (esave[i] == 0)
12687c478bdstevel@tonic-gate						break;
12697c478bdstevel@tonic-gate					if(esave[i] != FILLER)
1270f6db9f2cf						(void) vputchar(esave[i]);
12717c478bdstevel@tonic-gate				}
12727c478bdstevel@tonic-gate				splitw = 0;
12737c478bdstevel@tonic-gate				vgoto(sdl, sdc);
12747c478bdstevel@tonic-gate			}
12757c478bdstevel@tonic-gate			if (tag_reset_wrap == 1) {
12767c478bdstevel@tonic-gate				tag_reset_wrap = 0;
12777c478bdstevel@tonic-gate				value(vi_WRAPSCAN) = 0;
12787c478bdstevel@tonic-gate			}
12797c478bdstevel@tonic-gate			continue;
12807c478bdstevel@tonic-gate
12817c478bdstevel@tonic-gate		/*
12827c478bdstevel@tonic-gate		 * u		undo the last changing command.
12837c478bdstevel@tonic-gate		 */
12847c478bdstevel@tonic-gate		case 'u':
12857c478bdstevel@tonic-gate			vundo(1);
12867c478bdstevel@tonic-gate			continue;
12877c478bdstevel@tonic-gate
12887c478bdstevel@tonic-gate		/*
12897c478bdstevel@tonic-gate		 * U		restore current line to initial state.
12907c478bdstevel@tonic-gate		 */
12917c478bdstevel@tonic-gate		case 'U':
12927c478bdstevel@tonic-gate			vUndo();
12937c478bdstevel@tonic-gate			continue;
12947c478bdstevel@tonic-gate
12957c478bdstevel@tonic-gatefonfon:
1296f6db9f2cf			(void) beep();
12977c478bdstevel@tonic-gate			vmacp = 0;
12987c478bdstevel@tonic-gate			inopen = 1;	/* might have been -1 */
12997c478bdstevel@tonic-gate			continue;
13007c478bdstevel@tonic-gate		}
13017c478bdstevel@tonic-gate
13027c478bdstevel@tonic-gate		/*
13037c478bdstevel@tonic-gate		 * Rest of commands are decoded by the operate
13047c478bdstevel@tonic-gate		 * routine.
13057c478bdstevel@tonic-gate		 */
13067c478bdstevel@tonic-gate		operate(c, cnt);
13077c478bdstevel@tonic-gate	}
13087c478bdstevel@tonic-gate}
13097c478bdstevel@tonic-gate
13107c478bdstevel@tonic-gate/*
13117c478bdstevel@tonic-gate * Grab the word after the cursor so we can look for it as a tag.
13127c478bdstevel@tonic-gate */
1313f6db9f2cfvoid
1314f6db9f2cfgrabtag(void)
13157c478bdstevel@tonic-gate{
1316f6db9f2cf	unsigned char *cp, *dp;
13177c478bdstevel@tonic-gate
13187c478bdstevel@tonic-gate	cp = vpastwh(cursor);
13197c478bdstevel@tonic-gate	if (*cp) {
13207c478bdstevel@tonic-gate		dp = lasttag;
13217c478bdstevel@tonic-gate		do {
13227c478bdstevel@tonic-gate			if (dp < &lasttag[sizeof lasttag - 2])
13237c478bdstevel@tonic-gate				*dp++ = *cp;
13247c478bdstevel@tonic-gate			cp++;
13257c478bdstevel@tonic-gate			/* only allow ascii alphabetics */
13267c478bdstevel@tonic-gate		} while ((isascii(*cp) && isalpha(*cp)) || isdigit(*cp) || *cp == '_');
13277c478bdstevel@tonic-gate		*dp++ = 0;
13287c478bdstevel@tonic-gate	}
13297c478bdstevel@tonic-gate}
13307c478bdstevel@tonic-gate
13317c478bdstevel@tonic-gate/*
13327c478bdstevel@tonic-gate * Before appending lines, set up addr1 and
13337c478bdstevel@tonic-gate * the command mode undo information.
13347c478bdstevel@tonic-gate */
1335f6db9f2cfvoid
1336f6db9f2cfprepapp(void)
13377c478bdstevel@tonic-gate{
13387c478bdstevel@tonic-gate
13397c478bdstevel@tonic-gate	addr1 = dot;
13407c478bdstevel@tonic-gate	deletenone();
13417c478bdstevel@tonic-gate	addr1++;
13427c478bdstevel@tonic-gate	appendnone();
13437c478bdstevel@tonic-gate}
13447c478bdstevel@tonic-gate
13457c478bdstevel@tonic-gate/*
13467c478bdstevel@tonic-gate * Execute function f with the address bounds addr1
13477c478bdstevel@tonic-gate * and addr2 surrounding cnt lines starting at dot.
13487c478bdstevel@tonic-gate */
1349f6db9f2cfvoid
13507c478bdstevel@tonic-gatevremote(cnt, f, arg)
13517c478bdstevel@tonic-gate	int cnt, (*f)(), arg;
13527c478bdstevel@tonic-gate{
1353f6db9f2cf	int oing = inglobal;
13547c478bdstevel@tonic-gate
13557c478bdstevel@tonic-gate	addr1 = dot;
13567c478bdstevel@tonic-gate	addr2 = dot + cnt - 1;
13577c478bdstevel@tonic-gate	inglobal = 0;
13587c478bdstevel@tonic-gate	if (FIXUNDO)
13597c478bdstevel@tonic-gate		undap1 = undap2 = dot;
13607c478bdstevel@tonic-gate	(*f)(arg);
13617c478bdstevel@tonic-gate	inglobal = oing;
13627c478bdstevel@tonic-gate	if (FIXUNDO)
13637c478bdstevel@tonic-gate		vundkind = VMANY;
1364bbfd0aacf	/*
1365bbfd0aacf	 * XPG6 assertion 273: For the following commands, don't set vmcurs
1366bbfd0aacf	 * to 0, so that undo positions the cursor column correctly when
1367bbfd0aacf	 * we've moved off the initial line that was changed eg. when G has
1368bbfd0aacf	 * moved us off the line, or when a multi-line change was done.
1369bbfd0aacf	 */
1370bbfd0aacf	if (lastcmd[0] != 'C' && lastcmd[0] != 'c' && lastcmd[0] != 'o' &&
1371bbfd0aacf	    lastcmd[0] != 'R' && lastcmd[0] != 'S' && lastcmd[0] != 's' &&
1372bbfd0aacf	    lastcmd[0] != 'i' && lastcmd[0] != 'a' && lastcmd[0] != 'A') {
1373bbfd0aacf		vmcurs = 0;
1374bbfd0aacf	}
13757c478bdstevel@tonic-gate}
13767c478bdstevel@tonic-gate
13777c478bdstevel@tonic-gate/*
13787c478bdstevel@tonic-gate * Save the current contents of linebuf, if it has changed.
13797c478bdstevel@tonic-gate */
1380f6db9f2cfvoid
1381f6db9f2cfvsave(void)
13827c478bdstevel@tonic-gate{
13837c478bdstevel@tonic-gate	unsigned char temp[LBSIZE];
13847c478bdstevel@tonic-gate
13857c478bdstevel@tonic-gate	strncpy(temp, linebuf, sizeof (temp));
13867c478bdstevel@tonic-gate	if (FIXUNDO && vundkind == VCHNG || vundkind == VCAPU) {
13877c478bdstevel@tonic-gate		/*
13887c478bdstevel@tonic-gate		 * If the undo state is saved in the temporary buffer
13897c478bdstevel@tonic-gate		 * vutmp, then we sync this into the temp file so that
13907c478bdstevel@tonic-gate		 * we will be able to undo even after we have moved off
13917c478bdstevel@tonic-gate		 * the line.  It would be possible to associate a line
13927c478bdstevel@tonic-gate		 * with vutmp but we assume that vutmp is only associated
13937c478bdstevel@tonic-gate		 * with line dot (e.g. in case ':') above, so beware.
13947c478bdstevel@tonic-gate		 */
13957c478bdstevel@tonic-gate		prepapp();
13967c478bdstevel@tonic-gate		strcLIN(vutmp);
13977c478bdstevel@tonic-gate		putmark(dot);
13987c478bdstevel@tonic-gate		vremote(1, yank, 0);
13997c478bdstevel@tonic-gate		vundkind = VMCHNG;
14007c478bdstevel@tonic-gate		notecnt = 0;
14017c478bdstevel@tonic-gate		undkind = UNDCHANGE;
14027c478bdstevel@tonic-gate	}
14037c478bdstevel@tonic-gate	/*
14047c478bdstevel@tonic-gate	 * Get the line out of the temp file and do nothing if it hasn't
14057c478bdstevel@tonic-gate	 * changed.  This may seem like a loss, but the line will
14067c478bdstevel@tonic-gate	 * almost always be in a read buffer so this may well avoid disk i/o.
14077c478bdstevel@tonic-gate	 */
14087c478bdstevel@tonic-gate	getDOT();
14097c478bdstevel@tonic-gate	if (strncmp(linebuf, temp, sizeof (temp)) == 0)
14107c478bdstevel@tonic-gate		return;
14117c478bdstevel@tonic-gate	strcLIN(temp);
14127c478bdstevel@tonic-gate	putmark(dot);
14137c478bdstevel@tonic-gate}
14147c478bdstevel@tonic-gate
14157c478bdstevel@tonic-gate#undef	forbid
1416f6db9f2cf#define	forbid(a)	if (a) { (void) beep(); return; }
14177c478bdstevel@tonic-gate
14187c478bdstevel@tonic-gate/*
14197c478bdstevel@tonic-gate * Do a z operation.
14207c478bdstevel@tonic-gate * Code here is rather long, and very uninteresting.
14217c478bdstevel@tonic-gate */
1422f6db9f2cfvoid
1423f6db9f2cfvzop(bool hadcnt, int cnt, int c)
14247c478bdstevel@tonic-gate{
1425f6db9f2cf	line *addr;
14267c478bdstevel@tonic-gate
14277c478bdstevel@tonic-gate	if (state != VISUAL) {
14287c478bdstevel@tonic-gate		/*
14297c478bdstevel@tonic-gate		 * Z from open; always like a z=.
14307c478bdstevel@tonic-gate		 * This code is a mess and should be cleaned up.
14317c478bdstevel@tonic-gate		 */
14327c478bdstevel@tonic-gate		vmoveitup(1, 1);
14337c478bdstevel@tonic-gate		vgoto(outline, 0);
14347c478bdstevel@tonic-gate		ostop(normf);
14357c478bdstevel@tonic-gate		setoutt();
14367c478bdstevel@tonic-gate		addr2 = dot;
14377c478bdstevel@tonic-gate		vclear();
14387c478bdstevel@tonic-gate		destline = WECHO;
14397c478bdstevel@tonic-gate		zop2(Xhadcnt ? Xcnt : value(vi_WINDOW) - 1, '=');
14407c478bdstevel@tonic-gate		if (state == CRTOPEN)
14417c478bdstevel@tonic-gate			putnl();
14427c478bdstevel@tonic-gate		putNFL();
14437c478bdstevel@tonic-gate		termreset();
14447c478bdstevel@tonic-gate		Outchar = vputchar;
14457c478bdstevel@tonic-gate		(void)ostart();
14467c478bdstevel@tonic-gate		vcnt = 0;
14477c478bdstevel@tonic-gate		outline = destline = 0;
14487c478bdstevel@tonic-gate		vjumpto(dot, cursor, 0);
14497c478bdstevel@tonic-gate		return;
14507c478bdstevel@tonic-gate	}
14517c478bdstevel@tonic-gate	if (hadcnt) {
14527c478bdstevel@tonic-gate		addr = zero + cnt;
14537c478bdstevel@tonic-gate		if (addr < one)
14547c478bdstevel@tonic-gate			addr = one;
14557c478bdstevel@tonic-gate		if (addr > dol)
14567c478bdstevel@tonic-gate			addr = dol;
14577c478bdstevel@tonic-gate		markit(addr);
14587c478bdstevel@tonic-gate	} else
14597c478bdstevel@tonic-gate		switch (c) {
14607c478bdstevel@tonic-gate
14617c478bdstevel@tonic-gate		case '+':
14627c478bdstevel@tonic-gate			addr = dot + vcnt - vcline;
14637c478bdstevel@tonic-gate			break;
14647c478bdstevel@tonic-gate
14657c478bdstevel@tonic-gate		case '^':
14667c478bdstevel@tonic-gate			addr = dot - vcline - 1;
14677c478bdstevel@tonic-gate			forbid (addr < one);
14687c478bdstevel@tonic-gate			c = '-';
14697c478bdstevel@tonic-gate			break;
14707c478bdstevel@tonic-gate
14717c478bdstevel@tonic-gate		default:
14727c478bdstevel@tonic-gate			addr = dot;
14737c478bdstevel@tonic-gate			break;
14747c478bdstevel@tonic-gate		}
14757c478bdstevel@tonic-gate	switch (c) {
14767c478bdstevel@tonic-gate
14777c478bdstevel@tonic-gate	case '.':
14787c478bdstevel@tonic-gate	case '-':
14797c478bdstevel@tonic-gate		break;
14807c478bdstevel@tonic-gate
14817c478bdstevel@tonic-gate	case '^':
14827c478bdstevel@tonic-gate		forbid (addr <= one);
14837c478bdstevel@tonic-gate		break;
14847c478bdstevel@tonic-gate
14857c478bdstevel@tonic-gate	case '+':
14867c478bdstevel@tonic-gate		forbid (addr >= dol);
14879097ca5Toomas Soome		/* FALLTHROUGH */
14887c478bdstevel@tonic-gate
14897c478bdstevel@tonic-gate	case CR:
14907c478bdstevel@tonic-gate	case NL:
14917c478bdstevel@tonic-gate		c = CR;
14927c478bdstevel@tonic-gate		break;
14937c478bdstevel@tonic-gate
14947c478bdstevel@tonic-gate	default:
1495f6db9f2cf		(void) beep();
14967c478bdstevel@tonic-gate		return;
14977c478bdstevel@tonic-gate	}
14987c478bdstevel@tonic-gate	vmoving = 0;
1499f6db9f2cf	vjumpto(addr, (unsigned char *)NOSTR, c);
15007c478bdstevel@tonic-gate}
1501