remove generated compile, depcomp, missing and mkinstalldirs
[nvi.git] / common / put.c
blob706a2480d6ed29750c5b12aa94498d267f8437ef
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.18 2001/06/25 15:19:11 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:11 $";
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(SCR *sp, CB *cbp, CHAR_T *namep, MARK *cp, MARK *rp, int append)
37 CHAR_T name;
38 TEXT *ltp, *tp;
39 db_recno_t lno;
40 size_t blen, clen, len;
41 int rval;
42 CHAR_T *bp, *t;
43 CHAR_T *p;
45 if (cbp == NULL)
46 if (namep == NULL) {
47 cbp = sp->wp->dcbp;
48 if (cbp == NULL) {
49 msgq(sp, M_ERR,
50 "053|The default buffer is empty");
51 return (1);
53 } else {
54 name = *namep;
55 CBNAME(sp, cbp, name);
56 if (cbp == NULL) {
57 msgq(sp, M_ERR, "054|Buffer %s is empty",
58 KEY_NAME(sp, name));
59 return (1);
62 tp = cbp->textq.cqh_first;
65 * It's possible to do a put into an empty file, meaning that the cut
66 * buffer simply becomes the file. It's a special case so that we can
67 * ignore it in general.
69 * !!!
70 * Historically, pasting into a file with no lines in vi would preserve
71 * the single blank line. This is surely a result of the fact that the
72 * historic vi couldn't deal with a file that had no lines in it. This
73 * implementation treats that as a bug, and does not retain the blank
74 * line.
76 * Historical practice is that the cursor ends at the first character
77 * in the file.
79 if (cp->lno == 1) {
80 if (db_last(sp, &lno))
81 return (1);
82 if (lno == 0) {
83 for (; tp != (void *)&cbp->textq;
84 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
85 if (db_append(sp, 1, lno, tp->lb, tp->len))
86 return (1);
87 rp->lno = 1;
88 rp->cno = 0;
89 return (0);
93 /* If a line mode buffer, append each new line into the file. */
94 if (F_ISSET(cbp, CB_LMODE)) {
95 lno = append ? cp->lno : cp->lno - 1;
96 rp->lno = lno + 1;
97 for (; tp != (void *)&cbp->textq;
98 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
99 if (db_append(sp, 1, lno, tp->lb, tp->len))
100 return (1);
101 rp->cno = 0;
102 (void)nonblank(sp, rp->lno, &rp->cno);
103 return (0);
107 * If buffer was cut in character mode, replace the current line with
108 * one built from the portion of the first line to the left of the
109 * split plus the first line in the CB. Append each intermediate line
110 * in the CB. Append a line built from the portion of the first line
111 * to the right of the split plus the last line in the CB.
113 * Get the first line.
115 lno = cp->lno;
116 if (db_get(sp, lno, DBG_FATAL, &p, &len))
117 return (1);
119 GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1);
120 t = bp;
122 /* Original line, left of the split. */
123 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
124 MEMCPYW(bp, p, clen);
125 p += clen;
126 t += clen;
129 /* First line from the CB. */
130 if (tp->len != 0) {
131 MEMCPYW(t, tp->lb, tp->len);
132 t += tp->len;
135 /* Calculate length left in the original line. */
136 clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
139 * !!!
140 * In the historical 4BSD version of vi, character mode puts within
141 * a single line have two cursor behaviors: if the put is from the
142 * unnamed buffer, the cursor moves to the character inserted which
143 * appears last in the file. If the put is from a named buffer,
144 * the cursor moves to the character inserted which appears first
145 * in the file. In System III/V, it was changed at some point and
146 * the cursor always moves to the first character. In both versions
147 * of vi, character mode puts that cross line boundaries leave the
148 * cursor on the first character. Nvi implements the System III/V
149 * behavior, and expect POSIX.2 to do so as well.
151 rp->lno = lno;
152 rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
155 * If no more lines in the CB, append the rest of the original
156 * line and quit. Otherwise, build the last line before doing
157 * the intermediate lines, because the line changes will lose
158 * the cached line.
160 if (tp->q.cqe_next == (void *)&cbp->textq) {
161 if (clen > 0) {
162 MEMCPYW(t, p, clen);
163 t += clen;
165 if (db_set(sp, lno, bp, t - bp))
166 goto err;
167 if (sp->rptlchange != lno) {
168 sp->rptlchange = lno;
169 ++sp->rptlines[L_CHANGED];
171 } else {
173 * Have to build both the first and last lines of the
174 * put before doing any sets or we'll lose the cached
175 * line. Build both the first and last lines in the
176 * same buffer, so we don't have to have another buffer
177 * floating around.
179 * Last part of original line; check for space, reset
180 * the pointer into the buffer.
182 ltp = cbp->textq.cqh_last;
183 len = t - bp;
184 ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen);
185 t = bp + len;
187 /* Add in last part of the CB. */
188 MEMCPYW(t, ltp->lb, ltp->len);
189 if (clen)
190 MEMCPYW(t + ltp->len, p, clen);
191 clen += ltp->len;
194 * Now: bp points to the first character of the first
195 * line, t points to the last character of the last
196 * line, t - bp is the length of the first line, and
197 * clen is the length of the last. Just figured you'd
198 * want to know.
200 * Output the line replacing the original line.
202 if (db_set(sp, lno, bp, t - bp))
203 goto err;
204 if (sp->rptlchange != lno) {
205 sp->rptlchange = lno;
206 ++sp->rptlines[L_CHANGED];
209 /* Output any intermediate lines in the CB. */
210 for (tp = tp->q.cqe_next;
211 tp->q.cqe_next != (void *)&cbp->textq;
212 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
213 if (db_append(sp, 1, lno, tp->lb, tp->len))
214 goto err;
216 if (db_append(sp, 1, lno, t, clen))
217 goto err;
218 ++sp->rptlines[L_ADDED];
220 rval = 0;
222 if (0)
223 err: rval = 1;
225 FREE_SPACEW(sp, bp, blen);
226 return (rval);