vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

beancount.vim (2671B)


      1 " Beancount specific formatting
      2 " Language: beancount
      3 " Maintainer: Nathan Grigg
      4 " Latest Revision: 2021-03-06
      5 
      6 " Align currency on decimal point.
      7 function! beancount#align_commodity(line1, line2) abort
      8     " Save cursor position to adjust it if necessary.
      9     let l:cursor_col = col('.')
     10     let l:cursor_line = line('.')
     11 
     12     " Increment at start of loop, because of continue statements.
     13     let l:current_line = a:line1 - 1
     14     while l:current_line < a:line2
     15         let l:current_line += 1
     16         let l:line = getline(l:current_line)
     17         " This matches an account name followed by a space in one of the two
     18         " following cases:
     19         "  - A posting line, i.e., the line starts with indentation followed
     20         "    by an optional flag and the account.
     21         "  - A balance directive, i.e., the line starts with a date followed
     22         "    by the 'balance' keyword and the account.
     23         "  - A price directive, i.e., the line starts with a date followed by
     24         "    the 'price' keyword and a currency.
     25         let l:end_account = matchend(l:line, '\v' .
     26             \ '^[\-/[:digit:]]+\s+balance\s+([A-Z][A-Za-z0-9\-]+)(:[A-Z0-9][A-Za-z0-9\-]*)+ ' .
     27             \ '|^[\-/[:digit:]]+\s+price\s+\S+ ' .
     28             \ '|^\s+([!&#?%PSTCURM]\s+)?([A-Z][A-Za-z0-9\-]+)(:[A-Z0-9][A-Za-z0-9\-]*)+ '
     29             \ )
     30         if l:end_account < 0
     31             continue
     32         endif
     33 
     34         " Where does the number begin?
     35         let l:begin_number = matchend(l:line, '^ *', l:end_account)
     36 
     37         " Look for a minus sign and a number (possibly containing commas) and
     38         " align on the next column.
     39         let l:separator = matchend(l:line, '^\v([-+])?[,[:digit:]]+', l:begin_number) + 1
     40         if l:separator < 0 | continue | endif
     41         let l:has_spaces = l:begin_number - l:end_account
     42         let l:need_spaces = g:beancount_separator_col - l:separator + l:has_spaces
     43         if l:need_spaces < 0 | continue | endif
     44         call setline(l:current_line, l:line[0 : l:end_account - 1] . repeat(' ', l:need_spaces) . l:line[ l:begin_number : -1])
     45         if l:current_line == l:cursor_line && l:cursor_col >= l:end_account
     46             " Adjust cursor position for continuity.
     47             call cursor(0, l:cursor_col + l:need_spaces - l:has_spaces)
     48         endif
     49     endwhile
     50 endfunction
     51 
     52 " Call bean-doctor on the current line and dump output into a scratch buffer
     53 function! beancount#get_context() abort
     54     let l:context = system('bean-doctor context ' . shellescape(expand('%')) . ' ' . line('.'))
     55     botright new
     56     setlocal buftype=nofile bufhidden=hide noswapfile
     57     call append(0, split(l:context, '\v\n'))
     58     normal! gg
     59 endfunction