Skip to content

Commit b25e968

Browse files
author
Lifepillar
committed
Add a function for smart completion and alignment.
The idea is to bind this function to the <tab> key. Then, both autocompletion and alignment may be performed in Insert mode using the <tab> key (super-fast!). See the doc for the details.
1 parent 0532edf commit b25e968

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

autoload/ledger.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,18 @@ function! s:ledger_cmd(file, args)
486486
endf
487487
" }}}
488488

489+
function! ledger#autocomplete_and_align()
490+
if pumvisible()
491+
return "\<c-n>"
492+
" See http://stackoverflow.com/questions/23323747/vim-vimscript-get-exact-character-under-the-cursor
493+
elseif matchstr(getline('.'), '\%' . (col('.')-1) . 'c.') =~ '\d'
494+
norm h
495+
call ledger#align_amount_at_cursor()
496+
return "\<c-o>A"
497+
endif
498+
return "\<c-x>\<c-o>"
499+
endf
500+
489501
" Use current line as input to ledger entry and replace with output. If there
490502
" are errors, they are echoed instead.
491503
func! ledger#entry()

doc/ledger.txt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ Tips and useful commands
7777
The decimal separator can be set using `g:ledger_decimal_sep`. The default
7878
value of `g:ledger_decimal_sep` is `'.'`.
7979

80-
It is convenient to remap the command, for example to `<Leader>a`:
80+
See below for the recommended mappings.
8181

82-
au FileType ledger vnoremap <silent><buffer> <Leader>a \
83-
:LedgerAlign<CR>
84-
85-
* :call ledger#align_amount_at_cursor()
82+
* `:call ledger#align_amount_at_cursor()`
8683

8784
aligns the amount under the cursor and append/prepend the default currency.
8885
The default currency can be set using `g:ledger_default_commodity`. Whether
@@ -91,13 +88,23 @@ Tips and useful commands
9188
value is 1). A separator between the commodity and the amount may be set
9289
using `g:ledger_commodity_sep`.
9390

94-
It is convenient to define a mapping like the following:
91+
See below for the recommended mappings.
92+
93+
* `:call ledger#autocomplete_and_align()`
94+
95+
when the cursor is on a number or immediately after it, invokes
96+
`ledger#align_amount_at_cursor()` to align it and add the default currency;
97+
otherwise, performs autocompletion. If you define the following mappings
98+
then you may perform both autocompletion and alignment using the <Tab> key:
9599

96-
au FileType ledger inoremap <silent><buffer> <C-l> \
97-
<Esc>:call ledger#align_amount_at_cursor()<CR>
100+
au FileType ledger inoremap <silent> <Tab> \
101+
<C-r>=ledger#autocomplete_and_align()<CR>
102+
au FileType ledger vnoremap <silent> <Tab> :LedgerAlign<CR>
98103

99-
Now, you may type `123.45<C-l>`, and have `$123.45` properly aligned (assuming
100-
your default commodity is set to `'$'`).
104+
Now, you may type `asset:check<Tab><Space>123.45<Tab>`, and have the
105+
account name autocompleted and `$123.45` properly aligned (assuming your
106+
default commodity is set to `'$'`). Or you may press <Tab> in Visual mode
107+
to align a number of transactions at once.
101108

102109
* `:call ledger#entry()`
103110

0 commit comments

Comments
 (0)