|
| 1 | +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ledger') == -1 |
| 2 | + |
| 3 | +" Vim syntax file |
| 4 | +" filetype: ledger |
| 5 | +" by Johann Klähn; Use according to the terms of the GPL>=2. |
| 6 | +" by Stefan Karrmann; Use according to the terms of the GPL>=2. |
| 7 | +" by Wolfgang Oertl; Use according to the terms of the GPL>=2. |
| 8 | +" vim:ts=2:sw=2:sts=2:foldmethod=marker |
| 9 | + |
| 10 | +scriptencoding utf-8 |
| 11 | + |
| 12 | +if exists('b:current_syntax') |
| 13 | + finish |
| 14 | +endif |
| 15 | + |
| 16 | +if !exists ('b:is_hledger') |
| 17 | + if exists('g:ledger_is_hledger') |
| 18 | + let b:is_hledger = 1 |
| 19 | + else |
| 20 | + let b:is_hledger = 0 |
| 21 | + endif |
| 22 | +endif |
| 23 | + |
| 24 | +" Force old regex engine (:help two-engines) |
| 25 | +let s:oe = '\%#=1' |
| 26 | +let s:lb1 = '\@1<=' |
| 27 | + |
| 28 | +let s:line_comment_chars = b:is_hledger ? ';*#' : ';|*#%' |
| 29 | + |
| 30 | +let s:fb = get(g:, 'ledger_fold_blanks', 0) |
| 31 | +let s:skip = s:fb > 0 ? '\|^\n' : '' |
| 32 | +if s:fb == 1 |
| 33 | + let s:skip .= '\n\@!' |
| 34 | +endif |
| 35 | + |
| 36 | +let s:ledgerAmount_contains = '' |
| 37 | +if get(g:, 'ledger_commodity_spell', 0) == 0 |
| 38 | + let s:ledgerAmount_contains .= '@NoSpell' |
| 39 | +endif |
| 40 | + |
| 41 | +" for debugging |
| 42 | +syntax clear |
| 43 | + |
| 44 | +" DATE[=EDATE] [*|!] [(CODE)] DESC <-- first line of transaction |
| 45 | +" ACCOUNT AMOUNT [; NOTE] <-- posting |
| 46 | + |
| 47 | +exe 'syn region ledgerTransaction start=/^[[:digit:]~=]/ '. |
| 48 | + \ 'skip=/^\s'. s:skip . '/ end=/^/ fold keepend transparent '. |
| 49 | + \ 'contains=ledgerTransactionDate,ledgerTransactionMetadata,ledgerPosting,ledgerTransactionExpression' |
| 50 | +syn match ledgerTransactionDate /^\d\S\+/ contained |
| 51 | +syn match ledgerTransactionExpression /^[=~]\s\+\zs.*/ contained |
| 52 | +syn match ledgerPosting /^\s\+[^[:blank:];].*/ |
| 53 | + \ contained transparent contains=ledgerAccount,ledgerAmount,ledgerValueExpression,ledgerPostingMetadata |
| 54 | +" every space in an account name shall be surrounded by two non-spaces |
| 55 | +" every account name ends with a tab, two spaces or the end of the line |
| 56 | +exe 'syn match ledgerAccount '. |
| 57 | + \ '/'.s:oe.'^\s\+\zs\%(\S'.s:lb1.' \S\|\S\)\+\ze\%( \|\t\|\s*$\)/ contained' |
| 58 | +exe 'syn match ledgerAmount '. |
| 59 | + \ '/'.s:oe.'\S'.s:lb1.'\%( \|\t\)\s*\zs\%([^();[:space:]]\|\s\+[^();[:space:]]\)\+/ contains='.s:ledgerAmount_contains.' contained' |
| 60 | +exe 'syn match ledgerValueExpression '. |
| 61 | + \ '/'.s:oe.'\S'.s:lb1.'\%( \|\t\)\s*\zs(\%([^;[:space:]]\|\s\+[^;[:space:]]\)\+)/ contains='.s:ledgerAmount_contains.' contained' |
| 62 | + |
| 63 | +syn region ledgerPreDeclaration start=/^\(account\|payee\|commodity\|tag\)/ skip=/^\s/ end=/^/ |
| 64 | + \ keepend transparent |
| 65 | + \ contains=ledgerPreDeclarationType,ledgerPreDeclarationName,ledgerPreDeclarationDirective |
| 66 | +syn match ledgerPreDeclarationType /^\(account\|payee\|commodity\|tag\)/ contained |
| 67 | +syn match ledgerPreDeclarationName /^\S\+\s\+\zs.*/ contained |
| 68 | +syn match ledgerPreDeclarationDirective /^\s\+\zs\S\+/ contained |
| 69 | + |
| 70 | +syn match ledgerDirective |
| 71 | + \ /^\%(alias\|assert\|bucket\|capture\|check\|define\|expr\|fixed\|include\|year\)\s/ |
| 72 | +syn match ledgerOneCharDirective /^\%(P\|A\|Y\|N\|D\|C\)\s/ |
| 73 | + |
| 74 | +syn region ledgerBlockComment start=/^comment/ end=/^end comment/ |
| 75 | +syn region ledgerBlockTest start=/^test/ end=/^end test/ |
| 76 | +exe 'syn match ledgerComment /^['.s:line_comment_chars.'].*$/' |
| 77 | +" comments at eol must be preceded by at least 2 spaces / 1 tab |
| 78 | +if b:is_hledger |
| 79 | + syn region ledgerTransactionMetadata start=/;/ end=/^/ |
| 80 | + \ keepend contained contains=ledgerTags,ledgerValueTag,ledgerTypedTag |
| 81 | +else |
| 82 | + syn region ledgerTransactionMetadata start=/\%(\s\s\|\t\|^\s\+\);/ end=/^/ |
| 83 | + \ keepend contained contains=ledgerTags,ledgerValueTag,ledgerTypedTag |
| 84 | +endif |
| 85 | +syn region ledgerPostingMetadata start=/;/ end=/^/ |
| 86 | + \ keepend contained contains=ledgerTags,ledgerValueTag,ledgerTypedTag |
| 87 | +exe 'syn match ledgerTags '. |
| 88 | + \ '/'.s:oe.'\%(\%(;\s*\|^tag\s\+\)\)\@<='. |
| 89 | + \ ':[^:[:space:]][^:]*\%(::\?[^:[:space:]][^:]*\)*:\s*$/ '. |
| 90 | + \ 'contained contains=ledgerTag' |
| 91 | +syn match ledgerTag /:\zs[^:]\+\ze:/ contained |
| 92 | +exe 'syn match ledgerValueTag '. |
| 93 | + \ '/'.s:oe.'\%(\%(;\|^tag\)[^:]\+\)\@<=[^:]\+:\ze.\+$/ contained' |
| 94 | +exe 'syn match ledgerTypedTag '. |
| 95 | + \ '/'.s:oe.'\%(\%(;\|^tag\)[^:]\+\)\@<=[^:]\+::\ze.\+$/ contained' |
| 96 | + |
| 97 | +syn region ledgerApply |
| 98 | + \ matchgroup=ledgerStartApply start=/^apply\>/ |
| 99 | + \ matchgroup=ledgerEndApply end=/^end\s\+apply\>/ |
| 100 | + \ contains=ledgerApplyHead,ledgerApply,ledgerTransaction,ledgerComment |
| 101 | +exe 'syn match ledgerApplyHead '. |
| 102 | + \ '/'.s:oe.'\%(^apply\s\+\)\@<=\S.*$/ contained' |
| 103 | + |
| 104 | +syntax keyword ledgerTodo FIXME TODO |
| 105 | + \ contained containedin=ledgerComment,ledgerTransaction,ledgerTransactionMetadata,ledgerPostingMetadata |
| 106 | + |
| 107 | +highlight default link ledgerComment Comment |
| 108 | +highlight default link ledgerBlockComment Comment |
| 109 | +highlight default link ledgerBlockTest Comment |
| 110 | +highlight default link ledgerTransactionDate Constant |
| 111 | +highlight default link ledgerTransactionExpression Statement |
| 112 | +highlight default link ledgerTransactionMetadata Tag |
| 113 | +highlight default link ledgerPostingMetadata Tag |
| 114 | +highlight default link ledgerTypedTag Keyword |
| 115 | +highlight default link ledgerValueTag Type |
| 116 | +highlight default link ledgerTag Type |
| 117 | +highlight default link ledgerStartApply Tag |
| 118 | +highlight default link ledgerEndApply Tag |
| 119 | +highlight default link ledgerApplyHead Type |
| 120 | +highlight default link ledgerAccount Identifier |
| 121 | +highlight default link ledgerAmount Number |
| 122 | +highlight default link ledgerValueExpression Function |
| 123 | +highlight default link ledgerPreDeclarationType Type |
| 124 | +highlight default link ledgerPreDeclarationName Identifier |
| 125 | +highlight default link ledgerPreDeclarationDirective Type |
| 126 | +highlight default link ledgerDirective Type |
| 127 | +highlight default link ledgerOneCharDirective Type |
| 128 | +highlight default link ledgerTodo Todo |
| 129 | + |
| 130 | +" syncinc is easy: search for the first transaction. |
| 131 | +syn sync clear |
| 132 | +syn sync match ledgerSync grouphere ledgerTransaction "^[[:digit:]~=]" |
| 133 | + |
| 134 | +let b:current_syntax = b:is_hledger ? 'hledger' : 'ledger' |
| 135 | + |
| 136 | +endif |
0 commit comments