Skip to content

Commit 4d1dcb9

Browse files
author
Mildred Ki'Lya
committed
LedgerAlign: correct when account name is too long
When account names are too long, LedgerAlign was currupting the aligned lines. This commit ensures that: - In any case, if the account name is longer than the aligning column, the line is not corrupted and the amount is put at the end of the line (instead of putting it in the middle of the account name) - In any case, it ensures that there is a minimum of 2 spaces between the account name and the amount.
1 parent 11a27b4 commit 4d1dcb9

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

autoload/ledger.vim

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ function! s:findall(text, rx)
333333
endf
334334

335335
" Move the cursor to the specified column, filling the line with spaces if necessary.
336-
function! s:goto_col(pos)
337-
exec "normal!" a:pos . "|"
338-
let diff = a:pos - virtcol('.')
336+
" Ensure that at least min_spaces are added, and go to the end of the line if
337+
" the line is already too long
338+
function! s:goto_col(pos, min_spaces)
339+
exec "normal!" "$"
340+
let diff = max([a:min_spaces, a:pos - virtcol('.')])
339341
if diff > 0 | exec "normal!" diff . "a " | endif
340342
endf
341343

@@ -384,11 +386,10 @@ function! ledger#align_commodity()
384386
endif
385387
" Go to the column that allows us to align the decimal separator at g:ledger_align_at:
386388
if pos > 0
387-
call s:goto_col(g:ledger_align_at - pos - 1)
389+
call s:goto_col(g:ledger_align_at - pos - 1, 2)
388390
else
389-
call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2)
390-
endif
391-
" Append the part of the line that was previously removed:
391+
call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2, 2)
392+
endif " Append the part of the line that was previously removed:
392393
exe 'normal! a' . rhs
393394
endif
394395
endf!
@@ -404,11 +405,11 @@ function! ledger#align_amount_at_cursor()
404405
endif
405406
" Paste text at the correct column and append/prepend default commodity:
406407
if g:ledger_commodity_before
407-
call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1)
408+
call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1, 2)
408409
exe 'normal! a' . g:ledger_default_commodity . g:ledger_commodity_sep
409410
normal! p
410411
else
411-
call s:goto_col(g:ledger_align_at - pos - 1)
412+
call s:goto_col(g:ledger_align_at - pos - 1, 2)
412413
exe 'normal! pa' . g:ledger_commodity_sep . g:ledger_default_commodity
413414
endif
414415
endf!

0 commit comments

Comments
 (0)