Skip to content

Commit ba26b76

Browse files
authored
Merge pull request #41 from lifepillar/smart
Smart autocompletion and alignment with a single key (rebased)
2 parents 0532edf + 696f876 commit ba26b76

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
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: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ TIPS *ledger-tips*
3030

3131
Tips and useful commands
3232

33-
* Try account-completion (as explained below)
33+
* Try account-completion (as explained below). If you use YouCompleteMe, you
34+
should disable it for Ledger files. Put this in your .vimrc:
35+
36+
if exists('g:ycm_filetype_blacklist')
37+
call extend(g:ycm_filetype_blacklist, { 'ledger': 1 })
38+
endif
3439

3540
* You may use `:make` for syntax checking. It may be convenient to define a
3641
mapping for the following command:
@@ -77,12 +82,9 @@ Tips and useful commands
7782
The decimal separator can be set using `g:ledger_decimal_sep`. The default
7883
value of `g:ledger_decimal_sep` is `'.'`.
7984

80-
It is convenient to remap the command, for example to `<Leader>a`:
81-
82-
au FileType ledger vnoremap <silent><buffer> <Leader>a \
83-
:LedgerAlign<CR>
85+
See below for the recommended mappings.
8486

85-
* :call ledger#align_amount_at_cursor()
87+
* `:call ledger#align_amount_at_cursor()`
8688

8789
aligns the amount under the cursor and append/prepend the default currency.
8890
The default currency can be set using `g:ledger_default_commodity`. Whether
@@ -91,13 +93,31 @@ Tips and useful commands
9193
value is 1). A separator between the commodity and the amount may be set
9294
using `g:ledger_commodity_sep`.
9395

94-
It is convenient to define a mapping like the following:
96+
See below for the recommended mappings.
97+
98+
* `:call ledger#autocomplete_and_align()`
99+
100+
when the cursor is on a number or immediately after it, invokes
101+
`ledger#align_amount_at_cursor()` to align it and add the default currency;
102+
otherwise, performs autocompletion. If you define the following mappings in
103+
your `.vimrc` then you may perform both autocompletion and alignment using
104+
the <Tab> key:
105+
106+
au FileType ledger inoremap <silent> <Tab> \
107+
<C-r>=ledger#autocomplete_and_align()<CR>
108+
au FileType ledger vnoremap <silent> <Tab> :LedgerAlign<CR>
109+
110+
Alternatively, you may create a file `.vim/after/ftplugin/ledger.vim`
111+
containing the following definitions:
95112

96-
au FileType ledger inoremap <silent><buffer> <C-l> \
97-
<Esc>:call ledger#align_amount_at_cursor()<CR>
113+
inoremap <silent> <buffer> <Tab> \
114+
<C-r>=ledger#autocomplete_and_align()<CR>
115+
vnoremap <silent> <buffer> <Tab> :LedgerAlign<CR>
98116

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

102122
* `:call ledger#entry()`
103123

0 commit comments

Comments
 (0)