fix global code by turning it into two passes
[nvi.git] / vi / v_delete.c
blobcfed70d8c6bb48423accef22f670f4ec44bc7b46
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: v_delete.c,v 8.5 1993/11/04 16:17:23 bostic Exp $ (Berkeley) $Date: 1993/11/04 16:17:23 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include "vi.h"
15 #include "vcmd.h"
18 * v_Delete -- [buffer][count]D
19 * Delete line command.
21 int
22 v_Delete(sp, ep, vp, fm, tm, rp)
23 SCR *sp;
24 EXF *ep;
25 VICMDARG *vp;
26 MARK *fm, *tm, *rp;
28 recno_t lno;
29 size_t len;
31 if (file_gline(sp, ep, fm->lno, &len) == NULL) {
32 if (file_lline(sp, ep, &lno))
33 return (1);
34 if (lno == 0)
35 return (0);
36 GETLINE_ERR(sp, fm->lno);
37 return (1);
40 if (len == 0)
41 return (0);
43 tm->lno = fm->lno;
44 tm->cno = len;
46 if (cut(sp, ep,
47 F_ISSET(vp, VC_BUFFER) ? vp->buffer : DEFCB, fm, tm, 0))
48 return (1);
49 if (delete(sp, ep, fm, tm, 0))
50 return (1);
52 rp->lno = fm->lno;
53 rp->cno = fm->cno ? fm->cno - 1 : 0;
54 return (0);
58 * v_delete -- [buffer][count]d[count]motion
59 * Delete a range of text.
61 int
62 v_delete(sp, ep, vp, fm, tm, rp)
63 SCR *sp;
64 EXF *ep;
65 VICMDARG *vp;
66 MARK *fm, *tm, *rp;
68 recno_t nlines;
69 size_t len;
70 int lmode;
72 lmode = F_ISSET(vp, VC_LMODE);
73 if (cut(sp, ep,
74 F_ISSET(vp, VC_BUFFER) ? vp->buffer : DEFCB, fm, tm, lmode))
75 return (1);
76 if (delete(sp, ep, fm, tm, lmode))
77 return (1);
79 /* Check for deleting the file. */
80 if (file_lline(sp, ep, &nlines))
81 return (1);
82 if (nlines == 0) {
83 rp->lno = 1;
84 rp->cno = 0;
85 return (0);
89 * If deleting lines, leave the cursor at the lowest line deleted,
90 * else, leave the cursor where it started. Always correct for EOL.
92 * The historic vi would delete the line the cursor was on (even if
93 * not in line mode) if the motion from the cursor was past the EOF
94 * and the cursor didn't originate on the last line of the file. A
95 * strange special case. We never delete the line the cursor is on.
96 * We'd have to pass a flag down to the delete() routine which would
97 * have to special case it.
99 if (lmode) {
100 rp->lno = MIN(fm->lno, tm->lno);
101 if (rp->lno > nlines)
102 rp->lno = nlines;
103 rp->cno = 0;
104 (void)nonblank(sp, ep, rp->lno, &rp->cno);
105 return (0);
108 rp->lno = fm->lno;
109 if (file_gline(sp, ep, rp->lno, &len) == NULL) {
110 GETLINE_ERR(sp, rp->lno);
111 return (1);
113 if (fm->cno >= len)
114 rp->cno = len ? len - 1 : 0;
115 else
116 rp->cno = fm->cno;
117 return (0);