summaryrefslogtreecommitdiffstats
path: root/syntax/cnmfmt.vim
blob: 8787f0881a416df7335bfa37f495952d72fd6665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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: