Skip to content

Commit

Permalink
global: adjust implementations for bash-5.2 "patsub_replacement"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 28, 2022
1 parent 2b55aa1 commit 359a389
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 79 deletions.
12 changes: 6 additions & 6 deletions bind.sh
Expand Up @@ -9,18 +9,18 @@
function ble/init:bind/append {
local xarg="\"$1\":ble-decode/.hook $2; builtin eval -- \"\$_ble_decode_bind_hook\""
local rarg=$1 condition=$3${3:+' && '}
ble/util/print "${condition}builtin bind -x '${xarg//$apos/$APOS}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$apos/$APOS}'" >> "$fbind2"
ble/util/print "${condition}builtin bind -x '${xarg//$q/$Q}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$q/$Q}'" >> "$fbind2"
}
function ble/init:bind/append-macro {
local kseq1=$1 kseq2=$2 condition=$3${3:+' && '}
local sarg="\"$kseq1\":\"$kseq2\"" rarg=$kseq1
ble/util/print "${condition}builtin bind '${sarg//$apos/$APOS}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$apos/$APOS}'" >> "$fbind2"
ble/util/print "${condition}builtin bind '${sarg//$q/$Q}'" >> "$fbind1"
ble/util/print "${condition}builtin bind -r '${rarg//$q/$Q}'" >> "$fbind2"
}
function ble/init:bind/bind-s {
local sarg=$1
ble/util/print "builtin bind '${sarg//$apos/$APOS}'" >> "$fbind1"
ble/util/print "builtin bind '${sarg//$q/$Q}'" >> "$fbind1"
}

function ble/init:bind/generate-binder {
Expand All @@ -32,7 +32,7 @@ function ble/init:bind/generate-binder {
: >| "$fbind1"
: >| "$fbind2"

local apos=\' APOS="'\\''"
local q=\' Q="'\\''"
local altdqs24='\xC0\x98'
local altdqs27='\xC0\x9B'

Expand Down
27 changes: 15 additions & 12 deletions ble-core.sh
Expand Up @@ -171,7 +171,7 @@ function ble/dense-array#fill-range {
_ble_util_array_prototype.reserve $(($3-$2))
local _ble_script='
local -a sARR; sARR=("${_ble_util_array_prototype[@]::$3-$2}")
ARR=("${ARR[@]::$2}" "${sARR[@]/#/$4}" "${ARR[@]:$3}")'
ARR=("${ARR[@]::$2}" "${sARR[@]/#/"$4"}" "${ARR[@]:$3}")' # WA #D1570 checked
builtin eval -- "${_ble_script//ARR/$1}"
}

Expand All @@ -190,7 +190,7 @@ function _ble_util_string_prototype.reserve {
function ble/string#repeat {
_ble_util_string_prototype.reserve "$2"
ret=${_ble_util_string_prototype::$2}
ret="${ret// /$1}"
ret=${ret//' '/"$1"}
}

## 関数 ble/string#common-prefix a b
Expand Down Expand Up @@ -397,7 +397,7 @@ function ble/string#escape-for-sed-regex {
if [[ $ret == *['\.[*^$/']* ]]; then
local a b
for a in \\ \. \[ \* \^ \$ \/; do
b="\\$a" ret=${ret//"$a"/$b}
b='\'$a ret=${ret//"$a"/"$b"}
done
fi
}
Expand All @@ -406,7 +406,7 @@ function ble/string#escape-for-awk-regex {
if [[ $ret == *['\.[*?+|^$(){}/']* ]]; then
local a b
for a in \\ \. \[ \* \? \+ \| \^ \$ \( \) \{ \} \/; do
b="\\$a" ret=${ret//"$a"/$b}
b='\'$a ret=${ret//"$a"/"$b"}
done
fi
}
Expand All @@ -415,7 +415,7 @@ function ble/string#escape-for-extended-regex {
if [[ $ret == *['\.[*?+|^$(){}']* ]]; then
local a b
for a in \\ \. \[ \* \? \+ \| \^ \$ \( \) \{ \}; do
b="\\$a" ret=${ret//"$a"/$b}
b='\'$a ret=${ret//"$a"/"$b"}
done
fi
}
Expand Down Expand Up @@ -819,7 +819,7 @@ if ((_ble_bash>=40200)); then
((__ble_i==__ble_MaxLoop)) && __ble_error=1 __ble_value= # not found

[[ $hidden_only && $__ble_i == 0 ]] && continue
echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down Expand Up @@ -850,7 +850,7 @@ else
[[ $__ble_found ]] || __ble_error= __ble_value= # not found
[[ $hidden_only && $__ble_found == 0 ]] && continue

echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down Expand Up @@ -1379,7 +1379,7 @@ function ble/util/invoke-hook {
## scriptfile 呼出の起点として使用する関数のみで充分です。
##
function ble-autoload {
local apos="'" APOS="'\\''" file=$1 funcname
local q="'" Q="'\\''" file=$1 funcname
shift

# ※$FUNCNAME は元から環境変数に設定されている場合、
Expand All @@ -1389,7 +1389,7 @@ function ble-autoload {
for funcname in "$@"; do
builtin eval "function $funcname {
unset -f $funcname
ble-import '${file//$apos/$APOS}'
ble-import '${file//$q/$Q}'
$funcname \"\$@\"
}"
done
Expand Down Expand Up @@ -1616,7 +1616,8 @@ function ble/term/visible-bell/.worker {
# Note: ble/util/assign は使えない。本体の ble/util/assign と一時ファイルが衝突する可能性がある。

ble/util/sleep 0.05
builtin echo -n "${_ble_term_visible_bell_show//'%message%'/$_ble_term_rev${message::cols}}" >&2
local esc=${_ble_term_visible_bell_show//'%message%'/"$_ble_term_rev${message::cols}"}
builtin echo -n "$esc" >&2

# load time duration settings
declare msec=$bleopt_vbell_duration
Expand All @@ -1640,7 +1641,8 @@ function ble/term/visible-bell {
local message="$*"
message=${message:-$bleopt_vbell_default_message}

builtin echo -n "${_ble_term_visible_bell_show//'%message%'/${_ble_term_setaf[2]}$_ble_term_rev${message::cols}}" >&2
local esc=${_ble_term_visible_bell_show//'%message%'/"${_ble_term_setaf[2]}$_ble_term_rev${message::cols}"}
builtin echo -n "$esc" >&2
( ble/term/visible-bell/.worker __ble_suppress_joblist__ 1>/dev/null & )
}
function ble/term/visible-bell/cancel-erasure {
Expand Down Expand Up @@ -1739,7 +1741,8 @@ function ble/term/cursor-state/.update {
local state=$(($1))
[[ $_ble_term_cursor_current == $state ]] && return

ble/util/buffer "${_ble_term_Ss//@1/$state}"
local esc=${_ble_term_Ss//@1/"$state"}
ble/util/buffer "$esc"

_ble_term_cursor_current=$state
}
Expand Down
6 changes: 3 additions & 3 deletions ble-decode.sh
Expand Up @@ -1575,8 +1575,8 @@ function ble-decode-bind/cmap/.generate-binder-template {
}

function ble-decode-bind/cmap/.emit-bindx {
local ap="'" eap="'\\''"
echo "builtin bind -x '\"${1//$ap/$eap}\":ble-decode/.hook $2; builtin eval \"\$_ble_decode_bind_hook\"'"
local q="'" Q="'\''"
echo "builtin bind -x '\"${1//$q/$Q}\":ble-decode/.hook $2; builtin eval \"\$_ble_decode_bind_hook\"'"
}
function ble-decode-bind/cmap/.emit-bindr {
echo "builtin bind -r \"$1\""
Expand Down Expand Up @@ -1644,7 +1644,7 @@ function ble-decode-bind/.generate-source-to-unbind-default/.process {
local q=\' b=\\ Q="'\''"
# Note: Solaris xpg4 awk では gsub の置換後のエスケープシーケンスも処理される
[[ $_ble_bin_awk_solaris_xpg4 == yes ]] && Q="'$b$b''"
local QUOT_Q=\"${Q//"$b"/$b$b}\"
local QUOT_Q=\"${Q//"$b"/"$b$b"}\"
LC_ALL=C ble/bin/awk -v q="$q" '
BEGIN {
Q = '"$QUOT_Q"';
Expand Down
20 changes: 10 additions & 10 deletions ble-edit.sh
Expand Up @@ -1006,7 +1006,7 @@ function ble-edit/prompt/initialize {
c=$ret
fi
fi
windir=/cygdrive/$c/${path//$bsl/$sl}
windir=/cygdrive/$c/${path//"$bsl"/"$sl"}
fi

if [[ -e $windir && -w $windir ]]; then
Expand Down Expand Up @@ -1065,17 +1065,17 @@ function _ble_edit_prompt.load {
function ble-edit/prompt/update/append {
local text=$1 a b
if [[ $text == *['$\"`']* ]]; then
a='\' b='\\' text=${text//"$a"/$b}
a='$' b='\$' text=${text//"$a"/$b}
a='"' b='\"' text=${text//"$a"/$b}
a='`' b='\`' text=${text//"$a"/$b}
a='\' b='\'$a text=${text//"$a"/"$b"}
a='$' b='\'$a text=${text//"$a"/"$b"}
a='"' b='\'$a text=${text//"$a"/"$b"}
a='`' b='\'$a text=${text//"$a"/"$b"}
fi
ble-edit/draw/put "$text"
}
function ble-edit/prompt/update/process-text {
local text=$1 a b
if [[ $text == *'"'* ]]; then
a='"' b='\"' text=${text//"$a"/$b}
a='"' b='\"' text=${text//"$a"/"$b"}
fi
ble-edit/draw/put "$text"
}
Expand Down Expand Up @@ -4632,17 +4632,17 @@ function ble-edit/exec:gexec/.setup {
((${#_ble_edit_exec_lines[@]}==0)) && return 1
ble/util/buffer.flush >&2

local apos=\' APOS="'\\''"
local q=\' Q="'\\''"
local cmd
local -a buff
local count=0
buff[${#buff[@]}]=ble-edit/exec:gexec/.begin
for cmd in "${_ble_edit_exec_lines[@]}"; do
if [[ "$cmd" == *[^' ']* ]]; then
# Note: $_ble_edit_exec_lastarg は $_ を設定するためのものである。
local prologue="ble-edit/exec:gexec/.eval-prologue '${cmd//$apos/$APOS}' \"\$_ble_edit_exec_lastarg\""
buff[${#buff[@]}]="builtin eval -- '${prologue//$apos/$APOS}"
buff[${#buff[@]}]="${cmd//$apos/$APOS}"
local prologue="ble-edit/exec:gexec/.eval-prologue '${cmd//$q/$Q}' \"\$_ble_edit_exec_lastarg\""
buff[${#buff[@]}]="builtin eval -- '${prologue//$q/$Q}"
buff[${#buff[@]}]="${cmd//$q/$Q}"
buff[${#buff[@]}]="ble-edit/exec:gexec/.save-last-arg'"
buff[${#buff[@]}]="ble-edit/exec:gexec/.eval-epilogue"
((count++))
Expand Down
10 changes: 5 additions & 5 deletions keymap/vi_test.sh
Expand Up @@ -31,10 +31,10 @@ function ble/keymap:vi_test/check {
((ntest++,nsuccess++))
else
((ntest++))
echo "test($section/$id): keys = ($kspecs)"
echo " initial = \"$i:${in//$nl/$NL}\""
echo " expected = \"$f:${fin//$nl/$NL}\""
echo " result = \"$_ble_edit_ind:${_ble_edit_str//$nl/$NL}\""
ble/util/print "test($section/$id): keys = ($kspecs)"
ble/util/print " initial = \"$i:${in//$nl/"$NL"}\""
ble/util/print " expected = \"$f:${fin//$nl/"$NL"}\""
ble/util/print " result = \"$_ble_edit_ind:${_ble_edit_str//$nl/"$NL"}\""
fi >&2

# restore states
Expand All @@ -53,7 +53,7 @@ function ble/keymap:vi_test/show-summary {
else
local tip=$'\e[31mfailed\e[m'
fi
echo "# $title test: result $((nsuccess))/$((ntest)) $tip"
ble/util/print "# $title test: result $((nsuccess))/$((ntest)) $tip"
}

#------------------------------------------------------------------------------
Expand Down
46 changes: 23 additions & 23 deletions lib/core-complete.sh
Expand Up @@ -48,29 +48,29 @@ function ble-complete/util/escape-specialchars {
eval "$ble_util_upvar_setup"
local a b ret="$*" chars=']['$' \t\n\\''"'\''`$|&;<>()*?{}!^'
if [[ $ret == *["$chars"]* ]]; then
a=\\ b="\\$a" ret="${ret//"$a"/$b}"
a=\" b="\\$a" ret="${ret//"$a"/$b}"
a=\' b="\\$a" ret="${ret//"$a"/$b}"
a=\` b="\\$a" ret="${ret//"$a"/$b}"
a=\$ b="\\$a" ret="${ret//"$a"/$b}"
a=' ' b="\\$a" ret="${ret//"$a"/$b}"
a=$'\t' b="\\$a" ret="${ret//"$a"/$b}"
a=$'\n' b="\$'\n'" ret="${ret//"$a"/$b}"
a=\| b="\\$a" ret="${ret//"$a"/$b}"
a=\& b="\\$a" ret="${ret//"$a"/$b}"
a=\; b="\\$a" ret="${ret//"$a"/$b}"
a=\< b="\\$a" ret="${ret//"$a"/$b}"
a=\> b="\\$a" ret="${ret//"$a"/$b}"
a=\( b="\\$a" ret="${ret//"$a"/$b}"
a=\) b="\\$a" ret="${ret//"$a"/$b}"
a=\[ b="\\$a" ret="${ret//"$a"/$b}"
a=\* b="\\$a" ret="${ret//"$a"/$b}"
a=\? b="\\$a" ret="${ret//"$a"/$b}"
a=\] b="\\$a" ret="${ret//"$a"/$b}"
a=\{ b="\\$a" ret="${ret//"$a"/$b}"
a=\} b="\\$a" ret="${ret//"$a"/$b}"
a=\! b="\\$a" ret="${ret//"$a"/$b}"
a=\^ b="\\$a" ret="${ret//"$a"/$b}"
a=\\ b='\'$a ret=${ret//"$a"/"$b"}
a='"' b='\'$a ret=${ret//"$a"/"$b"}
a=\' b='\'$a ret=${ret//"$a"/"$b"}
a=\` b='\'$a ret=${ret//"$a"/"$b"}
a=\$ b='\'$a ret=${ret//"$a"/"$b"}
a=' ' b='\'$a ret=${ret//"$a"/"$b"}
a=$'\t' b='\'$a ret=${ret//"$a"/"$b"}
a=$'\n' b=\$\'\\n\' ret=${ret//"$a"/"$b"}
a=\| b='\'$a ret=${ret//"$a"/"$b"}
a=\& b='\'$a ret=${ret//"$a"/"$b"}
a=\; b='\'$a ret=${ret//"$a"/"$b"}
a=\< b='\'$a ret=${ret//"$a"/"$b"}
a=\> b='\'$a ret=${ret//"$a"/"$b"}
a=\( b='\'$a ret=${ret//"$a"/"$b"}
a=\) b='\'$a ret=${ret//"$a"/"$b"}
a=\[ b='\'$a ret=${ret//"$a"/"$b"}
a=\* b='\'$a ret=${ret//"$a"/"$b"}
a=\? b='\'$a ret=${ret//"$a"/"$b"}
a=\] b='\'$a ret=${ret//"$a"/"$b"}
a=\{ b='\'$a ret=${ret//"$a"/"$b"}
a=\} b='\'$a ret=${ret//"$a"/"$b"}
a=\! b='\'$a ret=${ret//"$a"/"$b"}
a=\^ b='\'$a ret=${ret//"$a"/"$b"}
fi
eval "$ble_util_upvar"
}
Expand Down
36 changes: 18 additions & 18 deletions lib/core-syntax.sh
Expand Up @@ -932,14 +932,14 @@ function ble-syntax:bash/cclass/update {
local histc2=${_ble_syntax_bash_histc12:1:1}
for key in "${!_ble_syntax_bash_charsFmt[@]}"; do
local a=${_ble_syntax_bash_charsFmt[key]}
a=${a//@h/$histc1}
a=${a//@q/$histc2}
a=${a//@h/"$histc1"}
a=${a//@q/"$histc2"}
_ble_syntax_bash_chars[key]=$a
done

local a=$_ble_syntax_bash_chars_simpleFmt
a=${a//@h/$histc1}
a=${a//@q/$histc2}
a=${a//@h/"$histc1"}
a=${a//@q/"$histc2"}
_ble_syntax_bashc_simple=$a
fi

Expand Down Expand Up @@ -1019,7 +1019,7 @@ function ble-syntax:bash/simple-word/update {
local letter='\[[!^]|[^'${_ble_syntax_bashc_simple}']'
local param1='\$([-*@#?$!0_]|[1-9][0-9]*|[a-zA-Z_][a-zA-Z_0-9]*)'
local param2='\$\{(#?[-*@#?$!0]|[#!]?([1-9][0-9]*|[a-zA-Z_][a-zA-Z_0-9]*))\}' # ${!!} ${!$} はエラーになる。履歴展開の所為?
local squot='"[^"]*"|\$"([^"\]|\\.)*"'; squot="${squot//\"/$q}"
local squot='"[^"]*"|\$"([^"\]|\\.)*"'; squot=${squot//\"/"$q"}
local dquot='\$?"([^'${_ble_syntax_bash_chars[CTX_QUOT]}']|\\.)*"'
_ble_syntax_bash_simple_rex_element='(\\.|'$squot'|'$dquot'|'$param1'|'$param2'|'$letter')'
_ble_syntax_bash_simple_rex_word='^'$_ble_syntax_bash_simple_rex_element'+$'
Expand Down Expand Up @@ -1442,7 +1442,7 @@ function ble-syntax:bash/check-history-expansion/.initialize-event {
local A="[$histc1]"
[[ $histc1 == '^' ]] && A='\^'
rex_event=$_ble_syntax_bash_histexpand_RexEventFmt
rex_event=${rex_event//@A/$A}
rex_event=${rex_event//@A/"$A"}
fi
}
## 関数 ble-syntax:bash/check-history-expansion/.initialize-quicksub
Expand All @@ -1453,8 +1453,8 @@ function ble-syntax:bash/check-history-expansion/.initialize-quicksub {
rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubDef
else
rex_quicksub=$_ble_syntax_bash_histexpand_RexQuicksubFmt
rex_quicksub=${rex_quicksub//@A/[$histc2]}
rex_quicksub=${rex_quicksub//@C/$histc2}
rex_quicksub=${rex_quicksub//@A/"[$histc2]"}
rex_quicksub=${rex_quicksub//@C/"$histc2"}
fi
}
function ble-syntax:bash/check-history-expansion/.check-modifiers {
Expand Down Expand Up @@ -3311,10 +3311,10 @@ function ble-syntax:bash/ctx-heredoc-word/remove-quotes {
if rex='^\$?"(([^\"]|\\.)*)(\\?$|")'; [[ $text =~ $rex ]]; then
local str=${BASH_REMATCH[1]}
local a b
b='\`' a='`'; str="${str//"$b"/$a}"
b='\"' a='"'; str="${str//"$b"/$a}"
b='\$' a='$'; str="${str//"$b"/$a}"
b='\\' a='\'; str="${str//"$b"/$a}"
b='\`' a='`'; str="${str//"$b"/"$a"}"
b='\"' a='"'; str="${str//"$b"/"$a"}"
b='\$' a='$'; str="${str//"$b"/"$a"}"
b='\\' a='\'; str="${str//"$b"/"$a"}"
result=$result$str
text=${text:${#BASH_REMATCH}}
continue
Expand Down Expand Up @@ -3350,12 +3350,12 @@ function ble-syntax:bash/ctx-heredoc-word/escape-delimiter {
local ret=$1
if [[ $ret == *[\\\'$_ble_syntax_bash_IFS$_ble_term_FS]* ]]; then
local a b fs=$_ble_term_FS
a=\\ ; b="\\$a"; ret="${ret//"$a"/$b}"
a=\' ; b="\\$a"; ret="${ret//"$a"/$b}"
a=' ' ; b="$_ble_syntax_bash_heredoc_EscSP"; ret="${ret//"$a"/$b}"
a=$'\t'; b="$_ble_syntax_bash_heredoc_EscHT"; ret="${ret//"$a"/$b}"
a=$'\n'; b="$_ble_syntax_bash_heredoc_EscLF"; ret="${ret//"$a"/$b}"
a=$fs ; b="$_ble_syntax_bash_heredoc_EscFS"; ret="${ret//"$a"/$b}"
a=\\ ; b="\\$a"; ret="${ret//"$a"/"$b"}"
a=\' ; b="\\$a"; ret="${ret//"$a"/"$b"}"
a=' ' ; b="$_ble_syntax_bash_heredoc_EscSP"; ret="${ret//"$a"/"$b"}"
a=$'\t'; b="$_ble_syntax_bash_heredoc_EscHT"; ret="${ret//"$a"/"$b"}"
a=$'\n'; b="$_ble_syntax_bash_heredoc_EscLF"; ret="${ret//"$a"/"$b"}"
a=$fs ; b="$_ble_syntax_bash_heredoc_EscFS"; ret="${ret//"$a"/"$b"}"
fi
escaped=$ret
}
Expand Down
4 changes: 2 additions & 2 deletions memo/D0628.extract-global-values.sh
Expand Up @@ -302,7 +302,7 @@ if [[ $mode == test5d ]]; then

((__ble_i==__ble_MaxLoop)) && __ble_error=1 __ble_value=NOT_FOUND

echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down Expand Up @@ -331,7 +331,7 @@ if [[ $mode == test5d ]]; then

((__ble_found)) || __ble_error= __ble_value=NOT_FOUND

echo "declare $__ble_name='${__ble_value//$__ble_q//$__ble_Q}'"
echo "declare $__ble_name='${__ble_value//$__ble_q/$__ble_Q}'"
done

[[ ! $__ble_error ]]
Expand Down

0 comments on commit 359a389

Please sign in to comment.