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