zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Completion batch #4: New UNIX functions
@ 2018-01-04 17:48 dana
  2018-01-05 14:54 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: dana @ 2018-01-04 17:48 UTC (permalink / raw)
  To: zsh-workers

As the _cmd_variant stuff is percolating, here's another batch: These are all
new functions belonging to the 'Unix' group. None of them are particularly
complicated.

subl is the command-line interface to Sublime Text; the others are probably
self-explanatory. There are several more coreutils-style commands like this that
i intend to do too, but i haven't started on them just yet.

As always, let me know if you see anything i've done weird.

dana


diff --git a/Completion/Unix/Command/_fold b/Completion/Unix/Command/_fold
new file mode 100644
index 000000000..cccdb6c1e
--- /dev/null
+++ b/Completion/Unix/Command/_fold
@@ -0,0 +1,25 @@
+#compdef fold gfold
+
+local curcontext=$curcontext variant
+local -a context line state state_descr args
+local -A opt_args
+
+_pick_variant -r variant busybox=BusyBox gnu='Free Soft' unix --version
+
+args=(
+  '(-b --bytes)'{-b,--bytes}'[count bytes rather than columns]'
+  '(: -)--help[display help information]'
+  '(-s --spaces)'{-s,--spaces}'[fold on whitespace]'
+  '(-w --width)'{-w+,--width=}'[specify line width]:line width (columns or bytes)'
+  '(: -)--version[display version information]'
+  '*: :_files'
+)
+
+# Non-GNU variants don't support long options (except BusyBox's --help)
+if [[ $variant == *busybox* ]]; then # See also: _busybox
+  args=( ${args:#((#s)|*\))(\*|)--^help*} )
+elif [[ $variant != gnu ]]; then
+  args=( ${args:#((#s)|*\))(\*|)--*} )
+fi
+
+_arguments -s -S : $args

diff --git a/Completion/Unix/Command/_getopt b/Completion/Unix/Command/_getopt
new file mode 100644
index 000000000..f6e65f99b
--- /dev/null
+++ b/Completion/Unix/Command/_getopt
@@ -0,0 +1,30 @@
+#compdef getopt ggetopt
+
+local curcontext=$curcontext
+local -a context line state state_descr args aopts
+local -A opt_args
+
+# Note: BusyBox getopt is borrowed straight from util-linux, so they're
+# basically identical
+if _pick_variant busybox=BusyBox util-linux='getopt*enhanced' unix --version; then
+  args=(
+    '(-a --alternative)'{-a,--alternative}'[allow long options with single -]'
+    '(: -)'{-h,--help}'[display help information]'
+    '*'{-l+,--longoptions=}'[specify long options]:long options'
+    '(-n --name)'{-n+,--name=}'[specify program name]:program name'
+    '(1 -o --options)'{-o+,--options=}'[specify short options]:short options'
+    '(-q --quiet)'{-q,--quiet}'[suppress getopt(3) error messages]'
+    '(-Q --quiet-output)'{-Q,--quiet-output}'[suppress normal output]'
+    '(-s --shell)'{-s+,--shell=}'[specify shell quoting conventions]:shell:(bash csh tcsh sh)'
+    '(: -)'{-T,--test}'[test for enhanced getopt]'
+    '(-u --unquoted)'{-u,--unquoted}'[do not quote output]'
+    '(: -)'{-V,--version}'[display version information]'
+    '(-o --options)1: :_guard "^-*" "short options"'
+    '*:argument'
+  )
+  [[ -n $POSIXLY_CORRECT ]] && aopts+=( -A '-*' )
+else
+  args=( '1:short options' '*:argument' )
+fi
+
+_arguments -s -S $aopts : $args

diff --git a/Completion/Unix/Command/_install b/Completion/Unix/Command/_install
new file mode 100644
index 000000000..007f4bd27
--- /dev/null
+++ b/Completion/Unix/Command/_install
@@ -0,0 +1,111 @@
+#compdef install ginstall
+
+local curcontext=$curcontext lx
+local -a context line state state_descr common_args args tmp
+local -A opt_args val_args
+
+# These are *almost* common — non-GNU variants need to remove the long options
+common_args=(
+  '(--backup)-b[create backups of destination files]'
+  '(-C -c --compare)'{-C,--compare}'[copy files; do nothing if identical destination file exists]'
+  '(-C -c --compare)-c[copy files (default)]'
+  '(-d --directory)'{-d,--directory}'[create directories]'
+  '(-g --group)'{-g+,--group=}'[specify destination file group]: :_groups'
+  '(-m --mode)'{-m+,--mode=}'[specify destination file mode]: :_modes'
+  '(-o --owner)'{-o+,--owner=}'[specify destination file owner]: :_users'
+  '(-p --preserve-timestamps)'{-p,--preserve-timestamps}'[preserve modification times]'
+  '(-s --strip)'{-s,--strip}'[strip binaries]'
+  '(-v --verbose)'{-v,--verbose}'[output verbosely]'
+  '*: :_files'
+)
+
+if _pick_variant gnu='Free Soft' unix --version; then
+  # Hide Linux-specific options on non-Linux platforms
+  [[ $OSTYPE == linux* ]] || lx='!'
+  args+=(
+    $common_args
+    '(-b --backup)--backup=[create backup; optionally specify method]:: :->controls'
+    "${lx}--context=[like -Z, or specify SELinux security context to set]::SELinux security context"
+    '-D[create all leading destination path components]'
+    '(: -)--help[display help information]'
+    "${lx}--preserve-context[preserve SELinux security context]"
+    '--strip-program=[specify program used to strip binaries]:strip program:_files'
+    '(-S --suffix)'{-S+,--suffix=}'[specify backup suffix]:backup suffix'
+    '(-t --target-directory)'{-t+,--target-directory=}'[copy source to specified directory]: :_directories'
+    '(-T --no-target-directory)'{-T,--no-target-directory}'[treat destination as regular file]'
+    '(: -)--version[display version information]'
+    "${lx}-Z[set SELinux security context on destination files to default type]"
+  )
+
+else
+  args+=(
+    ${common_args##((#s)|*\))(\*|)--*}
+    '-B+[specify backup suffix for -b]:backup suffix'
+    '-f+[specify destination file flags]: :_flags'
+  )
+  [[ $OSTYPE == dragonfly* ]] && args+=(
+    '!-D+[no effect (compatibility with NetBSD)]: :_directories'
+    '-L+[use user/group database files from specified directory]: :_directories'
+    '-l[fall back to system files if user/group not found in -L directory]'
+  )
+  [[ $OSTYPE == netbsd* ]] && {
+    args+=(
+      '-a+[specify shell command to run on files after install]:shell command'
+      '-r[use temporary files to perform safe copy]'
+      '-S+[specify arguments to pass to strip program]:arguments to strip program'
+    )
+    # NetBSD has no -v for some reason
+    args=( ${args##((#s)|*\))(\*|)-v*} )
+  }
+  [[ $OSTYPE == openbsd* ]] && args+=(
+    '-D[create all leading destination path components]'
+    '-F[flush installed file contents to disk]'
+  )
+  [[ $OSTYPE == (darwin|dragonfly)* ]] && args+=(
+    '-M[disable use of mmap(2)]'
+  )
+  [[ $OSTYPE == (freebsd|netbsd)* ]] && args+=(
+    '-D+[specify destination directory used for metadata log]: :_directories'
+    '-h+[store digest in metadata log using specified method]: :->digests'
+    '(-C -c --compare)-l+[link files (rather than copy) using specified method]: :->linkflags'
+    '-M+[log mtree(8) metadata for installed files to specified file]:metadata log file:_files'
+    '+N+[use user/group database files from specified directory]: :_directories'
+    '-T+[specify mtree(8) tags to store in metadata log]:mtree(8) tags'
+    '-U[indicate that install is unprivileged]'
+  )
+  [[ $OSTYPE == netbsd* ]] || args+=(
+    '-S[use temporary files to perform safe copy]'
+  )
+fi
+
+_arguments -s -S : $args && return 0
+
+case $state in
+  controls)
+    tmp=(
+      {none,off}':never make backups'
+      {numbered,t}':make numbered backups'
+      {existing,nil}':make numbered backups if they already exist'
+      # 'never' actually means 'always'...
+      {simple,never}':make simple backups'
+    )
+    _describe -t controls 'version control method' tmp && return 0
+    ;;
+  digests)
+    tmp=( none md5 rmd160 sha1 sha256 sha512 )
+    [[ $OSTYPE == netbsd* ]] && tmp+=( sha384 )
+    _values 'digest method' $tmp && return 0
+    ;;
+  linkflags)
+    tmp=(
+      'h[hard links]'
+      's[symlinks]'
+      'm[mixed (hard links for files on same file system)]'
+      'a[symlinks use absolute path]'
+      'r[symlinks use relative path]'
+    )
+    _values -S '' 'link flags' $tmp && return 0
+    ;;
+esac
+
+return 1

diff --git a/Completion/Unix/Command/_mktemp b/Completion/Unix/Command/_mktemp
new file mode 100644
index 000000000..d08ca39a8
--- /dev/null
+++ b/Completion/Unix/Command/_mktemp
@@ -0,0 +1,44 @@
+#compdef mktemp gmktemp
+
+local curcontext=$curcontext variant
+local -a context line state state_descr args
+local -A opt_args
+
+_pick_variant -r variant busybox=BusyBox gnu='Free Soft' unix --version
+
+args=(
+  '(-d --directory)'{-d,--directory}'[make directory instead of file]'
+  '(: -)--help[display help information]'
+  '(-p --tmpdir)'{-p+,--tmpdir=}'[make relative to specified directory]: :_directories'
+  '(-q --quiet)'{-q,--quiet}'[suppress error messages]'
+  '--suffix=[append specified suffix to template]:template suffix'
+  '-t[interpret template as single path component relative to temp dir]'
+  '(-u --dry-run)'{-u,--dry-run}'[print file name only]'
+  '(: -)'{-V,--version}'[display version information]'
+  '1: :_guard "^-*" "template name"'
+)
+
+# Non-GNU variants don't support long options (except BusyBox's --help)
+if [[ $variant == *busybox* ]]; then # See also: _busybox
+  args=( ${args:#((#s)|*\))(\*|)--^help*} )
+elif [[ $variant != gnu ]]; then
+  args=( ${args:#((#s)|*\))(\*|)--*} )
+fi
+
+[[ $variant == gnu ]] || {
+  # BusyBox, OpenBSD, and Solaris have -p, but -t doesn't take an argument
+  if [[ $variant == *busybox* ]] || [[ $OSTYPE == (openbsd|solaris)* ]]; then
+    args=( ${args:#((#s)|*\))(\*|)-t*} )
+    args+=( '-t[generate template relative to temp dir]' )
+  # Dragonfly, FreeBSD, and Darwin take an argument to -t and support any number
+  # of template files
+  else
+    args=( ${args:#((#s)|*\))(1:*|(\*|)-t*)} )
+    args+=(
+      '-t[generate template relative to temp dir using specified prefix]:template prefix'
+      '*: :_guard "^-*" "template name"'
+    )
+  fi
+}
+
+_arguments -s -S : $args

diff --git a/Completion/Unix/Command/_subl b/Completion/Unix/Command/_subl
new file mode 100644
index 000000000..012309ad2
--- /dev/null
+++ b/Completion/Unix/Command/_subl
@@ -0,0 +1,17 @@
+#compdef subl
+
+local curcontext=$curcontext
+local -a context line state state_descr
+local -A opt_args
+
+_arguments -s -S : \
+  '(-a -n --add --new-window)'{-a,--add}'[add to current window]' \
+  '(-b --background)'{-b,--background}'[do not activate application]' \
+  '--command[run specified command]:command' \
+  '(: -)'{-h,--help}'[display help information]' \
+  '(-a -n --add --new-window)'{-n,--new-window}'[open new window]' \
+  '--project[load specified project]:project file:_files' \
+  '(-s --stay)'{-s,--stay}'[keep application activated]' \
+  '(: -)'{-v,--version}'[display version information]' \
+  '(-w --wait)'{-w,--wait}'[wait for files to close]' \
+  '*:file or directory:_files'


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Completion batch #4: New UNIX functions
  2018-01-04 17:48 [PATCH] Completion batch #4: New UNIX functions dana
@ 2018-01-05 14:54 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2018-01-05 14:54 UTC (permalink / raw)
  To: zsh-workers

dana wrote:
> new functions belonging to the 'Unix' group. None of them are particularly

Actually, I think sublimetext belongs under X. X is supposed to cover
anything that needs a GUI so stuff like acroread, netscape, matlab etc
are there. I've also called it _sublimetext rather than _subl which is
how things are normally named.

> As always, let me know if you see anything i've done weird.

I've tweaked the other functions to remove superfluous local statements
and correct the return status in one case. _arguments' local
requirements are unfortunately somewhat confusing:

If you don't use states (:-> syntax) no extra variables need to be
declared local.

If you do, you need to declare context, state and line local. state and
context are arrays and you should loop over them calling, _wanted -C
"$context[1]" as appropriate.

However, in the vast majority of cases, it is only ever possible for
state and context to have one element in them. You need something like
optional arguments for multiple simultaneous states to be an issue. In
this case, as a simplification, you can call _arguments -C and instead
of using context, $curcontext is modified directly with the context for
the one state. For this to work, you need to preserve the existing
value, hence this usage:
  local curcontext="$curcontext" state line

opt_args ought also to be declared local but we tend to be lax about
that.

state_descr is a relatively new addition and most functions predate it.
I tend not to bother.

With the return status, it is key to remember that if people unset the
prefix-needed style, _arguments is likely to add matches and return a
state so don't do: _arguments -options '*: :->state' && return
the common && ret=0 idiom works for this case along with an explicit
check that $state is set.

Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-01-05 14:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-04 17:48 [PATCH] Completion batch #4: New UNIX functions dana
2018-01-05 14:54 ` Oliver Kiddle

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).