xref: /freebsd/contrib/nvi/vi/v_right.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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 #ifndef lint
13 static const char sccsid[] = "@(#)v_right.c	10.7 (Berkeley) 3/6/96";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 
24 #include "../common/common.h"
25 #include "vi.h"
26 
27 /*
28  * v_right -- [count]' ', [count]l
29  *	Move right by columns.
30  *
31  * PUBLIC: int v_right __P((SCR *, VICMD *));
32  */
33 int
34 v_right(sp, vp)
35 	SCR *sp;
36 	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) {
49 eol:		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  */
85 int
86 v_dollar(sp, vp)
87 	SCR *sp;
88 	VICMD *vp;
89 {
90 	size_t len;
91 	int isempty;
92 
93 	/*
94 	 * !!!
95 	 * A count moves down count - 1 rows, so, "3$" is the same as "2j$".
96 	 */
97 	if ((F_ISSET(vp, VC_C1SET) ? vp->count : 1) != 1) {
98 		/*
99 		 * !!!
100 		 * Historically, if the $ is a motion, and deleting from
101 		 * at or before the first non-blank of the line, it's a
102 		 * line motion, and the line motion flag is set.
103 		 */
104 		vp->m_stop.cno = 0;
105 		if (nonblank(sp, vp->m_start.lno, &vp->m_stop.cno))
106 			return (1);
107 		if (ISMOTION(vp) && vp->m_start.cno <= vp->m_stop.cno)
108 			F_SET(vp, VM_LMODE);
109 
110 		--vp->count;
111 		if (v_down(sp, vp))
112 			return (1);
113 	}
114 
115 	/*
116 	 * !!!
117 	 * Historically, it was illegal to use $ as a motion command on
118 	 * an empty line.  Unfortunately, even though C was historically
119 	 * aliased to c$, it (and not c$) was special cased to work on
120 	 * empty lines.  Since we alias C to c$ too, we have a problem.
121 	 * To fix it, we let c$ go through, on the assumption that it's
122 	 * not a problem for it to work.
123 	 */
124 	if (db_eget(sp, vp->m_stop.lno, NULL, &len, &isempty)) {
125 		if (!isempty)
126 			return (1);
127 		len = 0;
128 	}
129 
130 	if (len == 0) {
131 		if (ISMOTION(vp) && !ISCMD(vp->rkp, 'c')) {
132 			v_eol(sp, NULL);
133 			return (1);
134 		}
135 		return (0);
136 	}
137 
138 	/*
139 	 * Non-motion commands move to the end of the range.  Delete
140 	 * and yank stay at the start.  Ignore others.
141 	 */
142 	vp->m_stop.cno = len ? len - 1 : 0;
143 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
144 	return (0);
145 }
146