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