xref: /freebsd/contrib/nvi/vi/v_xchar.c (revision 9124ddeb4a551977cf6b2218291e7c666ce25f47)
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_xchar.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_xchar -- [buffer] [count]x
29  *	Deletes the character(s) on which the cursor sits.
30  *
31  * PUBLIC: int v_xchar __P((SCR *, VICMD *));
32  */
33 int
34 v_xchar(sp, vp)
35 	SCR *sp;
36 	VICMD *vp;
37 {
38 	size_t len;
39 	int isempty;
40 
41 	if (db_eget(sp, vp->m_start.lno, NULL, &len, &isempty)) {
42 		if (isempty)
43 			goto nodel;
44 		return (1);
45 	}
46 	if (len == 0) {
47 nodel:		msgq(sp, M_BERR, "206|No characters to delete");
48 		return (1);
49 	}
50 
51 	/*
52 	 * Delete from the cursor toward the end of line, w/o moving the
53 	 * cursor.
54 	 *
55 	 * !!!
56 	 * Note, "2x" at EOL isn't the same as "xx" because the left movement
57 	 * of the cursor as part of the 'x' command isn't taken into account.
58 	 * Historically correct.
59 	 */
60 	if (F_ISSET(vp, VC_C1SET))
61 		vp->m_stop.cno += vp->count - 1;
62 	if (vp->m_stop.cno >= len - 1) {
63 		vp->m_stop.cno = len - 1;
64 		vp->m_final.cno = vp->m_start.cno ? vp->m_start.cno - 1 : 0;
65 	} else
66 		vp->m_final.cno = vp->m_start.cno;
67 
68 	if (cut(sp,
69 	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
70 	    &vp->m_start, &vp->m_stop, 0))
71 		return (1);
72 	return (del(sp, &vp->m_start, &vp->m_stop, 0));
73 }
74 
75 /*
76  * v_Xchar -- [buffer] [count]X
77  *	Deletes the character(s) immediately before the current cursor
78  *	position.
79  *
80  * PUBLIC: int v_Xchar __P((SCR *, VICMD *));
81  */
82 int
83 v_Xchar(sp, vp)
84 	SCR *sp;
85 	VICMD *vp;
86 {
87 	u_long cnt;
88 
89 	if (vp->m_start.cno == 0) {
90 		v_sol(sp);
91 		return (1);
92 	}
93 
94 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
95 	if (cnt >= vp->m_start.cno)
96 		vp->m_start.cno = 0;
97 	else
98 		vp->m_start.cno -= cnt;
99 	--vp->m_stop.cno;
100 	vp->m_final.cno = vp->m_start.cno;
101 
102 	if (cut(sp,
103 	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
104 	    &vp->m_start, &vp->m_stop, 0))
105 		return (1);
106 	return (del(sp, &vp->m_start, &vp->m_stop, 0));
107 }
108