diff options
author | Bram Moolenaar <Bram@vim.org> | 2018-08-18 21:23:05 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2018-08-18 21:23:05 +0200 |
commit | 111bbd61e96d1d3ee4bc07ae435fd97b88413aba (patch) | |
tree | 7cc7115bc1741c28d2378d696cb5551936ce5fb1 /src/ex_getln.c | |
parent | 264cf5cfaf40e704aea2578e70c15ed9a9d0161e (diff) | |
download | vim-git-111bbd61e96d1d3ee4bc07ae435fd97b88413aba.tar.gz |
patch 8.1.0296: command parsing for 'incsearch' is a bit uglyv8.1.0296
Problem: Command parsing for 'incsearch' is a bit ugly.
Solution: Return when there is no pattern. Put common checks together.
Diffstat (limited to 'src/ex_getln.c')
-rw-r--r-- | src/ex_getln.c | 225 |
1 files changed, 105 insertions, 120 deletions
diff --git a/src/ex_getln.c b/src/ex_getln.c index bce4aac79..b0da5d80e 100644 --- a/src/ex_getln.c +++ b/src/ex_getln.c @@ -276,140 +276,125 @@ set_search_match(pos_T *t) do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, int *skiplen, int *patlen) { + char_u *cmd; + cmdmod_T save_cmdmod = cmdmod; + char_u *p; + int delim_optional = FALSE; + int delim; + char_u *end; + char_u *dummy; + exarg_T ea; + pos_T save_cursor; + *skiplen = 0; *patlen = ccline.cmdlen; - if (p_is && !cmd_silent) - { - // by default search all lines - search_first_line = 0; - search_last_line = MAXLNUM; + if (!p_is || cmd_silent) + return FALSE; - if (firstc == '/' || firstc == '?') - return TRUE; - if (firstc == ':') - { - char_u *cmd; - cmdmod_T save_cmdmod = cmdmod; - char_u *p; - int delim; - char_u *end; - char_u *dummy; - exarg_T ea; - - vim_memset(&ea, 0, sizeof(ea)); - ea.line1 = 1; - ea.line2 = 1; - ea.cmd = ccline.cmdbuff; - ea.addr_type = ADDR_LINES; - - parse_command_modifiers(&ea, &dummy, TRUE); - cmdmod = save_cmdmod; - - cmd = skip_range(ea.cmd, NULL); - if (*cmd == 's' || *cmd == 'g' || *cmd == 'v' || *cmd == 'l') - { - // Skip over "substitute" to find the pattern separator. - for (p = cmd; ASCII_ISALPHA(*p); ++p) - ; - if (*skipwhite(p) != NUL) - { - if (STRNCMP(cmd, "substitute", p - cmd) == 0 - || STRNCMP(cmd, "smagic", p - cmd) == 0 - || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 - || STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0 - || STRNCMP(cmd, "global", p - cmd) == 0 - || STRNCMP(cmd, "vglobal", p - cmd) == 0) - { - if (*cmd == 's' && cmd[1] == 'm') - p_magic = TRUE; - else if (*cmd == 's' && cmd[1] == 'n') - p_magic = FALSE; + // by default search all lines + search_first_line = 0; + search_last_line = MAXLNUM; - // Check for "global!/". - if (*cmd == 'g' && *p == '!') - { - p++; - if (*skipwhite(p) == NUL) - return FALSE; - } + if (firstc == '/' || firstc == '?') + return TRUE; + if (firstc != ':') + return FALSE; - // For ":sort" skip over flags. - if (cmd[0] == 's' && cmd[1] == 'o') - { - while (ASCII_ISALPHA(*(p = skipwhite(p)))) - ++p; - if (*p == NUL) - return FALSE; - } + vim_memset(&ea, 0, sizeof(ea)); + ea.line1 = 1; + ea.line2 = 1; + ea.cmd = ccline.cmdbuff; + ea.addr_type = ADDR_LINES; - p = skipwhite(p); - delim = *p++; - end = skip_regexp(p, delim, p_magic, NULL); - } - else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0 - || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0 - || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0 - || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0) - { - // Check for "!/". - if (*p == '!') - { - p++; - if (*skipwhite(p) == NUL) - return FALSE; - } - p = skipwhite(p); - delim = (vim_isIDc(*p)) ? ' ' : *p++; - end = skip_regexp(p, delim, p_magic, NULL); - } - else - { - end = p; - delim = -1; - } + parse_command_modifiers(&ea, &dummy, TRUE); + cmdmod = save_cmdmod; - if (end > p || *end == delim) - { - pos_T save_cursor = curwin->w_cursor; + cmd = skip_range(ea.cmd, NULL); + if (vim_strchr((char_u *)"sgvl", *cmd) == NULL) + return FALSE; - // found a non-empty pattern or // - *skiplen = (int)(p - ccline.cmdbuff); - *patlen = (int)(end - p); + // Skip over "substitute" to find the pattern separator. + for (p = cmd; ASCII_ISALPHA(*p); ++p) + ; + if (*skipwhite(p) == NUL) + return FALSE; - // parse the address range - curwin->w_cursor = is_state->search_start; - parse_cmd_address(&ea, &dummy); - if (ea.addr_count > 0) - { - // Allow for reverse match. - if (ea.line2 < ea.line1) - { - search_first_line = ea.line2; - search_last_line = ea.line1; - } - else - { - search_first_line = ea.line1; - search_last_line = ea.line2; - } - } - else if (cmd[0] == 's' && cmd[1] != 'o') - { - // :s defaults to the current line - search_first_line = curwin->w_cursor.lnum; - search_last_line = curwin->w_cursor.lnum; - } + if (STRNCMP(cmd, "substitute", p - cmd) == 0 + || STRNCMP(cmd, "smagic", p - cmd) == 0 + || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 + || STRNCMP(cmd, "vglobal", p - cmd) == 0) + { + if (*cmd == 's' && cmd[1] == 'm') + p_magic = TRUE; + else if (*cmd == 's' && cmd[1] == 'n') + p_magic = FALSE; + } + else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0) + { + // skip over flags + while (ASCII_ISALPHA(*(p = skipwhite(p)))) + ++p; + if (*p == NUL) + return FALSE; + } + else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0 + || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0 + || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0 + || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0 + || STRNCMP(cmd, "global", p - cmd) == 0) + { + // skip over "!" + if (*p == '!') + { + p++; + if (*skipwhite(p) == NUL) + return FALSE; + } + if (*cmd != 'g') + delim_optional = TRUE; + } + else + return FALSE; - curwin->w_cursor = save_cursor; - return TRUE; - } - } - } + p = skipwhite(p); + delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++; + end = skip_regexp(p, delim, p_magic, NULL); + + if (end == p && *end != delim) + return FALSE; + // found a non-empty pattern or // + + *skiplen = (int)(p - ccline.cmdbuff); + *patlen = (int)(end - p); + + // parse the address range + save_cursor = curwin->w_cursor; + curwin->w_cursor = is_state->search_start; + parse_cmd_address(&ea, &dummy); + if (ea.addr_count > 0) + { + // Allow for reverse match. + if (ea.line2 < ea.line1) + { + search_first_line = ea.line2; + search_last_line = ea.line1; + } + else + { + search_first_line = ea.line1; + search_last_line = ea.line2; } } + else if (cmd[0] == 's' && cmd[1] != 'o') + { + // :s defaults to the current line + search_first_line = curwin->w_cursor.lnum; + search_last_line = curwin->w_cursor.lnum; + } - return FALSE; + curwin->w_cursor = save_cursor; + return TRUE; } static void |