xref: /dflybsd-src/contrib/nvi2/vi/v_delete.c (revision 07bc39c2f4bbca56f12568e06d89da17f2eeb965)
1*e0b8e63eSJohn Marino /*-
2*e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994
3*e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4*e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5*e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6*e0b8e63eSJohn Marino  *
7*e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8*e0b8e63eSJohn Marino  */
9*e0b8e63eSJohn Marino 
10*e0b8e63eSJohn Marino #include "config.h"
11*e0b8e63eSJohn Marino 
12*e0b8e63eSJohn Marino #include <sys/types.h>
13*e0b8e63eSJohn Marino #include <sys/queue.h>
14*e0b8e63eSJohn Marino #include <sys/time.h>
15*e0b8e63eSJohn Marino 
16*e0b8e63eSJohn Marino #include <bitstring.h>
17*e0b8e63eSJohn Marino #include <limits.h>
18*e0b8e63eSJohn Marino #include <stdio.h>
19*e0b8e63eSJohn Marino 
20*e0b8e63eSJohn Marino #include "../common/common.h"
21*e0b8e63eSJohn Marino #include "vi.h"
22*e0b8e63eSJohn Marino 
23*e0b8e63eSJohn Marino /*
24*e0b8e63eSJohn Marino  * v_delete -- [buffer][count]d[count]motion
25*e0b8e63eSJohn Marino  *	       [buffer][count]D
26*e0b8e63eSJohn Marino  *	Delete a range of text.
27*e0b8e63eSJohn Marino  *
28*e0b8e63eSJohn Marino  * PUBLIC: int v_delete(SCR *, VICMD *);
29*e0b8e63eSJohn Marino  */
30*e0b8e63eSJohn Marino int
v_delete(SCR * sp,VICMD * vp)31*e0b8e63eSJohn Marino v_delete(SCR *sp, VICMD *vp)
32*e0b8e63eSJohn Marino {
33*e0b8e63eSJohn Marino 	recno_t nlines;
34*e0b8e63eSJohn Marino 	size_t len;
35*e0b8e63eSJohn Marino 	int lmode;
36*e0b8e63eSJohn Marino 
37*e0b8e63eSJohn Marino 	lmode = F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0;
38*e0b8e63eSJohn Marino 
39*e0b8e63eSJohn Marino 	/* Yank the lines. */
40*e0b8e63eSJohn Marino 	if (cut(sp, F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
41*e0b8e63eSJohn Marino 	    &vp->m_start, &vp->m_stop,
42*e0b8e63eSJohn Marino 	    lmode | (F_ISSET(vp, VM_CUTREQ) ? CUT_NUMREQ : CUT_NUMOPT)))
43*e0b8e63eSJohn Marino 		return (1);
44*e0b8e63eSJohn Marino 
45*e0b8e63eSJohn Marino 	/* Delete the lines. */
46*e0b8e63eSJohn Marino 	if (del(sp, &vp->m_start, &vp->m_stop, lmode))
47*e0b8e63eSJohn Marino 		return (1);
48*e0b8e63eSJohn Marino 
49*e0b8e63eSJohn Marino 	/*
50*e0b8e63eSJohn Marino 	 * Check for deletion of the entire file.  Try to check a close
51*e0b8e63eSJohn Marino 	 * by line so we don't go to the end of the file unnecessarily.
52*e0b8e63eSJohn Marino 	 */
53*e0b8e63eSJohn Marino 	if (!db_exist(sp, vp->m_final.lno + 1)) {
54*e0b8e63eSJohn Marino 		if (db_last(sp, &nlines))
55*e0b8e63eSJohn Marino 			return (1);
56*e0b8e63eSJohn Marino 		if (nlines == 0) {
57*e0b8e63eSJohn Marino 			vp->m_final.lno = 1;
58*e0b8e63eSJohn Marino 			vp->m_final.cno = 0;
59*e0b8e63eSJohn Marino 			return (0);
60*e0b8e63eSJohn Marino 		}
61*e0b8e63eSJohn Marino 	}
62*e0b8e63eSJohn Marino 
63*e0b8e63eSJohn Marino 	/*
64*e0b8e63eSJohn Marino 	 * One special correction, in case we've deleted the current line or
65*e0b8e63eSJohn Marino 	 * character.  We check it here instead of checking in every command
66*e0b8e63eSJohn Marino 	 * that can be a motion component.
67*e0b8e63eSJohn Marino 	 */
68*e0b8e63eSJohn Marino 	if (db_get(sp, vp->m_final.lno, 0, NULL, &len)) {
69*e0b8e63eSJohn Marino 		if (db_get(sp, nlines, DBG_FATAL, NULL, &len))
70*e0b8e63eSJohn Marino 			return (1);
71*e0b8e63eSJohn Marino 		vp->m_final.lno = nlines;
72*e0b8e63eSJohn Marino 	}
73*e0b8e63eSJohn Marino 
74*e0b8e63eSJohn Marino 	/*
75*e0b8e63eSJohn Marino 	 * !!!
76*e0b8e63eSJohn Marino 	 * Cursor movements, other than those caused by a line mode command
77*e0b8e63eSJohn Marino 	 * moving to another line, historically reset the relative position.
78*e0b8e63eSJohn Marino 	 *
79*e0b8e63eSJohn Marino 	 * This currently matches the check made in v_yank(), I'm hoping that
80*e0b8e63eSJohn Marino 	 * they should be consistent...
81*e0b8e63eSJohn Marino 	 */
82*e0b8e63eSJohn Marino 	if (!F_ISSET(vp, VM_LMODE)) {
83*e0b8e63eSJohn Marino 		F_CLR(vp, VM_RCM_MASK);
84*e0b8e63eSJohn Marino 		F_SET(vp, VM_RCM_SET);
85*e0b8e63eSJohn Marino 
86*e0b8e63eSJohn Marino 		/* Make sure the set cursor position exists. */
87*e0b8e63eSJohn Marino 		if (vp->m_final.cno >= len)
88*e0b8e63eSJohn Marino 			vp->m_final.cno = len ? len - 1 : 0;
89*e0b8e63eSJohn Marino 	}
90*e0b8e63eSJohn Marino 
91*e0b8e63eSJohn Marino 	/*
92*e0b8e63eSJohn Marino 	 * !!!
93*e0b8e63eSJohn Marino 	 * The "dd" command moved to the first non-blank; "d<motion>"
94*e0b8e63eSJohn Marino 	 * didn't.
95*e0b8e63eSJohn Marino 	 */
96*e0b8e63eSJohn Marino 	if (F_ISSET(vp, VM_LDOUBLE)) {
97*e0b8e63eSJohn Marino 		F_CLR(vp, VM_RCM_MASK);
98*e0b8e63eSJohn Marino 		F_SET(vp, VM_RCM_SETFNB);
99*e0b8e63eSJohn Marino 	}
100*e0b8e63eSJohn Marino 	return (0);
101*e0b8e63eSJohn Marino }
102