about summary refs log tree commit diff
path: root/Completion/Core/_approximate
blob: 6c7ae5d77eb10e34c42b9984eafcd9749902cd25 (plain) (blame)
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
#autoload

# This code will try to correct the string on the line based on the
# strings generated for the context. These corrected strings will be
# shown in a list and one can cycle through them as in a menucompletion
# or get the corrected prefix.

# We don't try correction if the string is too short or we have tried it
# already.

[[ _matcher_num -gt 1 || "${#:-$PREFIX$SUFFIX}" -le 1 ]] && return 1

local _comp_correct _correct_expl comax cfgacc
local curcontext="${curcontext}" oldcontext opm="$compstate[pattern_match]"

[[ "$curcontext" != [^:]#:correct:* ]] &&
    curcontext="${curcontext/:[^:]#:/:approximate:}"

oldcontext="$curcontext"

zstyle -s ":completion:${curcontext}:" max-errors cfgacc || cfgacc='2 numeric'

# Get the number of errors to accept.

if [[ "$cfgacc" = *numeric* && ${NUMERIC:-1} -ne 1 ]]; then
  # Stop if we also have a `!'.

  [[ "$cfgacc" = *not-numeric* ]] && return 1

  # Prefer the numeric argument if that has a sensible value.

  comax="${NUMERIC:-1}"
else
  comax="${cfgacc//[^0-9]}"
fi

# If the number of errors to accept is too small, give up.

[[ "$comax" -lt 1 ]] && return 1

_tags corrections original

# Otherwise temporarily define functions to use instead of
# the builtins that add matches. This is used to be able
# to stick the `(#a...)' into the right place (after an
# ignored prefix).

compadd() {
  [[ "$*" != *-([a-zA-Z/]#|)U* &&
     "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return

  if [[ "$PREFIX" = \~*/* ]]; then
    PREFIX="${PREFIX%%/*}/(#a${_comp_correct})${PREFIX#*/}"
  else
    PREFIX="(#a${_comp_correct})$PREFIX"
  fi
  builtin compadd "$_correct_expl[@]" "$@"
}

_comp_correct=1

[[ -z "$compstate[pattern_match]" ]] && compstate[pattern_match]='*'

while [[ _comp_correct -le comax ]]; do
  curcontext="${oldcontext/(#b)([^:]#:[^:]#:)/${match[1][1,-2]}-${_comp_correct}:}"

  _description corrections _correct_expl corrections \
               "e:$_comp_correct" "o:$PREFIX$SUFFIX"

  if _complete; then
    if zstyle -t ":completion:${curcontext}:" insert-unambiguous &&
       [[ "${#compstate[unambiguous]}" -ge "${#:-$PREFIX$SUFFIX}" ]]; then
      compstate[pattern_insert]=unambiguous
    elif _requested original &&
         ( [[ compstate[nmatches] -gt 1 ]] ||
           zstyle -t ":completion:${curcontext}:" original ); then
      local expl

      _description -V original expl original

      builtin compadd "$expl[@]" -U -Q - "$PREFIX$SUFFIX"

      # If you always want to see the list of possible corrections,
      # set `compstate[list]=list force' here.

      [[ "$compstate[list]" != list* ]] &&
          compstate[list]="$compstate[list] force"
    fi
    unfunction compadd
    compstate[pattern_match]="$opm"

    return 0
  fi

  [[ "${#:-$PREFIX$SUFFIX}" -le _comp_correct+1 ]] && break
  (( _comp_correct++ ))
done

unfunction compadd
compstate[pattern_match]="$opm"

return 1