rework initial error handling -- since we check for any screens on
[nvi.git] / ex / ex_move.c
blob965d22a72b0c3bc0175f0a3190a26c590bcfa8b4
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_move.c,v 8.6 1994/01/09 23:24:02 bostic Exp $ (Berkeley) $Date: 1994/01/09 23:24:02 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <string.h>
16 #include "vi.h"
17 #include "excmd.h"
19 enum which {COPY, MOVE};
20 static int cm __P((SCR *, EXF *, EXCMDARG *, enum which));
23 * ex_copy -- :[line [,line]] co[py] line [flags]
24 * Copy selected lines.
26 int
27 ex_copy(sp, ep, cmdp)
28 SCR *sp;
29 EXF *ep;
30 EXCMDARG *cmdp;
32 return (cm(sp, ep, cmdp, COPY));
36 * ex_move -- :[line [,line]] co[py] line
37 * Move selected lines.
39 int
40 ex_move(sp, ep, cmdp)
41 SCR *sp;
42 EXF *ep;
43 EXCMDARG *cmdp;
45 return (cm(sp, ep, cmdp, MOVE));
48 static int
49 cm(sp, ep, cmdp, cmd)
50 SCR *sp;
51 EXF *ep;
52 EXCMDARG *cmdp;
53 enum which cmd;
55 CB cb;
56 MARK fm1, fm2, m, tm;
57 recno_t diff;
58 int rval;
60 fm1 = cmdp->addr1;
61 fm2 = cmdp->addr2;
62 tm.lno = cmdp->lineno;
63 tm.cno = 0;
65 /* Make sure the destination is valid. */
66 if (cmd == MOVE && tm.lno >= fm1.lno && tm.lno < fm2.lno) {
67 msgq(sp, M_ERR, "Destination line is inside move range.");
68 return (1);
71 /* Save the text to a cut buffer. */
72 memset(&cb, 0, sizeof(cb));
73 CIRCLEQ_INIT(&cb.textq);
74 if (cut(sp, ep, &cb, NULL, &fm1, &fm2, CUT_LINEMODE))
75 return (1);
77 /* If we're not copying, delete the old text and adjust tm. */
78 if (cmd == MOVE) {
79 if (delete(sp, ep, &fm1, &fm2, 1)) {
80 rval = 1;
81 goto err;
83 if (tm.lno >= fm1.lno)
84 tm.lno -= (fm2.lno - fm1.lno) + 1;
87 /* Add the new text. */
88 if (put(sp, ep, &cb, NULL, &tm, &m, 1)) {
89 rval = 1;
90 goto err;
94 * Move and copy put the cursor on the last line moved or copied.
95 * The returned cursor from the put routine is the first line put,
96 * not the last, because that's the semantics of vi.
98 diff = (fm2.lno - fm1.lno) + 1;
99 sp->lno = m.lno + (diff - 1);
100 sp->cno = 0;
102 sp->rptlines[cmd == COPY ? L_COPIED : L_MOVED] += diff;
103 rval = 0;
105 err: (void)text_lfree(&cb.textq);
106 return (rval);