Last active
December 1, 2022 01:10
-
-
Save mhoran/1667695 to your computer and use it in GitHub Desktop.
goobook omnicomplete with awk ported to vimscript!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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