_xremap() {
    local i cur prev opts cmd
    COMPREPLY=()
    if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
        cur="$2"
    else
        cur="${COMP_WORDS[COMP_CWORD]}"
    fi
    prev="$3"
    cmd=""
    opts=""

    for i in "${COMP_WORDS[@]:0:COMP_CWORD}"
    do
        case "${cmd},${i}" in
            ",$1")
                cmd="xremap"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        xremap)
            opts="-h -V --device --ignore --mouse --watch --completions --output-device-name --vendor --product --list-devices --device-details --list-windows --no-window-logging --allow-launch --bridge --help --version [CONFIGS]..."
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi
            case "${prev}" in
                --device)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --ignore)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --watch)
                    COMPREPLY=($(compgen -W "device config" -- "${cur}"))
                    return 0
                    ;;
                --completions)
                    COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
                    return 0
                    ;;
                --output-device-name)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --vendor)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --product)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --allow-launch)
                    COMPREPLY=($(compgen -W "true false" -- "${cur}"))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
            ;;
    esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
    complete -F _xremap -o nosort -o bashdefault -o default xremap
else
    complete -F _xremap -o bashdefault -o default xremap
fi
