wincent / loupe

🔍 Enhanced in-file search for Vim
BSD 2-Clause "Simplified" License
144 stars 6 forks source link

Searching for * breaks loupe #13

Closed samuelallan72 closed 7 years ago

samuelallan72 commented 7 years ago

If I run /\v*, loupe errors out with

Error detected while processing function loupe#hlmatch:                                                                                                                                                                                                                                                                        
line   32:                                                                                                                                                                                                                                                                                                                     
E866: (NFA regexp) Misplaced *                                                                                                                                                                                                                                                                                                 
Error detected while processing function loupe#hlmatch:                                                                                                                                                                                                                                                                        
line   32:                                                                                                                                                                                                                                                                                                                     
E64: * follows nothing                                                                                                                                                                                                                                                                                                         
Error detected while processing function loupe#hlmatch:                                                                                                                                                                                                                                                                        
line   32:                                                                                                                                                                                                                                                                                                                     
E475: Invalid argument: \c\%#\v*  

If I run /*, loupe highlights everything with the highlighting for match under the cursor.

Maybe regex in searches isn't being escaped correctly?

wincent commented 7 years ago

It's true that it doesn't handle that case very gracefully, but searching for /\v* doesn't make any sense anyway, so I am not sure how much work I should put in to making Loupe handle it better.

What are you expecting to find when you search for /\v*? The help for /\v says that * is supposed to match "any number of the previous atom". There is no previous atom though, so I suspect the behavior is undefined, or at least not well-defined.

Starting Vim without Loupe (vim -U NONE and let g:LoupeLoaded=1 in the ~/.vimrc) and searching for /\v* shows: E486: Pattern not found: \v* — admittedly a different error than you're seeing under Loupe, but an error nonetheless.

wincent commented 7 years ago

Having said that, I think I can at least suppress this error message with a try/catch.

samuelallan72 commented 7 years ago

Yep, that's correct, but it's easy to forget the backslash escape... by the way, /\v* actually works for searching for * characters interestingly enough (I tried it in vim and neovim, without loading the vimrc).

Also, this commit fixes the error messages from loupe, but there is still something funny going on - if you do a non-magic search for * (/*), loupe highlights all the text in the file with the highlighting that is supposed to be for the match under the cursor.

wincent commented 7 years ago

... loupe highlights all the text in the file with the highlighting that is supposed to be for the match under the cursor.

That may be a tricky one to solve. The reason why that's happening is because it is trying to highlight any matching item under the cursor with matchadd(), and it does so by constructing a pattern out of \c\%# (that is, \c to turn on case-insensitive matching, and \%# for current cursor position) plus the pattern you searched for (in this case, *).

So you wind up with a pattern of \c\%#*, which ends up highlighting everything because \%# is a zero-width match (see :h cursor-position)... maybe(???)

You can try it out with:

:let x=matchadd('IncSearch', '\c\%#*')
:call matchdelete(x)

The simplest pattern, matchadd('IncSearch', '*'), does not highlight everything (it only highlights literal *, as you would expect).

An even simpler repro, just search with /\%#* and if you have 'hlsearch' on you'll see then entire buffer highlighted.

Not sure how to solve this edge case (or if there are other edge cases) in a way that doesn't break the usefulness of \%# (which is pretty useful because it allows us to highlight only the match under the cursor).

samuelallan72 commented 7 years ago

@wincent thanks for looking into this. Maybe I'll have a play around sometime to see if I can come up with a full solution. Either way, the issue isn't likely to happen very often, so not very high priority - the error messages (which is now fixed) was the main thing! :+1: