From 9f853addd1fdb44966eaaf904114542061a38c5b Mon Sep 17 00:00:00 2001 From: Wynn Wolf Arbor Date: Tue, 10 Mar 2020 17:37:03 +0100 Subject: Replace AlignCommodity with bean-format AlignCommodity replicates the functionality of the bean-format command that is shipped with beancount itself. Therefore, we can fully replace it by having vim invoke bean-format through formatprg. Even though bean-format's output might differ from AlignCommodity in significant ways, we feel that using one unified tool is superior to having several (possibly incompatible) custom implementations. That way, if bean-format is improved upon in the future, all downstream consumers profit. For regular usage, however, this change should be transparent. Note that while bean-format supports a few different ways of formatting the ledger, we have opted to hard-code one specific setting. This will match the default behaviour of fava, aligning currencies in column 61. Managing the arguments passed to bean-format dynamically is considered out of scope, especially given the fact that the user need only override formatprg to get a custom setting. This commit will also drop the g:beancount_separator_col variable and the tests for AlignCommodity. Ideally, testing will now done upstream for bean-format itself. --- README.md | 2 -- autoload/beancount.vim | 46 ---------------------------------------------- doc/beancount.txt | 38 ++++---------------------------------- ftplugin/beancount.vim | 10 ++-------- test/align.vader | 45 --------------------------------------------- 5 files changed, 6 insertions(+), 135 deletions(-) delete mode 100644 test/align.vader diff --git a/README.md b/README.md index 4c323f9..b2d00a8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,4 @@ copy all of the files into the appropriate places inside your '.vim' directory. * Use `:make` to run `bean-check` and load errors in the quickfix window. -* The `AlignCommodity` command lines up all your decimal points. - For full details, see [doc/beancount.txt](doc/beancount.txt). diff --git a/autoload/beancount.vim b/autoload/beancount.vim index c5d8f97..91f6999 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -6,52 +6,6 @@ function! s:startswith(string, prefix) abort return strpart(a:string, 0, strlen(a:prefix)) == a:prefix endfunction -" Align currency on decimal point. -function! beancount#align_commodity(line1, line2) abort - " Save cursor position to adjust it if necessary. - let l:cursor_col = col('.') - let l:cursor_line = line('.') - - " Increment at start of loop, because of continue statements. - let l:current_line = a:line1 - 1 - while l:current_line < a:line2 - let l:current_line += 1 - let l:line = getline(l:current_line) - " This matches an account name followed by a space in one of the two - " following cases: - " - A posting line, i.e., the line starts with indentation followed - " by an optional flag and the account. - " - A balance directive, i.e., the line starts with a date followed - " by the 'balance' keyword and the account. - " - A price directive, i.e., the line starts with a date followed by - " the 'price' keyword and a currency. - let l:end_account = matchend(l:line, '^\v' . - \ '[\-/[:digit:]]+\s+balance\s+([A-Z][A-Za-z0-9\-]+)(:[A-Z][A-Za-z0-9\-]*)+ ' . - \ '|[\-/[:digit:]]+\s+price\s+\S+ ' . - \ '|\s+([!&#?%PSTCURM]\s+)?([A-Z][A-Za-z0-9\-]+)(:[A-Z][A-Za-z0-9\-]*)+ ' - \ ) - if l:end_account < 0 - continue - endif - - " Where does the number begin? - let l:begin_number = matchend(l:line, '^ *', l:end_account) - - " Look for a minus sign and a number (possibly containing commas) and - " align on the next column. - let l:separator = matchend(l:line, '^\v([-+])?[,[:digit:]]+', l:begin_number) + 1 - if l:separator < 0 | continue | endif - let l:has_spaces = l:begin_number - l:end_account - let l:need_spaces = g:beancount_separator_col - l:separator + l:has_spaces - if l:need_spaces < 0 | continue | endif - call setline(l:current_line, l:line[0 : l:end_account - 1] . repeat(' ', l:need_spaces) . l:line[ l:begin_number : -1]) - if l:current_line == l:cursor_line && l:cursor_col >= l:end_account - " Adjust cursor position for continuity. - call cursor(0, l:cursor_col + l:need_spaces - l:has_spaces) - endif - endwhile -endfunction - function! s:count_expression(text, expression) abort return len(split(a:text, a:expression, 1)) - 1 endfunction diff --git a/doc/beancount.txt b/doc/beancount.txt index 63e25f5..e3801cc 100644 --- a/doc/beancount.txt +++ b/doc/beancount.txt @@ -21,35 +21,6 @@ Contents: COMMANDS *beancount-commands* - *beancount-:AlignCommodity* -:AlignCommodity Adds spaces between an account and commodity so that the - decimal points of the commodities all occur in the column - given by |g:beancount_separator_col|. If an amount has no - decimal point, the imaginary decimal point to the right - of the least significant digit will align. - - The command acts on a range, with the default being the - current line. If the cursor happens to be inside that - range and to the right of the account name, the cursor - will be pushed to the right the appropriate amount, so - that it remains on the same character. - - The script assumes the use of spaces for alignment. It - does not understand tabs. - - You can use the following insert-mode remap to - automatically align commodities every time you type a - decimal point: > - - inoremap . .:AlignCommodity -< - You may also want to set other mappings for this. For - example, I use > - - nnoremap = :AlignCommodity - vnoremap = :AlignCommodity -< - *beancount-:GetContext* :GetContext Uses bean-doctor context to display the context of the current line. @@ -73,17 +44,16 @@ OPTIONS *beancount-options* Default value: 0 -*g:beancount_separator_col* - The column that the decimal separator is aligned to. - - Default value: 50 - *b:beancount_root* Set the root Beancount file. This is used to gather values for the completion. If not set, the current file will be used. Default value: not set +FORMATTING *beancount-formatting* + +The plugin sets |'formatprg'| to 'bean-format -c61' to provide a formatter for +beancount files. See also |gq|. COMPLETION *beancount-completion* diff --git a/ftplugin/beancount.vim b/ftplugin/beancount.vim index b6e3540..ab54905 100644 --- a/ftplugin/beancount.vim +++ b/ftplugin/beancount.vim @@ -3,17 +3,14 @@ if exists('b:did_ftplugin') endif let b:did_ftplugin = 1 -let b:undo_ftplugin = 'setlocal foldmethod< comments< commentstring<' +let b:undo_ftplugin = 'setlocal foldmethod< formatprg< comments< commentstring<' setl foldmethod=syntax +setl formatprg=bean-format\ -c61 setl comments=b:; setl commentstring=;%s compiler beancount -" This variable customizes the behavior of the AlignCommodity command. -if !exists('g:beancount_separator_col') - let g:beancount_separator_col = 50 -endif if !exists('g:beancount_account_completion') let g:beancount_account_completion = 'default' endif @@ -21,9 +18,6 @@ if !exists('g:beancount_detailed_first') let g:beancount_detailed_first = 0 endif -command! -buffer -range AlignCommodity - \ :call beancount#align_commodity(, ) - command! -buffer -range GetContext \ :call beancount#get_context() diff --git a/test/align.vader b/test/align.vader deleted file mode 100644 index db6563e..0000000 --- a/test/align.vader +++ /dev/null @@ -1,45 +0,0 @@ -Given beancount: - 2012-12-12 balance Assets:LongLongLongAccount 50.00 - 2012-12-12 balance Assets:Cash 50.00 - 2012-12-12 balance Assets:Cash -50.00 - 2012-12-12 balance Assets:Cash +50.00 - 2012-12-12 price EUR 50.00 USD - metadata: 50 - Assets:Cash 50 - Assets:Cash 50.00 - ! Assets:Cash 50.00 - Assets:Cash 50.00 - Assets:Cash 50.00 USD - -Execute (align): - %AlignCommodity - -Expect beancount: - 2012-12-12 balance Assets:LongLongLongAccount 50.00 - 2012-12-12 balance Assets:Cash 50.00 - 2012-12-12 balance Assets:Cash -50.00 - 2012-12-12 balance Assets:Cash +50.00 - 2012-12-12 price EUR 50.00 USD - metadata: 50 - Assets:Cash 50 - Assets:Cash 50.00 - ! Assets:Cash 50.00 - Assets:Cash 50.00 - Assets:Cash 50.00 USD - -Execute (change alignment column and align again): - let g:beancount_separator_col=40 - %AlignCommodity - -Expect beancount: - 2012-12-12 balance Assets:LongLongLongAccount 50.00 - 2012-12-12 balance Assets:Cash 50.00 - 2012-12-12 balance Assets:Cash -50.00 - 2012-12-12 balance Assets:Cash +50.00 - 2012-12-12 price EUR 50.00 USD - metadata: 50 - Assets:Cash 50 - Assets:Cash 50.00 - ! Assets:Cash 50.00 - Assets:Cash 50.00 - Assets:Cash 50.00 USD -- cgit v1.2.3-2-gb3c3