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