#compdef -P *

# Copyright 2012-2023, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.

# Copy of __expand_tilde_by_ref from bash-completion
__python_argcomplete_expand_tilde_by_ref () {
    if [ "${!1:0:1}" = "~" ]; then
        if [ "${!1}" != "${!1//\/}" ]; then
            eval $1="${!1/%\/*}"/'${!1#*/}';
        else
            eval $1="${!1}";
        fi;
    fi
}

# Run something, muting output or redirecting it to the debug stream
# depending on the value of _ARC_DEBUG.
# If ARGCOMPLETE_USE_TEMPFILES is set, use tempfiles for IPC.
__python_argcomplete_run() {
    if [[ -z "${ARGCOMPLETE_USE_TEMPFILES-}" ]]; then
        __python_argcomplete_run_inner "$@"
        return
    fi
    local tmpfile="$(mktemp)"
    _ARGCOMPLETE_STDOUT_FILENAME="$tmpfile" __python_argcomplete_run_inner "$@"
    local code=$?
    cat "$tmpfile"
    rm "$tmpfile"
    return $code
}

__python_argcomplete_run_inner() {
    if [[ -z "${_ARC_DEBUG-}" ]]; then
        "$@" 8>&1 9>&2 1>/dev/null 2>&1
    else
        "$@" 8>&1 9>&2 1>&9 2>&1
    fi
}

# Scan the beginning of an executable file ($1) for a regexp ($2). By default,
# scan for the magic string indicating that the executable supports the
# argcomplete completion protocol. By default, scan the first kilobyte;
# if $3 is set to -n, scan until the first line break up to a kilobyte.
__python_argcomplete_scan_head() {
    if [[ -n "${ZSH_VERSION-}" ]]; then
        read -s -r -k 1024 -u 0 < "$1"
    else
        read -s -r ${3:--N} 1024 < "$1"
    fi
    [[ "$REPLY" =~ ${2:-PYTHON_ARGCOMPLETE_OK} ]]
}

__python_argcomplete_scan_head_noerr() {
    __python_argcomplete_scan_head "$@" 2>/dev/null
}

__python_argcomplete_which() {
    if [[ -n "${ZSH_VERSION-}" ]]; then
        whence -p "$@"
    else
        type -P "$@"
    fi
}

_python_argcomplete_global() {
    if [[ -z "${ZSH_VERSION-}" ]]; then
        local executable=$1
        __python_argcomplete_expand_tilde_by_ref executable
    else
        # TODO: check if we should call _default or use a different condition here
        if [[ "$service" != "-default-" ]]; then
            return
        fi
        local executable=${words[1]}
    fi

    local ARGCOMPLETE=0
    if [[ "$executable" == python* ]] || [[ "$executable" == pypy* ]]; then
        if [[ "${COMP_WORDS[1]}" == -m ]]; then
            if __python_argcomplete_run "$executable" -m argcomplete._check_module "${COMP_WORDS[2]}"; then
                ARGCOMPLETE=3
            else
                return
            fi
        elif [[ -f "${COMP_WORDS[1]}" ]] && __python_argcomplete_scan_head_noerr "${COMP_WORDS[1]}"; then
            local ARGCOMPLETE=2
        else
            return
        fi
    elif __python_argcomplete_which "$executable" >/dev/null 2>&1; then
        local SCRIPT_NAME=$(__python_argcomplete_which "$executable")
        if (__python_argcomplete_which pyenv && [[ "$SCRIPT_NAME" = $(pyenv root)/shims/* ]]) >/dev/null 2>&1; then
            local SCRIPT_NAME=$(pyenv which "$executable")
        fi
        if __python_argcomplete_scan_head_noerr "$SCRIPT_NAME"; then
            local ARGCOMPLETE=1
        elif __python_argcomplete_scan_head_noerr "$SCRIPT_NAME" '^#!(.*)$' -n && [[ "${BASH_REMATCH[1]}" =~ ^.*(python|pypy)[0-9\.]*$ ]]; then
            local interpreter="$BASH_REMATCH"
            if (__python_argcomplete_scan_head_noerr "$SCRIPT_NAME" "(PBR Generated)|(EASY-INSTALL-(SCRIPT|ENTRY-SCRIPT|DEV-SCRIPT))" \
                && "$interpreter" "$(__python_argcomplete_which python-argcomplete-check-easy-install-script)" "$SCRIPT_NAME") >/dev/null 2>&1; then
                local ARGCOMPLETE=1
            elif __python_argcomplete_run "$interpreter" -m argcomplete._check_console_script "$SCRIPT_NAME"; then
                local ARGCOMPLETE=1
            fi
        fi
    fi

    if [[ $ARGCOMPLETE != 0 ]]; then
        local IFS=$'\013'
        if [[ -n "${ZSH_VERSION-}" ]]; then
            local completions
            completions=($(IFS="$IFS" \
                COMP_LINE="$BUFFER" \
                COMP_POINT="$CURSOR" \
                _ARGCOMPLETE=$ARGCOMPLETE \
                _ARGCOMPLETE_SHELL="zsh" \
                _ARGCOMPLETE_SUPPRESS_SPACE=1 \
                __python_argcomplete_run "$executable" "${words[@]:1:${ARGCOMPLETE}-1}"))
            _describe "$executable" completions -o nosort
        else
            COMPREPLY=($(IFS="$IFS" \
                COMP_LINE="$COMP_LINE" \
                COMP_POINT="$COMP_POINT" \
                COMP_TYPE="$COMP_TYPE" \
                _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \
                _ARGCOMPLETE=$ARGCOMPLETE \
                _ARGCOMPLETE_SHELL="bash" \
                _ARGCOMPLETE_SUPPRESS_SPACE=1 \
                __python_argcomplete_run "$executable" "${COMP_WORDS[@]:1:${ARGCOMPLETE}-1}"))
            if [[ $? != 0 ]]; then
                unset COMPREPLY
            elif [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
                compopt -o nospace
            fi
        fi
    elif [[ -n "${ZSH_VERSION-}" ]]; then
        _default
    else
        type -t _completion_loader | grep -q 'function' && _completion_loader "$@"
    fi
}
if [[ -z "${ZSH_VERSION-}" ]]; then
    complete -o default -o bashdefault -D -F _python_argcomplete_global
else
    compdef _python_argcomplete_global -P '*'
fi
