| 1 |
From e945e1f541b09b47db1353b0ebf001761cee40ef Mon Sep 17 00:00:00 2001 |
| 2 |
From: Koichi Murase <[email protected]> |
| 3 |
Date: Thu, 13 Jan 2022 18:04:32 +0900 |
| 4 |
Subject: [PATCH] [stub] global: work around bash-5.2a patsub_replacement |
| 5 |
|
| 6 |
--- |
| 7 |
keymap/vi.sh | 8 ++- |
| 8 |
lib/core-syntax.sh | 36 ++++++++------ |
| 9 |
src/edit.sh | 12 +++-- |
| 10 |
src/util.sh | 121 ++++++++++++++++++++++++++++++++++++--------- |
| 11 |
4 files changed, 133 insertions(+), 44 deletions(-) |
| 12 |
|
| 13 |
diff --git a/keymap/vi.sh b/keymap/vi.sh |
| 14 |
index e586e54..f3824e0 100644 |
| 15 |
--- a/keymap/vi.sh |
| 16 |
+++ b/keymap/vi.sh |
| 17 |
@@ -6971,7 +6971,13 @@ function ble/widget/vi_xmap/visual-replace-char.hook { |
| 18 |
fi |
| 19 |
|
| 20 |
local ins=${_ble_edit_str:beg:end-beg} |
| 21 |
- ins=${ins//[!$'\n']/"$s"} |
| 22 |
+ if ((_ble_bash>=50200)) && shopt -q patsub_replacement; then |
| 23 |
+ shopt -u patsub_replacement |
| 24 |
+ ins=${ins//[!$'\n']/"$s"} |
| 25 |
+ shopt -s patsub_replacement |
| 26 |
+ else |
| 27 |
+ ins=${ins//[!$'\n']/"$s"} |
| 28 |
+ fi |
| 29 |
ble/widget/.replace-range "$beg" "$end" "$ins" |
| 30 |
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--)) |
| 31 |
_ble_edit_ind=$beg |
| 32 |
diff --git a/lib/core-syntax.sh b/lib/core-syntax.sh |
| 33 |
index e3214a2..00362f4 100644 |
| 34 |
--- a/lib/core-syntax.sh |
| 35 |
+++ b/lib/core-syntax.sh |
| 36 |
@@ -972,16 +972,16 @@ function ble/syntax:bash/cclass/update { |
| 37 |
local histc1=${_ble_syntax_bash_histc12:0:1} |
| 38 |
local histc2=${_ble_syntax_bash_histc12:1:1} |
| 39 |
for key in "${!_ble_syntax_bash_charsFmt[@]}"; do |
| 40 |
- local a=${_ble_syntax_bash_charsFmt[key]} |
| 41 |
- a=${a//@h/$histc1} |
| 42 |
- a=${a//@q/$histc2} |
| 43 |
- _ble_syntax_bash_chars[key]=$a |
| 44 |
+ local ret=${_ble_syntax_bash_charsFmt[key]} |
| 45 |
+ ble/string#replace "$ret" @h "$histc1" |
| 46 |
+ ble/string#replace "$ret" @q "$histc2" |
| 47 |
+ _ble_syntax_bash_chars[key]=$ret |
| 48 |
done |
| 49 |
|
| 50 |
- local a=$_ble_syntax_bash_chars_simpleFmt |
| 51 |
- a=${a//@h/$histc1} |
| 52 |
- a=${a//@q/$histc2} |
| 53 |
- _ble_syntax_bashc_simple=$a |
| 54 |
+ local ret=$_ble_syntax_bash_chars_simpleFmt |
| 55 |
+ ble/string#replace "$ret" @h "$histc1" |
| 56 |
+ ble/string#replace "$ret" @q "$histc2" |
| 57 |
+ _ble_syntax_bashc_simple=$ret |
| 58 |
fi |
| 59 |
|
| 60 |
if [[ $seed == *x ]]; then |
| 61 |
@@ -2251,8 +2251,9 @@ function ble/syntax:bash/check-history-expansion/.initialize-event { |
| 62 |
else |
| 63 |
local A="[$histc1]" |
| 64 |
[[ $histc1 == '^' ]] && A='\^' |
| 65 |
- rex_event=$_ble_syntax_bash_histexpand_RexEventFmt |
| 66 |
- rex_event=${rex_event//@A/$A} |
| 67 |
+ local ret=$_ble_syntax_bash_histexpand_RexEventFmt |
| 68 |
+ ble/string#replace "$ret" @A "$A" |
| 69 |
+ rex_event=$ret |
| 70 |
fi |
| 71 |
} |
| 72 |
## @fn ble/syntax:bash/check-history-expansion/.initialize-quicksub |
| 73 |
@@ -2262,9 +2263,10 @@ function ble/syntax:bash/check-history-expansion/.initialize-quicksub { |
| 74 |
if [[ $histc2 == '^' ]]; then |
| 75 |
rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubDef |
| 76 |
else |
| 77 |
- rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubFmt |
| 78 |
- rex_quicksub=${rex_quicksub//@A/[$histc2]} |
| 79 |
- rex_quicksub=${rex_quicksub//@C/$histc2} |
| 80 |
+ local ret=$_ble_syntax_bash_histexpand_RexQuicksubFmt |
| 81 |
+ ble/string#replace "$ret" @A "[$histc2]" |
| 82 |
+ ble/string#replace "$ret" @C "$histc2" |
| 83 |
+ rex_quicksub=$ret |
| 84 |
fi |
| 85 |
} |
| 86 |
function ble/syntax:bash/check-history-expansion/.check-modifiers { |
| 87 |
@@ -4450,8 +4452,12 @@ function ble/syntax:bash/ctx-heredoc-word/escape-delimiter { |
| 88 |
local ret=$1 |
| 89 |
if [[ $ret == *[\\\'$_ble_term_IFS$_ble_term_FS]* ]]; then |
| 90 |
local a b fs=$_ble_term_FS |
| 91 |
- a=\\ ; b="\\$a"; ret="${ret//"$a"/$b}" |
| 92 |
- a=\' ; b="\\$a"; ret="${ret//"$a"/$b}" |
| 93 |
+ if ((_ble_bash>=50200)) && shopt -q patsub_replacement; then |
| 94 |
+ b='\\&'; ret="${ret//[\\\']/$b}" |
| 95 |
+ else |
| 96 |
+ a=\\ ; b="\\$a"; ret="${ret//"$a"/$b}" |
| 97 |
+ a=\' ; b="\\$a"; ret="${ret//"$a"/$b}" |
| 98 |
+ fi |
| 99 |
a=' ' ; b="$_ble_syntax_bash_heredoc_EscSP"; ret="${ret//"$a"/$b}" |
| 100 |
a=$'\t'; b="$_ble_syntax_bash_heredoc_EscHT"; ret="${ret//"$a"/$b}" |
| 101 |
a=$'\n'; b="$_ble_syntax_bash_heredoc_EscLF"; ret="${ret//"$a"/$b}" |
| 102 |
diff --git a/src/edit.sh b/src/edit.sh |
| 103 |
index 4650217..bdee4e7 100644 |
| 104 |
--- a/src/edit.sh |
| 105 |
+++ b/src/edit.sh |
| 106 |
@@ -601,10 +601,14 @@ _ble_prompt_term_status_data=() |
| 107 |
function ble/prompt/print { |
| 108 |
local text=$1 a b |
| 109 |
if [[ ! $prompt_noesc && $text == *['$\"`']* ]]; then |
| 110 |
- a='\' b='\\' text=${text//"$a"/$b} |
| 111 |
- a='$' b='\$' text=${text//"$a"/$b} |
| 112 |
- a='"' b='\"' text=${text//"$a"/$b} |
| 113 |
- a='`' b='\`' text=${text//"$a"/$b} |
| 114 |
+ if ((_ble_bash>=50200)) && shopt -q patsub_replacement; then |
| 115 |
+ text=${text//[\\\$\"\`]/\\\\&} |
| 116 |
+ else |
| 117 |
+ a='\' b='\\' text=${text//"$a"/$b} |
| 118 |
+ a='$' b='\$' text=${text//"$a"/$b} |
| 119 |
+ a='"' b='\"' text=${text//"$a"/$b} |
| 120 |
+ a='`' b='\`' text=${text//"$a"/$b} |
| 121 |
+ fi |
| 122 |
fi |
| 123 |
ble/canvas/put.draw "$text" |
| 124 |
} |
| 125 |
diff --git a/src/util.sh b/src/util.sh |
| 126 |
index 3a7c16a..6ce9e42 100644 |
| 127 |
--- a/src/util.sh |
| 128 |
+++ b/src/util.sh |
| 129 |
@@ -115,7 +115,13 @@ function bleopt/.read-arguments { |
| 130 |
|
| 131 |
if [[ $op ]]; then |
| 132 |
var=("${var[@]}") # #D1570: WA bash-3.0 ${scal[@]/x} bug |
| 133 |
- ble/array#push specs "${var[@]/%/=$value}" # #D1570 WA checked |
| 134 |
+ if ((_ble_bash>=50200)) && shopt -q patsub_replacement; then |
| 135 |
+ shopt -u patsub_replacement |
| 136 |
+ ble/array#push specs "${var[@]/%/=$value}" # #D1570 WA checked |
| 137 |
+ shopt -s patsub_replacement |
| 138 |
+ else |
| 139 |
+ ble/array#push specs "${var[@]/%/=$value}" # #D1570 WA checked |
| 140 |
+ fi |
| 141 |
else |
| 142 |
ble/array#push pvars "${var[@]}" |
| 143 |
fi |
| 144 |
@@ -771,11 +777,18 @@ function ble/array#replace { |
| 145 |
} |
| 146 |
|
| 147 |
function ble/dense-array#fill-range { |
| 148 |
- ble/array#reserve-prototype $(($3-$2)) |
| 149 |
- local _ble_script=' |
| 150 |
- local -a sARR; sARR=("${_ble_array_prototype[@]::$3-$2}") |
| 151 |
- ARR=("${ARR[@]::$2}" "${sARR[@]/#/$4}" "${ARR[@]:$3}")' # WA #D1570 checked |
| 152 |
- builtin eval -- "${_ble_script//ARR/$1}" |
| 153 |
+ if ((_ble_bash>=50200)) && shopt -q patsub_replacement; then |
| 154 |
+ shopt -u patsub_replacement |
| 155 |
+ ble/dense-array#fill-range "$@" |
| 156 |
+ shopt -s patsub_replacement |
| 157 |
+ return |
| 158 |
+ else |
| 159 |
+ ble/array#reserve-prototype $(($3-$2)) |
| 160 |
+ local _ble_script=' |
| 161 |
+ local -a sARR; sARR=("${_ble_array_prototype[@]::$3-$2}") |
| 162 |
+ ARR=("${ARR[@]::$2}" "${sARR[@]/#/$4}" "${ARR[@]:$3}")' # WA #D1570 checked |
| 163 |
+ builtin eval -- "${_ble_script//ARR/$1}" |
| 164 |
+ fi |
| 165 |
} |
| 166 |
|
| 167 |
function ble/idict#copy { |
| 168 |
@@ -796,15 +809,48 @@ function ble/string#reserve-prototype { |
| 169 |
done |
| 170 |
} |
| 171 |
|
| 172 |
+## @fn ble/string#replace str before after |
| 173 |
+## @param[in] str before after |
| 174 |
+## @var[out] ret |
| 175 |
+if ((_ble_bash>=50200)); then |
| 176 |
+ function ble/string#replace { |
| 177 |
+ if shopt -q patsub_replacement; then |
| 178 |
+ shopt -u patsub_replacement |
| 179 |
+ ret=${1//"$1"/$2} |
| 180 |
+ shopt -s patsub_replacement |
| 181 |
+ else |
| 182 |
+ ret=${1//"$1"/$2} |
| 183 |
+ fi |
| 184 |
+ } |
| 185 |
+else |
| 186 |
+ function ble/string#replace { |
| 187 |
+ ret=${1//"$1"/$2} |
| 188 |
+ } |
| 189 |
+fi |
| 190 |
+ |
| 191 |
## @fn ble/string#repeat str count |
| 192 |
## @param[in] str |
| 193 |
## @param[in] count |
| 194 |
## @var[out] ret |
| 195 |
-function ble/string#repeat { |
| 196 |
- ble/string#reserve-prototype "$2" |
| 197 |
- ret=${_ble_string_prototype::$2} |
| 198 |
- ret="${ret// /$1}" |
| 199 |
-} |
| 200 |
+if ((_ble_bash>=50200)); then |
| 201 |
+ function ble/string#repeat { |
| 202 |
+ ble/string#reserve-prototype "$2" |
| 203 |
+ ret=${_ble_string_prototype::$2} |
| 204 |
+ if shopt -q patsub_replacement; then |
| 205 |
+ shopt -u patsub_replacement |
| 206 |
+ ret="${ret// /$1}" |
| 207 |
+ shopt -s patsub_replacement |
| 208 |
+ else |
| 209 |
+ ret="${ret// /$1}" |
| 210 |
+ fi |
| 211 |
+ } |
| 212 |
+else |
| 213 |
+ function ble/string#repeat { |
| 214 |
+ ble/string#reserve-prototype "$2" |
| 215 |
+ ret=${_ble_string_prototype::$2} |
| 216 |
+ ret="${ret// /$1}" |
| 217 |
+ } |
| 218 |
+fi |
| 219 |
|
| 220 |
## @fn ble/string#common-prefix a b |
| 221 |
## @param[in] a b |
| 222 |
@@ -1074,16 +1120,41 @@ function ble/string#rtrim { |
| 223 |
## @param[in] chars1 |
| 224 |
## @param[in,opt] chars2 |
| 225 |
## @var[out] ret |
| 226 |
-function ble/string#escape-characters { |
| 227 |
- ret=$1 |
| 228 |
- if [[ $ret == *["$2"]* ]]; then |
| 229 |
- local chars1=$2 chars2=${3:-$2} |
| 230 |
- local i n=${#chars1} a b |
| 231 |
- for ((i=0;i<n;i++)); do |
| 232 |
- a=${chars1:i:1} b=\\${chars2:i:1} ret=${ret//"$a"/$b} |
| 233 |
- done |
| 234 |
- fi |
| 235 |
-} |
| 236 |
+if ((_ble_bash>=50200)); then |
| 237 |
+ function ble/string#escape-characters { |
| 238 |
+ ret=$1 |
| 239 |
+ if [[ $ret == *["$2"]* ]]; then |
| 240 |
+ local patsub_replacement= |
| 241 |
+ shopt -q patsub_replacement && patsub_replacement=1 |
| 242 |
+ if [[ $3 ]]; then |
| 243 |
+ shopt -s patsub_replacement |
| 244 |
+ ret=${ret//["$2"]/\\\\&} |
| 245 |
+ [[ $patsub_replacement ]] || |
| 246 |
+ shopt -u patsub_replacement |
| 247 |
+ else |
| 248 |
+ shopt -u patsub_replacement |
| 249 |
+ local chars1=$2 chars2=${3:-$2} |
| 250 |
+ local i n=${#chars1} a b |
| 251 |
+ for ((i=0;i<n;i++)); do |
| 252 |
+ a=${chars1:i:1} b=\\${chars2:i:1} ret=${ret//"$a"/$b} |
| 253 |
+ done |
| 254 |
+ [[ ! $patsub_replacement ]] || |
| 255 |
+ shopt -s patsub_replacement |
| 256 |
+ fi |
| 257 |
+ fi |
| 258 |
+ } |
| 259 |
+else |
| 260 |
+ function ble/string#escape-characters { |
| 261 |
+ ret=$1 |
| 262 |
+ if [[ $ret == *["$2"]* ]]; then |
| 263 |
+ local chars1=$2 chars2=${3:-$2} |
| 264 |
+ local i n=${#chars1} a b |
| 265 |
+ for ((i=0;i<n;i++)); do |
| 266 |
+ a=${chars1:i:1} b=\\${chars2:i:1} ret=${ret//"$a"/$b} |
| 267 |
+ done |
| 268 |
+ fi |
| 269 |
+ } |
| 270 |
+fi |
| 271 |
|
| 272 |
## @fn ble/string#escape-for-sed-regex text |
| 273 |
## @fn ble/string#escape-for-awk-regex text |
| 274 |
@@ -5301,8 +5372,9 @@ function ble/term/visible-bell:term/init { |
| 275 |
_ble_term_visible_bell_prev=(term "$message") |
| 276 |
} |
| 277 |
function ble/term/visible-bell:term/show { |
| 278 |
- local sgr=$1 message=${_ble_term_visible_bell_prev[1]} |
| 279 |
- ble/util/put "${_ble_term_visible_bell_show//'%message%'/$sgr$message}" >&2 |
| 280 |
+ local sgr=$1 message=${_ble_term_visible_bell_prev[1]} ret |
| 281 |
+ ble/string#replace "$_ble_term_visible_bell_show" %message% "$sgr$message" |
| 282 |
+ ble/util/put "$ret" >&2 |
| 283 |
} |
| 284 |
function ble/term/visible-bell:term/update { |
| 285 |
ble/term/visible-bell:term/show "$@" |
| 286 |
@@ -5619,7 +5691,8 @@ function ble/term/cursor-state/.update { |
| 287 |
local state=$(($1)) |
| 288 |
[[ $_ble_term_cursor_current == "$state" ]] && return 0 |
| 289 |
|
| 290 |
- local ret=${_ble_term_Ss//@1/$state} |
| 291 |
+ local ret |
| 292 |
+ ble/string#replace "$_ble_term_Ss" @1 "$state" |
| 293 |
|
| 294 |
# Note: 既に pass-through seq が含まれている時はスキップする。 |
| 295 |
[[ $ret && $ret != $'\eP'*$'\e\\' ]] && |
| 296 |
-- |
| 297 |
2.33.1 |
| 298 |
|