Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

RFC: scp remote file completion with _multi_parts



Hi,

my main goal is to get completion of abbreviated paths of remote files,
e.g.

scp server:/u/s/d/zs/cop<TAB> gets
scp server:/usr/share/doc/zsh/copyright

Therefore, I propose this patch:

diff --git a/Completion/Unix/Command/_ssh b/Completion/Unix/Command/_ssh
index c52b38c..46184d3 100644
--- a/Completion/Unix/Command/_ssh
+++ b/Completion/Unix/Command/_ssh
@@ -2,32 +2,15 @@
 
 _remote_files () {
   # There should be coloring based on all the different ls -F classifiers.
-  local expl rempat remfiles remdispf remdispd args suf ret=1
+  local expl rempat remfiles args
 
   if zstyle -T ":completion:${curcontext}:files" remote-access; then
     zparseopts -D -E -a args p: 1 2 4 6 F:
-    if [[ -z $QIPREFIX ]]
-    then rempat="${PREFIX%%[^./][^/]#}\*"
-    else rempat="${(q)PREFIX%%[^./][^/]#}\*"
-    fi
-    remfiles=(${(M)${(f)"$(_call_program files ssh $args -a -x ${IPREFIX%:} ls -d1FL "$rempat" 2>/dev/null)"}%%[^/]#(|/)})
-    compset -P '*/'
-    compset -S '/*' || suf='remote file'
-
-    remdispf=(${remfiles:#*/})
-    remdispd=(${(M)remfiles:#*/})
+    rempat="${(q)PREFIX//\//*/}\*"
+    [[ $PREFIX = (.|..|)/* ]] && rempat=${PREFIX%%/*}/${rempat#*/}
+    remfiles=(${(f)"$(_call_program files ssh -o BatchMode=yes $args -a -x ${IPREFIX%:} ls -d1FL "$rempat" 2>/dev/null)"%[*=@|]})
 
-    _tags files
-    while _tags; do
-      while _next_label files expl ${suf:-remote directory}; do
-        [[ -n $suf ]] && compadd "$@" "$expl[@]" -d remdispf \
-	    ${(q)remdispf%[*=@|]} && ret=0 
-	compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \
-	    ${(q)remdispd%/} && ret=0
-      done
-      (( ret )) || return 0
-    done
-    return ret
+    _wanted files expl 'remote file' _multi_parts $@ -- / remfiles
   else
     _message -e remote-files 'remote file'
   fi

This patch has the following drawbacks:
• The completion description doesn't contain the file type anymore, i.e.
  you don't get the suffixes @, |, * and =.

• The directory separator / isn't automaticly removed from the end, if
  you insert a space.

• It doesn't complete files with braces, but this might be a bug anywhere
  else, because:

+_remote_files:7> rempat='/t\*/x\*/\*' 
+_remote_files:8> remfiles=+_remote_files:8> remfiles=( '/tmp/xx aa/ll12(45)' '/tmp/xx aa/zzz' ) 
+_remote_files:10> _wanted files expl 'remote file' _multi_parts -- / remfiles
…
+_multi_parts:94> compadd -O tmp1 -M 'r:|/=* r:|=* ' - 'll12(45)' zzz
+_multi_parts:96> [[ 2 -eq 0 ]]
+_multi_parts:99> [[ 2 -eq 1 ]]
+_multi_parts:129> ((  2  ))
…
+_multi_parts:174> compadd -J -default- -S '' -p '/tmp/xx aa/' -M 'r:|/=* r:|=* ' - zzz

Other changes of this patch:
• The patch doesn't request a full directory listing of the remote
  directory, but only those entries beginning with the prefix, e.g. for
  /usr/share/doc/zs the remote ls isn't /usr/share/doc/* but
  /usr/share/doc/zs*. I think this is better than before.

• It calls ssh with BatchMode=yes to prevent it from asking questions
  like confirming the fingerprint.

What do you think? Are these drawbacks worth the completion of
abbreviations?

Bye, Jörg.
-- 
“Unfortunately, the current generation of mail programs do not have
 checkers to see if the sender knows what he is talking about”
            (Andrew S. Tanenbaum)



Messages sorted by: Reverse Date, Date, Thread, Author