xref: /freebsd/contrib/nvi/vi/v_put.c (revision c271fa9295c13b3cc926562c9b204fa597dba7e6)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #ifndef lint
13f0957ccaSPeter Wemm static const char sccsid[] = "$Id: v_put.c,v 10.6 2001/06/25 15:19:34 skimo Exp $";
14b8ba871bSPeter Wemm #endif /* not lint */
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <sys/types.h>
17b8ba871bSPeter Wemm #include <sys/queue.h>
18b8ba871bSPeter Wemm #include <sys/time.h>
19b8ba871bSPeter Wemm 
20b8ba871bSPeter Wemm #include <bitstring.h>
21b8ba871bSPeter Wemm #include <limits.h>
22b8ba871bSPeter Wemm #include <stdio.h>
23b8ba871bSPeter Wemm 
24b8ba871bSPeter Wemm #include "../common/common.h"
25b8ba871bSPeter Wemm #include "vi.h"
26b8ba871bSPeter Wemm 
27*c271fa92SBaptiste Daroussin static void	inc_buf(SCR *, VICMD *);
28b8ba871bSPeter Wemm 
29b8ba871bSPeter Wemm /*
30b8ba871bSPeter Wemm  * v_Put -- [buffer]P
31b8ba871bSPeter Wemm  *	Insert the contents of the buffer before the cursor.
32b8ba871bSPeter Wemm  *
33*c271fa92SBaptiste Daroussin  * PUBLIC: int v_Put(SCR *, VICMD *);
34b8ba871bSPeter Wemm  */
35b8ba871bSPeter Wemm int
36f0957ccaSPeter Wemm v_Put(SCR *sp, VICMD *vp)
37b8ba871bSPeter Wemm {
38b8ba871bSPeter Wemm 	u_long cnt;
39b8ba871bSPeter Wemm 
40b8ba871bSPeter Wemm 	if (F_ISSET(vp, VC_ISDOT))
41b8ba871bSPeter Wemm 		inc_buf(sp, vp);
42b8ba871bSPeter Wemm 
43b8ba871bSPeter Wemm 	/*
44b8ba871bSPeter Wemm 	 * !!!
45b8ba871bSPeter Wemm 	 * Historic vi did not support a count with the 'p' and 'P'
46b8ba871bSPeter Wemm 	 * commands.  It's useful, so we do.
47b8ba871bSPeter Wemm 	 */
48b8ba871bSPeter Wemm 	for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
49b8ba871bSPeter Wemm 		if (put(sp, NULL,
50b8ba871bSPeter Wemm 		    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
51b8ba871bSPeter Wemm 		    &vp->m_start, &vp->m_final, 0))
52b8ba871bSPeter Wemm 			return (1);
53b8ba871bSPeter Wemm 		vp->m_start = vp->m_final;
54b8ba871bSPeter Wemm 		if (INTERRUPTED(sp))
55b8ba871bSPeter Wemm 			return (1);
56b8ba871bSPeter Wemm 	}
57b8ba871bSPeter Wemm 	return (0);
58b8ba871bSPeter Wemm }
59b8ba871bSPeter Wemm 
60b8ba871bSPeter Wemm /*
61b8ba871bSPeter Wemm  * v_put -- [buffer]p
62b8ba871bSPeter Wemm  *	Insert the contents of the buffer after the cursor.
63b8ba871bSPeter Wemm  *
64*c271fa92SBaptiste Daroussin  * PUBLIC: int v_put(SCR *, VICMD *);
65b8ba871bSPeter Wemm  */
66b8ba871bSPeter Wemm int
67f0957ccaSPeter Wemm v_put(SCR *sp, VICMD *vp)
68b8ba871bSPeter Wemm {
69b8ba871bSPeter Wemm 	u_long cnt;
70b8ba871bSPeter Wemm 
71b8ba871bSPeter Wemm 	if (F_ISSET(vp, VC_ISDOT))
72b8ba871bSPeter Wemm 		inc_buf(sp, vp);
73b8ba871bSPeter Wemm 
74b8ba871bSPeter Wemm 	/*
75b8ba871bSPeter Wemm 	 * !!!
76b8ba871bSPeter Wemm 	 * Historic vi did not support a count with the 'p' and 'P'
77b8ba871bSPeter Wemm 	 * commands.  It's useful, so we do.
78b8ba871bSPeter Wemm 	 */
79b8ba871bSPeter Wemm 	for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
80b8ba871bSPeter Wemm 		if (put(sp, NULL,
81b8ba871bSPeter Wemm 		    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
82b8ba871bSPeter Wemm 		    &vp->m_start, &vp->m_final, 1))
83b8ba871bSPeter Wemm 			return (1);
84b8ba871bSPeter Wemm 		vp->m_start = vp->m_final;
85b8ba871bSPeter Wemm 		if (INTERRUPTED(sp))
86b8ba871bSPeter Wemm 			return (1);
87b8ba871bSPeter Wemm 	}
88b8ba871bSPeter Wemm 	return (0);
89b8ba871bSPeter Wemm }
90b8ba871bSPeter Wemm 
91b8ba871bSPeter Wemm /*
92b8ba871bSPeter Wemm  * !!!
93b8ba871bSPeter Wemm  * Historical whackadoo.  The dot command `puts' the numbered buffer
94b8ba871bSPeter Wemm  * after the last one put.  For example, `"4p.' would put buffer #4
95b8ba871bSPeter Wemm  * and buffer #5.  If the user continued to enter '.', the #9 buffer
96b8ba871bSPeter Wemm  * would be repeatedly output.  This was not documented, and is a bit
97b8ba871bSPeter Wemm  * tricky to reconstruct.  Historical versions of vi also dropped the
98b8ba871bSPeter Wemm  * contents of the default buffer after each put, so after `"4p' the
99b8ba871bSPeter Wemm  * default buffer would be empty.  This makes no sense to me, so we
100b8ba871bSPeter Wemm  * don't bother.  Don't assume sequential order of numeric characters.
101b8ba871bSPeter Wemm  *
102b8ba871bSPeter Wemm  * And, if that weren't exciting enough, failed commands don't normally
103b8ba871bSPeter Wemm  * set the dot command.  Well, boys and girls, an exception is that
104b8ba871bSPeter Wemm  * the buffer increment gets done regardless of the success of the put.
105b8ba871bSPeter Wemm  */
106b8ba871bSPeter Wemm static void
107f0957ccaSPeter Wemm inc_buf(SCR *sp, VICMD *vp)
108b8ba871bSPeter Wemm {
109b8ba871bSPeter Wemm 	CHAR_T v;
110b8ba871bSPeter Wemm 
111b8ba871bSPeter Wemm 	switch (vp->buffer) {
112b8ba871bSPeter Wemm 	case '1':
113b8ba871bSPeter Wemm 		v = '2';
114b8ba871bSPeter Wemm 		break;
115b8ba871bSPeter Wemm 	case '2':
116b8ba871bSPeter Wemm 		v = '3';
117b8ba871bSPeter Wemm 		break;
118b8ba871bSPeter Wemm 	case '3':
119b8ba871bSPeter Wemm 		v = '4';
120b8ba871bSPeter Wemm 		break;
121b8ba871bSPeter Wemm 	case '4':
122b8ba871bSPeter Wemm 		v = '5';
123b8ba871bSPeter Wemm 		break;
124b8ba871bSPeter Wemm 	case '5':
125b8ba871bSPeter Wemm 		v = '6';
126b8ba871bSPeter Wemm 		break;
127b8ba871bSPeter Wemm 	case '6':
128b8ba871bSPeter Wemm 		v = '7';
129b8ba871bSPeter Wemm 		break;
130b8ba871bSPeter Wemm 	case '7':
131b8ba871bSPeter Wemm 		v = '8';
132b8ba871bSPeter Wemm 		break;
133b8ba871bSPeter Wemm 	case '8':
134b8ba871bSPeter Wemm 		v = '9';
135b8ba871bSPeter Wemm 		break;
136b8ba871bSPeter Wemm 	default:
137b8ba871bSPeter Wemm 		return;
138b8ba871bSPeter Wemm 	}
139b8ba871bSPeter Wemm 	VIP(sp)->sdot.buffer = vp->buffer = v;
140b8ba871bSPeter Wemm }
141