v_ch.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1992, 1993, 1994
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos static const char sccsid[] = "Id: v_ch.c,v 10.10 2001/06/25 15:19:30 skimo Exp (Berkeley) Date: 2001/06/25 15:19:30 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/time.h>
19 1.1 christos
20 1.1 christos #include <bitstring.h>
21 1.1 christos #include <limits.h>
22 1.1 christos #include <stdio.h>
23 1.1 christos #include <stdlib.h>
24 1.1 christos
25 1.1 christos #include "../common/common.h"
26 1.1 christos #include "vi.h"
27 1.1 christos
28 1.1 christos static void notfound __P((SCR *, ARG_CHAR_T));
29 1.1 christos static void noprev __P((SCR *));
30 1.1 christos
31 1.1 christos /*
32 1.1 christos * v_chrepeat -- [count];
33 1.1 christos * Repeat the last F, f, T or t search.
34 1.1 christos *
35 1.1 christos * PUBLIC: int v_chrepeat __P((SCR *, VICMD *));
36 1.1 christos */
37 1.1 christos int
38 1.1 christos v_chrepeat(SCR *sp, VICMD *vp)
39 1.1 christos {
40 1.1 christos vp->character = VIP(sp)->lastckey;
41 1.1 christos
42 1.1 christos switch (VIP(sp)->csearchdir) {
43 1.1 christos case CNOTSET:
44 1.1 christos noprev(sp);
45 1.1 christos return (1);
46 1.1 christos case FSEARCH:
47 1.1 christos return (v_chF(sp, vp));
48 1.1 christos case fSEARCH:
49 1.1 christos return (v_chf(sp, vp));
50 1.1 christos case TSEARCH:
51 1.1 christos return (v_chT(sp, vp));
52 1.1 christos case tSEARCH:
53 1.1 christos return (v_cht(sp, vp));
54 1.1 christos default:
55 1.1 christos abort();
56 1.1 christos }
57 1.1 christos /* NOTREACHED */
58 1.1 christos }
59 1.1 christos
60 1.1 christos /*
61 1.1 christos * v_chrrepeat -- [count],
62 1.1 christos * Repeat the last F, f, T or t search in the reverse direction.
63 1.1 christos *
64 1.1 christos * PUBLIC: int v_chrrepeat __P((SCR *, VICMD *));
65 1.1 christos */
66 1.1 christos int
67 1.1 christos v_chrrepeat(SCR *sp, VICMD *vp)
68 1.1 christos {
69 1.1 christos cdir_t savedir;
70 1.1 christos int rval;
71 1.1 christos
72 1.1 christos vp->character = VIP(sp)->lastckey;
73 1.1 christos savedir = VIP(sp)->csearchdir;
74 1.1 christos
75 1.1 christos switch (VIP(sp)->csearchdir) {
76 1.1 christos case CNOTSET:
77 1.1 christos noprev(sp);
78 1.1 christos return (1);
79 1.1 christos case FSEARCH:
80 1.1 christos rval = v_chf(sp, vp);
81 1.1 christos break;
82 1.1 christos case fSEARCH:
83 1.1 christos rval = v_chF(sp, vp);
84 1.1 christos break;
85 1.1 christos case TSEARCH:
86 1.1 christos rval = v_cht(sp, vp);
87 1.1 christos break;
88 1.1 christos case tSEARCH:
89 1.1 christos rval = v_chT(sp, vp);
90 1.1 christos break;
91 1.1 christos default:
92 1.1 christos abort();
93 1.1 christos }
94 1.1 christos VIP(sp)->csearchdir = savedir;
95 1.1 christos return (rval);
96 1.1 christos }
97 1.1 christos
98 1.1 christos /*
99 1.1 christos * v_cht -- [count]tc
100 1.1 christos * Search forward in the line for the character before the next
101 1.1 christos * occurrence of the specified character.
102 1.1 christos *
103 1.1 christos * PUBLIC: int v_cht __P((SCR *, VICMD *));
104 1.1 christos */
105 1.1 christos int
106 1.1 christos v_cht(SCR *sp, VICMD *vp)
107 1.1 christos {
108 1.1 christos if (v_chf(sp, vp))
109 1.1 christos return (1);
110 1.1 christos
111 1.1 christos /*
112 1.1 christos * v_chf places the cursor on the character, where the 't'
113 1.1 christos * command wants it to its left. We know this is safe since
114 1.1 christos * we had to move right for v_chf() to have succeeded.
115 1.1 christos */
116 1.1 christos --vp->m_stop.cno;
117 1.1 christos
118 1.1 christos /*
119 1.1 christos * Make any necessary correction to the motion decision made
120 1.1 christos * by the v_chf routine.
121 1.1 christos */
122 1.1 christos if (!ISMOTION(vp))
123 1.1 christos vp->m_final = vp->m_stop;
124 1.1 christos
125 1.1 christos VIP(sp)->csearchdir = tSEARCH;
126 1.1 christos return (0);
127 1.1 christos }
128 1.1 christos
129 1.1 christos /*
130 1.1 christos * v_chf -- [count]fc
131 1.1 christos * Search forward in the line for the next occurrence of the
132 1.1 christos * specified character.
133 1.1 christos *
134 1.1 christos * PUBLIC: int v_chf __P((SCR *, VICMD *));
135 1.1 christos */
136 1.1 christos int
137 1.1 christos v_chf(SCR *sp, VICMD *vp)
138 1.1 christos {
139 1.1 christos size_t len;
140 1.1 christos u_long cnt;
141 1.1 christos int isempty, key;
142 1.1 christos CHAR_T *endp, *p, *startp;
143 1.1 christos
144 1.1 christos /*
145 1.1 christos * !!!
146 1.1 christos * If it's a dot command, it doesn't reset the key for which we're
147 1.1 christos * searching, e.g. in "df1|f2|.|;", the ';' searches for a '2'.
148 1.1 christos */
149 1.1 christos key = vp->character;
150 1.1 christos if (!F_ISSET(vp, VC_ISDOT))
151 1.1 christos VIP(sp)->lastckey = key;
152 1.1 christos VIP(sp)->csearchdir = fSEARCH;
153 1.1 christos
154 1.1 christos if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
155 1.1 christos if (isempty)
156 1.1 christos goto empty;
157 1.1 christos return (1);
158 1.1 christos }
159 1.1 christos
160 1.1 christos if (len == 0) {
161 1.1 christos empty: notfound(sp, key);
162 1.1 christos return (1);
163 1.1 christos }
164 1.1 christos
165 1.1 christos endp = (startp = p) + len;
166 1.1 christos p += vp->m_start.cno;
167 1.1 christos for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
168 1.1 christos while (++p < endp && *p != key);
169 1.1 christos if (p == endp) {
170 1.1 christos notfound(sp, key);
171 1.1 christos return (1);
172 1.1 christos }
173 1.1 christos }
174 1.1 christos
175 1.1 christos vp->m_stop.cno = p - startp;
176 1.1 christos
177 1.1 christos /*
178 1.1 christos * Non-motion commands move to the end of the range.
179 1.1 christos * Delete and yank stay at the start, ignore others.
180 1.1 christos */
181 1.1 christos vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
182 1.1 christos return (0);
183 1.1 christos }
184 1.1 christos
185 1.1 christos /*
186 1.1 christos * v_chT -- [count]Tc
187 1.1 christos * Search backward in the line for the character after the next
188 1.1 christos * occurrence of the specified character.
189 1.1 christos *
190 1.1 christos * PUBLIC: int v_chT __P((SCR *, VICMD *));
191 1.1 christos */
192 1.1 christos int
193 1.1 christos v_chT(SCR *sp, VICMD *vp)
194 1.1 christos {
195 1.1 christos if (v_chF(sp, vp))
196 1.1 christos return (1);
197 1.1 christos
198 1.1 christos /*
199 1.1 christos * v_chF places the cursor on the character, where the 'T'
200 1.1 christos * command wants it to its right. We know this is safe since
201 1.1 christos * we had to move left for v_chF() to have succeeded.
202 1.1 christos */
203 1.1 christos ++vp->m_stop.cno;
204 1.1 christos vp->m_final = vp->m_stop;
205 1.1 christos
206 1.1 christos VIP(sp)->csearchdir = TSEARCH;
207 1.1 christos return (0);
208 1.1 christos }
209 1.1 christos
210 1.1 christos /*
211 1.1 christos * v_chF -- [count]Fc
212 1.1 christos * Search backward in the line for the next occurrence of the
213 1.1 christos * specified character.
214 1.1 christos *
215 1.1 christos * PUBLIC: int v_chF __P((SCR *, VICMD *));
216 1.1 christos */
217 1.1 christos int
218 1.1 christos v_chF(SCR *sp, VICMD *vp)
219 1.1 christos {
220 1.1 christos size_t len;
221 1.1 christos u_long cnt;
222 1.1 christos int isempty, key;
223 1.1 christos CHAR_T *endp, *p;
224 1.1 christos
225 1.1 christos /*
226 1.1 christos * !!!
227 1.1 christos * If it's a dot command, it doesn't reset the key for which
228 1.1 christos * we're searching, e.g. in "df1|f2|.|;", the ';' searches
229 1.1 christos * for a '2'.
230 1.1 christos */
231 1.1 christos key = vp->character;
232 1.1 christos if (!F_ISSET(vp, VC_ISDOT))
233 1.1 christos VIP(sp)->lastckey = key;
234 1.1 christos VIP(sp)->csearchdir = FSEARCH;
235 1.1 christos
236 1.1 christos if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
237 1.1 christos if (isempty)
238 1.1 christos goto empty;
239 1.1 christos return (1);
240 1.1 christos }
241 1.1 christos
242 1.1 christos if (len == 0) {
243 1.1 christos empty: notfound(sp, key);
244 1.1 christos return (1);
245 1.1 christos }
246 1.1 christos
247 1.1 christos endp = p - 1;
248 1.1 christos p += vp->m_start.cno;
249 1.1 christos for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
250 1.1 christos while (--p > endp && *p != key);
251 1.1 christos if (p == endp) {
252 1.1 christos notfound(sp, key);
253 1.1 christos return (1);
254 1.1 christos }
255 1.1 christos }
256 1.1 christos
257 1.1 christos vp->m_stop.cno = (p - endp) - 1;
258 1.1 christos
259 1.1 christos /*
260 1.1 christos * All commands move to the end of the range. Motion commands
261 1.1 christos * adjust the starting point to the character before the current
262 1.1 christos * one.
263 1.1 christos */
264 1.1 christos vp->m_final = vp->m_stop;
265 1.1 christos if (ISMOTION(vp))
266 1.1 christos --vp->m_start.cno;
267 1.1 christos return (0);
268 1.1 christos }
269 1.1 christos
270 1.1 christos static void
271 1.1 christos noprev(SCR *sp)
272 1.1 christos {
273 1.1 christos msgq(sp, M_BERR, "178|No previous F, f, T or t search");
274 1.1 christos }
275 1.1 christos
276 1.1 christos static void
277 1.1 christos notfound(SCR *sp, ARG_CHAR_T ch)
278 1.1 christos {
279 1.1 christos msgq(sp, M_BERR, "179|%s not found", KEY_NAME(sp, ch));
280 1.1 christos }
281