Skip to content

Commit ab585cf

Browse files
committed
Make BASH hook compatible with spaces in completion results
ZSH handles multi-word completion results correctly out of the box, but BASH needed some prodding to stop it interpreting all space-delimited words as separate suggestions. There are two parts to this fix: - The addition of a local `IFS` variable was required to prevent splitting from happening inside multi-word completions. - Spaces needed to be manually escaped when quotes were missing as completing as completion was otherwise dumping the raw result without escaping. I haven't looked into how filename completion mode handles this without showing backslashes in the completion result, but the behaviour here mirrors ZSH so this isn't a huge problem. Custom multi-word completions in BASH aren't particularly intuitive - these threads were helpful while figuring this solution out: https://stackoverflow.com/questions/26509260/bash-tab-completion-with-spaces https://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion https://stackoverflow.com/questions/10652492/bash-autocompletion-how-to-pass-this-array-to-compgen-without-significant-whit
1 parent 2488ee1 commit ab585cf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/HookFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function %%function_name%% {
4141
4242
local RESULT STATUS;
4343
44+
# Force splitting by newline instead of default delimiters
45+
local IFS=$'\n'
46+
4447
RESULT="$(%%completion_command%% </dev/null)";
4548
STATUS=$?;
4649
@@ -65,6 +68,11 @@ function %%function_name%% {
6568
6669
COMPREPLY=(`compgen -W "$RESULT" -- $cur`);
6770
71+
# Escape any spaces in results if the current word doesn't begin with a quote
72+
if [[ ! -z $COMPREPLY ]] && [[ ! $cur =~ ^[\'\"] ]]; then
73+
COMPREPLY=($(printf '%q\n' "${COMPREPLY[@]}"));
74+
fi;
75+
6876
__ltrim_colon_completions "$cur";
6977
7078
MAILCHECK=mail_check_backup;

0 commit comments

Comments
 (0)