182 lines
4.4 KiB
Plaintext
Raw Normal View History

2009-06-05 08:23:47 +02:00
# mutt completion
#
# 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.
2009-06-05 08:23:47 +02:00
have mutt || have muttng && {
# @param $1 (cur) Current word to complete
2009-06-05 08:23:47 +02:00
_muttaddr()
{
_muttaliases "$1"
_muttquery "$1"
2009-06-05 08:23:47 +02:00
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -u -- "$1" ) )
2009-06-05 08:23:47 +02:00
2009-10-04 19:42:50 +02:00
return 0
} # _muttaddr()
# Find muttrc to use
# @output muttrc filename
_muttrc()
{
# Search COMP_WORDS for '-F muttrc' or '-Fmuttrc' argument
set -- "${COMP_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
[ -f ~/.${muttcmd}/${muttcmd}rc ] && muttrc="~/.${muttcmd}/${muttcmd}rc"
[ -f ~/.${muttcmd}rc ] && muttrc="~/.${muttcmd}rc"
fi
printf "%s" "$muttrc"
} # _muttrc()
2009-06-05 08:23:47 +02:00
# 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
2009-06-05 08:23:47 +02:00
_muttconffiles()
{
2009-10-04 19:42:50 +02:00
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) ) )
2009-10-04 19:42:50 +02:00
for file in "${newconffiles[@]}"; do
[[ ! "$file" ]] || [[ "${sofar/ ${file} / }" != "$sofar" ]] &&
continue
sofar="$sofar $file"
sofar=" $(eval _muttconffiles \"$sofar\" $file) "
2009-10-04 19:42:50 +02:00
done
shift
done
2009-12-21 00:09:02 +02:00
printf '%s\n' $sofar
} # _muttconffiles()
2009-06-05 08:23:47 +02:00
# @param $1 (cur) Current word to complete
2009-06-05 08:23:47 +02:00
_muttaliases()
{
local cur=$1 muttrc muttcmd=${COMP_WORDS[0]}
2009-10-04 19:42:50 +02:00
local -a conffiles aliases
2009-06-05 08:23:47 +02:00
muttrc=$(_muttrc)
2009-10-04 19:42:50 +02:00
[ -z "$muttrc" ] && return 0
2009-06-05 08:23:47 +02:00
2009-10-04 19:42:50 +02:00
conffiles=( $(eval _muttconffiles $muttrc $muttrc) )
aliases=( $( sed -n 's|^alias[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*$|\1|p' \
2009-10-04 19:42:50 +02:00
$(eval echo "${conffiles[@]}") ) )
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${aliases[*]}" -- "$cur" ) )
2009-06-05 08:23:47 +02:00
2009-10-04 19:42:50 +02:00
return 0
2009-06-05 08:23:47 +02:00
}
# @param $1 (cur) Current word to complete
2009-06-05 08:23:47 +02:00
_muttquery()
{
local cur=$1 querycmd muttcmd=${COMP_WORDS[0]}
2009-10-04 19:42:50 +02:00
local -a queryresults
querycmd="$( $muttcmd -Q query_command | sed -e 's|^query_command=\"\(.*\)\"$|\1|' -e 's|%s|'$cur'|' )"
if [[ -z "$cur" || -z "$querycmd" ]]; then
2009-10-04 19:42:50 +02:00
queryresults=()
else
queryresults=( $( $querycmd | \
sed -n '2,$s|^\([^[:space:]]\{1,\}\).*|\1|p' ) )
2009-10-04 19:42:50 +02:00
fi
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "${queryresults[*]}" \
-- "$cur" ) )
return 0
2009-06-05 08:23:47 +02:00
}
# @param $1 (cur) Current word to complete
2009-06-05 08:23:47 +02:00
_muttfiledir()
{
local cur=$1 folder muttrc spoolfile muttcmd=${COMP_WORDS[0]}
2009-10-04 19:42:50 +02:00
muttrc=$(_muttrc)
2009-10-04 19:42:50 +02:00
if [[ $cur == [=+]* ]]; then
folder="$( $muttcmd -F "$muttrc" -Q folder | sed -e 's|^folder=\"\(.*\)\"$|\1|' )"
2009-10-04 19:42:50 +02:00
: folder:=~/Mail
# Match any file in $folder beginning with $cur
# (minus the leading '=' sign).
COMPREPLY=( $( compgen -f -- "$folder/${cur:1}" ) )
COMPREPLY=( ${COMPREPLY[@]#$folder/} )
return 0
elif [ "$cur" == !* ]; then
spoolfile="$( $muttcmd -F "$muttrc" -Q spoolfile | \
sed -e 's|^spoolfile=\"\(.*\)\"$|\1|' )"
2009-10-04 19:42:50 +02:00
[ ! -z "$spoolfile" ] && eval cur="${cur/^!/$spoolfile}";
fi
_filedir
return 0
} # _muttfiledir
2009-06-05 08:23:47 +02:00
_mutt()
{
2009-10-04 19:42:50 +02:00
local cur prev
cur=`_get_cword =+!`
prev=`_get_pword =+!`
2009-10-04 19:42:50 +02:00
COMPREPLY=()
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"
2009-10-04 19:42:50 +02:00
return 0
;;
-A)
_muttaliases "$cur"
2009-10-04 19:42:50 +02:00
return 0
;;
-e|-m|-Q|-s|-h|-p|-R|-v|-y|-z|-Z)
2009-10-04 19:42:50 +02:00
return 0
;;
*)
_muttaddr "$cur"
2009-10-04 19:42:50 +02:00
return 0
;;
esac
;;
esac
} # _mutt()
complete -F _mutt -o default -o filenames mutt muttng
} # have mutt
# Local variables:
# mode: shell-script
2009-10-04 19:42:50 +02:00
# sh-basic-offset: 4
# sh-indent-comment: t
2009-10-04 19:42:50 +02:00
# indent-tabs-mode: nil
# End:
2009-10-04 19:42:50 +02:00
# ex: ts=4 sw=4 et filetype=sh