Skip to content

Review "advanced alignment mode" commits for workable contributions #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions autoload/ledger.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "\<c-o>A"
endif
return "\<c-x>\<c-o>"
Expand Down Expand Up @@ -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
30 changes: 29 additions & 1 deletion ftplugin/ledger.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, <q-args>)

command! -buffer -complete=file -nargs=1 LoadAccounts call LedgerLoadAccounts(<q-args>)
" }}}