51 lines
2.1 KiB
Text
51 lines
2.1 KiB
Text
# Author: Daniel Fichtinger
|
|
# License: ISC
|
|
|
|
# How it works:
|
|
# the only way to specify a dictionary for ltex-ls is through
|
|
# the LSP configuration. in kak-lsp, this is in toml format and
|
|
# always set as an option from Kakoune.
|
|
#
|
|
# Live-reloading is supported, meaning that if we update the configuration
|
|
# mid-session, ltex-ls will load the new dictionary. However, we have to set
|
|
# the _full_ config each time
|
|
#
|
|
# So we define a command that reads a line-delimited list of words from our dictionary file
|
|
# It uses kak -f to format them into a toml array format s.t.
|
|
# foo\nbar\n becomes
|
|
# ["foo", "bar"]
|
|
#
|
|
# Then we filter the current lsp_servers option, and replace the \[.*\] occurrences
|
|
# _only_ on the dictionary line, to update the dict array to our new list.
|
|
#
|
|
# If we run this command hooked to ltex-ls startup, then our custom dictionary
|
|
# will be loaded. Now we can easily define a command to add a word to the dictionary -- all it has to do is append the word to the end of the dict file, the call ltex-dict-set
|
|
provide-module ltex-dict %~
|
|
|
|
declare-option -docstring %{
|
|
Path to the custom dictionary. Must be a textfile with newline delimited words.
|
|
|
|
Default: %exp{%val{config}/ltex-dict.txt}
|
|
} str ltex_dict_path %exp{%val{config}/ltex-dict.txt}
|
|
define-command -hidden ltex-dict-set %{
|
|
set-option buffer lsp_servers %sh{
|
|
list="$(cat "$kak_opt_ltex_dict_path" | kak -f '<a-o>%<a-J>Zbi\"<esc>a\"<esc>z<a-,>r,x_i[<esc>a]<esc>')"
|
|
echo "$kak_opt_lsp_servers" | sed "/^\s*dictionary =/ s/\[.*\]/$list/g"
|
|
}
|
|
}
|
|
define-command -docstring %{
|
|
Adds the current word word to the dictionary.
|
|
|
|
If the selection size is 1, then the word the cursor is on is added.
|
|
If the selection is larger, its contents are added verbatim.
|
|
} ltex-add %{
|
|
execute-keys -draft %sh{
|
|
if [ "$kak_selection_length" = 1 ]; then
|
|
printf 'bw_<a-|>{ cat; echo; } | tee -a %s<ret>\n' "$kak_opt_ltex_dict_path"
|
|
else
|
|
printf '<a-|>{ cat; echo; } | tee -a %s<ret>\n' "$kak_opt_ltex_dict_path"
|
|
fi
|
|
}
|
|
}
|
|
|
|
~
|