move configure.in to configure.ac
[nvi.git] / ex / ex_move.c
blob324853ef0faa07fafa9f3a6e4a125dffd20aae3b
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_move.c,v 10.15 2001/06/25 15:19:17 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:17 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "../common/common.h"
28 * ex_copy -- :[line [,line]] co[py] line [flags]
29 * Copy selected lines.
31 * PUBLIC: int ex_copy __P((SCR *, EXCMD *));
33 int
34 ex_copy(SCR *sp, EXCMD *cmdp)
36 CB cb;
37 MARK fm1, fm2, m, tm;
38 db_recno_t cnt;
39 int rval;
41 rval = 0;
43 NEEDFILE(sp, cmdp);
46 * It's possible to copy things into the area that's being
47 * copied, e.g. "2,5copy3" is legitimate. Save the text to
48 * a cut buffer.
50 fm1 = cmdp->addr1;
51 fm2 = cmdp->addr2;
52 memset(&cb, 0, sizeof(cb));
53 CIRCLEQ_INIT(&cb.textq);
54 for (cnt = fm1.lno; cnt <= fm2.lno; ++cnt)
55 if (cut_line(sp, cnt, 0, 0, &cb)) {
56 rval = 1;
57 goto err;
59 cb.flags |= CB_LMODE;
61 /* Put the text into place. */
62 tm.lno = cmdp->lineno;
63 tm.cno = 0;
64 if (put(sp, &cb, NULL, &tm, &m, 1))
65 rval = 1;
66 else {
68 * Copy puts the cursor on the last line copied. The cursor
69 * returned by the put routine is the first line put, not the
70 * last, because that's the historic semantic of vi.
72 cnt = (fm2.lno - fm1.lno) + 1;
73 sp->lno = m.lno + (cnt - 1);
74 sp->cno = 0;
76 err: text_lfree(&cb.textq);
77 return (rval);
81 * ex_move -- :[line [,line]] mo[ve] line
82 * Move selected lines.
84 * PUBLIC: int ex_move __P((SCR *, EXCMD *));
86 int
87 ex_move(SCR *sp, EXCMD *cmdp)
89 LMARK *lmp;
90 MARK fm1, fm2;
91 db_recno_t cnt, diff, fl, tl, mfl, mtl;
92 size_t blen, len;
93 int mark_reset;
94 CHAR_T *bp;
95 CHAR_T *p;
97 NEEDFILE(sp, cmdp);
100 * It's not possible to move things into the area that's being
101 * moved.
103 fm1 = cmdp->addr1;
104 fm2 = cmdp->addr2;
105 if (cmdp->lineno >= fm1.lno && cmdp->lineno <= fm2.lno) {
106 msgq(sp, M_ERR, "139|Destination line is inside move range");
107 return (1);
111 * Log the positions of any marks in the to-be-deleted lines. This
112 * has to work with the logging code. What happens is that we log
113 * the old mark positions, make the changes, then log the new mark
114 * positions. Then the marks end up in the right positions no matter
115 * which way the log is traversed.
117 * XXX
118 * Reset the MARK_USERSET flag so that the log can undo the mark.
119 * This isn't very clean, and should probably be fixed.
121 fl = fm1.lno;
122 tl = cmdp->lineno;
124 /* Log the old positions of the marks. */
125 mark_reset = 0;
126 for (lmp = sp->ep->marks.lh_first; lmp != NULL; lmp = lmp->q.le_next)
127 if (lmp->name != ABSMARK1 &&
128 lmp->lno >= fl && lmp->lno <= tl) {
129 mark_reset = 1;
130 F_CLR(lmp, MARK_USERSET);
131 (void)log_mark(sp, lmp);
134 /* Get memory for the copy. */
135 GET_SPACE_RETW(sp, bp, blen, 256);
137 /* Move the lines. */
138 diff = (fm2.lno - fm1.lno) + 1;
139 if (tl > fl) { /* Destination > source. */
140 mfl = tl - diff;
141 mtl = tl;
142 for (cnt = diff; cnt--;) {
143 if (db_get(sp, fl, DBG_FATAL, &p, &len))
144 return (1);
145 BINC_RETW(sp, bp, blen, len);
146 MEMCPYW(bp, p, len);
147 if (db_append(sp, 1, tl, bp, len))
148 return (1);
149 if (mark_reset)
150 for (lmp = sp->ep->marks.lh_first;
151 lmp != NULL; lmp = lmp->q.le_next)
152 if (lmp->name != ABSMARK1 &&
153 lmp->lno == fl)
154 lmp->lno = tl + 1;
155 if (db_delete(sp, fl))
156 return (1);
158 } else { /* Destination < source. */
159 mfl = tl;
160 mtl = tl + diff;
161 for (cnt = diff; cnt--;) {
162 if (db_get(sp, fl, DBG_FATAL, &p, &len))
163 return (1);
164 BINC_RETW(sp, bp, blen, len);
165 MEMCPYW(bp, p, len);
166 if (db_append(sp, 1, tl++, bp, len))
167 return (1);
168 if (mark_reset)
169 for (lmp = sp->ep->marks.lh_first;
170 lmp != NULL; lmp = lmp->q.le_next)
171 if (lmp->name != ABSMARK1 &&
172 lmp->lno == fl)
173 lmp->lno = tl;
174 ++fl;
175 if (db_delete(sp, fl))
176 return (1);
179 FREE_SPACEW(sp, bp, blen);
181 sp->lno = tl; /* Last line moved. */
182 sp->cno = 0;
184 /* Log the new positions of the marks. */
185 if (mark_reset)
186 for (lmp = sp->ep->marks.lh_first;
187 lmp != NULL; lmp = lmp->q.le_next)
188 if (lmp->name != ABSMARK1 &&
189 lmp->lno >= mfl && lmp->lno <= mtl)
190 (void)log_mark(sp, lmp);
193 sp->rptlines[L_MOVED] += diff;
194 return (0);