@@ -121,15 +121,46 @@ __bp_trim_whitespace() {
121121
122122
123123# Trims whitespace and removes any leading or trailing semicolons from $2 and
124- # writes the resulting string to the variable name passed as $1. Used for
125- # manipulating substrings in PROMPT_COMMAND
124+ # writes the resulting string to the variable name passed as $1. This also
125+ # removes the no-op colons, which are converted from the hooks to remove. Used
126+ # for manipulating substrings in PROMPT_COMMAND
126127__bp_sanitize_string () {
127- local var=${1:? } text=${2:- } sanitized
128- __bp_trim_whitespace sanitized " $text "
128+ local var=${1:? } sanitized=${2:- }
129+
130+ local unset_extglob=
131+ if ! shopt -q extglob; then
132+ unset_extglob=yes
133+ shopt -s extglob
134+ fi
135+
136+ # We specify newline character through the variable `nl' because $'\n'
137+ # inside "${var//...}" is treated literally as "\$'\\n'" when `extquote' is
138+ # unset (shopt -u extquote). (Note: Bash 5.2's extquote seems to be buggy.)
139+ local tmp sp=$' \t ' nl=$' \n '
140+ while
141+ # Quoting parameter expansions $nl in PAT of ${var//PAT/REP} is
142+ # required by shellcheck. On the other hand, we should not quote the
143+ # parameter expansions $nl in REP because the quotes will remain in the
144+ # replaced result with `shopt -s compat42'.
145+ tmp=" ${sanitized// [";$nl"]* (["$sp"]):* (["$sp"])[";$nl"]/ $nl } "
146+ [[ " $tmp " != " $sanitized " ]]
147+ do
148+ sanitized=" $tmp "
149+ done
150+ sanitized=" ${sanitized#:* (["$sp"])[";$nl"]} "
151+ sanitized=" ${sanitized% [";$nl"]* (["$sp"]): } "
152+ __bp_trim_whitespace sanitized " $sanitized "
129153 sanitized=${sanitized% ;}
130154 sanitized=${sanitized# ;}
131155 __bp_trim_whitespace sanitized " $sanitized "
156+ if [[ " $sanitized " == " :" ]]; then
157+ sanitized=
158+ fi
132159 printf -v " $var " ' %s' " $sanitized "
160+
161+ if [[ -n " $unset_extglob " ]]; then
162+ shopt -u extglob
163+ fi
133164}
134165
135166# This function is installed as part of the PROMPT_COMMAND;
@@ -323,20 +354,12 @@ __bp_install() {
323354 shopt -s extdebug > /dev/null 2>&1
324355 fi ;
325356
326- # We specify newline character through the variable `nl' because $'\n'
327- # inside "${var//...}" is treated literally as "\$'\\n'" when `extquote' is
328- # unset (shopt -u extquote). (Note: Bash 5.2's extquote seems to be buggy.)
329- local existing_prompt_command nl=$' \n '
357+ local existing_prompt_command
330358 # Remove setting our trap install string and sanitize the existing prompt command string
331359 existing_prompt_command=" ${PROMPT_COMMAND:- } "
332360 # Edge case of appending to PROMPT_COMMAND
333361 existing_prompt_command=" ${existing_prompt_command// $__bp_install_string /: } " # no-op
334- existing_prompt_command=" ${existing_prompt_command// $nl : $nl / $nl } " # remove known-token only
335- existing_prompt_command=" ${existing_prompt_command// $nl : ;/ $nl } " # remove known-token only
336362 __bp_sanitize_string existing_prompt_command " $existing_prompt_command "
337- if [[ " ${existing_prompt_command:-: } " == " :" ]]; then
338- existing_prompt_command=
339- fi
340363
341364 # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
342365 # actually entered something.
0 commit comments