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