First working version

This commit is contained in:
Trey Blancher 2023-04-25 11:47:34 -04:00
parent 0c2837ead1
commit 54eee2c62b

View File

@ -29,29 +29,38 @@ endfunction
function! s:get_section() function! s:get_section()
let cur = getline('.') let cur = getline('.')
if cur !~ '^\w\+:' if cur !~ '^\w\+:'
return getline(search('^\w\+:', 'b')) return getline(search('^\w\+:', 'bn'))
else else
return getline('.') return getline('.')
endif endif
endfunction endfunction
function! s:search_mutt_aliases(query) function! s:search_mutt_aliases(query)
let aliases = {} let l:aliases = {}
if filereadable(expand('~/.mutt/aliases')) let l:aliases_file = '~/.mutt/aliases'
for myline in readfile(expand('~/.mutt/aliases')) if exists('g:mutt_aliases')
if myline =~ '^alias' let l:aliases_file = g:mutt_aliases
let mylist = split(myline) endif
let myname = mylist[1] "echomsg 'l:aliases_file = ' . l:aliases_file
if filereadable(expand(l:aliases_file))
for l:myline in readfile(expand(l:aliases_file))
if l:myline =~ '^alias'
"echomsg "Found an alias..."
let l:mylist = split(l:myline)
let l:myname = l:mylist[1]
if l:myname =~ a:query if l:myname =~ a:query
let addresses = join(mylist[2:]) "echomsg 'Found matching alias: ' . l:myname
let aliases[l:myname] = l:addresses let l:addresses = join(l:mylist[2:])
"echomsg 'l:addresses before final processing: ' . l:addresses
let l:aliases[l:myname] = substitute(l:addresses, '\\"', '', 'g')
"echomsg 'l:addresses after final processing: ' . l:aliases[l:myname]
endif endif
else else
continue continue
endif endif
endfor endfor
endif endif
return aliases return l:aliases
endfunction endfunction
function! s:query_goobook(query) function! s:query_goobook(query)
@ -67,22 +76,37 @@ function! s:query_goobook(query)
endfunction endfunction
function! s:address_map(name, address) function! s:address_map(name, address)
if a:address !~ ','
"echomsg 'Address is not a list of adresses... ' . a:address
return {
\ 'menu': '[muttgoobook]',
\ 'abbr': a:name,
\ 'word': a:name . ' <' . a:address . '>'
\}
else
"echomsg 'Address is a list of adresses... ' . a:address
return { return {
\ 'menu': '[muttgoobook]', \ 'menu': '[muttgoobook]',
\ 'abbr': a:name, \ 'abbr': a:name,
\ 'word': a:address \ 'word': a:address
\} \}
endif
endfunction endfunction
function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
"echomsg 'Calling completor...' "echomsg 'Calling completor...'
let l:matches = {} let l:matches = {}
"let l:cur = getpos('.')
if !s:is_address_field(s:get_section()) if !s:is_address_field(s:get_section())
"echomsg 'Not in an address field. Exiting...' "echomsg 'Not in an address field. Exiting...'
call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1) "call setpos(l:cur)
"call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1)
return return
endif endif
" restore position
"call setpos(l:cur)
"echomsg "a:ctx['col'] = " . a:ctx['col'] "echomsg "a:ctx['col'] = " . a:ctx['col']
"echomsg "a:ctx['typed'] = " . a:ctx['typed'] "echomsg "a:ctx['typed'] = " . a:ctx['typed']
@ -90,7 +114,16 @@ function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
let l:typed = a:ctx['typed'] let l:typed = a:ctx['typed']
"echomsg "Getting keyword..." "echomsg "Getting keyword..."
let l:kw = substitute(l:typed, '\c' . s:get_fields() . '\(.*\)$', '\2', '') let l:fields = s:get_fields()
"echomsg "Fields: " . l:fields
if l:typed =~? l:fields
"echomsg "Matched a field. l:typed = " . l:typed
let l:kw = substitute(l:typed, '\c' . l:fields . '\(.*\)$', '\2', '')
else
"echomsg "Did not match a field. l:typed = " . l:typed
let l:kw = substitute(l:typed, '^\s\+', '', '')
endif
let l:kwlen = len(l:kw) let l:kwlen = len(l:kw)
let l:startcol = l:col - l:kwlen let l:startcol = l:col - l:kwlen
@ -102,7 +135,7 @@ function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
"echomsg "a:ctx: " . a:ctx "echomsg "a:ctx: " . a:ctx
"echomsg "l:startcol: " . l:startcol "echomsg "l:startcol: " . l:startcol
"echomsg "l:matches: " . l:matches "echomsg "l:matches: " . l:matches
call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1) "call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1)
return return
endif endif
@ -110,8 +143,8 @@ function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
let l:address_dict = extend(s:search_mutt_aliases(l:kw), s:query_goobook(l:kw)) let l:address_dict = extend(s:search_mutt_aliases(l:kw), s:query_goobook(l:kw))
"for [k, v] in items(l:address_dict) "for [k, v] in items(l:address_dict)
" "echomsg "Key: " . k " echomsg "Key: " . k
" "echomsg "Value: " . v " echomsg "Value: " . v
"endfor "endfor
"echomsg "Getting matches..." "echomsg "Getting matches..."