update to 1.01
[nvi.git] / common / cut.c
blobd6423fe07189013f9eb815f988093361d919efa0
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: cut.c,v 8.19 1994/01/11 22:17:59 bostic Exp $ (Berkeley) $Date: 1994/01/11 22:17:59 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "vi.h"
22 static int cb_line __P((SCR *, EXF *, recno_t, size_t, size_t, TEXT **));
23 static int cb_rotate __P((SCR *));
25 /*
26 * cut --
27 * Put a range of lines/columns into a buffer.
29 * There are two buffer areas, both found in the global structure. The first
30 * is the linked list of all the buffers the user has named, the second is the
31 * default buffer storage. There is a pointer, too, which is the current
32 * default buffer, i.e. it may point to the default buffer or a named buffer
33 * depending on into what buffer the last text was cut. In both delete and
34 * yank operations, text is cut into either the buffer named by the user, or
35 * the default buffer. If it's a delete of information on more than a single
36 * line, the contents of the numbered buffers are rotated up one, the contents
37 * of the buffer named '9' are discarded, and the text is also cut into the
38 * buffer named '1'.
40 * In all cases, upper-case buffer names are the same as lower-case names,
41 * with the exception that they cause the buffer to be appended to instead
42 * of replaced.
44 * !!!
45 * The contents of the default buffer would disappear after most operations in
46 * historic vi. It's unclear that this is useful, so we don't bother.
48 * When users explicitly cut text into the numeric buffers, historic vi became
49 * genuinely strange. I've never been able to figure out what was supposed to
50 * happen. It behaved differently if you deleted text than if you yanked text,
51 * and, in the latter case, the text was appended to the buffer instead of
52 * replacing the contents. Hopefully it's not worth getting right.
54 int
55 cut(sp, ep, cbp, namep, fm, tm, flags)
56 SCR *sp;
57 EXF *ep;
58 CB *cbp;
59 CHAR_T *namep;
60 int flags;
61 MARK *fm, *tm;
63 CHAR_T name;
64 TEXT *tp;
65 recno_t lno;
66 size_t len;
67 int append, namedbuffer, setdefcb;
69 #if defined(DEBUG) && 0
70 TRACE(sp, "cut: from {%lu, %d}, to {%lu, %d}%s\n",
71 fm->lno, fm->cno, tm->lno, tm->cno,
72 LF_ISSET(CUT_LINEMODE) ? " LINE MODE" : "");
73 #endif
74 if (cbp == NULL) {
75 if (LF_ISSET(CUT_DELETE) && fm->lno != tm->lno) {
76 (void)cb_rotate(sp);
77 name = '1';
78 goto defcb;
80 if (namep == NULL) {
81 cbp = sp->gp->dcb_store;
82 append = namedbuffer = 0;
83 setdefcb = 1;
84 } else {
85 name = *namep;
86 defcb: CBNAME(sp, cbp, name);
87 append = isupper(name);
88 namedbuffer = setdefcb = 1;
90 } else
91 append = namedbuffer = setdefcb = 0;
94 * If this is a new buffer, create it and add it into the list.
95 * Otherwise, if it's not an append, free its current contents.
97 if (cbp == NULL) {
98 CALLOC(sp, cbp, CB *, 1, sizeof(CB));
99 cbp->name = name;
100 CIRCLEQ_INIT(&cbp->textq);
101 if (namedbuffer) {
102 LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
103 } else
104 sp->gp->dcb_store = cbp;
105 } else if (!append) {
106 text_lfree(&cbp->textq);
107 cbp->len = 0;
108 cbp->flags = 0;
111 /* In line mode, it's pretty easy, just cut the lines. */
112 if (LF_ISSET(CUT_LINEMODE)) {
113 for (lno = fm->lno; lno <= tm->lno; ++lno) {
114 if (cb_line(sp, ep, lno, 0, 0, &tp))
115 goto mem;
116 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
117 cbp->len += tp->len;
119 cbp->flags |= CB_LMODE;
120 } else {
121 /* Get the first line. */
122 len = fm->lno < tm->lno ? 0 : tm->cno - fm->cno;
123 if (cb_line(sp, ep, fm->lno, fm->cno, len, &tp))
124 goto mem;
125 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
126 cbp->len += tp->len;
128 /* Get the intermediate lines. */
129 for (lno = fm->lno; ++lno < tm->lno;) {
130 if (cb_line(sp, ep, lno, 0, 0, &tp))
131 goto mem;
132 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
133 cbp->len += tp->len;
136 /* Get the last line. */
137 if (tm->lno > fm->lno && tm->cno > 0) {
138 if (cb_line(sp, ep, lno, 0, tm->cno, &tp)) {
139 mem: if (append)
140 msgq(sp, M_ERR,
141 "Contents of %s buffer lost.",
142 charname(sp, name));
143 text_lfree(&cbp->textq);
144 cbp->len = 0;
145 cbp->flags = 0;
146 return (1);
148 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
149 cbp->len += tp->len;
152 if (setdefcb)
153 sp->gp->dcbp = cbp; /* Repoint default buffer. */
154 return (0);
158 * cb_rotate --
159 * Rotate the numbered buffers up one.
161 static int
162 cb_rotate(sp)
163 SCR *sp;
165 CB *cbp, *del_cbp;
167 del_cbp = NULL;
168 for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
169 switch(cbp->name) {
170 case '1':
171 cbp->name = '2';
172 break;
173 case '2':
174 cbp->name = '3';
175 break;
176 case '3':
177 cbp->name = '4';
178 break;
179 case '4':
180 cbp->name = '5';
181 break;
182 case '5':
183 cbp->name = '6';
184 break;
185 case '6':
186 cbp->name = '7';
187 break;
188 case '7':
189 cbp->name = '8';
190 break;
191 case '8':
192 cbp->name = '9';
193 break;
194 case '9':
195 del_cbp = cbp;
196 break;
198 if (del_cbp != NULL) {
199 LIST_REMOVE(del_cbp, q);
200 text_lfree(&del_cbp->textq);
201 FREE(del_cbp, sizeof(CB));
203 return (0);
207 * cb_line --
208 * Cut a portion of a single line.
210 static int
211 cb_line(sp, ep, lno, fcno, clen, newp)
212 SCR *sp;
213 EXF *ep;
214 recno_t lno;
215 size_t fcno, clen;
216 TEXT **newp;
218 TEXT *tp;
219 size_t len;
220 char *p;
222 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
223 GETLINE_ERR(sp, lno);
224 return (1);
227 if ((*newp = tp = text_init(sp, NULL, 0, len)) == NULL)
228 return (1);
231 * A length of zero means to cut from the MARK to the end
232 * of the line.
234 if (len != 0) {
235 if (clen == 0)
236 clen = len - fcno;
237 memmove(tp->lb, p + fcno, clen);
238 tp->len = clen;
240 return (0);
244 * text_init --
245 * Allocate a new TEXT structure.
247 TEXT *
248 text_init(sp, p, len, total_len)
249 SCR *sp;
250 const char *p;
251 size_t len, total_len;
253 TEXT *tp;
255 MALLOC(sp, tp, TEXT *, sizeof(TEXT));
256 if (tp == NULL)
257 return (NULL);
258 /* ANSI C doesn't define a call to malloc(2) for 0 bytes. */
259 if (tp->lb_len = total_len) {
260 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
261 if (tp->lb == NULL) {
262 free(tp);
263 return (NULL);
265 if (p != NULL && len != 0)
266 memmove(tp->lb, p, len);
267 } else
268 tp->lb = NULL;
269 tp->len = len;
270 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
271 tp->wd = NULL;
272 tp->wd_len = 0;
273 return (tp);
277 * text_lfree --
278 * Free a chain of text structures.
280 void
281 text_lfree(headp)
282 TEXTH *headp;
284 TEXT *tp;
286 while ((tp = headp->cqh_first) != (void *)headp) {
287 CIRCLEQ_REMOVE(headp, tp, q);
288 text_free(tp);
293 * text_free --
294 * Free a text structure.
296 void
297 text_free(tp)
298 TEXT *tp;
300 if (tp->lb != NULL)
301 FREE(tp->lb, tp->lb_len);
302 if (tp->wd != NULL)
303 FREE(tp->wd, tp->wd_len);
304 FREE(tp, sizeof(TEXT));
308 * put --
309 * Put text buffer contents into the file.
311 * !!!
312 * Historically, pasting into a file with no lines in vi would preserve
313 * the single blank line. This is almost certainly a result of the fact
314 * that historic vi couldn't deal with a file that had no lines in it.
315 * This implementation treats that as a bug, and does not retain the
316 * blank line.
319 put(sp, ep, cbp, namep, cp, rp, append)
320 SCR *sp;
321 EXF *ep;
322 CB *cbp;
323 CHAR_T *namep;
324 MARK *cp, *rp;
325 int append;
327 CHAR_T name;
328 TEXT *ltp, *tp;
329 recno_t lno;
330 size_t blen, clen, len;
331 char *bp, *p, *t;
333 if (cbp == NULL)
334 if (namep == NULL) {
335 cbp = sp->gp->dcbp;
336 if (cbp == NULL) {
337 msgq(sp, M_ERR, "The default buffer is empty.");
338 return (1);
340 } else {
341 name = *namep;
342 CBNAME(sp, cbp, name);
343 if (cbp == NULL) {
344 msgq(sp, M_ERR,
345 "Buffer %s is empty.", charname(sp, name));
346 return (1);
349 tp = cbp->textq.cqh_first;
352 * It's possible to do a put into an empty file, meaning that the
353 * cut buffer simply becomes the file. It's a special case so
354 * that we can ignore it in general.
356 * Historical practice is that the cursor ends up on the first
357 * non-blank character of the first line inserted.
359 if (cp->lno == 1) {
360 if (file_lline(sp, ep, &lno))
361 return (1);
362 if (lno == 0) {
363 for (; tp != (void *)&cbp->textq;
364 ++lno, tp = tp->q.cqe_next)
365 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
366 return (1);
367 rp->lno = 1;
368 rp->cno = 0;
369 (void)nonblank(sp, ep, rp->lno, &rp->cno);
370 goto ret;
374 /* If a line mode buffer, append each new line into the file. */
375 if (F_ISSET(cbp, CB_LMODE)) {
376 lno = append ? cp->lno : cp->lno - 1;
377 rp->lno = lno + 1;
378 for (; tp != (void *)&cbp->textq; ++lno, tp = tp->q.cqe_next)
379 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
380 return (1);
381 rp->cno = 0;
382 (void)nonblank(sp, ep, rp->lno, &rp->cno);
383 goto ret;
387 * If buffer was cut in character mode, replace the current line with
388 * one built from the portion of the first line to the left of the
389 * split plus the first line in the CB. Append each intermediate line
390 * in the CB. Append a line built from the portion of the first line
391 * to the right of the split plus the last line in the CB.
393 * Get the first line.
395 lno = cp->lno;
396 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
397 GETLINE_ERR(sp, lno);
398 return (1);
401 GET_SPACE_RET(sp, bp, blen, tp->len + len + 1);
402 t = bp;
404 /* Original line, left of the split. */
405 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
406 memmove(bp, p, clen);
407 p += clen;
408 t += clen;
411 /* First line from the CB. */
412 memmove(t, tp->lb, tp->len);
413 t += tp->len;
415 /* Calculate length left in original line. */
416 clen = len ? len - cp->cno - (append ? 1 : 0) : 0;
419 * If no more lines in the CB, append the rest of the original
420 * line and quit. Otherwise, build the last line before doing
421 * the intermediate lines, because the line changes will lose
422 * the cached line.
424 if (tp->q.cqe_next == (void *)&cbp->textq) {
426 * Historical practice is that if a non-line mode put
427 * is inside a single line, the cursor ends up on the
428 * last character inserted.
430 rp->lno = lno;
431 rp->cno = (t - bp) - 1;
433 if (clen > 0) {
434 memmove(t, p, clen);
435 t += clen;
437 if (file_sline(sp, ep, lno, bp, t - bp))
438 goto mem;
439 } else {
441 * Have to build both the first and last lines of the
442 * put before doing any sets or we'll lose the cached
443 * line. Build both the first and last lines in the
444 * same buffer, so we don't have to have another buffer
445 * floating around.
447 * Last part of original line; check for space, reset
448 * the pointer into the buffer.
450 ltp = cbp->textq.cqh_last;
451 len = t - bp;
452 ADD_SPACE_RET(sp, bp, blen, ltp->len + clen);
453 t = bp + len;
455 /* Add in last part of the CB. */
456 memmove(t, ltp->lb, ltp->len);
457 if (clen)
458 memmove(t + ltp->len, p, clen);
459 clen += ltp->len;
462 * Now: bp points to the first character of the first
463 * line, t points to the last character of the last
464 * line, t - bp is the length of the first line, and
465 * clen is the length of the last. Just figured you'd
466 * want to know.
468 * Output the line replacing the original line.
470 if (file_sline(sp, ep, lno, bp, t - bp))
471 goto mem;
474 * Historical practice is that if a non-line mode put
475 * covers multiple lines, the cursor ends up on the
476 * first character inserted. (Of course.)
478 rp->lno = lno;
479 rp->cno = (t - bp) - 1;
481 /* Output any intermediate lines in the CB. */
482 for (tp = tp->q.cqe_next;
483 tp->q.cqe_next != (void *)&cbp->textq;
484 ++lno, tp = tp->q.cqe_next)
485 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
486 goto mem;
488 if (file_aline(sp, ep, 1, lno, t, clen)) {
489 mem: FREE_SPACE(sp, bp, blen);
490 return (1);
493 FREE_SPACE(sp, bp, blen);
495 /* Reporting... */
496 ret: sp->rptlines[L_PUT] += lno - cp->lno;
498 return (0);