aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/bd.bash
blob: 59f4718fb0ad1c8ae76f28c0640c6ffd297621c8 (plain) (blame)
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
# Completion setup for bd()
_bd() {

    # The function accepts only one argument, so it doesn't make sense to
    # complete anywhere else
    ((COMP_CWORD == 1)) || return

    # Iterate through slash-delimited matching parent path elements, as piped
    # in by the subshell
    local ci word
    while IFS= read -rd/ word ; do
        COMPREPLY[ci++]=$word
    done < <(

        # Build an array of path nodes, leaf to root
        path=$PWD
        while [[ -n $path ]] ; do
            node=${path##*/}
            path=${path%/*}
            [[ -n $node ]] || continue
            nodes[ni++]=$node
        done

        # Continue if we have at least two nodes, counting the leaf
        ((${#nodes} > 1)) || return

        # Shift off the leaf, since it's not meaningful to go "back to" the
        # current directory
        nodes=("${nodes[@]:1}")

        # Turn on case-insensitive matching, if configured to do so
        while read -r _ setting ; do
            case $setting in
                ('completion-ignore-case on')
                    shopt -s nocasematch 2>/dev/null
                    break
                    ;;
            esac
        done < <(bind -v)

        # Iterate through the nodes and print the ones that match the word
        # being completed, with a trailing slash as terminator
        for node in "${nodes[@]}" ; do
            case $node in
                ("$2"*)
                    printf '%s/' "$node"
                    ;;
            esac
        done
    )
}
complete -F _bd bd