diff options
-rw-r--r-- | autoload/beancount.vim | 44 | ||||
-rw-r--r-- | doc/beancount.txt | 12 | ||||
-rw-r--r-- | ftplugin/beancount.vim | 5 |
3 files changed, 32 insertions, 29 deletions
diff --git a/autoload/beancount.vim b/autoload/beancount.vim index 9c5df3d..08a7737 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -8,32 +8,44 @@ endfunction " Align currency on decimal point. function! beancount#align_commodity(line1, line2) - " Saving cursor position to adjust it if necessary. + " Save cursor position to adjust it if necessary. let l:cursor_col = col('.') let l:cursor_line = line('.') - " This lets me increment at start of loop, because of continue statements. + " Increment at start of loop, because of continue statements. let l:current_line = a:line1 - 1 while l:current_line < a:line2 let l:current_line += 1 let l:line = getline(l:current_line) - " This matches an account name followed by a space. There may be - " some conflicts with non-transaction syntax that I don't know about. - " It won't match a comment or any non-indented line. - let l:end_acc = matchend(l:line, '^\v(([\-/[:digit:]]+\s+(balance|price))|\s+[!&#?%PSTCURM])?\s+\S+[^:] ') - if l:end_acc < 0 | continue | endif - " Where does commodity amount begin? - let l:end_space = matchend(l:line, '^ *', l:end_acc) - - " Now look for a minus sign and a number, and align on the next column. - let l:comma = g:beancount_decimal_separator ==# ',' ? '.' : ',' - let l:separator = matchend(l:line, '^\v(-)?[' . l:comma . '[:digit:]]+', l:end_space) + 1 + " This matches an account name followed by a space in one of the two + " following cases: + " - A posting line, i.e., the line starts with indentation followed + " by an optional flag and the account. + " - A balance directive, i.e., the line starts with a date followed + " by the 'balance' keyword and the account. + " - A price directive, i.e., the line starts with a date followed by + " the 'price' keyword and a currency. + let l:end_account = matchend(l:line, '^\v' . + \ '[\-/[:digit:]]+\s+balance\s+([A-Z][A-Za-z0-9\-]+)(:[A-Z][A-Za-z0-9\-]*)+ ' . + \ '|[\-/[:digit:]]+\s+price\s+\S+ ' . + \ '|\s+([!&#?%PSTCURM]\s+)?([A-Z][A-Za-z0-9\-]+)(:[A-Z][A-Za-z0-9\-]*)+ ' + \ ) + if l:end_account < 0 + continue + endif + + " Where does the number begin? + let l:begin_number = matchend(l:line, '^ *', l:end_account) + + " Look for a minus sign and a number (possibly containing commas) and + " align on the next column. + let l:separator = matchend(l:line, '^\v(-)?[,[:digit:]]+', l:begin_number) + 1 if l:separator < 0 | continue | endif - let l:has_spaces = l:end_space - l:end_acc + let l:has_spaces = l:begin_number - l:end_account let l:need_spaces = g:beancount_separator_col - l:separator + l:has_spaces if l:need_spaces < 0 | continue | endif - call setline(l:current_line, l:line[0 : l:end_acc - 1] . repeat(' ', l:need_spaces) . l:line[ l:end_space : -1]) - if l:current_line == l:cursor_line && l:cursor_col >= l:end_acc + call setline(l:current_line, l:line[0 : l:end_account - 1] . repeat(' ', l:need_spaces) . l:line[ l:begin_number : -1]) + if l:current_line == l:cursor_line && l:cursor_col >= l:end_account " Adjust cursor position for continuity. call cursor(0, l:cursor_col + l:need_spaces - l:has_spaces) endif diff --git a/doc/beancount.txt b/doc/beancount.txt index 048a6c6..2c7a20e 100644 --- a/doc/beancount.txt +++ b/doc/beancount.txt @@ -34,15 +34,14 @@ COMMANDS *beancount-commands* will be pushed to the right the appropriate amount, so that it remains on the same character. - The alignment character can be set using - |g:beancount_decimal_separator|. The script assumes the - use of spaces for alignment. It does not understand tabs. + The script assumes the use of spaces for alignment. It + does not understand tabs. You can use the following insert-mode remap to automatically align commodities every time you type a decimal point: > - inoremap . .<C-O>:AlignCommodity<CR> + inoremap . .<C-\><C-O>:AlignCommodity<CR> < You may also want to set other mappings for this. For example, I use > @@ -79,11 +78,6 @@ OPTIONS *beancount-options* Default value: 50 -*g:beancount_decimal_separator* - Set the decimal separator that numbers are aligned by. - - Default value: '.' - *b:beancount_root* Set the root Beancount file. This is used to gather values for the completion. If not set, the current file will be used. diff --git a/ftplugin/beancount.vim b/ftplugin/beancount.vim index 696e9e0..b6e3540 100644 --- a/ftplugin/beancount.vim +++ b/ftplugin/beancount.vim @@ -10,13 +10,10 @@ setl comments=b:; setl commentstring=;%s compiler beancount -" These two variables customize the behavior of the AlignCommodity command. +" This variable customizes the behavior of the AlignCommodity command. if !exists('g:beancount_separator_col') let g:beancount_separator_col = 50 endif -if !exists('g:beancount_decimal_separator') - let g:beancount_decimal_separator = '.' -endif if !exists('g:beancount_account_completion') let g:beancount_account_completion = 'default' endif |