Skip to content

Instantly share code, notes, and snippets.

@justmytwospence
Forked from mhoran/mailcomplete.vim
Last active August 29, 2015 14:09
Show Gist options
  • Save justmytwospence/3a07fa83c30f943b0ff9 to your computer and use it in GitHub Desktop.
Save justmytwospence/3a07fa83c30f943b0ff9 to your computer and use it in GitHub Desktop.
" http://recursivedream.com/blog/2012/auto-completing-google-contacts-in-vim/
" Escape query and handle goobook errors
function! mailcomplete#Complete(findstart, base)
if a:findstart == 1
let line = getline('.')
let idx = col('.')
while idx > 0
let idx -= 1
let c = line[idx]
" break on header and previous email
if c == ':' || c == '>'
return idx + 2
else
continue
endif
endwhile
return idx
else
if exists("g:goobookrc")
let goobook="goobook -c " . g:goobookrc
else
let goobook="goobook"
endif
let res=system(goobook . ' query ' . shellescape(a:base))
if v:shell_error
return []
else
"return split(system(trim . '|' . fmt, res), '\n')
return mailcomplete#Format(mailcomplete#Trim(res))
endif
endif
endfunc
function! mailcomplete#Trim(res)
" next up: port this to vimscript!
let trim="sed '/^$/d' | grep -v '(group)$' | cut -f1,2"
return split(system(trim, a:res), '\n')
endfunc
function! mailcomplete#Format(contacts)
"let fmt='awk ''BEGIN{FS="\t"}{printf "%s <%s>\n", $2, $1}'''
let contacts=map(copy(a:contacts), "split(v:val, '\t')")
let ret=[]
for [email, name] in contacts
call add(ret, printf("%s <%s>", name, email))
endfor
return ret
endfunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment