2015-12-17 05:07:09 +01:00
decl -hidden range-faces spell_regions
decl -hidden str spell_tmp_file
2016-01-24 10:05:21 +01:00
def -params ..1 spell -docstring "Check spelling of the current buffer with aspell (the first optional argument is the language against which the check will be performed)" %{
2015-12-17 05:07:09 +01:00
try %{ addhl ranges 'spell_regions' }
%sh{
file=$(mktemp -d -t kak-spell.XXXXXXXX)/buffer
2016-10-06 14:32:51 +02:00
printf 'eval -no-hooks write %s\n' "${file}"
printf 'set buffer spell_tmp_file %s\n' "${file}"
2015-12-17 05:07:09 +01:00
}
%sh{
2016-01-24 10:05:21 +01:00
if [ $# -ge 1 ]; then
if [ ${#1} -ne 2 -a ${#1} -ne 5 ]; then
2016-04-23 07:47:01 +02:00
echo 'echo -color Error Invalid language code (examples of expected format: en, en_US, en-US)'
2016-01-24 10:05:21 +01:00
rm -r $(dirname $kak_opt_spell_tmp_file)
exit 1
else
options="-l $1"
fi
fi
2016-10-06 14:32:51 +02:00
sed 's/^/^/' < $kak_opt_spell_tmp_file | aspell -a $options 2>&1 | tee /tmp/spell-out | {
2015-12-17 05:07:09 +01:00
line_num=1
regions=$kak_timestamp
2016-10-06 14:32:51 +02:00
read line # drop the identification message
2015-12-17 05:07:09 +01:00
while read line; do
2016-09-24 11:27:35 +02:00
case "$line" in
2016-10-06 14:32:51 +02:00
[\#\&]*)
2016-09-24 11:27:35 +02:00
if expr "$line" : '^&' >/dev/null; then
begin=$(printf %s\\n "$line" | cut -d ' ' -f 4 | sed 's/:$//')
else
begin=$(printf %s\\n "$line" | cut -d ' ' -f 3)
fi
word=$(printf %s\\n "$line" | cut -d ' ' -f 2)
end=$((begin + ${#word}))
regions="$regions:$line_num.$begin,$line_num.$end|Error"
;;
'') line_num=$((line_num + 1));;
2016-10-06 14:32:51 +02:00
\*) ;;
*) printf 'echo -color Error %%{%s}\n' "${line}";;
2015-12-17 05:07:09 +01:00
esac
done
2016-10-06 14:32:51 +02:00
printf 'set buffer spell_regions %%{%s}' "${regions}"
2015-12-17 05:07:09 +01:00
}
rm -r $(dirname $kak_opt_spell_tmp_file)
}
}