aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/mex.bash
blob: b1e0e1a7ba1cb1d6a439851bce77013e23d55f5b (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
# Load _completion_ignore_case helper function
if ! declare -F _completion_ignore_case >/dev/null ; then
    source "$HOME"/.bash_completion.d/_completion_ignore_case.bash
fi

# Completion setup for mex(1df), completing non-executable files in $PATH
_mex() {

    # Iterate through completions produced by subshell
    local ci comp
    while IFS= read -d / -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

        # Make globs expand appropriately
        shopt -u dotglob
        shopt -s nullglob
        if _completion_ignore_case ; then
            shopt -s nocaseglob
        fi

        # Break $PATH up into an array
        declare -a paths
        IFS=: read -a paths -r \
            < <(printf '%s\n' "$PATH")

        # Iterate through each path, collecting non-executable filenames
        for path in "${paths[@]}" ; do
            for name in "$path"/"$2"* ; do

                # Skip anything that is not a plain file
                [[ -f $name ]] || continue
                # Skip files that are already executable
                ! [[ -x $name ]] || continue

                # Chop off leading path
                name=${name##*/}

                # Skip certain filename patterns
                case $name in
                    # DOS batch file
                    (*.bat) continue ;;
                    # README files
                    (README*) continue ;;
                esac

                # Print name of the file
                printf '%s/' "${name##*/}"

            done
        done
    )
}
complete -F _mex mex