From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24819 invoked by alias); 25 Nov 2011 22:00:29 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 29918 Received: (qmail 23983 invoked from network); 25 Nov 2011 22:00:26 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at ruderich.org designates 178.63.68.96 as permitted sender) Date: Fri, 25 Nov 2011 23:00:17 +0100 From: Simon Ruderich To: zsh-workers@zsh.org Subject: PATCH: Completion: Add new _remote_files() helper and use it to reduce code duplication. Message-ID: <7e26d3ea1b2ebe2924bc4c05adfca80e1e9e2e83.1322257147.git.simon@ruderich.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="=_zucker.schokokeks.org-25216-1322258421-0001-2" Content-Disposition: inline User-Agent: Mutt/1.5.21 (2011-04-20) --=_zucker.schokokeks.org-25216-1322258421-0001-2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable --- Hello, This patch adds a new completion utility _remote_files() which tries to reduce some code duplication when completing remote files. Please report any mistakes or overcomplicated solutions (patches are welcome). I've tested it (except for _rlogin as I don't use rsh) but I'm sure there are some cases I missed. Please have a look and test it. Regards, Simon Completion/Base/Utility/.distfiles | 2 +- Completion/Base/Utility/_remote_files | 77 +++++++++++++++++++++++++++++= ++++ Completion/Unix/Command/_git | 35 +-------------- Completion/Unix/Command/_rlogin | 29 +------------ Completion/Unix/Command/_rsync | 28 +----------- Completion/Unix/Command/_ssh | 38 +--------------- 6 files changed, 83 insertions(+), 126 deletions(-) create mode 100644 Completion/Base/Utility/_remote_files diff --git a/Completion/Base/Utility/.distfiles b/Completion/Base/Utility/.= distfiles index 97d86bd..d5e359a 100644 --- a/Completion/Base/Utility/.distfiles +++ b/Completion/Base/Utility/.distfiles @@ -5,5 +5,5 @@ _arg_compile _combination _regex_arguments _valu= es _arguments _set_command _retrieve_cache _guard _cache_invalid _describe _sep_parts _pick_variant _call_function _multi_parts _store_cache _regex_words -_complete_help_generic +_complete_help_generic _remote_files ' diff --git a/Completion/Base/Utility/_remote_files b/Completion/Base/Utilit= y/_remote_files new file mode 100644 index 0000000..c35e3d3 --- /dev/null +++ b/Completion/Base/Utility/_remote_files @@ -0,0 +1,77 @@ +#autoload + +# Complete files on remote systems using SSH (and other programs). Needs +# key-based authentication with no passwords or a running ssh-agent to wor= k. +# +# Usage: +# _remote_files [--no-files] [--no-dirs] -- [] +# +# Options: +# - --no-files: don't complete remote files +# - --no-dirs: don't complete remote directories +# +# Commands: +# - ssh: Additional options for non-interactive use are automatically added +# (see below). +# - All other commands are used unaltered. +# +# Examples: +# - _remote_files -- ssh +# Use ssh to retrieve the remote paths. The "--" is not optional! +# - _remote_files --no-files -- ssh -2 -p 42 +# Use ssh with the option -2 and -p 42 to retrieve the remote paths and +# complete only directories. +# - _remote_files -- rsh +# Use rsh to retrieve the remote paths. + + +# There should be coloring based on all the different ls -F classifiers. +local expl rempat remfiles remdispf remdispd args cmd cmd_args suf ret=3D1 + +if zstyle -T ":completion:${curcontext}:files" remote-access; then + + # Parse options to _remote_files. Stops at the first "--". + zparseopts -D -E -a args -no-files -no-dirs + shift + + # Command to run on the remote system. + cmd=3D"$1" + shift + + # Handle arguments to ssh. + if [[ $cmd =3D=3D ssh ]]; then + zparseopts -D -E -a cmd_args p: 1 2 4 6 F: + cmd_args=3D"-o BatchMode=3Dyes $cmd_args -a -x" + else + cmd_args=3D"$@" + fi + + if [[ -z $QIPREFIX ]] + then rempat=3D"${PREFIX%%[^./][^/]#}\*" + else rempat=3D"${(q)PREFIX%%[^./][^/]#}\*" + fi + + remfiles=3D(${(M)${(f)"$(_call_program files $cmd $cmd_args ${IPREFIX%:}= ls -d1FL -- "$rempat" 2>/dev/null)"}%%[^/]#(|/)}) + + compset -P '*/' + compset -S '/*' || suf=3D'remote file' + + remdispf=3D(${remfiles:#*/}) + remdispd=3D(${(M)remfiles:#*/}) + + _tags files + while _tags; do + while _next_label files expl ${suf:-remote directory}; do + [[ ${args[(I)--no-files]} -eq 0 ]] && \ + [[ -n $suf ]] && compadd "$@" "$expl[@]" -d remdispf \ + ${(q)remdispf%[*=3D@|]} && ret=3D0 + [[ ${args[(I)--no-dirs]} -eq 0 ]] && \ + compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \ + ${(q)remdispd%/} && ret=3D0 + done + (( ret )) || return 0 + done + return ret +else + _message -e remote-files 'remote file' +fi diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git index 4a830f2..702afe0 100644 --- a/Completion/Unix/Command/_git +++ b/Completion/Unix/Command/_git @@ -5444,39 +5444,6 @@ __git_tree_files () { =20 # Repository Argument Types =20 -# _remote_files -_remote_files_git () { - # FIXME: these should be imported from _ssh - # TODO: this should take -/ to only get directories - # There should be coloring based on all the different ls -F classifiers. - local expl rempat remfiles remdispf remdispd args suf ret=3D1 - - 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=3D"${PREFIX%%[^./][^/]#}\*" - else rempat=3D"${(q)PREFIX%%[^./][^/]#}\*" - fi - remfiles=3D(${(M)${(f)"$(_call_program files ssh $args -a -x ${IPREFIX= %:} ls -d1FL "$rempat" 2>/dev/null)"}%%[^/]#(|/)}) - compset -P '*/' - compset -S '/*' || suf=3D'remote file' - - remdispd=3D(${(M)remfiles:#*/}) - - _tags files - while _tags; do - while _next_label files expl ${suf:-remote directory}; do - compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \ - ${(q)remdispd%/} && ret=3D0 - done - (( ret )) || return 0 - done - return ret - else - _message -e remote-files 'remote file' - fi -} - (( $+functions[__git_remote_repositories] )) || __git_remote_repositories () { local service @@ -5484,7 +5451,7 @@ __git_remote_repositories () { service=3D _ssh =20 if compset -P '*:'; then - _remote_files_git + _remote_files --no-files -- ssh else _ssh_hosts -S: fi diff --git a/Completion/Unix/Command/_rlogin b/Completion/Unix/Command/_rlo= gin index 0d8355b..a04c6d0 100644 --- a/Completion/Unix/Command/_rlogin +++ b/Completion/Unix/Command/_rlogin @@ -1,32 +1,5 @@ #compdef rlogin rsh remsh=3Drsh rcp =20 -_rcp_remote_files () { - # There should be coloring based on all the different ls -F classifiers. - local expl remfiles remdispf remdispd suf ret=3D1 - - if zstyle -T ":completion:${curcontext}:" remote-access; then - remfiles=3D(${(M)${(f)"$(rsh ${IPREFIX%:} ls -d1F ${(Q)PREFIX%%[^./][^= /]#}\* 2>/dev/null)"}%%[^/]#(|/)}) - compset -P '*/' - compset -S '/*' || suf=3D'remote file' - - remdispf=3D(${remfiles:#*/}) - remdispd=3D(${(M)remfiles:#*/}) - - _tags files - while _tags; do - while _next_label files expl ${suf:-remote directory}; do - [[ -n $suf ]] && compadd "$@" "$expl[@]" -d remdispf \ - ${(q)remdispf%[*=3D@|]} && ret=3D0=20 - compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \ - ${(q)remdispd%/} && ret=3D0 - done - (( ret )) || return 0 - done - else - _message -e remote-files 'remote file' - fi -} - _rlogin () { case "$service" in rlogin) @@ -65,7 +38,7 @@ _rlogin () { =20 if [[ -n "$state" ]]; then if compset -P '*:'; then - _rcp_remote_files && ret=3D0 + _remote_files -- rsh && ret=3D0 elif compset -P '*@'; then _wanted hosts expl host _rlogin_hosts -S: && ret=3D0 else diff --git a/Completion/Unix/Command/_rsync b/Completion/Unix/Command/_rsync index b999c1b..f24a06e 100644 --- a/Completion/Unix/Command/_rsync +++ b/Completion/Unix/Command/_rsync @@ -59,33 +59,7 @@ elif compset -P 1 '*::' || compset -P 1 'rsync://*/'; th= en elif compset -P 'rsync://'; then _rsync_user_or_host / "$@" elif compset -P 1 '*:'; then - - if zstyle -T ":completion:${curcontext}:files" remote-access; then - if [[ -z $QIPREFIX ]] - then rempat=3D"${PREFIX%%[^./][^/]#}\*" - else rempat=3D"${(q)PREFIX%%[^./][^/]#}\*" - fi - remfiles=3D(${(M)${(f)"$(_call_program files ssh -a -x ${IPREFIX%:} ls= -d1FL "$rempat" 2>/dev/null)"}%%[^/]#(|/)}) - compset -P '*/' - compset -S '/*' || suf=3D'remote file' - - remdispf=3D(${remfiles:#*/}) - remdispd=3D(${(M)remfiles:#*/}) - - _tags files - while _tags; do - while _next_label files expl ${suf:-remote directory}; do - [[ -n $suf ]] && compadd "$@" "$expl[@]" -d remdispf \ - ${(q)remdispf%[*=3D@|]} && ret=3D0=20 - compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \ - ${(q)remdispd%/} && ret=3D0 - done - (( ret )) || return 0 - done - else - _message -e remote-files 'remote file' - fi - + _remote_files -- ssh else _rsync_user_or_host : "$@" fi diff --git a/Completion/Unix/Command/_ssh b/Completion/Unix/Command/_ssh index bd23c36..a6f2b40 100644 --- a/Completion/Unix/Command/_ssh +++ b/Completion/Unix/Command/_ssh @@ -5,40 +5,6 @@ # TODO: update ssh-keygen (not based on 5.9) # TODO: sshd, ssh-keyscan, ssh-keysign =20 - -_remote_files () { - # There should be coloring based on all the different ls -F classifiers. - local expl rempat remfiles remdispf remdispd args suf ret=3D1 - - 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=3D"${PREFIX%%[^./][^/]#}\*" - else rempat=3D"${(q)PREFIX%%[^./][^/]#}\*" - fi - remfiles=3D(${(M)${(f)"$(_call_program files ssh -o BatchMode=3Dyes $a= rgs -a -x ${IPREFIX%:} ls -d1FL -- "$rempat" 2>/dev/null)"}%%[^/]#(|/)}) - compset -P '*/' - compset -S '/*' || suf=3D'remote file' - - remdispf=3D(${remfiles:#*/}) - remdispd=3D(${(M)remfiles:#*/}) - - _tags files - while _tags; do - while _next_label files expl ${suf:-remote directory}; do - [[ -n $suf ]] && compadd "$@" "$expl[@]" -d remdispf \ - ${(q)remdispf%[*=3D@|]} && ret=3D0=20 - compadd ${suf:+-S/} "$@" "$expl[@]" -d remdispd \ - ${(q)remdispd%/} && ret=3D0 - done - (( ret )) || return 0 - done - return ret - else - _message -e remote-files 'remote file' - fi -} - _ssh () { local curcontext=3D"$curcontext" state line expl common tmp cmds suf ret= =3D1 typeset -A opt_args @@ -386,7 +352,7 @@ _ssh () { ;; file) if compset -P '*:'; then - _remote_files ${(kv)~opt_args[(I)-[FP1246]]/-P/-p} && ret=3D0 + _remote_files -- ssh ${(kv)~opt_args[(I)-[FP1246]]/-P/-p} && ret= =3D0 elif compset -P '*@'; then suf=3D( -S '' ) compset -S ':*' || suf=3D( -r: -S: ) @@ -400,7 +366,7 @@ _ssh () { ;; rfile) if compset -P '*:'; then - _remote_files && ret=3D0 + _remote_files -- ssh && ret=3D0 elif compset -P '*@'; then _wanted hosts expl host _ssh_hosts -r: -S: && ret=3D0 else --=20 1.7.7.3 --=20 + privacy is necessary + using gnupg http://gnupg.org + public key id: 0x92FEFDB7E44C32F9 --=_zucker.schokokeks.org-25216-1322258421-0001-2 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAEBCAAGBQJO0A/xAAoJEJL+/bfkTDL5nEIQAJQ+xKp8gn9vum3cE1zKOc+9 94yNd4mj2ulL/dcP1nrFe8qYVWg7NKJBUVvQpIYQAOWz/Zkp3Zy0/ipQZpQN5I0Y kla77rOASZ2m1CFnLY8zzey767mT5dKePdm9f4jEfGTWCSuMgnaplWYFTAtpuDIf fRv9gU8cZvD38NTEjBKwnk9Yh/jpphyVMgSZE4raokeEw4r5AQmUSfIYx134JFkT ax0NLTmjEWK/0GNejFbqnXI34bao8o8ZNaR28WjqcLYRAjYXbmaqE+kTg/ufOWbn 36Zs52sOe3FVIqN/usMPpnNUO0ZQCpXOuM7FH+2Tvc0fibsIY5npxiY49BdqxTyL CuWPuqsIu8YdHi14TBPzGqUBWs7+BCDSCMiMJnyM0nOhRNQvSsjpPY3HYC1P664i FQqGq+DrGPEbN2UDzK5rzz2kiSsFIi9DiFenMa+fx/cBqDqnUFMeDh7Cyg4ZEKRg JVRm273VgGk48Pho813jy8f7uH+cac9HfhBezVs91ZvzHyO27VtFSkYskx4i8LyF hWJzYGsyo4eJlAjJnQGncffWprlP4l3rJ++JDCcmnygTUqVu44Hy39Eo8uT+MQkR CQXH+RiDwzKxYmvtSSxa1iG3cPI/0fecQS7sdDStZlF7IhSggzC5UUxzoJ9m/wJP +5Kjik7QS30zlu4FODVk =7MjL -----END PGP SIGNATURE----- --=_zucker.schokokeks.org-25216-1322258421-0001-2--