HTML syntax highlighting in javascript strings in vim

I don't know if this is possible / reasonable, but I was curious to find out if I can have lines in javascript files with html highlighting. I found that lines in php can have SQL syntax highlighting, so I believe this is possible.

But I do not know vim scripts, so any help on this is appreciated.

I am using Improved Javascript Syntax .

PS: If it were possible to turn it on and off when editing a js file, that would be great.

thank

+4
javascript html vim syntax-highlighting vim-syntax-highlighting
Sep 24 '09 at 13:33
source share
1 answer

Yes, perhaps if you are not against hacking a syntax file. First you need to include the HTML syntax file from the Javascript syntax file - see :help syn-include for information on this; secondly, you need to declare that the HTML syntax can be found inside certain elements (like strings). Thirdly, if you want to be able to enable and disable it, you can make these commands dependent on the global variable and write some mappings that set or cancel the variable and then reload the syntax file.

For examples on how inclusion works, take a look at syntax/html.vim (which includes Javascript and CSS syntax files), syntax/perl.vim (which includes a POD syntax file), or php.vim (which includes highlighting SQL syntax in strings due to global validity).

Edit: I worked a bit to make this happen in my copy.

At the beginning of syntax/javascript.vim , just below syn case ignore , add

 syn include @javaScriptHTML syntax/html.vim unlet b:current_syntax syn spell default " HTML enables spell-checking globally, turn it off 

Then add @javaScriptHTML to the contained= lists for javaScriptStringD and javaScriptStringS .

Finally, you need to edit syntax/html.vim so that it doesn't include syntax/javascript.vim if it was loaded from javascript: find the line that reads

 if main_syntax != 'java' || exists("java_javascript") 

and change it to

 if main_syntax != 'javascript' && ( main_syntax != 'java' || exists("java_javascript") 
+3
Sep 25 '09 at 5:36
source share



All Articles