summaryrefslogtreecommitdiffstats
path: root/completions/xdg-mime
blob: db97034ddd6ebb1c1304e82f545f3ad8c40d5f0f (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# xdg-mime(1) completion                                   -*- shell-script -*-

_comp_cmd_xdg_mime__mimetype()
{
    local d i
    local -a arr
    for d in /usr/share/mime /usr/local/share/mime; do
        _comp_compgen -v arr -C "$d" -- -f -o plusdirs -X "!*.xml" || continue
        for i in "${!arr[@]}"; do
            case ${arr[i]} in
                packages*) unset -v "arr[i]" ;; # not a MIME type dir
                *.xml) arr[i]=${arr[i]%.xml} ;;
                */*) ;;
                *) arr[i]+=/ ;;
            esac
        done
        ((${#arr[@]})) &&
            COMPREPLY+=("${arr[@]}")
    done
    [[ ${COMPREPLY-} != */ ]] || compopt -o nospace
}

_comp_cmd_xdg_mime()
{
    local cur prev words cword comp_args
    _comp_initialize -- "$@" || return

    local REPLY
    _comp_count_args
    local args=$REPLY
    if ((args == 1)); then
        if [[ $cur == -* ]]; then
            _comp_compgen -- -W '--help --manual --version'
            return
        fi
        _comp_compgen -- -W 'query default install uninstall'
        return
    fi

    case ${words[1]} in
        query)
            if ((args == 2)); then
                _comp_compgen -- -W 'filetype default'
                return
            fi
            ((args == 3)) || return
            case ${words[2]} in
                filetype) _comp_compgen_filedir ;;
                default) _comp_cmd_xdg_mime__mimetype ;;
            esac
            ;;
        default)
            if ((args == 2)); then
                local -a desktops
                if _comp_expand_glob desktops '/usr/share/applications/*.desktop'; then
                    desktops=("${desktops[@]##*/}")
                    _comp_compgen -- -W '"${desktops[@]}"'
                fi
            else
                _comp_cmd_xdg_mime__mimetype
            fi
            ;;
        install)
            if [[ $cur == -* ]]; then
                _comp_compgen -- -W '--mode --novendor'
            elif [[ $prev == --mode ]]; then
                _comp_compgen -- -W 'user system'
            else
                _comp_compgen_filedir xml
            fi
            ;;
        uninstall)
            if [[ $cur == -* ]]; then
                _comp_compgen -- -W '--mode'
            elif [[ $prev == --mode ]]; then
                _comp_compgen -- -W 'user system'
            else
                _comp_compgen_filedir xml
            fi
            ;;
    esac
} &&
    complete -F _comp_cmd_xdg_mime xdg-mime

# ex: filetype=sh