Skip to content

Commit cb08189

Browse files
committed
fix: Use garanteed-set buffer-local variables instead of possibly unset globals
1 parent c2cd5bd commit cb08189

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

autoload/ledger.vim

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function! ledger#transaction_date_set(lnum, type, ...) abort
256256
return
257257
endif
258258

259-
let formatted = strftime(g:ledger_date_format, time)
259+
let formatted = strftime(b:ledger_date_format, time)
260260
if has_key(trans, 'date') && ! empty(trans['date'])
261261
let date = split(trans['date'], '=')
262262
else
@@ -671,13 +671,13 @@ function! s:decimalpos(expr) abort
671671
" Remove trailing comments
672672
let l:expr = substitute(a:expr, '\v +;.*$', '', '')
673673
" Find first or last possible decimal separator candidate
674-
if g:ledger_align_last
675-
let pos = matchend(l:expr, '\v.*[' . g:ledger_decimal_sep . ']')
674+
if b:ledger_align_last
675+
let pos = matchend(l:expr, '\v.*[' . b:ledger_decimal_sep . ']')
676676
if pos > 0
677677
let pos = strchars(a:expr[:pos]) + 1
678678
endif
679679
else
680-
let pos = match(l:expr, '\v[' . g:ledger_decimal_sep . ']')
680+
let pos = match(l:expr, '\v[' . b:ledger_decimal_sep . ']')
681681
if pos > 0
682682
let pos = strchars(a:expr[:pos]) - 1
683683
endif
@@ -688,7 +688,7 @@ endfunction
688688
" Align the amount expression after an account name at the decimal point.
689689
"
690690
" This function moves the amount expression of a posting so that the decimal
691-
" separator is aligned at the column specified by g:ledger_align_at.
691+
" separator is aligned at the column specified by b:ledger_align_at.
692692
"
693693
" For example, after selecting:
694694
"
@@ -712,9 +712,9 @@ function! ledger#align_commodity() abort
712712
" Remove everything after the account name (including spaces):
713713
call setline('.', substitute(l:line, '\m^\s\+[^[:space:]].\{-}\zs\(\t\| \).*$', '', ''))
714714
let pos = -1
715-
if g:ledger_align_commodity == 1
715+
if b:ledger_align_commodity == 1
716716
let pos = 0
717-
elseif g:ledger_decimal_sep !=# ''
717+
elseif b:ledger_decimal_sep !=# ''
718718
" Find the position of the first decimal separator:
719719
let pos = s:decimalpos(rhs)
720720
endif
@@ -725,11 +725,11 @@ function! ledger#align_commodity() abort
725725
let pos = strchars(rhs[:pos])
726726
endif
727727
endif
728-
" Go to the column that allows us to align the decimal separator at g:ledger_align_at:
728+
" Go to the column that allows us to align the decimal separator at b:ledger_align_at:
729729
if pos >= 0
730-
call s:goto_col(g:ledger_align_at - pos - 1, 2)
730+
call s:goto_col(b:ledger_align_at - pos - 1, 2)
731731
else
732-
call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2, 2)
732+
call s:goto_col(b:ledger_align_at - strdisplaywidth(rhs) - 2, 2)
733733
endif " Append the part of the line that was previously removed:
734734
execute 'normal! a' . rhs
735735
endif
@@ -758,13 +758,13 @@ function! ledger#align_amount_at_cursor() abort
758758
let pos = len(@")
759759
endif
760760
" Paste text at the correct column and append/prepend default commodity:
761-
if g:ledger_commodity_before
762-
call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1, 2)
763-
execute 'normal! a' . g:ledger_default_commodity . g:ledger_commodity_sep
761+
if b:ledger_commodity_before
762+
call s:goto_col(b:ledger_align_at - pos - len(b:ledger_default_commodity) - len(b:ledger_commodity_sep) - 1, 2)
763+
execute 'normal! a' . b:ledger_default_commodity . b:ledger_commodity_sep
764764
normal! p
765765
else
766-
call s:goto_col(g:ledger_align_at - pos - 1, 2)
767-
execute 'normal! pa' . g:ledger_commodity_sep . g:ledger_default_commodity
766+
call s:goto_col(b:ledger_align_at - pos - 1, 2)
767+
execute 'normal! pa' . b:ledger_commodity_sep . b:ledger_default_commodity
768768
endif
769769
endfunction
770770

@@ -806,7 +806,7 @@ endfunction
806806
"
807807
" Returns 0 if the quickfix window is empty, 1 otherwise.
808808
function! s:quickfix_toggle(...) abort
809-
if g:ledger_use_location_list
809+
if b:ledger_use_location_list
810810
let l:list = 'l'
811811
let l:open = (len(getloclist(winnr())) > 0)
812812
else
@@ -815,13 +815,13 @@ function! s:quickfix_toggle(...) abort
815815
endif
816816

817817
if l:open
818-
execute (g:ledger_qf_vertical ? 'vert' : 'botright') l:list.'open' g:ledger_qf_size
818+
execute (b:ledger_qf_vertical ? 'vert' : 'botright') l:list.'open' b:ledger_qf_size
819819
" Set local mappings to quit the quickfix window or lose focus.
820820
nnoremap <silent> <buffer> <tab> <c-w><c-w>
821821
execute 'nnoremap <silent> <buffer> q :' l:list.'close<CR>'
822822
" Note that the following settings do not persist (e.g., when you close and re-open the quickfix window).
823823
" See: https://superuser.com/questions/356912/how-do-i-change-the-quickix-title-status-bar-in-vim
824-
if g:ledger_qf_hide_file
824+
if b:ledger_qf_hide_file
825825
setlocal conceallevel=2
826826
setlocal concealcursor=nc
827827
syntax match qfFile /^[^|]*/ transparent conceal
@@ -849,19 +849,19 @@ function! s:quickfix_populate(data) abort
849849
" Format to parse reports:
850850
set errorformat+=%f:%l\ %m
851851
set errorformat+=%-G%.%#
852-
execute (g:ledger_use_location_list ? 'l' : 'c').'getexpr' 'a:data'
852+
execute (b:ledger_use_location_list ? 'l' : 'c').'getexpr' 'a:data'
853853
let &errorformat = l:efm " Restore global errorformat
854854
return
855855
endfunction
856856

857857
" Build a ledger command to process the given file.
858858
function! s:ledger_cmd(file, args) abort
859-
let l:options = g:ledger_extra_options
860-
if len(g:ledger_date_format) > 0 && !b:ledger_is_hledger
861-
let l:options = join([l:options, '--date-format', g:ledger_date_format,
862-
\ '--input-date-format', g:ledger_date_format])
859+
let l:options = b:ledger_extra_options
860+
if len(b:ledger_date_format) > 0 && !b:ledger_is_hledger
861+
let l:options = join([l:options, '--date-format', b:ledger_date_format,
862+
\ '--input-date-format', b:ledger_date_format])
863863
endif
864-
return join([g:ledger_bin, l:options, '-f', shellescape(expand(a:file)), a:args])
864+
return join([b:ledger_bin, l:options, '-f', shellescape(expand(a:file)), a:args])
865865
endfunction
866866

867867
function! ledger#autocomplete_and_align() abort
@@ -883,7 +883,7 @@ endfunction
883883
" Use current line as input to ledger entry and replace with output. If there
884884
" are errors, they are echoed instead.
885885
function! ledger#entry() abort
886-
let l:output = split(system(s:ledger_cmd(g:ledger_main, join(['entry', '--', getline('.')]))), '\n')
886+
let l:output = split(system(s:ledger_cmd(b:ledger_main, join(['entry', '--', getline('.')]))), '\n')
887887
" Filter out warnings
888888
let l:output = filter(l:output, "v:val !~? '^Warning: '")
889889
" Errors may occur
@@ -928,7 +928,7 @@ function! ledger#output(report) abort
928928
return 0
929929
endif
930930
" Open a new buffer to show Ledger's output.
931-
execute get(s:winpos_map, g:ledger_winpos, 'bo new')
931+
execute get(s:winpos_map, b:ledger_winpos, 'bo new')
932932
setlocal buftype=nofile bufhidden=wipe modifiable nobuflisted noswapfile nowrap
933933
call append(0, a:report)
934934
setlocal nomodifiable
@@ -950,7 +950,7 @@ endfunction
950950
function! ledger#register(file, args) abort
951951
let l:cmd = s:ledger_cmd(a:file, join([
952952
\ 'register',
953-
\ "--format='" . g:ledger_qf_register_format . "'",
953+
\ "--format='" . b:ledger_qf_register_format . "'",
954954
\ "--prepend-format='%(filename):%(beg_line) '",
955955
\ a:args
956956
\ ]))
@@ -968,14 +968,14 @@ function! ledger#reconcile(file, account, target_amount) abort
968968
let l:cmd = s:ledger_cmd(a:file, join([
969969
\ 'register',
970970
\ '--uncleared',
971-
\ "--format='" . g:ledger_qf_reconcile_format . "'",
971+
\ "--format='" . b:ledger_qf_reconcile_format . "'",
972972
\ "--prepend-format='%(filename):%(beg_line) %(pending ? \"P\" : \"U\") '",
973973
\ shellescape(a:account)
974974
\ ]))
975975
let l:file = expand(a:file) " Needed for #show_balance() later
976976
call s:quickfix_populate(split(system(l:cmd), '\n'))
977977
if s:quickfix_toggle('Reconcile ' . a:account, 'Nothing to reconcile')
978-
let g:ledger_target_amount = a:target_amount
978+
let b:ledger_target_amount = a:target_amount
979979
" Show updated account balance upon saving, as long as the quickfix window is open
980980
augroup reconcile
981981
autocmd!
@@ -990,7 +990,7 @@ function! ledger#reconcile(file, account, target_amount) abort
990990
endfunction
991991

992992
function! s:finish_reconciling() abort
993-
unlet g:ledger_target_amount
993+
unlet b:ledger_target_amount
994994
augroup reconcile
995995
autocmd!
996996
augroup END
@@ -1015,11 +1015,11 @@ function! ledger#show_balance(file, ...) abort
10151015
\ '--empty',
10161016
\ '--collapse',
10171017
\ "--format='%(scrub(get_at(display_total, 0)))|%(scrub(get_at(display_total, 1)))|%(quantity(scrub(get_at(display_total, 1))))'",
1018-
\ (empty(g:ledger_default_commodity) ? '' : '-X ' . shellescape(g:ledger_default_commodity))
1018+
\ (empty(b:ledger_default_commodity) ? '' : '-X ' . shellescape(b:ledger_default_commodity))
10191019
\ ]))
10201020
let l:output = split(system(l:cmd), '\n')
10211021
" Errors may occur, for example, when the account has multiple commodities
1022-
" and g:ledger_default_commodity is empty.
1022+
" and b:ledger_default_commodity is empty.
10231023
if v:shell_error
10241024
call s:quickfix_populate(l:output)
10251025
call s:quickfix_toggle('Errors', 'Unable to parse errors')
@@ -1031,18 +1031,18 @@ function! ledger#show_balance(file, ...) abort
10311031
call s:error_message('Could not determine balance. Did you use a valid account?')
10321032
return
10331033
endif
1034-
echo g:ledger_pending_string
1034+
echo b:ledger_pending_string
10351035
echohl LedgerPending
10361036
echon l:amounts[0]
10371037
echohl NONE
1038-
echon ' ' g:ledger_cleared_string
1038+
echon ' ' b:ledger_cleared_string
10391039
echohl LedgerCleared
10401040
echon l:amounts[1]
10411041
echohl NONE
1042-
if exists('g:ledger_target_amount')
1043-
echon ' ' g:ledger_target_string
1042+
if exists('b:ledger_target_amount')
1043+
echon ' ' b:ledger_target_string
10441044
echohl LedgerTarget
1045-
echon printf('%.2f', (g:ledger_target_amount - str2float(l:amounts[2])))
1045+
echon printf('%.2f', (b:ledger_target_amount - str2float(l:amounts[2])))
10461046
echohl NONE
10471047
endif
10481048
endfunction

0 commit comments

Comments
 (0)