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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
27 * Put text buffer contents into the file.
29 * PUBLIC: int put(SCR *, CB *, CHAR_T *, MARK *, MARK *, int);
32 put(SCR *sp, CB *cbp, CHAR_T *namep, MARK *cp, MARK *rp, int append)
37 size_t blen, clen, len;
47 "053|The default buffer is empty");
52 CBNAME(sp, cbp, name);
54 msgq(sp, M_ERR, "054|Buffer %s is empty",
60 tp = TAILQ_FIRST(cbp->textq);
63 * It's possible to do a put into an empty file, meaning that the cut
64 * buffer simply becomes the file. It's a special case so that we can
65 * ignore it in general.
68 * Historically, pasting into a file with no lines in vi would preserve
69 * the single blank line. This is surely a result of the fact that the
70 * historic vi couldn't deal with a file that had no lines in it. This
71 * implementation treats that as a bug, and does not retain the blank
74 * Historical practice is that the cursor ends at the first character
78 if (db_last(sp, &lno))
82 ++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
83 if (db_append(sp, 1, lno, tp->lb, tp->len))
91 /* If a line mode buffer, append each new line into the file. */
92 if (F_ISSET(cbp, CB_LMODE)) {
93 lno = append ? cp->lno : cp->lno - 1;
96 ++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
97 if (db_append(sp, 1, lno, tp->lb, tp->len))
100 (void)nonblank(sp, rp->lno, &rp->cno);
105 * If buffer was cut in character mode, replace the current line with
106 * one built from the portion of the first line to the left of the
107 * split plus the first line in the CB. Append each intermediate line
108 * in the CB. Append a line built from the portion of the first line
109 * to the right of the split plus the last line in the CB.
111 * Get the first line.
114 if (db_get(sp, lno, DBG_FATAL, &p, &len))
117 GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1);
120 /* Original line, left of the split. */
121 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
127 /* First line from the CB. */
129 MEMCPY(t, tp->lb, tp->len);
133 /* Calculate length left in the original line. */
134 clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
138 * In the historical 4BSD version of vi, character mode puts within
139 * a single line have two cursor behaviors: if the put is from the
140 * unnamed buffer, the cursor moves to the character inserted which
141 * appears last in the file. If the put is from a named buffer,
142 * the cursor moves to the character inserted which appears first
143 * in the file. In System III/V, it was changed at some point and
144 * the cursor always moves to the first character. In both versions
145 * of vi, character mode puts that cross line boundaries leave the
146 * cursor on the first character. Nvi implements the System III/V
147 * behavior, and expect POSIX.2 to do so as well.
150 rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
153 * If no more lines in the CB, append the rest of the original
154 * line and quit. Otherwise, build the last line before doing
155 * the intermediate lines, because the line changes will lose
158 if (TAILQ_NEXT(tp, q) == NULL) {
163 if (db_set(sp, lno, bp, t - bp))
165 if (sp->rptlchange != lno) {
166 sp->rptlchange = lno;
167 ++sp->rptlines[L_CHANGED];
171 * Have to build both the first and last lines of the
172 * put before doing any sets or we'll lose the cached
173 * line. Build both the first and last lines in the
174 * same buffer, so we don't have to have another buffer
177 * Last part of original line; check for space, reset
178 * the pointer into the buffer.
180 ltp = TAILQ_LAST(cbp->textq, _texth);
182 ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen);
185 /* Add in last part of the CB. */
186 MEMCPY(t, ltp->lb, ltp->len);
188 MEMCPY(t + ltp->len, p, clen);
192 * Now: bp points to the first character of the first
193 * line, t points to the last character of the last
194 * line, t - bp is the length of the first line, and
195 * clen is the length of the last. Just figured you'd
198 * Output the line replacing the original line.
200 if (db_set(sp, lno, bp, t - bp))
202 if (sp->rptlchange != lno) {
203 sp->rptlchange = lno;
204 ++sp->rptlines[L_CHANGED];
207 /* Output any intermediate lines in the CB. */
208 for (tp = TAILQ_NEXT(tp, q); TAILQ_NEXT(tp, q) != NULL;
209 ++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
210 if (db_append(sp, 1, lno, tp->lb, tp->len))
213 if (db_append(sp, 1, lno, t, clen))
215 ++sp->rptlines[L_ADDED];
222 FREE_SPACEW(sp, bp, blen);