From 485972dd64317e6a09ce7694cf0f903fb6d13cbd Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 19:05:11 +0900 Subject: [PATCH 01/10] vim-patch:8.1.0629: "gn" selects the wrong text with a multi-line match Problem: "gn" selects the wrong text with a multi-line match. Solution: Get the end position from searchit() directly. (closes vim/vim#3695) https://github.com/vim/vim/commit/5d24a2257e597fd752e33b2c1e9c19cf9114a517 --- src/nvim/edit.c | 8 +-- src/nvim/eval.c | 4 +- src/nvim/ex_docmd.c | 6 +-- src/nvim/ex_getln.c | 2 +- src/nvim/normal.c | 2 +- src/nvim/search.c | 94 +++++++++++++++--------------------- src/nvim/testdir/test_gn.vim | 19 ++++++++ 7 files changed, 71 insertions(+), 64 deletions(-) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 5ac95b64e7..9af003f140 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -4045,13 +4045,15 @@ static int ins_compl_get_exp(pos_T *ini) if (CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode) || (compl_cont_status & CONT_SOL)) { found_new_match = search_for_exact_line(ins_buf, pos, - compl_direction, compl_pattern); - } else - found_new_match = searchit(NULL, ins_buf, pos, + compl_direction, + compl_pattern); + } else { + found_new_match = searchit(NULL, ins_buf, pos, NULL, compl_direction, compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, RE_LAST, (linenr_T)0, NULL, NULL); + } msg_silent--; if (!compl_started || set_match_pos) { /* set "compl_started" even on fail */ diff --git a/src/nvim/eval.c b/src/nvim/eval.c index a4606f76f3..49ebf8cef0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -14139,7 +14139,7 @@ static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) } pos = save_cursor = curwin->w_cursor; - subpatnum = searchit(curwin, curbuf, &pos, dir, (char_u *)pat, 1, + subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, (char_u *)pat, 1, options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL); if (subpatnum != FAIL) { if (flags & SP_SUBPAT) @@ -14657,7 +14657,7 @@ do_searchpair( clearpos(&foundpos); pat = pat3; for (;; ) { - n = searchit(curwin, curbuf, &pos, dir, pat, 1L, + n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L, options, RE_SEARCH, lnum_stop, &tm, NULL); if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos))) { // didn't find it or found the first match again: FAIL diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index ec4b16fbb0..16118d642b 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -3750,12 +3750,12 @@ static linenr_T get_address(exarg_T *eap, // Start the search just like for the above do_search(). pos.col = (*cmd != '?') ? MAXCOL : 0; pos.coladd = 0; - if (searchit(curwin, curbuf, &pos, + if (searchit(curwin, curbuf, &pos, NULL, *cmd == '?' ? BACKWARD : FORWARD, (char_u *)"", 1L, SEARCH_MSG, - i, (linenr_T)0, NULL, NULL) != FAIL) + i, (linenr_T)0, NULL, NULL) != FAIL) { lnum = pos.lnum; - else { + } else { cmd = NULL; goto error; } diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 3bfda1f9f0..479d195966 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -1069,7 +1069,7 @@ static void command_line_next_incsearch(CommandLineState *s, bool next_match) search_flags += SEARCH_KEEP; } emsg_off++; - s->i = searchit(curwin, curbuf, &t, + s->i = searchit(curwin, curbuf, &t, NULL, next_match ? FORWARD : BACKWARD, pat, s->count, search_flags, RE_SEARCH, 0, NULL, NULL); diff --git a/src/nvim/normal.c b/src/nvim/normal.c index db2da6a807..c59de4f4e3 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -3782,7 +3782,7 @@ find_decl ( for (;; ) { valid = false; (void)valid; // Avoid "dead assignment" warning. - t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD, + t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD, pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL); if (curwin->w_cursor.lnum >= old_pos.lnum) { t = false; // match after start is failure too diff --git a/src/nvim/search.c b/src/nvim/search.c index e64233985b..5661c41129 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -497,8 +497,8 @@ void last_pat_prog(regmmatch_T *regmatch) } /// lowest level search function. -/// Search for 'count'th occurrence of pattern 'pat' in direction 'dir'. -/// Start at position 'pos' and return the found position in 'pos'. +/// Search for 'count'th occurrence of pattern "pat" in direction "dir". +/// Start at position "pos" and return the found position in "pos". /// /// if (options & SEARCH_MSG) == 0 don't give any messages /// if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages @@ -519,6 +519,7 @@ int searchit( buffer without a window! */ buf_T *buf, pos_T *pos, + pos_T *end_pos, // set to end of the match, unless NULL Direction dir, char_u *pat, long count, @@ -819,11 +820,22 @@ int searchit( pos->col -= utf_head_off(ptr, ptr + pos->col); } } + if (end_pos != NULL) { + end_pos->lnum = lnum + matchpos.lnum; + end_pos->col = matchpos.col; + } } else { pos->lnum = lnum + matchpos.lnum; pos->col = matchpos.col; + if (end_pos != NULL) { + end_pos->lnum = lnum + endpos.lnum; + end_pos->col = endpos.col; + } } pos->coladd = 0; + if (end_pos != NULL) { + end_pos->coladd = 0; + } found = 1; first_match = false; @@ -1196,7 +1208,7 @@ int do_search( } } - c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD, + c = searchit(curwin, curbuf, &pos, NULL, dirc == '/' ? FORWARD : BACKWARD, searchstr, count, (spats[0].off.end * SEARCH_END + (options @@ -3933,7 +3945,7 @@ abort_search: int current_search( long count, - int forward /* move forward or backwards */ + int forward // true for forward, false for backward ) { bool old_p_ws = p_ws; @@ -3946,8 +3958,8 @@ current_search( if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor)) dec_cursor(); + pos_T end_pos; // end position of the pattern match pos_T orig_pos; // position of the cursor at beginning - pos_T first_match; // position of first match pos_T pos; // position after the pattern int result; // result of various function calls @@ -3981,78 +3993,52 @@ current_search( int dir = forward ? i : !i; int flags = 0; - if (!dir && !one_char) + if (!dir && !one_char) { flags = SEARCH_END; + } + end_pos = pos; - result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD), + result = searchit(curwin, curbuf, &pos, &end_pos, + (dir ? FORWARD : BACKWARD), spats[last_idx].pat, i ? count : 1, SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL); - /* First search may fail, but then start searching from the - * beginning of the file (cursor might be on the search match) - * except when Visual mode is active, so that extending the visual - * selection works. */ - if (!result && i) { /* not found, abort */ + // First search may fail, but then start searching from the + // beginning of the file (cursor might be on the search match) + // except when Visual mode is active, so that extending the visual + // selection works. + if (i == 1 && !result) { // not found, abort */ curwin->w_cursor = orig_pos; if (VIsual_active) VIsual = save_VIsual; p_ws = old_p_ws; return FAIL; - } else if (!i && !result) { - if (forward) { /* try again from start of buffer */ + } else if (i == 0 && !result) { + if (forward) { // try again from start of buffer clearpos(&pos); - } else { /* try again from end of buffer */ - /* searching backwards, so set pos to last line and col */ + } else { // try again from end of buffer + // searching backwards, so set pos to last line and col pos.lnum = curwin->w_buffer->b_ml.ml_line_count; pos.col = (colnr_T)STRLEN( ml_get(curwin->w_buffer->b_ml.ml_line_count)); } } - if (i == 0) { - first_match = pos; - } p_ws = old_p_ws; } - const int flags = forward ? SEARCH_END : SEARCH_START; pos_T start_pos = pos; - const Direction direction = forward ? FORWARD : BACKWARD; - - // Check again from the current cursor position, - // since the next match might actually be only one char wide - one_char = is_one_char(spats[last_idx].pat, false, &pos, direction); - if (one_char < 0) { - // search failed, abort - return FAIL; - } - - /* move to match, except for zero-width matches, in which case, we are - * already on the next match */ - if (!one_char) { - p_ws = false; - for (int i = 0; i < 2; i++) { - result = searchit(curwin, curbuf, &pos, direction, - spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, - 0, NULL, NULL); - // Search successfull, break out from the loop - if (result) { - break; - } - // search failed, try again from the last search position match - pos = first_match; - } - } p_ws = old_p_ws; - // not found - if (!result) { - return FAIL; + + if (!VIsual_active) { + VIsual = start_pos; } - if (!VIsual_active) - VIsual = start_pos; - - curwin->w_cursor = pos; + // put cursor on last character of match + curwin->w_cursor = end_pos; + if (lt(VIsual, end_pos)) { + dec_cursor(); + } VIsual_active = true; VIsual_mode = 'v'; @@ -4111,7 +4097,7 @@ static int is_one_char(char_u *pattern, bool move, pos_T *cur, // accept a match at the cursor position flag = SEARCH_START; } - if (searchit(curwin, curbuf, &pos, direction, pattern, 1, + if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1, SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL) { // Zero-width pattern should match somewhere, then we can check if // start and end are in the same position. diff --git a/src/nvim/testdir/test_gn.vim b/src/nvim/testdir/test_gn.vim index 405425a42b..5e74289b00 100644 --- a/src/nvim/testdir/test_gn.vim +++ b/src/nvim/testdir/test_gn.vim @@ -133,4 +133,23 @@ func Test_gn_command() set belloff&vim endfu +func Test_gn_multi_line() + new + call setline(1, [ + \ 'func Tm1()', + \ ' echo "one"', + \ 'endfunc', + \ 'func Tm2()', + \ ' echo "two"', + \ 'endfunc', + \ 'func Tm3()', + \ ' echo "three"', + \ 'endfunc', + \]) + /\v^func Tm\d\(\)\n.*\zs".*"\ze$ + normal jgnrx + call assert_equal(' echo xxxxx', getline(5)) + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab From 777c2a25ce00f12b2d0dc26d594b1ba7ba10dcc6 Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 12:26:32 +0900 Subject: [PATCH 02/10] vim-patch:8.1.1270: cannot see current match position Problem: Cannot see current match position. Solution: Show "3/44" when using the "n" command and "S" is not in 'shortmess'. (Christian Brabandt, closes vim/vim#4317) https://github.com/vim/vim/commit/9dfa3139198b38b28673e251a3756430065914e9 --- runtime/doc/options.txt | 4 +- runtime/doc/pattern.txt | 11 ++ src/nvim/option.c | 2 +- src/nvim/option_defs.h | 1 + src/nvim/options.lua | 2 +- src/nvim/search.c | 240 +++++++++++++++++++++----- src/nvim/testdir/test_search_stat.vim | 108 ++++++++++++ 7 files changed, 323 insertions(+), 45 deletions(-) create mode 100644 src/nvim/testdir/test_search_stat.vim diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index cce9f681f9..34779541d8 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -5254,7 +5254,7 @@ A jump table for the options with a short description can be found at |Q_op|. function to get the effective shiftwidth value. *'shortmess'* *'shm'* -'shortmess' 'shm' string (Vim default "filnxtToOF", Vi default: "") +'shortmess' 'shm' string (Vim default "filnxtToOFS", Vi default: "S") global This option helps to avoid all the |hit-enter| prompts caused by file messages, for example with CTRL-G, and to avoid some other messages. @@ -5294,6 +5294,8 @@ A jump table for the options with a short description can be found at |Q_op|. q use "recording" instead of "recording @a" F don't give the file info when editing a file, like `:silent` was used for the command + S do not show search count message when searching, e.g. + "[1/5]" This gives you the opportunity to avoid that a change between buffers requires you to hit , but still gives as useful a message as diff --git a/runtime/doc/pattern.txt b/runtime/doc/pattern.txt index 88b7f65209..022dc5607e 100644 --- a/runtime/doc/pattern.txt +++ b/runtime/doc/pattern.txt @@ -141,6 +141,17 @@ use to abandon the search. All matches for the last used search pattern will be highlighted if you set the 'hlsearch' option. This can be suspended with the |:nohlsearch| command. +When 'shortmess' does not include the "S" flag, Vim will automatically show an +index, on which the cursor is. This can look like this: > + + [1/5] Cursor is on first of 5 matches. + [1/>99] Cursor is on first of more than 99 matches. + [>99/>99] Cursor is after 99 match of more than 99 matches. + [?/??] Unknown how many matches exists, generating the + statistics was aborted because of search timeout. + +Note: the count does not take offset into account. + When no match is found you get the error: *E486* Pattern not found Note that for the |:global| command this behaves like a normal message, for Vi compatibility. For the |:s| command the "e" flag can be used to avoid the diff --git a/src/nvim/option.c b/src/nvim/option.c index 43ee6bf451..fde1116cc9 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -315,7 +315,7 @@ static char_u SHM_ALL[] = { SHM_RO, SHM_MOD, SHM_FILE, SHM_LAST, SHM_TEXT, SHM_LINES, SHM_NEW, SHM_WRI, SHM_ABBREVIATIONS, SHM_WRITE, SHM_TRUNC, SHM_TRUNCALL, SHM_OVER, SHM_OVERALL, SHM_SEARCH, SHM_ATTENTION, SHM_INTRO, SHM_COMPLETIONMENU, - SHM_RECORDING, SHM_FILEINFO, + SHM_RECORDING, SHM_FILEINFO, SHM_SEARCHCOUNT, 0, }; diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 3e116095fd..a9f44976c9 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -173,6 +173,7 @@ enum { SHM_COMPLETIONMENU = 'c', ///< Completion menu messages. SHM_RECORDING = 'q', ///< Short recording message. SHM_FILEINFO = 'F', ///< No file info messages. + SHM_SEARCHCOUNT = 'S', ///< Search sats: '[1/10]' }; /// Represented by 'a' flag. #define SHM_ALL_ABBREVIATIONS ((char_u[]) { \ diff --git a/src/nvim/options.lua b/src/nvim/options.lua index affddd8084..3f28190ec3 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2134,7 +2134,7 @@ return { type='string', list='flags', scope={'global'}, vim=true, varname='p_shm', - defaults={if_true={vi="", vim="filnxtToOF"}} + defaults={if_true={vi="S", vim="filnxtToOFS"}} }, { full_name='showbreak', abbreviation='sbr', diff --git a/src/nvim/search.c b/src/nvim/search.c index 5661c41129..e1eb7200d7 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -14,6 +14,7 @@ #include "nvim/ascii.h" #include "nvim/vim.h" #include "nvim/search.h" +#include "nvim/buffer.h" #include "nvim/charset.h" #include "nvim/cursor.h" #include "nvim/edit.h" @@ -997,6 +998,8 @@ int do_search( char_u *dircp; char_u *strcopy = NULL; char_u *ps; + char_u *msgbuf = NULL; + size_t len; /* * A line offset is not remembered, this is vi compatible. @@ -1123,61 +1126,95 @@ int do_search( if ((options & SEARCH_ECHO) && messaging() && !cmd_silent && msg_silent == 0) { - char_u *msgbuf; char_u *trunc; - if (*searchstr == NUL) + // Compute msg_row early. + msg_start(); + + if (*searchstr == NUL) { p = spats[last_idx].pat; - else + } else { p = searchstr; - msgbuf = xmalloc(STRLEN(p) + 40); + } + + if (!shortmess(SHM_SEARCHCOUNT)) { + // Reserve enough space for the search pattern + offset + + // search stat. + if (msg_scrolled != 0) { + // Use all the columns. + len = (int)(Rows - msg_row) * Columns - 1; + } else { + // Use up to 'showcmd' column. + len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; + } + if (len < STRLEN(p) + 40 + 11) { + len = STRLEN(p) + 40 + 11; + } + } else { + // Reserve enough space for the search pattern + offset. + len = STRLEN(p) + 40; + } + + msgbuf = xmalloc((int)len); { + memset(msgbuf, ' ', len); msgbuf[0] = dirc; + msgbuf[len - 1] = NUL; + if (utf_iscomposing(utf_ptr2char(p))) { // Use a space to draw the composing char on. msgbuf[1] = ' '; - STRCPY(msgbuf + 2, p); - } else - STRCPY(msgbuf + 1, p); + STRNCPY(msgbuf + 2, p, STRLEN(p)); + } else { + STRNCPY(msgbuf + 1, p, STRLEN(p)); + } if (spats[0].off.line || spats[0].off.end || spats[0].off.off) { - p = msgbuf + STRLEN(msgbuf); + p = msgbuf + STRLEN(p) + 1; *p++ = dirc; - if (spats[0].off.end) + if (spats[0].off.end) { *p++ = 'e'; - else if (!spats[0].off.line) + } else if (!spats[0].off.line) { *p++ = 's'; - if (spats[0].off.off > 0 || spats[0].off.line) + } + if (spats[0].off.off > 0 || spats[0].off.line) { *p++ = '+'; - if (spats[0].off.off != 0 || spats[0].off.line) - sprintf((char *)p, "%" PRId64, (int64_t)spats[0].off.off); - else - *p = NUL; + } + if (spats[0].off.off != 0 || spats[0].off.line) { + int l = 0; + l = sprintf((char *)p, "%ld", spats[0].off.off); + p[l] = ' '; // remove NUL from sprintf + } } - msg_start(); trunc = msg_strtrunc(msgbuf, FALSE); + if (trunc != NULL) { + xfree(msgbuf); + msgbuf = trunc; + } - /* The search pattern could be shown on the right in rightleft - * mode, but the 'ruler' and 'showcmd' area use it too, thus - * it would be blanked out again very soon. Show it on the - * left, but do reverse the text. */ + // The search pattern could be shown on the right in rightleft + // mode, but the 'ruler' and 'showcmd' area use it too, thus + // it would be blanked out again very soon. Show it on the + // left, but do reverse the text. if (curwin->w_p_rl && *curwin->w_p_rlc == 's') { char_u *r = reverse_text(trunc != NULL ? trunc : msgbuf); - xfree(trunc); - trunc = r; + xfree(msgbuf); + msgbuf = r; + // move reversed text to beginning of buffer + while (*r != NUL && *r == ' ') { + r++; + } + memmove(msgbuf, r, msgbuf + STRLEN(msgbuf) - r); + // overwrite old text + memset(r, ' ', msgbuf + STRLEN(msgbuf) - r); } - if (trunc != NULL) { - msg_outtrans(trunc); - xfree(trunc); - } else - msg_outtrans(msgbuf); + msg_outtrans(msgbuf); msg_clr_eos(); msg_check(); - xfree(msgbuf); - gotocmdline(FALSE); + gotocmdline(false); ui_flush(); - msg_nowait = TRUE; /* don't wait for this message */ + msg_nowait = true; // don't wait for this message } } @@ -1217,8 +1254,16 @@ int do_search( + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF)))), RE_LAST, (linenr_T)0, tm, timed_out); - if (dircp != NULL) - *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */ + if (dircp != NULL) { + *dircp = dirc; // restore second '/' or '?' for normal_cmd() + } + + if (!shortmess(SHM_SEARCH) + && ((dirc == '/' && lt(pos, curwin->w_cursor)) + || (dirc == '?' && lt(curwin->w_cursor, pos)))) { + os_delay(500L, false); // leave some time for top_bot_msg + } + if (c == FAIL) { retval = 0; goto end_do_search; @@ -1260,16 +1305,25 @@ int do_search( } } - /* - * The search command can be followed by a ';' to do another search. - * For example: "/pat/;/foo/+3;?bar" - * This is like doing another search command, except: - * - The remembered direction '/' or '?' is from the first search. - * - When an error happens the cursor isn't moved at all. - * Don't do this when called by get_address() (it handles ';' itself). - */ - if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';') + // Show [1/15] if 'S' is not in 'shortmess'. + if ((options & SEARCH_ECHO) + && messaging() + && !(cmd_silent + msg_silent) + && c != FAIL + && !shortmess(SHM_SEARCHCOUNT) + && msgbuf != NULL) { + search_stat(dirc, &pos, msgbuf); + } + + // The search command can be followed by a ';' to do another search. + // For example: "/pat/;/foo/+3;?bar" + // This is like doing another search command, except: + // - The remembered direction '/' or '?' is from the first search. + // - When an error happens the cursor isn't moved at all. + // Don't do this when called by get_address() (it handles ';' itself). + if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';') { break; + } dirc = *++pat; if (dirc != '?' && dirc != '/') { @@ -1288,7 +1342,7 @@ int do_search( end_do_search: if ((options & SEARCH_KEEP) || cmdmod.keeppatterns) spats[0].off = old_off; - xfree(strcopy); + xfree(msgbuf); return retval; } @@ -4141,6 +4195,108 @@ int linewhite(linenr_T lnum) return *p == NUL; } +// Add the search count "[3/19]" to "msgbuf". +static void search_stat(int dirc, pos_T *pos, char_u *msgbuf) +{ + int save_ws = p_ws; + int wraparound = false; + pos_T p = (*pos); + static pos_T lastpos = { 0, 0, 0 }; + static int cur = 0; + static int cnt = 0; + static int chgtick = 0; + static char_u *lastpat = NULL; + static buf_T *lbuf = NULL; + proftime_T start; +#define OUT_OF_TIME 999 + + wraparound = ((dirc == '?' && lt(lastpos, p)) + || (dirc == '/' && lt(p, lastpos))); + + // If anything relevant changed the count has to be recomputed. + // STRNICMP ignores case, but we should not ignore case. + // Unfortunately, there is no STRNICMP function. + if (!(chgtick == buf_get_changedtick(curbuf) + && STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0 + && STRLEN(lastpat) == STRLEN(spats[last_idx].pat) + && equalpos(lastpos, curwin->w_cursor) + && lbuf == curbuf) || wraparound || cur < 0 || cur > 99) { + cur = 0; + cnt = 0; + clearpos(&lastpos); + lbuf = curbuf; + } + + if (equalpos(lastpos, curwin->w_cursor) && !wraparound + && (dirc == '/' ? cur < cnt : cur > 0)) { + cur += dirc == '/' ? 1 : -1; + } else { + p_ws = false; + start = profile_setlimit(20L); + while (!got_int && searchit(curwin, curbuf, &lastpos, NULL, + FORWARD, NULL, 1, SEARCH_PEEK + SEARCH_KEEP, + RE_LAST, (linenr_T)0, NULL, NULL) != FAIL) { + // Stop after passing the time limit. + if (profile_passed_limit(start)) { + cnt = OUT_OF_TIME; + cur = OUT_OF_TIME; + break; + } + cnt++; + if (ltoreq(lastpos, p)) { + cur++; + } + fast_breakcheck(); + if (cnt > 99) { + break; + } + } + if (got_int) { + cur = -1; // abort + } + } + if (cur > 0) { +#define STAT_BUF_LEN 10 + char t[STAT_BUF_LEN] = ""; + + if (curwin->w_p_rl && *curwin->w_p_rlc == 's') { + if (cur == OUT_OF_TIME) { + vim_snprintf(t, STAT_BUF_LEN, "[?/??]"); + } else if (cnt > 99 && cur > 99) { + vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]"); + } else if (cnt > 99) { + vim_snprintf(t, STAT_BUF_LEN, "[>99/%d]", cur); + } else { + vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cnt, cur); + } + } else { + if (cur == OUT_OF_TIME) { + vim_snprintf(t, STAT_BUF_LEN, "[?/??]"); + } else if (cnt > 99 && cur > 99) { + vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]"); + } else if (cnt > 99) { + vim_snprintf(t, STAT_BUF_LEN, "[%d/>99]", cur); + } else { + vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cur, cnt); + } + } + STRNCPY(msgbuf + STRLEN(msgbuf) - STRLEN(t), t, STRLEN(t)); + if (dirc == '?' && cur == 100) { + cur = -1; + } + + xfree(lastpat); + lastpat = vim_strsave(spats[last_idx].pat); + chgtick = buf_get_changedtick(curbuf); + lbuf = curbuf; + lastpos = p; + + // keep the message even after redraw + give_warning(msgbuf, false); + } + p_ws = save_ws; +} + /* * Find identifiers or defines in included files. * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase. diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim new file mode 100644 index 0000000000..37e2fdaef5 --- /dev/null +++ b/src/nvim/testdir/test_search_stat.vim @@ -0,0 +1,108 @@ +" Tests for search_stats, when "S" is not in 'shortmess' +" +" This test is fragile, it might not work interactively, but it works when run +" as test! + +func! Test_search_stat() + new + set shortmess-=S + call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10)) + + " 1) match at second line + call cursor(1, 1) + let @/ = 'fo*\(bar\?\)\?' + let g:a = execute(':unsilent :norm! n') + let stat = '\[2/50\]' + let pat = escape(@/, '()*?'). '\s\+' + call assert_match(pat .. stat, g:a) + + " 2) Match at last line + call cursor(line('$')-2, 1) + let g:a = execute(':unsilent :norm! n') + let stat = '\[50/50\]' + call assert_match(pat .. stat, g:a) + + " 3) No search stat + set shortmess+=S + call cursor(1, 1) + let stat = '\[2/50\]' + let g:a = execute(':unsilent :norm! n') + call assert_notmatch(pat .. stat, g:a) + set shortmess-=S + + " 4) Many matches + call cursor(line('$')-2, 1) + let @/ = '.' + let pat = escape(@/, '()*?'). '\s\+' + let g:a = execute(':unsilent :norm! n') + let stat = '\[>99/>99\]' + call assert_match(pat .. stat, g:a) + + " 5) Many matches + call cursor(1, 1) + let g:a = execute(':unsilent :norm! n') + let stat = '\[2/>99\]' + call assert_match(pat .. stat, g:a) + + " 6) right-left + if exists("+rightleft") + set rl + call cursor(1,1) + let @/ = 'foobar' + let pat = 'raboof/\s\+' + let g:a = execute(':unsilent :norm! n') + let stat = '\[20/2\]' + call assert_match(pat .. stat, g:a) + set norl + endif + + " 7) right-left bottom + if exists("+rightleft") + set rl + call cursor('$',1) + let pat = 'raboof?\s\+' + let g:a = execute(':unsilent :norm! N') + let stat = '\[20/20\]' + call assert_match(pat .. stat, g:a) + set norl + endif + + " 8) right-left back at top + if exists("+rightleft") + set rl + call cursor('$',1) + let pat = 'raboof/\s\+' + let g:a = execute(':unsilent :norm! n') + let stat = '\[20/1\]' + call assert_match(pat .. stat, g:a) + call assert_match('search hit BOTTOM, continuing at TOP', g:a) + set norl + endif + + " 9) normal, back at top + call cursor(1,1) + let @/ = 'foobar' + let pat = '?foobar\s\+' + let g:a = execute(':unsilent :norm! N') + let stat = '\[20/20\]' + call assert_match(pat .. stat, g:a) + call assert_match('search hit TOP, continuing at BOTTOM', g:a) + + " 10) normal, no match + call cursor(1,1) + let @/ = 'zzzzzz' + let g:a = '' + try + let g:a = execute(':unsilent :norm! n') + catch /^Vim\%((\a\+)\)\=:E486/ + let stat = '' + " error message is not redir'ed to g:a, it is empty + call assert_true(empty(g:a)) + catch + call assert_false(1) + endtry + + " close the window + set shortmess+=S + bwipe! +endfunc From 6cee73195fb5d1d081a1e9ea907cc996d97082de Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 15:57:27 +0900 Subject: [PATCH 03/10] vim-patch:8.1.1271: compiler warnings for use of STRNCPY() Problem: Compiler warnings for use of STRNCPY(). (John Marriott) Solution: Use mch_memmove() instead of STRNCPY(). https://github.com/vim/vim/commit/b3de6c4a769986e6eb4e228519a6483d2999ad8f --- src/nvim/search.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index e1eb7200d7..9756faff33 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1164,9 +1164,9 @@ int do_search( if (utf_iscomposing(utf_ptr2char(p))) { // Use a space to draw the composing char on. msgbuf[1] = ' '; - STRNCPY(msgbuf + 2, p, STRLEN(p)); + memmove(msgbuf + 2, p, STRLEN(p)); } else { - STRNCPY(msgbuf + 1, p, STRLEN(p)); + memmove(msgbuf + 1, p, STRLEN(p)); } if (spats[0].off.line || spats[0].off.end || spats[0].off.off) { p = msgbuf + STRLEN(p) + 1; @@ -4280,7 +4280,7 @@ static void search_stat(int dirc, pos_T *pos, char_u *msgbuf) vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cur, cnt); } } - STRNCPY(msgbuf + STRLEN(msgbuf) - STRLEN(t), t, STRLEN(t)); + memmove(msgbuf + STRLEN(msgbuf) - STRLEN(t), t, STRLEN(t)); if (dirc == '?' && cur == 100) { cur = -1; } From e8ca281d3b9243e66430f7f139c19aabead3de95 Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 16:04:46 +0900 Subject: [PATCH 04/10] vim-patch:8.1.1283: delaying half a second after the top-bot message Problem: Delaying half a second after the top-bot message. Solution: Instead of the delay add "W" to the search count. https://github.com/vim/vim/commit/c7a10b35de70471519d104a74d402c63557f0512 --- src/nvim/search.c | 21 ++++++++++++++++----- src/nvim/testdir/test_search_stat.vim | 5 ++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 9756faff33..2a14afd762 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1055,6 +1055,8 @@ int do_search( * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar". */ for (;; ) { + bool show_top_bot_msg = false; + searchstr = pat; dircp = NULL; /* use previous pattern */ @@ -1261,7 +1263,7 @@ int do_search( if (!shortmess(SHM_SEARCH) && ((dirc == '/' && lt(pos, curwin->w_cursor)) || (dirc == '?' && lt(curwin->w_cursor, pos)))) { - os_delay(500L, false); // leave some time for top_bot_msg + show_top_bot_msg = true; } if (c == FAIL) { @@ -1312,7 +1314,7 @@ int do_search( && c != FAIL && !shortmess(SHM_SEARCHCOUNT) && msgbuf != NULL) { - search_stat(dirc, &pos, msgbuf); + search_stat(dirc, &pos, show_top_bot_msg, msgbuf); } // The search command can be followed by a ';' to do another search. @@ -4196,7 +4198,8 @@ int linewhite(linenr_T lnum) } // Add the search count "[3/19]" to "msgbuf". -static void search_stat(int dirc, pos_T *pos, char_u *msgbuf) +static void search_stat(int dirc, pos_T *pos, + bool show_top_bot_msg, char_u *msgbuf) { int save_ws = p_ws; int wraparound = false; @@ -4256,8 +4259,9 @@ static void search_stat(int dirc, pos_T *pos, char_u *msgbuf) } } if (cur > 0) { -#define STAT_BUF_LEN 10 +#define STAT_BUF_LEN 12 char t[STAT_BUF_LEN] = ""; + int len; if (curwin->w_p_rl && *curwin->w_p_rlc == 's') { if (cur == OUT_OF_TIME) { @@ -4280,7 +4284,14 @@ static void search_stat(int dirc, pos_T *pos, char_u *msgbuf) vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cur, cnt); } } - memmove(msgbuf + STRLEN(msgbuf) - STRLEN(t), t, STRLEN(t)); + + len = STRLEN(t); + if (show_top_bot_msg && len + 3 < STAT_BUF_LEN) { + STRCPY(t + len, " W"); + len += 2; + } + + memmove(msgbuf + STRLEN(msgbuf) - len, t, len); if (dirc == '?' && cur == 100) { cur = -1; } diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 37e2fdaef5..57dad81b81 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -3,6 +3,8 @@ " This test is fragile, it might not work interactively, but it works when run " as test! +source shared.vim + func! Test_search_stat() new set shortmess-=S @@ -79,7 +81,7 @@ func! Test_search_stat() set norl endif - " 9) normal, back at top + " 9) normal, back at bottom call cursor(1,1) let @/ = 'foobar' let pat = '?foobar\s\+' @@ -87,6 +89,7 @@ func! Test_search_stat() let stat = '\[20/20\]' call assert_match(pat .. stat, g:a) call assert_match('search hit TOP, continuing at BOTTOM', g:a) + call assert_match('\[20/20\] W', Screenline(&lines)) " 10) normal, no match call cursor(1,1) From 287fc076e137ea2fac8ac7288fd59eb28c4062cf Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 16:20:40 +0900 Subject: [PATCH 05/10] vim-patch:8.1.1288: search stats don't show for mapped command Problem: Search stats don't show for mapped command. Solution: Remove SEARCH_PEEK from searchit flags. Add a test. (Christian Brabandt) https://github.com/vim/vim/commit/9ce3fa828d238ff28d57b0092bb37575e20010ec --- src/nvim/search.c | 4 ++-- src/nvim/testdir/test_search_stat.vim | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 2a14afd762..62e1338496 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -4237,8 +4237,8 @@ static void search_stat(int dirc, pos_T *pos, p_ws = false; start = profile_setlimit(20L); while (!got_int && searchit(curwin, curbuf, &lastpos, NULL, - FORWARD, NULL, 1, SEARCH_PEEK + SEARCH_KEEP, - RE_LAST, (linenr_T)0, NULL, NULL) != FAIL) { + FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, + (linenr_T)0, NULL, NULL) != FAIL) { // Stop after passing the time limit. if (profile_passed_limit(start)) { cnt = OUT_OF_TIME; diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 57dad81b81..107cd54a0e 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -8,6 +8,7 @@ source shared.vim func! Test_search_stat() new set shortmess-=S + " Append 50 lines with text to search for, "foobar" appears 20 times call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10)) " 1) match at second line @@ -105,6 +106,30 @@ func! Test_search_stat() call assert_false(1) endtry + " 11) normal, n comes from a mapping + " Need to move over more than 64 lines to trigger char_avail(. + nnoremap n nzv + call cursor(1,1) + call append(50, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10)) + call setline(2, 'find this') + call setline(70, 'find this') + let @/ = 'find this' + let pat = '/find this\s\+' + let g:a = execute(':unsilent :norm n') + " g:a will contain several lines + let g:b = split(g:a, "\n")[-1] + let stat = '\[1/2\]' + call assert_match(pat .. stat, g:b) + unmap n + + " 11) normal, but silent + call cursor(1,1) + let @/ = 'find this' + let pat = '/find this\s\+' + let g:a = execute(':norm! n') + let stat = '\[1/2\]' + call assert_notmatch(pat .. stat, g:a) + " close the window set shortmess+=S bwipe! From 68202520c621ec9e58474576272df1456921047f Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 16:25:38 +0900 Subject: [PATCH 06/10] vim-patch:8.1.1289: may not have enough space to add "W" to search stats Problem: May not have enough space to add "W" to search stats. Solution: Reserve a bit more space. (Christian Brabandt) https://github.com/vim/vim/commit/b6cb26ffe1795ae62d8235960dccf517c2b2ed45 --- src/nvim/search.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 62e1338496..35875e1cd6 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1000,6 +1000,7 @@ int do_search( char_u *ps; char_u *msgbuf = NULL; size_t len; +#define SEARCH_STAT_BUF_LEN 12 /* * A line offset is not remembered, this is vi compatible. @@ -1149,8 +1150,8 @@ int do_search( // Use up to 'showcmd' column. len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; } - if (len < STRLEN(p) + 40 + 11) { - len = STRLEN(p) + 40 + 11; + if (len < STRLEN(p) + SEARCH_STAT_BUF_LEN + 1) { + len = STRLEN(p) + SEARCH_STAT_BUF_LEN + 1; } } else { // Reserve enough space for the search pattern + offset. @@ -4259,34 +4260,33 @@ static void search_stat(int dirc, pos_T *pos, } } if (cur > 0) { -#define STAT_BUF_LEN 12 - char t[STAT_BUF_LEN] = ""; + char t[SEARCH_STAT_BUF_LEN] = ""; int len; if (curwin->w_p_rl && *curwin->w_p_rlc == 's') { if (cur == OUT_OF_TIME) { - vim_snprintf(t, STAT_BUF_LEN, "[?/??]"); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); } else if (cnt > 99 && cur > 99) { - vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]"); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]"); } else if (cnt > 99) { - vim_snprintf(t, STAT_BUF_LEN, "[>99/%d]", cur); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/%d]", cur); } else { - vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cnt, cur); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cnt, cur); } } else { if (cur == OUT_OF_TIME) { - vim_snprintf(t, STAT_BUF_LEN, "[?/??]"); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); } else if (cnt > 99 && cur > 99) { - vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]"); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]"); } else if (cnt > 99) { - vim_snprintf(t, STAT_BUF_LEN, "[%d/>99]", cur); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>99]", cur); } else { - vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cur, cnt); + vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cur, cnt); } } len = STRLEN(t); - if (show_top_bot_msg && len + 3 < STAT_BUF_LEN) { + if (show_top_bot_msg && len + 3 < SEARCH_STAT_BUF_LEN) { STRCPY(t + len, " W"); len += 2; } From 2d567ac47ea48019e5e70c8b01b8be0be863a1ca Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 16:34:03 +0900 Subject: [PATCH 07/10] vim-patch:8.1.1350: "W" for wrapping not shown when more than 99 matches Problem: "W" for wrapping not shown when more than 99 matches. Solution: Adjust check for length. (Masato Nishihata, closes vim/vim#4388) https://github.com/vim/vim/commit/dc6855af974f2ef553aceee619fadcb858e25d39 --- src/nvim/search.c | 2 +- src/nvim/testdir/test_search_stat.vim | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 35875e1cd6..e1465258ed 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -4286,7 +4286,7 @@ static void search_stat(int dirc, pos_T *pos, } len = STRLEN(t); - if (show_top_bot_msg && len + 3 < SEARCH_STAT_BUF_LEN) { + if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN) { STRCPY(t + len, " W"); len += 2; } diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 107cd54a0e..322d137e2e 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -40,12 +40,20 @@ func! Test_search_stat() let g:a = execute(':unsilent :norm! n') let stat = '\[>99/>99\]' call assert_match(pat .. stat, g:a) + call cursor(line('$'), 1) + let g:a = execute(':unsilent :norm! n') + let stat = '\[1/>99\] W' + call assert_match(pat .. stat, g:a) " 5) Many matches call cursor(1, 1) let g:a = execute(':unsilent :norm! n') let stat = '\[2/>99\]' call assert_match(pat .. stat, g:a) + call cursor(1, 1) + let g:a = execute(':unsilent :norm! N') + let stat = '\[>99/>99\] W' + call assert_match(pat .. stat, g:a) " 6) right-left if exists("+rightleft") From ec671c7048b4139c3323e7ff3932f93b62751edc Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 17:08:29 +0900 Subject: [PATCH 08/10] vim-patch:8.1.1390: search stats are off when using count or offset Problem: Search stats are off when using count or offset. Solution: Recompute the stats when needed. (Masato Nishihata, closes vim/vim#4410) https://github.com/vim/vim/commit/8f46e4c4bde13fd5ad68a6670b79cc462b65fbec --- src/nvim/search.c | 17 ++++++++--- src/nvim/testdir/test_search_stat.vim | 43 +++++++++++++++++++-------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index e1465258ed..2c2484169b 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1000,6 +1000,7 @@ int do_search( char_u *ps; char_u *msgbuf = NULL; size_t len; + bool has_offset = false; #define SEARCH_STAT_BUF_LEN 12 /* @@ -1280,7 +1281,9 @@ int do_search( * Add character and/or line offset */ if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';')) { - if (spats[0].off.line) { /* Add the offset to the line number. */ + pos_T org_pos = pos; + + if (spats[0].off.line) { // Add the offset to the line number. c = pos.lnum + spats[0].off.off; if (c < 1) pos.lnum = 1; @@ -1306,6 +1309,9 @@ int do_search( break; } } + if (!equalpos(pos, org_pos)) { + has_offset = true; + } } // Show [1/15] if 'S' is not in 'shortmess'. @@ -1315,7 +1321,8 @@ int do_search( && c != FAIL && !shortmess(SHM_SEARCHCOUNT) && msgbuf != NULL) { - search_stat(dirc, &pos, show_top_bot_msg, msgbuf); + search_stat(dirc, &pos, show_top_bot_msg, msgbuf, + (count != 1 || has_offset)); } // The search command can be followed by a ';' to do another search. @@ -4199,8 +4206,9 @@ int linewhite(linenr_T lnum) } // Add the search count "[3/19]" to "msgbuf". +// When "recompute" is true Always recompute the numbers. static void search_stat(int dirc, pos_T *pos, - bool show_top_bot_msg, char_u *msgbuf) + bool show_top_bot_msg, char_u *msgbuf, bool recompute) { int save_ws = p_ws; int wraparound = false; @@ -4224,7 +4232,8 @@ static void search_stat(int dirc, pos_T *pos, && STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat) && equalpos(lastpos, curwin->w_cursor) - && lbuf == curbuf) || wraparound || cur < 0 || cur > 99) { + && lbuf == curbuf) + || wraparound || cur < 0 || cur > 99 || recompute) { cur = 0; cnt = 0; clearpos(&lastpos); diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 322d137e2e..0f52158560 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -11,7 +11,7 @@ func! Test_search_stat() " Append 50 lines with text to search for, "foobar" appears 20 times call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10)) - " 1) match at second line + " match at second line call cursor(1, 1) let @/ = 'fo*\(bar\?\)\?' let g:a = execute(':unsilent :norm! n') @@ -19,13 +19,13 @@ func! Test_search_stat() let pat = escape(@/, '()*?'). '\s\+' call assert_match(pat .. stat, g:a) - " 2) Match at last line + " Match at last line call cursor(line('$')-2, 1) let g:a = execute(':unsilent :norm! n') let stat = '\[50/50\]' call assert_match(pat .. stat, g:a) - " 3) No search stat + " No search stat set shortmess+=S call cursor(1, 1) let stat = '\[2/50\]' @@ -33,7 +33,7 @@ func! Test_search_stat() call assert_notmatch(pat .. stat, g:a) set shortmess-=S - " 4) Many matches + " Many matches call cursor(line('$')-2, 1) let @/ = '.' let pat = escape(@/, '()*?'). '\s\+' @@ -45,7 +45,7 @@ func! Test_search_stat() let stat = '\[1/>99\] W' call assert_match(pat .. stat, g:a) - " 5) Many matches + " Many matches call cursor(1, 1) let g:a = execute(':unsilent :norm! n') let stat = '\[2/>99\]' @@ -55,7 +55,7 @@ func! Test_search_stat() let stat = '\[>99/>99\] W' call assert_match(pat .. stat, g:a) - " 6) right-left + " right-left if exists("+rightleft") set rl call cursor(1,1) @@ -67,7 +67,7 @@ func! Test_search_stat() set norl endif - " 7) right-left bottom + " right-left bottom if exists("+rightleft") set rl call cursor('$',1) @@ -78,7 +78,7 @@ func! Test_search_stat() set norl endif - " 8) right-left back at top + " right-left back at top if exists("+rightleft") set rl call cursor('$',1) @@ -90,7 +90,7 @@ func! Test_search_stat() set norl endif - " 9) normal, back at bottom + " normal, back at bottom call cursor(1,1) let @/ = 'foobar' let pat = '?foobar\s\+' @@ -100,7 +100,7 @@ func! Test_search_stat() call assert_match('search hit TOP, continuing at BOTTOM', g:a) call assert_match('\[20/20\] W', Screenline(&lines)) - " 10) normal, no match + " normal, no match call cursor(1,1) let @/ = 'zzzzzz' let g:a = '' @@ -114,7 +114,26 @@ func! Test_search_stat() call assert_false(1) endtry - " 11) normal, n comes from a mapping + " with count + call cursor(1, 1) + let @/ = 'fo*\(bar\?\)\?' + let g:a = execute(':unsilent :norm! 2n') + let stat = '\[3/50\]' + let pat = escape(@/, '()*?'). '\s\+' + call assert_match(pat .. stat, g:a) + let g:a = execute(':unsilent :norm! 2n') + let stat = '\[5/50\]' + call assert_match(pat .. stat, g:a) + + " with offset + call cursor(1, 1) + call feedkeys("/fo*\\(bar\\?\\)\\?/+1\", 'tx') + let g:a = execute(':unsilent :norm! n') + let stat = '\[5/50\]' + let pat = escape(@/ .. '/+1', '()*?'). '\s\+' + call assert_match(pat .. stat, g:a) + + " normal, n comes from a mapping " Need to move over more than 64 lines to trigger char_avail(. nnoremap n nzv call cursor(1,1) @@ -130,7 +149,7 @@ func! Test_search_stat() call assert_match(pat .. stat, g:b) unmap n - " 11) normal, but silent + " normal, but silent call cursor(1,1) let @/ = 'find this' let pat = '/find this\s\+' From 526382861495f92dc1e95a86d4a9b545459b7747 Mon Sep 17 00:00:00 2001 From: erw7 Date: Mon, 27 May 2019 16:41:50 +0900 Subject: [PATCH 09/10] vim-patch:8.1.1375: without "TS" in 'shortmess' get a hit-enter prompt often Problem: Without "TS" in 'shortmess' get a hit-enter prompt often. Solution: Always truncate the search message. Also avoid putting it in the message history. (closes vim/vim#4413) https://github.com/vim/vim/commit/984f031fb02fe301a8dbf8a35b871c9f60b8f61e --- src/nvim/search.c | 56 ++++++++++++++++----------- src/nvim/testdir/test_search_stat.vim | 3 ++ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index 2c2484169b..d08385f4c2 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1131,10 +1131,32 @@ int do_search( if ((options & SEARCH_ECHO) && messaging() && !cmd_silent && msg_silent == 0) { char_u *trunc; + char_u off_buf[40]; + int off_len = 0; // Compute msg_row early. msg_start(); + // Get the offset, so we know how long it is. + if (spats[0].off.line || spats[0].off.end || spats[0].off.off) { + p = off_buf; + *p++ = dirc; + if (spats[0].off.end) { + *p++ = 'e'; + } else if (!spats[0].off.line) { + *p++ = 's'; + } + if (spats[0].off.off > 0 || spats[0].off.line) { + *p++ = '+'; + } + *p = NUL; + if (spats[0].off.off != 0 || spats[0].off.line) { + snprintf((char *)p, sizeof(off_buf) - 1 - (p - off_buf), + "%" PRId64, spats[0].off.off); + } + off_len = STRLEN(off_buf); + } + if (*searchstr == NUL) { p = spats[last_idx].pat; } else { @@ -1143,7 +1165,9 @@ int do_search( if (!shortmess(SHM_SEARCHCOUNT)) { // Reserve enough space for the search pattern + offset + - // search stat. + // search stat. Use all the space available, so that the + // search state is right aligned. If there is not enough space + // msg_strtrunc() will shorten in the middle. if (msg_scrolled != 0) { // Use all the columns. len = (int)(Rows - msg_row) * Columns - 1; @@ -1151,12 +1175,12 @@ int do_search( // Use up to 'showcmd' column. len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; } - if (len < STRLEN(p) + SEARCH_STAT_BUF_LEN + 1) { - len = STRLEN(p) + SEARCH_STAT_BUF_LEN + 1; + if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3) { + len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3; } } else { // Reserve enough space for the search pattern + offset. - len = STRLEN(p) + 40; + len = STRLEN(p) + off_len + 3; } msgbuf = xmalloc((int)len); @@ -1172,25 +1196,11 @@ int do_search( } else { memmove(msgbuf + 1, p, STRLEN(p)); } - if (spats[0].off.line || spats[0].off.end || spats[0].off.off) { - p = msgbuf + STRLEN(p) + 1; - *p++ = dirc; - if (spats[0].off.end) { - *p++ = 'e'; - } else if (!spats[0].off.line) { - *p++ = 's'; - } - if (spats[0].off.off > 0 || spats[0].off.line) { - *p++ = '+'; - } - if (spats[0].off.off != 0 || spats[0].off.line) { - int l = 0; - l = sprintf((char *)p, "%ld", spats[0].off.off); - p[l] = ' '; // remove NUL from sprintf - } + if (off_len > 0) { + memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len); } - trunc = msg_strtrunc(msgbuf, FALSE); + trunc = msg_strtrunc(msgbuf, true); if (trunc != NULL) { xfree(msgbuf); msgbuf = trunc; @@ -4311,8 +4321,10 @@ static void search_stat(int dirc, pos_T *pos, lbuf = curbuf; lastpos = p; - // keep the message even after redraw + // keep the message even after redraw, but don't put in history + msg_hist_off = true; give_warning(msgbuf, false); + msg_hist_off = false; } p_ws = save_ws; } diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 0f52158560..cf36f3214a 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -13,11 +13,14 @@ func! Test_search_stat() " match at second line call cursor(1, 1) + let messages_before = execute('messages') let @/ = 'fo*\(bar\?\)\?' let g:a = execute(':unsilent :norm! n') let stat = '\[2/50\]' let pat = escape(@/, '()*?'). '\s\+' call assert_match(pat .. stat, g:a) + " didn't get added to message history + call assert_equal(messages_before, execute('messages')) " Match at last line call cursor(line('$')-2, 1) From 2a4e8a427e5e84f8e8a9477c0bfb8c1d376b3f1e Mon Sep 17 00:00:00 2001 From: erw7 Date: Fri, 7 Jun 2019 09:03:15 +0900 Subject: [PATCH 10/10] vim-patch:8.1.1475: search string not displayed when 'rightleft' is set Problem: Search string not displayed when 'rightleft' is set. Solution: Clear the right part of the old text. (closes vim/vim#4488, closes vim/vim#4489) https://github.com/vim/vim/commit/db294adc65d73ffa5cdf3d0ab45ccbf05b965414 --- src/nvim/search.c | 9 +++++++-- src/nvim/testdir/test_search.vim | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/nvim/search.c b/src/nvim/search.c index d08385f4c2..4d02a07cbd 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -1218,9 +1218,14 @@ int do_search( while (*r != NUL && *r == ' ') { r++; } - memmove(msgbuf, r, msgbuf + STRLEN(msgbuf) - r); + size_t pat_len = msgbuf + STRLEN(msgbuf) - r; + memmove(msgbuf, r, pat_len); // overwrite old text - memset(r, ' ', msgbuf + STRLEN(msgbuf) - r); + if ((size_t)(r - msgbuf) >= pat_len) { + memset(r, ' ', pat_len); + } else { + memset(msgbuf + pat_len, ' ', r - msgbuf); + } } msg_outtrans(msgbuf); msg_clr_eos(); diff --git a/src/nvim/testdir/test_search.vim b/src/nvim/testdir/test_search.vim index f4fe4051e3..87cad241e2 100644 --- a/src/nvim/testdir/test_search.vim +++ b/src/nvim/testdir/test_search.vim @@ -609,3 +609,25 @@ func Test_search_match_at_curpos() close! endfunc + +func Test_search_display_pattern() + new + call setline(1, ['foo', 'bar', 'foobar']) + + call cursor(1, 1) + let @/ = 'foo' + let pat = escape(@/, '()*?'. '\s\+') + let g:a = execute(':unsilent :norm! n') + call assert_match(pat, g:a) + + " right-left + if exists("+rightleft") + set rl + call cursor(1, 1) + let @/ = 'foo' + let pat = 'oof/\s\+' + let g:a = execute(':unsilent :norm! n') + call assert_match(pat, g:a) + set norl + endif +endfunc