Skip to content

Commit

Permalink
Add syntax hilighting for multiple content-types
Browse files Browse the repository at this point in the history
  • Loading branch information
pjz committed Aug 2, 2014
1 parent 686fbf0 commit 3aa3e44
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions ftplugin/simplate.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,82 @@ let g:loaded_simplate_vim = 1 " simplate.vim version number
let s:keepcpo = &cpo
set cpo&vim

" Note that the next two functions are modifications of TextEnableCodeSnip from
" http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
" ------------------------------------------------------------------------------

" This sets the language used in the initial sections to the specified filetype
function! SyntaxEnableLanguage(filesyntax) abort
let ft=toupper(a:filesyntax)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filesyntax.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filesyntax.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ start="\%^" start="^\[----*\]\s*$"rs=e+1,hs=e+1,ms=e+1
\ end="^\[----*\]"re=s-1,he=s-1,me=s-1 keepend
\ contains=@'.group
endfunction

" This enables sections identified by the specified contenttype to be
" hilighted as if they're the specified filesyntax
" Note that contenttype is actually a pattern
function! SyntaxEnableTemplate(filesyntax,contenttype) abort
let ft=toupper(a:filesyntax)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filesyntax.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filesyntax.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ start="^\[----*\].*\c'.a:contenttype.'.*$"rs=e+1,hs=e+1,ms=e+1
\ end="^\[----*\]"re=s-1,he=s-1,me=s-1 keepend
\ contains=@'.group
endfunction


syntax on
syntax include @Python syntax/python.vim
syntax region pythonCode keepend start=/\%^/ start=/^\[----*\]\s*$/rs=e+1,hs=e+1,ms=e+1 end=/^\[----*\]/re=s-1,he=s-1,me=s-1 contains=@Python

" only one language at a time is currently supported
call SyntaxEnableLanguage( 'python' )

call SyntaxEnableTemplate( 'html', 'text/html' )
call SyntaxEnableTemplate( 'css', 'text/css' )
call SyntaxEnableTemplate( 'javascript', 'application/javascript' )

" rfc 2376
call SyntaxEnableTemplate( 'xml', 'text/xml' )
call SyntaxEnableTemplate( 'xml', 'application/xml' )
call SyntaxEnableTemplate( 'xml', '.*/.*+xml' )

"" Not in the standard vim distro:

" call SyntaxEnableTemplate( 'json', 'application/json' )

" ------------------------------------------------------------------------------
let &cpo= s:keepcpo
Expand Down

0 comments on commit 3aa3e44

Please sign in to comment.