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
|
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{
|
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)'"
|
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
|
|
|
|
2021-06-19 03:21:50 +02:00
|
|
|
printf 'eval -no-hooks write %s\n' "${kak_response_fifo}" > $kak_command_fifo
|
|
|
|
|
2016-10-18 14:14:37 +02:00
|
|
|
{
|
2024-03-29 17:27:27 +01:00
|
|
|
trap - INT QUIT
|
2021-06-19 03:21:50 +02:00
|
|
|
sed 's/^/^/' | eval "aspell --byte-offsets -a $options" 2>&1 | awk '
|
2020-03-09 12:22:34 +01:00
|
|
|
BEGIN {
|
|
|
|
line_num = 1
|
|
|
|
regions = ENVIRON["kak_timestamp"]
|
|
|
|
server_command = sprintf("kak -p \"%s\"", ENVIRON["kak_session"])
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if (/^@\(#\)/) {
|
2020-03-22 08:03:50 +01:00
|
|
|
# drop the identification message
|
2020-03-09 12:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (/^\*/) {
|
2020-03-22 08:03:50 +01:00
|
|
|
# nothing
|
2020-03-09 12:22:34 +01:00
|
|
|
}
|
2020-11-06 13:41:12 +01:00
|
|
|
|
2021-07-01 21:52:59 +02:00
|
|
|
else if (/^[+-]/) {
|
2020-11-06 13:41:12 +01:00
|
|
|
# required to ignore undocumented aspell functionality
|
|
|
|
}
|
2020-03-09 12:22:34 +01:00
|
|
|
|
|
|
|
else if (/^$/) {
|
|
|
|
line_num++
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (/^[#&]/) {
|
2020-05-29 04:22:27 +02:00
|
|
|
word_len = length($2)
|
2020-03-09 12:22:34 +01:00
|
|
|
word_pos = substr($0, 1, 1) == "&" ? substr($4, 1, length($4) - 1) : $3;
|
2021-09-07 08:55:01 +02:00
|
|
|
regions = regions " " line_num "." word_pos "+" word_len "|DiagnosticError"
|
2020-03-09 12:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
line = $0
|
|
|
|
gsub(/"/, "&&", line)
|
|
|
|
command = "fail \"" line "\""
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
|
|
|
if (!length(command))
|
|
|
|
command = "set-option \"buffer=" ENVIRON["kak_bufname"] "\" spell_regions " regions
|
|
|
|
|
|
|
|
print command | server_command
|
|
|
|
close(server_command)
|
|
|
|
}
|
|
|
|
'
|
2021-06-19 03:21:50 +02:00
|
|
|
} <$kak_response_fifo >/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##*.}"
|
|
|
|
|
2020-05-15 11:21:02 +02:00
|
|
|
start_first="${kak_opt_spell_regions%%|*}"
|
|
|
|
start_first="${start_first#* }"
|
2017-03-01 14:14:47 +01:00
|
|
|
|
2020-05-15 11:59:30 +02:00
|
|
|
# Make sure properly formatted selection descriptions are in `%opt{spell_regions}`
|
2020-05-15 11:19:29 +02:00
|
|
|
if ! printf %s "${start_first}" | grep -qE '^[0-9]+\.[0-9]+,[0-9]+\.[0-9]+$'; then
|
2017-03-01 14:14:47 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-05-15 17:37:03 +02:00
|
|
|
printf %s "${kak_opt_spell_regions#* }" | awk -v start_first="${start_first}" \
|
|
|
|
-v anchor_line="${anchor_line}" \
|
2020-05-15 11:59:30 +02:00
|
|
|
-v anchor_col="${anchor_col}" '
|
|
|
|
BEGIN {
|
|
|
|
anchor_line = int(anchor_line)
|
|
|
|
anchor_col = int(anchor_col)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
for (i = 1; i <= NF; i++) {
|
|
|
|
sel = $i
|
|
|
|
sub(/\|.+$/, "", sel)
|
|
|
|
|
|
|
|
start_line = sel
|
|
|
|
sub(/\..+$/, "", start_line)
|
|
|
|
start_line = int(start_line)
|
|
|
|
|
|
|
|
start_col = sel
|
|
|
|
sub(/,.+$/, "", start_col)
|
|
|
|
sub(/^.+\./, "", start_col)
|
|
|
|
start_col = int(start_col)
|
|
|
|
|
|
|
|
if (start_line < anchor_line \
|
|
|
|
|| (start_line == anchor_line && start_col <= anchor_col))
|
2017-03-01 14:14:47 +01:00
|
|
|
continue
|
|
|
|
|
2020-05-15 17:37:03 +02:00
|
|
|
target_sel = sel
|
2017-03-01 14:14:47 +01:00
|
|
|
break
|
2020-05-15 11:59:30 +02:00
|
|
|
}
|
2020-05-15 17:37:03 +02:00
|
|
|
}
|
2017-03-01 14:14:47 +01:00
|
|
|
|
2020-05-15 17:37:03 +02:00
|
|
|
END {
|
|
|
|
if (!target_sel)
|
|
|
|
target_sel = start_first
|
2017-03-01 14:14:47 +01:00
|
|
|
|
2020-05-15 17:37:03 +02:00
|
|
|
printf "select %s\n", target_sel
|
2020-05-15 11:59:30 +02:00
|
|
|
}'
|
2017-03-01 14:14:47 +01:00
|
|
|
} }
|
|
|
|
|
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" \
|
2020-09-21 13:07:35 +02:00
|
|
|
spell-replace %{
|
|
|
|
prompt \
|
|
|
|
-shell-script-candidates %{
|
|
|
|
options=""
|
|
|
|
if [ -n "$kak_opt_spell_last_lang" ]; then
|
|
|
|
options="-l '$kak_opt_spell_last_lang'"
|
|
|
|
fi
|
|
|
|
printf %s "$kak_selection" |
|
|
|
|
eval "aspell -a $options" |
|
2023-09-05 08:48:26 +02:00
|
|
|
sed -n -e '/^&/ { s/^[^:]*: //; s/, /\n/g; p;}'
|
2020-09-21 13:07:35 +02:00
|
|
|
} \
|
|
|
|
"Replace with: " \
|
|
|
|
%{
|
|
|
|
evaluate-commands -save-regs a %{
|
|
|
|
set-register a %val{text}
|
|
|
|
execute-keys c <c-r>a <esc>
|
|
|
|
}
|
2017-03-01 14:14:02 +01:00
|
|
|
}
|
2020-09-21 13:07:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 13:51:46 +02:00
|
|
|
|
|
|
|
define-command -params 0.. \
|
|
|
|
-docstring "Add the current selection to the dictionary" \
|
2020-04-27 12:28:29 +02:00
|
|
|
spell-add %{ evaluate-commands %sh{
|
2020-08-23 18:55:50 +02:00
|
|
|
options=""
|
2020-04-15 13:51:46 +02:00
|
|
|
if [ -n "$kak_opt_spell_last_lang" ]; then
|
|
|
|
options="-l '$kak_opt_spell_last_lang'"
|
|
|
|
fi
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
# use selections
|
|
|
|
eval set -- "$kak_quoted_selections"
|
|
|
|
fi
|
|
|
|
while [ $# -gt 0 ]; do
|
2020-04-27 12:28:29 +02:00
|
|
|
word="$1"
|
|
|
|
if ! printf '*%s\n#\n' "${word}" | eval "aspell -a $options" >/dev/null; then
|
|
|
|
printf 'fail "Unable to add word: %s"' "$(printf %s "${word}" | sed 's/"/&&/g')"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-04-15 13:51:46 +02:00
|
|
|
shift
|
|
|
|
done
|
|
|
|
}}
|