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