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