about summary refs log tree commit diff
path: root/Completion/Base/_tilde
blob: d871ddc119a4eb869d7eb0b0fab4c7f1f61d4bdc (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
#compdef -tilde-

# We use all named directories and user names here. If this is too slow
# for you or if there are too many of them, you may want to use
# `compgen -k friends -qS/' or something like that. To get all user names
# if there are no matches in the `friends' array, add
#   `(( compstate[nmatches] )) || compgen -nu -qS/'
# below that.

setopt localoptions extendedglob

local d s dirs list lines revlines i

if [[ "$SUFFIX" = */* ]]; then
  ISUFFIX="/${SUFFIX#*/}$ISUFFIX"
  SUFFIX="${SUFFIX%%/*}"
  s=(-S '')
else
  s=(-qS/)
fi

if [[ -prefix [-+] ]]; then
  lines=(${(f)"$(dirs -v)"})
  integer i
  if [[ ( -prefix - && ! -o pushdminus ) ||
	( -prefix + && -o pushdminus ) ]]; then
    revlines=( $lines )
    for (( i = 1; i <= $#lines; i++ )); do
      lines[$i]="$((i-1)) -- ${revlines[-$i]##[0-9]#[	 ]#}"
    done
  else
    for (( i = 1; i <= $#lines; i++ )); do
      lines[$i]="$((i-1)) -- ${lines[$i]##[0-9]#[	 ]#}"
    done
  fi
  list=(${lines%% *})

  compset -P '[-+]'
  _description d 'directory stack'
  compadd "$d[@]" -V dirs -S/ -ld lines -Q - "$list[@]" 
else
  _users "$@"
  if (( $# )); then
    d=( "$@" )
  else
    _description d 'named directory'
  fi
  compgen "$d[@]" -n "$s[@]"
fi