From 11a27b44835d65e4533c5ddfeed725f90f85b6d3 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Thu, 14 Mar 2019 22:45:26 +0100 Subject: [PATCH 1/2] LedgerAlign: Better detect decimal point This commit detect the location of the decimal point consistently in the following circumstances (given g:ledger_decimal_sep='.'): -30 = 0 456.78 It uses the decimal point location if it can be found, and if it cannot, it uses the first character after the first group of digits. --- autoload/ledger.vim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/autoload/ledger.vim b/autoload/ledger.vim index 9d6cbc6..c3a6427 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -373,12 +373,15 @@ 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:]]*') - else + let pos = -1 + if g:ledger_decimal_sep != '' " Find the position of the first decimal separator: let pos = s:strpos(rhs, '\V' . g:ledger_decimal_sep) endif + if pos < 0 + " Find the position after the first digits + let pos = matchend(rhs, '\m\d[^[:space:]]*') + 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) From 4d1dcb9b3c7b59a983f32206bc7e3db825ac7ea9 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Thu, 14 Mar 2019 22:48:02 +0100 Subject: [PATCH 2/2] 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. --- autoload/ledger.vim | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/autoload/ledger.vim b/autoload/ledger.vim index c3a6427..cde6205 100644 --- a/autoload/ledger.vim +++ b/autoload/ledger.vim @@ -333,9 +333,11 @@ function! s:findall(text, rx) endf " Move the cursor to the specified column, filling the line with spaces if necessary. -function! s:goto_col(pos) - exec "normal!" a:pos . "|" - let diff = a:pos - virtcol('.') +" Ensure that at least min_spaces are added, and go to the end of the line if +" the line is already too long +function! s:goto_col(pos, min_spaces) + exec "normal!" "$" + let diff = max([a:min_spaces, a:pos - virtcol('.')]) if diff > 0 | exec "normal!" diff . "a " | endif endf @@ -384,11 +386,10 @@ function! ledger#align_commodity() 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) + call s:goto_col(g:ledger_align_at - pos - 1, 2) else - call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2) - endif - " Append the part of the line that was previously removed: + call s:goto_col(g:ledger_align_at - strdisplaywidth(rhs) - 2, 2) + endif " Append the part of the line that was previously removed: exe 'normal! a' . rhs endif endf! @@ -404,11 +405,11 @@ function! ledger#align_amount_at_cursor() endif " Paste text at the correct column and append/prepend default commodity: if g:ledger_commodity_before - call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1) + call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1, 2) exe 'normal! a' . g:ledger_default_commodity . g:ledger_commodity_sep normal! p else - call s:goto_col(g:ledger_align_at - pos - 1) + call s:goto_col(g:ledger_align_at - pos - 1, 2) exe 'normal! pa' . g:ledger_commodity_sep . g:ledger_default_commodity endif endf!