summaryrefslogtreecommitdiff
path: root/autoload/tools/spell.kak
blob: 4bd330577b8b7c23731d8e955eaba9644885017e (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
declare-option -hidden range-specs spell_regions
declare-option -hidden str spell_last_lang

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 %{
    try %{ add-highlighter window/ ranges 'spell_regions' }
    evaluate-commands %sh{
        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)'"
                exit 1
            else
                options="-l '$1'"
                printf 'set-option buffer spell_last_lang %s\n' "$1"
            fi
        }

        if [ $# -ge 1 ]; then
            use_lang "$1"
        elif [ -n "${kak_opt_spell_lang}" ]; then
            use_lang "${kak_opt_spell_lang}"
        fi

        printf 'eval -no-hooks write %s\n' "${kak_response_fifo}" > $kak_command_fifo

        {
            trap - INT QUIT
            sed 's/^/^/' | eval "aspell --byte-offsets -a $options" 2>&1 | awk '
                BEGIN {
                    line_num = 1
                    regions = ENVIRON["kak_timestamp"]
                    server_command = sprintf("kak -p \"%s\"", ENVIRON["kak_session"])
                }

                {
                    if (/^@\(#\)/) {
                        # drop the identification message
                    }

                    else if (/^\*/) {
                        # nothing
                    }

                    else if (/^[+-]/) {
                        # required to ignore undocumented aspell functionality
                    }

                    else if (/^$/) {
                        line_num++
                    }

                    else if (/^[#&]/) {
                        word_len = length($2)
                        word_pos = substr($0, 1, 1) == "&" ? substr($4, 1, length($4) - 1) : $3;
                        regions = regions " " line_num "." word_pos "+" word_len "|DiagnosticError"
                    }

                    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)
                }
            '
        } <$kak_response_fifo >/dev/null 2>&1 &
    }
}

define-command spell-clear %{
    unset-option buffer spell_regions
}

define-command spell-next %{ evaluate-commands %sh{
    anchor_line="${kak_selection_desc%%.*}"
    anchor_col="${kak_selection_desc%%,*}"
    anchor_col="${anchor_col##*.}"

    start_first="${kak_opt_spell_regions%%|*}"
    start_first="${start_first#* }"

    # Make sure properly formatted selection descriptions are in `%opt{spell_regions}`
    if ! printf %s "${start_first}" | grep -qE '^[0-9]+\.[0-9]+,[0-9]+\.[0-9]+$'; then
        exit
    fi

    printf %s "${kak_opt_spell_regions#* }" | awk -v start_first="${start_first}" \
                                                  -v anchor_line="${anchor_line}" \
                                                  -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))
                    continue

                target_sel = sel
                break
            }
        }

        END {
            if (!target_sel)
                target_sel = start_first

            printf "select %s\n", target_sel
        }'
} }

define-command \
    -docstring "Suggest replacement words for the current selection, against the last language used by the spell-check command" \
    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" |
                sed -n -e '/^&/ { s/^[^:]*: //; s/, /\n/g; p;}'
        } \
        "Replace with: " \
        %{
            evaluate-commands -save-regs a %{
                set-register a %val{text}
                execute-keys c <c-r>a <esc>
            }
        }
}


define-command -params 0.. \
    -docstring "Add the current selection to the dictionary" \
    spell-add %{ evaluate-commands %sh{
    options=""
    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
        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
        shift
    done
}}