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