-
Notifications
You must be signed in to change notification settings - Fork 0
/
move2.c
327 lines (291 loc) · 6.4 KB
/
move2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* move2.c */
/* Author:
* Steve Kirkendall
* 1500 SW Park #326
* Portland OR, 97201
*/
/* This function contains the movement functions that perform RE searching */
#include "config.h"
#include "vi.h"
#include "regexp.h"
#include <string.h>
static regexp *re; /* compiled version of the pattern to search for */
static prevsf; /* boolean: previous search direction was forward? */
#ifndef NO_EXTENSIONS
/*ARGSUSED*/
MARK m_wsrch(word, m, cnt)
char *word; /* the word to search for */
MARK m; /* the starting point */
int cnt; /* ignored */
{
char buffer[WSRCH_MAX + 6];
/* wrap \< and \> around the word */
strncpy(buffer, "/\\<", WSRCH_MAX + 6);
strncat(buffer, word, WSRCH_MAX);
strcat(buffer, "\\>");
/* show the searched-for word on the bottom line */
move(LINES - 1, 0);
qaddstr(buffer);
clrtoeol();
refresh();
/* search for the word */
return m_fsrch(m, buffer);
}
#endif
MARK m_nsrch(m, cnt, cmd)
MARK m; /* where to start searching */
long cnt; /* number of searches to do */
int cmd; /* command character -- 'n' or 'N' */
{
int oldprevsf; /* original value of prevsf, so we can fix any changes */
DEFAULT(1L);
/* clear the bottom line. In particular, we want to loose any
* "(wrapped)" notice.
*/
move(LINES - 1, 0);
clrtoeol();
/* if 'N' command, then invert the "prevsf" variable */
oldprevsf = prevsf;
if (cmd == 'N')
{
prevsf = !prevsf;
}
/* search forward if prevsf -- i.e., if previous search was forward */
while (--cnt >= 0L && m != MARK_UNSET)
{
if (prevsf)
{
m = m_fsrch(m, (char *)0);
}
else
{
m = m_bsrch(m, (char *)0);
}
}
/* restore the old value of prevsf -- if cmd=='N' then it was inverted,
* and the m_fsrch() and m_bsrch() functions force it to a (possibly
* incorrect) value. The value of prevsf isn't supposed to be changed
* at all here!
*/
prevsf = oldprevsf;
return m;
}
MARK m_fsrch(m, ptrn)
MARK m; /* where to start searching */
char *ptrn; /* pattern to search for */
{
long l; /* line# of line to be searched */
char *line; /* text of line to be searched */
int wrapped;/* boolean: has our search wrapped yet? */
int pos; /* where we are in the line */
#ifndef CRUNCH
long delta = INFINITY;/* line offset, for things like "/foo/+1" */
#endif
/* remember: "previous search was forward" */
prevsf = TRUE;
if (ptrn && *ptrn)
{
/* locate the closing '/', if any */
line = parseptrn(ptrn);
#ifndef CRUNCH
if (*line)
{
delta = atol(line);
}
#endif
ptrn++;
/* free the previous pattern */
if (re) _free_(re);
/* compile the pattern */
re = regcomp(ptrn);
if (!re)
{
return MARK_UNSET;
}
}
else if (!re)
{
msg("No previous expression");
return MARK_UNSET;
}
/* search forward for the pattern */
pos = markidx(m) + 1;
pfetch(markline(m));
if (pos >= plen)
{
pos = 0;
m = (m | (BLKSIZE - 1)) + 1;
}
wrapped = FALSE;
for (l = markline(m); l != markline(m) + 1 || !wrapped; l++)
{
/* wrap search */
if (l > nlines)
{
/* if we wrapped once already, then the search failed */
if (wrapped)
{
break;
}
/* else maybe we should wrap now? */
if (*o_wrapscan)
{
l = 0;
wrapped = TRUE;
continue;
}
else
{
break;
}
}
/* get this line */
line = fetchline(l);
/* check this line */
if (regexec(re, &line[pos], (pos == 0)))
{
/* match! */
pos = (int)(re->startp[0] - line);
if (IsHIdx(line, pos) == HAN_SECOND)
{ /* bogus match, from 2nd byte! */
pos++;
continue;
}
if (wrapped && *o_warn)
msg("(wrapped)");
#ifndef CRUNCH
if (delta != INFINITY)
{
l += delta;
if (l < 1 || l > nlines)
{
msg("search offset too big");
return MARK_UNSET;
}
force_flags = LNMD|INCL;
return MARK_AT_LINE(l);
}
#endif
if (re->leavep)
return MARK_AT_LINE(l) + (int)(re->leavep - line);
else
return MARK_AT_LINE(l) + (int)(re->startp[0] - line);
}
pos = 0;
}
/* not found */
#ifdef DEBUG
msg("/%s/ not found", ptrn);
#else
msg(*o_wrapscan ? "Not found" : "Hit bottom without finding RE");
#endif
return MARK_UNSET;
}
MARK m_bsrch(m, ptrn)
MARK m; /* where to start searching */
char *ptrn; /* pattern to search for */
{
long l; /* line# of line to be searched */
char *line; /* text of line to be searched */
int wrapped;/* boolean: has our search wrapped yet? */
int pos; /* last acceptable idx for a match on this line */
int last; /* remembered idx of the last acceptable match on this line */
int try; /* an idx at which we strat searching for another match */
#ifndef CRUNCH
long delta = INFINITY;/* line offset, for things like "/foo/+1" */
#endif
/* remember: "previous search was not forward" */
prevsf = FALSE;
if (ptrn && *ptrn)
{
/* locate the closing '?', if any */
line = parseptrn(ptrn);
#ifndef CRUNCH
if (*line)
{
delta = atol(line);
}
#endif
ptrn++;
/* free the previous pattern, if any */
if (re) _free_(re);
/* compile the pattern */
re = regcomp(ptrn);
if (!re)
{
return MARK_UNSET;
}
}
else if (!re)
{
msg("No previous expression");
return MARK_UNSET;
}
/* search backward for the pattern */
pos = markidx(m);
wrapped = FALSE;
for (l = markline(m); l != markline(m) - 1 || !wrapped; l--)
{
/* wrap search */
if (l < 1)
{
if (*o_wrapscan)
{
l = nlines + 1;
wrapped = TRUE;
continue;
}
else
{
break;
}
}
/* get this line */
line = fetchline(l);
/* check this line */
if (regexec(re, line, 1) && (int)(re->startp[0] - line) < pos)
{
/* match! now find the last acceptable one in this line */
do
{
if (re->leavep)
last = (int)(re->leavep - line);
else
last = (int)(re->startp[0] - line);
try = (int)(re->endp[0] - line);
} while (try > 0
&& regexec(re, &line[try], FALSE)
&& (int)(re->startp[0] - line) < pos);
if (IsHIdx(line, last) == HAN_SECOND)
{ /* bogus match, from 2nd byte! */
pos = last;
continue;
}
if (wrapped && *o_warn)
msg("(wrapped)");
#ifndef CRUNCH
if (delta != INFINITY)
{
l += delta;
if (l < 1 || l > nlines)
{
msg("search offset too big");
return MARK_UNSET;
}
force_flags = LNMD|INCL;
return MARK_AT_LINE(l);
}
#endif
return MARK_AT_LINE(l) + last;
}
pos = BLKSIZE;
}
/* not found */
#ifdef DEBUG
msg("?%s? not found", ptrn);
#else
msg(*o_wrapscan ? "Not found" : "Hit bottom without finding RE");
#endif
return MARK_UNSET;
}