From f3f5a8da5ec9095ba8902b86854e7f806afc89b5 Mon Sep 17 00:00:00 2001 From: DJ Edmonson Date: Fri, 20 Jan 2017 16:38:38 -0800 Subject: [PATCH 1/3] Add advanced alignment mode In advance alignment mode, it behaves similar to emacs alignment. Activated by setting g:ledger_align_advance = 1 --- autoload/ledger.vim | 22 ++++++++++++++++------ ftplugin/ledger.vim | 4 ++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 9d6cbc6..9a3fd22 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 = '\(= \)\?-\?\([A-Z$€£₹_(]\+ *\)\?\(-\?\([0-9]\+\|[0-9,\.]\{-1,}\)\)\([,\.][0-9)]\+\)\?\zs\( *[0-9A-Za-z€£₹_\"]\+\)\?\([ \t]*[@={]@\?[^\n;]\{-}\)\?\([ \t]\+;.\{-}\|[ \t]*\)' + let pos = match(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 diff --git a/ftplugin/ledger.vim b/ftplugin/ledger.vim index d0648be..227f4a7 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 From ab277eb63c776d6ff3d17be385f8c73e44a9009e Mon Sep 17 00:00:00 2001 From: DJ Edmonson Date: Fri, 27 Jan 2017 15:07:59 -0800 Subject: [PATCH 2/3] Update regex to match numbers with thousand seperators better --- autoload/ledger.vim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 9a3fd22..552776a 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -374,8 +374,8 @@ function! ledger#align_commodity() " Remove everything after the account name (including spaces): .s/\m^\s\+[^[:space:]].\{-}\zs\(\t\| \).*$// if g:ledger_align_advance == 1 - let pat = '\(= \)\?-\?\([A-Z$€£₹_(]\+ *\)\?\(-\?\([0-9]\+\|[0-9,\.]\{-1,}\)\)\([,\.][0-9)]\+\)\?\zs\( *[0-9A-Za-z€£₹_\"]\+\)\?\([ \t]*[@={]@\?[^\n;]\{-}\)\?\([ \t]\+;.\{-}\|[ \t]*\)' - let pos = match(rhs, pat) + 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 if g:ledger_decimal_sep == '' let pos = matchend(rhs, '\m\d[^[:space:]]*') @@ -518,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 "\\" From 635f015a6cdc3c65623b67c8b730613befa4bbc8 Mon Sep 17 00:00:00 2001 From: DJ Edmonson Date: Tue, 20 Mar 2018 11:30:55 -0700 Subject: [PATCH 3/3] Add ability to load accounts file for autocompletion --- autoload/ledger.vim | 20 ++++++++++++++++++++ ftplugin/ledger.vim | 26 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 552776a..8bfc89a 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -694,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 227f4a7..dcf5641 100644 --- a/ftplugin/ledger.vim +++ b/ftplugin/ledger.vim @@ -330,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 @@ -375,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 @@ -445,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() " }}}