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
[] = "$Id: put.c,v 10.18 2001/06/25 15:19:11 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:11 $";
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(SCR
*sp
, CB
*cbp
, CHAR_T
*namep
, MARK
*cp
, MARK
*rp
, int append
)
40 size_t blen
, clen
, len
;
50 "053|The default buffer is empty");
55 CBNAME(sp
, cbp
, name
);
57 msgq(sp
, M_ERR
, "054|Buffer %s is empty",
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.
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
76 * Historical practice is that the cursor ends at the first character
80 if (db_last(sp
, &lno
))
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
))
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;
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
))
102 (void)nonblank(sp
, rp
->lno
, &rp
->cno
);
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.
116 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
119 GET_SPACE_RETW(sp
, bp
, blen
, tp
->len
+ len
+ 1);
122 /* Original line, left of the split. */
123 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
124 MEMCPYW(bp
, p
, clen
);
129 /* First line from the CB. */
131 MEMCPYW(t
, tp
->lb
, tp
->len
);
135 /* Calculate length left in the original line. */
136 clen
= len
== 0 ? 0 : len
- (cp
->cno
+ (append
? 1 : 0));
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.
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
160 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
165 if (db_set(sp
, lno
, bp
, t
- bp
))
167 if (sp
->rptlchange
!= lno
) {
168 sp
->rptlchange
= lno
;
169 ++sp
->rptlines
[L_CHANGED
];
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
179 * Last part of original line; check for space, reset
180 * the pointer into the buffer.
182 ltp
= cbp
->textq
.cqh_last
;
184 ADD_SPACE_RETW(sp
, bp
, blen
, ltp
->len
+ clen
);
187 /* Add in last part of the CB. */
188 MEMCPYW(t
, ltp
->lb
, ltp
->len
);
190 MEMCPYW(t
+ ltp
->len
, p
, clen
);
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
200 * Output the line replacing the original line.
202 if (db_set(sp
, lno
, bp
, t
- bp
))
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
))
216 if (db_append(sp
, 1, lno
, t
, clen
))
218 ++sp
->rptlines
[L_ADDED
];
225 FREE_SPACEW(sp
, bp
, blen
);