First working version

This commit is contained in:
Trey Blancher 2023-04-25 11:47:34 -04:00
parent 0c2837ead1
commit 54eee2c62b
1 changed files with 53 additions and 20 deletions

View File

@ -29,29 +29,38 @@ endfunction
function! s:get_section()
let cur = getline('.')
if cur !~ '^\w\+:'
return getline(search('^\w\+:', 'b'))
return getline(search('^\w\+:', 'bn'))
else
return getline('.')
endif
endfunction
function! s:search_mutt_aliases(query)
let aliases = {}
if filereadable(expand('~/.mutt/aliases'))
for myline in readfile(expand('~/.mutt/aliases'))
if myline =~ '^alias'
let mylist = split(myline)
let myname = mylist[1]
let l:aliases = {}
let l:aliases_file = '~/.mutt/aliases'
if exists('g:mutt_aliases')
let l:aliases_file = g:mutt_aliases
endif
"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
let addresses = join(mylist[2:])
let aliases[l:myname] = l:addresses
"echomsg 'Found matching alias: ' . l:myname
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
else
continue
endif
endfor
endif
return aliases
return l:aliases
endfunction
function! s:query_goobook(query)
@ -67,22 +76,37 @@ function! s:query_goobook(query)
endfunction
function! s:address_map(name, address)
return {
\ 'menu': '[muttgoobook]',
\ 'abbr': a:name,
\ 'word': a: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 {
\ 'menu': '[muttgoobook]',
\ 'abbr': a:name,
\ 'word': a:address
\}
endif
endfunction
function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
"echomsg 'Calling completor...'
let l:matches = {}
"let l:cur = getpos('.')
if !s:is_address_field(s:get_section())
"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
endif
" restore position
"call setpos(l:cur)
"echomsg "a:ctx['col'] = " . a:ctx['col']
"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']
"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:startcol = l:col - l:kwlen
@ -102,7 +135,7 @@ function! asyncomplete#sources#muttgoobook#completor(opt, ctx) abort
"echomsg "a:ctx: " . a:ctx
"echomsg "l:startcol: " . l:startcol
"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
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))
"for [k, v] in items(l:address_dict)
" "echomsg "Key: " . k
" "echomsg "Value: " . v
" echomsg "Key: " . k
" echomsg "Value: " . v
"endfor
"echomsg "Getting matches..."