Skip to content

Instantly share code, notes, and snippets.

@arenevier
Created August 12, 2011 14:12
Show Gist options
  • Save arenevier/1142114 to your computer and use it in GitHub Desktop.
Save arenevier/1142114 to your computer and use it in GitHub Desktop.
autodetect email language to set correct spellang in vim
" strip email headers; otherwise, guess would be biased toward english
" put it in ~/.vim/ftplugin/mail.vim
function s:streapHeaders()
let l:headersend = 0
let l:result = []
for l:line in getline(1, '$')
if len(l:line) == 0
let l:headersend = 1
elseif l:headersend
let l:result = add(l:result, l:line)
endif
endfor
return join(l:result, "\n")
endfunction
function s:guessLang()
let l:content = s:streapHeaders()
if len(l:content) == 0
" no content; default to french
return "fr"
endif
" for each language, get number of misspelled words according to aspell.
" The langue with the least misspelled words is considered the spell
" language
let l:lang = ""
let l:missmin = -1
for l:guess in ["fr", "en"]
let l:misslen = len(split(system("cat | aspell -l " . l:guess . " list | sort -u", l:content)))
if l:misslen == 0
let l:lang = l:guess
break
elseif l:missmin == -1 || l:misslen < l:missmin
let l:missmin = l:misslen
let l:lang = l:guess
endif
endfor
return l:lang
endfunction
if executable('aspell')
exe "set spell spelllang=" . s:guessLang()
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment