From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23592 invoked by alias); 26 Oct 2016 14:07:31 -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: 39731 Received: (qmail 17586 invoked from network); 26 Oct 2016 14:07:31 -0000 X-Qmail-Scanner-Diagnostics: from rcpt-expgw.biglobe.ne.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(133.208.98.2):SA:0(-1.3/5.0):. Processed in 0.501757 secs); 26 Oct 2016 14:07:31 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=RP_MATCHES_RCVD,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: takimoto-j@kba.biglobe.ne.jp X-Qmail-Scanner-Mime-Attachments: |_awk| X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at spf01.biglobe.ne.jp designates 133.208.98.2 as permitted sender) X-Biglobe-Sender: From: "Jun T." Content-Type: multipart/mixed; boundary="Apple-Mail=_60DF505F-24BF-4AD4-8E99-AB2EEA2B9E61" Subject: [PATCH] _awk: support gawk ver.3 and 4 Message-Id: <18D9B3FC-1C39-4A94-9BAB-EADFE9E15FB9@kba.biglobe.ne.jp> Date: Wed, 26 Oct 2016 23:07:21 +0900 To: zsh-workers@zsh.org Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) X-Mailer: Apple Mail (2.1878.6) X-Biglobe-Spnum: 61063 --Apple-Mail=_60DF505F-24BF-4AD4-8E99-AB2EEA2B9E61 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Add support for gawk ver.3 and 4 in _awk. The new _awk is also attached just for convenience. Any comments are welcome. Jun diff --git a/Completion/Unix/Command/_awk b/Completion/Unix/Command/_awk index c493c3b..a3b2422 100644 --- a/Completion/Unix/Command/_awk +++ b/Completion/Unix/Command/_awk @@ -1,21 +1,119 @@ -#compdef awk - -# completions for awk -# This only aims to complete POSIX awk options, as described in -# awk(P). Most awk implementations, such as gawk and mawk, will have -# additional options that this does not complete. Also, currently -# this completion does not allow everything that POSIX allows. For -# example, awk(P) states that the user may specify assignments -# without using the -v option; this does not support that. +#compdef awk gawk + +# For gawk ver.3 and 4, in addition to POSIX. +# +# gawk's options '-W ...' (such as '-W help') are not supported. +# gawk3 has some synomyms for long options (e.g., --compat is a synomym +# for --traditional). These are not supported either. # -# In addition, the "program text" completion is not perfect. For -# instance, type "awk -" and then hit tab. You will be presented -# both with the dashed options and with the "program text" option. -# Fixing this is beyond my current _arguments expertise--help -# appreciated. - -_arguments -S -s '-F+[define input field separator to be an extended = regular expression]:extended regular expression:' \ - '*-v+[assign values to variables]:assignment:' \ - '(1)-f+[program file]:program file:_files' \ - '1:program text:' \ - '*:input files:_files' +# 'gawk -f' will complete files in AWKPATH in addition to those in +# the current directory. If this is annoying, you may try +# zstyle ':completion:*:*:gawk:option-f-1:*' tag-order program-files + +local variant curcontext=3D"$curcontext" state state_descr line ret=3D1 +local -A opt_args +local -a args + +_pick_variant -r variant gawk4=3D'GNU Awk 4' gawk3=3D'GNU Awk 3' posix = --version + +args=3D( + {-F+,--field-separator}'[define input field separator by extended = regex]:extended regular expression:' + '*'{-v+,--assign}'[assign values to variables]:assignment:' + '(1)*'{-f+,--file}'[read program file]:program file:->script' + '1: :_guard "^-*" "program text"' + '*:input files:_files' +) + +case $variant in + (gawk*) + args+=3D( + {-c,--traditional}'[run in compatibility mode]' + '(- : *)'{-C,--copyright}'[print copyright info and exit]' + {-d-,--dump-variables=3D-}'[print a sorted list of global = variables]::output file:_files' + {-e,--source}'[pass program text in arg]:program text:' + '(1)'{-E+,--exec}'[like -f, but safer for CGI]:program = file:->script' + '(- : *)'{-h,--help}'[print usage message and exit]' + {-L-,--lint=3D-}'[warn about dubious or non-portable = constructs]::flag:((fatal\:"treat warnings as fatal error" = invalid\:"warn only about thigs that are actually invalid"))' + {-n,--non-decimal-data}'[auto-detect octal/hexadecimal values in = input]' + {-N,--use-lc-numeric}"[force use of locale's decimal point = character]" + {-O,--optimize}'[enable optimization]' + {-p-,--profile=3D-}'[output profiling data to file]::output = file:_files' + {-P,--posix}'[run in strict POSIX mode]' + {-r,--re-interval}'[enable interval expressions in regex = matching]' + {-t,--lint-old}'[warn about non-portable constructs]' + '(- : *)'{-V,--version}'[print version info and exit]' + ) + ;| + (gawk4) + args+=3D( + {-b,--characters-as-bytes}'[treat all input data as single-byte = characters]' + {-D-,--debug=3D-}'[enable debugging]::debugger command = file:_files' + {-g,--gen-pot}'[scan awk program and generate .po file on = stdout]' + '*'{-i+,--include}'[load source library]:library file:->script' + '*'{-l+,--load}'[load dynamic extension]:extension:->extension' + {-M,--bignum}'[select arbitrary-precision arithmetic on numbers]' + {-o-,--pretty-print=3D-}'[pretty-print awk program]::output = file:_files' + {-S,--sandbox}'[disable system(), redirections and dynamic = extensions]' + ) + ;; + (gawk3) + # one letter options are new in gawk4 + args=3D( ${args:#(|\*)(|\(*\))-[cCdEhLnNtOpPreV]*} ) + args+=3D( + '--gen-po[scan awk program and generate .po file on stdout]' + ) + ;; + (*) + # remove long options + args=3D( ${args:#*--*} ) +esac + +_arguments -S -s -C : $args && ret=3D0 + +# Complete files in . (current directory) and AWKPATH/AWKLIBPATH. +# Use different tag/description for files in . even if . is in AWKPATH. +_files_in_curdir_or_path() { + local expl pat1 pat2 + if [[ -n $6 ]]; then # $6 =3D 'so', 'dll' or '' + pat1=3D"-g *.$6" + pat2=3D"-g *.$6" + fi + if [[ $words[CURRENT] =3D=3D */* || $variant !=3D gawk* || \ + -n $opt_args[(I)(-c|--traditional|-P|--posix)] ]]; then + _wanted $2 expl $3 _files $pat1 && ret=3D0 + else + local prog=3D'BEGIN {print ENVIRON["'$1'"]}' + local -aU paths + # split AWKPATH into paths, and replace null element by '.'. + paths=3D( "${(@)${(@s.:.)$(_call_program get-awk-env \ + $words[1] ${(q)prog})}:/#%/.}" ) + if (( $paths[(I).] )); then + # If '.' is in paths, remove it; we will handle it separately + paths=3D( ${(@)paths:#.} ) + else + # If '.' is not in paths, we should not complete files in '.' + pat1=3D'-g *(-/)' + fi + if (( $#paths )); then + _alternative "${2}:${3}:_files ${(b)pat1}" \ + "${4}:${5}:_files -W paths ${(b)pat2}" && ret=3D0 + else + _wanted $2 expl $3 _files $pat1 && ret=3D0 + fi + fi +} + +case $state in + (script) + _files_in_curdir_or_path AWKPATH program-files 'program file' \ + library-files 'library in AWKPATH' + ;; + (extension) + local ext=3Dso + [[ $OSTYPE =3D=3D cygwin* ]] && ext=3Ddll + _files_in_curdir_or_path AWKLIBPATH extensions 'extension' \ + library-files 'extension in AWKLIBPATH' $ext + ;; +esac + +return ret --Apple-Mail=_60DF505F-24BF-4AD4-8E99-AB2EEA2B9E61 Content-Disposition: attachment; filename=_awk Content-Type: application/octet-stream; name="_awk" Content-Transfer-Encoding: 7bit #compdef awk gawk # For gawk ver.3 and 4, in addition to POSIX. # # gawk's options '-W ...' (such as '-W help') are not supported. # gawk3 has some synomyms for long options (e.g., --compat is a synomym # for --traditional). These are not supported either. # # 'gawk -f' will complete files in AWKPATH in addition to those in # the current directory. If this is annoying, you may try # zstyle ':completion:*:*:gawk:option-f-1:*' tag-order program-files local variant curcontext="$curcontext" state state_descr line ret=1 local -A opt_args local -a args _pick_variant -r variant gawk4='GNU Awk 4' gawk3='GNU Awk 3' posix --version args=( {-F+,--field-separator}'[define input field separator by extended regex]:extended regular expression:' '*'{-v+,--assign}'[assign values to variables]:assignment:' '(1)*'{-f+,--file}'[read program file]:program file:->script' '1: :_guard "^-*" "program text"' '*:input files:_files' ) case $variant in (gawk*) args+=( {-c,--traditional}'[run in compatibility mode]' '(- : *)'{-C,--copyright}'[print copyright info and exit]' {-d-,--dump-variables=-}'[print a sorted list of global variables]::output file:_files' {-e,--source}'[pass program text in arg]:program text:' '(1)'{-E+,--exec}'[like -f, but safer for CGI]:program file:->script' '(- : *)'{-h,--help}'[print usage message and exit]' {-L-,--lint=-}'[warn about dubious or non-portable constructs]::flag:((fatal\:"treat warnings as fatal error" invalid\:"warn only about thigs that are actually invalid"))' {-n,--non-decimal-data}'[auto-detect octal/hexadecimal values in input]' {-N,--use-lc-numeric}"[force use of locale's decimal point character]" {-O,--optimize}'[enable optimization]' {-p-,--profile=-}'[output profiling data to file]::output file:_files' {-P,--posix}'[run in strict POSIX mode]' {-r,--re-interval}'[enable interval expressions in regex matching]' {-t,--lint-old}'[warn about non-portable constructs]' '(- : *)'{-V,--version}'[print version info and exit]' ) ;| (gawk4) args+=( {-b,--characters-as-bytes}'[treat all input data as single-byte characters]' {-D-,--debug=-}'[enable debugging]::debugger command file:_files' {-g,--gen-pot}'[scan awk program and generate .po file on stdout]' '*'{-i+,--include}'[load source library]:library file:->script' '*'{-l+,--load}'[load dynamic extension]:extension:->extension' {-M,--bignum}'[select arbitrary-precision arithmetic on numbers]' {-o-,--pretty-print=-}'[pretty-print awk program]::output file:_files' {-S,--sandbox}'[disable system(), redirections and dynamic extensions]' ) ;; (gawk3) # one letter options are new in gawk4 args=( ${args:#(|\*)(|\(*\))-[cCdEhLnNtOpPreV]*} ) args+=( '--gen-po[scan awk program and generate .po file on stdout]' ) ;; (*) # remove long options args=( ${args:#*--*} ) esac _arguments -S -s -C : $args && ret=0 # Complete files in . (current directory) and AWKPATH/AWKLIBPATH. # Use different tag/description for files in . even if . is in AWKPATH. _files_in_curdir_or_path() { local expl pat1 pat2 if [[ -n $6 ]]; then # $6 = 'so', 'dll' or '' pat1="-g *.$6" pat2="-g *.$6" fi if [[ $words[CURRENT] == */* || $variant != gawk* || \ -n $opt_args[(I)(-c|--traditional|-P|--posix)] ]]; then _wanted $2 expl $3 _files $pat1 && ret=0 else local prog='BEGIN {print ENVIRON["'$1'"]}' local -aU paths # split AWKPATH into paths, and replace null element by '.'. paths=( "${(@)${(@s.:.)$(_call_program get-awk-env \ $words[1] ${(q)prog})}:/#%/.}" ) if (( $paths[(I).] )); then # If '.' is in paths, remove it; we will handle it separately paths=( ${(@)paths:#.} ) else # If '.' is not in paths, we should not complete files in '.' pat1='-g *(-/)' fi if (( $#paths )); then _alternative "${2}:${3}:_files ${(b)pat1}" \ "${4}:${5}:_files -W paths ${(b)pat2}" && ret=0 else _wanted $2 expl $3 _files $pat1 && ret=0 fi fi } case $state in (script) _files_in_curdir_or_path AWKPATH program-files 'program file' \ library-files 'library in AWKPATH' ;; (extension) local ext=so [[ $OSTYPE == cygwin* ]] && ext=dll _files_in_curdir_or_path AWKLIBPATH extensions 'extension' \ library-files 'extension in AWKLIBPATH' $ext ;; esac return ret --Apple-Mail=_60DF505F-24BF-4AD4-8E99-AB2EEA2B9E61--