aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/keep.bash
blob: 6829db9c6ffa7fc88580a67428fe0c9aa4d8eafa (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
# Complete calls to keep() with variables and functions, or if -d is given with
# stuff that's already kept
_keep() {

    # Determine what we're doing based on first completion word
    local mode
    mode=keep
    if ((COMP_CWORD > 1)) ; then
        case ${COMP_WORDS[1]} in
            -h) return 1 ;;
            -d) mode=delete ;;
        esac
    fi

    # Collect words from an appropriate type of completion
    local word
    while read -r word ; do
        [[ -n $word ]] || continue
        COMPREPLY[${#COMPREPLY[@]}]=$word
    done < <(

        # Switch on second word; is it a -d option?
        case $mode in

            # Keepable names: all functions and variables
            (keep)
                compgen -A function -A variable \
                    -- "$2"
                ;;

            # Kept names: .bash-suffixed names in keep dir
            (delete)
                # Make globs behave correctly
                shopt -s nullglob
                while read -r _ setting ; do
                    case $setting in
                        ('completion-ignore-case on')
                            shopt -s nocaseglob
                            break
                            ;;
                    esac
                done < <(bind -v)

                # Build list of kept names
                dir=${BASHKEEP:-"$HOME"/.bashkeep.d}
                cword=$2
                kept=("$dir"/"$cword"*.bash)
                kept=("${kept[@]##*/}")
                kept=("${kept[@]%.bash}")

                # Print kept names
                printf '%s\n' "${kept[@]}"
                ;;
        esac
    )
}
complete -F _keep keep