rc spell: Allow storing a language in an option

This commit adds a `spell_lang` option that will be used by the
`spell` command when no language is passed to it directly.

Setting a buffer/window local option allows using `:spell` seamlessly
across several buffers, written in different languages.
This commit is contained in:
Frank LENORMAND 2019-11-08 12:41:01 +01:00
parent 9d700344f7
commit be8d0e6726

View File

@ -1,14 +1,17 @@
declare-option -hidden range-specs spell_regions declare-option -hidden range-specs spell_regions
declare-option -hidden str spell_lang declare-option -hidden str spell_last_lang
declare-option -hidden str spell_tmp_file declare-option -hidden str spell_tmp_file
define-command -params ..1 \ declare-option -docstring "default language to use when none is passed to the spell-check command" str spell_lang
-docstring %{spell [<language>]: spell check the current buffer
The first optional argument is the language against which the check will be performed define-command -params ..1 -docstring %{
Formats of language supported: spell [<language>]: spell check the current buffer
- ISO language code, e.g. 'en'
- language code above followed by a dash or underscore with an ISO country code, e.g. 'en-US'} \ The first optional argument is the language against which the check will be performed (overrides `spell_lang`)
spell %{ 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 %{
try %{ add-highlighter window/ ranges 'spell_regions' } try %{ add-highlighter window/ ranges 'spell_regions' }
evaluate-commands %sh{ evaluate-commands %sh{
file=$(mktemp -d "${TMPDIR:-/tmp}"/kak-spell.XXXXXXXX)/buffer file=$(mktemp -d "${TMPDIR:-/tmp}"/kak-spell.XXXXXXXX)/buffer
@ -16,15 +19,21 @@ Formats of language supported:
printf 'set-option buffer spell_tmp_file %s\n' "${file}" printf 'set-option buffer spell_tmp_file %s\n' "${file}"
} }
evaluate-commands %sh{ evaluate-commands %sh{
if [ $# -ge 1 ]; then use_lang() {
if [ ${#1} -ne 2 ] && [ ${#1} -ne 5 ]; then if ! printf %s "$1" | grep -qE '^[a-z]{2,3}([_-][A-Z]{2})?$'; then
echo "echo -markup '{Error}Invalid language code (examples of expected format: en, en_US, en-US)'" echo "fail 'Invalid language code (examples of expected format: en, en_US, en-US)'"
rm -rf "$(dirname "$kak_opt_spell_tmp_file")" rm -rf "$(dirname "$kak_opt_spell_tmp_file")"
exit 1 exit 1
else else
options="-l '$1'" options="-l '$1'"
printf 'set-option buffer spell_lang %s\n' "$1" printf 'set-option buffer spell_last_lang %s\n' "$1"
fi fi
}
if [ $# -ge 1 ]; then
use_lang "$1"
elif [ -n "${kak_opt_spell_lang}" ]; then
use_lang "${kak_opt_spell_lang}"
fi fi
{ {
@ -107,9 +116,11 @@ define-command spell-next %{ evaluate-commands %sh{
fi fi
} } } }
define-command spell-replace %{ evaluate-commands %sh{ define-command \
if [ -n "$kak_opt_spell_lang" ]; then -docstring "Suggest replacement words for the current selection, against the last language used by the spell-check command" \
options="-l '$kak_opt_spell_lang'" spell-replace %{ evaluate-commands %sh{
if [ -n "$kak_opt_spell_last_lang" ]; then
options="-l '$kak_opt_spell_last_lang'"
fi fi
suggestions=$(printf %s "$kak_selection" | eval "aspell -a $options" | grep '^&' | cut -d: -f2) suggestions=$(printf %s "$kak_selection" | eval "aspell -a $options" | grep '^&' | cut -d: -f2)
menu=$(printf %s "${suggestions#?}" | awk -F', ' ' menu=$(printf %s "${suggestions#?}" | awk -F', ' '