1 /* $NetBSD: put.c,v 1.1.1.2 2008/05/18 14:29:50 aymeric Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
32 * Put text buffer contents into the file.
34 * PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int));
37 put(SCR
*sp
, CB
*cbp
, CHAR_T
*namep
, MARK
*cp
, MARK
*rp
, int append
)
42 size_t blen
, clen
, len
;
52 "053|The default buffer is empty");
57 CBNAME(sp
, cbp
, name
);
59 msgq(sp
, M_ERR
, "054|Buffer %s is empty",
65 tp
= cbp
->textq
.cqh_first
;
68 * It's possible to do a put into an empty file, meaning that the cut
69 * buffer simply becomes the file. It's a special case so that we can
70 * ignore it in general.
73 * Historically, pasting into a file with no lines in vi would preserve
74 * the single blank line. This is surely a result of the fact that the
75 * historic vi couldn't deal with a file that had no lines in it. This
76 * implementation treats that as a bug, and does not retain the blank
79 * Historical practice is that the cursor ends at the first character
83 if (db_last(sp
, &lno
))
86 for (; tp
!= (void *)&cbp
->textq
;
87 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
88 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
96 /* If a line mode buffer, append each new line into the file. */
97 if (F_ISSET(cbp
, CB_LMODE
)) {
98 lno
= append
? cp
->lno
: cp
->lno
- 1;
100 for (; tp
!= (void *)&cbp
->textq
;
101 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
102 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
105 (void)nonblank(sp
, rp
->lno
, &rp
->cno
);
110 * If buffer was cut in character mode, replace the current line with
111 * one built from the portion of the first line to the left of the
112 * split plus the first line in the CB. Append each intermediate line
113 * in the CB. Append a line built from the portion of the first line
114 * to the right of the split plus the last line in the CB.
116 * Get the first line.
119 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
122 GET_SPACE_RETW(sp
, bp
, blen
, tp
->len
+ len
+ 1);
125 /* Original line, left of the split. */
126 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
127 MEMCPYW(bp
, p
, clen
);
132 /* First line from the CB. */
134 MEMCPYW(t
, tp
->lb
, tp
->len
);
138 /* Calculate length left in the original line. */
139 clen
= len
== 0 ? 0 : len
- (cp
->cno
+ (append
? 1 : 0));
143 * In the historical 4BSD version of vi, character mode puts within
144 * a single line have two cursor behaviors: if the put is from the
145 * unnamed buffer, the cursor moves to the character inserted which
146 * appears last in the file. If the put is from a named buffer,
147 * the cursor moves to the character inserted which appears first
148 * in the file. In System III/V, it was changed at some point and
149 * the cursor always moves to the first character. In both versions
150 * of vi, character mode puts that cross line boundaries leave the
151 * cursor on the first character. Nvi implements the System III/V
152 * behavior, and expect POSIX.2 to do so as well.
155 rp
->cno
= len
== 0 ? 0 : sp
->cno
+ (append
&& tp
->len
? 1 : 0);
158 * If no more lines in the CB, append the rest of the original
159 * line and quit. Otherwise, build the last line before doing
160 * the intermediate lines, because the line changes will lose
163 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
168 if (db_set(sp
, lno
, bp
, t
- bp
))
170 if (sp
->rptlchange
!= lno
) {
171 sp
->rptlchange
= lno
;
172 ++sp
->rptlines
[L_CHANGED
];
176 * Have to build both the first and last lines of the
177 * put before doing any sets or we'll lose the cached
178 * line. Build both the first and last lines in the
179 * same buffer, so we don't have to have another buffer
182 * Last part of original line; check for space, reset
183 * the pointer into the buffer.
185 ltp
= cbp
->textq
.cqh_last
;
187 ADD_SPACE_RETW(sp
, bp
, blen
, ltp
->len
+ clen
);
190 /* Add in last part of the CB. */
191 MEMCPYW(t
, ltp
->lb
, ltp
->len
);
193 MEMCPYW(t
+ ltp
->len
, p
, clen
);
197 * Now: bp points to the first character of the first
198 * line, t points to the last character of the last
199 * line, t - bp is the length of the first line, and
200 * clen is the length of the last. Just figured you'd
203 * Output the line replacing the original line.
205 if (db_set(sp
, lno
, bp
, t
- bp
))
207 if (sp
->rptlchange
!= lno
) {
208 sp
->rptlchange
= lno
;
209 ++sp
->rptlines
[L_CHANGED
];
212 /* Output any intermediate lines in the CB. */
213 for (tp
= tp
->q
.cqe_next
;
214 tp
->q
.cqe_next
!= (void *)&cbp
->textq
;
215 ++lno
, ++sp
->rptlines
[L_ADDED
], tp
= tp
->q
.cqe_next
)
216 if (db_append(sp
, 1, lno
, tp
->lb
, tp
->len
))
219 if (db_append(sp
, 1, lno
, t
, clen
))
221 ++sp
->rptlines
[L_ADDED
];
228 FREE_SPACEW(sp
, bp
, blen
);