-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.c
More file actions
192 lines (171 loc) · 4.35 KB
/
search.c
File metadata and controls
192 lines (171 loc) · 4.35 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
#include "me.h"
static int readpattern(char *prompt, char *apat);
static int nextch(struct line **pcurline, int *pcuroff, int dir);
static int delins(int dlength, char *instr, int use_meta);
/* The argument "bc" comes from the buffer, and "pc" comes from the pattern. */
static inline int eq(unsigned char bc, unsigned char pc)
{
if (!exact_search)
return ensure_upper(bc) == ensure_upper(pc);
return bc == pc;
}
/* Switches between case-sensitive and case-insensitive. */
int toggle_exact_search(int f, int n)
{
struct window *wp;
exact_search = !exact_search;
for_each_wind(wp)
curwp->w_flag |= WFMODE;
return TRUE;
}
/*
Searches for a pattern in either direction. If found, reset the "." to be at
the start or just after the match string, and (perhaps) repaint the display.
*/
int search_next(const char *pattern, int direct, int beg_or_end)
{
struct line *curline = curwp->w_dotp, *scanline, *matchline;
int curoff = curwp->w_doto, scanoff, matchoff;
const char *patptr;
int c;
beg_or_end ^= direct;
loop:
matchline = curline;
matchoff = curoff;
if ((c = nextch(&curline, &curoff, direct)) == -1)
return FALSE;
if (!eq(c, pattern[0]))
goto loop;
scanline = curline;
scanoff = curoff;
patptr = pattern;
while (*++patptr != '\0') {
if ((c = nextch(&scanline, &scanoff, direct)) == -1)
return FALSE;
if (!eq(c, *patptr))
goto loop;
}
/* A SUCCESSFULL MATCH!!! */
if (beg_or_end == PTEND) {
curwp->w_dotp = scanline;
curwp->w_doto = scanoff;
} else {
curwp->w_dotp = matchline;
curwp->w_doto = matchoff;
}
curwp->w_flag |= WFMOVE;
return TRUE;
}
/* If the user did not give a string, use the old one (if there is one) */
static int readpattern(char *prompt, char *apat)
{
char tpat[NPAT];
int status;
status = mlgetstring(tpat, NPAT, ENTERC, "%s (%s): ", prompt, apat);
if (status == TRUE)
strcpy(apat, tpat);
else if (status == FALSE && apat[0] != 0) /* Use old pattern */
status = TRUE;
return status;
}
/* Replacement buffer can be empty, which deletes the replacement content */
int clear_rpat(int f, int n)
{
rpat[0] = '\0';
mlwrite("Replacement buffer is cleared");
return TRUE;
}
/*
Deletes a specified length from the current point then either insert the string
directly or make use of replacement meta-array.
*/
static int delins(int dlength, char *instr, int use_meta)
{
int status;
if ((status = ldelete((long)dlength, FALSE)) == TRUE)
status = linstr(instr);
return status;
}
/* Queries and searches for a string and replace it with another string. */
int qreplace(int f, int n)
{
int numsub, nummatch, interactive, status, dlength, c = 0;
if ((status = readpattern("Query replace", pat)) != TRUE)
return status;
if ((status = readpattern("with", rpat)) == ABORT)
return status;
dlength = strlen(pat);
numsub = 0;
nummatch = 0;
interactive = 1;
replace_loop:
if (curwp->w_dotp == curwp->w_bufp->b_linep)
goto loop_done;
if (!search_next(pat, FORWARD, PTBEG))
goto loop_done;
++nummatch;
if (!interactive)
goto do_replace;
mlwrite("Replace (%s) with (%s)? ", pat, rpat);
qprompt:
update(TRUE);
switch ((c = tgetc())) {
case '!':
interactive = 0;
/* fall through */
case ' ':
goto do_replace;
case 'n':
forwchar(FALSE, 1);
goto replace_loop;
case 0x07: /* ASCII code of ^G is 0x07 ('\a') */
goto finish;
default:
ansibeep();
mlwrite("(SPACE)Ok, (N)ext, (!)All, (^G)Done");
goto qprompt;
}
do_replace:
if ((status = delins(dlength, rpat, TRUE)) != TRUE)
return status;
++numsub;
goto replace_loop;
loop_done:
/* If it's the last one that we say `n`, back a char */
if (c == 'n')
backchar(FALSE, 1);
finish:
mlwrite("%d substitutions (in %d matches)", numsub, nummatch);
return TRUE;
}
/* Returns -1 when it hits the boundry (start of buffer or end of buffer) */
static int nextch(struct line **pcurline, int *pcuroff, int dir)
{
struct line *b_linep = curwp->w_bufp->b_linep;
struct line *curline = *pcurline;
int curoff = *pcuroff, c;
if (dir == FORWARD) {
if (curline == b_linep)
return -1;
if (curoff == curline->l_used) {
curline = curline->l_fp;
curoff = 0;
c = '\n';
} else {
c = curline->l_text[curoff++];
}
} else {
if (curoff == 0) {
if (curline->l_bp == b_linep)
return -1;
curline = curline->l_bp;
curoff = curline->l_used;
c = '\n';
} else {
c = curline->l_text[--curoff];
}
}
*pcurline = curline;
*pcuroff = curoff;
return c;
}