aboutsummaryrefslogtreecommitdiffstats
path: root/autoload/beancount.vim
blob: ecd423bc5ed371c3983c091640c6d934f58984e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
" Equivalent to python's startswith
" Matches based on user's ignorecase preference
function! s:startswith(string, prefix)
  return strpart(a:string, 0, strlen(a:prefix)) == a:prefix
endfunction

" Align currency on decimal point.
function! beancount#align_commodity(line1, line2)
    " Saving cursor position to adjust it if necessary.
    let cursor_col = col('.')
    let cursor_line = line('.')

    " This lets me increment at start of loop, because of continue statements.
    let i = a:line1 - 1
    while i < a:line2
        let i += 1
        let s = getline(i)
        " This matches an account name followed by a space. There may be
        " some conflicts with non-transaction syntax that I don't know about.
        " It won't match a comment or any non-indented line.
        let end_acc = matchend(s, '^\v(([\-/[:digit:]]+\s+(balance|price))|\s+[!&#?%PSTCURM])?\s+\S+[^:] ')
        if end_acc < 0 | continue | endif
        " Where does commodity amount begin?
        let end_space = matchend(s, '^ *', end_acc)

        " Now look for a minus sign and a number, and align on the next column.
        let l:comma = g:beancount_decimal_separator == ',' ? '.' : ','
        let separator = matchend(s, '^\v(-)?[' . l:comma . '[:digit:]]+', end_space) + 1
        if separator < 0 | continue | endif
        let has_spaces = end_space - end_acc
        let need_spaces = g:beancount_separator_col - separator + has_spaces
        if need_spaces < 0 | continue | endif
        call setline(i, s[0 : end_acc - 1] . repeat(" ", need_spaces) . s[ end_space : -1])
        if i == cursor_line && cursor_col >= end_acc
            " Adjust cursor position for continuity.
            call cursor(0, cursor_col + need_spaces - has_spaces)
        endif
    endwhile
endfunction

function! s:count_expression(text, expression)
  return len(split(a:text, a:expression, 1)) - 1
endfunction

function! s:sort_accounts_by_depth(name1, name2)
  let l:depth1 = s:count_expression(a:name1, ':')
  let l:depth2 = s:count_expression(a:name2, ':')
  return depth1 == depth2 ? 0 : depth1 > depth2 ? 1 : -1
endfunction

let s:directives = ["open", "close", "commodity", "txn", "balance", "pad", "note", "document", "price", "event", "query", "custom"]

" ------------------------------
" Completion functions
" ------------------------------
function! beancount#complete(findstart, base)
    if a:findstart
        let l:col = searchpos('\s', "bn", line("."))[1]
        if col == 0
            return -1
        else
            return col
        endif
    endif

    let l:partial_line = strpart(getline("."), 0, getpos(".")[2])
    " Match directive types
    if l:partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d \S*$'
        return beancount#complete_basic(s:directives, a:base, '')
    endif

    let l:two_tokens = searchpos('\S\+\s', "bn", line("."))[1]
    let l:prev_token = strpart(getline("."), l:two_tokens, getpos(".")[2] - l:two_tokens)
    " Match curriences if previous token is number
    if l:prev_token =~ '^\d\+\([\.,]\d\+\)*'
        call beancount#load_currencies()
        return beancount#complete_basic(b:beancount_currencies, a:base, '')
    endif

    let l:first = strpart(a:base, 0, 1)
    let l:rest = strpart(a:base, 1)
    if l:first == "#"
        call beancount#load_tags()
        return beancount#complete_basic(b:beancount_tags, l:rest, '#')
    elseif l:first == "^"
        call beancount#load_links()
        return beancount#complete_basic(b:beancount_links, l:rest, '^')
    elseif l:first == '"'
        call beancount#load_payees()
        return beancount#complete_basic(b:beancount_payees, l:rest, '"')
    else
        call beancount#load_accounts()
        return beancount#complete_account(a:base)
    endif
endfunction

function! s:get_root()
    if exists('b:beancount_root')
        return b:beancount_root
    endif
    return expand('%')
endfunction

function! beancount#load_accounts()
    if !exists('b:beancount_accounts')
        let l:root = s:get_root()
        let b:beancount_accounts = beancount#find_accounts(l:root)
    endif
endfunction

function! beancount#load_tags()
    if !exists('b:beancount_tags')
        let l:root = s:get_root()
        let b:beancount_tags = beancount#find_tags(l:root)
    endif
endfunction

function! beancount#load_links()
    if !exists('b:beancount_links')
        let l:root = s:get_root()
        let b:beancount_links = beancount#find_links(l:root)
    endif
endfunction

function! beancount#load_currencies()
    if !exists('b:beancount_currencies')
        let l:root = s:get_root()
        let b:beancount_currencies = beancount#find_currencies(l:root)
    endif
endfunction

function! beancount#load_payees()
    if !exists('b:beancount_payees')
        let l:root = s:get_root()
        let b:beancount_payees = beancount#find_payees(l:root)
    endif
endfunction

" General completion function
function! beancount#complete_basic(input, base, prefix)
    let l:matches = filter(copy(a:input), 's:startswith(v:val, a:base)')

    return map(l:matches, 'a:prefix . v:val')
endfunction

" Complete account name.
function! beancount#complete_account(base)
    if g:beancount_account_completion ==? 'chunks'
        let l:pattern = '^\V' . substitute(a:base, ":", '\\[^:]\\*:', "g") . '\[^:]\*'
    else
        let l:pattern = '^\V\.\*' . substitute(a:base, ":", '\\.\\*:\\.\\*', "g") . '\.\*'
    endif

    let l:matches = []
    let l:index = -1
    while 1
        let l:index = match(b:beancount_accounts, l:pattern, l:index + 1)
        if l:index == -1 | break | endif
        call add(l:matches, matchstr(b:beancount_accounts[l:index], l:pattern))
    endwhile

    if g:beancount_detailed_first
        let l:matches = reverse(sort(l:matches, 's:sort_accounts_by_depth'))
    endif

    return l:matches
endfunction

function! beancount#query_single(root_file, query)
python << EOF
import vim
import subprocess
import os

# We intentionally want to ignore stderr so it doesn't mess up our query processing
output = subprocess.check_output(['bean-query', vim.eval('a:root_file'), vim.eval('a:query')], stderr=open(os.devnull, 'w')).split('\n')
print(output)
output = output[2:]

result_list = [y for y in (x.strip() for x in output) if y]

vim.command('return [{}]'.format(','.join(repr(x) for x in sorted(result_list))))
EOF
endfunction

" Get list of accounts.
function! beancount#find_accounts(root_file)
    return beancount#query_single(a:root_file, 'select distinct account;')
endfunction

" Get list of tags.
function! beancount#find_tags(root_file)
    return beancount#query_single(a:root_file, 'select distinct tags;')
endfunction

" Get list of links.
function! beancount#find_links(root_file)
    return beancount#query_single(a:root_file, 'select distinct links;')
endfunction

" Get list of currencies.
function! beancount#find_currencies(root_file)
    return beancount#query_single(a:root_file, 'select distinct currency;')
endfunction

" Get list of payees.
function! beancount#find_payees(root_file)
    return beancount#query_single(a:root_file, 'select distinct payee;')
endfunction

" Call bean-doctor on the current line and dump output into a scratch buffer
function! beancount#get_context()
    let context = system('bean-doctor context ' . expand('%') . ' ' . line('.'))
    botright new
    setlocal buftype=nofile bufhidden=hide noswapfile
    call append(0, split(context, '\v\n'))
    normal! gg
endfunction