rework ARGS structures as part of ex parser rework
[nvi.git] / common / cut.c
blob1d4e0b9a444d582bb4ebf32effd8dc1918a67020
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.14 1993/11/19 11:54:55 bostic Exp $ (Berkeley) $Date: 1993/11/19 11:54:55 $";
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 int
30 cut(sp, ep, name, fm, tm, lmode)
31 SCR *sp;
32 EXF *ep;
33 ARG_CHAR_T name;
34 int lmode;
35 MARK *fm, *tm;
37 CB *cbp;
38 TEXT *tp;
39 recno_t lno;
40 size_t len;
41 int append;
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" : "");
46 #endif
48 * !!!
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.
56 append = 0;
57 if (isdigit(name)) {
58 (void)cb_rotate(sp);
59 if (name != '1')
60 append = 1;
64 * Upper-case buffer names map into lower-case buffers, but with
65 * append mode set so the buffer is appended to, not overwritten.
67 if (isupper(name))
68 append = 1;
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.
75 if (cbp == NULL) {
76 if ((cbp = malloc(sizeof(CB))) == NULL) {
77 msgq(sp, M_SYSERR, NULL);
78 return (1);
80 memset(cbp, 0, sizeof(CB));
81 cbp->name = name;
82 LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
83 CIRCLEQ_INIT(&cbp->textq);
84 } else if (!append) {
85 text_lfree(&cbp->textq);
86 cbp->len = 0;
87 cbp->flags = 0;
90 if (lmode) {
91 for (lno = fm->lno; lno <= tm->lno; ++lno) {
92 if (cb_line(sp, ep, lno, 0, 0, &tp))
93 goto mem;
94 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
95 cbp->len += tp->len;
97 cbp->flags |= CB_LMODE;
98 return (0);
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))
104 goto mem;
106 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
107 cbp->len += tp->len;
109 for (lno = fm->lno; ++lno < tm->lno;) {
110 if (cb_line(sp, ep, lno, 0, 0, &tp))
111 goto mem;
112 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
113 cbp->len += tp->len;
116 if (tm->lno > fm->lno && tm->cno > 0) {
117 if (cb_line(sp, ep, lno, 0, tm->cno, &tp)) {
118 mem: if (append)
119 msgq(sp, M_ERR,
120 "Contents of %s buffer lost.",
121 charname(sp, name));
122 text_lfree(&cbp->textq);
123 cbp->len = 0;
124 cbp->flags = 0;
125 return (1);
127 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
128 cbp->len += tp->len;
130 return (0);
134 * cb_rotate --
135 * Rotate the numbered buffers up one.
137 static int
138 cb_rotate(sp)
139 SCR *sp;
141 CB *cbp, *del_cbp;
143 del_cbp = NULL;
144 for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
145 switch(cbp->name) {
146 case '1':
147 cbp->name = '2';
148 break;
149 case '2':
150 cbp->name = '3';
151 break;
152 case '3':
153 cbp->name = '4';
154 break;
155 case '4':
156 cbp->name = '5';
157 break;
158 case '5':
159 cbp->name = '6';
160 break;
161 case '6':
162 cbp->name = '7';
163 break;
164 case '7':
165 cbp->name = '8';
166 break;
167 case '8':
168 cbp->name = '9';
169 break;
170 case '9':
171 del_cbp = cbp;
172 break;
174 if (del_cbp != NULL) {
175 LIST_REMOVE(del_cbp, q);
176 text_lfree(&del_cbp->textq);
177 FREE(del_cbp, sizeof(CB));
179 return (0);
183 * cb_line --
184 * Cut a portion of a single line.
186 static int
187 cb_line(sp, ep, lno, fcno, clen, newp)
188 SCR *sp;
189 EXF *ep;
190 recno_t lno;
191 size_t fcno, clen;
192 TEXT **newp;
194 TEXT *tp;
195 size_t len;
196 char *p;
198 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
199 GETLINE_ERR(sp, lno);
200 return (1);
203 if ((*newp = tp = text_init(sp, NULL, 0, len)) == NULL)
204 return (1);
207 * A length of zero means to cut from the MARK to the end
208 * of the line.
210 if (len != 0) {
211 if (clen == 0)
212 clen = len - fcno;
213 memmove(tp->lb, p + fcno, clen);
214 tp->len = clen;
216 return (0);
220 * text_init --
221 * Allocate a new TEXT structure.
223 TEXT *
224 text_init(sp, p, len, total_len)
225 SCR *sp;
226 const char *p;
227 size_t len, total_len;
229 TEXT *tp;
231 if ((tp = malloc(sizeof(TEXT))) == NULL)
232 goto mem;
233 if ((tp->lb = malloc(tp->lb_len = total_len)) == NULL) {
234 free(tp);
235 mem: msgq(sp, M_SYSERR, NULL);
236 return (NULL);
238 #ifdef DEBUG
239 if (total_len)
240 memset(tp->lb, 0, total_len - 1);
241 #endif
243 if (p != NULL && len != 0)
244 memmove(tp->lb, p, len);
245 tp->len = len;
246 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
247 tp->wd = NULL;
248 tp->wd_len = 0;
250 return (tp);
254 * text_lfree --
255 * Free a chain of text structures.
257 void
258 text_lfree(headp)
259 TEXTH *headp;
261 TEXT *tp;
263 while ((tp = headp->cqh_first) != (void *)headp) {
264 CIRCLEQ_REMOVE(headp, tp, q);
265 text_free(tp);
270 * text_free --
271 * Free a text structure.
273 void
274 text_free(tp)
275 TEXT *tp;
277 if (tp->lb != NULL)
278 FREE(tp->lb, tp->lb_len);
279 if (tp->wd != NULL)
280 FREE(tp->wd, tp->wd_len);
281 FREE(tp, sizeof(TEXT));
285 * put --
286 * Put text buffer contents into the file.
288 * !!!
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
293 * blank line.
296 put(sp, ep, name, cp, rp, append)
297 SCR *sp;
298 EXF *ep;
299 ARG_CHAR_T name;
300 MARK *cp, *rp;
301 int append;
303 CB *cbp;
304 TEXT *ltp, *tp;
305 recno_t lno;
306 size_t blen, clen, len;
307 int lmode;
308 char *bp, *p, *t;
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.
323 if (cp->lno == 1) {
324 if (file_lline(sp, ep, &lno))
325 return (1);
326 if (lno == 0) {
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))
330 return (1);
331 rp->lno = 1;
332 rp->cno = 0;
333 (void)nonblank(sp, ep, rp->lno, &rp->cno);
334 goto ret;
339 * If buffer was created in line mode, append each new line into the
340 * file.
342 if (lmode) {
343 lno = append ? cp->lno : cp->lno - 1;
344 rp->lno = 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))
347 return (1);
348 rp->cno = 0;
349 (void)nonblank(sp, ep, rp->lno, &rp->cno);
350 goto ret;
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.
362 lno = cp->lno;
363 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
364 GETLINE_ERR(sp, lno);
365 return (1);
368 GET_SPACE(sp, bp, blen, tp->len + len + 1);
369 t = bp;
371 /* Original line, left of the split. */
372 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
373 memmove(bp, p, clen);
374 p += clen;
375 t += clen;
378 /* First line from the CB. */
379 memmove(t, tp->lb, tp->len);
380 t += 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
389 * the cached line.
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.
397 rp->lno = lno;
398 rp->cno = (t - bp) - 1;
400 if (clen > 0) {
401 memmove(t, p, clen);
402 t += clen;
404 if (file_sline(sp, ep, lno, bp, t - bp))
405 goto mem;
406 } else {
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
412 * floating around.
414 * Last part of original line; check for space, reset
415 * the pointer into the buffer.
417 ltp = cbp->textq.cqh_last;
418 len = t - bp;
419 ADD_SPACE(sp, bp, blen, ltp->len + clen);
420 t = bp + len;
422 /* Add in last part of the CB. */
423 memmove(t, ltp->lb, ltp->len);
424 if (clen)
425 memmove(t + ltp->len, p, clen);
426 clen += ltp->len;
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
433 * want to know.
435 * Output the line replacing the original line.
437 if (file_sline(sp, ep, lno, bp, t - bp))
438 goto mem;
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.)
445 rp->lno = lno;
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))
453 goto mem;
455 if (file_aline(sp, ep, 1, lno, t, clen)) {
456 mem: FREE_SPACE(sp, bp, blen);
457 return (1);
460 FREE_SPACE(sp, bp, blen);
462 /* Reporting... */
463 ret: sp->rptlines[L_PUT] += lno - cp->lno;
465 return (0);