diff --git a/main.c b/main.c index 3e9d9ab8b..3192b2b4e 100644 --- a/main.c +++ b/main.c @@ -2321,7 +2321,7 @@ int main(int argc, char *argv[]) { } win_created = true; if (cmd) { - vis_prompt_cmd(vis, cmd); + vis_prompt_cmd(vis, cmd, strlen(cmd)); cmd = NULL; } } @@ -2330,7 +2330,7 @@ int main(int argc, char *argv[]) { if (!vis_window_new(vis, NULL)) vis_die(vis, "Can not create empty buffer\n"); if (cmd) - vis_prompt_cmd(vis, cmd); + vis_prompt_cmd(vis, cmd, strlen(cmd)); } int status = vis_run(vis); diff --git a/sam.c b/sam.c index 90d19c297..268b07496 100644 --- a/sam.c +++ b/sam.c @@ -694,7 +694,7 @@ static Regex *parse_regex(Vis *vis, const char **s) { char *pattern = parse_delimited(s, CMD_REGEX); if (!pattern && *s == before) return NULL; - Regex *regex = vis_regex(vis, pattern); + Regex *regex = vis_regex(vis, pattern, strlen(pattern)); free(pattern); return regex; } diff --git a/text-regex-tre.c b/text-regex-tre.c index db32d6d9e..fc0884a7e 100644 --- a/text-regex-tre.c +++ b/text-regex-tre.c @@ -121,9 +121,7 @@ void text_regex_free(Regex *r) { } int text_regex_compile(Regex *regex, const char *string, size_t len, int cflags) { - int r = len > 0 - ? tre_regncomp(®ex->regex, string, len, cflags) - : tre_regcomp(®ex->regex, string, cflags); + int r = tre_regncomp(®ex->regex, string, len, cflags); if (r) tre_regcomp(®ex->regex, "\0\0", 0); diff --git a/vis-cmds.c b/vis-cmds.c index 4cfb3bd5d..1d706b2a8 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -874,7 +874,7 @@ static bool cmd_help(Vis *vis, Win *win, Command *cmd, const char *argv[], Selec view_cursors_to(vis->win->view.selection, 0); if (argv[1]) - vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, argv[1]); + vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, argv[1], strlen(argv[1])); return true; } diff --git a/vis-motions.c b/vis-motions.c index 1d3f49a23..2fc706298 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -16,10 +16,10 @@ static Regex *search_word(Vis *vis, Text *txt, size_t pos) { if (!buf) return NULL; snprintf(expr, sizeof(expr), "[[:<:]]%s[[:>:]]", buf); - Regex *regex = vis_regex(vis, expr); + Regex *regex = vis_regex(vis, expr, strlen(expr)); if (!regex) { snprintf(expr, sizeof(expr), "\\<%s\\>", buf); - regex = vis_regex(vis, expr); + regex = vis_regex(vis, expr, strlen(expr)); } free(buf); return regex; @@ -46,7 +46,7 @@ static size_t search_word_backward(Vis *vis, Text *txt, size_t pos) { } static size_t search_forward(Vis *vis, Text *txt, size_t pos) { - Regex *regex = vis_regex(vis, NULL); + Regex *regex = vis_regex(vis, NULL, 0); if (regex) pos = text_search_forward(txt, pos, regex); text_regex_free(regex); @@ -54,7 +54,7 @@ static size_t search_forward(Vis *vis, Text *txt, size_t pos) { } static size_t search_backward(Vis *vis, Text *txt, size_t pos) { - Regex *regex = vis_regex(vis, NULL); + Regex *regex = vis_regex(vis, NULL, 0); if (regex) pos = text_search_backward(txt, pos, regex); text_regex_free(regex); @@ -293,7 +293,8 @@ bool vis_motion(Vis *vis, enum VisMotion motion, ...) { case VIS_MOVE_SEARCH_BACKWARD: { const char *pattern = va_arg(ap, char*); - Regex *regex = vis_regex(vis, pattern); + size_t len = va_arg(ap, size_t); + Regex *regex = vis_regex(vis, pattern, len); if (!regex) { vis_cancel(vis); goto err; diff --git a/vis-prompt.c b/vis-prompt.c index c2fbf9c1d..2884996ca 100644 --- a/vis-prompt.c +++ b/vis-prompt.c @@ -4,17 +4,17 @@ #include "text-objects.h" #include "text-util.h" -bool vis_prompt_cmd(Vis *vis, const char *cmd) { - if (!cmd || !cmd[0] || !cmd[1]) +bool vis_prompt_cmd(Vis *vis, const char *cmd, size_t len) { + if (!cmd || len < 2) return true; switch (cmd[0]) { case '/': - return vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, cmd+1); + return vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, cmd+1, len-1); case '?': - return vis_motion(vis, VIS_MOVE_SEARCH_BACKWARD, cmd+1); + return vis_motion(vis, VIS_MOVE_SEARCH_BACKWARD, cmd+1, len-1); case '+': case ':': - register_put0(vis, &vis->registers[VIS_REG_COMMAND], cmd+1); + register_put(vis, &vis->registers[VIS_REG_COMMAND], cmd+1, len-1); return vis_cmd(vis, cmd+1); default: return false; @@ -53,6 +53,7 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { Text *txt = prompt->file->text; Win *win = prompt->parent; char *cmd = NULL; + size_t len = 0; Filerange range = view_selections_get(view->selection); if (!vis->mode->visual) { @@ -64,7 +65,7 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { else if (prompt->file == vis->search_file) pattern = "^(/|\\?)"; int cflags = REG_EXTENDED|REG_NEWLINE|(REG_ICASE*vis->ignorecase); - if (pattern && regex && text_regex_compile(regex, pattern, 0, cflags) == 0) { + if (pattern && regex && text_regex_compile(regex, pattern, strlen(pattern), cflags) == 0) { size_t end = text_line_end(txt, pos); size_t prev = text_search_backward(txt, end, regex); if (prev > pos) @@ -76,8 +77,10 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { } text_regex_free(regex); } - if (text_range_valid(&range)) - cmd = text_bytes_alloc0(txt, range.start, text_range_size(&range)); + if (text_range_valid(&range)) { + len = text_range_size(&range); + cmd = text_bytes_alloc0(txt, range.start, len); + } if (!win || !cmd) { if (!win) @@ -90,14 +93,14 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { return keys; } - size_t len = strlen(cmd); - if (len > 0 && cmd[len-1] == '\n') - cmd[len-1] = '\0'; + if (len > 0 && cmd[len-1] == '\n') { + cmd[--len] = '\0'; + } bool lastline = (range.end == text_size(txt)); prompt_restore(prompt); - if (vis_prompt_cmd(vis, cmd)) { + if (vis_prompt_cmd(vis, cmd, len)) { prompt_hide(prompt); if (!lastline) { text_delete(txt, range.start, text_range_size(&range)); diff --git a/vis-text-objects.c b/vis-text-objects.c index 3368e077f..2ad53be66 100644 --- a/vis-text-objects.c +++ b/vis-text-objects.c @@ -31,7 +31,7 @@ bool vis_textobject(Vis *vis, enum VisTextObject id) { static Filerange search_forward(Vis *vis, Text *txt, size_t pos) { Filerange range = text_range_empty(); - Regex *regex = vis_regex(vis, NULL); + Regex *regex = vis_regex(vis, NULL, 0); if (regex) range = text_object_search_forward(txt, pos, regex); text_regex_free(regex); @@ -40,7 +40,7 @@ static Filerange search_forward(Vis *vis, Text *txt, size_t pos) { static Filerange search_backward(Vis *vis, Text *txt, size_t pos) { Filerange range = text_range_empty(); - Regex *regex = vis_regex(vis, NULL); + Regex *regex = vis_regex(vis, NULL, 0); if (regex) range = text_object_search_backward(txt, pos, regex); text_regex_free(regex); diff --git a/vis.c b/vis.c index 002af2144..f270d8a4e 100644 --- a/vis.c +++ b/vis.c @@ -1578,8 +1578,7 @@ void vis_insert_nl(Vis *vis) { vis_window_invalidate(win); } -Regex *vis_regex(Vis *vis, const char *pattern) { - size_t len = 0; +Regex *vis_regex(Vis *vis, const char *pattern, size_t len) { if (!pattern && !(pattern = register_get(vis, &vis->registers[VIS_REG_SEARCH], &len))) return NULL; Regex *regex = text_regex_new(); @@ -1590,8 +1589,7 @@ Regex *vis_regex(Vis *vis, const char *pattern) { text_regex_free(regex); return NULL; } - if (len == 0) - register_put0(vis, &vis->registers[VIS_REG_SEARCH], pattern); + register_put(vis, &vis->registers[VIS_REG_SEARCH], pattern, len+1); return regex; } diff --git a/vis.h b/vis.h index eb8ec92d1..f92471be8 100644 --- a/vis.h +++ b/vis.h @@ -836,7 +836,7 @@ bool vis_option_register(Vis*, const char *names[], enum VisOption, bool vis_option_unregister(Vis*, const char *name); /** Execute any kind (``:``, ``?``, ``/``) of prompt command */ -bool vis_prompt_cmd(Vis*, const char *cmd); +bool vis_prompt_cmd(Vis*, const char *cmd, size_t len); /** * Pipe a given file range to an external process. @@ -921,7 +921,7 @@ void vis_keys_feed(Vis*, const char *keys); * .. warning:: The caller must free the regex object using `text_regex_free`. * @endrst */ -Regex *vis_regex(Vis*, const char *pattern); +Regex *vis_regex(Vis*, const char *pattern, size_t len); /** * Take an undo snapshot to which we can later revert.