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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
27 static void cb_rotate(SCR *);
31 * Put a range of lines/columns into a TEXT buffer.
33 * There are two buffer areas, both found in the global structure. The first
34 * is the linked list of all the buffers the user has named, the second is the
35 * unnamed buffer storage. There is a pointer, too, which is the current
36 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
37 * depending on into what buffer the last text was cut. Logically, in both
38 * delete and yank operations, if the user names a buffer, the text is cut
39 * into it. If it's a delete of information on more than a single line, the
40 * contents of the numbered buffers are rotated up one, the contents of the
41 * buffer named '9' are discarded, and the text is cut into the buffer named
42 * '1'. The text is always cut into the unnamed buffer.
44 * In all cases, upper-case buffer names are the same as lower-case names,
45 * with the exception that they cause the buffer to be appended to instead
46 * of replaced. Note, however, that if text is appended to a buffer, the
47 * default buffer only contains the appended text, not the entire contents
51 * The contents of the default buffer would disappear after most operations
52 * in historic vi. It's unclear that this is useful, so we don't bother.
54 * When users explicitly cut text into the numeric buffers, historic vi became
55 * genuinely strange. I've never been able to figure out what was supposed to
56 * happen. It behaved differently if you deleted text than if you yanked text,
57 * and, in the latter case, the text was appended to the buffer instead of
58 * replacing the contents. Hopefully it's not worth getting right, and here
59 * we just treat the numeric buffers like any other named buffer.
61 * PUBLIC: int cut(SCR *, CHAR_T *, MARK *, MARK *, int);
64 cut(SCR *sp, CHAR_T *namep, MARK *fm, MARK *tm, int flags)
69 int append, copy_one, copy_def;
72 * If the user specified a buffer, put it there. (This may require
73 * a copy into the numeric buffers. We do the copy so that we don't
74 * have to reference count and so we don't have to deal with things
75 * like appends to buffers that are used multiple times.)
77 * Otherwise, if it's supposed to be put in a numeric buffer (usually
78 * a delete) put it there. The rules for putting things in numeric
79 * buffers were historically a little strange. There were three cases.
81 * 1: Some motions are always line mode motions, which means
82 * that the cut always goes into the numeric buffers.
83 * 2: Some motions aren't line mode motions, e.g. d10w, but
84 * can cross line boundaries. For these commands, if the
85 * cut crosses a line boundary, it goes into the numeric
86 * buffers. This includes most of the commands.
87 * 3: Some motions aren't line mode motions, e.g. d`<char>,
88 * but always go into the numeric buffers, regardless. This
89 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
91 * Otherwise, put it in the unnamed buffer.
93 append = copy_one = copy_def = 0;
96 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
97 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
101 if ((append = isupper(name))) {
104 name = tolower(name);
106 namecb: CBNAME(sp, cbp, name);
107 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
108 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
113 cbp = &sp->gp->dcb_store;
117 * If this is a new buffer, create it and add it into the list.
118 * Otherwise, if it's not an append, free its current contents.
121 CALLOC_RET(sp, cbp, 1, sizeof(CB));
123 TAILQ_INIT(cbp->textq);
124 SLIST_INSERT_HEAD(sp->gp->cutq, cbp, q);
125 } else if (!append) {
126 text_lfree(cbp->textq);
132 /* In line mode, it's pretty easy, just cut the lines. */
133 if (LF_ISSET(CUT_LINEMODE)) {
134 cbp->flags |= CB_LMODE;
135 for (lno = fm->lno; lno <= tm->lno; ++lno)
136 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
140 * Get the first line. A length of ENTIRE_LINE causes
141 * cut_line to cut from the MARK to the end of the line.
143 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
144 ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
147 /* Get the intermediate lines. */
148 for (lno = fm->lno; ++lno < tm->lno;)
149 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
152 /* Get the last line. */
153 if (tm->lno != fm->lno &&
154 cut_line(sp, lno, 0, tm->cno + 1, cbp))
158 append = 0; /* Only append to the named buffer. */
159 sp->gp->dcbp = cbp; /* Repoint the default buffer on each pass. */
161 if (copy_one) { /* Copy into numeric buffer 1. */
163 CBNAME(sp, cbp, name);
167 if (copy_def) { /* Copy into the default buffer. */
168 cbp = &sp->gp->dcb_store;
175 text_lfree(cbp->textq);
183 * Rotate the numbered buffers up one.
188 CB *cbp, *del_cbp = NULL, *pre_cbp = NULL;
190 SLIST_FOREACH(cbp, sp->gp->cutq, q) {
192 case '1': case '2': case '3':
193 case '4': case '5': case '6':
198 if (cbp == SLIST_FIRST(sp->gp->cutq))
199 SLIST_REMOVE_HEAD(sp->gp->cutq, q);
201 SLIST_REMOVE_AFTER(pre_cbp, q);
207 if (del_cbp != NULL) {
208 text_lfree(del_cbp->textq);
215 * Cut a portion of a single line.
217 * PUBLIC: int cut_line(SCR *, recno_t, size_t, size_t, CB *);
220 cut_line(SCR *sp, recno_t lno, size_t fcno, size_t clen, CB *cbp)
227 if (db_get(sp, lno, DBG_FATAL, &p, &len))
230 /* Create a TEXT structure that can hold the entire line. */
231 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
235 * If the line isn't empty and it's not the entire line,
236 * copy the portion we want, and reset the TEXT length.
239 if (clen == ENTIRE_LINE)
241 MEMCPY(tp->lb, p + fcno, clen);
245 /* Append to the end of the cut buffer. */
246 TAILQ_INSERT_TAIL(cbp->textq, tp, q);
254 * Discard all cut buffers.
256 * PUBLIC: void cut_close(GS *);
263 /* Free cut buffer list. */
264 while ((cbp = SLIST_FIRST(gp->cutq)) != NULL) {
265 if (!TAILQ_EMPTY(cbp->textq))
266 text_lfree(cbp->textq);
267 SLIST_REMOVE_HEAD(gp->cutq, q);
271 /* Free default cut storage. */
272 cbp = &gp->dcb_store;
273 if (!TAILQ_EMPTY(cbp->textq))
274 text_lfree(cbp->textq);
279 * Allocate a new TEXT structure.
281 * PUBLIC: TEXT *text_init(SCR *, const CHAR_T *, size_t, size_t);
284 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
288 CALLOC(sp, tp, 1, sizeof(TEXT));
291 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
292 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
293 MALLOC(sp, tp->lb, tp->lb_len);
294 if (tp->lb == NULL) {
298 if (p != NULL && len != 0)
299 MEMCPY(tp->lb, p, len);
307 * Free a chain of text structures.
309 * PUBLIC: void text_lfree(TEXTH *);
312 text_lfree(TEXTH *headp)
316 while ((tp = TAILQ_FIRST(headp)) != NULL) {
317 TAILQ_REMOVE(headp, tp, q);
324 * Free a text structure.
326 * PUBLIC: void text_free(TEXT *);