bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: About `M-C-e` expand result `'` failed


From: Martin D Kealey
Subject: Re: About `M-C-e` expand result `'` failed
Date: Sat, 3 Feb 2024 22:44:12 +1000

On Wed, 31 Jan 2024 at 01:04, Andreas Schwab <schwab@suse.de> wrote:

> On Jan 30 2024, Zachary Santer wrote:
> > There's no way this is the intended behavior, right?
>
> The command is doing exactly what it is documented to do, that is do all
> of the shell word expansions.
>

If that's how the documentation is interpreted, then clearly there's a bug
in the documentation, because this is almost never *useful* behaviour.

It actually says:

> shell-expand-line (M-C-e) ... This performs alias and history expansion
as well as all of the shell word expansions.

It does *not* say that it does quote removal.

It could be argued that quote removal is implicitly required for some of
the steps to proceed, but in that case it can equally be argued that such
removal must therefore be reversed afterwards.


By all means, add a "strip-quotes" command to readline, so the user can use
in the exceptional cases where you want to diverge from what the shell
would have done without M-C-e, but by default any expansion or substitution
that's triggered by a readline command should render a result that's immune
to that same expansion being done again when the user hits enter.

Which quotes to reinstate probably depends on which expansions have already
been done. This includes history expansion when histreedit is on; for
example, this is unhelpful:

$ ! echo Hi
Hi
$ echo !:0!$
[expands history and re-loads input buffer]
$ echo !Hi
bash_5.1.4p47-release: !Hi: event not found

I suggest that the history expansion should note the result of an expansion
includes a history expansion character ("!" by default), and apply a
modification if it would be recognized as such (followed by
[[:alnum:]_:?%*$-], and not in single quotes).

That modification would be:
 - outside quotes, insert \ before a recognized history character; if
necessary mark this byte to prevent it from being doubled by subsequent
expansions.
 - inside single quotes, nothing
 - inside double quotes, insert "" (two double-quote characters) after it.

Similar safeguards would be needed after any other kind of expansion,
except that it suffices to treat "!" as a separator character for the
purpose of deciding whether or not a word needs to be re-quoted. (As long
as it's always single-quoted, or backslash-escaped, the history characters
don't need any other special treatment.)

One way to decide whether any history chars need protection would simply be
to apply history expansion to the result of the requested expansion, and if
that changes the text, something in it needs protecting. Ideally the
history expansion logic would note the location of any history expansion
characters, so that readline could use that as a hint of what to fix.

-Martin


reply via email to

[Prev in Thread] Current Thread [Next in Thread]