robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
710431c696
master
1" Beancount specific formatting 2" Language: beancount 3" Maintainer: Nathan Grigg 4" Latest Revision: 2021-03-06 5 6" Align currency on decimal point. 7function ! beancount#align_commodity ( line1 , line2 ) abort 8" Save cursor position to adjust it if necessary. 9let l: cursor_col = col ( '.' ) 10let l: cursor_line = line ( '.' ) 11 12" Increment at start of loop, because of continue statements. 13let l: current_line = a: line1 - 1 14while l: current_line < a: line2 15let l: current_line += 1 16let l: line = getline ( l: current_line ) 17" This matches an account name followed by a space in one of the two 18" following cases: 19" - A posting line, i.e., the line starts with indentation followed 20" by an optional flag and the account. 21" - A balance directive, i.e., the line starts with a date followed 22" by the 'balance' keyword and the account. 23" - A price directive, i.e., the line starts with a date followed by 24" the 'price' keyword and a currency. 25let l: end_account = matchend ( l: line , '\v' . 26 \'^[\-/[:digit:]]+\s+balance\s+([A-Z][A-Za-z0-9\-]+)(:[A-Z0-9][A-Za-z0-9\-]*)+ ' . 27 \'|^[\-/[:digit:]]+\s+price\s+\S+ ' . 28 \'|^\s+([!&#?%PSTCURM]\s+)?([A-Z][A-Za-z0-9\-]+)(:[A-Z0-9][A-Za-z0-9\-]*)+ ' 29 \) 30if l: end_account < 0 31continue 32endif 33 34" Where does the number begin? 35let l: begin_number = matchend ( l: line , '^ *' , l: end_account ) 36 37" Look for a minus sign and a number (possibly containing commas) and 38" align on the next column. 39let l: separator = matchend ( l: line , '^\v([-+])?[,[:digit:]]+' , l: begin_number ) + 1 40if l: separator < 0 |continue |endif 41let l: has_spaces = l: begin_number - l: end_account 42let l: need_spaces = g: beancount_separator_col - l: separator + l: has_spaces 43if l: need_spaces < 0 |continue |endif 44call setline ( l: current_line , l: line [ 0 : l: end_account - 1 ] . repeat ( ' ' , l: need_spaces ) . l: line [ l: begin_number : -1 ]) 45if l: current_line == l: cursor_line && l: cursor_col >= l: end_account 46" Adjust cursor position for continuity. 47call cursor ( 0 , l: cursor_col + l: need_spaces - l: has_spaces ) 48endif 49endwhile 50endfunction 51 52" Call bean-doctor on the current line and dump output into a scratch buffer 53function ! beancount#get_context () abort 54let l: context = system ( 'bean-doctor context ' . shellescape ( expand ( '%' )) . ' ' . line ( '.' )) 55botright new 56setlocal buftype = nofilebufhidden = hidenoswapfile 57call append ( 0 , split ( l: context , '\v\n' )) 58normal ! gg 59endfunction