lint
[nvi.git] / ex / ex_move.c
blob3b397ffdc2400f66a95e62fd67823a7b63760205
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.2 1993/07/21 09:00:31 bostic Exp $ (Berkeley) $Date: 1993/07/21 09:00:31 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include "vi.h"
15 #include "excmd.h"
17 enum which {COPY, MOVE};
18 static int cm __P((SCR *, EXF *, EXCMDARG *, enum which));
21 * ex_copy -- :[line [,line]] co[py] line [flags]
22 * Copy selected lines.
24 int
25 ex_copy(sp, ep, cmdp)
26 SCR *sp;
27 EXF *ep;
28 EXCMDARG *cmdp;
30 return (cm(sp, ep, cmdp, COPY));
34 * ex_move -- :[line [,line]] co[py] line
35 * Move selected lines.
37 int
38 ex_move(sp, ep, cmdp)
39 SCR *sp;
40 EXF *ep;
41 EXCMDARG *cmdp;
43 return (cm(sp, ep, cmdp, MOVE));
46 static int
47 cm(sp, ep, cmdp, cmd)
48 SCR *sp;
49 EXF *ep;
50 EXCMDARG *cmdp;
51 enum which cmd;
53 MARK fm1, fm2, m, tm;
54 recno_t lline;
56 fm1 = cmdp->addr1;
57 fm2 = cmdp->addr2;
58 tm.lno = cmdp->lineno;
59 tm.cno = 0;
61 /* Make sure the destination is valid. */
62 if (cmd == MOVE && tm.lno >= fm1.lno && tm.lno < fm2.lno) {
63 msgq(sp, M_ERR,
64 "Destination line is inside move range.");
65 return (1);
68 /* Save the text to a cut buffer. */
69 if (cut(sp, ep, DEFCB, &fm1, &fm2, 1))
70 return (1);
72 /* If we're not copying, delete the old text & adjust tm. */
73 if (cmd == MOVE) {
74 if (delete(sp, ep, &fm1, &fm2, 1))
75 return (1);
76 if (tm.lno >= fm1.lno)
77 tm.lno -= (fm2.lno - fm1.lno) + 1;
80 /* Add the new text. */
81 m.lno = sp->lno;
82 m.cno = sp->cno;
83 if (put(sp, ep, DEFCB, &tm, &m, 1))
84 return (1);
86 if (sp->lno < 1)
87 sp->lno = 1;
88 else {
89 if (file_lline(sp, ep, &lline))
90 return (1);
91 if (sp->lno > lline)
92 sp->lno = lline;
95 /* Reporting. */
96 sp->rptlines[cmd ==
97 COPY ? L_COPIED : L_MOVED] += (fm2.lno - fm1.lno) + 1;
99 F_SET(sp, S_AUTOPRINT);
100 return (0);