vi: make regexp search case-insensitive if ":set ignorecase" is active

Reported by Dan Moinescu <dan@moinescu.net>.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-04-21 15:51:41 +02:00
parent d96ffda62e
commit 264f37363d

View File

@ -1707,65 +1707,53 @@ static char *new_screen(int ro, int co)
// search for pattern starting at p // search for pattern starting at p
static char *char_search(char *p, const char *pat, int dir, int range) static char *char_search(char *p, const char *pat, int dir, int range)
{ {
char *q;
struct re_pattern_buffer preg; struct re_pattern_buffer preg;
const char *err;
char *q;
int i; int i;
int size; int size;
re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
preg.translate = 0; if (ignorecase)
preg.fastmap = 0; re_syntax_options = RE_SYNTAX_POSIX_EXTENDED | RE_ICASE;
preg.buffer = 0;
preg.allocated = 0; memset(&preg, 0, sizeof(preg));
err = re_compile_pattern(pat, strlen(pat), &preg);
if (err != NULL) {
status_line_bold("bad search pattern '%s': %s", pat, err);
return p;
}
// assume a LIMITED forward search // assume a LIMITED forward search
q = next_line(p);
q = end_line(q);
q = end - 1; q = end - 1;
if (dir == BACK) { if (dir == BACK)
q = prev_line(p);
q = text; q = text;
}
// count the number of chars to search over, forward or backward
size = q - p;
if (size < 0)
size = p - q;
// RANGE could be negative if we are searching backwards // RANGE could be negative if we are searching backwards
range = q - p; range = q - p;
q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
if (q != 0) {
// The pattern was not compiled
status_line_bold("bad search pattern: '%s': %s", pat, q);
i = 0; // return p if pattern not compiled
goto cs1;
}
q = p; q = p;
size = range;
if (range < 0) { if (range < 0) {
size = -size;
q = p - size; q = p - size;
if (q < text) if (q < text)
q = text; q = text;
} }
// search for the compiled pattern, preg, in p[] // search for the compiled pattern, preg, in p[]
// range < 0- search backward // range < 0: search backward
// range > 0- search forward // range > 0: search forward
// 0 < start < size // 0 < start < size
// re_search() < 0 not found or error // re_search() < 0: not found or error
// re_search() > 0 index of found pattern // re_search() >= 0: index of found pattern
// struct pattern char int int int struct reg // struct pattern char int int int struct reg
// re_search (*pattern_buffer, *string, size, start, range, *regs) // re_search(*pattern_buffer, *string, size, start, range, *regs)
i = re_search(&preg, q, size, 0, range, 0); i = re_search(&preg, q, size, /*start:*/ 0, range, /*struct re_registers*:*/ NULL);
if (i == -1) { regfree(&preg);
p = 0; if (i < 0)
i = 0; // return NULL if pattern not found return NULL;
} if (dir == FORWARD)
cs1:
if (dir == FORWARD) {
p = p + i; p = p + i;
} else { else
p = p - i; p = p - i;
}
return p; return p;
} }
@ -1790,7 +1778,7 @@ static char *char_search(char *p, const char *pat, int dir, int range)
len = strlen(pat); len = strlen(pat);
if (dir == FORWARD) { if (dir == FORWARD) {
stop = end - 1; // assume range is p - end-1 stop = end - 1; // assume range is p..end-1
if (range == LIMITED) if (range == LIMITED)
stop = next_line(p); // range is to next line stop = next_line(p); // range is to next line
for (start = p; start < stop; start++) { for (start = p; start < stop; start++) {
@ -1799,7 +1787,7 @@ static char *char_search(char *p, const char *pat, int dir, int range)
} }
} }
} else if (dir == BACK) { } else if (dir == BACK) {
stop = text; // assume range is text - p stop = text; // assume range is text..p
if (range == LIMITED) if (range == LIMITED)
stop = prev_line(p); // range is to prev line stop = prev_line(p); // range is to prev line
for (start = p - len; start >= stop; start--) { for (start = p - len; start >= stop; start--) {