zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Improve _sh
@ 2020-03-11 21:36 dana
  0 siblings, 0 replies; only message in thread
From: dana @ 2020-03-11 21:36 UTC (permalink / raw)
  To: Zsh hackers list

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

dana


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
-else
-  local n=${words[(b:2:i)[^-]*]}
-  if (( n <= CURRENT )); then
-    compset -n $n
-    _alternative \
-      'files:file:_files' \
-      'commands:command:_normal' && return 0
-  fi
-  _default
-fi
+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
+args=(
+  "(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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-03-11 21:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-11 21:36 [PATCH] Improve _sh dana

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