qstrahl / vim-matchmaker

Highlight matches for the word under the cursor
38 stars 10 forks source link

Add option to not override search highlight #17

Open xanderdunn opened 9 years ago

xanderdunn commented 9 years ago

Currently, if my cursor is on top of the word that appears in my search, it will be highlighted according to Matchmaker's highlight setting, but ignore the search highlight. See in this example where my cursor is over "brew" and I also did a search for "brew": screen shot 2015-01-03 at 16 55 47 Instances of "brew" that are not exact matches are still search highlighted orange, while exact matches are highlighted

In the case where the Matchmaker highlight need not conflict with the search highlight (Can have both underline and background color highlights), I would love for Matchmaker to show both highlights rather than just Matchmaker's highlight. In the case above, I would see both an underline and an orange background highlight.

Perhaps you could look at the current search buffer, see if it contains the word the cursor is currently on top of, and if so, apply both the search highlight as well as Matchmaker's highlight?

qstrahl commented 9 years ago

Yikes that's really a doozy. I'm not sure if it's possible but I will take a look at it.

On Sat, 3 Jan 2015 20:23 Alex Dunn notifications@github.com wrote:

Currently, if my cursor is on top of the word that appears in my search, it will be highlighted according to Matchmaker's highlight setting, but ignore the search highlight. See in this example where my cursor is over "brew" and I also did a search for "brew": [image: screen shot 2015-01-03 at 16 55 47] https://cloud.githubusercontent.com/assets/1313618/5604313/cf2b2a48-9369-11e4-97f2-329982de3a79.png Instances of "brew" that are not exact matches are still search highlighted orange, while exact matches are highlighted

In the case where the Matchmaker highlight need not conflict with the search highlight (Can have both underline and background color highlights), I would love for Matchmaker to show both highlights rather than just Matchmaker's highlight. In the case above, I would see both an underline and an orange background highlight.

Perhaps you could look at the current search buffer, see if it contains the word the cursor is currently on top of, and if so, apply both the search highlight as well as Matchmaker's highlight?

— Reply to this email directly or view it on GitHub https://github.com/qstrahl/vim-matchmaker/issues/17.

tommcdo commented 9 years ago

Not every two highlighting styles can be mixed. For example, I'm using the same color scheme as you are, so my search highlight uses a yellow background. However, I've customized my Matchmaker highlight to use a gray background. In this case, it's infeasible to display both highlighting styles.

There's another issue. Since Vim can only assign one highlight group to a region of text, you'd have to figure out the intersection of the two patterns (cword and search pattern). I won't say it's impossible, but I anticipate it's extremely difficult at the least, and might add some noticeable computational expense.

xanderdunn commented 9 years ago

I think we can mix underline, undercurl, and foreground color easily with background color. If it doesn't fit that situation, then we can fall back to Matchmaker's current behavior.

It looks like vim applies highlight groups to regular expressions? What about using the & operator to look for things that satisfy both expressions?

I think it's simplified by the fact that Mathmaker will only ever search for whole words.

Or, what about searching using Matchmaker's search word, finding all locations that have that search word, and then asking what highlight group is currently applied to that word? Then mix the highlight groups according to the above.

xanderdunn commented 9 years ago

Here is some vimscript that looks up the highlight group at a certain location:

return synIDattr(synID(line("."),col("."),1),"name")
tommcdo commented 9 years ago

I was thinking more about the idea of blending syntax groups. The ideas you're bringing up could lead somewhere, but I'm wondering if it might be more sane to allow the user to define a syntax group to use when some text matches the cursor word and the search pattern. This is more friendly to the case that the Matchmaker group can't be programmatically blended with the search group.

xanderdunn commented 9 years ago

I suppose that would work.

My original thought was to prevent Mathmaker from removing syntax highlighting that it shouldn't be removing. If we check how a certain word is already highlighted and then add Matchmaker's highlights onto it where possible, then it would be a much more general solution that deals with highlighting from anything else, not just from search highlights.

Are there people who would want to see three different highlights: Matchmaker highlighter, search highlight, and matchmaker + search highlight? I've never seen an IDE do that.

But, if we were to implement it this way, then I could simply define that third highlight group to be the mix of my search highlight group and my Matchmaker highlight group. That would solve it for me 99% of the time.

tommcdo commented 9 years ago

I've also been thinking about the overlapping pattern problem. Consider the following case:

If the text foobar fight occurs, we have foo highlighted by the Matchmaker group, fight highlighted by the search group, and bar highlighted by both. It will definitely be tricky to determine what pattern will be highlighted by both groups.

An alternative approach could be to get a list of positions that match the cursor word and cross-reference it against a list of positions that match the search pattern, and then add a new highlight match for the specific position ranges that intersect. This would be pretty expensive, and not very tolerant of the text changing. All in all, this approach is awful, but I bring it up to reinforce that the most reliable would probably have to be determining the pattern that matches the intersection.

Anyway, this is an interesting problem and I'll keep it in mind for a while.

tommcdo commented 9 years ago

To address your last comment, Vim doesn't do syntax highlighting by listing what style properties to add to text; it does so by saying which group to apply to which patterns. As far as I can tell, there's no reasonable way to append style properties to those of an existing highlight group.

tommcdo commented 9 years ago

I want to clear up a possible confusion. Matchmaker isn't removing highlighting from portions of text that are highlight by the search pattern. It's just applying a new highlight group to the text that matches the cursor word. At any given time, only one highlight group is displayed for a region of text: even if other groups are defined by patterns that match the text, only one (probably the one that is defined latest) is visible.

xanderdunn commented 9 years ago

I want to clear up a possible confusion. Matchmaker isn't removing highlighting from portions of text that are highlight by the search pattern. It's just applying a new highlight group to the text that matches the cursor word. At any given time, only one highlight group is displayed for a region of text: even if other groups are defined by patterns that match the text, only one (probably the one that is defined latest) is visible.

Yes, I understand. From the user's viewpoint, Matchmaker is removing highlighting. That's what I mean.

To address your last comment, Vim doesn't do syntax highlighting by listing what style properties to add to text; it does so by saying which group to apply to which patterns.

Yeah, I don't mean appending to existing highlight groups. Just define a third highlight group that is a mixture of the first two (or whatever you want) and apply that to the text.

xanderdunn commented 9 years ago

We might call something external that computes regular expression intersection. For example.

tommcdo commented 9 years ago

I think intersection is the wrong word for what we need. Going back to my example, the string bar matches neither pattern, but it is a substring of text matching from each pattern.

xanderdunn commented 9 years ago

Oh, yes. Completely correct. Good example. We need the intersection of the matching results, even when that's a substring that, by itself, wouldn't match either expression.

matt1003 commented 8 years ago

I believe the "option to not override search highlight" already exists; as this can be achieved by setting matchmaker_matchpriority to a negative value. According to :help matchadd, the hlsearch has a priority of zero, and will not be overridden by matches with lower priorities (yes, a negative priority value is allowed).

This wont help combine multiple highlight groups (i.e. to combine the underline with the background highlighting); however it is very useful for those who use a high contrast color for hlsearch vs a low contrast color for vim-matchmaker.