-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinefunc.c
More file actions
247 lines (222 loc) · 4.29 KB
/
linefunc.c
File metadata and controls
247 lines (222 loc) · 4.29 KB
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
/*
* STevie - ST editor for VI enthusiasts. ...Tim Thompson...twitch!tjt...
*/
#include "stdio.h"
#include "stevie.h"
/*
* nextline(curr)
*
* Return a pointer to the beginning of the next line after the
* 'curr' char. Return NULL if there is no next line.
*/
char *
nextline(curr)
char *curr;
{
while ( curr<Fileend ) {
if ( *curr++ == '\n' )
break;
}
if ( curr >= Fileend )
return(NULL);
return(curr);
}
/*
* prevline(curr)
*
* Return a pointer to the beginning of the previous line before the
* 'curr' char. Return NULL if there is no previous line.
*/
char *
prevline(curr)
char *curr;
{
int nnl = 0;
/* If we are currently positioned on a '\n', */
/* we are on a blank line. Adjust accordingly. */
if ( *curr == '\n' )
nnl = -1;
while ( curr>Filemem ) {
/* look for the 2nd previous newline */
if ( *curr == '\n' ) {
nnl++;
if ( nnl == 2)
break;
}
curr--;
}
if ( curr <= Filemem ) {
/* If we found 1 newline, we found the first line */
if ( nnl == 1 )
return(Filemem);
else
return(NULL);
}
/* return the first char of the previous line */
return(++curr);
}
/*
* coladvance(p,col)
*
* Try to advance to the specified column, starting at p.
*/
char *
coladvance(p,col)
char *p;
int col;
{
int c, inc;
/* If we're on a blank ('\n' only) line, we can't do anything */
if ( *p == '\n' )
return(p);
/* try to advance to the specified column */
for ( c=0; col-- > 0; c++ ) {
/* Count a tab for what it's worth */
if ( *p == '\t' ) {
inc = (7 - c%8);
col -= inc;
c += inc;
}
p++;
/* Don't go past the end of */
/* the file or the line. */
if ( p==Fileend || *p=='\n' ) {
p--;
break;
}
}
return(p);
}
char *strcpy(), *malloc();
char *
alloc(size)
unsigned size;
{
char *p; /* pointer to new storage space */
p = malloc(size);
if ( p == (char *)NULL ) { /* if there is no more room... */
message("alloc() is unable to find memory!");
}
return(p);
}
char *
strsave(string)
char *string;
{
return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
}
static char *laststr = NULL;
static int lastdir;
char *
ssearch(dir,str)
int dir; /* FORWARD or BACKWARD */
char *str;
{
if ( laststr != NULL )
free(laststr);
laststr = strsave(str);
lastdir = dir;
if ( dir == BACKWARD )
return(bcksearch(str));
else
return(fwdsearch(str));
}
dosearch(dir,str)
int dir;
char *str;
{
char *p;
if ( (p=ssearch(dir,str)) == NULL )
message("Pattern not found");
else {
char *savep;
cursupdate();
/* if we're backing up, we make sure the line we're on */
/* is on the screen. */
Curschar = savep = p;
/* get to the beginning of the line */
beginline();
if ( Curschar < Topchar )
Topchar = Curschar;
Curschar = savep;
cursupdate();
updatescreen();
}
}
repsearch()
{
if ( laststr == NULL )
beep();
else {
dosearch(lastdir,laststr);
}
}
char *
fwdsearch(str)
char *str;
{
register char *sofar = str;
register char *infile = Curschar+1;
int leng = strlen(str);
char *stopit;
/* search forward to the end of the file */
for ( ; infile < Fileend && *sofar != '\0' ; infile++ ) {
if ( *infile == *sofar )
sofar++;
else
sofar = str;
}
if ( *sofar == '\0' )
return(infile-strlen(str));
/* search from the beginning of the file to Curschar */
infile = Filemem;
sofar = str;
stopit = Curschar + leng;
for ( ; infile <= stopit && *sofar != '\0' ; infile++ ) {
if ( *infile == *sofar )
sofar++;
else
sofar = str;
}
if ( *sofar == '\0' )
return(infile-leng);
else
return(NULL);
}
char *
bcksearch(str)
char *str;
{
int leng = strlen(str);
char *infile = Curschar+1;
char *endofstr, *sofar, *stopit;
/* make sure str isn't empty before getting pointer to */
/* its last character. */
if ( leng == 0 )
return(NULL);
endofstr = &str[leng-1];
sofar = endofstr;
/* search backward to the beginning of the file */
for ( ; infile >= Filemem && sofar >= str ; infile-- ) {
if ( *infile == *sofar )
sofar--;
else
sofar = endofstr;
}
if ( sofar < str )
return(++infile);
/* search backward from the end of the file */
sofar = endofstr;
infile = Fileend-1;
stopit = Curschar - leng;
for ( ; infile >= stopit && sofar >= str ; infile-- ) {
if ( *infile == *sofar )
sofar--;
else
sofar = endofstr;
}
if ( sofar < str )
return(++infile);
else
return(NULL);
}