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