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