2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: cut.c,v 8.16 1993/12/29 10:48:43 bostic Exp $ (Berkeley) $Date: 1993/12/29 10:48:43 $";
12 #include <sys/types.h>
22 static int cb_line
__P((SCR
*, EXF
*, recno_t
, size_t, size_t, TEXT
**));
23 static int cb_rotate
__P((SCR
*));
27 * Put a range of lines/columns into a buffer.
30 cut(sp
, ep
, name
, fm
, tm
, lmode
)
43 #if defined(DEBUG) && 0
44 TRACE(sp
, "cut: from {%lu, %d}, to {%lu, %d}%s\n",
45 fm
->lno
, fm
->cno
, tm
->lno
, tm
->cno
, lmode
? " LINE MODE" : "");
49 * The numeric buffers in historic vi offer us yet another opportunity
50 * for contemplation. First, the default buffer was the same as buffer
51 * '1'. Second, cutting into any numeric buffer caused buffers '1' to
52 * '8' to be rotated up one, and '9' to drop off the end. Finally,
53 * text cut into a numeric buffer other than '1' was always appended
54 * to the buffer (after the rotation), it was not a replacement.
64 * Upper-case buffer names map into lower-case buffers, but with
65 * append mode set so the buffer is appended to, not overwritten.
69 CBNAME(sp
, cbp
, name
);
72 * If this is a new buffer, create it, and add it into the list.
73 * Otherwise, if it's not an append, free its current contents.
76 CALLOC(sp
, cbp
, CB
*, 1, sizeof(CB
));
78 LIST_INSERT_HEAD(&sp
->gp
->cutq
, cbp
, q
);
79 CIRCLEQ_INIT(&cbp
->textq
);
81 text_lfree(&cbp
->textq
);
87 for (lno
= fm
->lno
; lno
<= tm
->lno
; ++lno
) {
88 if (cb_line(sp
, ep
, lno
, 0, 0, &tp
))
90 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
93 cbp
->flags
|= CB_LMODE
;
97 /* Get the first line. */
98 len
= fm
->lno
< tm
->lno
? 0 : tm
->cno
- fm
->cno
;
99 if (cb_line(sp
, ep
, fm
->lno
, fm
->cno
, len
, &tp
))
102 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
105 for (lno
= fm
->lno
; ++lno
< tm
->lno
;) {
106 if (cb_line(sp
, ep
, lno
, 0, 0, &tp
))
108 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
112 if (tm
->lno
> fm
->lno
&& tm
->cno
> 0) {
113 if (cb_line(sp
, ep
, lno
, 0, tm
->cno
, &tp
)) {
116 "Contents of %s buffer lost.",
118 text_lfree(&cbp
->textq
);
123 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
131 * Rotate the numbered buffers up one.
140 for (cbp
= sp
->gp
->cutq
.lh_first
; cbp
!= NULL
; cbp
= cbp
->q
.le_next
)
170 if (del_cbp
!= NULL
) {
171 LIST_REMOVE(del_cbp
, q
);
172 text_lfree(&del_cbp
->textq
);
173 FREE(del_cbp
, sizeof(CB
));
180 * Cut a portion of a single line.
183 cb_line(sp
, ep
, lno
, fcno
, clen
, newp
)
194 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
195 GETLINE_ERR(sp
, lno
);
199 if ((*newp
= tp
= text_init(sp
, NULL
, 0, len
)) == NULL
)
203 * A length of zero means to cut from the MARK to the end
209 memmove(tp
->lb
, p
+ fcno
, clen
);
217 * Allocate a new TEXT structure.
220 text_init(sp
, p
, len
, total_len
)
223 size_t len
, total_len
;
227 MALLOC(sp
, tp
, TEXT
*, sizeof(TEXT
));
230 /* ANSI C doesn't define a call to malloc(2) for 0 bytes. */
231 if (tp
->lb_len
= total_len
) {
232 MALLOC(sp
, tp
->lb
, CHAR_T
*, tp
->lb_len
);
233 if (tp
->lb
== NULL
) {
237 if (p
!= NULL
&& len
!= 0)
238 memmove(tp
->lb
, p
, len
);
242 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
250 * Free a chain of text structures.
258 while ((tp
= headp
->cqh_first
) != (void *)headp
) {
259 CIRCLEQ_REMOVE(headp
, tp
, q
);
266 * Free a text structure.
273 FREE(tp
->lb
, tp
->lb_len
);
275 FREE(tp
->wd
, tp
->wd_len
);
276 FREE(tp
, sizeof(TEXT
));
281 * Put text buffer contents into the file.
284 * Historically, pasting into a file with no lines in vi would preserve
285 * the single blank line. This is almost certainly a result of the fact
286 * that historic vi couldn't deal with a file that had no lines in it.
287 * This implementation treats that as a bug, and does not retain the
291 put(sp
, ep
, name
, cp
, rp
, append
)
301 size_t blen
, clen
, len
;
305 CBEMPTY(sp
, cbp
, name
);
307 tp
= cbp
->textq
.cqh_first
;
308 lmode
= F_ISSET(cbp
, CB_LMODE
);
311 * It's possible to do a put into an empty file, meaning that the
312 * cut buffer simply becomes the file. It's a special case so
313 * that we can ignore it in general.
315 * Historical practice is that the cursor ends up on the first
316 * non-blank character of the first line inserted.
319 if (file_lline(sp
, ep
, &lno
))
322 for (; tp
!= (void *)&cbp
->textq
;
323 ++lno
, tp
= tp
->q
.cqe_next
)
324 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
328 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
334 * If buffer was created in line mode, append each new line into the
338 lno
= append
? cp
->lno
: cp
->lno
- 1;
340 for (; tp
!= (void *)&cbp
->textq
; ++lno
, tp
= tp
->q
.cqe_next
)
341 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
344 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
349 * If buffer was cut in character mode, replace the current line with
350 * one built from the portion of the first line to the left of the
351 * split plus the first line in the CB. Append each intermediate line
352 * in the CB. Append a line built from the portion of the first line
353 * to the right of the split plus the last line in the CB.
355 * Get the first line.
358 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
359 GETLINE_ERR(sp
, lno
);
363 GET_SPACE_RET(sp
, bp
, blen
, tp
->len
+ len
+ 1);
366 /* Original line, left of the split. */
367 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
368 memmove(bp
, p
, clen
);
373 /* First line from the CB. */
374 memmove(t
, tp
->lb
, tp
->len
);
377 /* Calculate length left in original line. */
378 clen
= len
? len
- cp
->cno
- (append
? 1 : 0) : 0;
381 * If no more lines in the CB, append the rest of the original
382 * line and quit. Otherwise, build the last line before doing
383 * the intermediate lines, because the line changes will lose
386 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
388 * Historical practice is that if a non-line mode put
389 * is inside a single line, the cursor ends up on the
390 * last character inserted.
393 rp
->cno
= (t
- bp
) - 1;
399 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
403 * Have to build both the first and last lines of the
404 * put before doing any sets or we'll lose the cached
405 * line. Build both the first and last lines in the
406 * same buffer, so we don't have to have another buffer
409 * Last part of original line; check for space, reset
410 * the pointer into the buffer.
412 ltp
= cbp
->textq
.cqh_last
;
414 ADD_SPACE_RET(sp
, bp
, blen
, ltp
->len
+ clen
);
417 /* Add in last part of the CB. */
418 memmove(t
, ltp
->lb
, ltp
->len
);
420 memmove(t
+ ltp
->len
, p
, clen
);
424 * Now: bp points to the first character of the first
425 * line, t points to the last character of the last
426 * line, t - bp is the length of the first line, and
427 * clen is the length of the last. Just figured you'd
430 * Output the line replacing the original line.
432 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
436 * Historical practice is that if a non-line mode put
437 * covers multiple lines, the cursor ends up on the
438 * first character inserted. (Of course.)
441 rp
->cno
= (t
- bp
) - 1;
443 /* Output any intermediate lines in the CB. */
444 for (tp
= tp
->q
.cqe_next
;
445 tp
->q
.cqe_next
!= (void *)&cbp
->textq
;
446 ++lno
, tp
= tp
->q
.cqe_next
)
447 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
450 if (file_aline(sp
, ep
, 1, lno
, t
, clen
)) {
451 mem
: FREE_SPACE(sp
, bp
, blen
);
455 FREE_SPACE(sp
, bp
, blen
);
458 ret
: sp
->rptlines
[L_PUT
] += lno
- cp
->lno
;