Skip to content

Commit c27980b

Browse files
authored
Merge pull request #47 from abobov/master
Fix alignment of multi-byte commodity.
2 parents 5a0ef1c + d60953b commit c27980b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

autoload/ledger.vim

+16-4
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ function! s:goto_col(pos)
339339
if diff > 0 | exec "normal!" diff . "a " | endif
340340
endf
341341

342+
" Return substring position (in chars).
343+
function! s:strpos(expr, pat)
344+
let pos = match(a:expr, a:pat)
345+
if pos > 0
346+
let pos = strchars(a:expr[:pos]) - 1
347+
endif
348+
return pos
349+
endf
350+
342351
" Align the amount expression after an account name at the decimal point.
343352
"
344353
" This function moves the amount expression of a posting so that the decimal
@@ -368,7 +377,7 @@ function! ledger#align_commodity()
368377
let pos = matchend(rhs, '\m\d[^[:space:]]*')
369378
else
370379
" Find the position of the first decimal separator:
371-
let pos = match(rhs, '\V' . g:ledger_decimal_sep)
380+
let pos = s:strpos(rhs, '\V' . g:ledger_decimal_sep)
372381
endif
373382
" Go to the column that allows us to align the decimal separator at g:ledger_align_at:
374383
if pos > 0
@@ -386,14 +395,17 @@ function! ledger#align_amount_at_cursor()
386395
" Select and cut text:
387396
normal! viWd
388397
" Find the position of the decimal separator
389-
let pos = match(@", g:ledger_decimal_sep) " Returns zero when the separator is the empty string
398+
let pos = s:strpos(@", '\V' . g:ledger_decimal_sep) " Returns zero when the separator is the empty string
399+
if pos <= 0
400+
let pos = len(@")
401+
endif
390402
" Paste text at the correct column and append/prepend default commodity:
391403
if g:ledger_commodity_before
392-
call s:goto_col(g:ledger_align_at - (pos > 0 ? pos : len(@")) - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1)
404+
call s:goto_col(g:ledger_align_at - pos - len(g:ledger_default_commodity) - len(g:ledger_commodity_sep) - 1)
393405
exe 'normal! a' . g:ledger_default_commodity . g:ledger_commodity_sep
394406
normal! p
395407
else
396-
call s:goto_col(g:ledger_align_at - (pos > 0 ? pos : len(@")) - 1)
408+
call s:goto_col(g:ledger_align_at - pos - 1)
397409
exe 'normal! pa' . g:ledger_commodity_sep . g:ledger_default_commodity
398410
endif
399411
endf!

0 commit comments

Comments
 (0)