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.14 1993/11/19 11:54:55 bostic Exp $ (Berkeley) $Date: 1993/11/19 11:54:55 $";
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 if ((cbp
= malloc(sizeof(CB
))) == NULL
) {
77 msgq(sp
, M_SYSERR
, NULL
);
80 memset(cbp
, 0, sizeof(CB
));
82 LIST_INSERT_HEAD(&sp
->gp
->cutq
, cbp
, q
);
83 CIRCLEQ_INIT(&cbp
->textq
);
85 text_lfree(&cbp
->textq
);
91 for (lno
= fm
->lno
; lno
<= tm
->lno
; ++lno
) {
92 if (cb_line(sp
, ep
, lno
, 0, 0, &tp
))
94 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
97 cbp
->flags
|= CB_LMODE
;
101 /* Get the first line. */
102 len
= fm
->lno
< tm
->lno
? 0 : tm
->cno
- fm
->cno
;
103 if (cb_line(sp
, ep
, fm
->lno
, fm
->cno
, len
, &tp
))
106 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
109 for (lno
= fm
->lno
; ++lno
< tm
->lno
;) {
110 if (cb_line(sp
, ep
, lno
, 0, 0, &tp
))
112 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
116 if (tm
->lno
> fm
->lno
&& tm
->cno
> 0) {
117 if (cb_line(sp
, ep
, lno
, 0, tm
->cno
, &tp
)) {
120 "Contents of %s buffer lost.",
122 text_lfree(&cbp
->textq
);
127 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
135 * Rotate the numbered buffers up one.
144 for (cbp
= sp
->gp
->cutq
.lh_first
; cbp
!= NULL
; cbp
= cbp
->q
.le_next
)
174 if (del_cbp
!= NULL
) {
175 LIST_REMOVE(del_cbp
, q
);
176 text_lfree(&del_cbp
->textq
);
177 FREE(del_cbp
, sizeof(CB
));
184 * Cut a portion of a single line.
187 cb_line(sp
, ep
, lno
, fcno
, clen
, newp
)
198 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
199 GETLINE_ERR(sp
, lno
);
203 if ((*newp
= tp
= text_init(sp
, NULL
, 0, len
)) == NULL
)
207 * A length of zero means to cut from the MARK to the end
213 memmove(tp
->lb
, p
+ fcno
, clen
);
221 * Allocate a new TEXT structure.
224 text_init(sp
, p
, len
, total_len
)
227 size_t len
, total_len
;
231 if ((tp
= malloc(sizeof(TEXT
))) == NULL
)
233 if ((tp
->lb
= malloc(tp
->lb_len
= total_len
)) == NULL
) {
235 mem
: msgq(sp
, M_SYSERR
, NULL
);
240 memset(tp
->lb
, 0, total_len
- 1);
243 if (p
!= NULL
&& len
!= 0)
244 memmove(tp
->lb
, p
, len
);
246 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
255 * Free a chain of text structures.
263 while ((tp
= headp
->cqh_first
) != (void *)headp
) {
264 CIRCLEQ_REMOVE(headp
, tp
, q
);
271 * Free a text structure.
278 FREE(tp
->lb
, tp
->lb_len
);
280 FREE(tp
->wd
, tp
->wd_len
);
281 FREE(tp
, sizeof(TEXT
));
286 * Put text buffer contents into the file.
289 * Historically, pasting into a file with no lines in vi would preserve
290 * the single blank line. This is almost certainly a result of the fact
291 * that historic vi couldn't deal with a file that had no lines in it.
292 * This implementation treats that as a bug, and does not retain the
296 put(sp
, ep
, name
, cp
, rp
, append
)
306 size_t blen
, clen
, len
;
310 CBEMPTY(sp
, cbp
, name
);
312 tp
= cbp
->textq
.cqh_first
;
313 lmode
= F_ISSET(cbp
, CB_LMODE
);
316 * It's possible to do a put into an empty file, meaning that the
317 * cut buffer simply becomes the file. It's a special case so
318 * that we can ignore it in general.
320 * Historical practice is that the cursor ends up on the first
321 * non-blank character of the first line inserted.
324 if (file_lline(sp
, ep
, &lno
))
327 for (; tp
!= (void *)&cbp
->textq
;
328 ++lno
, tp
= tp
->q
.cqe_next
)
329 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
333 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
339 * If buffer was created in line mode, append each new line into the
343 lno
= append
? cp
->lno
: cp
->lno
- 1;
345 for (; tp
!= (void *)&cbp
->textq
; ++lno
, tp
= tp
->q
.cqe_next
)
346 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
349 (void)nonblank(sp
, ep
, rp
->lno
, &rp
->cno
);
354 * If buffer was cut in character mode, replace the current line with
355 * one built from the portion of the first line to the left of the
356 * split plus the first line in the CB. Append each intermediate line
357 * in the CB. Append a line built from the portion of the first line
358 * to the right of the split plus the last line in the CB.
360 * Get the first line.
363 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
364 GETLINE_ERR(sp
, lno
);
368 GET_SPACE(sp
, bp
, blen
, tp
->len
+ len
+ 1);
371 /* Original line, left of the split. */
372 if (len
> 0 && (clen
= cp
->cno
+ (append
? 1 : 0)) > 0) {
373 memmove(bp
, p
, clen
);
378 /* First line from the CB. */
379 memmove(t
, tp
->lb
, tp
->len
);
382 /* Calculate length left in original line. */
383 clen
= len
? len
- cp
->cno
- (append
? 1 : 0) : 0;
386 * If no more lines in the CB, append the rest of the original
387 * line and quit. Otherwise, build the last line before doing
388 * the intermediate lines, because the line changes will lose
391 if (tp
->q
.cqe_next
== (void *)&cbp
->textq
) {
393 * Historical practice is that if a non-line mode put
394 * is inside a single line, the cursor ends up on the
395 * last character inserted.
398 rp
->cno
= (t
- bp
) - 1;
404 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
408 * Have to build both the first and last lines of the
409 * put before doing any sets or we'll lose the cached
410 * line. Build both the first and last lines in the
411 * same buffer, so we don't have to have another buffer
414 * Last part of original line; check for space, reset
415 * the pointer into the buffer.
417 ltp
= cbp
->textq
.cqh_last
;
419 ADD_SPACE(sp
, bp
, blen
, ltp
->len
+ clen
);
422 /* Add in last part of the CB. */
423 memmove(t
, ltp
->lb
, ltp
->len
);
425 memmove(t
+ ltp
->len
, p
, clen
);
429 * Now: bp points to the first character of the first
430 * line, t points to the last character of the last
431 * line, t - bp is the length of the first line, and
432 * clen is the length of the last. Just figured you'd
435 * Output the line replacing the original line.
437 if (file_sline(sp
, ep
, lno
, bp
, t
- bp
))
441 * Historical practice is that if a non-line mode put
442 * covers multiple lines, the cursor ends up on the
443 * first character inserted. (Of course.)
446 rp
->cno
= (t
- bp
) - 1;
448 /* Output any intermediate lines in the CB. */
449 for (tp
= tp
->q
.cqe_next
;
450 tp
->q
.cqe_next
!= (void *)&cbp
->textq
;
451 ++lno
, tp
= tp
->q
.cqe_next
)
452 if (file_aline(sp
, ep
, 1, lno
, tp
->lb
, tp
->len
))
455 if (file_aline(sp
, ep
, 1, lno
, t
, clen
)) {
456 mem
: FREE_SPACE(sp
, bp
, blen
);
460 FREE_SPACE(sp
, bp
, blen
);
463 ret
: sp
->rptlines
[L_PUT
] += lno
- cp
->lno
;