Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham
	autolearn_force=no version=3.4.1
X-AuditID: cbfec7f4-f796c6d000001486-4d-576a540a07ab
Date: Wed, 22 Jun 2016 10:01:59 +0100
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: bug: suffix alias and tabcompletion
Message-id: <20160622100159.1d82792f@pwslap01u.europe.root.pri>
In-reply-to:
 <CAH+w=7YdV_LFUghpueqbjRHWyhzEA8iDarE=FGG1BSk2Fr+s0w@mail.gmail.com>
References: <20160621193918.GA3768@fau.de>
 <CAH+w=7YdV_LFUghpueqbjRHWyhzEA8iDarE=FGG1BSk2Fr+s0w@mail.gmail.com>
Organization: Samsung Cambridge Solution Centre
X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu)
MIME-version: 1.0
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7bit
X-Brightmail-Tracker:
 H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsVy+t/xy7pcIVnhBvcXiVscbH7I5MDoserg
	B6YAxigum5TUnMyy1CJ9uwSujFltX9gLWoUqNn53aWDs4+ti5OCQEDCRWLM1q4uRE8gUk7hw
	bz1bFyMXh5DAUkaJed/2soMkhARmMEksP8YHkTjHKNG0dxcLROIso8TqbTUgNouAqsTWmR/Y
	QGw2AUOJqZtmM4LYIgJaEjtOnmQCsYUFDCTOfX4DNpRXwF6if+lVsHpOgWCJludtUDMLJR4e
	/Q0W5xfQl7j69xMTxHX2EjOvnGGE6BWU+DH5Hlg9M9D8zduaWCFseYnNa94yQ8xRl7hxdzf7
	BEbhWUhaZiFpmYWkZQEj8ypG0dTS5ILipPRcQ73ixNzi0rx0veT83E2MkED+soNx8TGrQ4wC
	HIxKPLwMXZnhQqyJZcWVuYcYJTiYlUR4HwdkhQvxpiRWVqUW5ccXleakFh9ilOZgURLnnbvr
	fYiQQHpiSWp2ampBahFMlomDU6qBcast37Puvnc/hKRSpxyK3jyLTdCncg7rjCOqj28aT0yK
	jph0kcHmTuLdBSEVVbI6KcErnQNVAxirS8u2lhVUL32oMKlDZ0nJUaeDGjdWHNGS7t1/IMSi
	JdblCtMqfU8JtYZlAcfVHDMb6/cazkg9otccsKxqvvfK9c4hypsvfo4zPnv/b9EuJZbijERD
	Leai4kQAgjG31WACAAA=
X-Seq: zsh-workers 38745

On Tue, 21 Jun 2016 13:18:21 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Jun 21, 2016 at 12:39 PM, Michael Gebhard
> <michael.gebhard@fau.de> wrote:
> >
> > running the following commands and trying to complete the last line
> > by pressing tab produces a zsh taking up 100% cpu.
> >
> > zsh -f
> > touch foo.bar
> > alias -s bar='echo a &'
> 
> It's not (just) completion; attempting to run the command "foo.bar"
> produces a similar infinite loop.  The alias has expanded to "echo a &
> foo.bar" which then recursively expands "foo.bar" (because after the
> "&" it is in command position again) and off we go.  The "touch
> foo.bar" isn't required.

This is different from a normal alias because in that case the original
text is completely removed; you can only encounter the (normal or
global) alias again while expanding the alias, which we detect, or
after, which is a completely different set of input triggering the
expansion so there's no recursion.

In this case, the argument foo.bar appears again *after* the alias has
been expanded fully because the alias is inserted before it.  By the
time we get to foo.bar again as the argument, the trace of alias
expansion has been removed --- we've just got the resulting fully
expanded text "echo a &" and no more alias.

The nearest parallel to this case with normal aliases is this:

alias foo='echo a &'
% foo foo
[1] 3423
a
[2] 3424
% 
[1]  - done       echo a
% a

[2]  + done       echo a

You'll see the underlying expansion behaviour is the same --- both
aliases get expanded.  This is long-standing and I assume is regarded as
OK.

I suppose we'd need some way of marking something as having had a suffix
alias expanded before it, and then we'd need to reset that flag if we
encountered something not at the start of that input that was in command
position so we could expand a new suffix alias.

However, I don't think it's quite as simple as the two separate tests
- when expanding a suffix alias, set a lexical flag to say no more
suffix aliases;
- reset that flag any time you reach a new command position

because in the problem case we have reached a new command position and
don't want to reset the flag.  So there's some subtle ordering to sort
out.

pws

