First test with an external encoding.
[nvi.git] / common / put.c
blob60dcb5b2160d66764d1386c48c99f26ef8d7add4
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: put.c,v 10.16 2000/07/16 20:49:29 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:29 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "common.h"
29 * put --
30 * Put text buffer contents into the file.
32 * PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int));
34 int
35 put(sp, cbp, namep, cp, rp, append)
36 SCR *sp;
37 CB *cbp;
38 CHAR_T *namep;
39 MARK *cp, *rp;
40 int append;
42 CHAR_T name;
43 TEXT *ltp, *tp;
44 db_recno_t lno;
45 size_t blen, clen, len;
46 int rval;
47 CHAR_T *bp, *t;
48 CHAR_T *p;
50 if (cbp == NULL)
51 if (namep == NULL) {
52 cbp = sp->gp->dcbp;
53 if (cbp == NULL) {
54 msgq(sp, M_ERR,
55 "053|The default buffer is empty");
56 return (1);
58 } else {
59 name = *namep;
60 CBNAME(sp, cbp, name);
61 if (cbp == NULL) {
62 msgq(sp, M_ERR, "054|Buffer %s is empty",
63 KEY_NAME(sp, name));
64 return (1);
67 tp = cbp->textq.cqh_first;
70 * It's possible to do a put into an empty file, meaning that the cut
71 * buffer simply becomes the file. It's a special case so that we can
72 * ignore it in general.
74 * !!!
75 * Historically, pasting into a file with no lines in vi would preserve
76 * the single blank line. This is surely a result of the fact that the
77 * historic vi couldn't deal with a file that had no lines in it. This
78 * implementation treats that as a bug, and does not retain the blank
79 * line.
81 * Historical practice is that the cursor ends at the first character
82 * in the file.
84 if (cp->lno == 1) {
85 if (db_last(sp, &lno))
86 return (1);
87 if (lno == 0) {
88 for (; tp != (void *)&cbp->textq;
89 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
90 if (db_append(sp, 1, lno, tp->lb, tp->len))
91 return (1);
92 rp->lno = 1;
93 rp->cno = 0;
94 return (0);
98 /* If a line mode buffer, append each new line into the file. */
99 if (F_ISSET(cbp, CB_LMODE)) {
100 lno = append ? cp->lno : cp->lno - 1;
101 rp->lno = lno + 1;
102 for (; tp != (void *)&cbp->textq;
103 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
104 if (db_append(sp, 1, lno, tp->lb, tp->len))
105 return (1);
106 rp->cno = 0;
107 (void)nonblank(sp, rp->lno, &rp->cno);
108 return (0);
112 * If buffer was cut in character mode, replace the current line with
113 * one built from the portion of the first line to the left of the
114 * split plus the first line in the CB. Append each intermediate line
115 * in the CB. Append a line built from the portion of the first line
116 * to the right of the split plus the last line in the CB.
118 * Get the first line.
120 lno = cp->lno;
121 if (db_get(sp, lno, DBG_FATAL, &p, &len))
122 return (1);
124 GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1);
125 t = bp;
127 /* Original line, left of the split. */
128 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
129 MEMCPYW(bp, p, clen);
130 p += clen;
131 t += clen;
134 /* First line from the CB. */
135 if (tp->len != 0) {
136 MEMCPYW(t, tp->lb, tp->len);
137 t += tp->len;
140 /* Calculate length left in the original line. */
141 clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
144 * !!!
145 * In the historical 4BSD version of vi, character mode puts within
146 * a single line have two cursor behaviors: if the put is from the
147 * unnamed buffer, the cursor moves to the character inserted which
148 * appears last in the file. If the put is from a named buffer,
149 * the cursor moves to the character inserted which appears first
150 * in the file. In System III/V, it was changed at some point and
151 * the cursor always moves to the first character. In both versions
152 * of vi, character mode puts that cross line boundaries leave the
153 * cursor on the first character. Nvi implements the System III/V
154 * behavior, and expect POSIX.2 to do so as well.
156 rp->lno = lno;
157 rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
160 * If no more lines in the CB, append the rest of the original
161 * line and quit. Otherwise, build the last line before doing
162 * the intermediate lines, because the line changes will lose
163 * the cached line.
165 if (tp->q.cqe_next == (void *)&cbp->textq) {
166 if (clen > 0) {
167 MEMCPYW(t, p, clen);
168 t += clen;
170 if (db_set(sp, lno, bp, t - bp))
171 goto err;
172 if (sp->rptlchange != lno) {
173 sp->rptlchange = lno;
174 ++sp->rptlines[L_CHANGED];
176 } else {
178 * Have to build both the first and last lines of the
179 * put before doing any sets or we'll lose the cached
180 * line. Build both the first and last lines in the
181 * same buffer, so we don't have to have another buffer
182 * floating around.
184 * Last part of original line; check for space, reset
185 * the pointer into the buffer.
187 ltp = cbp->textq.cqh_last;
188 len = t - bp;
189 ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen);
190 t = bp + len;
192 /* Add in last part of the CB. */
193 MEMCPYW(t, ltp->lb, ltp->len);
194 if (clen)
195 MEMCPYW(t + ltp->len, p, clen);
196 clen += ltp->len;
199 * Now: bp points to the first character of the first
200 * line, t points to the last character of the last
201 * line, t - bp is the length of the first line, and
202 * clen is the length of the last. Just figured you'd
203 * want to know.
205 * Output the line replacing the original line.
207 if (db_set(sp, lno, bp, t - bp))
208 goto err;
209 if (sp->rptlchange != lno) {
210 sp->rptlchange = lno;
211 ++sp->rptlines[L_CHANGED];
214 /* Output any intermediate lines in the CB. */
215 for (tp = tp->q.cqe_next;
216 tp->q.cqe_next != (void *)&cbp->textq;
217 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
218 if (db_append(sp, 1, lno, tp->lb, tp->len))
219 goto err;
221 if (db_append(sp, 1, lno, t, clen))
222 goto err;
223 ++sp->rptlines[L_ADDED];
225 rval = 0;
227 if (0)
228 err: rval = 1;
230 FREE_SPACEW(sp, bp, blen);
231 return (rval);