xref: /onnv-gate/usr/src/cmd/vi/port/ex_vops.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53806Scf46844  * Common Development and Distribution License (the "License").
63806Scf46844  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "ex.h"
330Sstevel@tonic-gate #include "ex_tty.h"
340Sstevel@tonic-gate #include "ex_vis.h"
350Sstevel@tonic-gate 
363806Scf46844 void fixundo(void);
373806Scf46844 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * This file defines the operation sequences which interface the
400Sstevel@tonic-gate  * logical changes to the file buffer with the internal and external
410Sstevel@tonic-gate  * display representations.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Undo.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Undo is accomplished in two ways.  We often for small changes in the
480Sstevel@tonic-gate  * current line know how (in terms of a change operator) how the change
490Sstevel@tonic-gate  * occurred.  Thus on an intelligent terminal we can undo the operation
500Sstevel@tonic-gate  * by another such operation, using insert and delete character
510Sstevel@tonic-gate  * stuff.  The pointers vU[AD][12] index the buffer vutmp when this
520Sstevel@tonic-gate  * is possible and provide the necessary information.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * The other case is that the change involved multiple lines or that
550Sstevel@tonic-gate  * we have moved away from the line or forgotten how the change was
560Sstevel@tonic-gate  * accomplished.  In this case we do a redisplay and hope that the
570Sstevel@tonic-gate  * low level optimization routines (which don't look for winning
580Sstevel@tonic-gate  * via insert/delete character) will not lose too badly.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate unsigned char	*vUA1, *vUA2;
610Sstevel@tonic-gate unsigned char	*vUD1, *vUD2;
620Sstevel@tonic-gate 
63802Scf46844 void
vUndo(void)64802Scf46844 vUndo(void)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Avoid UU which clobbers ability to do u.
690Sstevel@tonic-gate 	 */
700Sstevel@tonic-gate 	if (vundkind == VNONE || vundkind == VCAPU || vUNDdot != dot) {
71802Scf46844 		(void) beep();
720Sstevel@tonic-gate 		return;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 	CP(vutmp, linebuf);
750Sstevel@tonic-gate 	vUD1 = linebuf; vUD2 = strend(linebuf);
760Sstevel@tonic-gate 	putmk1(dot, vUNDsav);
770Sstevel@tonic-gate 	getDOT();
780Sstevel@tonic-gate 	vUA1 = linebuf; vUA2 = strend(linebuf);
790Sstevel@tonic-gate 	vundkind = VCAPU;
800Sstevel@tonic-gate 	if (state == ONEOPEN || state == HARDOPEN) {
810Sstevel@tonic-gate 		vjumpto(dot, vUNDcurs, 0);
820Sstevel@tonic-gate 		return;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	vdirty(vcline, 1);
850Sstevel@tonic-gate 	if(MB_CUR_MAX > 1)
860Sstevel@tonic-gate 		rewrite = _ON;
870Sstevel@tonic-gate 	vsyncCL();
880Sstevel@tonic-gate 	if(MB_CUR_MAX > 1)
890Sstevel@tonic-gate 		rewrite = _OFF;
900Sstevel@tonic-gate 	cursor = linebuf;
910Sstevel@tonic-gate 	vfixcurs();
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
94802Scf46844 void
vundo(show)950Sstevel@tonic-gate vundo(show)
960Sstevel@tonic-gate bool show;	/* if true update the screen */
970Sstevel@tonic-gate {
98802Scf46844 	int cnt;
99802Scf46844 	line *addr;
100802Scf46844 	unsigned char *cp;
1010Sstevel@tonic-gate 	unsigned char temp[LBSIZE];
1020Sstevel@tonic-gate 	bool savenote;
1030Sstevel@tonic-gate 	int (*OO)();
1040Sstevel@tonic-gate 	short oldhold = hold;
1050Sstevel@tonic-gate 	unsigned multic[MULTI_BYTE_MAX];
1060Sstevel@tonic-gate 	int length;
1070Sstevel@tonic-gate 	wchar_t wchar;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	switch (vundkind) {
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	case VMANYINS:
1120Sstevel@tonic-gate 		wcursor = 0;
1130Sstevel@tonic-gate 		addr1 = undap1;
1140Sstevel@tonic-gate 		addr2 = undap2 - 1;
1150Sstevel@tonic-gate 		vsave();
116802Scf46844 		(void) YANKreg('1');
1170Sstevel@tonic-gate 		notecnt = 0;
1180Sstevel@tonic-gate 		/* fall into ... */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	case VMANY:
1210Sstevel@tonic-gate 	case VMCHNG:
1220Sstevel@tonic-gate 		vsave();
1230Sstevel@tonic-gate 		addr = dot - vcline;
1240Sstevel@tonic-gate 		notecnt = 1;
1250Sstevel@tonic-gate 		if (undkind == UNDPUT && undap1 == undap2) {
126802Scf46844 			(void) beep();
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 		/*
1300Sstevel@tonic-gate 		 * Undo() call below basically replaces undap1 to undap2-1
1310Sstevel@tonic-gate 		 * with dol through unddol-1.  Hack screen image to
1320Sstevel@tonic-gate 		 * reflect this replacement.
1330Sstevel@tonic-gate 		 */
1340Sstevel@tonic-gate 		if (show)
1350Sstevel@tonic-gate 			if (undkind == UNDMOVE)
1360Sstevel@tonic-gate 				vdirty(0, lines);
1370Sstevel@tonic-gate 			else
1380Sstevel@tonic-gate 				vreplace(undap1 - addr, undap2 - undap1,
1390Sstevel@tonic-gate 				    undkind == UNDPUT ? 0 : unddol - dol);
1400Sstevel@tonic-gate 		savenote = notecnt;
1410Sstevel@tonic-gate 		undo(1);
1420Sstevel@tonic-gate 		if (show && (vundkind != VMCHNG || addr != dot))
1430Sstevel@tonic-gate 			killU();
1440Sstevel@tonic-gate 		vundkind = VMANY;
1450Sstevel@tonic-gate 		cnt = dot - addr;
1460Sstevel@tonic-gate 		if (cnt < 0 || cnt > vcnt || state != VISUAL) {
1470Sstevel@tonic-gate 			if (show)
148802Scf46844 				vjumpto(dot, (unsigned char *)NOSTR, '.');
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 		if (!savenote)
1520Sstevel@tonic-gate 			notecnt = 0;
1530Sstevel@tonic-gate 		if (show) {
1540Sstevel@tonic-gate 			vcline = cnt;
1550Sstevel@tonic-gate 			if(MB_CUR_MAX > 1)
1560Sstevel@tonic-gate 				rewrite = _ON;
1570Sstevel@tonic-gate 			vrepaint(vmcurs);
1580Sstevel@tonic-gate 			if(MB_CUR_MAX > 1)
1590Sstevel@tonic-gate 				rewrite = _OFF;
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 		vmcurs = 0;
1620Sstevel@tonic-gate 		break;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	case VCHNG:
1650Sstevel@tonic-gate 	case VCAPU:
1660Sstevel@tonic-gate 		vundkind = VCHNG;
1670Sstevel@tonic-gate 		strcpy(temp, vutmp);
1680Sstevel@tonic-gate 		strcpy(vutmp, linebuf);
1690Sstevel@tonic-gate 		doomed = lcolumn(vUA2) - lcolumn(vUA1);
1700Sstevel@tonic-gate 		strcLIN(temp);
1710Sstevel@tonic-gate 		cp = vUA1; vUA1 = vUD1; vUD1 = cp;
1720Sstevel@tonic-gate 		cp = vUA2; vUA2 = vUD2; vUD2 = cp;
1730Sstevel@tonic-gate 		if (!show)
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate 		cursor = vUD1;
1760Sstevel@tonic-gate 		if (state == HARDOPEN) {
1770Sstevel@tonic-gate 			doomed = 0;
1780Sstevel@tonic-gate 			vsave();
1790Sstevel@tonic-gate 			vopen(dot, WBOT);
1800Sstevel@tonic-gate 			vnline(cursor);
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 		/*
1840Sstevel@tonic-gate 		 * Pseudo insert command.
1850Sstevel@tonic-gate 		 */
1860Sstevel@tonic-gate 		vcursat(cursor);
1870Sstevel@tonic-gate 		OO = Outchar; Outchar = vinschar; hold |= HOLDQIK;
1880Sstevel@tonic-gate 		vprepins();
1890Sstevel@tonic-gate 		temp[vUA2 - linebuf] = 0;
1900Sstevel@tonic-gate 		for (cp = &temp[vUA1 - linebuf]; *cp;) {
1910Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX);
1920Sstevel@tonic-gate 			if(length < 0) {
1930Sstevel@tonic-gate 				putoctal = 1;
1940Sstevel@tonic-gate 				putchar(*cp++);
1950Sstevel@tonic-gate 				putoctal = 0;
1960Sstevel@tonic-gate 			} else {
1970Sstevel@tonic-gate 				putchar(wchar);
1980Sstevel@tonic-gate 				cp += length;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		Outchar = OO; hold = oldhold;
2020Sstevel@tonic-gate 		endim();
2030Sstevel@tonic-gate 		physdc(cindent(), cindent() + doomed);
2040Sstevel@tonic-gate 		doomed = 0;
2050Sstevel@tonic-gate 		vdirty(vcline, 1);
2060Sstevel@tonic-gate 		if(MB_CUR_MAX > 1)
2070Sstevel@tonic-gate 			rewrite = _ON;
2080Sstevel@tonic-gate 		vsyncCL();
2090Sstevel@tonic-gate 		if(MB_CUR_MAX > 1)
2100Sstevel@tonic-gate 			rewrite = _OFF;
2110Sstevel@tonic-gate 		if (cursor > linebuf && cursor >= strend(linebuf))
2120Sstevel@tonic-gate 			cursor = lastchr(linebuf, cursor);
2130Sstevel@tonic-gate 		vfixcurs();
2140Sstevel@tonic-gate 		break;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	case VNONE:
217802Scf46844 		(void) beep();
2180Sstevel@tonic-gate 		break;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * Routine to handle a change inside a macro.
2240Sstevel@tonic-gate  * Fromvis is true if we were called from a visual command (as
2250Sstevel@tonic-gate  * opposed to an ex command).  This has nothing to do with being
2260Sstevel@tonic-gate  * in open/visual mode as :s/foo/bar is not fromvis.
2270Sstevel@tonic-gate  */
228802Scf46844 void
vmacchng(fromvis)2290Sstevel@tonic-gate vmacchng(fromvis)
2300Sstevel@tonic-gate bool fromvis;
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	line *savedot, *savedol;
2330Sstevel@tonic-gate 	unsigned char *savecursor;
2340Sstevel@tonic-gate 	unsigned char savelb[LBSIZE];
2350Sstevel@tonic-gate 	int nlines, more;
236802Scf46844 	line *a1, *a2;
2370Sstevel@tonic-gate 	unsigned char ch;	/* DEBUG */
2380Sstevel@tonic-gate 	int copyw(), copywR();
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (!inopen)
2410Sstevel@tonic-gate 		return;
2420Sstevel@tonic-gate 	if (!vmacp)
2430Sstevel@tonic-gate 		vch_mac = VC_NOTINMAC;
2440Sstevel@tonic-gate #ifdef UNDOTRACE
2450Sstevel@tonic-gate 	if (trace)
2460Sstevel@tonic-gate 		fprintf(trace, "vmacchng, vch_mac=%d, linebuf='%s', *dot=%o\n", vch_mac, linebuf, *dot);
2470Sstevel@tonic-gate #endif
2480Sstevel@tonic-gate 	if (vmacp && fromvis)
2490Sstevel@tonic-gate 		vsave();
2500Sstevel@tonic-gate #ifdef UNDOTRACE
2510Sstevel@tonic-gate 	if (trace)
2520Sstevel@tonic-gate 		fprintf(trace, "after vsave, linebuf='%s', *dot=%o\n", linebuf, *dot);
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate 	switch(vch_mac) {
2550Sstevel@tonic-gate 	case VC_NOCHANGE:
2560Sstevel@tonic-gate 		vch_mac = VC_ONECHANGE;
2570Sstevel@tonic-gate 		break;
2580Sstevel@tonic-gate 	case VC_ONECHANGE:
2590Sstevel@tonic-gate 		/* Save current state somewhere */
2600Sstevel@tonic-gate #ifdef UNDOTRACE
2610Sstevel@tonic-gate 		vudump("before vmacchng hairy case");
2620Sstevel@tonic-gate #endif
2630Sstevel@tonic-gate 		savedot = dot; savedol = dol; savecursor = cursor;
2640Sstevel@tonic-gate 		CP(savelb, linebuf);
2650Sstevel@tonic-gate 		nlines = dol - zero;
2660Sstevel@tonic-gate 		while ((line *) endcore - truedol < nlines)
2670Sstevel@tonic-gate 			if (morelines() < 0)
2680Sstevel@tonic-gate 				return;	/* or could be fatal error */
2690Sstevel@tonic-gate 		copyw(truedol+1, zero+1, nlines);
2700Sstevel@tonic-gate 		truedol += nlines;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate #ifdef UNDOTRACE
2730Sstevel@tonic-gate 		visdump("before vundo");
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate 		/* Restore state as it was at beginning of macro */
2760Sstevel@tonic-gate 		vundo(0);
2770Sstevel@tonic-gate #ifdef UNDOTRACE
2780Sstevel@tonic-gate 		visdump("after vundo");
2790Sstevel@tonic-gate 		vudump("after vundo");
2800Sstevel@tonic-gate #endif
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		/* Do the saveall we should have done then */
2830Sstevel@tonic-gate 		saveall();
2840Sstevel@tonic-gate #ifdef UNDOTRACE
2850Sstevel@tonic-gate 		vudump("after saveall");
2860Sstevel@tonic-gate #endif
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		/* Restore current state from where saved */
2890Sstevel@tonic-gate 		more = savedol - dol; /* amount we shift everything by */
2900Sstevel@tonic-gate 		if (more)
2910Sstevel@tonic-gate 			(*(more>0 ? copywR : copyw))(savedol+1, dol+1, truedol-dol);
2920Sstevel@tonic-gate 		unddol += more; truedol += more; undap2 += more;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		truedol -= nlines;
2950Sstevel@tonic-gate 		copyw(zero+1, truedol+1, nlines);
2960Sstevel@tonic-gate 		dot = savedot; dol = savedol ; cursor = savecursor;
2970Sstevel@tonic-gate 		CP(linebuf, savelb);
2980Sstevel@tonic-gate 		vch_mac = VC_MANYCHANGE;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		/* Arrange that no further undo saving happens within macro */
3010Sstevel@tonic-gate 		otchng = tchng;	/* Copied this line blindly - bug? */
3020Sstevel@tonic-gate 		inopen = -1;	/* no need to save since it had to be 1 or -1 before */
3030Sstevel@tonic-gate 		vundkind = VMANY;
3040Sstevel@tonic-gate #ifdef UNDOTRACE
3050Sstevel@tonic-gate 		vudump("after vmacchng");
3060Sstevel@tonic-gate #endif
3070Sstevel@tonic-gate 		break;
3080Sstevel@tonic-gate 	case VC_NOTINMAC:
3090Sstevel@tonic-gate 	case VC_MANYCHANGE:
3100Sstevel@tonic-gate 		/* Nothing to do for various reasons. */
3110Sstevel@tonic-gate 		break;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * Initialize undo information before an append.
3170Sstevel@tonic-gate  */
318802Scf46844 void
vnoapp(void)319802Scf46844 vnoapp(void)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	vUD1 = vUD2 = cursor;
3223806Scf46844 	/*
3233806Scf46844 	 * XPG6 assertion 273: Set vmcurs so that undo positions the
3243806Scf46844 	 * cursor column correctly when we've moved off the initial
3253806Scf46844 	 * line that was changed with the A, a, i, and R commands,
3263806Scf46844 	 * eg: when G has moved us off the line, or when a
3273806Scf46844 	 * multi-line change was done.
3283806Scf46844 	 */
3293806Scf46844 	if (lastcmd[0] == 'A' || lastcmd[0] == 'a' || lastcmd[0] == 'i' ||
3303806Scf46844 	    lastcmd[0] == 'R') {
3313806Scf46844 		vmcurs = cursor;
3323806Scf46844 	}
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate  * All the rest of the motion sequences have one or more
3370Sstevel@tonic-gate  * cases to deal with.  In the case wdot == 0, operation
3380Sstevel@tonic-gate  * is totally within current line, from cursor to wcursor.
3390Sstevel@tonic-gate  * If wdot is given, but wcursor is 0, then operation affects
3400Sstevel@tonic-gate  * the inclusive line range.  The hardest case is when both wdot
3410Sstevel@tonic-gate  * and wcursor are given, then operation affects from line dot at
3420Sstevel@tonic-gate  * cursor to line wdot at wcursor.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Move is simple, except for moving onto new lines in hardcopy open mode.
3470Sstevel@tonic-gate  */
348802Scf46844 int
vmove(void)349802Scf46844 vmove(void)
3500Sstevel@tonic-gate {
351802Scf46844 	int cnt;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (wdot) {
3540Sstevel@tonic-gate 		if (wdot < one || wdot > dol) {
355802Scf46844 			(void) beep();
356802Scf46844 			return (0);
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 		cnt = wdot - dot;
3590Sstevel@tonic-gate 		wdot = NOLINE;
3600Sstevel@tonic-gate 		if (cnt)
3610Sstevel@tonic-gate 			killU();
3620Sstevel@tonic-gate 		vupdown(cnt, wcursor);
363802Scf46844 		return (0);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * When we move onto a new line, save information for U undo.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if (vUNDdot != dot) {
3700Sstevel@tonic-gate 		vUNDsav = *dot;
3710Sstevel@tonic-gate 		vUNDcurs = wcursor;
3720Sstevel@tonic-gate 		vUNDdot = dot;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * In hardcopy open, type characters to left of cursor
3770Sstevel@tonic-gate 	 * on new line, or back cursor up if its to left of where we are.
3780Sstevel@tonic-gate 	 * In any case if the current line is ``rubbled'' i.e. has trashy
3790Sstevel@tonic-gate 	 * looking overstrikes on it or \'s from deletes, we reprint
3800Sstevel@tonic-gate 	 * so it is more comprehensible (and also because we can't work
3810Sstevel@tonic-gate 	 * if we let it get more out of sync since column() won't work right.
3820Sstevel@tonic-gate 	 */
3830Sstevel@tonic-gate 	if (state == HARDOPEN) {
384802Scf46844 		unsigned char *cp;
3850Sstevel@tonic-gate 		if (rubble) {
386802Scf46844 			int c;
3870Sstevel@tonic-gate 			int oldhold = hold;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 			sethard();
3900Sstevel@tonic-gate 			cp = wcursor;
3910Sstevel@tonic-gate 			c = *cp;
3920Sstevel@tonic-gate 			*cp = 0;
3930Sstevel@tonic-gate 			hold |= HOLDDOL;
394802Scf46844 			(void) vreopen(WTOP, lineDOT(), vcline);
3950Sstevel@tonic-gate 			hold = oldhold;
3960Sstevel@tonic-gate 			*cp = c;
3970Sstevel@tonic-gate 		} else if (wcursor > cursor) {
398802Scf46844 			int length;
3990Sstevel@tonic-gate 			char multic[MULTI_BYTE_MAX];
4000Sstevel@tonic-gate 			wchar_t wchar;
4010Sstevel@tonic-gate 			vfixcurs();
4020Sstevel@tonic-gate 			for (cp = cursor; *cp && cp < wcursor;) {
4030Sstevel@tonic-gate 				length = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX);
4040Sstevel@tonic-gate 				if(length == 0)
4050Sstevel@tonic-gate 					putchar(' ');
4060Sstevel@tonic-gate 				else if(length < 0) {
4070Sstevel@tonic-gate 					putoctal = 1;
4080Sstevel@tonic-gate 					putchar(*cp++);
4090Sstevel@tonic-gate 					putoctal = 0;
4100Sstevel@tonic-gate 				} else {
4110Sstevel@tonic-gate 					cp += length;
4120Sstevel@tonic-gate 					putchar(wchar);
4130Sstevel@tonic-gate 				}
4140Sstevel@tonic-gate 			}
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	vsetcurs(wcursor);
418802Scf46844 	return (0);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Delete operator.
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * Hard case of deleting a range where both wcursor and wdot
4250Sstevel@tonic-gate  * are specified is treated as a special case of change and handled
4260Sstevel@tonic-gate  * by vchange (although vchange may pass it back if it degenerates
4270Sstevel@tonic-gate  * to a full line range delete.)
4280Sstevel@tonic-gate  */
429802Scf46844 int
vdelete(unsigned char c)430802Scf46844 vdelete(unsigned char c)
4310Sstevel@tonic-gate {
432802Scf46844 	unsigned char *cp;
433802Scf46844 	int i;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (wdot) {
4360Sstevel@tonic-gate 		if (wcursor) {
437802Scf46844 			(void) vchange('d');
438802Scf46844 			return (0);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		if ((i = xdw()) < 0)
441802Scf46844 			return (0);
4420Sstevel@tonic-gate 		if (state != VISUAL) {
4430Sstevel@tonic-gate 			vgoto(LINE(0), 0);
444802Scf46844 			(void) vputchar('@');
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 		wdot = dot;
4470Sstevel@tonic-gate 		vremote(i, delete, 0);
4480Sstevel@tonic-gate 		notenam = (unsigned char *)"delete";
4490Sstevel@tonic-gate 		DEL[0] = 0;
4500Sstevel@tonic-gate 		killU();
4510Sstevel@tonic-gate 		vreplace(vcline, i, 0);
4520Sstevel@tonic-gate 		if (wdot > dol)
4530Sstevel@tonic-gate 			vcline--;
4540Sstevel@tonic-gate 		vrepaint(NOSTR);
455802Scf46844 		return (0);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	if (wcursor < linebuf)
4580Sstevel@tonic-gate 		wcursor = linebuf;
4590Sstevel@tonic-gate 	if (cursor == wcursor) {
460802Scf46844 		(void) beep();
461802Scf46844 		return (0);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 	i = vdcMID();
4640Sstevel@tonic-gate 	cp = cursor;
4650Sstevel@tonic-gate 	setDEL();
4660Sstevel@tonic-gate 	CP(cp, wcursor);
4670Sstevel@tonic-gate 	if (cp > linebuf && (cp[0] == 0 || c == '#'))
4680Sstevel@tonic-gate 		cp = lastchr(linebuf, cp);
4690Sstevel@tonic-gate 	if (state == HARDOPEN) {
4700Sstevel@tonic-gate 		bleep(i, cp);
4710Sstevel@tonic-gate 		cursor = cp;
472802Scf46844 		return (0);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	physdc(lcolumn(cursor), i);
4750Sstevel@tonic-gate 	DEPTH(vcline) = 0;
4760Sstevel@tonic-gate 	if(MB_CUR_MAX > 1)
4770Sstevel@tonic-gate 		rewrite = _ON;
478802Scf46844 	(void) vreopen(LINE(vcline), lineDOT(), vcline);
4790Sstevel@tonic-gate 	if(MB_CUR_MAX > 1)
4800Sstevel@tonic-gate 		rewrite = _OFF;
4810Sstevel@tonic-gate 	vsyncCL();
4820Sstevel@tonic-gate 	vsetcurs(cp);
483802Scf46844 	return (0);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate  * Change operator.
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * In a single line we mark the end of the changed area with '$'.
4900Sstevel@tonic-gate  * On multiple whole lines, we clear the lines first.
4910Sstevel@tonic-gate  * Across lines with both wcursor and wdot given, we delete
4920Sstevel@tonic-gate  * and sync then append (but one operation for undo).
4930Sstevel@tonic-gate  */
494802Scf46844 int
vchange(unsigned char c)495802Scf46844 vchange(unsigned char c)
4960Sstevel@tonic-gate {
497802Scf46844 	unsigned char *cp;
498802Scf46844 	int i, ind, cnt;
4990Sstevel@tonic-gate 	line *addr;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if (wdot) {
5020Sstevel@tonic-gate 		/*
5030Sstevel@tonic-gate 		 * Change/delete of lines or across line boundaries.
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 		if ((cnt = xdw()) < 0)
506802Scf46844 			return (0);
5070Sstevel@tonic-gate 		getDOT();
5080Sstevel@tonic-gate 		if (wcursor && cnt == 1) {
5090Sstevel@tonic-gate 			/*
5100Sstevel@tonic-gate 			 * Not really.
5110Sstevel@tonic-gate 			 */
5120Sstevel@tonic-gate 			wdot = 0;
5130Sstevel@tonic-gate 			if (c == 'd') {
514802Scf46844 				(void) vdelete(c);
515802Scf46844 				return (0);
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 			goto smallchange;
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		if (cursor && wcursor) {
5200Sstevel@tonic-gate 			/*
5210Sstevel@tonic-gate 			 * Across line boundaries, but not
5220Sstevel@tonic-gate 			 * necessarily whole lines.
5230Sstevel@tonic-gate 			 * Construct what will be left.
5240Sstevel@tonic-gate 			 */
5250Sstevel@tonic-gate 			*cursor = 0;
5260Sstevel@tonic-gate 			strcpy(genbuf, linebuf);
527*13093SRoger.Faulkner@Oracle.COM 			getaline(*wdot);
5280Sstevel@tonic-gate 			if (strlen(genbuf) + strlen(wcursor) > LBSIZE - 2) {
5290Sstevel@tonic-gate 				getDOT();
530802Scf46844 				(void) beep();
531802Scf46844 				return (0);
5320Sstevel@tonic-gate 			}
5330Sstevel@tonic-gate 			strcat(genbuf, wcursor);
5340Sstevel@tonic-gate 			if (c == 'd' && *vpastwh(genbuf) == 0) {
5350Sstevel@tonic-gate 				/*
5360Sstevel@tonic-gate 				 * Although this is a delete
5370Sstevel@tonic-gate 				 * spanning line boundaries, what
5380Sstevel@tonic-gate 				 * would be left is all white space,
5390Sstevel@tonic-gate 				 * so take it all away.
5400Sstevel@tonic-gate 				 */
5410Sstevel@tonic-gate 				wcursor = 0;
5420Sstevel@tonic-gate 				getDOT();
5430Sstevel@tonic-gate 				op = 0;
5440Sstevel@tonic-gate 				notpart(lastreg);
5450Sstevel@tonic-gate 				notpart('1');
546802Scf46844 				(void) vdelete(c);
547802Scf46844 				return (0);
5480Sstevel@tonic-gate 			}
5490Sstevel@tonic-gate 			ind = -1;
5500Sstevel@tonic-gate 		} else if (c == 'd' && wcursor == 0) {
551802Scf46844 			(void) vdelete(c);
552802Scf46844 			return (0);
5530Sstevel@tonic-gate 		} else
5540Sstevel@tonic-gate 			/*
5550Sstevel@tonic-gate 			 * We are just substituting text for whole lines,
5560Sstevel@tonic-gate 			 * so determine the first autoindent.
5570Sstevel@tonic-gate 			 */
5580Sstevel@tonic-gate 			if (value(vi_LISP) && value(vi_AUTOINDENT))
5590Sstevel@tonic-gate 				ind = lindent(dot);
5600Sstevel@tonic-gate 			else
5610Sstevel@tonic-gate 				ind = whitecnt(linebuf);
5620Sstevel@tonic-gate 		i = vcline >= 0 ? LINE(vcline) : WTOP;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		/*
5650Sstevel@tonic-gate 		 * Delete the lines from the buffer,
5660Sstevel@tonic-gate 		 * and remember how the partial stuff came about in
5670Sstevel@tonic-gate 		 * case we are told to put.
5680Sstevel@tonic-gate 		 */
5690Sstevel@tonic-gate 		addr = dot;
5700Sstevel@tonic-gate 		vremote(cnt, delete, 0);
5710Sstevel@tonic-gate 		setpk();
5720Sstevel@tonic-gate 		notenam = (unsigned char *)"delete";
5730Sstevel@tonic-gate 		if (c != 'd')
5740Sstevel@tonic-gate 			notenam = (unsigned char *)"change";
5750Sstevel@tonic-gate 		/*
5760Sstevel@tonic-gate 		 * If DEL[0] were nonzero, put would put it back
5770Sstevel@tonic-gate 		 * rather than the deleted lines.
5780Sstevel@tonic-gate 		 */
5790Sstevel@tonic-gate 		DEL[0] = 0;
5800Sstevel@tonic-gate 		if (cnt > 1)
5810Sstevel@tonic-gate 			killU();
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		/*
5840Sstevel@tonic-gate 		 * Now hack the screen image coordination.
5850Sstevel@tonic-gate 		 */
5860Sstevel@tonic-gate 		vreplace(vcline, cnt, 0);
5870Sstevel@tonic-gate 		wdot = NOLINE;
5880Sstevel@tonic-gate 		noteit(0);
5890Sstevel@tonic-gate 		vcline--;
5900Sstevel@tonic-gate 		if (addr <= dol)
5910Sstevel@tonic-gate 			dot--;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * If this is a across line delete/change,
5950Sstevel@tonic-gate 		 * cursor stays where it is; just splice together the pieces
5960Sstevel@tonic-gate 		 * of the new line.  Otherwise generate a autoindent
5970Sstevel@tonic-gate 		 * after a S command.
5980Sstevel@tonic-gate 		 */
5990Sstevel@tonic-gate 		if (ind >= 0) {
6003806Scf46844 			/*
6013806Scf46844 			 * XPG6 assertion 273: Set vmcurs so that cursor
6023806Scf46844 			 * column will be set by undo.
6033806Scf46844 			 */
6043806Scf46844 			fixundo();
6050Sstevel@tonic-gate 			*genindent(ind) = 0;
6060Sstevel@tonic-gate 			vdoappend(genbuf);
6070Sstevel@tonic-gate 		} else {
6080Sstevel@tonic-gate 			vmcurs = cursor;
6090Sstevel@tonic-gate 			strcLIN(genbuf);
6100Sstevel@tonic-gate 			vdoappend(linebuf);
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		/*
6140Sstevel@tonic-gate 		 * Indicate a change on hardcopies by
6150Sstevel@tonic-gate 		 * erasing the current line.
6160Sstevel@tonic-gate 		 */
6170Sstevel@tonic-gate 		if (c != 'd' && state != VISUAL && state != HARDOPEN) {
6180Sstevel@tonic-gate 			int oldhold = hold;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 			hold |= HOLDAT, vclrlin(i, dot), hold = oldhold;
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		/*
6240Sstevel@tonic-gate 		 * Open the line (logically) on the screen, and
6250Sstevel@tonic-gate 		 * update the screen tail.  Unless we are really a delete
6260Sstevel@tonic-gate 		 * go off and gather up inserted characters.
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		vcline++;
6290Sstevel@tonic-gate 		if (vcline < 0)
6300Sstevel@tonic-gate 			vcline = 0;
6310Sstevel@tonic-gate 		vopen(dot, i);
6320Sstevel@tonic-gate 		vsyncCL();
6330Sstevel@tonic-gate 		noteit(1);
6340Sstevel@tonic-gate 		if (c != 'd') {
6350Sstevel@tonic-gate 			if (ind >= 0) {
6360Sstevel@tonic-gate 				cursor = linebuf;
6373806Scf46844 				/*
6383806Scf46844 				 * XPG6 assertion 273: Set vmcurs so that
6393806Scf46844 				 * cursor column will be set by undo.  When
6403806Scf46844 				 * undo is preceded by 'S' or 'O' command,
6413806Scf46844 				 * white space isn't skipped in vnline(vmcurs).
6423806Scf46844 				 */
6433806Scf46844 				fixundo();
6440Sstevel@tonic-gate 				linebuf[0] = 0;
6450Sstevel@tonic-gate 				vfixcurs();
6460Sstevel@tonic-gate 			} else {
6470Sstevel@tonic-gate 				ind = 0;
6483806Scf46844 				/*
6493806Scf46844 				 * XPG6 assertion 273: Set vmcurs so that
6503806Scf46844 				 * cursor column will be set by undo.
6513806Scf46844 				 */
6523806Scf46844 				fixundo();
6530Sstevel@tonic-gate 				vcursat(cursor);
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 			vappend('x', 1, ind);
656802Scf46844 			return (0);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		if (*cursor == 0 && cursor > linebuf)
6590Sstevel@tonic-gate 			cursor = lastchr(linebuf, cursor);
6600Sstevel@tonic-gate 		vrepaint(cursor);
661802Scf46844 		return (0);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate smallchange:
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * The rest of this is just low level hacking on changes
6670Sstevel@tonic-gate 	 * of small numbers of characters.
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	if (wcursor < linebuf)
6700Sstevel@tonic-gate 		wcursor = linebuf;
6710Sstevel@tonic-gate 	if (cursor == wcursor) {
672802Scf46844 		(void) beep();
673802Scf46844 		return (0);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 	i = vdcMID();
6760Sstevel@tonic-gate 	cp = cursor;
6770Sstevel@tonic-gate 	if (state != HARDOPEN)
6780Sstevel@tonic-gate 		vfixcurs();
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * Put out the \\'s indicating changed text in hardcopy,
6820Sstevel@tonic-gate 	 * or mark the end of the change with $ if not hardcopy.
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	if (state == HARDOPEN)
6850Sstevel@tonic-gate 		bleep(i, cp);
6860Sstevel@tonic-gate 	else {
6870Sstevel@tonic-gate 		vcursbef(wcursor);
6880Sstevel@tonic-gate 		putchar('$');
6890Sstevel@tonic-gate 		i = cindent();
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Remember the deleted text for possible put,
6940Sstevel@tonic-gate 	 * and then prepare and execute the input portion of the change.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	cursor = cp;
6970Sstevel@tonic-gate 	setDEL();
6980Sstevel@tonic-gate 	CP(cursor, wcursor);
6993806Scf46844 	/*
7003806Scf46844 	 * XPG6 assertion 273: Set vmcurs so that cursor column will be
7013806Scf46844 	 * set by undo.
7023806Scf46844 	 */
7033806Scf46844 	fixundo();
7040Sstevel@tonic-gate 	if (state != HARDOPEN) {
7050Sstevel@tonic-gate 		/* place cursor at beginning of changing text */
7060Sstevel@tonic-gate 		vgotoCL(lcolumn(cp));
7070Sstevel@tonic-gate 		doomed = i - cindent();
7080Sstevel@tonic-gate 	} else {
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate 		sethard();
7110Sstevel@tonic-gate 		wcursor = cursor;
7120Sstevel@tonic-gate 		cursor = linebuf;
7130Sstevel@tonic-gate 		vgoto(outline, value(vi_NUMBER) << 3);
7140Sstevel@tonic-gate 		vmove();
7150Sstevel@tonic-gate */
7160Sstevel@tonic-gate 		doomed = 0;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	prepapp();
7190Sstevel@tonic-gate 	vappend('c', 1, 0);
720802Scf46844 	return (0);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * Open new lines.
7250Sstevel@tonic-gate  */
726802Scf46844 void
voOpen(int c,int cnt)727802Scf46844 voOpen(int c, int cnt)
7280Sstevel@tonic-gate {
729802Scf46844 	int ind = 0, i;
7300Sstevel@tonic-gate 	short oldhold = hold;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	vsave();
7330Sstevel@tonic-gate 	setLAST();
7340Sstevel@tonic-gate 	if (value(vi_AUTOINDENT))
7350Sstevel@tonic-gate 		ind = whitecnt(linebuf);
7360Sstevel@tonic-gate 	if (c == 'O') {
7370Sstevel@tonic-gate 		vcline--;
7380Sstevel@tonic-gate 		dot--;
7390Sstevel@tonic-gate 		if (dot > zero)
7400Sstevel@tonic-gate 			getDOT();
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	if (value(vi_AUTOINDENT)) {
7430Sstevel@tonic-gate 		if (value(vi_LISP))
7440Sstevel@tonic-gate 			ind = lindent(dot + 1);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	killU();
7470Sstevel@tonic-gate 	prepapp();
7480Sstevel@tonic-gate 	if (FIXUNDO)
7490Sstevel@tonic-gate 		vundkind = VMANY;
7500Sstevel@tonic-gate 	if (state != VISUAL)
7510Sstevel@tonic-gate 		c = WBOT + 1;
7520Sstevel@tonic-gate 	else {
7530Sstevel@tonic-gate 		c = vcline < 0 ? WTOP - cnt : LINE(vcline) + DEPTH(vcline);
7540Sstevel@tonic-gate 		if (c < ZERO)
7550Sstevel@tonic-gate 			c = ZERO;
7560Sstevel@tonic-gate 		i = LINE(vcline + 1) - c;
7570Sstevel@tonic-gate 		if (i < cnt && c <= WBOT && (!insert_line || !delete_line))
7580Sstevel@tonic-gate 			vinslin(c, cnt - i, vcline);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	*genindent(ind) = 0;
7610Sstevel@tonic-gate 	vdoappend(genbuf);
7620Sstevel@tonic-gate 	vcline++;
7630Sstevel@tonic-gate 	oldhold = hold;
7640Sstevel@tonic-gate 	hold |= HOLDROL;
7650Sstevel@tonic-gate 	vopen(dot, c);
7660Sstevel@tonic-gate 	hold = oldhold;
7670Sstevel@tonic-gate 	if (value(vi_SLOWOPEN))
7680Sstevel@tonic-gate 		/*
7690Sstevel@tonic-gate 		 * Oh, so lazy!
7700Sstevel@tonic-gate 		 */
7710Sstevel@tonic-gate 		vscrap();
7720Sstevel@tonic-gate 	else
7730Sstevel@tonic-gate 		vsync1(LINE(vcline));
7740Sstevel@tonic-gate 	cursor = linebuf;
7753806Scf46844 	/*
7763806Scf46844 	 * XPG6 assertion 273: Set vmcurs so that cursor column will be
7773806Scf46844 	 * set by undo.  For undo preceded by 'o' command, white space
7783806Scf46844 	 * isn't skipped in vnline(vmcurs).
7793806Scf46844 	 */
7803806Scf46844 	fixundo();
7810Sstevel@tonic-gate 	linebuf[0] = 0;
7821406Scf46844 	vappend('o', cnt, ind);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate  * > < and = shift operators.
7870Sstevel@tonic-gate  *
7880Sstevel@tonic-gate  * Note that =, which aligns lisp, is just a ragged sort of shift,
7890Sstevel@tonic-gate  * since it never distributes text between lines.
7900Sstevel@tonic-gate  */
7910Sstevel@tonic-gate unsigned char	vshnam[2] = { 'x', 0 };
7920Sstevel@tonic-gate 
793802Scf46844 int
vshftop(void)794802Scf46844 vshftop(void)
7950Sstevel@tonic-gate {
796802Scf46844 	line *addr;
797802Scf46844 	int cnt;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	if ((cnt = xdw()) < 0)
800802Scf46844 		return (0);
8010Sstevel@tonic-gate 	addr = dot;
8020Sstevel@tonic-gate 	vremote(cnt, vshift, 0);
8030Sstevel@tonic-gate 	vshnam[0] = op;
8040Sstevel@tonic-gate 	notenam = vshnam;
8050Sstevel@tonic-gate 	dot = addr;
8060Sstevel@tonic-gate 	vreplace(vcline, cnt, cnt);
8070Sstevel@tonic-gate 	if (state == HARDOPEN)
8080Sstevel@tonic-gate 		vcnt = 0;
8090Sstevel@tonic-gate 	vrepaint(NOSTR);
810802Scf46844 	return (0);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate /*
8140Sstevel@tonic-gate  * !.
8150Sstevel@tonic-gate  *
8160Sstevel@tonic-gate  * Filter portions of the buffer through unix commands.
8170Sstevel@tonic-gate  */
818802Scf46844 int
vfilter(void)819802Scf46844 vfilter(void)
8200Sstevel@tonic-gate {
821802Scf46844 	line *addr;
822802Scf46844 	int cnt;
8230Sstevel@tonic-gate 	unsigned char *oglobp;
8240Sstevel@tonic-gate 	short d;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if ((cnt = xdw()) < 0)
827802Scf46844 		return (0);
8280Sstevel@tonic-gate 	if (vglobp)
8290Sstevel@tonic-gate 		vglobp = (unsigned char *)uxb;
8300Sstevel@tonic-gate 	if (readecho('!'))
831802Scf46844 		return (0);
8320Sstevel@tonic-gate 	oglobp = globp; globp = genbuf + 1;
8330Sstevel@tonic-gate 	d = peekc; ungetchar(0);
8340Sstevel@tonic-gate 	CATCH
8350Sstevel@tonic-gate 		fixech();
836500Sceastha 		unix0(0, 0);
8370Sstevel@tonic-gate 	ONERR
8380Sstevel@tonic-gate 		splitw = 0;
8390Sstevel@tonic-gate 		ungetchar(d);
8400Sstevel@tonic-gate 		vrepaint(cursor);
8410Sstevel@tonic-gate 		globp = oglobp;
842802Scf46844 		return (0);
8430Sstevel@tonic-gate 	ENDCATCH
8440Sstevel@tonic-gate 	ungetchar(d); globp = oglobp;
8450Sstevel@tonic-gate 	addr = dot;
8460Sstevel@tonic-gate 	CATCH
8470Sstevel@tonic-gate 		vgoto(WECHO, 0); flusho();
8480Sstevel@tonic-gate 		vremote(cnt, vi_filter, 2);
8490Sstevel@tonic-gate 	ONERR
8500Sstevel@tonic-gate 		vdirty(0, lines);
8510Sstevel@tonic-gate 	ENDCATCH
8520Sstevel@tonic-gate 	if (dot == zero && dol > zero)
8530Sstevel@tonic-gate 		dot = one;
8540Sstevel@tonic-gate 	splitw = 0;
8550Sstevel@tonic-gate 	notenam = (unsigned char *)"";
8560Sstevel@tonic-gate 	/*
8570Sstevel@tonic-gate 	 * BUG: we shouldn't be depending on what undap2 and undap1 are,
8580Sstevel@tonic-gate 	 * since we may be inside a macro.  What's really wanted is the
8590Sstevel@tonic-gate 	 * number of lines we read from the filter.  However, the mistake
8600Sstevel@tonic-gate 	 * will be an overestimate so it only results in extra work,
8610Sstevel@tonic-gate 	 * it shouldn't cause any real mess-ups.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	vreplace(vcline, cnt, undap2 - undap1);
8640Sstevel@tonic-gate 	dot = addr;
8650Sstevel@tonic-gate 	if (dot > dol) {
8660Sstevel@tonic-gate 		dot--;
8670Sstevel@tonic-gate 		vcline--;
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 	vrepaint(NOSTR);
870802Scf46844 	return (0);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * Xdw exchanges dot and wdot if appropriate and also checks
8750Sstevel@tonic-gate  * that wdot is reasonable.  Its name comes from
8760Sstevel@tonic-gate  *	xchange dotand wdot
8770Sstevel@tonic-gate  */
878802Scf46844 int
xdw(void)879802Scf46844 xdw(void)
8800Sstevel@tonic-gate {
881802Scf46844 	unsigned char *cp;
882802Scf46844 	int cnt;
8830Sstevel@tonic-gate /*
8840Sstevel@tonic-gate 	register int notp = 0;
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (wdot == NOLINE || wdot < one || wdot > dol) {
888802Scf46844 		(void) beep();
8890Sstevel@tonic-gate 		return (-1);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 	vsave();
8920Sstevel@tonic-gate 	setLAST();
8930Sstevel@tonic-gate 	if (dot > wdot || (dot == wdot && wcursor != 0 && cursor > wcursor)) {
894802Scf46844 		line *addr;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		vcline -= dot - wdot;
8970Sstevel@tonic-gate 		addr = dot; dot = wdot; wdot = addr;
8980Sstevel@tonic-gate 		cp = cursor; cursor = wcursor; wcursor = cp;
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 	/*
9010Sstevel@tonic-gate 	 * If a region is specified but wcursor is at the beginning
9020Sstevel@tonic-gate 	 * of the last line, then we move it to be the end of the
9030Sstevel@tonic-gate 	 * previous line (actually off the end).
9040Sstevel@tonic-gate 	 */
9050Sstevel@tonic-gate 	if (cursor && wcursor == linebuf && wdot > dot) {
9060Sstevel@tonic-gate 		wdot--;
9070Sstevel@tonic-gate 		getDOT();
9080Sstevel@tonic-gate 		if (vpastwh(linebuf) >= cursor)
9090Sstevel@tonic-gate 			wcursor = 0;
9100Sstevel@tonic-gate 		else {
911*13093SRoger.Faulkner@Oracle.COM 			getaline(*wdot);
9120Sstevel@tonic-gate 			wcursor = strend(linebuf);
9130Sstevel@tonic-gate 			getDOT();
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 		/*
9160Sstevel@tonic-gate 		 * Should prepare in caller for possible dot == wdot.
9170Sstevel@tonic-gate 		 */
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 	cnt = wdot - dot + 1;
9200Sstevel@tonic-gate 	if (vreg) {
9210Sstevel@tonic-gate 		vremote(cnt, YANKreg, vreg);
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate 		if (notp)
9240Sstevel@tonic-gate 			notpart(vreg);
9250Sstevel@tonic-gate  */
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * Kill buffer code.  If delete operator is c or d, then save
9300Sstevel@tonic-gate 	 * the region in numbered buffers.
9310Sstevel@tonic-gate 	 *
9320Sstevel@tonic-gate 	 * BUG:			This may be somewhat inefficient due
9330Sstevel@tonic-gate 	 *			to the way named buffer are implemented,
9340Sstevel@tonic-gate 	 *			necessitating some optimization.
9350Sstevel@tonic-gate 	 */
9360Sstevel@tonic-gate 	vreg = 0;
9373806Scf46844 	/* XPG6 assertion 194 and 264: use numeric buffers for 'C' and 'S' */
9383806Scf46844 	if (any(op, (unsigned char *)"cdCS")) {
9390Sstevel@tonic-gate 		vremote(cnt, YANKreg, '1');
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate 		if (notp)
9420Sstevel@tonic-gate 			notpart('1');
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 	return (cnt);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * Routine for vremote to call to implement shifts.
9500Sstevel@tonic-gate  */
951802Scf46844 int
vshift(void)952802Scf46844 vshift(void)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	shift(op, 1);
956802Scf46844 	return (0);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /*
9600Sstevel@tonic-gate  * Replace a single character with the next input character.
9610Sstevel@tonic-gate  * A funny kind of insert.
9620Sstevel@tonic-gate  */
963802Scf46844 void
vrep(int cnt)964802Scf46844 vrep(int cnt)
9650Sstevel@tonic-gate {
966802Scf46844 	int i, c;
967802Scf46844 	unsigned char *endcurs;
9680Sstevel@tonic-gate 	endcurs = cursor;
9693806Scf46844 	/* point endcurs to last char entered */
9700Sstevel@tonic-gate 	for(i = 1; i <= cnt; i++) {
9710Sstevel@tonic-gate 		if(!*endcurs) {
972802Scf46844 			(void) beep();
9730Sstevel@tonic-gate 			return;
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		endcurs = nextchr(endcurs);
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 	i = lcolumn(endcurs);
9780Sstevel@tonic-gate 	vcursat(cursor);
9790Sstevel@tonic-gate 	doomed = i - cindent();
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * TRANSLATION_NOTE
9820Sstevel@tonic-gate 	 *	"r" is a terse mode message that corresponds to
9830Sstevel@tonic-gate 	 *	"REPLACE 1 CHAR".
9840Sstevel@tonic-gate 	 *	Translated message of "r" must be 1 character (not byte).
9850Sstevel@tonic-gate 	 *	Or, just leave it.
9860Sstevel@tonic-gate 	 */
9870Sstevel@tonic-gate 	if(value(vi_TERSE))
9880Sstevel@tonic-gate 		vshowmode(gettext("r"));
9890Sstevel@tonic-gate 	else
9900Sstevel@tonic-gate 		vshowmode(gettext("REPLACE 1 CHAR"));
9910Sstevel@tonic-gate 	if (!vglobp) {
9923806Scf46844 		/* get a key using getkey() */
9930Sstevel@tonic-gate 		c = getesc();
9940Sstevel@tonic-gate 		if (c == 0) {
9950Sstevel@tonic-gate 			vshowmode("");
9960Sstevel@tonic-gate 			vfixcurs();
9970Sstevel@tonic-gate 			return;
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 		ungetkey(c);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 	CP(vutmp, linebuf);
10020Sstevel@tonic-gate 	if (FIXUNDO)
10030Sstevel@tonic-gate 		vundkind = VCHNG;
10040Sstevel@tonic-gate 	wcursor = endcurs;
10050Sstevel@tonic-gate 	vUD1 = cursor; vUD2 = wcursor;
10060Sstevel@tonic-gate 	CP(cursor, wcursor);
10073806Scf46844 	/* before appending lines, set addr1 and undo information */
10080Sstevel@tonic-gate 	prepapp();
10090Sstevel@tonic-gate 	vappend('r', cnt, 0);
10100Sstevel@tonic-gate 	*lastcp++ = INS[0];
10110Sstevel@tonic-gate 	setLAST();
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate  * Yank.
10160Sstevel@tonic-gate  *
10170Sstevel@tonic-gate  * Yanking to string registers occurs for free (essentially)
10180Sstevel@tonic-gate  * in the routine xdw().
10190Sstevel@tonic-gate  */
1020802Scf46844 int
vyankit(void)1021802Scf46844 vyankit(void)
10220Sstevel@tonic-gate {
1023802Scf46844 	int cnt;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (wdot) {
10260Sstevel@tonic-gate 		if ((cnt = xdw()) < 0)
1027802Scf46844 			return (0);
10280Sstevel@tonic-gate 		vremote(cnt, yank, 0);
10290Sstevel@tonic-gate 		setpk();
10300Sstevel@tonic-gate 		notenam = (unsigned char *)"yank";
10310Sstevel@tonic-gate 		if (FIXUNDO)
10320Sstevel@tonic-gate 			vundkind = VNONE;
10330Sstevel@tonic-gate 		DEL[0] = 0;
10340Sstevel@tonic-gate 		wdot = NOLINE;
10350Sstevel@tonic-gate 		if (notecnt <= vcnt - vcline && notecnt < value(vi_REPORT))
10360Sstevel@tonic-gate 			notecnt = 0;
10370Sstevel@tonic-gate 		vrepaint(cursor);
1038802Scf46844 		return (0);
10391406Scf46844 	} else {
10401406Scf46844 		/*
10411406Scf46844 		 * For one line y<motion> commands, eg. 2yw, save the
10421406Scf46844 		 * command for a subsequent [count].
10431406Scf46844 		 */
10441406Scf46844 		setLAST();
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 	takeout(DEL);
1047802Scf46844 	return (0);
1048802Scf46844 
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate /*
10520Sstevel@tonic-gate  * Set pkill variables so a put can
10530Sstevel@tonic-gate  * know how to put back partial text.
10540Sstevel@tonic-gate  * This is necessary because undo needs the complete
10550Sstevel@tonic-gate  * line images to be saved, while a put wants to trim
10560Sstevel@tonic-gate  * the first and last lines.  The compromise
10570Sstevel@tonic-gate  * is for put to be more clever.
10580Sstevel@tonic-gate  */
1059802Scf46844 void
setpk(void)1060802Scf46844 setpk(void)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	if (wcursor) {
10640Sstevel@tonic-gate 		pkill[0] = cursor;
10650Sstevel@tonic-gate 		pkill[1] = wcursor;
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate }
10683806Scf46844 
10693806Scf46844 /*
10703806Scf46844  * XPG6 assertion 273 : If the command is C, c, o, R, S, or s, set vmcurs
10713806Scf46844  * so that the cursor column will be set by undo.
10723806Scf46844  */
10733806Scf46844 void
fixundo(void)10743806Scf46844 fixundo(void)
10753806Scf46844 {
10763806Scf46844 	if (lastcmd[0] == 'C' || lastcmd[0] == 'c' || lastcmd[0] == 'o' ||
10773806Scf46844 	    lastcmd[0] == 'R' || lastcmd[0] == 'S' || lastcmd[0] == 's') {
10783806Scf46844 		vmcurs = cursor;
10793806Scf46844 	}
10803806Scf46844 }
1081