@@ -339,6 +339,15 @@ function! s:goto_col(pos)
339
339
if diff > 0 | exec " normal!" diff . " a " | endif
340
340
endf
341
341
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
+
342
351
" Align the amount expression after an account name at the decimal point.
343
352
"
344
353
" This function moves the amount expression of a posting so that the decimal
@@ -368,7 +377,7 @@ function! ledger#align_commodity()
368
377
let pos = matchend (rhs, ' \m\d[^[:space:]]*' )
369
378
else
370
379
" 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 )
372
381
endif
373
382
" Go to the column that allows us to align the decimal separator at g:ledger_align_at:
374
383
if pos > 0
@@ -386,14 +395,17 @@ function! ledger#align_amount_at_cursor()
386
395
" Select and cut text:
387
396
normal ! viWd
388
397
" 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
390
402
" Paste text at the correct column and append/prepend default commodity:
391
403
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 )
393
405
exe ' normal! a' . g: ledger_default_commodity . g: ledger_commodity_sep
394
406
normal ! p
395
407
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 )
397
409
exe ' normal! pa' . g: ledger_commodity_sep . g: ledger_default_commodity
398
410
endif
399
411
endf !
0 commit comments