diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 9d6cbc6..8bfc89a 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -373,15 +373,25 @@ function! ledger#align_commodity() if rhs != '' " Remove everything after the account name (including spaces): .s/\m^\s\+[^[:space:]].\{-}\zs\(\t\| \).*$// - if g:ledger_decimal_sep == '' - let pos = matchend(rhs, '\m\d[^[:space:]]*') + if g:ledger_align_advance == 1 + let pat = '\(= \)\?\zs-\?\([A-Z$€£₹_(]\+ *\)\?\(-\?\([0-9]\+\|[0-9,\.]\{-1,}\)\)\+\ze\( *[0-9A-Za-z€£₹_\"]\+\)\?\([ \t]*[@={]@\?[^\n;]\{-}\)\?\([ \t]\+;.\{-}\|[ \t]*\)' + let pos = matchend(rhs, pat) else - " Find the position of the first decimal separator: - let pos = s:strpos(rhs, '\V' . g:ledger_decimal_sep) + if g:ledger_decimal_sep == '' + let pos = matchend(rhs, '\m\d[^[:space:]]*') + else + " Find the position of the first decimal separator: + let pos = s:strpos(rhs, '\V' . g:ledger_decimal_sep) + endif endif - " Go to the column that allows us to align the decimal separator at g:ledger_align_at: + if pos > 0 - call s:goto_col(g:ledger_align_at - pos - 1) + let pos = pos - 1 + endif + + " Go to the column that allows us to align the decimal separator at g:ledger_align_at: + if pos >= 0 + call s:goto_col(g:ledger_align_at - pos) else call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2) endif @@ -508,7 +518,8 @@ function! ledger#autocomplete_and_align() " confused with a commodity to be aligned). if match(getline('.'), '\s.*\d\%'.col('.').'c') > -1 norm h - call ledger#align_amount_at_cursor() + "call ledger#align_amount_at_cursor() + call ledger#align_commodity() return "\A" endif return "\\" @@ -683,3 +694,23 @@ function! ledger#show_balance(file, ...) endif endf " }}} + +function! ledger#load_accounts(file) + if empty(a:file) + call s:error_message('No file arg') + return + endif + + let l:lns = readfile(a:file) + let l:accounts = [] + + for line in l:lns + let l:m = matchstr(line, 'account \zs.\+\ze') + + if !empty(l:m) + call add(l:accounts, l:m) + endif + endfor + + return l:accounts +endf diff --git a/ftplugin/ledger.vim b/ftplugin/ledger.vim index d0648be..dcf5641 100644 --- a/ftplugin/ledger.vim +++ b/ftplugin/ledger.vim @@ -64,6 +64,10 @@ if !exists('g:ledger_align_at') let g:ledger_align_at = 60 endif +if !exists('g:ledger_align_advance') + let g:ledger_align_advance = 0 +endif + if !exists('g:ledger_default_commodity') let g:ledger_default_commodity = '' endif @@ -326,10 +330,12 @@ endfor unlet s:old s:new s:fun " }}}1 +let b:loaded_accounts = [] + function! s:collect_completion_data() "{{{1 let transactions = ledger#transactions() let cache = {'descriptions': [], 'tags': {}, 'accounts': {}} - let accounts = [] + let accounts = b:loaded_accounts for xact in transactions " collect descriptions if has_key(xact, 'description') && index(cache.descriptions, xact['description']) < 0 @@ -371,6 +377,26 @@ function! s:collect_completion_data() "{{{1 return cache endf "}}} +function! LedgerLoadAccounts(file) + let l:accounts = ledger#load_accounts(a:file) + + if len(l:accounts) > 0 + let b:loaded_accounts = l:accounts + endif + + let b:compl_cache = s:collect_completion_data() + let b:compl_cache['#'] = changenr() +endfunction + +function! LedgerLoadDefaultAccounts() + if exists('g:ledger_account_file') + call LedgerLoadAccounts(g:ledger_account_file) + endif +endfunction + +" Prime the autocomplete cache +call LedgerLoadDefaultAccounts() + " Helper functions {{{1 " return length of string with fix for multibyte characters @@ -441,5 +467,7 @@ command! -buffer -nargs=1 -complete=customlist,s:autocomplete_account_or_payee command! -buffer -complete=customlist,s:autocomplete_account_or_payee -nargs=* \ Register call ledger#register(g:ledger_main, ) + +command! -buffer -complete=file -nargs=1 LoadAccounts call LedgerLoadAccounts() " }}}