about summary refs log tree commit diff
path: root/Completion/User/_mailboxes
blob: 2cf5361ba7bf454ca69180f8f8b8207060bb3307 (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
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
#autoload

#emulate -L zsh
setopt localoptions nullglob

# This is still needlessly mutt-biased and should be fixed.

local -U dirboxes
local i j expl muttrc="${muttrc:-~/.muttrc}"
local pinedirectory="${pinedirectory:-~/mail}"
local maildirectory="${maildirectory:-~/Mail}"

if (( ! $+_mailbox_cache )) then

  typeset -U -g _mailbox_cache
  typeset -U -g _maildir_cache _mbox_cache _mh_cache _mutt_cache _pine_cache

  [[ -f ${~muttrc:-.} ]] &&
    _mutt_cache=( ${$(grep mailboxes ${~muttrc})[2,-1]} )

  _mbox_cache=( ${~maildirectory}/*(^/) )
  _pine_cache=( ${~pinedirectory}/**/*(.) )

  dirboxes=( ${~maildirectory}/*(/) )

  while (( $#dirboxes )); do
    i=${dirboxes[1]}
    shift dirboxes
    if [[ -d "$i/cur" ]]; then
      _maildir_cache=( "${_maildir_cache[@]}" "$i" )
    elif j=( $i/<1-> ) && [[ -n "$j" ]]; then
      _mh_cache=( "${_mh_cache[@]}" "$i" )
    else
      _mbox_cache=( "${_mbox_cache[@]}" "$i"/*(.) )
      dirboxes=( $dirboxes $i/*(/) )
    fi
  done

  [[ -n "$_mutt_cache" || -d ~/.elm || -d ~/.mutt ]] &&
      _mailbox_cache=( \! \< \> )
  [[ -n "$mailpath" ]] &&
      _mailbox_cache=( "${_mailbox_cache[@]}" "${(@)mailpath%%\?*}" )

  _mailbox_cache=( "${_mailbox_cache[@]}" $MAIL )
fi

if _wanted files expl 'mailbox specification'; then
  local -U mbox_names
  case "${curcontext}" in
  (*:elm:*) # I've probably got this wrong, or at least incomplete
    mbox_names=( "${_mbox_cache[@]}" "${_mailbox_cache[@]}" )
    compadd "${expl[@]}" - \! \< \>;;
  (*:mail:*)
    if compset -P '+|-f+'; then
      mbox_names=( "${(@)_mbox_cache#$~maildirectory/}" )
    else
      mbox_names=( +"${(@)^_mbox_cache#$~maildirectory/}" "${_mailbox_cache[@]}" )
      _path_files -J all-files
    fi;;
  (*:mh:*) # I've probably got this wrong, or at least incomplete
    (( $#_mh_cache )) && _multi_parts "${expl[@]}" / _mh_cache
    return;;
  (*:mutt:*)
    mbox_names=( "${_mutt_cache[@]}" "${_mailbox_cache[@]}" "${_maildir_cache[@]}" )
    compadd "${expl[@]}" - \! \< \>;;
  (*:pine:*)
    # Pine is like mail but with no leading `+' to disambiguate;
    # any files not in $pinedirectory must be absolute paths.
    mbox_names=( "${(@)_pine_cache#$~pinedirectory/}" "${_mbox_cache[@]}" )
    # _path_files -J all-files -g '/*'
    ;;
  (*:(zmail|zmlite):*)
    if compset -P '+|-f+'; then
      mbox_names=( "${(@)_mbox_cache#$~maildirectory/}" )
    else
      mbox_names=( +"${(@)^_mbox_cache#$~maildirectory/}"
		   "${_mailbox_cache[@]}" "${_mh_cache[@]}" )
      compadd "${expl[@]}" - % \&
      _path_files -J all-files
    fi
    ;;
  (*) # Some other program wants mailbox names?  Use them all?
    mbox_names=( "${_mailbox_cache[@]}" "${_mbox_cache[@]}"
		 "${_mh_cache[@]}" "${_mutt_cache[@]}" "${_pine_cache[@]}" );;
  esac

  (( $#mbox_names )) && _multi_parts "${expl[@]}" / mbox_names
fi