Skip to content

Instantly share code, notes, and snippets.

@compholio
Created October 27, 2021 16:18
Show Gist options
  • Save compholio/dbef636fb52b72a67b3afa5ed6f34675 to your computer and use it in GitHub Desktop.
Save compholio/dbef636fb52b72a67b3afa5ed6f34675 to your computer and use it in GitHub Desktop.
_filedir_xspec()
{
local cur prev words cword
_init_completion || return
_tilde "$cur" || return
local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
local -a toks
local newest=0
if [[ "$cur" = "?" ]] || [[ "${cur: -2}" = "/?" ]]; then
cur="${cur:0:-1}"
newest=1
fi
toks=( $(
compgen -d -- "$(quote_readline "$cur")" | {
while read -r tmp; do
printf '%s\n' $tmp
done
}
))
# Munge xspec to contain uppercase version too
# http://thread.gmane.org/gmane.comp.shells.bash.bugs/15294/focus=15306
eval xspec="${xspec}"
local matchop=!
if [[ $xspec == !* ]]; then
xspec=${xspec#!}
matchop=@
fi
xspec="$matchop($xspec|${xspec^^})"
toks+=( $(
eval compgen -f -X "'!$xspec'" -- "\$(quote_readline "\$cur")" | {
while read -r tmp; do
[[ -n $tmp ]] && printf '%s\n' $tmp
done
}
))
if [[ ${#toks[@]} -ne 0 ]]; then
compopt -o filenames
if [[ $newest -eq 1 ]]; then
newest=$(bash -c "ls -tr $cur | tail -n 1")
COMPREPLY=( "$cur$newest" )
else
COMPREPLY=( "${toks[@]}" )
fi
fi
} # _filedir_xspec()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment