beancount.vim (1484B)
1 " Vim indent file 2 " Language: beancount 3 " Maintainer: Nathan Grigg 4 " Latest Revision: 2017-03-20 5 6 if exists('b:did_indent') 7 finish 8 endif 9 let b:did_indent = 1 10 11 setlocal indentexpr=GetBeancountIndent(v:lnum) 12 13 let b:undo_indent = "setl inde<" 14 15 if exists('*GetBeancountIndent') 16 finish 17 endif 18 19 function! s:IsDirective(str) 20 return a:str =~# '\v^\s*(\d{4}-\d{2}-\d{2}|pushtag|poptag|option|plugin|include)' 21 endfunction 22 23 function! s:IsPosting(str) 24 return a:str =~# '\v^\s*[A-Z]\w+:' 25 endfunction 26 27 function! s:IsMetadata(str) 28 return a:str =~# '\v^\s*[a-z][a-zA-Z0-9\-_]+:' 29 endfunction 30 31 function! s:IsTransaction(str) 32 " The final \S represents the flag (e.g. * or !). 33 return a:str =~# '\v^\s*\d{4}-\d{2}-\d{2}\s+(txn\s+)?\S(\s|$)' 34 endfunction 35 36 function GetBeancountIndent(line_num) 37 let l:this_line = getline(a:line_num) 38 let l:prev_line = getline(a:line_num - 1) 39 " Don't touch comments 40 if l:this_line =~# '\v^\s*;' | return -1 | endif 41 " This is a new directive or previous line is blank. 42 if l:prev_line =~# '^\s*$' || s:IsDirective(l:this_line) | return 0 | endif 43 " Previous line is transaction or this is a posting. 44 if s:IsTransaction(l:prev_line) || s:IsPosting(l:this_line) | return &shiftwidth | endif 45 if s:IsMetadata(l:this_line) 46 let l:this_indent = indent(a:line_num - 1) 47 if ! s:IsMetadata(l:prev_line) | let l:this_indent += &shiftwidth | endif 48 return l:this_indent 49 endif 50 return -1 51 endfunction