Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: complete options in _chown



This makes _chown also complete its various options using _arguments.
As with _chmod, I've only tested with Solaris and GNU so let me know if
chown or chgrp --version isn't safe on any other system you might have
access to. Or if it has options other than -f -h and -R.

Oliver

Index: Completion/Unix/Command/_chown
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_chown,v
retrieving revision 1.7
diff -u -r1.7 _chown
--- Completion/Unix/Command/_chown	2 Jul 2002 14:06:48 -0000	1.7
+++ Completion/Unix/Command/_chown	19 Aug 2003 12:26:49 -0000
@@ -1,41 +1,82 @@
 #compdef chown chgrp
 
-local suf usr grp req line
+local curcontext="$curcontext" state line expl ret=1
+local suf usr grp req deref args
 
-line=( "${(@)words[2,CURRENT-1]:#-*}" )
+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 them self]'
+    '(-f --silent --quiet)'{-f,--silent,--quiet}"[don't report errors]"
+    '--reference=[copy ownership of specified file]:file:_files'
+    '(-R --recursive)'{-R,--recursive}'[change files and directories recursively]'
+    '(- : *)--help[display help information]'
+    '(- : *)--version[display version information]'
+  )    
+  [[ $service = chown ]] &&
+      args+=( '--from=[restrict changes to files by current ownership]: :->owner' )
+else
+  # based on $OSTYPE = solaris2.8
+  args=(
+    "-f[don't report errors]"
+    "-h[operate on symlinks them self]"
+    '-R[change files and directories recursively]'
+  )
+fi
 
-if [[ -prefix - ]]; then
-  _message -e options option
-elif [[ $#line -eq 0 ]]; then
-  if [[ $service = chgrp ]] || compset -P '*[:.]'; then
-    if (( EGID && $+commands[groups] )); then  # except for root
-      _wanted groups expl 'group' compadd $(groups) && return 0
+(( $+words[(r)--reference*] )) || args+=( '(--reference)1: :->owner' )
+_arguments -C -s "$args[@]" '*:files:->files' && ret=0
+
+case $state in
+  owner)
+    if [[ $service = chgrp ]] || compset -P '*[:.]'; then
+      if (( EGID && $+commands[groups] )); then  # except for root
+	_wanted groups expl 'group' compadd $(groups) && return 0
+      fi
+      _groups && ret=0
+    else
+      if compset -S '[.:]*'; then
+        suf=()
+      elif [[ $OSTYPE = (solaris*|hpux*|*bsd*) ]]; then
+	suf=( -qS ':' )
+      else
+	suf=( -qS '.' )
+      fi
+      _users "$suf[@]" && ret=0
     fi
-    _groups && return 0
-  else
-    if [[ $OSTYPE = (solaris*|hpux*|*bsd*) ]]; then
-      suf=':'
+  ;;
+  files)
+    (( $+opt_args[-h] || $+opt_args[--no-dereference] )) || deref="-"
+    if (( $+opt_args[--reference] )); then
+      if zstyle -t ":completion:${curcontext}:" disable-stat; then
+	_files && ret=0
+      else
+	zmodload -i zsh/stat 2>/dev/null
+	usr=$(stat +uid $opt_args[--reference])
+	grp=$(stat +gid $opt_args[--reference])
+	_wanted files expl file _files -g "*($deref^u$usr,$deref^g$grp)" && ret=0
+      fi
+      return ret
+    fi
+    if [[ $service = chgrp ]]; then
+      grp=${line[1]}
     else
-      suf='.'
+      usr=${line[1]%%[.:]*}
+      usr=${${(M)usr:#[0-9]#}:-${userdirs[$usr]:+.$usr.}}
+      grp=${${(M)line[1]%%[.:]*}#?}
     fi
-    compset -S '.*' && unset suf
-    _users -S "$suf" -q && return 0
-  fi
-else
-  if [[ $service = chgrp ]]; then
-    grp=${line[1]}
-  else
-    usr=${line[1]%%[.:]*}
-    usr=${${(M)usr:#[0-9]#}:-${userdirs[$usr]:+.$usr.}}
-    grp=${${(M)line[1]%%[.:]*}#?}
-  fi
-  [[ -n $grp ]] && grp="${${(M)grp:#[0-9]#}:-.$grp.}"
-  req=( ${usr:+\^u$usr} ${grp:+\^g$grp} )
-  (( EUID )) && req=( u$EUID$^req )
-  req=( -$^req )
-  req="*(${(j:,:)req})"
+    [[ -n $grp ]] && grp="${${(M)grp:#[0-9]#}:-.$grp.}"
+    req=( ${usr:+\^u$usr} ${grp:+\^g$grp} )
+    (( EUID )) && req=( u$EUID$^req )
+    req=( $deref$^req )
+    req="*(${(j:,:)req})"
 
-  ( : $~req ) 2> /dev/null || req='*'
+    ( : $~req ) 2> /dev/null || req='*'
 
-  _files -g "$req" && return 0
-fi
+    _wanted files expl file _files -g "$req" && ret=0
+  ;;
+esac
+
+return ret



Messages sorted by: Reverse Date, Date, Thread, Author