PageRenderTime 62ms CodeModel.GetById 40ms app.highlight 1ms RepoModel.GetById 19ms app.codeStats 0ms

/bash_completion_lib/complete -o default -o filenames/mutt

http://github.com/brinkman83/bashrc
#! | 139 lines | 115 code | 24 blank | 0 comment | 0 complexity | 3062a0303e2a909bd92744ae0fc9eea3 MD5 | raw file
  1# mutt completion
  2#
  3# Mutt doesn't have an "addressbook" like Pine, but it has aliases and
  4# a "query" function to retrieve addresses, so that's what we use here.
  5
  6
  7comp_include _filedir _get_cword
  8
  9
 10_muttaddr()
 11{
 12    _muttaliases
 13    _muttquery
 14    return 0
 15}
 16
 17_muttconffiles()
 18{
 19    local file sofar
 20    local -a newconffiles
 21
 22    sofar=" $1 "
 23    shift
 24    while [[ "$1" ]]; do
 25        newconffiles=( $(sed -rn 's|^source[[:space:]]+([^[:space:]]+).*$|\1|p' $(eval echo $1) ) )
 26        for file in ${newconffiles[@]}; do
 27        [[ ! "$file" ]] || [[ "${sofar/ ${file} / }" != "$sofar" ]] &&
 28            continue
 29        sofar="$sofar $file"
 30        sofar=" $(eval _muttconffiles \"$sofar\" $file) "
 31        done
 32        shift
 33    done
 34    echo $sofar
 35}
 36
 37_muttaliases()
 38{
 39    local cur muttrc
 40    local -a conffiles aliases
 41    cur=`_get_cword`
 42
 43    [ -f ~/.${muttcmd}/${muttcmd}rc ] && muttrc="~/.${muttcmd}/${muttcmd}rc"
 44    [ -f ~/.${muttcmd}rc ] && muttrc="~/.${muttcmd}rc"
 45    [ -z "$muttrc" ] && return 0
 46
 47    conffiles=( $(eval _muttconffiles $muttrc $muttrc) )
 48    aliases=( $( sed -rn 's|^alias[[:space:]]+([^[:space:]]+).*$|\1|p' \
 49            $(eval echo ${conffiles[@]}) ) )
 50    COMPREPLY=( ${COMPREPLY[@]} $( compgen -W "${aliases[*]}" -- $cur ) )
 51
 52    return 0
 53}
 54
 55_muttquery()
 56{
 57    local cur querycmd
 58    local -a queryresults
 59    cur=`_get_cword`
 60
 61    querycmd="$( $muttcmd -Q query_command  | sed -r 's|^query_command=\"(.*)\"$|\1|; s|%s|'$cur'|' )"
 62    if [ -z "$cur" -o -z "$querycmd" ]; then
 63        queryresults=()
 64    else 
 65        queryresults=( $( $querycmd | \
 66          sed -nr '2,$s|^([^[:space:]]+).*|\1|p' ) )
 67    fi
 68
 69    COMPREPLY=( ${COMPREPLY[@]} $( compgen -W "${queryresults[*]}" \
 70            -- $cur ) )
 71
 72    return 0
 73}
 74
 75_muttfiledir()
 76{
 77    local cur folder spoolfile
 78    cur=`_get_cword`
 79
 80    # This is currently not working so well. Perhaps this function should
 81    # just call _filedir() for the moment.
 82    if [[ $cur == [=+]* ]]; then
 83        folder="$( $muttcmd -Q folder | sed -r 's|^folder=\"(.*)\"$|\1|' )"
 84        : folder:=~/Mail
 85
 86        # Match any file in $folder beginning with $cur
 87        # (minus the leading '=' sign).
 88        COMPREPLY=( $( compgen -f -- "$folder/${cur:1}" ) )
 89        COMPREPLY=( ${COMPREPLY[@]#$folder/} )
 90        return 0
 91    elif [ "$cur" == !* ]; then
 92        spoolfile="$( $muttcmd -Q spoolfile | sed -r 's|^spoolfile=\"(.*)\"$|\1|' )"
 93        [ ! -z "$spoolfile" ] && eval cur="${cur/^!/$spoolfile}";
 94    fi
 95    _filedir
 96
 97    return 0
 98}
 99
100_mutt()
101{
102    local cur prev
103    cur=`_get_cword`
104    prev=${COMP_WORDS[COMP_CWORD-1]}
105
106    COMPREPLY=()
107    
108    [ ${COMP_WORDS[0]} == muttng ] && muttcmd="muttng" || muttcmd="mutt"
109
110    case "$cur" in
111    -*)
112        COMPREPLY=( $( compgen -W '-A -a -b -c -e -f -F -H -i -m -n \
113                        -p -Q -R -s -v -x -y -z -Z -h' \
114                        -- $cur ) )
115        return 0
116        ;;
117    *)
118        case "$prev" in
119        -@(a|f|F|H|i))
120            _muttfiledir
121            return 0
122            ;;
123        -A)
124            _muttaliases
125            return 0
126            ;;
127        -@(e|m|Q|s|h|p|R|v|y|z|Z))
128            return 0
129            ;;
130        *)
131            _muttaddr
132            return 0
133            ;;
134        esac
135        ;;
136    esac
137} # _mutt()
138
139