about summary refs log tree commit diff
path: root/Functions/Zftp/zfget_match
blob: a440cace1af54e9e72a048aeccf26b5f99982760 (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
# function zfget_match {

emulate -L zsh
zmodload -F zsh/files b:zf_ln || return 1

# the zfcd hack:  this may not be necessary here
if [[ $1 == $HOME || $1 == $HOME/* ]]; then
  1="~${1#$HOME}"
fi

if [[ $ZFTP_SYSTEM == UNIX* && $1 == */* ]]; then
  setopt localoptions clobber extendedglob
  local tmpf=${TMPPREFIX}zfgm$$
  zf_ln -fn =(<<<'') $tmpf || return 1

  if [[ -n $WIDGET ]]; then
    local dir=${1%/*}
    [[ $dir = */ ]] || dir="$dir/"
    zftp ls -LF $dir >$tmpf
    local reply1 reply2

    # dirs in reply1, files in reply2
    reply1=(${${(M)${${(f)"$(<$tmpf)"}##$dir}:#*/}%/})
    reply2=(${${${${(f)"$(<$tmpf)"}##$dir}%\*}:#*/})

    # try dir if ls -F doesn't work
    if ! (($#reply1)); then
      zftp dir $dir >$tmpf
      reply1=(${(M)${(f)"$(<$tmpf)"}:#d([^[:space:]]##[[:space:]]##)(#c8)?##})
      reply1=(${reply1/(#b)d([^[:space:]]##[[:space:]]##)(#c8)([^\/]##)\/#/$match[2]})

      reply2=(${${(f)"$(<$tmpf)"}:#d([^[:space:]]##[[:space:]]##)(#c8)?##})
      reply2=(${reply2/(#b)([^[:space:]]##[[:space:]]##)(#c8)(?##)/$match[2]})
    fi
    _wanted directories expl 'remote directory' compadd -S/ -q -P $dir - $reply1
    _wanted files expl 'remote file' compadd -P $dir - $reply2
  else
    # On the first argument to ls, we usually get away with a glob.
    zftp ls "$1*$2" >$tmpf
    reply=($(<$tmpf))
  fi
else
  local fcache_name
  zffcache
  if [[ -n $WIDGET ]]; then
    _wanted files expl 'remote file' compadd -F fignore - ${(P)fcache_name}
  else
    reply=(${(P)fcache_name});
  fi
fi
# }