zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun. T" <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: [PATCH] new completions for csplit, pr, ptx, truncate
Date: Mon, 31 Jan 2022 23:32:54 +0900	[thread overview]
Message-ID: <4439FFCD-2139-4DFA-B7A1-C7F06BD4230B@kba.biglobe.ne.jp> (raw)

These commands are in GNU coreutils, but there are non-GNU variants also.

pr accepts -2 -3 -4 ... for specifying number of columns.
The _pr below offers only -2 and -3, but I think it's enough.
pr also accepts +page_number to start printing from the specified page.
It is taken care of before calling _arguments.

csplit, pr and ptx accept a single '-' as a file name (to read from stdin).
For non-GNU variant (i.e., for BSD variant) we need to use
_argument -A '-?*'   (or -A '[-+]?*' for pr)
to stop completing options for 'csplit - -<TAB>'.



diff --git a/Completion/Unix/Command/_csplit b/Completion/Unix/Command/_csplit
new file mode 100644
index 000000000..5f72232bb
--- /dev/null
+++ b/Completion/Unix/Command/_csplit
@@ -0,0 +1,51 @@
+#compdef csplit
+
+local curcontext=$curcontext cnt_info ret=1
+local -a state state_descr line specs optA
+typeset -A opt_args
+
+# common specs
+specs=(
+  '(hv -f --prefix)'{-f+,--prefix=}'[specify prefix for output file names]:prefix [xx]: '
+  '(hv -n --digits -b --suffix-format)'{-n+,--digits=}'[specify number of digits in output file names]:number [2]: '
+  '(hv -k --keep-files)'{-k,--keep-files}'[do not remove output files on errors]'
+  '(hv -s --quiet --silent)'{-s,--quiet,--silent}'[do not print counts of output file sizes]'
+  '(hv)1: :_files'
+  '(hv)*: :->patterns'
+)
+
+if _pick_variant gnu=GNU unix --version; then
+  # GNU coreutils 8.32
+  specs+=(
+    '(hv -b --suffix-format -n --digits)'{-b+,--suffix-format=}'[specify format for numbers in output file names]:format [%%02d]: '
+    '(hv)--suppress-matched[suppress the lines matching the pattern]'
+    '(hv -z --elide-empty)'{-z,--elide-empty-files}'[remove empty output files]'
+    + hv
+    '(: * -)--help[display help and exit]'
+    '(: * -)--version[output version information and exit]'
+  )
+  cnt_info="(integer or '*')"
+else
+  # POSIX ({Free,Open}BSD, DragonFly, macOS)
+  specs=( ${specs:#(|*\))--*} )  # remove long options
+  optA=( -A '-?*' )  # a single '-' is a valid file name (stdin)
+fi
+
+_arguments -C -s -S $optA : $specs && ret=0
+
+case $state in
+  patterns)
+    if compset -P '(/?*/|%?*%)'; then
+      _message '[+|-]offset' && ret=0
+    elif compset -P '[/%]'; then
+      _message 'regex' && ret=0
+    elif compset -P '(|\\){'; then
+      _message "count $cnt_info" && ret=0
+    elif compset -P '[0-9]*'; then
+      _message 'line number' && ret=0
+    elif [[ ${words[CURRENT]} != -* ]] then
+      _message "line_number, '/regex/[offset]', '%%regex%%[offset]', or '{count}'" && ret=0
+    fi
+esac
+
+return ret
diff --git a/Completion/Unix/Command/_pr b/Completion/Unix/Command/_pr
new file mode 100644
index 000000000..2aeeb13b3
--- /dev/null
+++ b/Completion/Unix/Command/_pr
@@ -0,0 +1,103 @@
+#compdef pr
+
+local curcontext=$curcontext variant msg ret=1
+local -a state state_descr line specs optA
+typeset -A opt_args
+
+# take care of '+FIRST_PAGE[:LAST_PAGE]' (GNU) or '+FIRST_PAGE' (POSIX)
+if _pick_variant -r variant gnu=GNU $OSTYPE --version; then
+  msg='FIRST_PAGE[:LAST_PAGE]'
+else
+  msg='first page'
+fi
+
+if [[ $words[CURRENT] = +* ]]; then
+  _message "$msg" && return
+fi
+
+if (( ! ${words[(I)+[0-9]*]} )); then
+  # if +number is not on the command line
+  specs=( '(hv)--pages=[specify first and last page numbers]: : _message $msg' )
+fi
+
+# common specs
+specs+=(
+  '(hv -a --across)'{-a,--across}'[with multi-column output, print columns across rather than down]'
+  '(hv -d --double-space)'{-d,--double-space}'[double space the output]'
+  '(hv -e --expand-tabs)'{-e-,--expand-tabs=-}'[expand tab (or specified char) with specified number of spaces]::number of spaces [8]:->char_number'
+  '(hv -h --header -t --omit-header)'{-h+,--header=}'[specify text used in header]:header: '
+  '(hv -i --output-tabs)'{-i-,--output-tabs=-}'[replace specified number of spaces with tab (or specified char)]::number of spaces [8]:->char_number'
+  '(hv -l --length)'{-l+,--length=}'[specify the page length]:number of lines [66]: '
+  '(hv -m --merge)'{-m,--merge}'[print all files in parallel, one in each column]'
+  '(hv -n --number-lines)'{-n-,--number-lines=-}'[number lines with specified separator and width]::number of digits [5]:->char_number'
+  '(hv -o --indent)'{-o+,--indent=}'[specify left margin]:margin [0]: '
+  '(hv -r -no-file-warnings)'{-r,--no-file-warnings}'[omit warning when a file cannot be opened]'
+  '(hv -s --separator)'{-s-,--separator=-}'[specify column separator character]:character [tab]: '
+  '(hv -t --omit-header -h --header)'{-t,--omit-header}'[omit page headers and trailers]'
+  '(hv -w --width)'{-w+,--width=}'[specify page width for multi-column output]:number of characters [72]: '
+  '(hv)*: :_files'
+)
+# XXX: pr accepts -2 -3 -4 ... for specifying the number of columns.
+#      Here we offer only -2 and -3, and do so only if there is no
+#      -2 -3 -4 ... or --columns on the command line.
+if (( ! ${words[(I)-([0-9]##*|-columns*)]} )); then
+  specs+=( {-2,-3}'[specify number of columns]' )
+fi
+
+if [[ $variant = gnu ]]; then
+  # GNU coreutils 8.32
+  specs+=(
+    '(hv -c --show-control-chars)'{-c,--show-control-chars}'[use hat (^G) and octal backslash notation]'
+    '(hv -D --date-format)'{-D+,--date-format=}'[specify format for the header date]: :_date_formats'
+    '(hv -f -F --form-feed)'{-f,-F,--form-feed}'[use form feeds instead of newlines to separate pages]'
+    '(hv -J --join-lines)'{-J,--join-lines}'[merge full lines in multi-column output]'
+    '(hv -N --first-line-number)'{-N+,--first-line-number=}'[specify the line number of the 1st line]:number: '
+    '(hv -S --sep-string)'{-S-,--sep-string=-}'[specify column separator string]:string: '
+    '(hv -T --omit-pagination)'{-T,--omit-pagination}'[omit page headers and trailers, eliminate any pagination]'
+    '(hv -v --show-nonprinting)'{-v,--show-nonprinting}'[use octal backslash notation]'
+    '(hv -W --page-width)'{-W+,--page-width=}'[specify page width always]:number of characters [72]: '
+  )
+  if (( ! ${words[(I)-[0-9]##*]} )); then
+    # if -2 -3 -4 ... are not on the command line
+    specs+=(
+      '(hv)--columns=[specify number of columns]:number of columns: '
+      + hv
+      '(- *)--help[display help and exit]'
+      '(- *)--version[output version information and exit]'
+    )
+  fi
+else
+  specs=( ${specs:#(|*\))--*} )    # remove long options
+  case $variant in
+    freebsd*|dragonfly*|darwin*|netbsd*)
+      specs+=(
+	'(-f)-F[use form feeds instead of newlines to separate pages]'
+	'(-F)-f[same as -F but pause before the 1st page if stdout is terminal]'
+	'-p[pause before each page if stdout is terminal]'
+      )
+      ;|
+    freebsd*|dragonfly*|darwin*)
+      specs+=( '-L+[specify locale to use]: :_locales' )
+      ;;
+    openbsd*)
+      specs+=( '(-f -F)'{-f,-F}'[use form feeds instead of newlines to separate pages]' )
+      ;;
+  esac
+  optA=( -A '[-+]?*' )  # a single '-' is a valid file name (stdin)
+fi
+
+_arguments -C -s -S $optA : $specs && ret=0
+
+case $state in
+  char_number)
+    # argument for option -e (and -i, -n) can be -e. -e10 or -e.10
+    # where . is any non-digit character
+    if compset -p 1; then
+      _message "$state_descr" && ret=0
+    else
+      _message "a character [tab] (optional), and $state_descr" && ret=0
+    fi
+    ;;
+esac
+
+return ret
diff --git a/Completion/Unix/Command/_ptx b/Completion/Unix/Command/_ptx
new file mode 100644
index 000000000..12f1d2c9a
--- /dev/null
+++ b/Completion/Unix/Command/_ptx
@@ -0,0 +1,54 @@
+#compdef ptx
+
+local -a specs optA
+
+# common specs
+specs=(
+  '(hv -b --break-file)'{-b+,--break-file=}'[use characters in specified file as word separators]:break file:_files'
+  '(hv -f --ignore-case)'{-f,--ignore-case}'[fold lower case to upper case for sorting]'
+  '(hv -g --gap-size)'{-g+,--gap-size=}'[specify gap size between output fields]:number of chars [3]: '
+  '(hv -i --ignore-file)'{-i+,--ignore-file=}'[ignore keywords listed in specified file]:ignore file:_files'
+  '(hv -o --only-file)'{-o+,--only-file=}'[use only the keywords listed in specified file]:only file:_files'
+  '(hv -r --references)'{-r,--references}'[first field of each line is a reference]'
+  '(hv -w --width)'{-w+,--width=}'[specify page width, reference excluded]:number of characters [72]: '
+)
+
+if _pick_variant gnu=GNU unix --version; then
+  # GNU coreutils 8.32
+  specs+=(
+    '(hv -A --auto-reference)'{-A,--auto-reference}'[output automatically generated references]'
+    '(hv -G --traditional)'{-G,--traditional}"[behave more like System V 'ptx']"
+    '(hv -F --flag-truncation)'{-F+,--flag-truncation=}'[specify string for flagging line truncations]:string [/]: '
+    '(hv -M --macro-name)'{-M+,--macro-name=}"[specify macro name to use instead of 'xx']:macro name: "
+    '(hv)-O[generate output as roff directives]'
+    '(hv -R --right-side-refs)'{-R,--right-side-refs}'[put references at right, not counted in -w]'
+    '(hv -S --sentence-regexp)'{-S+,--sentence-regexp=}'[specify regexp for end of lines/sentences]:regexp: '
+    '(hv)-T[generate output as TeX directives]'
+    '(hv -W --word-regexp -b --break-file)'{-W+,--word-regexp=}'[specify regexp to match each keyword]:regexp: '
+    '(hv)--format=[specify the output format]:format:(roff tex)'
+    !{-t,--typeset-mode}'[not implemented]'
+    + hv
+    '(: * -)--help[display help and exit]'
+    '(: * -)--version[output version information and exit]'
+  )
+  if (( $words[(I)(-G|--traditional)] )); then
+    specs+=( + arg '1:input file:_files'  '2:output file:_files' )
+  else
+    specs+=( + arg '(-G --traditional)*:input file:_files' )
+  fi
+else
+  # The only non-GNU implementation I can find is the one in
+  # heirloom-doctools. FreeBSD has a package for this.
+  specs=( ${specs:#(|*\))--*} )    # remove long options
+  # remove '+' from -b+ -g+ -i+ -o+ -w+
+  local MATCH MBEGIN MEND
+  specs=( ${specs/(#m)-[bgiow]+/$MATCH[1,-2]} )
+  specs+=(
+    '-t[prepare output for typesetter]'
+    '1:input file:_files'
+    '2:output file:_files'
+  )
+  optA=( -A '-?*' )  # a single '-' is a valid file name (stdin)
+fi
+
+_arguments -s -S $optA : $specs
diff --git a/Completion/Unix/Command/_truncate b/Completion/Unix/Command/_truncate
new file mode 100644
index 000000000..117be9702
--- /dev/null
+++ b/Completion/Unix/Command/_truncate
@@ -0,0 +1,69 @@
+#compdef truncate
+
+local curcontext=$curcontext variant rs ret=1
+local -a state state_descr line specs optA
+typeset -A opt_args
+
+_pick_variant -r variant gnu=GNU $OSTYPE --version
+[[ $variant != gnu ]] && rs='-r -s' # -r/-s mutually exclusive
+
+# common specs
+specs=(
+  '(hv -c --no-create)'{-c,--no-create}'[do not create any files]'
+  "(hv $rs -r --reference)"{-r+,--reference=}'[base size on the specified file]:reference file:_files'
+  "(hv $rs -s --size)"{-s+,--size=}'[set or adjust the file size by specified bytes]:size:->size'
+  '(hv)*: :_files'
+)
+
+case $variant in
+  gnu) # GNU coreutils 8.32
+    specs+=(
+      '(hv -o --io-blocks)'{-o,--io-blocks}'[treat the specified size as number of IO blocks instead of bytes]'
+      + 'hv'
+      '(- *)--help[display help and exit]'
+      '(- *)--version[output version information and exit]'
+    )
+    ;;
+  *) # FreeBSD/DragonFly
+    specs=( ${specs:#(|*\))--*} )    # remove long options
+    optA=( -A '-*' )
+    ;;
+esac
+
+_arguments -C -s -S : $specs && ret=0
+
+case $state in
+  size)
+    local unit=bytes
+    (( ${#opt_args[(I)(-o|--io-blocks)]} )) && unit=blocks
+    local -a suffix=( K:1024 M G T )
+    local -a prefix=( '+:extend by' '-:reduce by' )
+    local prefix_char='[-+]'
+    case $variant in
+      gnu|freebsd*)
+	prefix+=( '/:round down to multiple of' '%:round up to multiple of' )
+	;|
+      gnu)
+	suffix=( K:1024 KB:1000 {M,G,T,P,E,Z,Y}{,B} )
+	prefix+=( '<:at most' '>:at least' )
+	prefix_char='([-+/%]|\\[<>])'
+	;;
+      freebsd*)
+	prefix_char='[-+/%]'
+	;;
+    esac
+    local -a numbers=( _numbers -u $unit size $suffix )
+
+    if compset -P "$prefix_char"; then
+      $numbers && ret=0
+    elif (( ${#opt_args[(I)(-r|--reference)]} )); then
+      # prefix is required if the reference file is given
+      _describe -t 'prefixes' 'prefix' prefix && ret=0
+    else
+      _alternative "prefixes:prefix:((${(@q)prefix}))" \
+      		   "sizes: :$numbers" && ret=0
+    fi
+  ;;
+esac
+
+return ret





                 reply	other threads:[~2022-01-31 14:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4439FFCD-2139-4DFA-B7A1-C7F06BD4230B@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).