Add regular expression support for the format search, match and

substitute modifiers.
This commit is contained in:
nicm
2019-06-13 19:46:00 +00:00
parent 7e6a26cc9d
commit 3e72e98e3b
6 changed files with 233 additions and 61 deletions

View File

@ -22,6 +22,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <regex.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
@ -1206,24 +1207,41 @@ window_pane_visible(struct window_pane *wp)
}
u_int
window_pane_search(struct window_pane *wp, const char *searchstr)
window_pane_search(struct window_pane *wp, const char *term, int regex,
int ignore)
{
struct screen *s = &wp->base;
char *newsearchstr, *line;
regex_t r;
char *new = NULL, *line;
u_int i;
int flags = 0, found;
xasprintf(&newsearchstr, "*%s*", searchstr);
if (!regex) {
if (ignore)
flags |= FNM_CASEFOLD;
xasprintf(&new, "*%s*", term);
} else {
if (ignore)
flags |= REG_ICASE;
if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
return (0);
}
for (i = 0; i < screen_size_y(s); i++) {
line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
if (fnmatch(newsearchstr, line, 0) == 0) {
free(line);
break;
}
if (!regex)
found = (fnmatch(new, line, 0) == 0);
else
found = (regexec(&r, line, 0, NULL, 0) == 0);
free(line);
if (found)
break;
}
if (!regex)
free(new);
else
regfree(&r);
free(newsearchstr);
if (i == screen_size_y(s))
return (0);
return (i + 1);