Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Restart Syntax Highlighting from Arbitrary Line

I have discovered an interesting edge-case in Vim syntax highlighting. Consider the following snippet from a company Makefile:

LDSCRIPT := $(subst ",,$(CONFIG_SYS_LDSCRIPT))

The above line simply removes double quotes from a given LDSCRIPT. Nothing syntactically wrong; the make runs as expected and without issue.

The Problem

Since the above line only contains one double quote, the highlighting rules mistakenly think that the rest of the body of the Makefile is quoted text and colors it as such. For simple Makefiles, this is an inconvenience; for 1KLOC+ Makefiles, this becomes a real hassle (especially since this preprocessing is near the top of the file).

The Question

Is there any way to either disable syntax highlighting based on lines that match some given regular expression (eg. subst[ \t]*['"],.*) or something similar? Failing that, is there some way to restart Vim's highlighting at some arbitrary line while preserving the highlights above?

If at all possible, I would like to avoid edits to the Makefile as this script is shared across a number of departments.

I am willing to write / modify vimscript to achieve this, however I have not done so before (to any reasonable degree). Any tips, pointers or other helpful hints would be much appreciated.

What I have Tried

:syntax sync minlines=1
:syntax sync fromstart
:syntax sync clear

None of the above seems to have any effect on the highlighting when run in the editor. Looking through the Vim help docs, it seems that :syn-sync-fourth may be able to do what I am after, however I am uncertain as to how this would function in an inverse manner (eg. to disable highlighting rather than to apply it).

like image 606
MysteryMoose Avatar asked Nov 05 '14 17:11

MysteryMoose


1 Answers

I think the best you can do is add an additional syntax rule (in ~/.vim/after/syntax/make.vim) to match the offending construct. This seems to work:

syn match makeIgnore /subst[ \t]*['"],,/ containedin=makeIdent

The containedin= is necessary because it's used in a $(...) construct.

like image 179
Ingo Karkat Avatar answered Nov 05 '22 20:11

Ingo Karkat