1/*	$NetBSD$ */
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#ifndef lint
15static const char sccsid[] = "Id: v_right.c,v 10.8 2001/06/25 15:19:34 skimo Exp (Berkeley) Date: 2001/06/25 15:19:34";
16#endif /* not lint */
17
18#include <sys/types.h>
19#include <sys/queue.h>
20#include <sys/time.h>
21
22#include <bitstring.h>
23#include <limits.h>
24#include <stdio.h>
25
26#include "../common/common.h"
27#include "vi.h"
28
29/*
30 * v_right -- [count]' ', [count]l
31 *	Move right by columns.
32 *
33 * PUBLIC: int v_right __P((SCR *, VICMD *));
34 */
35int
36v_right(SCR *sp, 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 eol;
44		return (1);
45	}
46
47	/* It's always illegal to move right on empty lines. */
48	if (len == 0) {
49eol:		v_eol(sp, NULL);
50		return (1);
51	}
52
53	/*
54	 * Non-motion commands move to the end of the range.  Delete and
55	 * yank stay at the start.  Ignore others.  Adjust the end of the
56	 * range for motion commands.
57	 *
58	 * !!!
59	 * Historically, "[cdsy]l" worked at the end of a line.  Also,
60	 * EOL is a count sink.
61	 */
62	vp->m_stop.cno = vp->m_start.cno +
63	    (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
64	if (vp->m_start.cno == len - 1 && !ISMOTION(vp)) {
65		v_eol(sp, NULL);
66		return (1);
67	}
68	if (vp->m_stop.cno >= len) {
69		vp->m_stop.cno = len - 1;
70		vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
71	} else if (ISMOTION(vp)) {
72		--vp->m_stop.cno;
73		vp->m_final = vp->m_start;
74	} else
75		vp->m_final = vp->m_stop;
76	return (0);
77}
78
79/*
80 * v_dollar -- [count]$
81 *	Move to the last column.
82 *
83 * PUBLIC: int v_dollar __P((SCR *, VICMD *));
84 */
85int
86v_dollar(SCR *sp, VICMD *vp)
87{
88	size_t len;
89	int isempty;
90
91	/*
92	 * !!!
93	 * A count moves down count - 1 rows, so, "3$" is the same as "2j$".
94	 */
95	if ((F_ISSET(vp, VC_C1SET) ? vp->count : 1) != 1) {
96		/*
97		 * !!!
98		 * Historically, if the $ is a motion, and deleting from
99		 * at or before the first non-blank of the line, it's a
100		 * line motion, and the line motion flag is set.
101		 */
102		vp->m_stop.cno = 0;
103		if (nonblank(sp, vp->m_start.lno, &vp->m_stop.cno))
104			return (1);
105		if (ISMOTION(vp) && vp->m_start.cno <= vp->m_stop.cno)
106			F_SET(vp, VM_LMODE);
107
108		--vp->count;
109		if (v_down(sp, vp))
110			return (1);
111	}
112
113	/*
114	 * !!!
115	 * Historically, it was illegal to use $ as a motion command on
116	 * an empty line.  Unfortunately, even though C was historically
117	 * aliased to c$, it (and not c$) was special cased to work on
118	 * empty lines.  Since we alias C to c$ too, we have a problem.
119	 * To fix it, we let c$ go through, on the assumption that it's
120	 * not a problem for it to work.
121	 */
122	if (db_eget(sp, vp->m_stop.lno, NULL, &len, &isempty)) {
123		if (!isempty)
124			return (1);
125		len = 0;
126	}
127
128	if (len == 0) {
129		if (ISMOTION(vp) && !ISCMD(vp->rkp, 'c')) {
130			v_eol(sp, NULL);
131			return (1);
132		}
133		return (0);
134	}
135
136	/*
137	 * Non-motion commands move to the end of the range.  Delete
138	 * and yank stay at the start.  Ignore others.
139	 */
140	vp->m_stop.cno = len ? len - 1 : 0;
141	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
142	return (0);
143}
144