Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_ch.c
blobafaa770d5bb5cf030787d1e45fdd596954cb63db
1 /* $NetBSD$ */
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.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 static void notfound __P((SCR *, ARG_CHAR_T));
31 static void noprev __P((SCR *));
34 * v_chrepeat -- [count];
35 * Repeat the last F, f, T or t search.
37 * PUBLIC: int v_chrepeat __P((SCR *, VICMD *));
39 int
40 v_chrepeat(SCR *sp, VICMD *vp)
42 vp->character = VIP(sp)->lastckey;
44 switch (VIP(sp)->csearchdir) {
45 case CNOTSET:
46 noprev(sp);
47 return (1);
48 case FSEARCH:
49 return (v_chF(sp, vp));
50 case fSEARCH:
51 return (v_chf(sp, vp));
52 case TSEARCH:
53 return (v_chT(sp, vp));
54 case tSEARCH:
55 return (v_cht(sp, vp));
56 default:
57 abort();
59 /* NOTREACHED */
63 * v_chrrepeat -- [count],
64 * Repeat the last F, f, T or t search in the reverse direction.
66 * PUBLIC: int v_chrrepeat __P((SCR *, VICMD *));
68 int
69 v_chrrepeat(SCR *sp, VICMD *vp)
71 cdir_t savedir;
72 int rval;
74 vp->character = VIP(sp)->lastckey;
75 savedir = VIP(sp)->csearchdir;
77 switch (VIP(sp)->csearchdir) {
78 case CNOTSET:
79 noprev(sp);
80 return (1);
81 case FSEARCH:
82 rval = v_chf(sp, vp);
83 break;
84 case fSEARCH:
85 rval = v_chF(sp, vp);
86 break;
87 case TSEARCH:
88 rval = v_cht(sp, vp);
89 break;
90 case tSEARCH:
91 rval = v_chT(sp, vp);
92 break;
93 default:
94 abort();
96 VIP(sp)->csearchdir = savedir;
97 return (rval);
101 * v_cht -- [count]tc
102 * Search forward in the line for the character before the next
103 * occurrence of the specified character.
105 * PUBLIC: int v_cht __P((SCR *, VICMD *));
108 v_cht(SCR *sp, VICMD *vp)
110 if (v_chf(sp, vp))
111 return (1);
114 * v_chf places the cursor on the character, where the 't'
115 * command wants it to its left. We know this is safe since
116 * we had to move right for v_chf() to have succeeded.
118 --vp->m_stop.cno;
121 * Make any necessary correction to the motion decision made
122 * by the v_chf routine.
124 if (!ISMOTION(vp))
125 vp->m_final = vp->m_stop;
127 VIP(sp)->csearchdir = tSEARCH;
128 return (0);
132 * v_chf -- [count]fc
133 * Search forward in the line for the next occurrence of the
134 * specified character.
136 * PUBLIC: int v_chf __P((SCR *, VICMD *));
139 v_chf(SCR *sp, VICMD *vp)
141 size_t len;
142 u_long cnt;
143 int isempty, key;
144 CHAR_T *endp, *p, *startp;
147 * !!!
148 * If it's a dot command, it doesn't reset the key for which we're
149 * searching, e.g. in "df1|f2|.|;", the ';' searches for a '2'.
151 key = vp->character;
152 if (!F_ISSET(vp, VC_ISDOT))
153 VIP(sp)->lastckey = key;
154 VIP(sp)->csearchdir = fSEARCH;
156 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
157 if (isempty)
158 goto empty;
159 return (1);
162 if (len == 0) {
163 empty: notfound(sp, key);
164 return (1);
167 endp = (startp = p) + len;
168 p += vp->m_start.cno;
169 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
170 while (++p < endp && *p != key);
171 if (p == endp) {
172 notfound(sp, key);
173 return (1);
177 vp->m_stop.cno = p - startp;
180 * Non-motion commands move to the end of the range.
181 * Delete and yank stay at the start, ignore others.
183 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
184 return (0);
188 * v_chT -- [count]Tc
189 * Search backward in the line for the character after the next
190 * occurrence of the specified character.
192 * PUBLIC: int v_chT __P((SCR *, VICMD *));
195 v_chT(SCR *sp, VICMD *vp)
197 if (v_chF(sp, vp))
198 return (1);
201 * v_chF places the cursor on the character, where the 'T'
202 * command wants it to its right. We know this is safe since
203 * we had to move left for v_chF() to have succeeded.
205 ++vp->m_stop.cno;
206 vp->m_final = vp->m_stop;
208 VIP(sp)->csearchdir = TSEARCH;
209 return (0);
213 * v_chF -- [count]Fc
214 * Search backward in the line for the next occurrence of the
215 * specified character.
217 * PUBLIC: int v_chF __P((SCR *, VICMD *));
220 v_chF(SCR *sp, VICMD *vp)
222 size_t len;
223 u_long cnt;
224 int isempty, key;
225 CHAR_T *endp, *p;
228 * !!!
229 * If it's a dot command, it doesn't reset the key for which
230 * we're searching, e.g. in "df1|f2|.|;", the ';' searches
231 * for a '2'.
233 key = vp->character;
234 if (!F_ISSET(vp, VC_ISDOT))
235 VIP(sp)->lastckey = key;
236 VIP(sp)->csearchdir = FSEARCH;
238 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
239 if (isempty)
240 goto empty;
241 return (1);
244 if (len == 0) {
245 empty: notfound(sp, key);
246 return (1);
249 endp = p - 1;
250 p += vp->m_start.cno;
251 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt--;) {
252 while (--p > endp && *p != key);
253 if (p == endp) {
254 notfound(sp, key);
255 return (1);
259 vp->m_stop.cno = (p - endp) - 1;
262 * All commands move to the end of the range. Motion commands
263 * adjust the starting point to the character before the current
264 * one.
266 vp->m_final = vp->m_stop;
267 if (ISMOTION(vp))
268 --vp->m_start.cno;
269 return (0);
272 static void
273 noprev(SCR *sp)
275 msgq(sp, M_BERR, "178|No previous F, f, T or t search");
278 static void
279 notfound(SCR *sp, ARG_CHAR_T ch)
281 msgq(sp, M_BERR, "179|%s not found", KEY_NAME(sp, ch));