align CHAR_T string in log
[nvi.git] / ex / ex_move.c
blobdebb8b2907fa9776f8170db3b10c1faca8929a3a
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.14 2000/07/15 20:26:35 skimo Exp $ (Berkeley) $Date: 2000/07/15 20:26:35 $";
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(sp, cmdp)
35 SCR *sp;
36 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(sp, cmdp)
90 SCR *sp;
91 EXCMD *cmdp;
93 LMARK *lmp;
94 MARK fm1, fm2;
95 db_recno_t cnt, diff, fl, tl, mfl, mtl;
96 size_t blen, len;
97 int mark_reset;
98 CHAR_T *bp;
99 CHAR_T *p;
101 NEEDFILE(sp, cmdp);
104 * It's not possible to move things into the area that's being
105 * moved.
107 fm1 = cmdp->addr1;
108 fm2 = cmdp->addr2;
109 if (cmdp->lineno >= fm1.lno && cmdp->lineno <= fm2.lno) {
110 msgq(sp, M_ERR, "139|Destination line is inside move range");
111 return (1);
115 * Log the positions of any marks in the to-be-deleted lines. This
116 * has to work with the logging code. What happens is that we log
117 * the old mark positions, make the changes, then log the new mark
118 * positions. Then the marks end up in the right positions no matter
119 * which way the log is traversed.
121 * XXX
122 * Reset the MARK_USERSET flag so that the log can undo the mark.
123 * This isn't very clean, and should probably be fixed.
125 fl = fm1.lno;
126 tl = cmdp->lineno;
128 /* Log the old positions of the marks. */
129 mark_reset = 0;
130 for (lmp = sp->ep->marks.lh_first; lmp != NULL; lmp = lmp->q.le_next)
131 if (lmp->name != ABSMARK1 &&
132 lmp->lno >= fl && lmp->lno <= tl) {
133 mark_reset = 1;
134 F_CLR(lmp, MARK_USERSET);
135 (void)log_mark(sp, lmp);
138 /* Get memory for the copy. */
139 GET_SPACE_RETW(sp, bp, blen, 256);
141 /* Move the lines. */
142 diff = (fm2.lno - fm1.lno) + 1;
143 if (tl > fl) { /* Destination > source. */
144 mfl = tl - diff;
145 mtl = tl;
146 for (cnt = diff; cnt--;) {
147 if (db_get(sp, fl, DBG_FATAL, &p, &len))
148 return (1);
149 BINC_RETW(sp, bp, blen, len);
150 MEMCPYW(bp, p, len);
151 if (db_append(sp, 1, tl, bp, len))
152 return (1);
153 if (mark_reset)
154 for (lmp = sp->ep->marks.lh_first;
155 lmp != NULL; lmp = lmp->q.le_next)
156 if (lmp->name != ABSMARK1 &&
157 lmp->lno == fl)
158 lmp->lno = tl + 1;
159 if (db_delete(sp, fl))
160 return (1);
162 } else { /* Destination < source. */
163 mfl = tl;
164 mtl = tl + diff;
165 for (cnt = diff; cnt--;) {
166 if (db_get(sp, fl, DBG_FATAL, &p, &len))
167 return (1);
168 BINC_RETW(sp, bp, blen, len);
169 MEMCPYW(bp, p, len);
170 if (db_append(sp, 1, tl++, bp, len))
171 return (1);
172 if (mark_reset)
173 for (lmp = sp->ep->marks.lh_first;
174 lmp != NULL; lmp = lmp->q.le_next)
175 if (lmp->name != ABSMARK1 &&
176 lmp->lno == fl)
177 lmp->lno = tl;
178 ++fl;
179 if (db_delete(sp, fl))
180 return (1);
183 FREE_SPACEW(sp, bp, blen);
185 sp->lno = tl; /* Last line moved. */
186 sp->cno = 0;
188 /* Log the new positions of the marks. */
189 if (mark_reset)
190 for (lmp = sp->ep->marks.lh_first;
191 lmp != NULL; lmp = lmp->q.le_next)
192 if (lmp->name != ABSMARK1 &&
193 lmp->lno >= mfl && lmp->lno <= mtl)
194 (void)log_mark(sp, lmp);
197 sp->rptlines[L_MOVED] += diff;
198 return (0);