Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / cut.c
blob52c4d6aec1422019abd2fb0f77127a5d3e902dfd
1 /* $NetBSD: cut.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: cut.c,v 10.18 2001/06/25 15:19:09 skimo Exp (Berkeley) Date: 2001/06/25 15:19:09";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "common.h"
32 static void cb_rotate __P((SCR *));
35 * cut --
36 * Put a range of lines/columns into a TEXT buffer.
38 * There are two buffer areas, both found in the global structure. The first
39 * is the linked list of all the buffers the user has named, the second is the
40 * unnamed buffer storage. There is a pointer, too, which is the current
41 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
42 * depending on into what buffer the last text was cut. Logically, in both
43 * delete and yank operations, if the user names a buffer, the text is cut
44 * into it. If it's a delete of information on more than a single line, the
45 * contents of the numbered buffers are rotated up one, the contents of the
46 * buffer named '9' are discarded, and the text is cut into the buffer named
47 * '1'. The text is always cut into the unnamed buffer.
49 * In all cases, upper-case buffer names are the same as lower-case names,
50 * with the exception that they cause the buffer to be appended to instead
51 * of replaced. Note, however, that if text is appended to a buffer, the
52 * default buffer only contains the appended text, not the entire contents
53 * of the buffer.
55 * !!!
56 * The contents of the default buffer would disappear after most operations
57 * in historic vi. It's unclear that this is useful, so we don't bother.
59 * When users explicitly cut text into the numeric buffers, historic vi became
60 * genuinely strange. I've never been able to figure out what was supposed to
61 * happen. It behaved differently if you deleted text than if you yanked text,
62 * and, in the latter case, the text was appended to the buffer instead of
63 * replacing the contents. Hopefully it's not worth getting right, and here
64 * we just treat the numeric buffers like any other named buffer.
66 * PUBLIC: int cut __P((SCR *, CHAR_T *, MARK *, MARK *, int));
68 int
69 cut(SCR *sp, CHAR_T *namep, MARK *fm, MARK *tm, int flags)
71 CB *cbp;
72 CHAR_T name = '\0';
73 db_recno_t lno;
74 int append, copy_one, copy_def;
77 * If the user specified a buffer, put it there. (This may require
78 * a copy into the numeric buffers. We do the copy so that we don't
79 * have to reference count and so we don't have to deal with things
80 * like appends to buffers that are used multiple times.)
82 * Otherwise, if it's supposed to be put in a numeric buffer (usually
83 * a delete) put it there. The rules for putting things in numeric
84 * buffers were historically a little strange. There were three cases.
86 * 1: Some motions are always line mode motions, which means
87 * that the cut always goes into the numeric buffers.
88 * 2: Some motions aren't line mode motions, e.g. d10w, but
89 * can cross line boundaries. For these commands, if the
90 * cut crosses a line boundary, it goes into the numeric
91 * buffers. This includes most of the commands.
92 * 3: Some motions aren't line mode motions, e.g. d`<char>,
93 * but always go into the numeric buffers, regardless. This
94 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
96 * Otherwise, put it in the unnamed buffer.
98 append = copy_one = copy_def = 0;
99 if (namep != NULL) {
100 name = *namep;
101 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
102 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
103 copy_one = 1;
104 cb_rotate(sp);
106 if ((append = ISUPPER(name)) == 1) {
107 if (!copy_one)
108 copy_def = 1;
109 name = TOLOWER(name);
111 namecb: CBNAME(sp, cbp, name);
112 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
113 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
114 name = '1';
115 cb_rotate(sp);
116 goto namecb;
117 } else
118 cbp = &sp->wp->dcb_store;
120 copyloop:
122 * If this is a new buffer, create it and add it into the list.
123 * Otherwise, if it's not an append, free its current contents.
125 if (cbp == NULL) {
126 CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
127 cbp->name = name;
128 CIRCLEQ_INIT(&cbp->textq);
129 LIST_INSERT_HEAD(&sp->wp->cutq, cbp, q);
130 } else if (!append) {
131 text_lfree(&cbp->textq);
132 cbp->len = 0;
133 cbp->flags = 0;
137 #define ENTIRE_LINE 0
138 /* In line mode, it's pretty easy, just cut the lines. */
139 if (LF_ISSET(CUT_LINEMODE)) {
140 cbp->flags |= CB_LMODE;
141 for (lno = fm->lno; lno <= tm->lno; ++lno)
142 if (cut_line(sp, lno, 0, 0, cbp))
143 goto cut_line_err;
144 } else {
146 * Get the first line. A length of 0 causes cut_line
147 * to cut from the MARK to the end of the line.
149 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
150 ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
151 goto cut_line_err;
153 /* Get the intermediate lines. */
154 for (lno = fm->lno; ++lno < tm->lno;)
155 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
156 goto cut_line_err;
158 /* Get the last line. */
159 if (tm->lno != fm->lno &&
160 cut_line(sp, lno, 0, tm->cno + 1, cbp))
161 goto cut_line_err;
164 append = 0; /* Only append to the named buffer. */
165 sp->wp->dcbp = cbp; /* Repoint the default buffer on each pass. */
167 if (copy_one) { /* Copy into numeric buffer 1. */
168 name = '1';
169 CBNAME(sp, cbp, name);
170 copy_one = 0;
171 goto copyloop;
173 if (copy_def) { /* Copy into the default buffer. */
174 cbp = &sp->wp->dcb_store;
175 copy_def = 0;
176 goto copyloop;
178 return (0);
180 cut_line_err:
181 text_lfree(&cbp->textq);
182 cbp->len = 0;
183 cbp->flags = 0;
184 return (1);
188 * cb_rotate --
189 * Rotate the numbered buffers up one.
191 static void
192 cb_rotate(SCR *sp)
194 CB *cbp, *del_cbp;
196 del_cbp = NULL;
197 for (cbp = sp->wp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
198 switch(cbp->name) {
199 case '1':
200 cbp->name = '2';
201 break;
202 case '2':
203 cbp->name = '3';
204 break;
205 case '3':
206 cbp->name = '4';
207 break;
208 case '4':
209 cbp->name = '5';
210 break;
211 case '5':
212 cbp->name = '6';
213 break;
214 case '6':
215 cbp->name = '7';
216 break;
217 case '7':
218 cbp->name = '8';
219 break;
220 case '8':
221 cbp->name = '9';
222 break;
223 case '9':
224 del_cbp = cbp;
225 break;
227 if (del_cbp != NULL) {
228 LIST_REMOVE(del_cbp, q);
229 text_lfree(&del_cbp->textq);
230 free(del_cbp);
235 * cut_line --
236 * Cut a portion of a single line.
238 * PUBLIC: int cut_line __P((SCR *, db_recno_t, size_t, size_t, CB *));
241 cut_line(SCR *sp, db_recno_t lno, size_t fcno, size_t clen, CB *cbp)
243 TEXT *tp;
244 size_t len;
245 CHAR_T *p;
247 /* Get the line. */
248 if (db_get(sp, lno, DBG_FATAL, &p, &len))
249 return (1);
251 /* Create a TEXT structure that can hold the entire line. */
252 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
253 return (1);
256 * If the line isn't empty and it's not the entire line,
257 * copy the portion we want, and reset the TEXT length.
259 if (len != 0) {
260 if (clen == 0)
261 clen = len - fcno;
262 MEMCPYW(tp->lb, p + fcno, clen);
263 tp->len = clen;
266 /* Append to the end of the cut buffer. */
267 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
268 cbp->len += tp->len;
270 return (0);
274 * cut_close --
275 * Discard all cut buffers.
277 * PUBLIC: void cut_close __P((WIN *));
279 void
280 cut_close(WIN *wp)
282 CB *cbp;
284 /* Free cut buffer list. */
285 while ((cbp = wp->cutq.lh_first) != NULL) {
286 if (cbp->textq.cqh_first != (void *)&cbp->textq)
287 text_lfree(&cbp->textq);
288 LIST_REMOVE(cbp, q);
289 free(cbp);
292 /* Free default cut storage. */
293 cbp = &wp->dcb_store;
294 if (cbp->textq.cqh_first != (void *)&cbp->textq)
295 text_lfree(&cbp->textq);
299 * text_init --
300 * Allocate a new TEXT structure.
302 * PUBLIC: TEXT *text_init __P((SCR *, const CHAR_T *, size_t, size_t));
304 TEXT *
305 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
307 TEXT *tp;
309 CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
310 if (tp == NULL)
311 return (NULL);
312 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
313 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
314 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len * sizeof(CHAR_T));
315 if (tp->lb == NULL) {
316 free(tp);
317 return (NULL);
319 if (p != NULL && len != 0)
320 MEMCPYW(tp->lb, p, len);
322 tp->len = len;
323 return (tp);
327 * text_lfree --
328 * Free a chain of text structures.
330 * PUBLIC: void text_lfree __P((TEXTH *));
332 void
333 text_lfree(TEXTH *headp)
335 TEXT *tp;
337 while ((tp = headp->cqh_first) != (void *)headp) {
338 CIRCLEQ_REMOVE(headp, tp, q);
339 text_free(tp);
344 * text_free --
345 * Free a text structure.
347 * PUBLIC: void text_free __P((TEXT *));
349 void
350 text_free(TEXT *tp)
352 if (tp->lb != NULL)
353 free(tp->lb);
354 free(tp);