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