1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-11 22:51:17 +02:00
zsh/Completion/Base/Completer/_prefix
Marlon Richert 6c993144bd 51860: simplify suffix handling in _prefix to remove less accurate hack
This solves the following problems in the _prefix completer:
- The old code had logic for dealing with compstate[unambiguous] that
  was unnecessary. It works fine without it.
- Because of this logic, if a widget set compstate[insert]=1 after
  calling _main_complete, an `x` was left after the completion on the
  command line.
- If the same widget also set `compstate[to_end]=`, then instead, the
  last character of the inserted completion would be treated as an
  autoremovable suffix, with the actual suffix being inserted to the
  line as a normal character.
- After inserting a completion, the cursor would move to the end of the
  entire current word on the command, not the end of word that was
  inserted. This is not what you want with _prefix, since you are trying
  to complete a word _before_ the one on the command line, after which
  you usually want to insert a separator, such as a space or slash,
  before the next word.
2023-06-22 13:13:09 -07:00

62 lines
1.4 KiB
Text

#autoload
# Try to ignore the suffix. A bit like e-o-c-prefix.
[[ _matcher_num -gt 1 || -z "$SUFFIX" ]] && return 1
local comp curcontext="$curcontext" tmp suf="$SUFFIX" \
_completer \
_matcher _c_matcher _matchers _matcher_num
integer ind
if ! zstyle -a ":completion:${curcontext}:" completer comp; then
comp=( "${(@)_completers[1,_completer_num-1]}" )
ind=${comp[(I)_prefix(|:*)]}
(( ind )) && comp=("${(@)comp[ind,-1]}")
fi
if zstyle -t ":completion:${curcontext}:" add-space; then
ISUFFIX=" $SUFFIX"
else
ISUFFIX="$SUFFIX"
fi
SUFFIX=''
local _completer_num=1
for tmp in "$comp[@]"; do
if [[ "$tmp" = *:-* ]]; then
_completer="${${tmp%:*}[2,-1]//_/-}${tmp#*:}"
tmp="${tmp%:*}"
elif [[ $tmp = *:* ]]; then
_completer="${tmp#*:}"
tmp="${tmp%:*}"
else
_completer="${tmp[2,-1]//_/-}"
fi
curcontext="${curcontext/:[^:]#:/:${_completer}:}"
zstyle -a ":completion:${curcontext}:" matcher-list _matchers ||
_matchers=( '' )
_matcher_num=1
_matcher=''
for _c_matcher in "$_matchers[@]"; do
if [[ "$_c_matcher" == +* ]]; then
_matcher="$_matcher $_c_matcher[2,-1]"
else
_matcher="$_c_matcher"
fi
if [[ "$tmp" != _prefix ]] && "$tmp"; then
if [[ -n $compstate[old_list] || ${compstate[unambiguous]%$suf} == $PREFIX ]]; then
compstate[to_end]=match
fi
return 0
fi
(( _matcher_num++ ))
done
(( _completer_num++ ))
done
return 1