kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

spell.kak (6363B)


      1 declare-option -hidden range-specs spell_regions
      2 declare-option -hidden str spell_last_lang
      3 
      4 declare-option -docstring "default language to use when none is passed to the spell-check command" str spell_lang
      5 
      6 define-command -params ..1 -docstring %{
      7     spell [<language>]: spell check the current buffer
      8 
      9     The first optional argument is the language against which the check will be performed (overrides `spell_lang`)
     10     Formats of language supported:
     11       - ISO language code, e.g. 'en'
     12       - language code above followed by a dash or underscore with an ISO country code, e.g. 'en-US'
     13     } spell %{
     14     try %{ add-highlighter window/ ranges 'spell_regions' }
     15     evaluate-commands %sh{
     16         use_lang() {
     17             if ! printf %s "$1" | grep -qE '^[a-z]{2,3}([_-][A-Z]{2})?$'; then
     18                 echo "fail 'Invalid language code (examples of expected format: en, en_US, en-US)'"
     19                 exit 1
     20             else
     21                 options="-l '$1'"
     22                 printf 'set-option buffer spell_last_lang %s\n' "$1"
     23             fi
     24         }
     25 
     26         if [ $# -ge 1 ]; then
     27             use_lang "$1"
     28         elif [ -n "${kak_opt_spell_lang}" ]; then
     29             use_lang "${kak_opt_spell_lang}"
     30         fi
     31 
     32         printf 'eval -no-hooks write %s\n' "${kak_response_fifo}" > $kak_command_fifo
     33 
     34         {
     35             trap - INT QUIT
     36             sed 's/^/^/' | eval "aspell --byte-offsets -a $options" 2>&1 | awk '
     37                 BEGIN {
     38                     line_num = 1
     39                     regions = ENVIRON["kak_timestamp"]
     40                     server_command = sprintf("kak -p \"%s\"", ENVIRON["kak_session"])
     41                 }
     42 
     43                 {
     44                     if (/^@\(#\)/) {
     45                         # drop the identification message
     46                     }
     47 
     48                     else if (/^\*/) {
     49                         # nothing
     50                     }
     51 
     52                     else if (/^[+-]/) {
     53                         # required to ignore undocumented aspell functionality
     54                     }
     55 
     56                     else if (/^$/) {
     57                         line_num++
     58                     }
     59 
     60                     else if (/^[#&]/) {
     61                         word_len = length($2)
     62                         word_pos = substr($0, 1, 1) == "&" ? substr($4, 1, length($4) - 1) : $3;
     63                         regions = regions " " line_num "." word_pos "+" word_len "|DiagnosticError"
     64                     }
     65 
     66                     else {
     67                         line = $0
     68                         gsub(/"/, "&&", line)
     69                         command = "fail \"" line "\""
     70                         exit
     71                     }
     72                 }
     73 
     74                 END {
     75                     if (!length(command))
     76                         command = "set-option \"buffer=" ENVIRON["kak_bufname"] "\" spell_regions " regions
     77 
     78                     print command | server_command
     79                     close(server_command)
     80                 }
     81             '
     82         } <$kak_response_fifo >/dev/null 2>&1 &
     83     }
     84 }
     85 
     86 define-command spell-clear %{
     87     unset-option buffer spell_regions
     88 }
     89 
     90 define-command spell-rel-jump -hidden -params 0..1 %{ evaluate-commands %sh{
     91     spell_first="${kak_opt_spell_regions%%|*}"
     92     spell_first="${spell_first#* }"
     93     spell_last="${kak_opt_spell_regions##* }"
     94     spell_last="${spell_last%|*}"
     95 
     96     # Make sure properly formatted selection descriptions are in `%opt{spell_regions}`
     97     if ! printf %s "${spell_first}" | grep -qE '^[0-9]+\.[0-9]+,[0-9]+\.[0-9]+$'; then
     98         exit
     99     fi
    100 
    101     get_prev=0
    102     if [ "$1" = "-rev" ]; then
    103         get_prev=1
    104     elif [ -n "$1" ]; then
    105         echo "fail -- Unrecognised parameter $(kakquote "$1")"
    106     fi
    107 
    108     printf %s "${kak_opt_spell_regions#* }" | \
    109         awk -v spell_first="${spell_first}" -v spell_last="${spell_last}" \
    110             -v get_prev="${get_prev}" -F '[.,|]' -v RS=" " '
    111             BEGIN {
    112                 split(ENVIRON["kak_selection_desc"], sel)
    113                 cursor_row = sel[3]
    114                 cursor_col = sel[4]
    115                 # reverse order so cursor can be at beginning for spell-prev
    116                 split(spell_last, tmp)
    117                 spell_last = tmp[3] "." tmp[4] "," tmp[1] "." tmp[2]
    118             }
    119 
    120             {
    121                 # $1 - $4 is candidate row.col,row.col
    122                 check_row = $1 > cursor_row
    123                 check_col = $1 == cursor_row && (get_prev ? $2 >= cursor_col : $2 > cursor_col)
    124 
    125                 if (check_row || check_col) {
    126                     next_spell = $1 "." $2 "," $3 "." $4
    127                     exit
    128                 }
    129                 prev_spell = $3 "." $4 "," $1 "." $2
    130             }
    131 
    132             END {
    133                 target_spell = get_prev ? prev_spell : next_spell
    134                 if (!target_spell)
    135                     target_spell = get_prev ? spell_last : spell_first
    136 
    137                 printf "select %s\n", target_spell
    138             }
    139         '
    140 } }
    141 
    142 define-command spell-next %{ spell-rel-jump }
    143 
    144 define-command spell-prev %{ spell-rel-jump -rev }
    145 
    146 define-command \
    147     -docstring "Suggest replacement words for the current selection, against the last language used by the spell-check command" \
    148     spell-replace %{
    149     prompt \
    150         -shell-script-candidates %{
    151             options=""
    152             if [ -n "$kak_opt_spell_last_lang" ]; then
    153                 options="-l '$kak_opt_spell_last_lang'"
    154             fi
    155             printf %s "$kak_selection" |
    156                 eval "aspell -a $options" |
    157                 sed -n -e '/^&/ { s/^[^:]*: //; s/, /\n/g; p;}'
    158         } \
    159         "Replace with: " \
    160         %{
    161             evaluate-commands -save-regs a %{
    162                 set-register a %val{text}
    163                 execute-keys c <c-r>a <esc>
    164             }
    165         }
    166 }
    167 
    168 
    169 define-command -params 0.. \
    170     -docstring "Add the current selection to the dictionary" \
    171     spell-add %{ evaluate-commands %sh{
    172     options=""
    173     if [ -n "$kak_opt_spell_last_lang" ]; then
    174         options="-l '$kak_opt_spell_last_lang'"
    175     fi
    176     if [ $# -eq 0 ]; then
    177         # use selections
    178         eval set -- "$kak_quoted_selections"
    179     fi
    180     while [ $# -gt 0 ]; do
    181         word="$1"
    182         if ! printf '*%s\n#\n' "${word}" | eval "aspell -a $options" >/dev/null; then
    183            printf 'fail "Unable to add word: %s"' "$(printf %s "${word}" | sed 's/"/&&/g')"
    184            exit 1
    185         fi
    186         shift
    187     done
    188 }}