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