diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 7ba1866..64b952c 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -930,7 +930,7 @@ function! ledger#output(report) abort endif " Open a new buffer to show Ledger's output. execute get(s:winpos_map, b:ledger_winpos, 'bo new') - setlocal buftype=nofile bufhidden=wipe modifiable nobuflisted noswapfile nowrap + setlocal buftype=nofile bufhidden=wipe modifiable nobuflisted noswapfile nowrap nolist call ledger#init() call append(0, a:report) setlocal nomodifiable diff --git a/ftplugin/ledger.vim b/ftplugin/ledger.vim index 01ab494..6615556 100644 --- a/ftplugin/ledger.vim +++ b/ftplugin/ledger.vim @@ -346,16 +346,34 @@ function! s:count_expression(text, expression) return len(split(a:text, a:expression, 1))-1 endfunction +augroup refresh_autocomplete_account_or_payee_cache + au! + autocmd! CmdlineLeave * if exists('s:accounts_cache') | unlet s:accounts_cache | endif + autocmd! CmdlineLeave * if exists('s:payees_cache') | unlet s:payees_cache | endif +augroup END + function! s:autocomplete_account_or_payee(argument_lead, command_line, cursor_position) if a:argument_lead =~# '^@' - let payees = s:get_descriptions_list() - let pattern = strpart(a:argument_lead, 1) - return map(filter(payees, "v:val =~? '" . pattern . "' && v:val !~? '^Warning: '"), - \ '"@" . escape(v:val, " ")') + if !exists('s:payees_cache') + let s:payees_cache = join(map(filter(s:get_descriptions_list(), "v:val !~? '^Warning: '"), '"@" .. v:val'), "\n") + endif + return s:payees_cache + else + if !exists('s:accounts_cache') + let s:accounts_cache = join(filter(s:get_accounts_list(), "v:val !~? '^Warning: '"), "\n") + endif + return s:accounts_cache + endif +endfunction + +function! s:ledger_complete(argument_lead, command_line, cursor_position) + let parts = split(strpart(a:command_line, 0, a:cursor_position), '\s\+', 1) + if len(parts) == 2 + return "accounts\nbalance\nbudget\ncleared\ncommodities\nconvert\ncsv\nentry\nemacs\nequity\npayees\npricemap\nprices\npricedb\nprint\npush\npop\nregister\nselect\nsource\nstats\nxml\n" + elseif len(parts) == 3 && parts[1] =~# 'accounts\|balance\|budget\|cleared\|commodities\|csv\|equity\|payees\|prices\|pricedb\|print\|register\|stats\|xml' + return s:autocomplete_account_or_payee(a:argument_lead, a:command_line, a:cursor_position) else - let accounts = s:get_accounts_list() - return map(filter(accounts, "v:val =~? '" . a:argument_lead . "' && v:val !~? '^Warning: '"), - \ 'escape(v:val, " ")') + return "" endif endfunction @@ -365,18 +383,18 @@ function! s:reconcile(file, account) endfunction " Commands -command! -buffer -nargs=? -complete=customlist,autocomplete_account_or_payee +command! -buffer -nargs=? -complete=custom,autocomplete_account_or_payee \ Balance call ledger#show_balance(b:ledger_main, ) -command! -buffer -nargs=+ -complete=customlist,autocomplete_account_or_payee +command! -buffer -nargs=+ -complete=custom,ledger_complete \ Ledger call ledger#output(ledger#report(b:ledger_main, )) command! -buffer -range LedgerAlign ,call ledger#align_commodity() command! -buffer LedgerAlignBuffer call ledger#align_commodity_buffer() -command! -buffer -nargs=1 -complete=customlist,autocomplete_account_or_payee +command! -buffer -nargs=1 -complete=custom,autocomplete_account_or_payee \ Reconcile call reconcile(b:ledger_main, ) -command! -buffer -complete=customlist,autocomplete_account_or_payee -nargs=* +command! -buffer -complete=custom,autocomplete_account_or_payee -nargs=* \ Register call ledger#register(b:ledger_main, )