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