Skip to content

Commit 071b425

Browse files
author
Lifepillar
committed
Split ledger#report() function.
Currently, the function is responsible for two independent tasks: running a report and creating a new buffer to display ledger’s output. In some situations, it may be convenient to be able to run a report without immediately displaying the output (e.g., because the user wants to perform some post-processing or the user wants to send the output somewhere else). Also, it is better to have functions that perform a single, simple, task than to have monolithic functions that perform multiple tasks. This commit addresses the above by splitting ledger#report() into two functions: - ledger#report(): now this just runs a ledger’s command and returns the output as a String. - ledger#output(): this new function takes a String containing a report and creates a new buffer to display it. Both functions have suitable return values that the user may use to check a function’s result (note that, before this commit, ledger#report() does not return anything). This change is backward-compatible and should cause no regressions. In particular, the behaviour of the :Ledger command remains unchanged.
1 parent 726359d commit 071b425

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

autoload/ledger.vim

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,21 +509,34 @@ endfunc
509509
" Parameters:
510510
" file The file to be processed.
511511
" args A string of Ledger command-line arguments.
512+
"
513+
" Returns:
514+
" Ledger's output as a String.
512515
function! ledger#report(file, args)
513516
let l:output = systemlist(s:ledger_cmd(a:file, a:args))
514517
if v:shell_error " If there are errors, show them in a quickfix/location list.
515518
call s:quickfix_populate(l:output)
516519
call s:quickfix_toggle('Errors', 'Unable to parse errors')
517-
return
518520
endif
519-
if empty(l:output)
521+
return l:output
522+
endf
523+
524+
" Open the output of a Ledger's command in a new buffer.
525+
"
526+
" Parameters:
527+
" report A String containing the output of a Ledger's command.
528+
"
529+
" Returns:
530+
" 1 if a new buffer is created; 0 otherwise.
531+
function! ledger#output(report)
532+
if empty(a:report)
520533
call s:warning_message('No results')
521-
return
534+
return 0
522535
endif
523536
" Open a new buffer to show Ledger's output.
524537
execute get(s:winpos_map, g:ledger_winpos, "bo new")
525-
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
526-
call append(0, l:output)
538+
setlocal buftype=nofile bufhidden=wipe modifiable nobuflisted noswapfile nowrap
539+
call append(0, a:report)
527540
setlocal nomodifiable
528541
" Set local mappings to quit window or lose focus.
529542
nnoremap <silent> <buffer> <tab> <c-w><c-w>
@@ -532,6 +545,7 @@ function! ledger#report(file, args)
532545
syntax match LedgerNumber /-\@1<!\d\+\([,.]\d\+\)\+/
533546
syntax match LedgerNegativeNumber /-\d\+\([,.]\d\+\)\+/
534547
syntax match LedgerImproperPerc /\d\d\d\+%/
548+
return 1
535549
endf
536550

537551
" Show an arbitrary register report in a quickfix list.

ftplugin/ledger.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ command! -buffer -nargs=? -complete=customlist,s:autocomplete_account_or_payee
429429
\ Balance call ledger#show_balance(g:ledger_main, <q-args>)
430430

431431
command! -buffer -nargs=+ -complete=customlist,s:autocomplete_account_or_payee
432-
\ Ledger call ledger#report(g:ledger_main, <q-args>)
432+
\ Ledger call ledger#output(ledger#report(g:ledger_main, <q-args>))
433433

434434
command! -buffer -range LedgerAlign <line1>,<line2>call ledger#align_commodity()
435435

0 commit comments

Comments
 (0)