summaryrefslogtreecommitdiff
path: root/completions/mutt
blob: 70699a154203b86ba1d0dacf3459909d89e03f6e (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# mutt completion                                          -*- shell-script -*-
#
# Mutt doesn't have an "addressbook" like Pine, but it has aliases and
# a "query" function to retrieve addresses, so that's what we use here.

# @param $1 (cur) Current word to complete
_muttaddr()
{
    _muttaliases "$1"
    _muttquery "$1"

    COMPREPLY+=( $( compgen -u -- "$1" ) )

    return 0
}


# Find muttrc to use
# @output  muttrc filename
_muttrc()
{
    # Search COMP_WORDS for '-F muttrc' or '-Fmuttrc' argument
    set -- "${words[@]}"
    while [ $# -gt 0 ]; do
        if [ "${1:0:2}" = -F ]; then
            if [ ${#1} -gt 2 ]; then
                muttrc="$(dequote "${1:2}")"
            else
                shift
                [ "$1" ] && muttrc="$(dequote "$1")"
            fi
            break
        fi
        shift
    done

    if [ -z "$muttrc" ]; then
        if [ -f ~/.${muttcmd}rc ]; then
            muttrc="~/.${muttcmd}rc"
        elif [ -f ~/.${muttcmd}/${muttcmd}rc ]; then
            muttrc="~/.${muttcmd}/${muttcmd}rc"
        fi
    fi
    printf "%s" "$muttrc"
}


# Recursively build list of sourced config files
# @param $1  List of config files found so far
# @param $2  Config file to process
# @output  List of config files
_muttconffiles()
{
    local file sofar
    local -a newconffiles

    sofar=" $1 "
    shift
    while [[ "$1" ]]; do
        newconffiles=( $(sed -n 's|^source[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' $(eval echo $1) ) )
        for file in "${newconffiles[@]}"; do
            __expand_tilde_by_ref file
            [[ ! -f "$file" || "${sofar/ ${file} / }" != "$sofar" ]] &&
                continue
            sofar+=" $file"
            sofar=" $(eval _muttconffiles \"$sofar\" $file) "
        done
        shift
    done
    printf '%s\n' $sofar
}


# @param $1 (cur) Current word to complete
_muttaliases()
{
    local cur=$1 muttrc muttcmd=${words[0]}
    local -a conffiles aliases

    muttrc=$(_muttrc)
    [ -z "$muttrc" ] && return 0

    conffiles=( $(eval _muttconffiles $muttrc $muttrc) )
    aliases=( $( sed -n 's|^alias[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' \
        $(eval echo "${conffiles[@]}") ) )
    COMPREPLY+=( $( compgen -W "${aliases[*]}" -- "$cur" ) )

    return 0
}


# @param $1 (cur) Current word to complete
_muttquery()
{
    local cur=$1 querycmd muttcmd=${words[0]}
    local -a queryresults

    querycmd="$( $muttcmd -Q query_command 2>/dev/null | sed -e 's|^query_command=\"\(.*\)\"$|\1|' -e 's|%s|'$cur'|' )"
    if [[ -z "$cur" || -z "$querycmd" ]]; then
        queryresults=()
    else
        __expand_tilde_by_ref querycmd
        queryresults=( $( $querycmd | \
            sed -n '2,$s|^\([^[:space:]]\{1,\}\).*|\1|p' ) )
    fi

    COMPREPLY+=( $( compgen -W "${queryresults[*]}" -- "$cur" ) )

    return 0
}


# @param $1 (cur) Current word to complete
_muttfiledir()
{
    local cur=$1 folder muttrc spoolfile muttcmd=${words[0]}

    muttrc=$(_muttrc)
    if [[ $cur == [=+]* ]]; then
        folder="$( $muttcmd -F "$muttrc" -Q folder 2>/dev/null | sed -e 's|^folder=\"\(.*\)\"$|\1|' )"
        : folder:=~/Mail

        # Match any file in $folder beginning with $cur
        # (minus the leading '=' sign).
        compopt -o filenames
        COMPREPLY=( $( compgen -f -- "$folder/${cur:1}" ) )
        COMPREPLY=( ${COMPREPLY[@]#$folder/} )
        return 0
    elif [ "$cur" == !* ]; then
        spoolfile="$( $muttcmd -F "$muttrc" -Q spoolfile 2>/dev/null | \
            sed -e 's|^spoolfile=\"\(.*\)\"$|\1|' )"
        [ ! -z "$spoolfile" ] && eval cur="${cur/^!/$spoolfile}"
    fi
    _filedir

    return 0
}


_mutt()
{
    local cur prev words cword
    _init_completion -n =+! || return

    case $cur in
    -*)
        COMPREPLY=( $( compgen -W '-A -a -b -c -e -f -F -H -i -m -n \
            -p -Q -R -s -v -x -y -z -Z -h' -- "$cur" ) )
        return 0
        ;;
    *)
        case $prev in
        -a|-f|-F|-H|-i)
            _muttfiledir "$cur"
            return 0
            ;;
        -A)
            _muttaliases "$cur"
            return 0
            ;;
        -e|-m|-Q|-s|-h|-p|-R|-v|-y|-z|-Z)
            return 0
            ;;
        *)
            _muttaddr "$cur"
            return 0
            ;;
        esac
        ;;
    esac
} &&
complete -F _mutt -o default mutt muttng

# ex: ts=4 sw=4 et filetype=sh