Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (74 sloc) 2.37 KB
#!bash
# completion for git-edit subcommand
function _git_edit {
# disable default completion for __git_complete
compopt +o bashdefault +o default
# disable default completion for __git_edit_complete
unset __GIT_EDIT_FALLBACK
# don't run if the line hasn't changed since the last completion
if [ -v __GIT_EDIT_LAST -a "$COMP_LINE" = "$__GIT_EDIT_LAST" ]; then
return
fi
# set up a callback to be called after completion has finished
if ! [[ "$PROMPT_COMMAND" =~ __git_edit_reset_prompt ]]; then
[ -n "$PROMPT_COMMAND" ] && PROMPT_COMMAND="$PROMPT_COMMAND;"
PROMPT_COMMAND="$PROMPT_COMMAND __git_edit_reset_prompt"
fi
# find the position of the edit command
local edit_pos=0
local pos
for (( pos = 0; pos < ${#words[@]}; pos++ )); do
local w=${words[pos]}
if [ "$w" = "edit" -o "$w" = "ed" ]; then
edit_pos=$pos
break
fi
done
# pass the previous pattern if a number is given
if [ $cword -gt $((edit_pos + 1)) ] && [[ "$cur" =~ ^[0-9]+$ ]]; then
local choice="$cur"
local cur="$prev"
else
unset choice
fi
COMPREPLY=( "$(
cd "${__GIT_EDIT_WORKDIR:-.}" || exit 1
git-edit --complete "$cur" $choice
)" )
if [ $? -eq 0 ]; then
export __GIT_EDIT_LAST="$COMP_LINE"
fi
}
function __git_edit_reset_prompt {
# clear the last completion input
unset __GIT_EDIT_LAST
}
# helper to add Git completions to a command
function __git_edit_complete {
# first argument is the command that should be completed
local command="$1"
# second argument is the name of the Git completion function, same as for __git_complete
local subcommand="${2:-_git}"
# third argument, if given, will be used as the working directory to get the completions from
local workdir="$3"
local wrapper="__git_edit_wrap_${command}_${subcommand}"
eval "$wrapper () { __git_edit_func_wrap $subcommand \"$workdir\" ; }"
complete -o nospace -F $wrapper $command 2>/dev/null \
|| complete -o nospace -F $wrapper $command
}
function __git_edit_func_wrap {
local cur=${COMP_WORDS[COMP_CWORD]}
__GIT_EDIT_FALLBACK=1
__GIT_EDIT_WORKDIR="$2"
__git_func_wrap $1
if [ -z "$COMPREPLY" -a -n "$__GIT_EDIT_FALLBACK" ]; then
pushd "${__GIT_EDIT_WORKDIR:-.}" >/dev/null
_filedir
popd >/dev/null
fi
unset __GIT_EDIT_WORKDIR __GIT_EDIT_FALLBACK
}
function _ed {
__git_edit_func_wrap _git_edit
}
complete -o nospace -F _ed ed