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.17 2000/07/22 17:31:19 skimo Exp $ (Berkeley) $Date: 2000/07/22 17:31:19 $";
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
;
55 "053|The default buffer is empty");
60 CBNAME(sp
, cbp
, name
);
62 msgq(sp
, M_ERR
, "054|Buffer %s is empty",
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.
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
81 * Historical practice is that the cursor ends at the first character
85 if (db_last(sp
, &lno
))
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
))
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;
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
))
107 (void)nonblank(sp
, rp
->lno
, &rp
->cno
);
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.
121 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
124 GET_SPACE_RETW(sp
, bp
, blen
, tp
->len
+ len
+ 1);
127 /* Original line, left of the split. */
128 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
129 MEMCPYW(bp
, p
, clen
);
134 /* First line from the CB. */
136 MEMCPYW(t
, tp
->lb
, tp
->len
);
140 /* Calculate length left in the original line. */
141 clen
= len
== 0 ? 0 : len
- (cp
->cno
+ (append
? 1 : 0));
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.
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
165 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
170 if (db_set(sp
, lno
, bp
, t
- bp
))
172 if (sp
->rptlchange
!= lno
) {
173 sp
->rptlchange
= lno
;
174 ++sp
->rptlines
[L_CHANGED
];
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
184 * Last part of original line; check for space, reset
185 * the pointer into the buffer.
187 ltp
= cbp
->textq
.cqh_last
;
189 ADD_SPACE_RETW(sp
, bp
, blen
, ltp
->len
+ clen
);
192 /* Add in last part of the CB. */
193 MEMCPYW(t
, ltp
->lb
, ltp
->len
);
195 MEMCPYW(t
+ ltp
->len
, p
, clen
);
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
205 * Output the line replacing the original line.
207 if (db_set(sp
, lno
, bp
, t
- bp
))
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
))
221 if (db_append(sp
, 1, lno
, t
, clen
))
223 ++sp
->rptlines
[L_ADDED
];
230 FREE_SPACEW(sp
, bp
, blen
);