#!/bin/sh
#
# $Id: vimspell.sh,v 1.2 2002/12/26 16:28:27 agx Exp $
#
# Spell a file & generate the syntax statements necessary to
# highlight in vim.  Based on a program from Krishna Gadepalli
# <krishna@stdavids.picker.com>.
#
# original version from:
# Neil Schemenauer <nascheme@ucalgary.ca>
# March 1999
#
# aspell, multi dictionary support and file type detection:
# Guido Guenther <agx@sigxcpu.org>
#
# I use:
#    noremap <F8> :so `vimspell.sh % english`<CR><CR>
#    noremap <S-F8> :so `vimspell.sh % deutsch`<CR><CR>
#    noremap <F7> :syntax clear SpellErrors<CR>
# in my .vimrc

INFILE=$1
# if you have "tempfile", use the following line
OUTFILE=`tempfile`
LANGUAGE=${2:-english}

case "$1" in
    *.tex)
# far from perfect, doesn't like m"ogen
	MODE='--mode=tex'
	;;
    /tmp/mutt-*)
# avoids checking other peoples quotes
    	MODE='--mode=email'
	;;
esac

cat $INFILE | aspell $MODE -d $LANGUAGE pipe | sort -u |
awk '
$1 ~ /\&/ {
	printf "syntax match SpellErrors \"\\<%s\\>\"\n", $2 ;
          }
$1 ~ /\#/ {
	printf "syntax match SpellErrors \"\\<%s\\>\"\n", $2 ;
          }
END   {
	printf "highlight link SpellErrors ErrorMsg\n\n" ;
      }
' > $OUTFILE
echo "!rm $OUTFILE" >> $OUTFILE
echo $OUTFILE
