This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dictcomplete.kak
73 lines (66 loc) · 2.5 KB
/
dictcomplete.kak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##
## dictcomplete.kak by lenormf
## Dictionary completion based on `aspell` in text-based buffers
##
declare-option -docstring "minimum amount of characters in a word necessary to trigger completion" int dict_min_chars 3
declare-option -docstring "language identifier passed to `aspell` when loading a dictionary" str dict_lang "en"
declare-option -docstring %{
size of the dictionary to load, as per `aspell` conventions:
- 10: tiny
- 20: really small
- 30: small
- 40: med-small
- 50: med
- 60: med-large
- 70: large
- 80: huge
- 90: insane
} \
int dict_size 30
declare-option -hidden completions dict_completions
define-command -hidden -params 1 dict-complete %{ evaluate-commands %sh{
{
candidates=$(
printf %s "${1}" | aspell -a \
-l "${kak_opt_dict_lang:-en}" \
--size "${kak_opt_dict_size:-30}" \
| awk -F ", " \
-v y="${kak_cursor_line}" \
-v x="${kak_cursor_column}" \
-v ts="${kak_timestamp}" '
BEGIN {
prefix = y "." x "@" ts
candidates = ""
}
END {
if (length(candidates))
print prefix candidates
}
/^&/ {
sub(/^&[^:]+: /, "", $0)
for (i = 1; i <= NF; i++) {
gsub(/\|/, "\\|", $i)
gsub("~", "~~", $i)
candidates = candidates " %~" $i "||{value}" $i "~"
}
}
')
if [ -n "${candidates}" ]; then
printf 'set %%{buffer=%s} dict_completions %s' "${kak_buffile}" "${candidates}" | kak -p "${kak_session}"
fi
} >/dev/null 2>&1 </dev/null &
} }
define-command -docstring %{Enable dictionary-completion for the current buffer} \
dict-complete-enable %{
set-option window completers option=dict_completions %opt{completers}
hook -group dict-complete buffer InsertIdle .* %{
try %{
evaluate-commands -draft %{
execute-keys h<a-i><a-w> <a-\;> "<a-k>[^\n]{%opt{dict_min_chars},}<ret>"
dict-complete %val{selection}
}
} catch %{
set-option buffer dict_completions
}
}
}