Skip to content

Commit 7d41d6d

Browse files
committed
Refactor ledger#entry()
The entry front-end was buggy because of how it escaped the query. It was fixed by refactoring to use `systemlist(s:ledger_cmd())` instead of `read !` with `shellescape()`. If the call to `ledger entry` fails, it echoes the failure message, and leaves the input query to be amended by the user.
1 parent 0ebef8f commit 7d41d6d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

autoload/ledger.vim

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,6 @@ function! ledger#align_amount_at_cursor()
394394
endif
395395
endf!
396396

397-
func! ledger#entry()
398-
" enter a new transaction based on the text in the current line.
399-
let l = line('.') - 1 " Insert transaction at the current line (i.e., below the line above the current one)
400-
let query = getline('.')
401-
normal! "_dd
402-
exec l . 'read !' g:ledger_bin '-f' shellescape(expand(g:ledger_main)) 'entry' shellescape(query)
403-
endfunc
404-
405397
" Report generation {{{1
406398

407399
" Helper functions and variables {{{2
@@ -490,6 +482,22 @@ function! s:ledger_cmd(file, args)
490482
endf
491483
" }}}
492484

485+
" Use current line as input to ledger entry and replace with output. If there
486+
" are errors, they are echoed instead.
487+
func! ledger#entry()
488+
let l:output = systemlist(s:ledger_cmd(g:ledger_main, join(["entry", getline('.')])))
489+
" Filter out warnings
490+
let l:output = filter(l:output, "v:val !~? '^Warning: '")
491+
" Errors may occur
492+
if v:shell_error
493+
echomsg join(l:output)
494+
return
495+
endif
496+
" Append output so we insert instead of overwrite, then delete line
497+
call append('.', l:output)
498+
normal! "_dd
499+
endfunc
500+
493501
" Run an arbitrary ledger command and show the output in a new buffer. If
494502
" there are errors, no new buffer is opened: the errors are displayed in a
495503
" quickfix window instead.

0 commit comments

Comments
 (0)