" Define `Ispell' language and personal dictionary, used in several places below let IspellLang = 'russian' let PersonalDict = '~/.ispell_' . IspellLang " Try to avoid misspelling words in the first place -- have the insert mode " +N/+P keys perform completion on partially-typed words by " checking the Linux word list and the personal `Ispell' dictionary; sort out " case sensibly (so that words at starts of sentences can still be completed " with words that are in the dictionary all in lower case): execute 'set dictionary+=' . '~/.ispell_english' execute 'set dictionary+=' . PersonalDict set dictionary+=/usr/dict/words " Spell checking operations are defined next. They are all set to normal mode " keystrokes beginning \s but function keys are also mapped to the most common " ones. The functions referred to are defined at the end of this .vimrc. " \se ("spelling english") saves the current file then spell checks it " interactively through `Ispell' and reloads the corrected version: nmap \se :call RecodeAndSpellcheck("") " \sr ("spelling russian") converts the file to the locale encoding, " saves it, then spell checks it interactively through `Ispell', " reloads the corrected version and converts it back to the original encoding: nmap \sr :call RecodeAndSpellcheck(IspellLang) " \sl ("spelling list") lists all spelling mistakes in the current buffer, " but excludes any in news/mail headers or in ("> ") quoted text: execute 'nmap \sl :up! grep -v "^>" grep -E -v "^[[:alpha:]-]+: " ' . \ ' ispell -l -d ' . IspellLang . ' sort uniq' " \sh ("spelling highlight") highlights (in red) all misspelt words in the " current buffer, and also excluding the possessive forms of any valid words " (e.g. "Lizzy's" won't be highlighted if "Lizzy" is in the dictionary); with " mail and news messages it ignores headers and quoted text; for HTML it " ignores tags and only checks words that will appear, and turns off other " syntax highlighting to make the errors more apparent [function at end of " file]: nmap \sh :call HighlightSpellingErrors() " \sc ("spelling clear") clears all highlighted misspellings; for HTML it " restores regular syntax highlighting: nmap \sc :if &ft == 'html' sy on \ else :sy clear SpellError endif " \sa ("spelling add") adds the word at the cursor position to the personal " dictionary (but for possessives adds the base word, so that when the cursor " is on "Ceri's" only "Ceri" gets added to the dictionary), and stops " highlighting that word as an error (if appropriate) [function at end of " file]: nmap \sa :call AddWordToDictionary() " Functions referred to above function! HighlightSpellingErrors() " highlights spelling errors in the current window; used for the \sh operation " defined above; " requires the ispell, sort, and uniq commands to be in the path; " requires the global variable IspellLang to be defined above, and to contain " the preferred `Ispell' language; " for mail/news messages, requires the grep command to be in the path; " for HTML documents, saves the file to disk and requires the lynx command to " be in the path " " by Smylers http://www.stripey.com/vim/ " (inspired by Krishna Gadepalli and Neil Schemenauer's vimspell.sh) " " 2000 Jun 1: for `Vim' 5.6 " for HTML files, remove all current syntax highlighting (so that " misspellings show up clearly), and note it's HTML for future reference: if &filetype == 'html' let HTML = 1 syntax clear " for everything else, simply remove any previously-identified spelling " errors (and corrections): else let HTML = 0 if hlexists('SpellError') syntax clear SpellError endif if hlexists('Normal') syntax clear Normal endif endif " form a command that has the text to be checked piping through standard " output; for HTML files this involves saving the current file and processing " it with `Lynx'; for everything else, use all the buffer except quoted text " and mail/news headers: update if HTML let PipeCmd = '! lynx --raw --dump --nolist % |' else let PipeCmd = '!' if &filetype == 'mail' let PipeCmd = PipeCmd . ' grep -v "^> " | grep -E -v "^[[:alpha:]-]+:" |' endif endif " execute that command, then generate a unique list of misspelt words and " store it in a temporary file: let ErrorsFile = tempname() execute PipeCmd . ' ispell -l -d '. g:IspellLang . \ ' | sort | uniq > ' . ErrorsFile " open that list of words in another window: execute 'split ' . ErrorsFile " for every word in that list ending with "'s", check if the root form " without the "'s" is in the dictionary, and if so remove the word from the " list: global /'s$/ execute 'read ! echo ' . expand('') . \ ' | ispell -l -d ' . g:IspellLang | delete " (If the root form is in the dictionary, ispell -l will have no output so " nothing will be read in, the cursor will remain in the same place and the " :delete will delete the word from the list. If the root form is not in the " dictionary, then ispell -l will output it and it will be read on to a new " line; the delete command will then remove that misspelt root form, leaving " the original possessive form in the list!) " only do anything if there are some misspellings: if strlen(getline('.')) > 0 " if (previously noted as) HTML, replace each non-alphanum char with a " regexp that matches either that char or a &...; entity: if HTML % substitute /\W/\\(&\\|\&\\(#\\d\\{2,4}\\|\w\\{2,8}\\);\\)/e endif " turn each mistake into a `Vim' command to place it in the SpellError " syntax highlighting group: % substitute /^/syntax match SpellError !\\!/ endif " save and close that file (so switch back to the one being checked): exit " make syntax highlighting case-sensitive, then execute all the match " commands that have just been set up in that temporary file, delete it, and " highlight all those words in red: syntax case match execute 'source ' . ErrorsFile call delete(ErrorsFile) highlight SpellError term=reverse ctermfg=DarkRed guifg=Red " with HTML, don't mark any errors in e-mail addresses or URLs, and ignore " anything marked in a fix-width font (as being computer code): if HTML syntax case ignore syntax match Normal !\<[[:alnum:]._-]\+@[[:alnum:]._-]\+\.\a\+\>! syntax match Normal \ !\<\(ht\|f\)tp://[-[:alnum:].]\+\a\(/[-_.[:alnum:]/#&=,]*\)\=\>! syntax region Normal start=!
! end=!
! syntax region Normal start=!! end=!! syntax region Normal start=!! end=!! endif endfunction " HighlightSpellingErrors() function! AddWordToDictionary() " adds the word under the cursor to the personal dictionary; used for the \sa " operation defined above; " requires the global variable PersonalDict to be defined above, and to contain " the `Ispell' personal dictionary; " " by Smylers http://www.stripey.com/vim/ " " 2000 Apr 30: for `Vim' 5.6 " get the word under the cursor, including the apostrophe as a word character " to allow for words like "won't", but then ignoring any apostrophes at the " start or end of the word: setlocal iskeyword+=' let Word = substitute(expand(''), "^'\\+", '', '') let Word = substitute(Word, "'\\+$", '', '') setlocal iskeyword-=' " override any SpellError highlighting that might exist for this word, " `highlighting' it as normal text: execute 'syntax match Normal #\<' . Word . '\>#' " remove any final "'s" so that possessive forms don't end up in the " dictionary, then add the word to the dictionary: let Word = substitute(Word, "'s$", '', '') execute '!echo "' . Word . '" >> ' . g:PersonalDict endfunction " AddWordToDictionary() function! RecodeAndSpellcheck(language) " If the console encoding differs from the file encoding... if strlen(&encoding) && strlen(&fileencoding) && &encoding != &fileencoding " ...save the file encoding... let FileEncoding = &fileencoding " ...and recode to the console encoding execute 'setlocal fileencoding=' . &encoding else let FileEncoding = '' endif update if strlen(a:language) execute '!ispell -x -d ' . a:language . ' ' . shellescape(expand("%")) else execute '!ispell -x ' . shellescape(expand("%")) endif edit % if strlen(FileEncoding) " Now recode it back to the saved file encoding execute 'setlocal fileencoding=' . FileEncoding update endif endfunction " RecodeAndSpellcheck()