Skip to content

Commit 56a3038

Browse files
authored
Merge pull request #73 from mildred/master
Fix LedgerAlign corruptions
2 parents 6eb3bb2 + 4d1dcb9 commit 56a3038

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

autoload/ledger.vim

Lines changed: 16 additions & 12 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

@@ -373,19 +375,21 @@ function! ledger#align_commodity()
373375
if rhs != ''
374376
" Remove everything after the account name (including spaces):
375377
.s/\m^\s\+[^[:space:]].\{-}\zs\(\t\| \).*$//
376-
if g:ledger_decimal_sep == ''
377-
let pos = matchend(rhs, '\m\d[^[:space:]]*')
378-
else
378+
let pos = -1
379+
if g:ledger_decimal_sep != ''
379380
" Find the position of the first decimal separator:
380381
let pos = s:strpos(rhs, '\V' . g:ledger_decimal_sep)
381382
endif
383+
if pos < 0
384+
" Find the position after the first digits
385+
let pos = matchend(rhs, '\m\d[^[:space:]]*')
386+
endif
382387
" Go to the column that allows us to align the decimal separator at g:ledger_align_at:
383388
if pos > 0
384-
call s:goto_col(g:ledger_align_at - pos - 1)
389+
call s:goto_col(g:ledger_align_at - pos - 1, 2)
385390
else
386-
call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2)
387-
endif
388-
" 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:
389393
exe 'normal! a' . rhs
390394
endif
391395
endf!
@@ -401,11 +405,11 @@ function! ledger#align_amount_at_cursor()
401405
endif
402406
" Paste text at the correct column and append/prepend default commodity:
403407
if g:ledger_commodity_before
404-
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)
405409
exe 'normal! a' . g:ledger_default_commodity . g:ledger_commodity_sep
406410
normal! p
407411
else
408-
call s:goto_col(g:ledger_align_at - pos - 1)
412+
call s:goto_col(g:ledger_align_at - pos - 1, 2)
409413
exe 'normal! pa' . g:ledger_commodity_sep . g:ledger_default_commodity
410414
endif
411415
endf!

0 commit comments

Comments
 (0)