1 /* $NetBSD: cut.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: cut.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
36 static void cb_rotate
__P((SCR
*));
40 * Put a range of lines/columns into a TEXT buffer.
42 * There are two buffer areas, both found in the global structure. The first
43 * is the linked list of all the buffers the user has named, the second is the
44 * unnamed buffer storage. There is a pointer, too, which is the current
45 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
46 * depending on into what buffer the last text was cut. Logically, in both
47 * delete and yank operations, if the user names a buffer, the text is cut
48 * into it. If it's a delete of information on more than a single line, the
49 * contents of the numbered buffers are rotated up one, the contents of the
50 * buffer named '9' are discarded, and the text is cut into the buffer named
51 * '1'. The text is always cut into the unnamed buffer.
53 * In all cases, upper-case buffer names are the same as lower-case names,
54 * with the exception that they cause the buffer to be appended to instead
55 * of replaced. Note, however, that if text is appended to a buffer, the
56 * default buffer only contains the appended text, not the entire contents
60 * The contents of the default buffer would disappear after most operations
61 * in historic vi. It's unclear that this is useful, so we don't bother.
63 * When users explicitly cut text into the numeric buffers, historic vi became
64 * genuinely strange. I've never been able to figure out what was supposed to
65 * happen. It behaved differently if you deleted text than if you yanked text,
66 * and, in the latter case, the text was appended to the buffer instead of
67 * replacing the contents. Hopefully it's not worth getting right, and here
68 * we just treat the numeric buffers like any other named buffer.
70 * PUBLIC: int cut __P((SCR *, ARG_CHAR_T *, MARK *, MARK *, int));
73 cut(SCR
*sp
, ARG_CHAR_T
*namep
, MARK
*fm
, MARK
*tm
, int flags
)
76 ARG_CHAR_T name
= '\0';
78 int append
, copy_one
, copy_def
;
81 * If the user specified a buffer, put it there. (This may require
82 * a copy into the numeric buffers. We do the copy so that we don't
83 * have to reference count and so we don't have to deal with things
84 * like appends to buffers that are used multiple times.)
86 * Otherwise, if it's supposed to be put in a numeric buffer (usually
87 * a delete) put it there. The rules for putting things in numeric
88 * buffers were historically a little strange. There were three cases.
90 * 1: Some motions are always line mode motions, which means
91 * that the cut always goes into the numeric buffers.
92 * 2: Some motions aren't line mode motions, e.g. d10w, but
93 * can cross line boundaries. For these commands, if the
94 * cut crosses a line boundary, it goes into the numeric
95 * buffers. This includes most of the commands.
96 * 3: Some motions aren't line mode motions, e.g. d`<char>,
97 * but always go into the numeric buffers, regardless. This
98 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
100 * Otherwise, put it in the unnamed buffer.
102 append
= copy_one
= copy_def
= 0;
105 if (LF_ISSET(CUT_NUMREQ
) || (LF_ISSET(CUT_NUMOPT
) &&
106 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
))) {
110 if ((append
= ISUPPER(name
))) {
113 name
= TOLOWER(name
);
115 namecb
: CBNAME(sp
, cbp
, name
);
116 } else if (LF_ISSET(CUT_NUMREQ
) || (LF_ISSET(CUT_NUMOPT
) &&
117 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
))) {
122 cbp
= &sp
->wp
->dcb_store
;
126 * If this is a new buffer, create it and add it into the list.
127 * Otherwise, if it's not an append, free its current contents.
130 CALLOC_RET(sp
, cbp
, CB
*, 1, sizeof(CB
));
132 TAILQ_INIT(&cbp
->textq
);
133 LIST_INSERT_HEAD(&sp
->wp
->cutq
, cbp
, q
);
134 } else if (!append
) {
135 text_lfree(&cbp
->textq
);
141 /* In line mode, it's pretty easy, just cut the lines. */
142 if (LF_ISSET(CUT_LINEMODE
)) {
143 cbp
->flags
|= CB_LMODE
;
144 for (lno
= fm
->lno
; lno
<= tm
->lno
; ++lno
)
145 if (cut_line(sp
, lno
, 0, ENTIRE_LINE
, cbp
))
149 * Get the first line. A length of ENTIRE_LINE causes cut_line
150 * to cut from the MARK to the end of the line.
152 if (cut_line(sp
, fm
->lno
, fm
->cno
, fm
->lno
!= tm
->lno
?
153 ENTIRE_LINE
: (tm
->cno
- fm
->cno
) + 1, cbp
))
156 /* Get the intermediate lines. */
157 for (lno
= fm
->lno
; ++lno
< tm
->lno
;)
158 if (cut_line(sp
, lno
, 0, ENTIRE_LINE
, cbp
))
161 /* Get the last line. */
162 if (tm
->lno
!= fm
->lno
&&
163 cut_line(sp
, lno
, 0, tm
->cno
+ 1, cbp
))
167 append
= 0; /* Only append to the named buffer. */
168 sp
->wp
->dcbp
= cbp
; /* Repoint the default buffer on each pass. */
170 if (copy_one
) { /* Copy into numeric buffer 1. */
172 CBNAME(sp
, cbp
, name
);
176 if (copy_def
) { /* Copy into the default buffer. */
177 cbp
= &sp
->wp
->dcb_store
;
184 text_lfree(&cbp
->textq
);
192 * Rotate the numbered buffers up one.
200 LIST_FOREACH(cbp
, &sp
->wp
->cutq
, q
)
230 if (del_cbp
!= NULL
) {
231 LIST_REMOVE(del_cbp
, q
);
232 text_lfree(&del_cbp
->textq
);
239 * Cut a portion of a single line.
241 * PUBLIC: int cut_line __P((SCR *, db_recno_t, size_t, size_t, CB *));
244 cut_line(SCR
*sp
, db_recno_t lno
, size_t fcno
, size_t clen
, CB
*cbp
)
251 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
254 /* Create a TEXT structure that can hold the entire line. */
255 if ((tp
= text_init(sp
, NULL
, 0, len
)) == NULL
)
259 * If the line isn't empty and it's not the entire line,
260 * copy the portion we want, and reset the TEXT length.
263 if (clen
== ENTIRE_LINE
)
265 MEMCPYW(tp
->lb
, p
+ fcno
, clen
);
269 /* Append to the end of the cut buffer. */
270 TAILQ_INSERT_TAIL(&cbp
->textq
, tp
, q
);
278 * Discard all cut buffers.
280 * PUBLIC: void cut_close __P((WIN *));
287 /* Free cut buffer list. */
288 while ((cbp
= LIST_FIRST(&wp
->cutq
)) != NULL
) {
289 if (!TAILQ_EMPTY(&cbp
->textq
))
290 text_lfree(&cbp
->textq
);
295 /* Free default cut storage. */
296 cbp
= &wp
->dcb_store
;
297 if (!TAILQ_EMPTY(&cbp
->textq
))
298 text_lfree(&cbp
->textq
);
303 * Allocate a new TEXT structure.
305 * PUBLIC: TEXT *text_init __P((SCR *, const CHAR_T *, size_t, size_t));
308 text_init(SCR
*sp
, const CHAR_T
*p
, size_t len
, size_t total_len
)
312 CALLOC(sp
, tp
, TEXT
*, 1, sizeof(TEXT
));
315 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
316 if ((tp
->lb_len
= total_len
* sizeof(CHAR_T
)) != 0) {
317 MALLOC(sp
, tp
->lb
, CHAR_T
*, tp
->lb_len
* sizeof(CHAR_T
));
318 if (tp
->lb
== NULL
) {
322 if (p
!= NULL
&& len
!= 0)
323 MEMCPYW(tp
->lb
, p
, len
);
331 * Free a chain of text structures.
333 * PUBLIC: void text_lfree __P((TEXTH *));
336 text_lfree(TEXTH
*headp
)
340 while ((tp
= TAILQ_FIRST(headp
)) != NULL
) {
341 TAILQ_REMOVE(headp
, tp
, q
);
348 * Free a text structure.
350 * PUBLIC: void text_free __P((TEXT *));