zsh-workers
 help / Atom feed
* [PATCH] Completion: Use _pick_variant -b to detect builtins
@ 2019-04-22  2:17 Matthew Martin
  0 siblings, 0 replies; 1+ messages in thread
From: Matthew Martin @ 2019-04-22  2:17 UTC (permalink / raw)
  To: zsh-workers

Convert completers for loadable builtins in zsh/files to _pick_variant
-b to detect if the command is builtin. After this patch I think all
completers that should use _pick_variant -b do.

---
 Completion/Unix/Command/_chown |  97 ++++++++++++++++-----------
 Completion/Unix/Command/_ln    | 115 ++++++++++++++++-----------------
 Completion/Unix/Command/_mv    |  86 +++++++++++++-----------
 Completion/Unix/Command/_rm    |  86 +++++++++++++-----------
 Completion/Unix/Command/_rmdir |  28 +++++---
 5 files changed, 234 insertions(+), 178 deletions(-)

diff --git a/Completion/Unix/Command/_chown b/Completion/Unix/Command/_chown
index a97a0e0bf..8fde02096 100644
--- a/Completion/Unix/Command/_chown
+++ b/Completion/Unix/Command/_chown
@@ -1,43 +1,68 @@
 #compdef chown chgrp gchown=chown gchgrp=chgrp zf_chown=chown zf_chgrp=chgrp
 
-local curcontext="$curcontext" state line expl ret=1
+local curcontext="$curcontext" state line expl ret=1 variant
 local suf usr grp req deref pattern arg args
 
-if _pick_variant gnu=Free\ Soft unix --version; then
-  args=(
-    '(-c --changes -v --verbose)'{-c,--changes}'[report each change made]'
-    '(-c --changes -v --verbose)'{-v,--verbose}'[output info for every file processed]'
-    '(-h --no-dereference)--dereference[dereference symlinks]'
-    '(-h --no-dereference --dereference)'{-h,--no-dereference}'[operate on symlinks themselves]'
-    '(-f --silent --quiet)'{-f,--silent,--quiet}"[don't report errors]"
-    '--preserve-root[fail to operate recursively on /]'
-    '--reference=[copy ownership of specified file]:file:_files'
-    '(-R --recursive)'{-R,--recursive}'[change files and directories recursively]'
-    '(-H -L -P)-L[follow all symlinks]'
-    '(-H -L -P)-H[follow symlinks on the command line]'
-    '(-H -L -P)-P[do not follow symlinks (default)]'
-    '(- : *)--help[display help information]'
-    '(- : *)--version[display version information]'
-  )    
-  [[ $service = chown ]] &&
-      args+=( '--from=[restrict changes to files by current ownership]: :->owner' )
-else
-  args=(
-    "-h[operate on symlinks them self]"
-    '-R[change files and directories recursively]'
-    '(-H -L -P)-L[follow all symlinks]'
-    '(-H -L -P)-H[follow symlinks on the command line]'
-    '(-H -L -P)-P[do not follow symlinks (default)]'
-  )
-  for pattern arg in \
-    '(dragonfly|freebsd)*' "-x[don't traverse file systems]" \
-    '(darwin|dragonfly|(free|net)bsd|solaris)*' "-f[don't report errors]" \
-    '(darwin|dragonfly|(free|net)bsd)*' '-v[output info for every file processed]' \
-    'solaris2.<11->' '-s[owner and/or group are Windows SID strings]'
-  do
-    [[ $OSTYPE = $~pattern ]] && args+=( $arg )
-  done
-fi
+_pick_variant -r variant -b zsh gnu=Free\ Soft $OSTYPE --version
+case "$variant" in
+  gnu)
+    args=(
+      '(-c --changes -v --verbose)'{-c,--changes}'[report each change made]'
+      '(-c --changes -v --verbose)'{-v,--verbose}'[output info for every file processed]'
+      '(-h --no-dereference)--dereference[dereference symlinks]'
+      '(-h --no-dereference --dereference)'{-h,--no-dereference}'[operate on symlinks themselves]'
+      '(-f --silent --quiet)'{-f,--silent,--quiet}"[don't report errors]"
+      '--preserve-root[fail to operate recursively on /]'
+      '--reference=[copy ownership of specified file]:file:_files'
+      '(-R --recursive)'{-R,--recursive}'[change files and directories recursively]'
+      '(-H -L -P)-L[follow all symlinks]'
+      '(-H -L -P)-H[follow symlinks on the command line]'
+      '(-H -L -P)-P[do not follow symlinks (default)]'
+      '(- : *)--help[display help information]'
+      '(- : *)--version[display version information]'
+    )
+    [[ $service = chown ]] &&
+        args+=( '--from=[restrict changes to files by current ownership]: :->owner' )
+    ;;
+  *)
+    args=(
+      '-h[operate on symlinks them self]'
+      '-R[change files and directories recursively]'
+    )
+    ;|
+  zsh)
+    args+=(
+      '-s[enable paranoid behavior]'
+    )
+    ;;
+  *)
+    args+=(
+      '(-H -L -P)-L[follow all symlinks]'
+      '(-H -L -P)-H[follow symlinks on the command line]'
+      '(-H -L -P)-P[do not follow symlinks (default)]'
+    )
+    ;|
+  dragonfly*|freebsd*)
+    args+=(
+      "-x[don't traverse file systems]"
+    )
+    ;|
+  darwin*|dragonfly*|freebsd*|netbsd*|solaris*)
+    args+=(
+      "-f[don't report errors]"
+    )
+    ;|
+  darwin*|dragonfly*|freebsd*|netbsd*)
+    args+=(
+       '-v[output info for every file processed]'
+    )
+    ;|
+  solaris2.<11->)
+    args+=(
+      '-s[owner and/or group are Windows SID strings]'
+    )
+    ;;
+esac
 
 (( $+words[(r)--reference*] )) || args+=( '(--reference)1: :->owner' )
 _arguments -C -s "$args[@]" '*: :->files' && ret=0
diff --git a/Completion/Unix/Command/_ln b/Completion/Unix/Command/_ln
index 3c1dcac76..339d327fe 100644
--- a/Completion/Unix/Command/_ln
+++ b/Completion/Unix/Command/_ln
@@ -1,6 +1,6 @@
 #compdef ln gln zf_ln
 
-local curcontext="$curcontext" state line ret=1
+local curcontext="$curcontext" state line ret=1 variant
 local -A opt_args
 
 local -a args opts
@@ -9,67 +9,66 @@ args=(
   '-s[create symbolic links instead of hard links]'
 )
 
-local variant
-_pick_variant -r variant gnu=gnu unix --help
-if [[ $variant == gnu ]]; then
-  opts=(-S)
-  args=(
-    '(-b --backup)-b[create a backup of each existing destination file]' \
-    '(-b --backup)--backup=[create a backup of each existing destination file]::method:((
-        none\:"never create backups"
-        off\:"never create backups"
-        numbered\:"create numbered backup"
-        t\:"create numbered backup"
-        existing\:"same as numbered if numbered backups exist, otherwise same as simple"
-        nil\:"same as numbered if numbered backups exist, otherwise same as simple"
-        simple\:"always create simple backups"
-        never\:"always create simple backups"))'
-    '(-d -F --directory)'{-d,-F,--directory}'[allow the superuser to attempt to hard link directories]'
-    '(-f --force)'{-f,--force}'[remove existing destination files]'
-    '(-i --interactive)'{-i,--interactive}'[prompt before removing destination files]'
-    '(-L --logical)'{-L,--logical}'[create hard links to symbolic link references]'
-    '(-n --no-dereference)'{-n,--no-dereference}'[treat destination symbolic link to a directory as if it were a normal file]'
-    '(-P --physical)'{-P,--physical}'[create hard links directly to symbolic links]'
-    '(-r --relative)'{-r,--relative}'[create symbolic links relative to link location]'
-    '(-s --symbolic)'{-s,--symbolic}'[create symbolic links instead of hard links]'
-    '(-S --suffix)'{-S,--suffix=}'[override default backup suffix]:suffix'
-    '(-t --target-directory)'{-t,--target-directory=}'[specify directory in which to create the links]: :_directories'
-    '(-T --no-target-directory)'{-T,--no-target-directory}'[treat destination as a normal file]'
-    '(-v --verbose)'{-v,--verbose}'[print name of each linked file]'
-    '--help[display usage information and exit]'
-    '--version[display version information and exit]')
-elif (( ${+builtins[ln]} )); then
-  args+=(
-    '-d[attempt to hard link directories]'
-    {-h,-n}'[do not dereference destination]'
-    '(-f)-i[prompt before removing destination files]')
-else
-  case $OSTYPE in
-    darwin*|dragonfly*|freebsd*|netbsd*|openbsd*)
-      args+=(
-        {-h,-n}'[do not dereference destination]'
-      )
+_pick_variant -r variant -b zsh gnu=gnu $OSTYPE --help
+case $variant; in
+  gnu)
+    opts=(-S)
+    args=(
+      '(-b --backup)-b[create a backup of each existing destination file]' \
+      '(-b --backup)--backup=[create a backup of each existing destination file]::method:((
+          none\:"never create backups"
+          off\:"never create backups"
+          numbered\:"create numbered backup"
+          t\:"create numbered backup"
+          existing\:"same as numbered if numbered backups exist, otherwise same as simple"
+          nil\:"same as numbered if numbered backups exist, otherwise same as simple"
+          simple\:"always create simple backups"
+          never\:"always create simple backups"))'
+      '(-d -F --directory)'{-d,-F,--directory}'[allow the superuser to attempt to hard link directories]'
+      '(-f --force)'{-f,--force}'[remove existing destination files]'
+      '(-i --interactive)'{-i,--interactive}'[prompt before removing destination files]'
+      '(-L --logical)'{-L,--logical}'[create hard links to symbolic link references]'
+      '(-n --no-dereference)'{-n,--no-dereference}'[treat destination symbolic link to a directory as if it were a normal file]'
+      '(-P --physical)'{-P,--physical}'[create hard links directly to symbolic links]'
+      '(-r --relative)'{-r,--relative}'[create symbolic links relative to link location]'
+      '(-s --symbolic)'{-s,--symbolic}'[create symbolic links instead of hard links]'
+      '(-S --suffix)'{-S,--suffix=}'[override default backup suffix]:suffix'
+      '(-t --target-directory)'{-t,--target-directory=}'[specify directory in which to create the links]: :_directories'
+      '(-T --no-target-directory)'{-T,--no-target-directory}'[treat destination as a normal file]'
+      '(-v --verbose)'{-v,--verbose}'[print name of each linked file]'
+      '--help[display usage information and exit]'
+      '--version[display version information and exit]')
+    ;;
+  zsh)
+    args+=(
+      '-d[attempt to hard link directories]'
+      {-h,-n}'[do not dereference destination]'
+      '(-f)-i[prompt before removing destination files]')
+    ;;
+  darwin*|dragonfly*|freebsd*|netbsd*|openbsd*)
+    args+=(
+      {-h,-n}'[do not dereference destination]'
+    )
     ;|
-    darwin*|dragonfly*|freebsd*|netbsd*)
-      args+=(
-        '-F[remove existing destination directories]'
-        '(-f)-i[prompt before removing destination files]'
-        '-v[print name of each linked file]'
-      )
+  darwin*|dragonfly*|freebsd*|netbsd*)
+    args+=(
+      '-F[remove existing destination directories]'
+      '(-f)-i[prompt before removing destination files]'
+      '-v[print name of each linked file]'
+    )
     ;|
-    dragonfly*|freebsd*|netbsd*|openbsd*)
-      args+=(
-        '(-L)-P[create hard links directly to symbolic links]'
-        '(-P)-L[create hard links to symbolic link references]'
-      )
+  dragonfly*|freebsd*|netbsd*|openbsd*)
+    args+=(
+      '(-L)-P[create hard links directly to symbolic links]'
+      '(-P)-L[create hard links to symbolic link references]'
+    )
     ;|
-    dragonfly*|freebsd*)
-      args+=(
-        "-w[warn if source of a symbolic link doesn't currently exist]"
-      )
+  dragonfly*|freebsd*)
+    args+=(
+      "-w[warn if source of a symbolic link doesn't currently exist]"
+    )
     ;;
-  esac
-fi
+esac
 
 _arguments -C -s $opts : \
   $args \
diff --git a/Completion/Unix/Command/_mv b/Completion/Unix/Command/_mv
index 3592efee6..2f0f844d4 100644
--- a/Completion/Unix/Command/_mv
+++ b/Completion/Unix/Command/_mv
@@ -1,42 +1,54 @@
 #compdef mv gmv
 
-local args
-if _pick_variant gnu=GNU unix --version; then
-  args=(
-    '(-b --backup -n --no-clobber)--backup=[make a backup of each existing destination file]: : _values "backup type"
-      {none,off}"[never make backups]"
-      {numbered,t}"[make numbered backups]"
-      {existing,nil}"[numbered backups, if they already exist]"
-      {simple,never}"[always make simple backups]"'
-    '(-b --backup -n --no-clobber)-b[make a backup of each existing destination file]'
-    '(-f --force -i --interactive -n --no-clobber)'{-f,--force}"[don't prompt before overwriting]"
-    '(-f --force -i --interactive -n --no-clobber)'{-i,--interactive}'[prompt before overwriting existing file]'
-    '--strip-trailing-slashes[remove any trailing slashes from each source argument]'
-    '(-S --suffix)'{-S+,--suffix=}'[specify the backup suffix]:backup suffix [~]'
-    '(-t --target-directory)'{-t+,--target-directory=}'[move all source arguments into specified directory]:directory:_directories'
-    '(-T --no-target-directory)'{-T,--no-target-directory}'[treat final argument as a normal file]'
-    '(-u --update)'{-u,--update}'[move only when destination file is older or missing]'
-    '(-v --verbose)'{-v,--verbose}'[show file names after they are moved]'
-    '(- *)--help[display usage information]'
-    '(- *)--version[display version information]'
-  )
-  [[ $OSTYPE = linux* ]] && args+=(
-    '(-Z --context)'{-Z,--context}'[set SELinux security context of destination file to default type]'
-  )
-else
-  local pattern arg
-  args=(
-    "(-i -n)-f[don't prompt before overwriting]"
-    '(-f -n)-i[prompt before overwriting existing file]'
-  )
-  for pattern arg in \
-    '(darwin|dragonfly|freebsd)*' "(-f -i)-n[don't overwrite existing file]" \
-    '(darwin|dragonfly|*bsd)*' '-v[show file names after they are moved]' \
-    '(dragonfly|freebsd)*' "-h[if target is a symlink to a directory, don't follow it]"
-  do
-    [[ $OSTYPE = $~pattern ]] && args+=( $arg )
-  done
-fi
+local args variant
+
+_pick_variant -r variant -b zsh gnu=GNU $OSTYPE --version
+case $variant; in
+  gnu)
+    args=(
+      '(-b --backup -n --no-clobber)--backup=[make a backup of each existing destination file]: : _values "backup type"
+        {none,off}"[never make backups]"
+        {numbered,t}"[make numbered backups]"
+        {existing,nil}"[numbered backups, if they already exist]"
+        {simple,never}"[always make simple backups]"'
+      '(-b --backup -n --no-clobber)-b[make a backup of each existing destination file]'
+      '(-f --force -i --interactive -n --no-clobber)'{-f,--force}"[don't prompt before overwriting]"
+      '(-f --force -i --interactive -n --no-clobber)'{-i,--interactive}'[prompt before overwriting existing file]'
+      '--strip-trailing-slashes[remove any trailing slashes from each source argument]'
+      '(-S --suffix)'{-S+,--suffix=}'[specify the backup suffix]:backup suffix [~]'
+      '(-t --target-directory)'{-t+,--target-directory=}'[move all source arguments into specified directory]:directory:_directories'
+      '(-T --no-target-directory)'{-T,--no-target-directory}'[treat final argument as a normal file]'
+      '(-u --update)'{-u,--update}'[move only when destination file is older or missing]'
+      '(-v --verbose)'{-v,--verbose}'[show file names after they are moved]'
+      '(- *)--help[display usage information]'
+      '(- *)--version[display version information]'
+    )
+    [[ $OSTYPE = linux* ]] && args+=(
+      '(-Z --context)'{-Z,--context}'[set SELinux security context of destination file to default type]'
+    )
+    ;;
+  *)
+    args=(
+      "(-i -n)-f[don't prompt before overwriting]"
+      '(-f -n)-i[prompt before overwriting existing file]'
+    )
+    ;|
+  darwin*|dragonfly*|freebsd*)
+    args+=(
+      "(-f -i)-n[don't overwrite existing file]"
+    )
+    ;|
+  darwin*|dragonfly*|*bsd*)
+    args+=(
+      '-v[show file names after they are moved]'
+    )
+    ;|
+  dragonfly*|freebsd*)
+    args+=(
+      "-h[if target is a symlink to a directory, don't follow it]"
+    )
+    ;;
+esac
 
 _arguments -s -S $args \
   '*:file:_files'
diff --git a/Completion/Unix/Command/_rm b/Completion/Unix/Command/_rm
index aa24a3fd6..dfd3a394a 100644
--- a/Completion/Unix/Command/_rm
+++ b/Completion/Unix/Command/_rm
@@ -1,5 +1,6 @@
 #compdef rm grm zf_rm
 
+local variant
 declare -a opts args
 args=(
   '(-f --force)'{-f,--force}'[ignore nonexistent files, never prompt]'
@@ -7,47 +8,58 @@ args=(
   '(-r -R --recursive)'{-r,-R,--recursive}'[remove directories and their contents recursively]'
   '*:: :->file'
 )
-if _pick_variant gnu=gnu unix --help; then
-  opts+=(-S)
-  args+=(
-    '(-i --interactive)-I[prompt when removing many files]'
-    '(-i -I)--interactive=-[prompt under given condition (defaulting to always)]::when:((once\:"prompt when removing many files"
-                                                                                         always\:"prompt before every removal"))'
-    '--one-file-system[stay within filesystems of files given as arguments]'
-    '(                   --preserve-root)--no-preserve-root[do not treat / specially]'
-    '(--no-preserve-root                )--preserve-root[do not remove / (default)]'
-    '(-d --dir)'{-d,--dir}'[remove directories as well]'
-    '(-v --verbose)'{-v,--verbose}'[explain what is being done]'
-    '(- *)--help[display help message and exit]'
-    '(- *)--version[output version information and exit]'
-  )
-else
-  args=(${args:#*)--*\[*})
-  case $OSTYPE in
-    darwin*|dragonfly*|freebsd*|netbsd*|openbsd*)
-      args+=(
-        '-d[remove directories as well]'
-        '-P[overwrite files before deleting them]'
-        '-v[explain what is being done]'
-      )
+_pick_variant -r variant -b zsh gnu=gnu $OSTYPE --help
+case $variant; in
+  gnu)
+    opts+=(-S)
+    args+=(
+      '(-i --interactive)-I[prompt when removing many files]'
+      '(-i -I)--interactive=-[prompt under given condition (defaulting to always)]::when:((once\:"prompt when removing many files"
+                                                                                           always\:"prompt before every removal"))'
+      '--one-file-system[stay within filesystems of files given as arguments]'
+      '(                   --preserve-root)--no-preserve-root[do not treat / specially]'
+      '(--no-preserve-root                )--preserve-root[do not remove / (default)]'
+      '(-d --dir)'{-d,--dir}'[remove directories as well]'
+      '(-v --verbose)'{-v,--verbose}'[explain what is being done]'
+      '(- *)--help[display help message and exit]'
+      '(- *)--version[output version information and exit]'
+    )
+    ;;
+  *)
+    args=(${args:#*)--*\[*})
+    ;|
+  darwin*|dragonfly*|freebsd*|netbsd*|openbsd*|zsh)
+    args+=(
+      '-d[remove directories as well]'
+    )
     ;|
-    darwin*|dragonfly*|freebsd*|netbsd*)
-      args+=(
-        '-W[attempt to undelete named files]'
-      )
+  zsh)
+    args+=(
+      '-s[enable paranoid behavior]'
+    )
+    ;;
+  darwin*|dragonfly*|freebsd*|netbsd*|openbsd*)
+    args+=(
+      '-P[overwrite files before deleting them]'
+      '-v[explain what is being done]'
+    )
     ;|
-    dragonfly*|freebsd*|netbsd*)
-      args+=(
-        "-x[don't cross file systems when removing a hierarchy]"
-      )
+  darwin*|dragonfly*|freebsd*|netbsd*)
+    args+=(
+      '-W[attempt to undelete named files]'
+    )
     ;|
-    dragonfly*|freebsd*)
-      args+=(
-        '(-i)-I[prompt when removing many files]'
-      )
+  dragonfly*|freebsd*|netbsd*)
+    args+=(
+      "-x[don't cross file systems when removing a hierarchy]"
+    )
+    ;|
+  dragonfly*|freebsd*)
+    args+=(
+      '(-i)-I[prompt when removing many files]'
+    )
     ;;
-  esac
-fi
+esac
 
 local curcontext=$curcontext state line ret=1
 declare -A opt_args
diff --git a/Completion/Unix/Command/_rmdir b/Completion/Unix/Command/_rmdir
index 0f5e16912..2ae0fd9b2 100644
--- a/Completion/Unix/Command/_rmdir
+++ b/Completion/Unix/Command/_rmdir
@@ -1,22 +1,30 @@
 #compdef rmdir grmdir
 
+local variant
 local -a args
 
 args=(
   '(-p --parents)'{-p,--parents}'[remove each component of the specified paths]'
 )
 
-if _pick_variant gnu=GNU unix --version; then
-  args+=(
-    '--ignore-fail-on-non-empty[ignore failure if directory is non-empty]'
-    '(-v --verbose)'{-v,--verbose}'[be verbose]'
-  )
-else
-  args=(${args:#*\)--*})
-  if [[ $OSTYPE == (dragonfly|freebsd)* ]]; then
+_pick_variant -r variant -b zsh gnu=GNU $OSTYPE --version
+case $variant; in
+  gnu)
+    args+=(
+      '--ignore-fail-on-non-empty[ignore failure if directory is non-empty]'
+      '(-v --verbose)'{-v,--verbose}'[be verbose]'
+    )
+    ;;
+  zsh)
+    args=()
+    ;;
+  *)
+    args=(${args:#*\)--*})
+    ;|
+  dragonfly*|freebsd*)
     args+=('-v[be verbose]')
-  fi
-fi
+    ;;
+esac
 
 _arguments -s -S -A '-*' \
   $args \
-- 
2.21.0


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

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-22  2:17 [PATCH] Completion: Use _pick_variant -b to detect builtins Matthew Martin

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/ public-inbox