split is "sp", not "s"
[nvi.git] / vi / v_delete.c
blobd00cbc518e2f2b47400321151c298749df7b68ff
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.7 1994/01/11 22:18:46 bostic Exp $ (Berkeley) $Date: 1994/01/11 22:18:46 $";
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 /* Yank the lines. */
47 if (cut(sp, ep, NULL,
48 F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL, fm, tm, CUT_DELETE))
49 return (1);
50 if (delete(sp, ep, fm, tm, 0))
51 return (1);
53 rp->lno = fm->lno;
54 rp->cno = fm->cno ? fm->cno - 1 : 0;
55 return (0);
59 * v_delete -- [buffer][count]d[count]motion
60 * Delete a range of text.
62 int
63 v_delete(sp, ep, vp, fm, tm, rp)
64 SCR *sp;
65 EXF *ep;
66 VICMDARG *vp;
67 MARK *fm, *tm, *rp;
69 recno_t nlines;
70 size_t len;
71 int lmode;
73 /* Yank the lines. */
74 lmode = F_ISSET(vp, VC_LMODE) ? CUT_LINEMODE : 0;
75 if (cut(sp, ep, NULL,
76 F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
77 fm, tm, lmode | CUT_DELETE))
78 return (1);
79 if (delete(sp, ep, fm, tm, lmode))
80 return (1);
82 /* Check for deleting the file. */
83 if (file_lline(sp, ep, &nlines))
84 return (1);
85 if (nlines == 0) {
86 rp->lno = 1;
87 rp->cno = 0;
88 return (0);
92 * If deleting lines, leave the cursor at the lowest line deleted,
93 * else, leave the cursor where it started. Always correct for EOL.
95 * The historic vi would delete the line the cursor was on (even if
96 * not in line mode) if the motion from the cursor was past the EOF
97 * and the cursor didn't originate on the last line of the file. A
98 * strange special case. We never delete the line the cursor is on.
99 * We'd have to pass a flag down to the delete() routine which would
100 * have to special case it.
102 if (lmode) {
103 rp->lno = MIN(fm->lno, tm->lno);
104 if (rp->lno > nlines)
105 rp->lno = nlines;
106 rp->cno = 0;
107 (void)nonblank(sp, ep, rp->lno, &rp->cno);
108 return (0);
111 rp->lno = fm->lno;
112 if (file_gline(sp, ep, rp->lno, &len) == NULL) {
113 GETLINE_ERR(sp, rp->lno);
114 return (1);
116 if (fm->cno >= len)
117 rp->cno = len ? len - 1 : 0;
118 else
119 rp->cno = fm->cno;
120 return (0);