2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid[] = "$Id: cut.c,v 10.12 2012/02/11 15:52:33 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
30 static void cb_rotate(SCR *);
34 * Put a range of lines/columns into a TEXT buffer.
36 * There are two buffer areas, both found in the global structure. The first
37 * is the linked list of all the buffers the user has named, the second is the
38 * unnamed buffer storage. There is a pointer, too, which is the current
39 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
40 * depending on into what buffer the last text was cut. Logically, in both
41 * delete and yank operations, if the user names a buffer, the text is cut
42 * into it. If it's a delete of information on more than a single line, the
43 * contents of the numbered buffers are rotated up one, the contents of the
44 * buffer named '9' are discarded, and the text is cut into the buffer named
45 * '1'. The text is always cut into the unnamed buffer.
47 * In all cases, upper-case buffer names are the same as lower-case names,
48 * with the exception that they cause the buffer to be appended to instead
49 * of replaced. Note, however, that if text is appended to a buffer, the
50 * default buffer only contains the appended text, not the entire contents
54 * The contents of the default buffer would disappear after most operations
55 * in historic vi. It's unclear that this is useful, so we don't bother.
57 * When users explicitly cut text into the numeric buffers, historic vi became
58 * genuinely strange. I've never been able to figure out what was supposed to
59 * happen. It behaved differently if you deleted text than if you yanked text,
60 * and, in the latter case, the text was appended to the buffer instead of
61 * replacing the contents. Hopefully it's not worth getting right, and here
62 * we just treat the numeric buffers like any other named buffer.
64 * PUBLIC: int cut(SCR *, CHAR_T *, MARK *, MARK *, int);
77 int append, copy_one, copy_def;
80 * If the user specified a buffer, put it there. (This may require
81 * a copy into the numeric buffers. We do the copy so that we don't
82 * have to reference count and so we don't have to deal with things
83 * like appends to buffers that are used multiple times.)
85 * Otherwise, if it's supposed to be put in a numeric buffer (usually
86 * a delete) put it there. The rules for putting things in numeric
87 * buffers were historically a little strange. There were three cases.
89 * 1: Some motions are always line mode motions, which means
90 * that the cut always goes into the numeric buffers.
91 * 2: Some motions aren't line mode motions, e.g. d10w, but
92 * can cross line boundaries. For these commands, if the
93 * cut crosses a line boundary, it goes into the numeric
94 * buffers. This includes most of the commands.
95 * 3: Some motions aren't line mode motions, e.g. d`<char>,
96 * but always go into the numeric buffers, regardless. This
97 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
99 * Otherwise, put it in the unnamed buffer.
101 append = copy_one = copy_def = 0;
104 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
105 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
109 if ((append = isupper(name))) {
112 name = tolower(name);
114 namecb: CBNAME(sp, cbp, name);
115 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
116 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
121 cbp = &sp->gp->dcb_store;
125 * If this is a new buffer, create it and add it into the list.
126 * Otherwise, if it's not an append, free its current contents.
129 CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
131 TAILQ_INIT(cbp->textq);
132 SLIST_INSERT_HEAD(sp->gp->cutq, cbp, q);
133 } else if (!append) {
134 text_lfree(cbp->textq);
140 /* In line mode, it's pretty easy, just cut the lines. */
141 if (LF_ISSET(CUT_LINEMODE)) {
142 cbp->flags |= CB_LMODE;
143 for (lno = fm->lno; lno <= tm->lno; ++lno)
144 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
148 * Get the first line. A length of ENTIRE_LINE causes
149 * cut_line to cut from the MARK to the end of the line.
151 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
152 ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
155 /* Get the intermediate lines. */
156 for (lno = fm->lno; ++lno < tm->lno;)
157 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
160 /* Get the last line. */
161 if (tm->lno != fm->lno &&
162 cut_line(sp, lno, 0, tm->cno + 1, cbp))
166 append = 0; /* Only append to the named buffer. */
167 sp->gp->dcbp = cbp; /* Repoint the default buffer on each pass. */
169 if (copy_one) { /* Copy into numeric buffer 1. */
171 CBNAME(sp, cbp, name);
175 if (copy_def) { /* Copy into the default buffer. */
176 cbp = &sp->gp->dcb_store;
183 text_lfree(cbp->textq);
191 * Rotate the numbered buffers up one.
196 CB *cbp, *del_cbp = NULL, *pre_cbp = NULL;
198 SLIST_FOREACH(cbp, sp->gp->cutq, q) {
200 case '1': case '2': case '3':
201 case '4': case '5': case '6':
206 if (cbp == SLIST_FIRST(sp->gp->cutq))
207 SLIST_REMOVE_HEAD(sp->gp->cutq, q);
209 SLIST_REMOVE_AFTER(pre_cbp, q);
215 if (del_cbp != NULL) {
216 text_lfree(del_cbp->textq);
223 * Cut a portion of a single line.
225 * PUBLIC: int cut_line(SCR *, recno_t, size_t, size_t, CB *);
240 if (db_get(sp, lno, DBG_FATAL, &p, &len))
243 /* Create a TEXT structure that can hold the entire line. */
244 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
248 * If the line isn't empty and it's not the entire line,
249 * copy the portion we want, and reset the TEXT length.
252 if (clen == ENTIRE_LINE)
254 MEMCPY(tp->lb, p + fcno, clen);
258 /* Append to the end of the cut buffer. */
259 TAILQ_INSERT_TAIL(cbp->textq, tp, q);
267 * Discard all cut buffers.
269 * PUBLIC: void cut_close(GS *);
276 /* Free cut buffer list. */
277 while ((cbp = SLIST_FIRST(gp->cutq)) != NULL) {
278 if (!TAILQ_EMPTY(cbp->textq))
279 text_lfree(cbp->textq);
280 SLIST_REMOVE_HEAD(gp->cutq, q);
284 /* Free default cut storage. */
285 cbp = &gp->dcb_store;
286 if (!TAILQ_EMPTY(cbp->textq))
287 text_lfree(cbp->textq);
292 * Allocate a new TEXT structure.
294 * PUBLIC: TEXT *text_init(SCR *, const CHAR_T *, size_t, size_t);
305 CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
308 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
309 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
310 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
311 if (tp->lb == NULL) {
315 if (p != NULL && len != 0)
316 MEMCPY(tp->lb, p, len);
324 * Free a chain of text structures.
326 * PUBLIC: void text_lfree(TEXTH *);
329 text_lfree(TEXTH *headp)
333 while ((tp = TAILQ_FIRST(headp)) != NULL) {
334 TAILQ_REMOVE(headp, tp, q);
341 * Free a text structure.
343 * PUBLIC: void text_free(TEXT *);