rework cut buffers to match historic practice
[nvi.git] / vi / v_delete.c
blob6f40d128c18cd76352a8769741b6724f95d8de6d
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.6 1994/01/09 14:21:11 bostic Exp $ (Berkeley) $Date: 1994/01/09 14:21:11 $";
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 /* The default buffer for deletes is '1'. */
47 if (!F_ISSET(vp, VC_BUFFER))
48 vp->buffer = '1';
49 if (cut(sp, ep, NULL, &vp->buffer, fm, tm, CUT_ROTATE))
50 return (1);
51 if (delete(sp, ep, fm, tm, 0))
52 return (1);
54 rp->lno = fm->lno;
55 rp->cno = fm->cno ? fm->cno - 1 : 0;
56 return (0);
60 * v_delete -- [buffer][count]d[count]motion
61 * Delete a range of text.
63 int
64 v_delete(sp, ep, vp, fm, tm, rp)
65 SCR *sp;
66 EXF *ep;
67 VICMDARG *vp;
68 MARK *fm, *tm, *rp;
70 recno_t nlines;
71 size_t len;
72 int lmode;
74 /* The default buffer for deletes is '1'. */
75 if (!F_ISSET(vp, VC_BUFFER))
76 vp->buffer = '1';
78 lmode = F_ISSET(vp, VC_LMODE) ? CUT_LINEMODE : 0;
79 if (cut(sp, ep, NULL, &vp->buffer, fm, tm, lmode | CUT_ROTATE))
80 return (1);
81 if (delete(sp, ep, fm, tm, lmode))
82 return (1);
84 /* Check for deleting the file. */
85 if (file_lline(sp, ep, &nlines))
86 return (1);
87 if (nlines == 0) {
88 rp->lno = 1;
89 rp->cno = 0;
90 return (0);
94 * If deleting lines, leave the cursor at the lowest line deleted,
95 * else, leave the cursor where it started. Always correct for EOL.
97 * The historic vi would delete the line the cursor was on (even if
98 * not in line mode) if the motion from the cursor was past the EOF
99 * and the cursor didn't originate on the last line of the file. A
100 * strange special case. We never delete the line the cursor is on.
101 * We'd have to pass a flag down to the delete() routine which would
102 * have to special case it.
104 if (lmode) {
105 rp->lno = MIN(fm->lno, tm->lno);
106 if (rp->lno > nlines)
107 rp->lno = nlines;
108 rp->cno = 0;
109 (void)nonblank(sp, ep, rp->lno, &rp->cno);
110 return (0);
113 rp->lno = fm->lno;
114 if (file_gline(sp, ep, rp->lno, &len) == NULL) {
115 GETLINE_ERR(sp, rp->lno);
116 return (1);
118 if (fm->cno >= len)
119 rp->cno = len ? len - 1 : 0;
120 else
121 rp->cno = fm->cno;
122 return (0);