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.18 2001/06/25 15:19:09 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:09 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
30 static void cb_rotate
__P((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 __P((SCR *, CHAR_T *, MARK *, MARK *, int));
67 cut(SCR
*sp
, CHAR_T
*namep
, MARK
*fm
, MARK
*tm
, int flags
)
72 int append
, copy_one
, copy_def
;
75 * If the user specified a buffer, put it there. (This may require
76 * a copy into the numeric buffers. We do the copy so that we don't
77 * have to reference count and so we don't have to deal with things
78 * like appends to buffers that are used multiple times.)
80 * Otherwise, if it's supposed to be put in a numeric buffer (usually
81 * a delete) put it there. The rules for putting things in numeric
82 * buffers were historically a little strange. There were three cases.
84 * 1: Some motions are always line mode motions, which means
85 * that the cut always goes into the numeric buffers.
86 * 2: Some motions aren't line mode motions, e.g. d10w, but
87 * can cross line boundaries. For these commands, if the
88 * cut crosses a line boundary, it goes into the numeric
89 * buffers. This includes most of the commands.
90 * 3: Some motions aren't line mode motions, e.g. d`<char>,
91 * but always go into the numeric buffers, regardless. This
92 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
94 * Otherwise, put it in the unnamed buffer.
96 append
= copy_one
= copy_def
= 0;
99 if (LF_ISSET(CUT_NUMREQ
) || LF_ISSET(CUT_NUMOPT
) &&
100 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
)) {
104 if ((append
= isupper(name
)) == 1) {
107 name
= tolower(name
);
109 namecb
: CBNAME(sp
, cbp
, name
);
110 } else if (LF_ISSET(CUT_NUMREQ
) || LF_ISSET(CUT_NUMOPT
) &&
111 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
)) {
116 cbp
= &sp
->wp
->dcb_store
;
120 * If this is a new buffer, create it and add it into the list.
121 * Otherwise, if it's not an append, free its current contents.
124 CALLOC_RET(sp
, cbp
, CB
*, 1, sizeof(CB
));
126 CIRCLEQ_INIT(&cbp
->textq
);
127 LIST_INSERT_HEAD(&sp
->wp
->cutq
, cbp
, q
);
128 } else if (!append
) {
129 text_lfree(&cbp
->textq
);
135 #define ENTIRE_LINE 0
136 /* In line mode, it's pretty easy, just cut the lines. */
137 if (LF_ISSET(CUT_LINEMODE
)) {
138 cbp
->flags
|= CB_LMODE
;
139 for (lno
= fm
->lno
; lno
<= tm
->lno
; ++lno
)
140 if (cut_line(sp
, lno
, 0, 0, cbp
))
144 * Get the first line. A length of 0 causes cut_line
145 * to cut from the MARK to the end of the line.
147 if (cut_line(sp
, fm
->lno
, fm
->cno
, fm
->lno
!= tm
->lno
?
148 ENTIRE_LINE
: (tm
->cno
- fm
->cno
) + 1, cbp
))
151 /* Get the intermediate lines. */
152 for (lno
= fm
->lno
; ++lno
< tm
->lno
;)
153 if (cut_line(sp
, lno
, 0, ENTIRE_LINE
, cbp
))
156 /* Get the last line. */
157 if (tm
->lno
!= fm
->lno
&&
158 cut_line(sp
, lno
, 0, tm
->cno
+ 1, cbp
))
162 append
= 0; /* Only append to the named buffer. */
163 sp
->wp
->dcbp
= cbp
; /* Repoint the default buffer on each pass. */
165 if (copy_one
) { /* Copy into numeric buffer 1. */
167 CBNAME(sp
, cbp
, name
);
171 if (copy_def
) { /* Copy into the default buffer. */
172 cbp
= &sp
->wp
->dcb_store
;
179 text_lfree(&cbp
->textq
);
187 * Rotate the numbered buffers up one.
195 for (cbp
= sp
->wp
->cutq
.lh_first
; cbp
!= NULL
; cbp
= cbp
->q
.le_next
)
225 if (del_cbp
!= NULL
) {
226 LIST_REMOVE(del_cbp
, q
);
227 text_lfree(&del_cbp
->textq
);
234 * Cut a portion of a single line.
236 * PUBLIC: int cut_line __P((SCR *, db_recno_t, size_t, size_t, CB *));
239 cut_line(SCR
*sp
, db_recno_t lno
, size_t fcno
, size_t clen
, CB
*cbp
)
246 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
249 /* Create a TEXT structure that can hold the entire line. */
250 if ((tp
= text_init(sp
, NULL
, 0, len
)) == NULL
)
254 * If the line isn't empty and it's not the entire line,
255 * copy the portion we want, and reset the TEXT length.
260 MEMCPYW(tp
->lb
, p
+ fcno
, clen
);
264 /* Append to the end of the cut buffer. */
265 CIRCLEQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
273 * Discard all cut buffers.
275 * PUBLIC: void cut_close __P((WIN *));
282 /* Free cut buffer list. */
283 while ((cbp
= wp
->cutq
.lh_first
) != NULL
) {
284 if (cbp
->textq
.cqh_first
!= (void *)&cbp
->textq
)
285 text_lfree(&cbp
->textq
);
290 /* Free default cut storage. */
291 cbp
= &wp
->dcb_store
;
292 if (cbp
->textq
.cqh_first
!= (void *)&cbp
->textq
)
293 text_lfree(&cbp
->textq
);
298 * Allocate a new TEXT structure.
300 * PUBLIC: TEXT *text_init __P((SCR *, const CHAR_T *, size_t, size_t));
303 text_init(SCR
*sp
, const CHAR_T
*p
, size_t len
, size_t total_len
)
307 CALLOC(sp
, tp
, TEXT
*, 1, sizeof(TEXT
));
310 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
311 if ((tp
->lb_len
= total_len
* sizeof(CHAR_T
)) != 0) {
312 MALLOC(sp
, tp
->lb
, CHAR_T
*, tp
->lb_len
* sizeof(CHAR_T
));
313 if (tp
->lb
== NULL
) {
317 if (p
!= NULL
&& len
!= 0)
318 MEMCPYW(tp
->lb
, p
, len
);
326 * Free a chain of text structures.
328 * PUBLIC: void text_lfree __P((TEXTH *));
331 text_lfree(TEXTH
*headp
)
335 while ((tp
= headp
->cqh_first
) != (void *)headp
) {
336 CIRCLEQ_REMOVE(headp
, tp
, q
);
343 * Free a text structure.
345 * PUBLIC: void text_free __P((TEXT *));