aboutsummaryrefslogtreecommitdiffstats
path: root/autoload
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--autoload/beancount.vim44
1 files changed, 28 insertions, 16 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