summaryrefslogtreecommitdiffstats
path: root/completions/cd
blob: 67dc3de869f9780426aacc026ffc1fd8eb714d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# cd(1) completion                                         -*- shell-script -*-

# This meta-cd function observes the CDPATH variable, so that `cd`
# additionally completes on directories under those specified in CDPATH.
_comp_cmd_cd()
{
    local cur prev words cword comp_args
    _comp_initialize -- "$@" || return

    if [[ $cur == -* ]]; then
        _comp_compgen_help -c help "$1"
        compopt +o nospace
        return
    fi

    local i j k

    compopt -o filenames

    # Use standard dir completion if no CDPATH or parameter starts with /,
    # ./ or ../
    if [[ ! ${CDPATH-} || $cur == ?(.)?(.)/* ]]; then
        _comp_compgen_filedir -d
        return
    fi

    local mark_dirs="" mark_symdirs=""
    _comp_readline_variable_on mark-directories && mark_dirs=set
    _comp_readline_variable_on mark-symlinked-directories && mark_symdirs=set

    # we have a CDPATH, so loop on its contents
    local paths dirs
    _comp_split -F : paths "$CDPATH"
    for i in "${paths[@]}"; do
        # create an array of matched subdirs
        k=${#COMPREPLY[@]}
        _comp_compgen -v dirs -c "$i/$cur" -- -d
        for j in "${dirs[@]}"; do
            if [[ ($mark_symdirs && -L $j || $mark_dirs && ! -L $j) && ! -d ${j#"$i/"} ]]; then
                j+="/"
            fi
            COMPREPLY[k++]=${j#"$i/"}
        done
    done

    _comp_compgen -a filedir -d

    if ((${#COMPREPLY[@]} == 1)); then
        i=${COMPREPLY[0]}
        if [[ $i == "$cur" && $i != "*/" ]]; then
            COMPREPLY[0]="${i}/"
        fi
    fi
}
if shopt -q cdable_vars; then
    complete -v -F _comp_cmd_cd -o nospace cd pushd
else
    complete -F _comp_cmd_cd -o nospace cd pushd
fi