Discussion:
[PATCH] Fix custom program's completions when initial word is set
Luca Boccassi
2018-11-23 12:48:54 UTC
Permalink
The change to fix mid-word initial completion inadvertently broke custom
command completion.
To reproduce:

cat > repro<<EOF
_repro()
{
echo "completed"
}
EOF
source repro
complete -I -F _repro
git sho
^ tab, nothing happens

This is because if complete -I is set, iw_compspec will always be
non-null and therefore foundcs will always be set to false and bash
will ignore the result of programmable_completions().
The fix is to only override foundcs if both iw_compspec is not null
and we are not in command position.

Signed-off-by: Luca Boccassi <***@debian.org>
---
Dear Maintainer,

noticed this a bit too late after some more testing. This fix appears
to work, let me know if you'd like a different solution.
Thanks!

bashline.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bashline.c b/bashline.c
index d56cd79d..f2d17a70 100644
--- a/bashline.c
+++ b/bashline.c
@@ -1583,7 +1583,7 @@ attempt_shell_completion (text, start, end)
/* command completion if programmable completion fails */
/* If we have a completion for the initial word, we can prefer that */
in_command_position = s == start && (iw_compspec || STREQ (n, text)); /* XXX */
- foundcs = foundcs && (iw_compspec == 0);
+ foundcs = foundcs && (iw_compspec == 0 || in_command_position == 0);
}
/* empty command name following command separator */
else if (s >= e && n[0] == '\0' && text[0] == '\0' && start > 0 &&
--
2.19.1
Tom Ryder
2018-11-25 10:04:18 UTC
Permalink
Post by Luca Boccassi
The fix is to only override foundcs if both iw_compspec is not null
and we are not in command position.
Thank you for this patch. I first ran into the issue with 5.0-beta2
another way: I noticed that my default completion spec with -D as
suggested by the Bash manual page was no longer working:

_completion_loader()
{
. "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
}
complete -D -F _completion_loader -o bashdefault -o default

In 5.0-beta2, after running this code, for any command with no
completion specs defined in /etc/bash_completion.d, completing an
argument does nothing.

Your second patch does not correct that, but it looks like that's
because a non-zero `foundcs` is coerced to 1 in it, when there are other
meaningful values for the integer as the first parameter for
`pcomp_set_readline_variables(int, int)`.

The attached patch is my own attempt, which seems to correct my issues
as well as the one you raised in this post. Long-time user, first-time
poster...
--
Tom Ryder <https://sanctum.geek.nz/>
Luca Boccassi
2018-11-26 10:36:41 UTC
Permalink
Post by Luca Boccassi
The fix is to only override foundcs if both iw_compspec is not null
and we are not in command position.
Thank you for this patch. I first ran into the issue with 5.0-beta2 
another way: I noticed that my default completion spec with -D as 
    _completion_loader()
    {
         . "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return
124
    }
    complete -D -F _completion_loader -o bashdefault -o default
In 5.0-beta2, after running this code, for any command with no 
completion specs defined in /etc/bash_completion.d, completing an 
argument does nothing.
Your second patch does not correct that, but it looks like that's 
because a non-zero `foundcs` is coerced to 1 in it, when there are
other 
meaningful values for the integer as the first parameter for 
`pcomp_set_readline_variables(int, int)`.
The attached patch is my own attempt, which seems to correct my
issues 
as well as the one you raised in this post. Long-time user, first-
time 
poster...
Hi,

Thanks for the update, yes it looks like your patch is a better
solution, I've tested it as well, thanks.
--
Kind regards,
Luca Boccassi
Loading...