zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] Improve _sh
Date: Wed, 11 Mar 2020 16:36:52 -0500	[thread overview]
Message-ID: <DDBF2FA0-651A-46EC-B388-55950D64D4F6@dana.is> (raw)

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


                 reply	other threads:[~2020-03-11 21:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DDBF2FA0-651A-46EC-B388-55950D64D4F6@dana.is \
    --to=dana@dana.is \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).