/
search.c
469 lines (404 loc) · 14.1 KB
/
search.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* Search and replace functions
Copyright (c) 1997-2012 Free Software Foundation, Inc.
Copyright (c) 2012 Michael L. Gran
This file is part of Michael Gran's unofficial fork of GNU Zile.
GNU Zile is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Zile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Zile; see the file COPYING. If not, write to the
Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
MA 02111-1301, USA. */
#include <config.h>
#include <libguile.h>
#include <stdlib.h>
#include <ctype.h>
#include <regex.h>
#include "main.h"
#include "extern.h"
/* Return true if there are no upper-case letters in the given string.
If `regex' is true, ignore escaped characters. */
static bool
no_upper (const char *s, size_t len, int regex)
{
int quote_flag = 0;
for (size_t i = 0; i < len; i++)
{
if (regex && s[i] == '\\')
quote_flag = !quote_flag;
else if (!quote_flag && isupper ((int) s[i]))
return false;
}
return true;
}
static const char *re_find_err = NULL;
static int
find_substr (castr as1, castr as2, const char *n, size_t nsize, size_t from, size_t to,
bool forward, bool notbol, bool noteol, bool regex, bool icase)
{
int ret = -1;
struct re_pattern_buffer pattern;
struct re_registers search_regs;
reg_syntax_t syntax = RE_SYNTAX_EMACS;
memset (&pattern, 0, sizeof (pattern));
/* if (!regex)
syntax |= RE_PLAIN; */
if (icase)
syntax |= RE_ICASE;
re_set_syntax (syntax);
search_regs.num_regs = 1;
re_find_err = re_compile_pattern (n, (int) nsize, &pattern);
pattern.not_bol = notbol;
pattern.not_eol = noteol;
if (!re_find_err)
/* FIXME: The current implementation memcpys the two strings into
a freshly malloced block, i.e. is horribly inefficient for
large buffers. */
ret = re_search_2 (&pattern,
astr_cstr (as1), (int) astr_len (as1),
astr_cstr (as2), (int) astr_len (as2),
forward ? from : to - 1,
forward ? (to - from) : -(to - 1 - from),
&search_regs,
(int) astr_len (as1) + astr_len (as2));
if (ret >= 0)
ret = forward ? search_regs.end[0] : ret;
return ret;
}
static bool
search (size_t o, const char *s, int forward, int regexp)
{
size_t ssize = strlen (s);
if (ssize < 1)
return false;
/* Attempt match. */
bool notbol = forward ? o > 0 : false;
bool noteol = forward ? false : o < get_buffer_size (cur_bp);
size_t from = forward ? o : 0;
size_t to = forward ? get_buffer_size (cur_bp) : o;
int pos = find_substr (get_buffer_pre_point (cur_bp), get_buffer_post_point (cur_bp),
s, ssize, from, to, forward, notbol, noteol, regexp,
get_variable_bool ("case-fold-search") && no_upper (s, ssize, regexp));
if (pos < 0)
return false;
goto_offset (pos);
thisflag |= FLAG_NEED_RESYNC;
return true;
}
static castr last_search = NULL;
static SCM
do_search (bool forward, bool regexp, castr pattern)
{
SCM ok = SCM_BOOL_F;
if (pattern == NULL)
pattern = minibuf_read ("%s%s: ", last_search ? astr_cstr (last_search) : NULL,
regexp ? "RE search" : "Search", forward ? "" : " backward");
if (pattern == NULL)
return G_keyboard_quit ();
if (astr_len (pattern) != 0)
{
last_search = pattern;
if (!search (get_buffer_pt (cur_bp), astr_cstr (pattern), forward, regexp))
minibuf_error ("Search failed: \"%s\"", pattern);
else
ok = SCM_BOOL_T;
}
return ok;
}
SCM_DEFINE (G_search_forward, "search-forward", 1, 0, 0,
(SCM gpattern), "\
Search forward from point for the user specified text.")
{
char *pattern = guile_to_locale_string_safe (gpattern);
return do_search (true, false, astr_new_cstr (pattern));
}
SCM_DEFINE (G_search_backward, "search-backward", 1, 0, 0,
(SCM gpattern), "\
Search backward from point for the user specified text.")
{
char *pattern = guile_to_locale_string_safe (gpattern);
return do_search (false, false, astr_new_cstr (pattern));
}
SCM_DEFINE (G_search_forward_regexp, "search-forward-regexp", 1, 0, 0,
(SCM gpattern), "\
Search forward from point for regular expression REGEXP.")
{
char *pattern = guile_to_locale_string_safe (gpattern);
return do_search (true, true, astr_new_cstr (pattern));
}
SCM_DEFINE (G_search_backward_regexp, "search-backward-regexp", 1, 0, 0,
(SCM gpattern), "\
Search backward from point for match for regular expression REGEXP.")
{
char *pattern = guile_to_locale_string_safe (gpattern);
return do_search (false, true, astr_new_cstr(pattern));
}
/*
* Incremental search engine.
*/
static SCM
isearch (int forward, int regexp)
{
Marker *old_mark = copy_marker (get_buffer_mark (get_window_bp (cur_wp)));
set_buffer_isearch (get_window_bp (cur_wp), true);
int last = true;
astr pattern = astr_new ();
size_t start = get_buffer_pt (cur_bp), cur = start;
for (;;)
{
/* Make the minibuf message. */
astr buf = astr_fmt ("%sI-search%s: %s",
(last ?
(regexp ? "Regexp " : "") :
(regexp ? "Failing regexp " : "Failing ")),
forward ? "" : " backward",
astr_cstr (pattern));
/* Regex error. */
if (re_find_err)
{
if ((strncmp (re_find_err, "Premature ", 10) == 0) ||
(strncmp (re_find_err, "Unmatched ", 10) == 0) ||
(strncmp (re_find_err, "Invalid ", 8) == 0))
{
re_find_err = "incomplete input";
}
astr_cat (buf, astr_fmt (" [%s]", re_find_err));
re_find_err = NULL;
}
minibuf_write ("%s", astr_cstr (buf));
int c = getkey (GETKEY_DEFAULT);
if (c == KBD_CANCEL)
{
goto_offset (start);
thisflag |= FLAG_NEED_RESYNC;
/* Quit. */
G_keyboard_quit ();
/* Restore old mark position. */
if (get_buffer_mark (cur_bp))
unchain_marker (get_buffer_mark (cur_bp));
set_buffer_mark (cur_bp, copy_marker (old_mark));
break;
}
else if (c == KBD_BS)
{
if (astr_len (pattern) > 0)
{
astr_truncate (pattern, astr_len (pattern) - 1);
cur = start;
goto_offset (start);
thisflag |= FLAG_NEED_RESYNC;
}
else
ding ();
}
else if (c & KBD_CTRL && (c & 0xff) == 'q')
{
minibuf_write ("%s^Q-", astr_cstr (buf));
astr_cat_char (pattern, getkey_unfiltered (GETKEY_DEFAULT));
}
else if (c & KBD_CTRL && ((c & 0xff) == 'r' || (c & 0xff) == 's'))
{
/* Invert direction. */
if ((c & 0xff) == 'r')
forward = false;
else if ((c & 0xff) == 's')
forward = true;
if (astr_len (pattern) > 0)
{
/* Find next match. */
cur = get_buffer_pt (cur_bp);
/* Save search string. */
last_search = astr_cpy (astr_new (), pattern);
}
else if (last_search != NULL)
astr_cpy (pattern, last_search);
}
else if (c & KBD_META || c & KBD_CTRL || c > KBD_TAB)
{
if (c == KBD_RET && astr_len (pattern) == 0)
do_search (forward, regexp, NULL);
else
{
if (astr_len (pattern) > 0)
{
/* Save mark. */
set_mark ();
set_marker_o (get_buffer_mark (cur_bp), start);
/* Save search string. */
last_search = astr_cpy (astr_new (), pattern);
minibuf_write ("Mark saved when search started");
}
else
minibuf_clear ();
if (c != KBD_RET)
ungetkey (c);
}
break;
}
else
astr_cat_char (pattern, c);
if (astr_len (pattern) > 0)
last = search (cur, astr_cstr (pattern), forward, regexp);
else
last = true;
if (thisflag & FLAG_NEED_RESYNC)
{
//resync_redisplay (cur_wp);
term_redisplay ();
}
}
/* done */
set_buffer_isearch (get_window_bp (cur_wp), false);
if (old_mark)
unchain_marker (old_mark);
return SCM_BOOL_T;
}
SCM_DEFINE (G_isearch_forward, "isearch-forward", 0, 0, 0, (void), "\
Do incremental search forward.\n\
With a prefix argument, do an incremental regular expression search instead.\n\
As you type characters, they add to the search string and are found.\n\
Type return to exit, leaving point at location found.\n\
Type C-s to search again forward, C-r to search again backward.\n\
C-g when search is successful aborts and moves point to starting point.")
{
return isearch (true, lastflag & FLAG_SET_UNIARG);
}
SCM_DEFINE (G_isearch_backward, "isearch-backward", 0, 0, 0, (void), "\
Do incremental search backward.\n\
With a prefix argument, do a regular expression search instead.\n\
As you type characters, they add to the search string and are found.\n\
Type return to exit, leaving point at location found.\n\
Type @kbd{C-r} to search again backward, @kbd{C-s} to search again forward.\n\
@kbd{C-g} when search is successful aborts and moves point to starting point.")
{
return isearch (false, lastflag & FLAG_SET_UNIARG);
}
SCM_DEFINE (G_isearch_forward_regexp, "isearch-forward-regexp", 0, 0, 0, (void), "\
Do incremental search forward for regular expression.\n\
With a prefix argument, do a regular string search instead.\n\
Like ordinary incremental search except that your input\n\
is treated as a regexp. See @kbd{M-x isearch-forward} for more info.")
{
return isearch (true, !(lastflag & FLAG_SET_UNIARG));
}
SCM_DEFINE (G_isearch_backward_regexp, "isearch-backward-regexp", 0, 0, 0, (void), "\
Do incremental search forward for regular expression.\n\
With a prefix argument, do a regular string search instead.\n\
Like ordinary incremental search except that your input\n\
is treated as a regexp. See @kbd{M-x isearch-forward} for more info.")
{
return isearch (false, !(lastflag & FLAG_SET_UNIARG));
}
/*
* Check the case of a string.
* Returns 2 if it is all upper case, 1 if just the first letter is,
* and 0 otherwise.
*/
static int
check_case (astr as)
{
size_t i;
for (i = 0; i < astr_len (as) && isupper ((int) astr_get (as, i)); i++)
;
if (i == astr_len (as))
return 2;
else if (i == 1)
for (; i < astr_len (as) && !isupper ((int) astr_get (as, i)); i++)
;
return i == astr_len (as);
}
SCM_DEFINE (G_query_replace, "query-replace", 0, 0, 0, (void), "\
Replace occurrences of a string with other text.\n\
As each match is found, the user must type a character saying\n\
what to do with it.")
{
bool ok;
castr find = minibuf_read ("Query replace string: ", "");
if (find == NULL)
return G_keyboard_quit ();
if (astr_len (find) == 0)
return SCM_BOOL_F;
bool find_no_upper = no_upper (astr_cstr (find), astr_len (find), false);
castr repl = minibuf_read ("Query replace `%s' with: ", "", astr_cstr (find));
if (repl == NULL)
return G_keyboard_quit ();
bool noask = false;
size_t count = 0;
while (search (get_buffer_pt (cur_bp), astr_cstr (find), true, false))
{
int c = ' ';
if (!noask)
{
if (thisflag & FLAG_NEED_RESYNC)
window_resync (cur_wp);
for (;;)
{
minibuf_write
("Query replacing `%s' with `%s' (y, n, !, ., q)? ", astr_cstr (find),
astr_cstr (repl));
c = getkey (GETKEY_DEFAULT);
if (c == KBD_CANCEL || c == KBD_RET || c == ' ' || c == 'y'
|| c == 'n' || c == 'q' || c == '.' || c == '!')
break;
/* FIXME: Remove this prompt (see Lua Zile) */
minibuf_error ("Please answer y, n, !, . or q.");
waitkey ();
}
minibuf_clear ();
if (c == 'q') /* Quit immediately. */
break;
else if (c == KBD_CANCEL) /* C-g */
{
ok = G_keyboard_quit ();
break;
}
else if (c == '!') /* Replace all without asking. */
noask = true;
else if (c == 'n' || c == KBD_RET || c == KBD_DEL) /* Do not replace. */
continue;
}
/* Perform replacement. */
++count;
castr case_repl = repl;
Region r = region_new (get_buffer_pt (cur_bp) - astr_len (find), get_buffer_pt (cur_bp));
if (find_no_upper && get_variable_bool ("case-replace"))
{
int case_type = check_case (get_buffer_region (cur_bp, r).as);
if (case_type != 0)
case_repl = astr_recase (astr_cpy (astr_new (), repl),
case_type == 1 ? case_capitalized : case_upper);
}
Marker *m = point_marker ();
goto_offset (r.start);
replace_estr (astr_len (find), estr_new_astr (case_repl));
goto_offset (get_marker_o (m));
unchain_marker (m);
if (c == '.') /* Replace and quit. */
break;
}
if (thisflag & FLAG_NEED_RESYNC)
window_resync (cur_wp);
if (ok)
minibuf_write ("Replaced %d occurrences", count);
return scm_from_bool (ok);
}
void
init_guile_search_procedures (void)
{
#include "search.x"
scm_c_export ("search-forward",
"search-backward",
"search-forward-regexp",
"search-backward-regexp",
"isearch-forward",
"isearch-backward",
"isearch-forward-regexp",
"isearch-backward-regexp",
"query-replace",
NULL);
}