Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@haya14busa
Copy link
Contributor

inchlsearch-demo

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 :)

@vim-ml
Copy link

vim-ml commented Oct 9, 2017 via email

@vim-ml
Copy link

vim-ml commented Oct 9, 2017 via email

@vim-ml
Copy link

vim-ml commented Oct 9, 2017 via email

@haya14busa
Copy link
Contributor Author

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)

@vim-ml
Copy link

vim-ml commented Oct 11, 2017 via email

@chrisbra
Copy link
Member

did you check the build failure?

@k-takata
Copy link
Member

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.

@haya14busa
Copy link
Contributor Author

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)

By removing SEARCH_KEEP flag, command line text is stored as last regexp
pattern and used for 'hlsearch' feture while incremental searching.

Add 'inchlsearch' ("inc"remental "hlsearch") option to turn on/off this feature.

This patch is based on itchyny's patch[1]. 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.

[1]: https://groups.google.com/forum/#!searchin/vim_dev/incsearch|sort:relevance/vim_dev/zdp8W-6xUzg/PrqDpP7cilIJ
@haya14busa haya14busa force-pushed the incsearch-hi-all branch 2 times, most recently from 18261aa to ec14f43 Compare October 14, 2017 10:07
@codecov-io
Copy link

codecov-io commented Oct 14, 2017

Codecov Report

Merging #2198 into master will increase coverage by 0.11%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2198      +/-   ##
==========================================
+ Coverage   74.31%   74.42%   +0.11%     
==========================================
  Files          90       90              
  Lines      132039   132089      +50     
  Branches    28994    29001       +7     
==========================================
+ Hits        98126    98310     +184     
+ Misses      33884    33750     -134     
  Partials       29       29
Impacted Files Coverage Δ
src/ex_getln.c 73.04% <100%> (+0.54%) ⬆️
src/search.c 73.71% <100%> (+0.12%) ⬆️
src/if_xcmdsrv.c 84.35% <0%> (-0.72%) ⬇️
src/if_perl.xs 85.61% <0%> (-0.45%) ⬇️
src/ex_cmds.c 77.64% <0%> (-0.33%) ⬇️
src/window.c 80.86% <0%> (-0.14%) ⬇️
src/quickfix.c 92.32% <0%> (-0.12%) ⬇️
src/ui.c 45.76% <0%> (-0.08%) ⬇️
src/fold.c 85.31% <0%> (-0.08%) ⬇️
src/ex_docmd.c 75.36% <0%> (-0.05%) ⬇️
... and 30 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 f8e8c06...ad1e575. Read the comment docs.

@haya14busa
Copy link
Contributor Author

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 :)

@haya14busa
Copy link
Contributor Author

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)

{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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help text needs to be updated. The 'hlsearch' option is not needed
to enable this feature.

Copy link
Member

@yegappan yegappan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

@brammool
Copy link
Contributor

brammool commented Oct 19, 2017 via email

@vim-ml
Copy link

vim-ml commented Oct 19, 2017 via email

@vim-ml
Copy link

vim-ml commented Oct 19, 2017 via email

@markonm
Copy link

markonm commented Oct 19, 2017

If this patch relied on incsearch option, 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.

@chrisbra
Copy link
Member

But it does work when 'hlsearch' is always on?

Not sure what you mean with work...

@mj-2017
Copy link

mj-2017 commented Oct 26, 2017

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?

@chrisbra
Copy link
Member

it is not included yet.

@mj-2017
Copy link

mj-2017 commented Oct 26, 2017

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.

@k-takata
Copy link
Member

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

@mj-2017
Copy link

mj-2017 commented Oct 26, 2017

Thanks very much for the link!

@chrisbra
Copy link
Member

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/

@haya14busa
Copy link
Contributor Author

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

@chrisbra

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!

@chrisbra
Copy link
Member

@haya14busa

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.

@haya14busa
Copy link
Contributor Author

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.

@haya14busa
Copy link
Contributor Author

haya14busa commented Oct 29, 2017

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 hlsearch and :set incsearch, last pattern is highlighted same as before. The autocmd just turn on and off 'hlsearch' when entering/leaving cmdline.

@brammool brammool closed this in 2e51d9a Oct 29, 2017
@haya14busa
Copy link
Contributor Author

Thank you for including it! ✨

@haya14busa haya14busa deleted the incsearch-hi-all branch October 29, 2017 22:54
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this option! It works great!

@markonm
Copy link

markonm commented Nov 1, 2017

The augroup provided in description of 'incsearch' doesn't work on Windows.

@chrisbra
Copy link
Member

chrisbra commented Nov 1, 2017

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

@markonm
Copy link

markonm commented Nov 1, 2017

Why is backslash needed? It works without it.

@markonm
Copy link

markonm commented Nov 1, 2017

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.

@benknoble
Copy link
Contributor

benknoble commented Nov 18, 2017

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)?

@duff
Copy link

duff commented Nov 18, 2017

@benknoble I saw the same behavior. For now at least, I removed my nnoremap / /\v mapping.

@k-takata
Copy link
Member

It is discussed at #2337.

adizero pushed a commit to adizero/vim that referenced this pull request May 19, 2018
Problem:    Incremental search only shows one match.
Solution:   When 'incsearch' and and 'hlsearch' are both set highlight all
            matches. (haya14busa, closes vim#2198)
ndhoule added a commit to ndhoule/vimrc that referenced this pull request Sep 19, 2018
Replaced by native functionality:

neovim/neovim#7463
vim/vim#2198

Set `hlsearch` and `incsearch` to get the same functionality natively.
janlazo added a commit to janlazo/neovim that referenced this pull request Jun 29, 2019
Problem:    Incremental search only shows one match.
Solution:   When 'incsearch' and and 'hlsearch' are both set highlight all
            matches. (haya14busa, closes vim/vim#2198)
vim/vim@2e51d9a
janlazo added a commit to janlazo/neovim that referenced this pull request Jun 29, 2019
Problem:    Incremental search only shows one match.
Solution:   When 'incsearch' and and 'hlsearch' are both set highlight all
            matches. (haya14busa, closes vim/vim#2198)
vim/vim@2e51d9a
teto pushed a commit to teto/neovim that referenced this pull request Jul 1, 2019
Problem:    Incremental search only shows one match.
Solution:   When 'incsearch' and and 'hlsearch' are both set highlight all
            matches. (haya14busa, closes vim/vim#2198)
vim/vim@2e51d9a
yuttie added a commit to yuttie/dot-vim that referenced this pull request Jan 9, 2023
> ⚠️ Deprecated This plugin is no longer necessary since the main functionality is now built into vim/neovim
>
> See:
> - https://medium.com/@haya14busa/incsearch-vim-is-dead-long-live-incsearch-2b7070d55250
> - vim/vim#2198
> - Successor plugin: https://github.com/haya14busa/is.vim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.