summaryrefslogtreecommitdiffstats
path: root/syntax/cnmfmt.vim
diff options
context:
space:
mode:
Diffstat (limited to 'syntax/cnmfmt.vim')
-rw-r--r--syntax/cnmfmt.vim87
1 files changed, 87 insertions, 0 deletions
diff --git a/syntax/cnmfmt.vim b/syntax/cnmfmt.vim
new file mode 100644
index 0000000..8787f08
--- /dev/null
+++ b/syntax/cnmfmt.vim
@@ -0,0 +1,87 @@
+" Vim syntax file
+" Language: CNMfmt (CNM 0.3)
+" Maintainer: <clsr@clsr.net>
+" Last Change: 2017-08-18
+" Version: 0.1.0
+" Bugs: expects LIFO tags
+
+if !exists('g:main_syntax')
+ if exists('b:current_syntax')
+ finish
+ endif
+ let g:main_syntax = 'cnmfmt'
+endif
+
+let s:fmts = {
+ \ 'B': {'tag': '\*', 'cterm': 'bold'},
+ \ 'I': {'tag': '/', 'cterm': 'italic'},
+ \ 'U': {'tag': '_', 'cterm': 'underline'},
+ \ 'M': {'tag': '`', 'cterm': 'inverse'},
+\ }
+
+let s:fmtkeepend = 1
+
+function! s:generateFmt(name)
+ let l:region = 'cnmfmt' . a:name
+ let l:cterm = 'cterm='
+ let l:containedin = []
+ let l:fmts = split(a:name, '\zs')
+ let l:tag = s:fmts[l:fmts[-1]]['tag']
+ let l:tag2 = l:tag . l:tag
+ let l:containedin = ''
+
+ for l:f in l:fmts
+ let l:cterm = l:cterm . s:fmts[l:f]['cterm'] . ','
+ endfor
+
+ if strlen(a:name) > 1
+ let l:containedin = ' containedin=cnmfmt' . a:name[:-2]
+ endif
+
+ " XXX: with keepend, URLs close too soon; without, tags don't close if not LIFO
+ exe 'syn region ' . l:region . ' start="' . l:tag2 . '" end="' . l:tag2 . '\|\n\t*\n" skip="\\\\\|\\' . l:tag . '" contained contains=@cnmfmts' . l:containedin . (s:fmtkeepend?' keepend':'')
+ exe 'hi def ' . l:region . ' ' . l:cterm[:-2]
+
+ for l:k in keys(s:fmts)
+ if index(l:fmts, l:k) < 0
+ call s:generateFmt(a:name . l:k)
+ endif
+ endfor
+endfunction
+
+for s:f in keys(s:fmts)
+ call s:generateFmt(s:f)
+endfor
+
+
+syn cluster cnmfmt contains=@cnmfmts,@cnmfmtStyles
+syn cluster cnmfmts contains=cnmfmtLink,cnmfmtEscape
+syn cluster cnmfmtStyles contains=cnmfmtB,cnmfmtI,cnmfmtU,cnmfmtM
+
+syn region cnmfmtLink matchgroup=cnmfmtLinkAt start="@@" end="@@\|\n\t*\n" skip="\\\\\|\\@" keepend contained contains=cnmfmtLinkUrl
+
+syn match cnmfmtLinkText ".*" contained contains=@cnmfmtStyles
+syn match cnmfmtLinkUrl "\(\\ \|\S\)\+" contained nextgroup=cnmfmtLinkText
+
+syn match cnmfmtEscape +\\[nt\\ */_`@]+
+syn match cnmfmtEscape +\\x\x\{2\}+
+syn match cnmfmtEscape +\\u\x\{4\}+
+syn match cnmfmtEscape +\\U\x\{8\}+
+
+
+syn match cnmfmtText "\%^\_.*\%$" contains=@cnmfmt,@Spell
+
+
+hi def link cnmfmtEscape Special
+hi def link cnmfmtLinkAt Special
+hi def link cnmfmtLinkUrl String
+hi def link cnmfmtLinkText Underlined
+
+
+let b:current_syntax = 'cnmfmt'
+if g:main_syntax ==# 'cnmfmt'
+ unlet g:main_syntax
+endif
+
+
+" vim: set noet: