1 /* $OpenBSD: v_left.c,v 1.7 2022/12/26 19:16:04 jmc 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_left -- [count]^H, [count]h
27 * Move left by columns.
28 *
29 * PUBLIC: int v_left(SCR *, VICMD *);
30 */
31 int
v_left(SCR * sp,VICMD * vp)32 v_left(SCR *sp, VICMD *vp)
33 {
34 recno_t cnt;
35
36 /*
37 * !!!
38 * The ^H and h commands always failed in the first column.
39 */
40 if (vp->m_start.cno == 0) {
41 v_sol(sp);
42 return (1);
43 }
44
45 /* Find the end of the range. */
46 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
47 if (vp->m_start.cno > cnt)
48 vp->m_stop.cno = vp->m_start.cno - cnt;
49 else
50 vp->m_stop.cno = 0;
51
52 /*
53 * All commands move to the end of the range. Motion commands
54 * adjust the starting point to the character before the current
55 * one.
56 */
57 if (ISMOTION(vp))
58 --vp->m_start.cno;
59 vp->m_final = vp->m_stop;
60 return (0);
61 }
62
63 /*
64 * v_cfirst -- [count]_
65 * Move to the first non-blank character in a line.
66 *
67 * PUBLIC: int v_cfirst(SCR *, VICMD *);
68 */
69 int
v_cfirst(SCR * sp,VICMD * vp)70 v_cfirst(SCR *sp, VICMD *vp)
71 {
72 recno_t cnt, lno;
73
74 /*
75 * !!!
76 * If the _ is a motion component, it makes the command a line motion
77 * e.g. "d_" deletes the line. It also means that the cursor doesn't
78 * move.
79 *
80 * The _ command never failed in the first column.
81 */
82 if (ISMOTION(vp))
83 F_SET(vp, VM_LMODE);
84 /*
85 * !!!
86 * Historically a specified count makes _ move down count - 1
87 * rows, so, "3_" is the same as "2j_".
88 */
89 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
90 if (cnt != 1) {
91 --vp->count;
92 return (v_down(sp, vp));
93 }
94
95 /*
96 * Move to the first non-blank.
97 *
98 * Can't just use RCM_SET_FNB, in case _ is used as the motion
99 * component of another command.
100 */
101 vp->m_stop.cno = 0;
102 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
103 return (1);
104
105 /*
106 * !!!
107 * The _ command has to fail if the file is empty and we're doing
108 * a delete. If deleting line 1, and 0 is the first nonblank,
109 * make the check.
110 */
111 if (vp->m_stop.lno == 1 &&
112 vp->m_stop.cno == 0 && ISCMD(vp->rkp, 'd')) {
113 if (db_last(sp, &lno))
114 return (1);
115 if (lno == 0) {
116 v_sol(sp);
117 return (1);
118 }
119 }
120
121 /*
122 * Delete and non-motion commands move to the end of the range,
123 * yank stays at the start. Ignore others.
124 */
125 vp->m_final =
126 ISMOTION(vp) && ISCMD(vp->rkp, 'y') ? vp->m_start : vp->m_stop;
127 return (0);
128 }
129
130 /*
131 * v_first -- ^
132 * Move to the first non-blank character in this line.
133 *
134 * PUBLIC: int v_first(SCR *, VICMD *);
135 */
136 int
v_first(SCR * sp,VICMD * vp)137 v_first(SCR *sp, VICMD *vp)
138 {
139 /*
140 * !!!
141 * Yielding to none in our quest for compatibility with every
142 * historical blemish of vi, no matter how strange it might be,
143 * we permit the user to enter a count and then ignore it.
144 */
145
146 /*
147 * Move to the first non-blank.
148 *
149 * Can't just use RCM_SET_FNB, in case ^ is used as the motion
150 * component of another command.
151 */
152 vp->m_stop.cno = 0;
153 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
154 return (1);
155
156 /*
157 * !!!
158 * The ^ command succeeded if used as a command when the cursor was
159 * on the first non-blank in the line, but failed if used as a motion
160 * component in the same situation.
161 */
162 if (ISMOTION(vp) && vp->m_start.cno == vp->m_stop.cno) {
163 v_sol(sp);
164 return (1);
165 }
166
167 /*
168 * If moving right, non-motion commands move to the end of the range.
169 * Delete and yank stay at the start. Motion commands adjust the
170 * ending point to the character before the current ending character.
171 *
172 * If moving left, all commands move to the end of the range. Motion
173 * commands adjust the starting point to the character before the
174 * current starting character.
175 */
176 if (vp->m_start.cno < vp->m_stop.cno)
177 if (ISMOTION(vp)) {
178 --vp->m_stop.cno;
179 vp->m_final = vp->m_start;
180 } else
181 vp->m_final = vp->m_stop;
182 else {
183 if (ISMOTION(vp))
184 --vp->m_start.cno;
185 vp->m_final = vp->m_stop;
186 }
187 return (0);
188 }
189
190 /*
191 * v_ncol -- [count]|
192 * Move to column count or the first column on this line. If the
193 * requested column is past EOL, move to EOL. The nasty part is
194 * that we have to know character column widths to make this work.
195 *
196 * PUBLIC: int v_ncol(SCR *, VICMD *);
197 */
198 int
v_ncol(SCR * sp,VICMD * vp)199 v_ncol(SCR *sp, VICMD *vp)
200 {
201 if (F_ISSET(vp, VC_C1SET) && vp->count > 1) {
202 --vp->count;
203 vp->m_stop.cno =
204 vs_colpos(sp, vp->m_start.lno, (size_t)vp->count);
205 /*
206 * !!!
207 * The | command succeeded if used as a command and the cursor
208 * didn't move, but failed if used as a motion component in the
209 * same situation.
210 */
211 if (ISMOTION(vp) && vp->m_stop.cno == vp->m_start.cno) {
212 v_nomove(sp);
213 return (1);
214 }
215 } else {
216 /*
217 * !!!
218 * The | command succeeded if used as a command in column 0
219 * without a count, but failed if used as a motion component
220 * in the same situation.
221 */
222 if (ISMOTION(vp) && vp->m_start.cno == 0) {
223 v_sol(sp);
224 return (1);
225 }
226 vp->m_stop.cno = 0;
227 }
228
229 /*
230 * If moving right, non-motion commands move to the end of the range.
231 * Delete and yank stay at the start. Motion commands adjust the
232 * ending point to the character before the current ending character.
233 *
234 * If moving left, all commands move to the end of the range. Motion
235 * commands adjust the starting point to the character before the
236 * current starting character.
237 */
238 if (vp->m_start.cno < vp->m_stop.cno)
239 if (ISMOTION(vp)) {
240 --vp->m_stop.cno;
241 vp->m_final = vp->m_start;
242 } else
243 vp->m_final = vp->m_stop;
244 else {
245 if (ISMOTION(vp))
246 --vp->m_start.cno;
247 vp->m_final = vp->m_stop;
248 }
249 return (0);
250 }
251
252 /*
253 * v_zero -- 0
254 * Move to the first column on this line.
255 *
256 * PUBLIC: int v_zero(SCR *, VICMD *);
257 */
258 int
v_zero(SCR * sp,VICMD * vp)259 v_zero(SCR *sp, VICMD *vp)
260 {
261 /*
262 * !!!
263 * The 0 command succeeded if used as a command in the first column
264 * but failed if used as a motion component in the same situation.
265 */
266 if (ISMOTION(vp) && vp->m_start.cno == 0) {
267 v_sol(sp);
268 return (1);
269 }
270
271 /*
272 * All commands move to the end of the range. Motion commands
273 * adjust the starting point to the character before the current
274 * one.
275 */
276 vp->m_stop.cno = 0;
277 if (ISMOTION(vp))
278 --vp->m_start.cno;
279 vp->m_final = vp->m_stop;
280 return (0);
281 }
282