-
Notifications
You must be signed in to change notification settings - Fork 1
/
move2.c
238 lines (209 loc) · 4.34 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
/* move2.c */
/* Author:
* Steve Kirkendall
* 14407 SW Teal Blvd. #C
* Beaverton, OR 97005
* kirkenda@cs.pdx.edu
*/
/* This function contains the movement functions that perform RE searching */
#include "config.h"
#include "vi.h"
#include "regexp.h"
extern long atol();
static regexp *re; /* compiled version of the pattern to search for */
static prevsf; /* boolean: previous search direction was forward? */
MARK m_nsrch(m)
MARK m; /* where to start searching */
{
if (prevsf)
{
m = m_fsrch(m, (char *)0);
prevsf = TRUE;
}
else
{
m = m_bsrch(m, (char *)0);
prevsf = FALSE;
}
return m;
}
MARK m_Nsrch(m)
MARK m; /* where to start searching */
{
if (prevsf)
{
m = m_bsrch(m, (char *)0);
prevsf = TRUE;
}
else
{
m = m_fsrch(m, (char *)0);
prevsf = FALSE;
}
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 */
long delta; /* line offset, for things like "/foo/+1" */
/* remember: "previous search was forward" */
prevsf = TRUE;
delta = 0L;
if (ptrn && *ptrn)
{
/* locate the closing '/', if any */
line = parseptrn(ptrn);
if (*line)
{
delta = atol(line);
}
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! */
if (wrapped && *o_warn)
msg("(wrapped)");
if (delta != 0L)
{
l += delta;
if (l < 1 || l > nlines)
{
msg("search offset too big");
return MARK_UNSET;
}
return m_front(MARK_AT_LINE(l), 0L);
}
return MARK_AT_LINE(l) + (int)(re->startp[0] - line);
}
pos = 0;
}
/* not found */
msg(*o_wrapscan ? "Not found" : "Hit bottom without finding RE");
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 */
/* remember: "previous search was not forward" */
prevsf = FALSE;
if (ptrn && *ptrn)
{
/* locate the closing '?', if any */
line = parseptrn(ptrn);
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
{
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 (wrapped && *o_warn)
msg("(wrapped)");
return MARK_AT_LINE(l) + last;
}
pos = BLKSIZE;
}
/* not found */
msg(*o_wrapscan ? "Not found" : "Hit top without finding RE");
return MARK_UNSET;
}