aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/path.bash
blob: 8db6a74ac20293cc42e30fb55337465ae09ee101 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Completion for path
_path() {

    # What to do depends on which word we're completing
    if ((COMP_CWORD == 1)) ; then

        # Complete operation as first word
        local cmd
        while read -r cmd ; do
            COMPREPLY[${#COMPREPLY[@]}]=$cmd
        done < <(compgen -W '
            append
            check
            help
            insert
            list
            pop
            remove
            shift
        ' -- "$2")

    # Complete with either directories or $PATH entries as all other words
    else
        case ${COMP_WORDS[1]} in

            # Complete with a directory
            insert|append|check)
                local dirname
                while IFS= read -rd '' dirname ; do
                    [[ -n $dirname ]] || continue
                    COMPREPLY[${#COMPREPLY[@]}]=$dirname
                done < <(

                    # Set options to glob correctly
                    shopt -s dotglob nullglob

                    # Make globbing case-insensitive if appropriate
                    while read -r _ setting ; do
                        case $setting in
                            ('completion-ignore-case on')
                                shopt -s nocaseglob
                                break
                                ;;
                        esac
                    done < <(bind -v)

                    # Collect directory names, strip trailing slash
                    local -a dirnames
                    dirnames=("$2"*/)
                    dirnames=("${dirnames[@]%/}")

                    # Print quoted entries, null-delimited
                    printf '%q\0' "${dirnames[@]}"
                )
                ;;

            # Complete with directories from PATH
            remove)
                local -a promptarr
                IFS=: read -rd '' -a promptarr < \
                    <(printf '%s\0' "$PATH")
                local part
                for part in "${promptarr[@]}" ; do
                    case $part in
                        "$2"*)
                            COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q' "$part")
                            ;;
                    esac
                done
                ;;

            # No completion
            *)
                return 1
                ;;
        esac
    fi
}
complete -F _path path