diff options
| -rw-r--r-- | autoload/coc/source/khard.vim | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/autoload/coc/source/khard.vim b/autoload/coc/source/khard.vim index 63dd8bd..3cd68de 100644 --- a/autoload/coc/source/khard.vim +++ b/autoload/coc/source/khard.vim @@ -1,13 +1,4 @@ -" global contacts list -let s:contacts = [] - -function! coc#source#khard#init() abort - " read contacts once on init - for line in systemlist('khard email --remove-first-line --parsable') - let [address, name, type] = split(line, "\t") - call add(s:contacts, printf('%s <%s>', name, address)) - endfor - +function! coc#source#khard#init() " plugin config return { \ 'priority': 9, @@ -16,13 +7,33 @@ function! coc#source#khard#init() abort \} endfunction -function coc#source#khard#should_complete(option) +function! coc#source#khard#should_complete(option) " only complete addresses on to, cc, bcc and reply-to header lines return tolower(a:option['line']) =~ '^\(to\|cc\|bcc\|reply-to\):' endfunction -function! coc#source#khard#complete(option, cb) abort - " init already loaded all the contacts, just forward them to CoC - call a:cb(s:contacts) +" global contacts list +let s:contacts = v:null +function! s:get_contacts() + if s:contacts isnot v:null + return s:contacts + endif + + let s:contacts = [] + let lines = systemlist('khard email --remove-first-line --parsable') + if v:shell_error != 0 + echoerr 'khard command failed with exit code ' . v:shell_error + return s:contacts + endif + for line in lines + let [address, name, type] = split(line, "\t") + call add(s:contacts, printf('%s <%s>', name, address)) + endfor + + return s:contacts +endfunction + +function! coc#source#khard#complete(option, cb) + call a:cb(s:get_contacts()) endfunction |