When writing an email, my email client allows me use an external text editor. So, I use vim.

I write emails either in English, either in French, and so, I set the spell check language accordingly each time. I was wondering if it was possible to detect it automatically when vim is launched. Clochix came with a hacky but functional solution. The main idea is to call aspell with your email text, once for each possible language, and count the number of misspelled words. The languages which triggers the lowest number of misspelled word is considered the right one. This can obviously lead to bad results if  email has only a few words, especially if most of them are not written correctly. But mostly, that should work.

In Clochix's implementation, we create a wrapper script around vim. It will call aspell, and call vim with arguments to launch it with the correct spellchecker. While this works, I don't like that idea much. If one day, I wish to change that configuration, I'll probably forget to remove that script. Actually, over the years, my binary folder has become cluttered with two lines scripts such as this one. Half of them could probably be removed, but I'm not totally sure I don't need them for some reason. So, I implemented this as a vim script.

When headers are edited, they contain a lot of words such as: Subject, To, etc. Using them would result in a bias towards English. So, I remove them. I just keep the subject content because it has meaning, and because it is the only information we have when starting a new mail (when replying to an email, we have the previous mail content).

This script is github. I hope you'll find it useful