2015-12-17 05:07:09 +01:00
|
|
|
decl -hidden range-faces spell_regions
|
|
|
|
decl -hidden str spell_tmp_file
|
|
|
|
|
2016-10-11 09:03:41 +02:00
|
|
|
def -params ..1 \
|
|
|
|
-docstring %{spell [<language>]: spell check the current buffer
|
|
|
|
The first optional argument is the language against which the check will be performed
|
|
|
|
Formats of language supported:
|
|
|
|
- ISO language code, e.g. 'en'
|
|
|
|
- language code above followed by a dash or underscore with an ISO country code, e.g. 'en-US'} \
|
|
|
|
spell %{
|
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-10-18 14:14:37 +02:00
|
|
|
rm -r "$(dirname "$kak_opt_spell_tmp_file")"
|
2016-01-24 10:05:21 +01:00
|
|
|
exit 1
|
|
|
|
else
|
2016-10-18 14:14:37 +02:00
|
|
|
options="-l '$1'"
|
2016-01-24 10:05:21 +01:00
|
|
|
fi
|
|
|
|
fi
|
2016-10-18 14:14:37 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
sed 's/^/^/' "$kak_opt_spell_tmp_file" | eval "aspell -a $options" 2>&1 | {
|
|
|
|
line_num=1
|
|
|
|
regions=$kak_timestamp
|
|
|
|
read line # drop the identification message
|
|
|
|
while read -r line; do
|
|
|
|
case "$line" in
|
|
|
|
[\#\&]*)
|
|
|
|
if expr "$line" : '^&' >/dev/null; then
|
2016-10-26 23:47:24 +02:00
|
|
|
pos=$(printf %s\\n "$line" | cut -d ' ' -f 4 | sed 's/:$//')
|
2016-10-18 14:14:37 +02:00
|
|
|
else
|
2016-10-26 23:47:24 +02:00
|
|
|
pos=$(printf %s\\n "$line" | cut -d ' ' -f 3)
|
2016-10-18 14:14:37 +02:00
|
|
|
fi
|
|
|
|
word=$(printf %s\\n "$line" | cut -d ' ' -f 2)
|
2016-10-26 23:47:24 +02:00
|
|
|
regions="$regions:$line_num.$pos+${#word}|Error"
|
2016-10-18 14:14:37 +02:00
|
|
|
;;
|
|
|
|
'') line_num=$((line_num + 1));;
|
|
|
|
\*) ;;
|
|
|
|
*) printf 'echo -color Error %%{%s}\n' "${line}" | kak -p "${kak_session}";;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
printf 'set "buffer=%s" spell_regions %%{%s}' "${kak_bufname}" "${regions}" \
|
|
|
|
| kak -p "${kak_session}"
|
|
|
|
}
|
|
|
|
rm -r $(dirname "$kak_opt_spell_tmp_file")
|
|
|
|
} </dev/null >/dev/null 2>&1 &
|
2015-12-17 05:07:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-20 13:31:06 +02:00
|
|
|
|
2016-10-20 14:12:05 +02:00
|
|
|
def spell-replace %{%sh{
|
2016-10-20 13:31:06 +02:00
|
|
|
suggestions=$(echo "$kak_selection" | aspell -a | grep '^&' | cut -d: -f2)
|
|
|
|
menu=$(echo "${suggestions#?}" | awk -F', ' '
|
|
|
|
{
|
|
|
|
for (i=1; i<=NF; i++)
|
|
|
|
printf "%s", "%{"$i"}" "%{exec -itersel c"$i"<esc>be}"
|
|
|
|
}
|
|
|
|
')
|
|
|
|
printf '%s\n' "try %{ menu -auto-single $menu }"
|
|
|
|
}}
|