Skip to content

Instantly share code, notes, and snippets.

@mhoran
Last active December 1, 2022 01:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhoran/1667695 to your computer and use it in GitHub Desktop.
Save mhoran/1667695 to your computer and use it in GitHub Desktop.
goobook omnicomplete with awk ported to vimscript!
" 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)
"let trim="sed '/^$/d' | grep -v '(group)$' | cut -f1,2"
let ret=[]
for line in split(a:res, '\n')
if line !~ '(group)$'
call add(ret, split(line, '\t')[:1])
endif
endfor
return ret
endfunc
function! mailcomplete#Format(contacts)
"let fmt='awk ''BEGIN{FS="\t"}{printf "%s <%s>\n", $2, $1}'''
let ret=[]
for [email, name] in a: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