2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden range-specs spell_regions
|
2019-11-08 12:41:01 +01:00
|
|
|
declare-option -hidden str spell_last_lang
|
2017-11-03 08:34:41 +01:00
|
|
|
declare-option -hidden str spell_tmp_file
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2019-11-08 12:41:01 +01:00
|
|
|
declare-option -docstring "default language to use when none is passed to the spell-check command" str spell_lang
|
|
|
|
|
|
|
|
define-command -params ..1 -docstring %{
|
|
|
|
spell [<language>]: spell check the current buffer
|
|
|
|
|
|
|
|
The first optional argument is the language against which the check will be performed (overrides `spell_lang`)
|
|
|
|
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 %{
|
2018-07-26 10:59:42 +02:00
|
|
|
try %{ add-highlighter window/ ranges 'spell_regions' }
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-06-09 13:05:31 +02:00
|
|
|
file=$(mktemp -d "${TMPDIR:-/tmp}"/kak-spell.XXXXXXXX)/buffer
|
2018-07-04 08:55:05 +02:00
|
|
|
printf 'eval -no-hooks write -sync %s\n' "${file}"
|
2017-11-03 08:34:41 +01:00
|
|
|
printf 'set-option buffer spell_tmp_file %s\n' "${file}"
|
2015-12-17 05:07:09 +01:00
|
|
|
}
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2019-11-08 12:41:01 +01:00
|
|
|
use_lang() {
|
|
|
|
if ! printf %s "$1" | grep -qE '^[a-z]{2,3}([_-][A-Z]{2})?$'; then
|
|
|
|
echo "fail 'Invalid language code (examples of expected format: en, en_US, en-US)'"
|
2017-03-01 14:14:02 +01:00
|
|
|
rm -rf "$(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'"
|
2019-11-08 12:41:01 +01:00
|
|
|
printf 'set-option buffer spell_last_lang %s\n' "$1"
|
2016-01-24 10:05:21 +01:00
|
|
|
fi
|
2019-11-08 12:41:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ $# -ge 1 ]; then
|
|
|
|
use_lang "$1"
|
|
|
|
elif [ -n "${kak_opt_spell_lang}" ]; then
|
|
|
|
use_lang "${kak_opt_spell_lang}"
|
2016-01-24 10:05:21 +01:00
|
|
|
fi
|
2016-10-18 14:14:37 +02:00
|
|
|
|
|
|
|
{
|
2017-01-15 12:22:47 +01:00
|
|
|
sed 's/^/^/' "$kak_opt_spell_tmp_file" | eval "aspell --byte-offsets -a $options" 2>&1 | {
|
2016-10-18 14:14:37 +02:00
|
|
|
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)
|
2019-06-03 01:26:25 +02:00
|
|
|
# trim whitespace to make `wc` output consistent across implementations
|
|
|
|
len=$(($(printf %s "$word" | wc -c)))
|
2018-06-17 05:30:40 +02:00
|
|
|
regions="$regions $line_num.$pos+${len}|Error"
|
2016-10-18 14:14:37 +02:00
|
|
|
;;
|
|
|
|
'') line_num=$((line_num + 1));;
|
|
|
|
\*) ;;
|
2017-07-19 17:18:52 +02:00
|
|
|
*) printf 'echo -markup %%{{Error}%s}\n' "${line}" | kak -p "${kak_session}";;
|
2016-10-18 14:14:37 +02:00
|
|
|
esac
|
|
|
|
done
|
2018-06-17 05:30:40 +02:00
|
|
|
printf 'set-option "buffer=%s" spell_regions %s' "${kak_bufname}" "${regions}" \
|
2016-10-18 14:14:37 +02:00
|
|
|
| kak -p "${kak_session}"
|
|
|
|
}
|
2017-03-01 14:14:02 +01:00
|
|
|
rm -rf $(dirname "$kak_opt_spell_tmp_file")
|
2016-10-18 14:14:37 +02:00
|
|
|
} </dev/null >/dev/null 2>&1 &
|
2015-12-17 05:07:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-20 13:31:06 +02:00
|
|
|
|
2018-09-21 21:30:13 +02:00
|
|
|
define-command spell-clear %{
|
|
|
|
unset-option buffer spell_regions
|
|
|
|
}
|
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
define-command spell-next %{ evaluate-commands %sh{
|
2017-03-01 14:14:47 +01:00
|
|
|
anchor_line="${kak_selection_desc%%.*}"
|
|
|
|
anchor_col="${kak_selection_desc%%,*}"
|
|
|
|
anchor_col="${anchor_col##*.}"
|
|
|
|
|
2018-06-17 05:30:40 +02:00
|
|
|
start_first="${kak_opt_spell_regions#* }"
|
2017-03-01 14:14:47 +01:00
|
|
|
start_first="${start_first%%|*}"
|
2018-07-26 11:16:44 +02:00
|
|
|
start_first="${start_first#\'}"
|
2017-03-01 14:14:47 +01:00
|
|
|
|
|
|
|
find_next_word_desc() {
|
|
|
|
## XXX: the `spell` command adds sorted selection descriptions to the range
|
|
|
|
printf %s\\n "${1}" \
|
2018-06-17 05:30:40 +02:00
|
|
|
| sed -e "s/'//g" -e 's/^[0-9]* //' -e 's/|[^ ]*//g' \
|
|
|
|
| tr ' ' '\n' \
|
|
|
|
| while IFS=, read -r start end; do
|
2017-03-01 14:14:47 +01:00
|
|
|
start_line="${start%.*}"
|
|
|
|
start_col="${start#*.}"
|
|
|
|
end_line="${end%.*}"
|
|
|
|
end_col="${end#*.}"
|
|
|
|
|
|
|
|
if [ "${start_line}" -lt "${anchor_line}" ]; then
|
|
|
|
continue
|
|
|
|
elif [ "${start_line}" -eq "${anchor_line}" ] \
|
|
|
|
&& [ "${start_col}" -le "${anchor_col}" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
printf 'select %s,%s\n' "${start}" "${end}"
|
|
|
|
break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# no selection descriptions are in `spell_regions`
|
|
|
|
if ! expr "${start_first}" : '[0-9][0-9]*\.[0-9][0-9]*,[0-9][0-9]*\.[0-9]' >/dev/null; then
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
next_word_desc=$(find_next_word_desc "${kak_opt_spell_regions}")
|
|
|
|
if [ -n "${next_word_desc}" ]; then
|
|
|
|
printf %s\\n "${next_word_desc}"
|
|
|
|
else
|
|
|
|
printf 'select %s\n' "${start_first}"
|
|
|
|
fi
|
|
|
|
} }
|
|
|
|
|
2019-11-08 12:41:01 +01:00
|
|
|
define-command \
|
|
|
|
-docstring "Suggest replacement words for the current selection, against the last language used by the spell-check command" \
|
|
|
|
spell-replace %{ evaluate-commands %sh{
|
|
|
|
if [ -n "$kak_opt_spell_last_lang" ]; then
|
|
|
|
options="-l '$kak_opt_spell_last_lang'"
|
2017-03-28 09:32:21 +02:00
|
|
|
fi
|
|
|
|
suggestions=$(printf %s "$kak_selection" | eval "aspell -a $options" | grep '^&' | cut -d: -f2)
|
2017-03-01 14:14:02 +01:00
|
|
|
menu=$(printf %s "${suggestions#?}" | awk -F', ' '
|
|
|
|
{
|
|
|
|
for (i=1; i<=NF; i++)
|
2018-06-15 16:37:30 +02:00
|
|
|
printf "%s", "%{"$i"}" "%{execute-keys -itersel %{c"$i"<esc>be}}"
|
2017-03-01 14:14:02 +01:00
|
|
|
}
|
2016-10-20 13:31:06 +02:00
|
|
|
')
|
2017-03-01 14:14:02 +01:00
|
|
|
printf 'try %%{ menu -auto-single %s }' "${menu}"
|
|
|
|
} }
|