Skip to content

Commit 685aeaa

Browse files
committed
Add ledger support, closes sheerun#488
1 parent a9cc6fd commit 685aeaa

File tree

6 files changed

+232
-7
lines changed

6 files changed

+232
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A collection of language packs for Vim.
1010
> One to rule them all, one to find them, one to bring them all and in the darkness bind them.
1111
1212
- It **won't affect your startup time**, as scripts are loaded only on demand\*.
13-
- It **installs and updates 120+ times faster** than the <!--Package Count-->148<!--/Package Count--> packages it consists of.
13+
- It **installs and updates 120+ times faster** than the <!--Package Count-->149<!--/Package Count--> packages it consists of.
1414
- Solid syntax and indentation support (other features skipped). Only the best language packs.
1515
- All unnecessary files are ignored (like enormous documentation from php support).
1616
- No support for esoteric languages, only most popular ones (modern too, like `slim`).
@@ -110,6 +110,7 @@ If you need full functionality of any plugin, please use it directly with your p
110110
- [jsx](https://github.com/MaxMEllon/vim-jsx-pretty) (autoload, after)
111111
- [julia](https://github.com/JuliaEditorSupport/julia-vim) (syntax, indent, autoload, ftplugin)
112112
- [kotlin](https://github.com/udalov/kotlin-vim) (syntax, indent, ftplugin)
113+
- [ledger](https://github.com/ledger/vim-ledger) (syntax, compiler, indent)
113114
- [less](https://github.com/groenewege/vim-less) (syntax, indent, ftplugin)
114115
- [lilypond](https://github.com/anowlcalledjosh/vim-lilypond) (syntax, indent, compiler, ftplugin)
115116
- [livescript](https://github.com/gkz/vim-ls) (syntax, indent, compiler, ftplugin)

build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ PACKS="
202202
jsx:MaxMEllon/vim-jsx-pretty:_ALL
203203
julia:JuliaEditorSupport/julia-vim
204204
kotlin:udalov/kotlin-vim
205+
ledger:ledger/vim-ledger:_BASIC
205206
less:groenewege/vim-less:_NOAFTER
206207
lilypond:anowlcalledjosh/vim-lilypond
207208
livescript:gkz/vim-ls

compiler/ledger.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ledger') == -1
2+
3+
" Vim Compiler File
4+
" Compiler: ledger
5+
" by Johann Klähn; Use according to the terms of the GPL>=2.
6+
" vim:ts=2:sw=2:sts=2:foldmethod=marker
7+
8+
scriptencoding utf-8
9+
10+
if exists('current_compiler') || !exists('g:ledger_bin')
11+
finish
12+
endif
13+
14+
let current_compiler = g:ledger_bin
15+
16+
if exists(':CompilerSet') != 2
17+
command -nargs=* CompilerSet setlocal <args>
18+
endif
19+
20+
if !g:ledger_is_hledger
21+
" Capture Ledger errors (%-C ignores all lines between "While parsing..." and "Error:..."):
22+
CompilerSet errorformat=%EWhile\ parsing\ file\ \"%f\"\\,\ line\ %l:,%ZError:\ %m,%-C%.%#
23+
" Capture Ledger warnings:
24+
CompilerSet errorformat+=%tarning:\ \"%f\"\\,\ line\ %l:\ %m
25+
" Skip all other lines:
26+
CompilerSet errorformat+=%-G%.%#
27+
exe 'CompilerSet makeprg='.substitute(g:ledger_bin, ' ', '\\ ', 'g').'\ -f\ %\ '.substitute(g:ledger_extra_options, ' ', '\\ ', 'g').'\ source\ %'
28+
else
29+
exe 'CompilerSet makeprg=('.substitute(g:ledger_bin, ' ', '\\ ', 'g').'\ -f\ %\ print\ '.substitute(g:ledger_extra_options, ' ', '\\ ', 'g').'\ >\ /dev/null)'
30+
endif
31+
32+
endif

ftdetect/polyglot.vim

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,10 @@ au BufNewFile,BufRead *.psc1 set ft=xml
426426
au BufNewFile,BufRead *.proto set ft=proto
427427

428428
" pug, from pug.vim in digitaltoad/vim-pug
429-
au BufNewFile,BufReadPost *.pug set ft=pug
429+
au BufNewFile,BufRead *.pug set ft=pug
430430

431431
" Jade
432-
au BufNewFile,BufReadPost *.jade set ft=pug
432+
au BufNewFile,BufRead *.jade set ft=pug
433433

434434
" puppet, from puppet.vim in rodjek/vim-puppet
435435
au BufNewFile,BufRead *.pp set ft=puppet
@@ -452,7 +452,7 @@ au BufNewFile,BufRead *.qml set ft=qml
452452
au BufNewFile,BufRead *.rkt,*.rktl set ft=racket
453453

454454
" raku, from raku.vim in Raku/vim-raku
455-
au BufNewFile,BufWritePost,BufReadPost *.pm6,*.p6,*.t6,*.pod6,*.raku,*.rakumod,*.rakudoc,*.rakutest set ft=raku
455+
au BufNewFile,BufRead *.pm6,*.p6,*.t6,*.pod6,*.raku,*.rakumod,*.rakudoc,*.rakutest set ft=raku
456456

457457
" raml, from raml.vim in IN3D/vim-raml
458458
au BufNewFile,BufRead *.raml set ft=raml
@@ -518,8 +518,8 @@ au BufNewFile,BufRead *.sol setf solidity
518518

519519
" stylus, from stylus.vim in wavded/vim-stylus
520520
" Stylus
521-
au BufNewFile,BufReadPost *.styl set ft=stylus
522-
au BufNewFile,BufReadPost *.stylus set ft=stylus
521+
au BufNewFile,BufRead *.styl set ft=stylus
522+
au BufNewFile,BufRead *.stylus set ft=stylus
523523

524524
" svelte, from svelte.vim in evanleck/vim-svelte
525525
au BufNewFile,BufRead *.svelte set ft=svelte
@@ -601,12 +601,15 @@ au BufNewFile,BufRead *.vue,*.wpy setf vue
601601
au BufNewFile,BufRead *.xdc set ft=xdc
602602

603603
" zephir, from zephir.vim in xwsoul/vim-zephir
604-
au BufNewFile,BufReadPost *.zep set ft=zephir
604+
au BufNewFile,BufRead *.zep set ft=zephir
605605

606606
" zig, from zig.vim in ziglang/zig.vim
607607
au BufNewFile,BufRead *.zig set ft=zig
608608
au BufNewFile,BufRead *.zir set ft=zir
609609

610+
" ledger
611+
au BufNewFile,BufRead *.ldg,*.ledger,*.journal set ft=ledger
612+
610613
" restore Vi compatibility settings
611614
let &cpo = s:cpo_save
612615
unlet s:cpo_save

indent/ledger.vim

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'ledger') == -1
2+
3+
" Vim filetype indent file
4+
" filetype: ledger
5+
" by Johann Klähn; Use according to the terms of the GPL>=2.
6+
" vim:ts=2:sw=2:sts=2:foldmethod=marker
7+
8+
scriptencoding utf-8
9+
10+
if exists('b:did_indent')
11+
finish
12+
endif
13+
let b:did_indent = 1
14+
15+
setl autoindent
16+
setl indentexpr=GetLedgerIndent()
17+
18+
if exists('*GetLedgerIndent')
19+
finish
20+
endif
21+
22+
function GetLedgerIndent(...)
23+
" You can pass in a line number when calling this function manually.
24+
let lnum = a:0 > 0 ? a:1 : v:lnum
25+
" If this line is empty look at (the indentation of) the last line.
26+
" Note that inside of a transaction no blank lines are allowed.
27+
let line = getline(lnum)
28+
let prev = getline(lnum - 1)
29+
30+
if line =~# '^\s\+\S'
31+
" Lines that already are indented (→postings, sub-directives) keep their indentation.
32+
return &shiftwidth
33+
elseif line =~# '^\s*$'
34+
" Current line is empty, try to guess its type based on the previous line.
35+
if prev =~# '^\([[:digit:]~=]\|\s\+\S\)'
36+
" This is very likely a posting or a sub-directive.
37+
" While lines following the start of a transaction are automatically
38+
" indented you will have to indent the first line following a
39+
" pre-declaration manually. This makes it easier to type long lists of
40+
" 'account' pre-declarations without sub-directives, for example.
41+
return &shiftwidth
42+
else
43+
return 0
44+
endif
45+
else
46+
" Everything else is not indented:
47+
" start of transactions, pre-declarations, apply/end-lines
48+
return 0
49+
endif
50+
endf
51+
52+
endif

syntax/ledger.vim

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)