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