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

[PATCH] Improve _sh

Was sitting on this:

When i was testing _su for workers/45421 i was frustrated by the fact that it
wouldn't complete -c correctly for me, and that's due to the way sh completion
works. _sh is a generic completer for traditional UNIX shells, so it doesn't
really understand the options they take, and only provides very limited
completion for -c.

It wouldn't be difficult to add full completion for most of the listed shells,
and maybe i'll do that later, but in the mean time: All of the shells in the
#compdef line appear to support the -ceilx options, and in order to ensure
that stuff like `sh -pc ...` works we can just silently ignore any others. I
think that'd be nicer than what we're currently doing, at least?

PS: Stacked-option-ignoring doesn't really work here when i have the +x
variants added for Bourne shells, due to workers/45422. Since ignoring those
options more accurately is one of the main benefits of this change, i'll
probably merge with that line commented out if it goes in first

PPS: I added fish to the check for Bourne shells, just in case, but i'm not
going to worry about it for now otherwise


diff --git a/Completion/Unix/Command/_sh b/Completion/Unix/Command/_sh
index 39d299c58..399baa1ed 100644
--- a/Completion/Unix/Command/_sh
+++ b/Completion/Unix/Command/_sh
@@ -1,14 +1,35 @@
 #compdef sh ash csh dash ksh ksh88 ksh93 mksh oksh pdksh rc tcsh yash
-if (( CURRENT == ${words[(i)-c]} + 1 )); then
-  _cmdstring
-  local n=${words[(b:2:i)[^-]*]}
-  if (( n <= CURRENT )); then
-    compset -n $n
-    _alternative \
-      'files:file:_files' \
-      'commands:command:_normal' && return 0
-  fi
-  _default
+local bourne argv0
+local -a args all_opts=( -{{0..9},{A..Z},{a..z}} )
+[[ $service == (csh|?csh|fish|rc) ]] || bourne=1
+# Bourne-style shells support +x variants
+(( bourne )) && all_opts+=( ${all_opts/#-/+} )
+# Bourne-style shells take argv[0] as the second argument to -c
+(( bourne )) && argv0=':argv[0]:'
+# All of the recognised shells support at least these arguments
+  "(1 -)-c[execute specified command string]: :_cmdstring$argv0"
+  '-e[exit immediately on non-zero return]'
+  '-i[act as interactive shell]'
+  '-l[act as login shell]'
+  '-x[print command trace]'
+  '1:script file:_files'
+  '*:: :_files'
+# Bourne-style shells support -o/+o option. Not all of them support -ooption in
+# the same argument, but we'll allow it here for those that do
+(( bourne )) && args+=(
+  '-o+[set specified option]:option:'
+  '+o+[unset specified option]:option:'
+# Since this is a generic function we don't know what other options these shells
+# support, but we don't want them to break the ones listed above, so we'll just
+# ignore any other single-alphanumeric option. Obviously this doesn't account
+# for long options
+args+=( '!'${^${all_opts:#(${(~j<|>)${(@M)${(@M)args#(*[\*\)]|)[+-]?}%[+-]?}})}} )
+_arguments -s -S -A '-*' : $args

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