From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: (qmail 22248 invoked from network); 5 May 2020 16:50:28 -0000 Received-SPF: pass (primenet.com.au: domain of zsh.org designates 203.24.36.2 as permitted sender) receiver=inbox.vuxu.org; client-ip=203.24.36.2 envelope-from= Received: from ns1.primenet.com.au (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTPUTF8; 5 May 2020 16:50:28 -0000 Received: (qmail 17745 invoked by alias); 5 May 2020 16:50:19 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 24819 Received: (qmail 20495 invoked by uid 1010); 5 May 2020 16:50:19 -0000 X-Qmail-Scanner-Diagnostics: from wout5-smtp.messagingengine.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.2/25801. spamassassin: 3.4.4. Clear:RC:0(64.147.123.21):SA:0(-1.1/5.0):. Processed in 4.50563 secs); 05 May 2020 16:50:19 -0000 X-Envelope-From: d.s@daniel.shahaf.name X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at daniel.shahaf.name does not designate permitted sender hosts) X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrjeeigddutdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkjghfofggtgfgsehtqh dttdertdejnecuhfhrohhmpeffrghnihgvlhcuufhhrghhrghfuceougdrshesuggrnhhi vghlrdhshhgrhhgrfhdrnhgrmhgvqeenucggtffrrghtthgvrhhnpefhtdetfeehveeutd ehuddtieefgeettedtjedtffehudeiieejleetteekudetheenucfkphepuddtledrieei rdduhedrvdefleenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfh hrohhmpegurdhssegurghnihgvlhdrshhhrghhrghfrdhnrghmvg X-ME-Proxy: Date: Tue, 5 May 2020 16:49:34 +0000 From: Daniel Shahaf To: Dan Arad Cc: zsh-users@zsh.org Subject: Re: Help Request/Bug Report: comparguments causes _arguments to fail in certain cases Message-ID: <20200505164934.3f6b7f42@tarpaulin.shahaf.local2> In-Reply-To: References: <20200503165802.6540ad48@tarpaulin.shahaf.local2> <20200504131830.3572e317@tarpaulin.shahaf.local2> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > > > Well, for starters, if you =C2=ABshift words=C2=BB, you should decrem= ent CURRENT =20 > > > as well. > > > > > > However, _python uses the =C2=AB*::=E2=80=A6=C2=BB form of an _argume= nts optspec, > > > which should take care of $words/$NUMERIC for you. I assume the reas= on > > > it didn't is that you used =C2=ABcompdef -p=C2=BB. Try -P instead? = If I'm not > > > mistaken, that would also handle =C2=ABpython =E2=80=A6 script.py =C2=BB for you > > > (where the ellipsis stands for python's --options). =20 > > > > > > I tested the 4 possible options, and found that switching -p with -P > > doesn't change anything, but shifting words and decrementing CURRENT =20 > > works! But... I feel like it's a hack I'm not supposed to do > > (correct me if I'm wrong). Those parameters are documented, and manipulated by a number of functions (including _python, as it happens), so you _are_ using the API. See also the 'compset' builtin. However, as mentioned, I suspect that hardcoding a skip/decrement of one element won't DTRT if python is invoked with options (e.g., =C2=ABpython --foo script.py =C2=BB)=C2=A0=E2=80=94 not unless you reimplement _pyt= hon's parsing of those options and any arguments to them. > > I now tried using the BUFFER variable instead of words (since it remains > > unchanged), but found that it also has caveats: The BUFFER variable > > contains the raw value of the line, whereas the words array contains the > > words after expansions (I think that's the right term). Partly. $words has undergone alias expansion, but not further expansions. In particular, the words are still quoted. > > To demonstrate this I tried running again completion for `python ps,py` > > with `#compdef -P *`, and printing out BUFFER and words. The debug log = =20 > > (and my terminal) showed the following: > > First of all _python was called and dispatched my script which printed > > `BUFFER=3Dpython ps.py` and `words=3Dps.py`. That's what I'd expect. > > After _python returned, my script was invoked again and printed > > `BUFFER=3Dpython ps.py` and `words=3Dpython*3* ps.py` (note the 3). I'm not sure offhand where this comes from. $context, $funcstack, et al may have clues. > > I do think the right solution is to be dispatched by _python with =20 > > `#compdef -P *.py`, but to be able to run I need the words array as > > seen by _python Once an element is shifted off $words, it's lost forever (free() is called on it), so there'll be no way to access a "previous" value of $words unless a copy had been made somewhere. Moreover, consider that python may be itself wrapped by some precommand: for example, =C2=ABenv X=3DY python=C2=BB, =C2=ABsudo -u foo python=C2=BB, = or =C2=ABssh foo python=C2=BB. You might try looking up the place that originally populates $words and making it stash a copy of $words in another, read-only array; then you'll be able to use =C2=ABcompset -P=C2=BB and access that array. (Compare vcs_= info's use of *_orig keys in ${hook_com}.) > > or a to understand how BUFFER can be expanded/converted to its correct = =20 > > form. If you have any input on this, I would very much appreciate > > it. Something along these lines, I think: local -a words2=3D( ${(z):-"$PREBUFFER${LBUFFER}x"} ) words2[-1]=3D${words2[-1]%x} words2[1,${words2[(I)(;|&&|=E2=80=A6)]}]=3D() To understand the second line, see addx() in Src/Zle/zle_tricky.c. With that out of the way, the first line's a textbook use of the ${(z)} tokenizer. The third line just throws out everything until the start of the current _simple command_ (see zshmisc(1))=C2=A0=E2=80=94 or, at least, = it will once the ellipsis is filled out. =20 Caveats: that snippet doesn't do alias expansion, doesn't try to deal with ${RBUFFER}, doesn't try to deal with SHORT_LOOPS, doesn't try to deal with precommands and precommand assignments, should be changed to use zZ+c+ if the INTERACTIVE_COMMENTS option is set=E2=80=A6 Although it's a plugin that I worked on myself, I should probably mention that zsh-syntax-highlighting has faced the same problem and you and implemented a solution to it, which handles a number of the above caveats. > There's also a third approach: instead of trying to run the binary, make it stash the output in a well-known location. For example, /usr/bin/foo could store the output in /usr/share/argcomplete/foo. (I've always wanted to standardize _some_ solution to this problem; I'm tired of writing completion functions by hand=E2=80=A6) Cheers, Daniel