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.15 1993/12/09 19:42:03 bostic Exp $ (Berkeley) $Date: 1993/12/09 19:42:03 $";
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 tp
->lb_len
= total_len
;
231 MALLOC(sp
, tp
->lb
, CHAR_T
*, total_len
);
232 if (tp
->lb
== NULL
) {
233 FREE(tp
, sizeof(TEXT
));
238 memset(tp
->lb
, 0, total_len
- 1);
241 if (p
!= NULL
&& len
!= 0)
242 memmove(tp
->lb
, p
, len
);
244 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
253 * Free a chain of text structures.
261 while ((tp
= headp
->cqh_first
) != (void *)headp
) {
262 CIRCLEQ_REMOVE(headp
, tp
, q
);
269 * Free a text structure.
276 FREE(tp
->lb
, tp
->lb_len
);
278 FREE(tp
->wd
, tp
->wd_len
);
279 FREE(tp
, sizeof(TEXT
));
284 * Put text buffer contents into the file.
287 * Historically, pasting into a file with no lines in vi would preserve
288 * the single blank line. This is almost certainly a result of the fact
289 * that historic vi couldn't deal with a file that had no lines in it.
290 * This implementation treats that as a bug, and does not retain the
294 put(sp
, ep
, name
, cp
, rp
, append
)
304 size_t blen
, clen
, len
;
308 CBEMPTY(sp
, cbp
, name
);
310 tp
= cbp
->textq
.cqh_first
;
311 lmode
= F_ISSET(cbp
, CB_LMODE
);
314 * It's possible to do a put into an empty file, meaning that the
315 * cut buffer simply becomes the file. It's a special case so
316 * that we can ignore it in general.
318 * Historical practice is that the cursor ends up on the first
319 * non-blank character of the first line inserted.
322 if (file_lline(sp
, ep
, &lno
))
325 for (; tp
!= (void *)&cbp
->textq
;
326 ++lno
, tp
= tp
->q
.cqe_next
)
327 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
331 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
337 * If buffer was created in line mode, append each new line into the
341 lno
= append
? cp
->lno
: cp
->lno
- 1;
343 for (; tp
!= (void *)&cbp
->textq
; ++lno
, tp
= tp
->q
.cqe_next
)
344 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
347 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
352 * If buffer was cut in character mode, replace the current line with
353 * one built from the portion of the first line to the left of the
354 * split plus the first line in the CB. Append each intermediate line
355 * in the CB. Append a line built from the portion of the first line
356 * to the right of the split plus the last line in the CB.
358 * Get the first line.
361 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
362 GETLINE_ERR(sp
, lno
);
366 GET_SPACE_RET(sp
, bp
, blen
, tp
->len
+ len
+ 1);
369 /* Original line, left of the split. */
370 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
371 memmove(bp
, p
, clen
);
376 /* First line from the CB. */
377 memmove(t
, tp
->lb
, tp
->len
);
380 /* Calculate length left in original line. */
381 clen
= len
? len
- cp
->cno
- (append
? 1 : 0) : 0;
384 * If no more lines in the CB, append the rest of the original
385 * line and quit. Otherwise, build the last line before doing
386 * the intermediate lines, because the line changes will lose
389 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
391 * Historical practice is that if a non-line mode put
392 * is inside a single line, the cursor ends up on the
393 * last character inserted.
396 rp
->cno
= (t
- bp
) - 1;
402 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
406 * Have to build both the first and last lines of the
407 * put before doing any sets or we'll lose the cached
408 * line. Build both the first and last lines in the
409 * same buffer, so we don't have to have another buffer
412 * Last part of original line; check for space, reset
413 * the pointer into the buffer.
415 ltp
= cbp
->textq
.cqh_last
;
417 ADD_SPACE_RET(sp
, bp
, blen
, ltp
->len
+ clen
);
420 /* Add in last part of the CB. */
421 memmove(t
, ltp
->lb
, ltp
->len
);
423 memmove(t
+ ltp
->len
, p
, clen
);
427 * Now: bp points to the first character of the first
428 * line, t points to the last character of the last
429 * line, t - bp is the length of the first line, and
430 * clen is the length of the last. Just figured you'd
433 * Output the line replacing the original line.
435 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
439 * Historical practice is that if a non-line mode put
440 * covers multiple lines, the cursor ends up on the
441 * first character inserted. (Of course.)
444 rp
->cno
= (t
- bp
) - 1;
446 /* Output any intermediate lines in the CB. */
447 for (tp
= tp
->q
.cqe_next
;
448 tp
->q
.cqe_next
!= (void *)&cbp
->textq
;
449 ++lno
, tp
= tp
->q
.cqe_next
)
450 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
453 if (file_aline(sp
, ep
, 1, lno
, t
, clen
)) {
454 mem
: FREE_SPACE(sp
, bp
, blen
);
458 FREE_SPACE(sp
, bp
, blen
);
461 ret
: sp
->rptlines
[L_PUT
] += lno
- cp
->lno
;