Skip to content

Instantly share code, notes, and snippets.

@chocolateboy
Last active August 29, 2015 14:09
Show Gist options
  • Save chocolateboy/0edc3d1d696ecdee053a to your computer and use it in GitHub Desktop.
Save chocolateboy/0edc3d1d696ecdee053a to your computer and use it in GitHub Desktop.
GFT: GitHub-Flavored Text for Vim
" name: GitHub-Flavored Text for Vim
"
" description: highlight embedded code blocks with GitHub-Flavored Markdown code fences
"
" based on: http://vim.wikia.com/wiki/VimTip857
"
" keywords: syntax highlighting, single file, same file, multi, multiple, github flavoured text,
" github flavored vim, github flavoured vim, polyglot, guard, guards, fenced
"
" example:
"
" Blah blah blah.
"
" TODO: fix foo!
"
" ```javascript
" var foo = foo()
" return foo + bar()
" ```
"
" TODO: rewrite in Scala!
"
" ```scala
" var foo = foo()
" return foo + bar()
" ```
"
" TODO: rewrite in Kotlin!
"
" ```kotlin
" var foo = foo()
" return foo + bar()
" ```
"
" TODO: rewrite in Swift!
"
" ```swift
" var foo = foo()
" return foo + bar()
" ```
"
" Blah blah blah &c.
"
" usage:
"
" :call GFT(syntax [, name [, fenceHL ] ])
"
" params:
"
" syntax: the vim syntax to use e.g. 'javascript'
" name: the fence name e.g. 'js' in ```js (default: same as the syntax)
" fenceHL: the name of the group used to highlight the fence delimiters:
" use 'none' to disable delimiter highlighting (default: SpecialComment)
"
" e.g.:
"
" :call GFT('javascript')
" :call GFT('javascript', 'js')
" :call GFT('javascript', 'js', 'SpecialComment')
" :call GFT('javascript', 'js', 'none') " don't highlight the delimiters
" :call GFT('javascript', '') " default unnamed blocks (```) to JavaScript
function! GFT(filetype, ...) abort
let ft = toupper(a:filetype)
let group = 'textGroup' . ft
let name = a:0 > 0 ? a:1 : a:filetype
let fenceHL = a:0 > 1 ? a:2 : 'SpecialComment'
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:filetype . '.vim'
try
execute 'syntax include @' . group . ' after/syntax/' . a:filetype . '.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 .
\ ' matchgroup=' . fenceHL .
\ ' start="```' . name . '" end="```"' .
\ ' contains=@' . group
execute 'syntax sync fromstart'
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment