Subject: Re: bug#36103: 24.5; Blank spaces around parentheses in history expansion in shell mode

From: Mauro Aranda <[email-masked]>
Date: Tue, 3 Oct 2023 07:54:34 -0300

Noam Postavsky <[email-masked]> writes:

 > Enric Rodríguez Carbonell <[email-masked]> writes:
 >
 >> $ for file in $(ls); do echo $file; done
 >
 >> $ !for
 >
 >> M-x comint-replace-by-expanded-history
 >
 >> $ for file in $ ( ls ) ; do echo $file ; done
 >>
 >> The blank spaces around the parentheses should not be there.
 >> Because of these blank spaces, this command cannot be executed,
 >> as "$ (" should be "$(".
 >
 > Yes, the problem is in comint-arguments which attempts to parse a shell
 > line into separate arguments, using just a bunch of regexps.  This is
 > doomed to failure I think.  But the alternative would be a full blown
 > shell parser, so I don't know how feasible it will be to fix this bug.
 > It's been with us a long time.

I just wanted to point out that since we now have treesitter support for
bash, this bug might be easier to fix, at least partially...

A first idea (trying to be backward-compatible and non-intrusive) is to
define a new variable, comint-arguments-function, turn the current
comint-arguments function into a comint-arguments-default function, and
set comint-arguments-function to that by default.  From now on,
comint-arguments would just call the comint-arguments-function and
return whatever that returns.

Then, shell.el can set comint-arguments-function to a new function,
that, if there is treesitter support for the program being run by shell
(bash or sh, AFAICS), tries to do a better job than comint-arguments as
of now.  If there's no treesitter support, or something fails, then it
can just punt to comint-arguments-default-function.