# override builtin fg, nobody uses this anyway
# use my fg 'find grep' script instead from environment
fg(){
    env fg "$@"
}

# fuzzy finding cd
cd() { 
    case $1 in
        \?)
            if [ -f "${XDG_CACHE_HOME:-${HOME}/.cache}"/last_dir ] ; then
                read -r loc < "${XDG_CACHE_HOME:-${HOME}/.cache}"/last_dir
                [ "$loc" ] && cd "$loc"
            fi
            ;;
        *)
            if [ "$1" ] ; then
                if [ -f "$1" ] ; then
                    # if given a file, go to its directory
                    set -- "${1%/*}"
                fi
                # try any globs of the arg
                builtin cd --  "$1"  ||
                builtin cd --  "$1"* ||
                builtin cd -- *"$1"  ||
                builtin cd -- *"$1"* ||
                for _i in 1 2 3 4 ; do
                    _cddir="$(find . -type d -maxdepth $_i -iname "*$1*" | head -n 1)"
                    if [ "$_cddir" ] ; then
                        builtin cd -- "$_cddir"
                        break
                    fi
                done
                unset _i
            else
                builtin cd -- "${HOME}"
            fi 2>/dev/null

            # set our prompt - see prompt.shellrc for more details
            if [ "$RANGER_LEVEL" ] ; then
                export PS1="[ranger: $(_ps1_print)] "
            else
                export PS1="$(_ps1_print)"
            fi

            # save last location, but only if its not homedir or '.'
            case $1 in
                '.'|''|' ') ;;
                *)
                    printf '%s\n' "$PWD" > \
                        "${XDG_CACHE_HOME:-${HOME}/.cache}"/last_dir
            esac
    esac
}

