aboutsummaryrefslogtreecommitdiffstats
path: root/usr.bin/vi/vi/v_text.c
blob: 01d17442fc00bd3f9dad453e83c8577c01ce5f6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@(#)v_text.c	8.30 (Berkeley) 3/14/94";
#endif /* not lint */

#include <sys/types.h>
#include <queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>

#include <db.h>
#include <regex.h>

#include "vi.h"
#include "vcmd.h"

/*
 * !!!
 * Repeated input in the historic vi is mostly wrong and this isn't very
 * backward compatible.  For example, if the user entered "3Aab\ncd" in
 * the historic vi, the "ab" was repeated 3 times, and the "\ncd" was then
 * appended to the result.  There was also a hack which I don't remember
 * right now, where "3o" would open 3 lines and then let the user fill them
 * in, to make screen movements on 300 baud modems more tolerable.  I don't
 * think it's going to be missed.
 */

#define	SET_TXT_STD(sp, f) {						\
	LF_INIT((f) | TXT_CNTRLT | TXT_ESCAPE |				\
	    TXT_MAPINPUT | TXT_RECORD | TXT_RESOLVE);			\
	if (O_ISSET(sp, O_ALTWERASE))					\
		LF_SET(TXT_ALTWERASE);					\
	if (O_ISSET(sp, O_AUTOINDENT))					\
		LF_SET(TXT_AUTOINDENT);					\
	if (O_ISSET(sp, O_BEAUTIFY))					\
		LF_SET(TXT_BEAUTIFY);					\
	if (O_ISSET(sp, O_SHOWMATCH))					\
		LF_SET(TXT_SHOWMATCH);					\
	if (O_ISSET(sp, O_WRAPMARGIN))					\
		LF_SET(TXT_WRAPMARGIN);					\
	if (F_ISSET(sp, S_SCRIPT))					\
		LF_SET(TXT_CR);						\
	if (O_ISSET(sp, O_TTYWERASE))					\
		LF_SET(TXT_TTYWERASE);					\
}

/*
 * !!!
 * There's a problem with the way that we do logging for change commands with
 * implied motions (e.g. A, I, O, cc, etc.).  Since the main vi loop logs the
 * starting cursor position before the change command "moves" the cursor, the
 * cursor position to which we return on undo will be where the user entered
 * the change command, not the start of the change.  Several of the following
 * routines re-log the cursor to make this work correctly.  Historic vi tried
 * to do the same thing, and mostly got it right.  (The only spectacular way
 * it fails is if the user entered 'o' from anywhere but the last character of
 * the line, the undo returned the cursor to the start of the line.  If the
 * user was on the last character of the line, the cursor returned to that
 * position.)  We also check for mapped keys waiting, i.e. if we're in the
 * middle of a map, don't bother logging the cursor.
 */
#define	LOG_CORRECT {							\
	if (!MAPPED_KEYS_WAITING(sp))					\
		(void)log_cursor(sp, ep);				\
}
#define	LOG_CORRECT_FIRST {						\
	if (first == 1) {						\
		LOG_CORRECT;						\
		first = 0;						\
	}								\
}

static int v_CS __P((SCR *, EXF *, VICMDARG *, u_int));

/*
 * v_iA -- [count]A
 *	Append text to the end of the line.
 */
int
v_iA(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	u_long cnt;
	size_t len;
	u_int flags;
	int first;
	char *p;

	SET_TXT_STD(sp, TXT_APPENDEOL);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (first = 1, lno = vp->m_start.lno,
	    cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		/* Move the cursor to the end of the line + 1. */
		if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0) {
				GETLINE_ERR(sp, lno);
				return (1);
			}
			lno = 1;
			len = 0;
		} else {
			/* Correct logging for implied cursor motion. */
			if (first == 1) {
				sp->cno = len == 0 ? 0 : len - 1;
				LOG_CORRECT;
				first = 0;
			}

			/* Start the change after the line. */
			sp->cno = len;
		}

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, OOBLNO, flags))
			return (1);

		SET_TXT_STD(sp, TXT_APPENDEOL | TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		sp->cno = vp->m_final.cno;
	}
	return (0);
}

/*
 * v_ia -- [count]a
 *	Append text to the cursor position.
 */
int
v_ia(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	u_long cnt;
	u_int flags;
	size_t len;
	char *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (lno = vp->m_start.lno,
	    cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		/*
		 * Move the cursor one column to the right and
		 * repaint the screen.
		 */
		if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0) {
				GETLINE_ERR(sp, lno);
				return (1);
			}
			lno = 1;
			len = 0;
			LF_SET(TXT_APPENDEOL);
		} else if (len) {
			if (len == sp->cno + 1) {
				sp->cno = len;
				LF_SET(TXT_APPENDEOL);
			} else
				++sp->cno;
		} else
			LF_SET(TXT_APPENDEOL);

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, OOBLNO, flags))
			return (1);

		SET_TXT_STD(sp, TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		sp->cno = vp->m_final.cno;
	}
	return (0);
}

/*
 * v_iI -- [count]I
 *	Insert text at the first non-blank character in the line.
 */
int
v_iI(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	u_long cnt;
	size_t len;
	u_int flags;
	int first;
	char *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (first = 1, lno = vp->m_start.lno,
	    cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		/*
		 * Move the cursor to the start of the line and repaint
		 * the screen.
		 */
		if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0) {
				GETLINE_ERR(sp, lno);
				return (1);
			}
			lno = 1;
			len = 0;
		} else {
			sp->cno = 0;
			if (nonblank(sp, ep, lno, &sp->cno))
				return (1);

			/* Correct logging for implied cursor motion. */
			LOG_CORRECT_FIRST;
		}
		if (len == 0)
			LF_SET(TXT_APPENDEOL);

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, OOBLNO, flags))
			return (1);

		SET_TXT_STD(sp, TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		sp->cno = vp->m_final.cno;
	}
	return (0);
}

/*
 * v_ii -- [count]i
 *	Insert text at the cursor position.
 */
int
v_ii(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	u_long cnt;
	size_t len;
	u_int flags;
	char *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (lno = vp->m_start.lno,
	    cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0) {
				GETLINE_ERR(sp, vp->m_start.lno);
				return (1);
			}
			lno = 1;
			len = 0;
		}
		/* If len == sp->cno, it's a replay caused by a count. */
		if (len == 0 || len == sp->cno)
			LF_SET(TXT_APPENDEOL);

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, OOBLNO, flags))
			return (1);

		/*
		 * On replay, if the line isn't empty, advance the insert
		 * by one (make it an append).
		 */
		SET_TXT_STD(sp, TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		if ((sp->cno = vp->m_final.cno) != 0)
			++sp->cno;
	}
	return (0);
}

/*
 * v_iO -- [count]O
 *	Insert text above this line.
 */
int
v_iO(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t ai_line, lno;
	size_t len;
	u_long cnt;
	u_int flags;
	int first;
	char *p;

	SET_TXT_STD(sp, TXT_APPENDEOL);
	if (F_ISSET(vp, VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (first = 1, cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		if (sp->lno == 1) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0)
				goto insert;
			p = NULL;
			len = 0;
			ai_line = OOBLNO;
		} else {
insert:			p = "";
			sp->cno = 0;

			/* Correct logging for implied cursor motion. */
			LOG_CORRECT_FIRST;

			if (file_iline(sp, ep, sp->lno, p, 0))
				return (1);
			if ((p = file_gline(sp, ep, sp->lno, &len)) == NULL) {
				GETLINE_ERR(sp, sp->lno);
				return (1);
			}
			ai_line = sp->lno + 1;
		}

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, ai_line, flags))
			return (1);

		SET_TXT_STD(sp, TXT_APPENDEOL | TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		sp->cno = vp->m_final.cno;
	}
	return (0);
}

/*
 * v_io -- [count]o
 *	Insert text after this line.
 */
int
v_io(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t ai_line, lno;
	size_t len;
	u_long cnt;
	u_int flags;
	int first;
	char *p;

	SET_TXT_STD(sp, TXT_APPENDEOL);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	for (first = 1,
	    cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
		if (sp->lno == 1) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0)
				goto insert;
			p = NULL;
			len = 0;
			ai_line = OOBLNO;
		} else {
insert:			p = "";
			sp->cno = 0;

			/* Correct logging for implied cursor motion. */
			LOG_CORRECT_FIRST;

			len = 0;
			if (file_aline(sp, ep, 1, sp->lno, p, len))
				return (1);
			if ((p = file_gline(sp, ep, ++sp->lno, &len)) == NULL) {
				GETLINE_ERR(sp, sp->lno);
				return (1);
			}
			ai_line = sp->lno - 1;
		}

		if (v_ntext(sp, ep,
		    &sp->tiq, NULL, p, len, &vp->m_final, 0, ai_line, flags))
			return (1);

		SET_TXT_STD(sp, TXT_APPENDEOL | TXT_REPLAY);
		sp->lno = lno = vp->m_final.lno;
		sp->cno = vp->m_final.cno;
	}
	return (0);
}

/*
 * v_Change -- [buffer][count]C
 *	Change line command.
 */
int
v_Change(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	return (v_CS(sp, ep, vp, 0));
}

/*
 * v_Subst -- [buffer][count]S
 *	Line substitute command.
 */
int
v_Subst(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	u_int flags;

	/*
	 * The S command is the same as a 'C' command from the beginning
	 * of the line.  This is hard to do in the parser, so do it here.
	 *
	 * If autoindent is on, the change is from the first *non-blank*
	 * character of the line, not the first character.  And, to make
	 * it just a bit more exciting, the initial space is handled as
	 * auto-indent characters.
	 */
	LF_INIT(0);
	if (O_ISSET(sp, O_AUTOINDENT)) {
		vp->m_start.cno = 0;
		if (nonblank(sp, ep, vp->m_start.lno, &vp->m_start.cno))
			return (1);
		LF_SET(TXT_AICHARS);
	} else
		vp->m_start.cno = 0;
	sp->cno = vp->m_start.cno;
	return (v_CS(sp, ep, vp, flags));
}

/*
 * v_CS --
 *	C and S commands.
 */
static int
v_CS(sp, ep, vp, iflags)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
	u_int iflags;
{
	MARK *tm;
	recno_t lno;
	size_t len;
	char *p;
	u_int flags;

	SET_TXT_STD(sp, iflags);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);

	/*
	 * There are two cases -- if a count is supplied, we do a line
	 * mode change where we delete the lines and then insert text
	 * into a new line.  Otherwise, we replace the current line.
	 */
	vp->m_stop.lno =
	    vp->m_start.lno + (F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0);
	if (vp->m_start.lno != vp->m_stop.lno) {
		/* Make sure that the to line is real. */
		if (file_gline(sp, ep,
		    vp->m_stop.lno, &vp->m_stop.cno) == NULL) {
			v_eof(sp, ep, &vp->m_start);
			return (1);
		}
		if (vp->m_stop.cno != 0)
			--vp->m_stop.cno;

		/* Cut the lines. */
		if (cut(sp, ep,
		    NULL, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
		    &vp->m_start, &vp->m_stop, CUT_LINEMODE))
			return (1);

		/* Insert a line while we still can... */
		if (file_iline(sp, ep, vp->m_start.lno, "", 0))
			return (1);
		++vp->m_start.lno;
		++vp->m_stop.lno;

		/* Delete the lines. */
		if (delete(sp, ep, &vp->m_start, &vp->m_stop, 1))
			return (1);

		/* Get the inserted line. */
		if ((p = file_gline(sp, ep, --vp->m_start.lno, &len)) == NULL) {
			GETLINE_ERR(sp, vp->m_start.lno);
			return (1);
		}
		tm = NULL;
		sp->lno = vp->m_start.lno;
		sp->cno = 0;
		LF_SET(TXT_APPENDEOL);
	} else {
		/* The line may be empty, but that's okay. */
		if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
			if (file_lline(sp, ep, &lno))
				return (1);
			if (lno != 0) {
				GETLINE_ERR(sp, vp->m_start.lno);
				return (1);
			}
			vp->m_stop.cno = len = 0;
			LF_SET(TXT_APPENDEOL);
		} else {
			if (len == 0) {
				vp->m_stop.cno = 0;
				LF_SET(TXT_APPENDEOL);
			} else
				vp->m_stop.cno = len - 1;
			if (cut(sp, ep,
			    NULL, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
			    &vp->m_start, &vp->m_stop, CUT_LINEMODE))
				return (1);
			LF_SET(TXT_EMARK | TXT_OVERWRITE);
		}
		tm = &vp->m_stop;
	}

	/* Correct logging for implied cursor motion. */
	LOG_CORRECT;

	return (v_ntext(sp, ep,
	    &sp->tiq, tm, p, len, &vp->m_final, 0, OOBLNO, flags));
}

/*
 * v_change -- [buffer][count]c[count]motion
 *	Change command.
 */
int
v_change(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	size_t blen, len;
	u_int flags;
	int lmode, rval;
	char *bp, *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);

	/*
	 * Move the cursor to the start of the change.  Note, if autoindent
	 * is turned on, the cc command in line mode changes from the first
	 * *non-blank* character of the line, not the first character.  And,
	 * to make it just a bit more exciting, the initial space is handled
	 * as auto-indent characters.
	 */
	lmode = F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0;
	if (lmode) {
		vp->m_start.cno = 0;
		if (O_ISSET(sp, O_AUTOINDENT)) {
			if (nonblank(sp, ep, vp->m_start.lno, &vp->m_start.cno))
				return (1);
			LF_SET(TXT_AICHARS);
		}
	}
	sp->lno = vp->m_start.lno;
	sp->cno = vp->m_start.cno;

	/* Correct logging for implied cursor motion. */
	LOG_CORRECT;

	/*
	 * If changing within a single line, the line either currently has
	 * text or it doesn't.  If it doesn't, just insert text.  Otherwise,
	 * copy it and overwrite it.
	 */
	if (vp->m_start.lno == vp->m_stop.lno) {
		if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
			if (p == NULL) {
				if (file_lline(sp, ep, &lno))
					return (1);
				if (lno != 0) {
					GETLINE_ERR(sp, vp->m_start.lno);
					return (1);
				}
			}
			vp->m_stop.cno = len = 0;
			LF_SET(TXT_APPENDEOL);
		} else {
			if (cut(sp, ep,
			    NULL, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
			    &vp->m_start, &vp->m_stop, lmode))
				return (1);
			if (len == 0)
				LF_SET(TXT_APPENDEOL);
			LF_SET(TXT_EMARK | TXT_OVERWRITE);
		}
		return (v_ntext(sp, ep, &sp->tiq,
		    &vp->m_stop, p, len, &vp->m_final, 0, OOBLNO, flags));
	}

	/*
	 * It's trickier if changing over multiple lines.  If we're in
	 * line mode we delete all of the lines and insert a replacement
	 * line which the user edits.  If there was leading whitespace
	 * in the first line being changed, we copy it and use it as the
	 * replacement.  If we're not in line mode, we just delete the
	 * text and start inserting.
	 *
	 * Copy the text.
	 */
	if (cut(sp, ep, NULL, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
	    &vp->m_start, &vp->m_stop, lmode))
		return (1);

	/* If replacing entire lines and there's leading text. */
	if (lmode && vp->m_start.cno) {
		/* Get a copy of the first line changed. */
		if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
			GETLINE_ERR(sp, vp->m_start.lno);
			return (1);
		}
		/* Copy the leading text elsewhere. */
		GET_SPACE_RET(sp, bp, blen, vp->m_start.cno);
		memmove(bp, p, vp->m_start.cno);
	} else
		bp = NULL;

	/* Delete the text. */
	if (delete(sp, ep, &vp->m_start, &vp->m_stop, lmode))
		return (1);

	/* If replacing entire lines, insert a replacement line. */
	if (lmode) {
		if (file_iline(sp, ep, vp->m_start.lno, bp, vp->m_start.cno))
			return (1);
		sp->lno = vp->m_start.lno;
		len = sp->cno = vp->m_start.cno;
	}

	/* Get the line we're editing. */
	if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
		if (file_lline(sp, ep, &lno))
			return (1);
		if (lno != 0) {
			GETLINE_ERR(sp, vp->m_start.lno);
			return (1);
		}
		len = 0;
	}

	/* Check to see if we're appending to the line. */
	if (vp->m_start.cno >= len)
		LF_SET(TXT_APPENDEOL);

	rval = v_ntext(sp, ep,
	    &sp->tiq, NULL, p, len, &vp->m_final, 0, OOBLNO, flags);

	if (bp != NULL)
		FREE_SPACE(sp, bp, blen);
	return (rval);
}

/*
 * v_Replace -- [count]R
 *	Overwrite multiple characters.
 */
int
v_Replace(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	u_long cnt;
	size_t len;
	u_int flags;
	char *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);

	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
	if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
		if (file_lline(sp, ep, &lno))
			return (1);
		if (lno != 0) {
			GETLINE_ERR(sp, vp->m_start.lno);
			return (1);
		}
		len = 0;
		LF_SET(TXT_APPENDEOL);
	} else {
		if (len == 0)
			LF_SET(TXT_APPENDEOL);
		LF_SET(TXT_OVERWRITE | TXT_REPLACE);
	}
	vp->m_stop.lno = vp->m_start.lno;
	vp->m_stop.cno = len ? len - 1 : 0;
	if (v_ntext(sp, ep, &sp->tiq,
	    &vp->m_stop, p, len, &vp->m_final, 0, OOBLNO, flags))
		return (1);

	/*
	 * Special case.  The historic vi handled [count]R badly, in that R
	 * would replace some number of characters, and then the count would
	 * append count-1 copies of the replacing chars to the replaced space.
	 * This seems wrong, so this version counts R commands.  There is some
	 * trickiness in moving back to where the user stopped replacing after
	 * each R command.  Basically, if the user ended with a newline, we
	 * want to use vp->m_final.cno (which will be 0).  Otherwise, use the
	 * column after the returned cursor, unless it would be past the end of
	 * the line, in which case we append to the line.
	 */
	while (--cnt) {
		if ((p = file_gline(sp, ep, vp->m_final.lno, &len)) == NULL)
			GETLINE_ERR(sp, vp->m_final.lno);
		SET_TXT_STD(sp, TXT_REPLAY);

		sp->lno = vp->m_final.lno;

		if (len == 0 || vp->m_final.cno == len - 1) {
			sp->cno = len;
			LF_SET(TXT_APPENDEOL);
		} else {
			sp->cno = vp->m_final.cno;
			if (vp->m_final.cno != 0)
				++sp->cno;
			LF_SET(TXT_OVERWRITE | TXT_REPLACE);
		}

		vp->m_stop.lno = sp->lno;
		vp->m_stop.cno = sp->cno;
		if (v_ntext(sp, ep, &sp->tiq,
		    &vp->m_stop, p, len, &vp->m_final, 0, OOBLNO, flags))
			return (1);
	}
	return (0);
}

/*
 * v_subst -- [buffer][count]s
 *	Substitute characters.
 */
int
v_subst(sp, ep, vp)
	SCR *sp;
	EXF *ep;
	VICMDARG *vp;
{
	recno_t lno;
	size_t len;
	u_int flags;
	char *p;

	SET_TXT_STD(sp, 0);
	if (F_ISSET(vp,  VC_ISDOT))
		LF_SET(TXT_REPLAY);
	if ((p = file_gline(sp, ep, vp->m_start.lno, &len)) == NULL) {
		if (file_lline(sp, ep, &lno))
			return (1);
		if (lno != 0) {
			GETLINE_ERR(sp, vp->m_start.lno);
			return (1);
		}
		len = 0;
		LF_SET(TXT_APPENDEOL);
	} else {
		if (len == 0)
			LF_SET(TXT_APPENDEOL);
		LF_SET(TXT_EMARK | TXT_OVERWRITE);
	}

	vp->m_stop.lno = vp->m_start.lno;
	vp->m_stop.cno =
	    vp->m_start.cno + (F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0);
	if (vp->m_stop.cno > len - 1)
		vp->m_stop.cno = len - 1;

	if (p != NULL &&
	    cut(sp, ep, NULL, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
	    &vp->m_start, &vp->m_stop, 0))
		return (1);

	return (v_ntext(sp, ep, &sp->tiq,
	    &vp->m_stop, p, len, &vp->m_final, 0, OOBLNO, flags));
}