1 /* $NetBSD: cut.c,v 1.3 2013/11/25 22:43:46 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
31 static void cb_rotate
__P((SCR
*));
35 * Put a range of lines/columns into a TEXT buffer.
37 * There are two buffer areas, both found in the global structure. The first
38 * is the linked list of all the buffers the user has named, the second is the
39 * unnamed buffer storage. There is a pointer, too, which is the current
40 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
41 * depending on into what buffer the last text was cut. Logically, in both
42 * delete and yank operations, if the user names a buffer, the text is cut
43 * into it. If it's a delete of information on more than a single line, the
44 * contents of the numbered buffers are rotated up one, the contents of the
45 * buffer named '9' are discarded, and the text is cut into the buffer named
46 * '1'. The text is always cut into the unnamed buffer.
48 * In all cases, upper-case buffer names are the same as lower-case names,
49 * with the exception that they cause the buffer to be appended to instead
50 * of replaced. Note, however, that if text is appended to a buffer, the
51 * default buffer only contains the appended text, not the entire contents
55 * The contents of the default buffer would disappear after most operations
56 * in historic vi. It's unclear that this is useful, so we don't bother.
58 * When users explicitly cut text into the numeric buffers, historic vi became
59 * genuinely strange. I've never been able to figure out what was supposed to
60 * happen. It behaved differently if you deleted text than if you yanked text,
61 * and, in the latter case, the text was appended to the buffer instead of
62 * replacing the contents. Hopefully it's not worth getting right, and here
63 * we just treat the numeric buffers like any other named buffer.
65 * PUBLIC: int cut __P((SCR *, ARG_CHAR_T *, MARK *, MARK *, int));
68 cut(SCR
*sp
, ARG_CHAR_T
*namep
, MARK
*fm
, MARK
*tm
, int flags
)
71 ARG_CHAR_T name
= '\0';
73 int append
, copy_one
, copy_def
;
76 * If the user specified a buffer, put it there. (This may require
77 * a copy into the numeric buffers. We do the copy so that we don't
78 * have to reference count and so we don't have to deal with things
79 * like appends to buffers that are used multiple times.)
81 * Otherwise, if it's supposed to be put in a numeric buffer (usually
82 * a delete) put it there. The rules for putting things in numeric
83 * buffers were historically a little strange. There were three cases.
85 * 1: Some motions are always line mode motions, which means
86 * that the cut always goes into the numeric buffers.
87 * 2: Some motions aren't line mode motions, e.g. d10w, but
88 * can cross line boundaries. For these commands, if the
89 * cut crosses a line boundary, it goes into the numeric
90 * buffers. This includes most of the commands.
91 * 3: Some motions aren't line mode motions, e.g. d`<char>,
92 * but always go into the numeric buffers, regardless. This
93 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
95 * Otherwise, put it in the unnamed buffer.
97 append
= copy_one
= copy_def
= 0;
100 if (LF_ISSET(CUT_NUMREQ
) || (LF_ISSET(CUT_NUMOPT
) &&
101 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
))) {
105 if ((append
= ISUPPER(name
))) {
108 name
= TOLOWER(name
);
110 namecb
: CBNAME(sp
, cbp
, name
);
111 } else if (LF_ISSET(CUT_NUMREQ
) || (LF_ISSET(CUT_NUMOPT
) &&
112 (LF_ISSET(CUT_LINEMODE
) || fm
->lno
!= tm
->lno
))) {
117 cbp
= &sp
->wp
->dcb_store
;
121 * If this is a new buffer, create it and add it into the list.
122 * Otherwise, if it's not an append, free its current contents.
125 CALLOC_RET(sp
, cbp
, CB
*, 1, sizeof(CB
));
127 TAILQ_INIT(&cbp
->textq
);
128 LIST_INSERT_HEAD(&sp
->wp
->cutq
, cbp
, q
);
129 } else if (!append
) {
130 text_lfree(&cbp
->textq
);
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, ENTIRE_LINE
, cbp
))
144 * Get the first line. A length of ENTIRE_LINE 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 LIST_FOREACH(cbp
, &sp
->wp
->cutq
, q
)
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.
258 if (clen
== ENTIRE_LINE
)
260 MEMCPYW(tp
->lb
, p
+ fcno
, clen
);
264 /* Append to the end of the cut buffer. */
265 TAILQ_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
= LIST_FIRST(&wp
->cutq
)) != NULL
) {
284 if (!TAILQ_EMPTY(&cbp
->textq
))
285 text_lfree(&cbp
->textq
);
290 /* Free default cut storage. */
291 cbp
= &wp
->dcb_store
;
292 if (!TAILQ_EMPTY(&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
= TAILQ_FIRST(headp
)) != NULL
) {
336 TAILQ_REMOVE(headp
, tp
, q
);
343 * Free a text structure.
345 * PUBLIC: void text_free __P((TEXT *));