This patch supports highlighting all matched text while incremental searching.
By removing SEARCH_KEEP flag, command line text is stored as last regexp
pattern and used for 'hlsearch' feture while incremental searching.
This patch is based on @itchyny's patch1. itchyny's patch didn't handle
cancelling incremental search by <Esc>
or <C-c>
and command line text is
stored as last pattern even if users cancell the search.
This patch fix this problem by store/restore the search pattern.
Also, I added 'inchlsearch' ("inc"remental "hlsearch") option to turn on/off this feature.
I'm the author of https://github.com/haya14busa/incsearch.vim which provide
similar functionality.
Actually, I'm satisfied with this plugin but I believe this feature is so
useful that it should be implemented with default vim :)
https://github.com/vim/vim/pull/2198
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
We can enable this feature without new 'inchlsearch' option and regardless 'hlsearch' with following patch.
But, I wonder is it really ok to make it default behavior. IncSearch and Search highlight is similar in some colorscheme and when you type .
, all characters will be highlighted.
I thought someone may feel it's annoying behavior.
What do you think?
diff --git a/src/ex_getln.c b/src/ex_getln.c index 578a7c5fd..adc226fbf 100644 --- a/src/ex_getln.c +++ b/src/ex_getln.c @@ -1681,10 +1681,12 @@ getcmdline( { pos_T t; int search_flags = SEARCH_NOOF + SEARCH_PEEK; + int save_p_hls = p_hls; if (char_avail()) continue; save_last_search_pattern(); + p_hls = TRUE; cursor_off(); out_flush(); if (c == Ctrl_G) @@ -1694,8 +1696,6 @@ getcmdline( } else t = match_start; - if (!p_ihls) - search_flags += SEARCH_KEEP; ++emsg_off; i = searchit(curwin, curbuf, &t, c == Ctrl_G ? FORWARD : BACKWARD, @@ -1748,6 +1748,7 @@ getcmdline( old_botline = curwin->w_botline; update_screen(NOT_VALID); restore_last_search_pattern(); + p_hls = save_p_hls; redrawcmdline(); } else @@ -1896,6 +1897,7 @@ cmdline_changed: #ifdef FEAT_RELTIME proftime_T tm; #endif + int save_p_hls = p_hls; /* if there is a character waiting, search and redraw later */ if (char_avail()) @@ -1906,6 +1908,7 @@ cmdline_changed: incsearch_postponed = FALSE; curwin->w_cursor = search_start; /* start at old position */ save_last_search_pattern(); + p_hls = TRUE; /* If there is no command line, don't do anything */ if (ccline.cmdlen == 0) @@ -1923,8 +1926,6 @@ cmdline_changed: /* Set the time limit to half a second. */ profile_setlimit(500L, &tm); #endif - if (!p_ihls) - search_flags += SEARCH_KEEP; i = do_search(NULL, firstc, ccline.cmdbuff, count, search_flags, #ifdef FEAT_RELTIME @@ -1984,6 +1985,7 @@ cmdline_changed: update_screen(SOME_VALID); restore_cmdline(&save_ccline); restore_last_search_pattern(); + p_hls = save_p_hls; /* Leave it at the end to make CTRL-R CTRL-W work. */ if (i != 0)
—
You are receiving this because you commented.
did you check the build failure?
—
You are receiving this because you commented.
I don't think it is related to this PR, because the master branch also failed with the same error.
Not sure why lua fails.
—
You are receiving this because you commented.
It looks like #2203 will fix the build failure. Will update this pull-request to include the fix once #2203 included. (I ran CI against my fork repo and I confirmed CI passed in OSX too. haya14busa#5)
—
You are receiving this because you commented.
@haya14busa pushed 1 commit.
—
You are receiving this because you are subscribed to this thread.
Merging #2198 into master will decrease coverage by
0.04%
.
The diff coverage is100%
.
@@ Coverage Diff @@ ## master #2198 +/- ## ========================================== - Coverage 74.34% 74.29% -0.05% ========================================== Files 90 90 Lines 131984 132006 +22 Branches 28988 30871 +1883 ========================================== - Hits 98121 98079 -42 - Misses 33863 33923 +60 - Partials 0 4 +4
Impacted Files | Coverage Δ | |
---|---|---|
src/option.c | 84.47% <ø> (ø) |
⬆️ |
src/search.c | 73.71% <100%> (+0.12%) |
⬆️ |
src/ex_getln.c | 72.94% <100%> (+0.57%) |
⬆️ |
src/memline.c | 70.81% <0%> (-3.42%) |
⬇️ |
src/if_xcmdsrv.c | 83.63% <0%> (-0.9%) |
⬇️ |
src/fold.c | 84.66% <0%> (-0.72%) |
⬇️ |
src/ex_cmds2.c | 80.21% <0%> (-0.3%) |
⬇️ |
src/term.c | 51.2% <0%> (-0.22%) |
⬇️ |
src/message.c | 68.26% <0%> (-0.2%) |
⬇️ |
src/if_py_both.h | 76.09% <0%> (-0.15%) |
⬇️ |
... and 14 more |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 1d68d9b...a552edd. Read the comment docs.
—
You are receiving this because you commented.
I added test for highlight while incremental searching by runnning vim with term_start()
.
I cannot find a way to test highlight while searching with Vim itself because
"redraw" command while searching turn of incserach and inchlsearch highlight.
We cannot test gVim with term_start, but I think it's better than nothing :)
—
You are receiving this because you commented.
@haya14busa pushed 1 commit.
—
You are receiving this because you are subscribed to this thread.
Hi, Bram!
Could you tell me your thought about this patch?
I understand bug fixes have higher priority than new feature like this and I
won't say please merge it right now, but I want to hear your opinion and
improve this patch if needed.
(I updated the patch to fix the conflict with recent changes)
—
You are receiving this because you commented.
@yegappan commented on this pull request.
> @@ -4386,6 +4386,17 @@ A jump table for the options with a short description can be found at |Q_op|. default now. This should work fine for most people, however if you have any problem with it, try using on-the-spot style. + *'inchlsearch'* *'ihls'* *'noinchlsearch'* *'noihls'* +'inchlsearch' 'ihls' boolean (default off) + global + {not in Vi} + {not available when compiled without the + |+extra_search| features} + While typing a search command, all matched strings are highlighted. + It works only when both 'incsearch' and 'hlsearch' are on. The type of
The help text needs to be updated. The 'hlsearch' option is not needed
to enable this feature.
—
You are receiving this because you commented.
@yegappan commented on this pull request.
Few help files need to be updated to refer to this new option.
The eval.txt help file needs to be updated to include this option. Search for 'extra_search' in eval.txt.
Remove the following item from todo.txt:
Should be easy to highlight all matches with 'incsearch'. Idea by Itchyny,
2015 Feb 6.
Add some text about this feature to usr_03.txt under "TUNING SEARCHES".
Add a reference to this option in various.txt under "+extra_search".
—
You are receiving this because you commented.
—
You are receiving this because you commented.
If this patch relied on incsearch
options then old behavior could not be preserved even with CmdlineEnter
and CmdlineLeave
events. It would be a pithy for those users who use search mostly as navigation tool.
I think idea of using hlsearch
and autocommands is the best. It allows preservation of old behavior and is consistent with current current hlsearch
option description.
—
You are receiving this because you commented.
@haya14busa pushed 4 commits.
—
You are receiving this because you are subscribed to this thread.
Thanks for the responses!
I'll add the CmdlineEnter and CmdlineLeave autocommand events, these are
useful for other things as well.
This is really great!
Now that we have Cmdline{Enter,Leave}, I also believe combination of 'incsearch' and 'incsearch' is enough instead of adding another option.
Users can control the behavior on their own.
I updated the patch. Changed document and added one additional test (test highlight while searching with &hlsearch == false
.
augroup vimrc-incsearch-highlight
+ autocmd!
+ autocmd CmdlineEnter * :if expand('<afile>') == '/' | set hlsearch | endif
+ autocmd CmdlineLeave * :if expand('<afile>') == '/' | set nohlsearch | endif
+ augroup END
That does not work as expected. I tried this configuration when the patch was released. It does enable hlsearch when you search something (actually, only when typing the first char after entering search mode (e.g. not after /
but only after you enter the first character to search for)). However, the way it works is, it will highlight the last used search pattern and not the currently entered pattern. And once we finish the search, it will be disabled.
You can try this: vim --clean -c ':h'
. Enter the autocommand au CmdlineEnter * :if expand('<afile>') == '/' | set hlsearch | endif
, and then do :let @/='.*'
. Now enter /vim
. Note, highlighting starts only after pressing v
. Note also, it highlights the last search pattern .*
instead of the typed one v
and therefore highlights the complete text. Once you have typed vim
and pressed enter the highlight vanishes, so you never actually see only vim
highlighted.
Of course this is not a problem with your patch, I am just pointing out, that this does not work as expected.
BTW: You actually want rather this in the documentation: :if expand('<afile>') =~ '[/?]'
—
@chrisbra I was confused with your report. I couldn't reproduce it nor understand what you mean. But on second thought, the report totally makes sense if I assume you didn't apply the latest attached patch. Can you confirm that you used the latest attached patch?
BTW: You actually want rather this in the documentation: :if expand('') =~ '[/?]'
You can use / for the pattern instead:
autocmd CmdlineEnter / set hlsearch
Thanks! That's nice. I updated the patch.
But it does work when 'hlsearch' is always on?
Not sure what you mean with work...
I pulled the latest vim.git repo yesterday and it appears this patch was included (based on looking at the source files). However, I have both incsearch and hlsearch options set to On, but I don't get the new functionality provided by the patch. What am I missing?
it is not included yet.
Oh, that explains it. I guess when I pull'd what I thought was this patch based on a hash of 9fbe6fb, I didn't do it correctly and apparently got one of Bram's latest patches which was of course was already in the vim pull that I had done. Um, so how can I download the latest version of this patch?
Thanks very much for the reply.
Um, so how can I download the latest version of this patch?
Go to this link:
https://github.com/vim/vim/pull/2198.diff
Or:
git checkout -b haya14busa-incsearch-hi-all master
git pull https://github.com/haya14busa/vim.git incsearch-hi-all
Thanks very much for the link!
You don't have to checkout the contributors repository. You can also switch directly to a pull request using something like this:
git fetch origin pull/<ID>/head:branch
git checkout branch
Now you can build and test yourself.
See also the github documentation:
https://help.github.com/articles/checking-out-pull-requests-locally/
Hi!
I added tests for incremental highlight of <C-g>
and <C-t>
(I found that I forgot to write them...).
I think the tests covered enough cases for this patch now.
git fetch origin pull//head:branch
git checkout branch
bwt, this works well for first time, but there is no tracking information (no
upstream branch) for the created branch, so you cannot update the branch by
simple git pull
command. Instead you need to run this following command.
$ git pull origin pull/2198/head
Not sure what you mean with work...
Work like the GIF I pasted on the top comment?
Sorry for repeating it, but I assume you didn't use the latest patch.
Can you confirm that you used the latest patch?
If your report is true, it's certainly the problem of this patch, not the problem of the autocmd.
So, I want to confirm that and want to fix if the problem is reproducible with latest code.
Thanks!
Sorry for repeating it, but I assume you didn't use the latest patch.
that was unrelated to your patch and was only a comment that setting the 'hlsearch' setting on CmdlineEnter
/ CmdlineLeave
autocommands won't work as expected.
Ok, then what is your expectation?
Last pattern should be highlighted right after pressing search command (/
) instead of first character?
Or, you mentioned
it highlights the last search pattern .* instead of the typed one v
but, without my patch, it's totally expected behavior. Incremental searching won't change the last search pattern.
autocmd CmdlineEnter [/\?] :set hlsearch | redraw
By calling :redraw
, last pattern will be highlighted right after pressing /
or ?
.
Other behavior you reported seems expected behavior. When ':set hlsearchand
:set incsearch`, last pattern is highlighted same as before. The autocmd just turn on and off 'hlsearch' when entering/leaving cmdline.
Thank you for including it! ✨
@duff commented on this pull request.
> @@ -4447,7 +4447,17 @@ A jump table for the options with a short description can be found at |Q_op|. match may not be found. This is to avoid that Vim hangs while you are typing the pattern. The highlighting can be set with the 'i' flag in 'highlight'. - See also: 'hlsearch'. + When 'hlsearch' is on, all matched strings are highlighted too while typing + a search command. See also: 'hlsearch'. + If you don't want turn 'hlsearch' on, but want to highlight all matches + while searching, you can turn on and off 'hlsearch' with autocmd. + Example: > + augroup vimrc-incsearch-highlight
Thank you for this option! It works great!
The augroup provided in description of 'incsearch' doesn't work on Windows.
yeah, it looks like this:
autocmd CmdlineEnter [/\?] :set hlsearch
does not match on Windows. However this works:
autocmd CmdlineEnter /,\? :set hlsearch
autocmd CmdlineLeave /,\? :set nohlsearch
Why is backslash needed? It works without it.
@xtal8 I think that the question mark has a special meaning inside the pattern used in an autocmd. It matches any character. But here, you don't want the special meaning, you want a literal question mark. The one standing for a search command line you entered after pressing ?
while in normal mode. Otherwise ?
would match all symbols standing for any Ex command line (see :h cmdwind-char
).
I think it's used in a sense of wildcards not patterns. But when I tried to use autocommand with only ?
(without backslahs) hlsearch wasn't enabled inside a search started with /
. That's why I asked.
Hi all, I've noticed that after updating to vim 8.0.1250
, my search behavior was a little weird. I think it's related to this patch, but it seems
/\v
matches every character in a file, where it didn't previously. When I got to search now (using my nnoremap / /\v
), the entire file is highlighted by incsearch
and hlsearch
until I start typing my pattern. Further, if I hit <CR>
, pressing n
will simply take me character-by-character through the file.
This didn't occur before. Is this considered intended behavior (i.e. that an empty search pattern with the very-magic
flag is a match for every character?
2017-11-18(Sat) 14:40:37 UTC+9 David Ben Knoble:
> Hi all, I've noticed that after updating to vim 8.0.1250, my search behavior was a little weird. I think it's related to this patch, but it seems
>
> /\v
>
> matches every character in a file, where it didn't previously. When I got to search now (using my nnoremap / /\v), the entire file is highlighted by incsearch and hlsearch until I start typing my pattern. Further, if I hit <CR>, pressing n will simply take me character-by-character through the file.
>
> This didn't occur before. Is this considered intended behavior (i.e. that an empty search pattern with the very-magic flag is a match for every character?
Your reported issue is discussed in following thread.
https://groups.google.com/d/topic/vim_dev/YlKJq45uYY8/discussion
--
Best regards,
Hirohito Higashi (h_east)
@benknoble I saw the same behavior. For now at least, I removed my nnoremap / /\v
mapping.
It is discussed at #2337.