1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-17 22:31:12 +01:00
zsh/Completion/Commands/_correct_filename

73 lines
1.9 KiB
Text
Raw Normal View History

1999-08-23 12:07:17 +02:00
#compdef -k complete-word \C-xC
1999-04-15 20:17:36 +02:00
# Function to correct a filename. Can be used as a completion widget,
# or as a function in its own right, in which case it will print the
# corrected filename to standard output.
#
# You can adapt max_approx to the maximum number of mistakes
# which are allowed in total.
1999-04-15 20:21:49 +02:00
#
# If the numeric prefix is greater than 1, the maximum number of errors
# will be set to that.
# Doesn't get right special characters in the filename; should
# strip them (e.g. "foo\ bar" -> "foo bar") and then re-insert them.
1999-04-15 20:17:36 +02:00
emulate -LR zsh
setopt extendedglob
1999-04-15 20:21:49 +02:00
local file="$PREFIX$SUFFIX" trylist tilde etilde testcmd
1999-04-15 20:17:36 +02:00
integer approx max_approx=6
1999-04-15 20:21:49 +02:00
if [[ -z $WIDGET ]]; then
file=$1
local IPREFIX
else
1999-06-27 07:33:04 +02:00
(( ${NUMERIC:-1} > 1 )) && max_approx=$NUMERIC
1999-04-15 20:21:49 +02:00
fi
if [[ $file = \~*/* ]]; then
tilde=${file%%/*}
etilde=${~tilde}
file=${file/#$tilde/$etilde}
fi
if [[ $CURRENT -eq 1 && $file != /* ]]; then
testcmd=1
elif [[ $file = \=* ]]; then
[[ -n $WIDGET ]] && PREFIX="$PREFIX[2,-1]"
IPREFIX="${IPREFIX}="
file="$file[2,-1]"
testcmd=1
fi
1999-04-15 20:17:36 +02:00
1999-04-15 20:21:49 +02:00
# We need the -Q's to avoid the tilde we've put back getting quoted.
if [[ -z $testcmd && -e "$file" ]] ||
{ [[ -n $testcmd ]] && whence "$file" >&/dev/null }; then
1999-04-15 20:17:36 +02:00
if [[ -n $WIDGET ]]; then
1999-04-15 20:24:09 +02:00
compadd -QUf -i "$IPREFIX" -I "$ISUFFIX" "${file/#$etilde/$tilde}"
1999-04-15 20:18:42 +02:00
[[ -n "$compstate[insert]" ]] && compstate[insert]=menu
1999-04-15 20:17:36 +02:00
else
print "$file"
fi
return
fi
for (( approx = 1; approx <= max_approx; approx++ )); do
1999-04-15 20:21:49 +02:00
if [[ -z $testcmd ]]; then
trylist=( (#a$approx)"$file"(N) )
else
trylist=( "${(@)${(@f)$(whence -wm "(#a$approx)$file" 2>/dev/null)}%:*}" )
[[ $file = */* ]] || trylist=(${trylist##*/})
fi
1999-04-15 20:17:36 +02:00
(( $#trylist )) && break
done
(( $#trylist )) || return 1
if [[ -n $WIDGET ]]; then
1999-04-15 20:24:09 +02:00
compadd -QUf -i "$IPREFIX" -I "$ISUFFIX" "${trylist[@]/#$etilde/$tilde}"
1999-04-15 20:18:42 +02:00
[[ -n "$compstate[insert]" ]] && compstate[insert]=menu
1999-04-15 20:17:36 +02:00
else
1999-04-15 20:21:49 +02:00
print "$IPREFIX${^trylist[@]}"
1999-04-15 20:17:36 +02:00
fi