about summary refs log tree commit diff
path: root/Completion/Core/_expand
blob: 985f21899ffb6141cfb31deca510fb7c0cbe63d0 (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
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
#autoload

# This completer function is intended to be used as the first completer
# function and allows one to say more explicitly when and how the word
# from the line should be expanded than expand-or-complete.
# This function will allow other completer functions to be called if
# the expansions done produce no result or do not change the original
# word from the line.

setopt localoptions nullglob

[[ _matcher_num -gt 1 ]] && return 1

local exp word sort expr expl subd suf=" " force opt

(( $# )) &&
    while getopts gsco opt; do
      force="$force$opt"
    done

if [[ "$funcstack[2]" = _prefix ]]; then
  word="$IPREFIX$PREFIX$SUFFIX"
else
  word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
fi

# First, see if we should insert all *completions*.

if [[ "$force" = *c* ]] ||
   { zstyle -s ":completion:${curcontext}:" completions expr &&
     [[ "${(e):-\$[$expr]}" -eq 1 ]] }; then
  compstate[insert]=all
  return 1
fi

# In exp we will collect the expansion.

exp=("$word")

# First try substitution. That weird thing spanning multiple lines
# changes quoted spaces, tabs, and newlines into spaces and protects
# this function from aborting on parse errors in the expansion.

if [[ "$force" = *s* ]] ||
   { { zstyle -s ":completion:${curcontext}:" substitute expr ||
       { [[ "$curcontext" = expand-word:* ]] && expr=1 } } &&
         [[ "${(e):-\$[$expr]}" -eq 1 ]] }; then
  exp=( ${(f)"$(print -lR - ${(e)exp//\\[ 	
]/ })"} ) 2>/dev/null
else
  exp=( "${exp:s/\\\$/\$}" )
fi

# If the array is empty, store the original string again.

(( $#exp )) || exp=("$word")

subd=("$exp[@]")

# Now try globbing.

[[ "$force" = *g* ]] ||
  { { zstyle -s ":completion:${curcontext}:" glob expr ||
      { [[ "$curcontext" = expand-word:* ]] && expr=1 } } &&
        [[ "${(e):-\$[$expr]}" -eq 1 ]] } &&
    exp=( ${~exp} )

# If we don't have any expansions or only one and that is the same
# as the original string, we let other completers run.

(( $#exp )) || exp=("$subd[@]")

[[ $#exp -eq 1 && "$exp[1]" = "$word"(|\(N\)) ]] && return 1

# With subst-globs-only we bail out if there were no glob expansions,
# regardless of any substitutions

[[ "$force" = *o* ]] ||
  { zstyle -s ":completion:${curcontext}:" subst-globs-only expr &&
      [[ "${(e):-\$[$expr]}" -eq 1 ]] } && 
        [[ "$subd" = "$exp"(|\(N\)) ]] && return 1

# Now add as matches whatever the user requested.

zstyle -s ":completion:${curcontext}:" sort sort

[[ "$sort" = (yes|true|1|on) ]] && exp=( "${(@o)exp}" )

# If there is only one expansion, add a suitable suffix

if (( $#exp == 1 )); then
  if [[ -d $exp && "$exp[1]" != */ ]]; then
    suf=/
  elif zstyle -T ":completion:${curcontext}:" add-space; then
    suf=
  fi
fi

if [[ -z "$compstate[insert]" ]] ;then
  if [[ "$sort" = menu ]]; then
    _description expansions expl expansions "o:$word"
  else
    _description -V expansions expl expansions "o:$word"
  fi

  compadd "$expl[@]" -UQ -qS "$suf" - "$exp[@]"
else
  _tags all-expansions expansions original

  _requested all-expansions expl 'all expansions' &&
      compadd "$expl[@]" -UQ -qS "$suf" - "$exp"

  if [[ $#exp -gt 1 ]] && _requested expansions; then
    local i normal dir

    if [[ "$sort" = menu ]]; then
      _description expansions expl expansions "o:$word"
    else
      _description -V expansions expl expansions "o:$word"
    fi
    if zstyle -T ":completion:${curcontext}:" add-space; then
      suf=' '
    else
      suf=
    fi
    normal=()
    dir=()

    for i in "$exp[@]"; do
      if [[ -d "$i" && "$i" != */ ]]; then
        dir=( "$dir[@]" "$i" )
      else
	normal=( "$normal[@]" "$i" )
      fi
    done
    (( $#dir ))    && compadd "$expl[@]" -UQ -qS/ - "$dir[@]"
    (( $#normal )) && compadd "$expl[@]" -UQ -qS "$suf" - "$normal[@]"
  fi

  _requested original expl original && compadd "$expl[@]" -UQ - "$word"

  compstate[insert]=menu
fi

return 0