#!/bin/sh -
umask 077

# vimspell - vim utility script to highlight spelling mistakes
# Steve Kinzler, steve@kinzler.com, Aug 04
# https://kinzler.com/me/home.html#other

# rewrite and expansion of the original vimspell script by
# Krishna Gadepalli, krishna@stdavids.picker.com

# noremap <F8> :w<Bar>so `vimspell %`<Bar>!vimspell % -r<CR><CR>

# NOTE: vim 7.0+ has builtin spellchecking, better than this
# NOTE: 2025-10-20 the aspell arguments are broken

in="$1"
base=`basename "$in"`
syn="${TMP-$HOME/tmp}/$base.syntax"

rm -f "$syn"
test "$2" = -r && exit

spell=spell
(aspell) > /dev/null 2>&1 && spell='aspell -l --mode=none'

arg=
ldict="${LOCAL_DICT-$HOME/local/lib/local_dict}"
tdict=/tmp/vsw$$
trepl=/tmp/vsr$$
trap "rm -f $tdict $trepl; exit" 0 1 2 13 15
test -f "$ldict" &&
	case "$spell" in
	*aspell*)	(echo personal_ws-1.1 en 1; cat "$ldict") > $tdict
			(echo personal_repl-1.1 en 0)		  > $trepl
			arg="--repl=$trepl -p $tdict";;
	*)		arg="+$ldict";;
	esac

(echo 'syntax clear'
 $spell $arg < "$in" |
    sed 's/^/syntax match SpellErrors "\\</; s/$/\\>"/'
 echo 'highlight link SpellErrors ErrorMsg') > "$syn"

echo "$syn"
