Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_move.c
blob10c3fae30b2f9f00f6831a43df746af531976935
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
30 * ex_copy -- :[line [,line]] co[py] line [flags]
31 * Copy selected lines.
33 * PUBLIC: int ex_copy __P((SCR *, EXCMD *));
35 int
36 ex_copy(SCR *sp, EXCMD *cmdp)
38 CB cb;
39 MARK fm1, fm2, m, tm;
40 db_recno_t cnt;
41 int rval;
43 rval = 0;
45 NEEDFILE(sp, cmdp);
48 * It's possible to copy things into the area that's being
49 * copied, e.g. "2,5copy3" is legitimate. Save the text to
50 * a cut buffer.
52 fm1 = cmdp->addr1;
53 fm2 = cmdp->addr2;
54 memset(&cb, 0, sizeof(cb));
55 CIRCLEQ_INIT(&cb.textq);
56 for (cnt = fm1.lno; cnt <= fm2.lno; ++cnt)
57 if (cut_line(sp, cnt, 0, 0, &cb)) {
58 rval = 1;
59 goto err;
61 cb.flags |= CB_LMODE;
63 /* Put the text into place. */
64 tm.lno = cmdp->lineno;
65 tm.cno = 0;
66 if (put(sp, &cb, NULL, &tm, &m, 1))
67 rval = 1;
68 else {
70 * Copy puts the cursor on the last line copied. The cursor
71 * returned by the put routine is the first line put, not the
72 * last, because that's the historic semantic of vi.
74 cnt = (fm2.lno - fm1.lno) + 1;
75 sp->lno = m.lno + (cnt - 1);
76 sp->cno = 0;
78 err: text_lfree(&cb.textq);
79 return (rval);
83 * ex_move -- :[line [,line]] mo[ve] line
84 * Move selected lines.
86 * PUBLIC: int ex_move __P((SCR *, EXCMD *));
88 int
89 ex_move(SCR *sp, EXCMD *cmdp)
91 LMARK *lmp;
92 MARK fm1, fm2;
93 db_recno_t cnt, diff, fl, tl, mfl, mtl;
94 size_t blen, len;
95 int mark_reset;
96 CHAR_T *bp;
97 CHAR_T *p;
99 NEEDFILE(sp, cmdp);
102 * It's not possible to move things into the area that's being
103 * moved.
105 fm1 = cmdp->addr1;
106 fm2 = cmdp->addr2;
107 if (cmdp->lineno >= fm1.lno && cmdp->lineno <= fm2.lno) {
108 msgq(sp, M_ERR, "139|Destination line is inside move range");
109 return (1);
113 * Log the positions of any marks in the to-be-deleted lines. This
114 * has to work with the logging code. What happens is that we log
115 * the old mark positions, make the changes, then log the new mark
116 * positions. Then the marks end up in the right positions no matter
117 * which way the log is traversed.
119 * XXX
120 * Reset the MARK_USERSET flag so that the log can undo the mark.
121 * This isn't very clean, and should probably be fixed.
123 fl = fm1.lno;
124 tl = cmdp->lineno;
126 /* Log the old positions of the marks. */
127 mark_reset = 0;
128 for (lmp = sp->ep->marks.lh_first; lmp != NULL; lmp = lmp->q.le_next)
129 if (lmp->name != ABSMARK1 &&
130 lmp->lno >= fl && lmp->lno <= tl) {
131 mark_reset = 1;
132 F_CLR(lmp, MARK_USERSET);
133 (void)log_mark(sp, lmp);
136 /* Get memory for the copy. */
137 GET_SPACE_RETW(sp, bp, blen, 256);
139 /* Move the lines. */
140 diff = (fm2.lno - fm1.lno) + 1;
141 if (tl > fl) { /* Destination > source. */
142 mfl = tl - diff;
143 mtl = tl;
144 for (cnt = diff; cnt--;) {
145 if (db_get(sp, fl, DBG_FATAL, &p, &len))
146 return (1);
147 BINC_RETW(sp, bp, blen, len);
148 MEMCPYW(bp, p, len);
149 if (db_append(sp, 1, tl, bp, len))
150 return (1);
151 if (mark_reset)
152 for (lmp = sp->ep->marks.lh_first;
153 lmp != NULL; lmp = lmp->q.le_next)
154 if (lmp->name != ABSMARK1 &&
155 lmp->lno == fl)
156 lmp->lno = tl + 1;
157 if (db_delete(sp, fl))
158 return (1);
160 } else { /* Destination < source. */
161 mfl = tl;
162 mtl = tl + diff;
163 for (cnt = diff; cnt--;) {
164 if (db_get(sp, fl, DBG_FATAL, &p, &len))
165 return (1);
166 BINC_RETW(sp, bp, blen, len);
167 MEMCPYW(bp, p, len);
168 if (db_append(sp, 1, tl++, bp, len))
169 return (1);
170 if (mark_reset)
171 for (lmp = sp->ep->marks.lh_first;
172 lmp != NULL; lmp = lmp->q.le_next)
173 if (lmp->name != ABSMARK1 &&
174 lmp->lno == fl)
175 lmp->lno = tl;
176 ++fl;
177 if (db_delete(sp, fl))
178 return (1);
181 FREE_SPACEW(sp, bp, blen);
183 sp->lno = tl; /* Last line moved. */
184 sp->cno = 0;
186 /* Log the new positions of the marks. */
187 if (mark_reset)
188 for (lmp = sp->ep->marks.lh_first;
189 lmp != NULL; lmp = lmp->q.le_next)
190 if (lmp->name != ABSMARK1 &&
191 lmp->lno >= mfl && lmp->lno <= mtl)
192 (void)log_mark(sp, lmp);
195 sp->rptlines[L_MOVED] += diff;
196 return (0);