1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-25 01:21:09 +01:00
zsh/Functions/Zle/insert-files
2017-06-16 22:28:54 -07:00

42 lines
1,001 B
Text

# insert-files() {
# Autoload this function, run `zle -N <func-name>' and bind <func-name>
# to a key.
# This function allows you type a file pattern, and see the results of the
# expansion at each step. When you hit return, they will be inserted into
# the command line.
emulate -L zsh
setopt nobadpattern
local key str files
files=( *(N:q) )
if (( $#files )); then
zle -R "files: ${str}_" "$files[@]"
else
zle -R "files: ${str}_ (failed)"
fi
read -k key
while [[ '#key' -ne '#\\r' && '#key' -ne '#\\n' &&
'#key' -ne '#\\C-g' ]]; do
if [[ '#key' -eq '#\\C-h' || '#key' -eq '#\\C-?' ]]; then
[[ -n "$str" ]] && str="$str[1,-2]"
else
str="$str$key"
fi
eval "files=( \${~str}*(N:q) )"
if (( $#files )); then
zle -R "files: ${str}_" "$files[@]"
else
zle -R "files: ${str}_ (failed)"
fi
read -k key
done
zle -Rc
if [[ '#key' -ne '#\\C-g' && $#files -gt 0 ]]; then
[[ "$LBUFFER[-1]" = ' ' ]] || files=('' "$files[@]")
LBUFFER="$LBUFFER$files "
fi
# }