1/*	$NetBSD: v_xchar.c,v 1.3 2014/01/26 21:43:45 christos Exp $	*/
2/*-
3 * Copyright (c) 1992, 1993, 1994
4 *	The Regents of the University of California.  All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 *	Keith Bostic.  All rights reserved.
7 *
8 * See the LICENSE file for redistribution information.
9 */
10
11#include "config.h"
12
13#include <sys/cdefs.h>
14#if 0
15#ifndef lint
16static const char sccsid[] = "Id: v_xchar.c,v 10.10 2001/06/25 15:19:36 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:36 ";
17#endif /* not lint */
18#else
19__RCSID("$NetBSD: v_xchar.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20#endif
21
22#include <sys/types.h>
23#include <sys/queue.h>
24#include <sys/time.h>
25
26#include <bitstring.h>
27#include <limits.h>
28#include <stdio.h>
29
30#include "../common/common.h"
31#include "vi.h"
32
33/*
34 * v_xchar -- [buffer] [count]x
35 *	Deletes the character(s) on which the cursor sits.
36 *
37 * PUBLIC: int v_xchar __P((SCR *, VICMD *));
38 */
39int
40v_xchar(SCR *sp, VICMD *vp)
41{
42	size_t len;
43	int isempty;
44
45	if (db_eget(sp, vp->m_start.lno, NULL, &len, &isempty)) {
46		if (isempty)
47			goto nodel;
48		return (1);
49	}
50	if (len == 0) {
51nodel:		msgq(sp, M_BERR, "206|No characters to delete");
52		return (1);
53	}
54
55	/*
56	 * Delete from the cursor toward the end of line, w/o moving the
57	 * cursor.
58	 *
59	 * !!!
60	 * Note, "2x" at EOL isn't the same as "xx" because the left movement
61	 * of the cursor as part of the 'x' command isn't taken into account.
62	 * Historically correct.
63	 */
64	if (F_ISSET(vp, VC_C1SET))
65		vp->m_stop.cno += vp->count - 1;
66	if (vp->m_stop.cno >= len - 1) {
67		vp->m_stop.cno = len - 1;
68		vp->m_final.cno = vp->m_start.cno ? vp->m_start.cno - 1 : 0;
69	} else
70		vp->m_final.cno = vp->m_start.cno;
71
72	if (cut(sp,
73	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
74	    &vp->m_start, &vp->m_stop, 0))
75		return (1);
76	return (del(sp, &vp->m_start, &vp->m_stop, 0));
77}
78
79/*
80 * v_Xchar -- [buffer] [count]X
81 *	Deletes the character(s) immediately before the current cursor
82 *	position.
83 *
84 * PUBLIC: int v_Xchar __P((SCR *, VICMD *));
85 */
86int
87v_Xchar(SCR *sp, VICMD *vp)
88{
89	u_long cnt;
90
91	if (vp->m_start.cno == 0) {
92		v_sol(sp);
93		return (1);
94	}
95
96	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
97	if (cnt >= vp->m_start.cno)
98		vp->m_start.cno = 0;
99	else
100		vp->m_start.cno -= cnt;
101	--vp->m_stop.cno;
102	vp->m_final.cno = vp->m_start.cno;
103
104	if (cut(sp,
105	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
106	    &vp->m_start, &vp->m_stop, 0))
107		return (1);
108	return (del(sp, &vp->m_start, &vp->m_stop, 0));
109}
110