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.
13 static const char sccsid
[] = "@(#)put.c 10.11 (Berkeley) 9/23/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
30 * Put text buffer contents into the file.
32 * PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int));
35 put(sp
, cbp
, namep
, cp
, rp
, append
)
45 size_t blen
, clen
, len
;
54 "053|The default buffer is empty");
59 CBNAME(sp
, cbp
, name
);
61 msgq(sp
, M_ERR
, "054|Buffer %s is empty",
66 tp
= cbp
->textq
.cqh_first
;
69 * It's possible to do a put into an empty file, meaning that the cut
70 * buffer simply becomes the file. It's a special case so that we can
71 * ignore it in general.
74 * Historically, pasting into a file with no lines in vi would preserve
75 * the single blank line. This is surely a result of the fact that the
76 * historic vi couldn't deal with a file that had no lines in it. This
77 * implementation treats that as a bug, and does not retain the blank
80 * Historical practice is that the cursor ends at the first character
84 if (db_last(sp
, &lno
))
87 for (; tp
!= (void *)&cbp
->textq
;
88 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
89 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
97 /* If a line mode buffer, append each new line into the file. */
98 if (F_ISSET(cbp
, CB_LMODE
)) {
99 lno
= append
? cp
->lno
: cp
->lno
- 1;
101 for (; tp
!= (void *)&cbp
->textq
;
102 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
103 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
106 (void)nonblank(sp
, rp
->lno
, &rp
->cno
);
111 * If buffer was cut in character mode, replace the current line with
112 * one built from the portion of the first line to the left of the
113 * split plus the first line in the CB. Append each intermediate line
114 * in the CB. Append a line built from the portion of the first line
115 * to the right of the split plus the last line in the CB.
117 * Get the first line.
120 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
123 GET_SPACE_RET(sp
, bp
, blen
, tp
->len
+ len
+ 1);
126 /* Original line, left of the split. */
127 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
133 /* First line from the CB. */
135 memcpy(t
, tp
->lb
, tp
->len
);
139 /* Calculate length left in the original line. */
140 clen
= len
== 0 ? 0 : len
- (cp
->cno
+ (append
? 1 : 0));
144 * In the historical 4BSD version of vi, character mode puts within
145 * a single line have two cursor behaviors: if the put is from the
146 * unnamed buffer, the cursor moves to the character inserted which
147 * appears last in the file. If the put is from a named buffer,
148 * the cursor moves to the character inserted which appears first
149 * in the file. In System III/V, it was changed at some point and
150 * the cursor always moves to the first character. In both versions
151 * of vi, character mode puts that cross line boundaries leave the
152 * cursor on the first character. Nvi implements the System III/V
153 * behavior, and expect POSIX.2 to do so as well.
156 rp
->cno
= len
== 0 ? 0 : sp
->cno
+ (append
&& tp
->len
? 1 : 0);
159 * If no more lines in the CB, append the rest of the original
160 * line and quit. Otherwise, build the last line before doing
161 * the intermediate lines, because the line changes will lose
164 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
169 if (db_set(sp
, lno
, bp
, t
- bp
))
171 if (sp
->rptlchange
!= lno
) {
172 sp
->rptlchange
= lno
;
173 ++sp
->rptlines
[L_CHANGED
];
177 * Have to build both the first and last lines of the
178 * put before doing any sets or we'll lose the cached
179 * line. Build both the first and last lines in the
180 * same buffer, so we don't have to have another buffer
183 * Last part of original line; check for space, reset
184 * the pointer into the buffer.
186 ltp
= cbp
->textq
.cqh_last
;
188 ADD_SPACE_RET(sp
, bp
, blen
, ltp
->len
+ clen
);
191 /* Add in last part of the CB. */
192 memcpy(t
, ltp
->lb
, ltp
->len
);
194 memcpy(t
+ ltp
->len
, p
, clen
);
198 * Now: bp points to the first character of the first
199 * line, t points to the last character of the last
200 * line, t - bp is the length of the first line, and
201 * clen is the length of the last. Just figured you'd
204 * Output the line replacing the original line.
206 if (db_set(sp
, lno
, bp
, t
- bp
))
208 if (sp
->rptlchange
!= lno
) {
209 sp
->rptlchange
= lno
;
210 ++sp
->rptlines
[L_CHANGED
];
213 /* Output any intermediate lines in the CB. */
214 for (tp
= tp
->q
.cqe_next
;
215 tp
->q
.cqe_next
!= (void *)&cbp
->textq
;
216 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
217 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
220 if (db_append(sp
, 1, lno
, t
, clen
))
222 ++sp
->rptlines
[L_ADDED
];
229 FREE_SPACE(sp
, bp
, blen
);