Skip to content

Commit

Permalink
init: correctly restore unset "IFS" and work around non-canonical "IFS"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 20, 2021
1 parent 4d77fab commit c33fad0
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 92 deletions.
2 changes: 2 additions & 0 deletions archive/getopt.sh
Expand Up @@ -81,10 +81,12 @@ function ble/getopt.init {
OPTIND=1 OPTION= OPTARG=
}
function ble/getopt.print-argument-message {
local IFS=$_ble_term_IFS
local index=$((OPTIND-1))
echo "${_optargs[0]##*/} (argument#$index \`${_optargs[index]}'): $*" >&2
}
function ble/getopt.print-message {
local IFS=$_ble_term_IFS
local index=$((OPTIND-1))
echo "${_optargs[0]##*/} (arguments): $*" >&2
}
Expand Down
11 changes: 8 additions & 3 deletions ble.pp
Expand Up @@ -146,6 +146,7 @@ function ble/base/restore-POSIXLY_CORRECT {
return 1 2>/dev/null || builtin exit 1
fi

_ble_init_original_IFS_set=${IFS+set}
_ble_init_original_IFS=$IFS
IFS=$' \t\n'

Expand Down Expand Up @@ -607,7 +608,6 @@ function ble-update {
#%x inc.r|@|lib/core-complete-def|
#%x inc.r|@|lib/core-syntax-def|
#------------------------------------------------------------------------------
# function .ble-time { echo "$*"; time "$@"; }

_ble_attached=
function ble-attach {
Expand Down Expand Up @@ -781,8 +781,13 @@ function ble/base/process-blesh-arguments {
ble/base/process-blesh-arguments "$@"

# 状態復元
IFS=$_ble_init_original_IFS
unset -v _ble_init_original_IFS
if [[ $_ble_init_original_IFS_set ]]; then
IFS=$_ble_init_original_IFS
else
builtin unset -v IFS
fi
builtin unset -v _ble_init_original_IFS_set
builtin unset -v _ble_init_original_IFS
if [[ ! $_ble_attached ]]; then
ble/base/restore-bash-options
ble/base/restore-POSIXLY_CORRECT
Expand Down
1 change: 1 addition & 0 deletions keymap/emacs.sh
Expand Up @@ -48,6 +48,7 @@ function ble/keymap:emacs/is-command-white {
# frequently used command is checked first
return 0
elif [[ $1 == ble/widget/* ]]; then
local IFS=$_ble_term_IFS
local cmd=${1#ble/widget/}; cmd=${cmd%%[$' \t\n']*}
[[ $cmd == emacs/* || " ${_ble_keymap_emacs_white_list[*]} " == *" $cmd "* ]] && return 0
fi
Expand Down
22 changes: 16 additions & 6 deletions keymap/vi.sh
Expand Up @@ -81,7 +81,7 @@ function ble-edit/content/nonbol-eolp {
## 関数 ble/keymap:vi/string#encode-rot13 text...
## @var[out] ret
function ble/keymap:vi/string#encode-rot13 {
local text=$*
local text=$1
local -a buff=() ch
for ((i=0;i<${#text};i++)); do
ch=${text:i:1}
Expand Down Expand Up @@ -178,6 +178,7 @@ function ble/keymap:vi/imap-repeat/pop {
((top_index>=0)) && unset -v '_ble_keymap_vi_irepeat[top_index]'
}
function ble/keymap:vi/imap-repeat/push {
local IFS=$_ble_term_IFS
ble/array#push _ble_keymap_vi_irepeat "${KEYS[*]-}:$WIDGET"
}

Expand Down Expand Up @@ -236,6 +237,7 @@ function ble/keymap:vi/imap/is-command-white {
# frequently used command is checked first
return 0
elif [[ $1 == ble/widget/* ]]; then
local IFS=$_ble_term_IFS
local cmd=${1#ble/widget/}; cmd=${cmd%%[$' \t\n']*}
[[ $cmd == vi_imap/* || " ${_ble_keymap_vi_imap_white_list[*]} " == *" $cmd "* ]] && return 0
fi
Expand Down Expand Up @@ -803,6 +805,7 @@ function ble/keymap:vi/register#set-edit {
ble/keymap:vi/register#set "$@" || return 1
local reg=$1 type=$2 content=$3
if [[ $reg == '' || $reg == 34 ]]; then
local IFS=$_ble_term_IFS
local widget=${WIDGET%%[$' \t\n']*}
if [[ $content == *$'\n'* || " $widget " == " ${_ble_keymap_vi_register_49_widget_list[*]} " ]]; then
local n
Expand Down Expand Up @@ -1335,7 +1338,8 @@ function ble/keymap:vi/operator:d {
done

# yank
IFS=$'\n' eval 'local yank_content="${atext[*]-}"'
IFS=$'\n' builtin eval 'local yank_content="${atext[*]-}"'
local IFS=$_ble_term_IFS
local yank_type=B:"${afill[*]-}"
ble/keymap:vi/register#set-edit "$reg" "$yank_type" "$yank_content" || return 1

Expand Down Expand Up @@ -1424,7 +1428,8 @@ function ble/keymap:vi/operator:y {
ble/array#push atext "$stext"
done

IFS=$'\n' eval 'local yank_content="${atext[*]-}"'
IFS=$'\n' builtin eval 'local yank_content="${atext[*]-}"'
local IFS=$_ble_term_IFS
yank_type=B:"${afill[*]-}"
else
yank_type=
Expand Down Expand Up @@ -2226,7 +2231,8 @@ function ble/widget/vi-command/linewise-goto.impl {
# single char arguments

function ble/keymap:vi/async-read-char.hook {
local command=${@:1:$#-1} key=${@:$#}
local IFS=$_ble_term_IFS
local command="${*:1:$#-1}" key="${*:$#}"
if ((key==(_ble_decode_Ctrl|0x6B))); then # C-k
ble-decode/keymap/push vi_digraph
_ble_keymap_vi_digraph__hook="$command"
Expand All @@ -2236,6 +2242,7 @@ function ble/keymap:vi/async-read-char.hook {
}

function ble/keymap:vi/async-read-char {
local IFS=$_ble_term_IFS
_ble_decode_key__hook="ble/keymap:vi/async-read-char.hook $*"
return 148
}
Expand Down Expand Up @@ -2291,6 +2298,7 @@ function ble/keymap:vi/mark/update-mark-history {
local value=${_ble_keymap_vi_mark_local[imark]}
ble/array#push save "$imark:$value"
done
local IFS=$_ble_term_IFS
_ble_keymap_vi_mark_history[_ble_keymap_vi_mark_hindex]="${save[*]-}"

# load
Expand Down Expand Up @@ -2559,6 +2567,7 @@ function ble/keymap:vi/repeat/record-special {
return 1
}
function ble/keymap:vi/repeat/record-normal {
local IFS=$_ble_term_IFS
local -a repeat; repeat=("$KEYMAP" "${KEYS[*]-}" "$WIDGET" "$ARG" "$FLAG" "$REG" '')
if [[ $KEYMAP == vi_[xs]map ]]; then
repeat[6]=$_ble_keymap_vi_xmap_prev_edit
Expand All @@ -2585,6 +2594,7 @@ function ble/keymap:vi/repeat/record-insert {
_ble_keymap_vi_repeat_irepeat=("${_ble_keymap_vi_irepeat[@]}")
elif ((${#_ble_keymap_vi_irepeat[@]})); then
# 挿入モード突入操作が初期化されていたら、挿入操作がある時のみに記録
local IFS=$_ble_term_IFS
_ble_keymap_vi_repeat=(vi_nmap "${KEYS[*]-}" ble/widget/vi_nmap/insert-mode 1 '' '')
_ble_keymap_vi_repeat_irepeat=("${_ble_keymap_vi_irepeat[@]}")
fi
Expand All @@ -2601,7 +2611,7 @@ function ble/keymap:vi/repeat/invoke {
local repeat_arg=$_ble_edit_arg
local repeat_reg=$_ble_keymap_vi_reg
local KEYMAP=${_ble_keymap_vi_repeat[0]}
local -a KEYS=(${_ble_keymap_vi_repeat[1]})
local -a KEYS; ble/string#split-words KEYS "${_ble_keymap_vi_repeat[1]}"
local WIDGET=${_ble_keymap_vi_repeat[2]}

# keymap の状態復元
Expand Down Expand Up @@ -3433,7 +3443,7 @@ function ble/widget/vi_nmap/paste.impl/block {

local ret cols=$_ble_textmap_cols

local -a afill=(${_ble_edit_kill_type:2})
local -a afill; ble/string#split-words afill "${_ble_edit_kill_type:2}"
local atext; ble/string#split-lines atext "$_ble_edit_kill_ring"
local ntext=${#atext[@]}

Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete.sh
Expand Up @@ -481,7 +481,7 @@ function ble/complete/source/reduce-compv-for-ambiguous-match {
## @param[in] DATA
## @var[in] COMP_PREFIX
function ble/complete/cand/yield {
local ACTION=$1 CAND=$2 DATA="${*:3}"
local ACTION=$1 CAND=$2 DATA=$3
[[ $flag_force_fignore ]] && ! ble/complete/.fignore/filter "$CAND" && return

local PREFIX_LEN=0
Expand Down
4 changes: 3 additions & 1 deletion lib/core-syntax.sh
Expand Up @@ -4176,6 +4176,8 @@ function ble/syntax/parse {
local end0=${4:-$end}
((end==beg&&end0==beg&&_ble_syntax_dbeg<0)) && return

local IFS=$_ble_term_IFS

local -r iN=${#text} shift=$((end-end0))
#%if !release
if ! ((0<=beg&&beg<=end&&end<=iN&&beg<=end0)); then
Expand Down Expand Up @@ -5616,7 +5618,7 @@ function ble/highlight/layer:syntax/update-error-table {
if ((jN)); then
for ((j=0;j<jN;j++)); do
local -a range
range=(${_ble_highlight_layer_syntax3_list[j]})
ble/string#split-words range "${_ble_highlight_layer_syntax3_list[j]}"

local a=${range[0]} b=${range[1]}
((a>=DMAX0?(a+=DMAX-DMAX0):(a>=DMIN&&(a=DMIN)),
Expand Down
2 changes: 1 addition & 1 deletion lib/init-bind.sh
Expand Up @@ -79,7 +79,7 @@ function ble/init:bind/generate-binder {
local bind18XX=0
if ((40400<=_ble_bash&&_ble_bash<50000)); then
# Insert a dummy entry in "cmd_xmap"
ble/util/print "[[ -o emacs ]] && bind 'set keyseq-timeout 1'" >> "$fbind1"
ble/util/print "[[ -o emacs ]] && builtin bind 'set keyseq-timeout 1'" >> "$fbind1"
fbind2=$fbind1 ble/init:bind/append '\C-x\C-x' 24 '[[ -o emacs ]]'
elif ((_ble_bash<40300)); then
bind18XX=1
Expand Down
2 changes: 2 additions & 0 deletions lib/init-term.sh
Expand Up @@ -18,12 +18,14 @@ function ble/init:term/register-varname {
}

function ble/init:term/define-cap {
local IFS=$_ble_term_IFS
local name=$1 def=$2
shift 2
ble/util/assign "$name" "ble/init:term/tput $* || echo -n \"\$def\""
ble/init:term/register-varname "$name"
}
function ble/init:term/define-cap.2 {
local IFS=$_ble_term_IFS
local name=$1 def=$2
shift 2
ble/util/assign "$name" "echo -n x; ble/init:term/tput $* || echo -n \"\$def\"; echo -n x"
Expand Down
2 changes: 2 additions & 0 deletions lib/vim-surround.sh
Expand Up @@ -128,10 +128,12 @@ function ble/lib/vim-surround.sh/async-inputtarget.hook {
fi
}
function ble/lib/vim-surround.sh/async-inputtarget {
local IFS=$_ble_term_IFS
_ble_decode_key__hook="ble/lib/vim-surround.sh/async-inputtarget.hook init:digit $*"
return 148
}
function ble/lib/vim-surround.sh/async-inputtarget-noarg {
local IFS=$_ble_term_IFS
_ble_decode_key__hook="ble/lib/vim-surround.sh/async-inputtarget.hook init $*"
return 148
}
Expand Down
23 changes: 12 additions & 11 deletions src/canvas.sh
Expand Up @@ -361,7 +361,7 @@ function ble/canvas/attach {
# ble/canvas

function ble/canvas/put.draw {
DRAW_BUFF[${#DRAW_BUFF[*]}]="$*"
DRAW_BUFF[${#DRAW_BUFF[*]}]=$1
}
function ble/canvas/put-ind.draw {
local count=${1-1}
Expand Down Expand Up @@ -729,7 +729,7 @@ function ble/canvas/trace/.process-csi-sequence {
# CUP "CSI H"
# HVP "CSI f"
local -a params
params=(${param//[^0-9]/ })
ble/string#split-words params "${param//[^0-9]/ }"
local x1 y1
((x1=params[1]-1))
((y1=params[0]-1))
Expand Down Expand Up @@ -1533,12 +1533,12 @@ function ble/textmap#getxy.cur {
fi

local -a _pos
_pos=(${_ble_textmap_pos[$1]})
ble/string#split-words _pos "${_ble_textmap_pos[$1]}"

# 追い出しされたか check
if (($1<_ble_textmap_length)); then
local -a _eoc
_eoc=(${_ble_textmap_pos[$1+1]})
ble/string#split-words _eoc "${_ble_textmap_pos[$1+1]}"
((_eoc[2])) && ((_pos[0]=0,_pos[1]++))
fi

Expand Down Expand Up @@ -1577,16 +1577,17 @@ function ble/textmap#get-index-at {
## 関数 ble/textmap#hit/.getxy.cur index
## @var[in,out] pos
function ble/textmap#hit/.getxy.out {
set -- ${_ble_textmap_pos[$1]}
x=$1 y=$2
local a
ble/string#split-words a "${_ble_textmap_pos[$1]}"
x=${a[0]} y=${a[1]}
}
function ble/textmap#hit/.getxy.cur {
local index=$1
set -- ${_ble_textmap_pos[index]}
x=$1 y=$2
local index=$1 a
ble/string#split-words a "${_ble_textmap_pos[index]}"
x=${a[0]} y=${a[1]}
if ((index<_ble_textmap_length)); then
set -- ${_ble_textmap_pos[index+1]}
(($3)) && ((x=0,y++))
ble/string#split-words a "${_ble_textmap_pos[index+1]}"
((a[2])) && ((x=0,y++))
fi
}

Expand Down
1 change: 1 addition & 0 deletions src/color.sh
Expand Up @@ -835,6 +835,7 @@ function ble/highlight/layer:region/.update-dirty-range {
}

function ble/highlight/layer:region/update {
local IFS=$_ble_term_IFS
local omin=-1 omax=-1 osgr= olen=${#_ble_highlight_layer_region_osel[@]}
if ((olen)); then
omin=${_ble_highlight_layer_region_osel[0]}
Expand Down
21 changes: 14 additions & 7 deletions src/decode.sh
Expand Up @@ -297,6 +297,7 @@ ble-decode-kbd/.initialize
## @var[out] ret
function ble-decode-kbd {
local kspecs; ble/string#split-words kspecs "$*"
local IFS=$_ble_term_IFS
local kspec code codes
codes=()
for kspec in "${kspecs[@]}"; do
Expand Down Expand Up @@ -371,6 +372,7 @@ function ble-decode-unkbd/.single-key {
## キーを表す整数値の列を指定します。
## @var[out] ret
function ble-decode-unkbd {
local IFS=$_ble_term_IFS
local -a kspecs
local key
for key in $*; do
Expand Down Expand Up @@ -765,9 +767,9 @@ function ble-decode-char {
# シーケンスが登録されていない時
if [[ $_ble_decode_char2_reach ]]; then
local reach rest
reach=($_ble_decode_char2_reach)
ble/string#split-words reach "$_ble_decode_char2_reach"
rest=${_ble_decode_char2_seq:reach[1]}
rest=(${rest//_/ } $char)
ble/string#split-words rest "${rest//_/ } $char"

_ble_decode_char2_reach=
_ble_decode_char2_seq=
Expand Down Expand Up @@ -976,6 +978,7 @@ function ble-decode-char/unbind {
done
}
function ble-decode-char/dump {
local IFS=$_ble_term_IFS
local tseq=$1 nseq ccode
nseq=("${@:2}")
builtin eval "local -a ccodes; ccodes=(\${!_ble_decode_cmap_$tseq[@]})"
Expand Down Expand Up @@ -1072,10 +1075,10 @@ function ble-decode/DEFAULT_KEYMAP {

## 設定関数 ble/widget/.SHELL_COMMAND command
## ble-bind -c で登録されたコマンドを処理します。
function ble/widget/.SHELL_COMMAND { eval "$*"; }
function ble/widget/.SHELL_COMMAND { local IFS=$_ble_term_IFS; builtin eval -- "$*"; }
## 設定関数 ble/widget/.EDIT_COMMAND command
## ble-bind -x で登録されたコマンドを処理します。
function ble/widget/.EDIT_COMMAND { eval "$*"; }
function ble/widget/.EDIT_COMMAND { local IFS=$_ble_term_IFS; builtin eval -- "$*"; }

## 関数 ble-decode-key/bind keycodes command
## @param[in] keycodes command
Expand Down Expand Up @@ -1365,7 +1368,8 @@ function ble-decode-key {
if [[ $bleopt_decode_error_kseq_discard ]]; then
_ble_decode_key__seq=
else
local -a keys=(${_ble_decode_key__seq//_/ } $key)
local -a keys
ble/string#split-words keys "${_ble_decode_key__seq//_/ } $key"
_ble_decode_key__seq=
# 2文字目以降を処理
ble-decode-key "${keys[@]:1}"
Expand Down Expand Up @@ -1536,7 +1540,7 @@ function ble-decode/widget/.call-keyseq {

# setup variables
local WIDGET=$command KEYMAP=$_ble_decode_keymap LASTWIDGET=$_ble_decode_widget_last
local -a KEYS=(${_ble_decode_key__seq//_/ } $key)
local -a KEYS; ble/string#split-words KEYS "${_ble_decode_key__seq//_/ } $key"
_ble_decode_widget_last=$WIDGET
_ble_decode_key__seq=

Expand All @@ -1555,7 +1559,7 @@ function ble-decode/widget/.call-async-read {

# setup variables
local WIDGET=$1 KEYMAP=$_ble_decode_keymap LASTWIDGET=$_ble_decode_widget_last
local -a KEYS=($2)
local -a KEYS; ble/string#split-words KEYS "$2"
builtin eval -- "$WIDGET"
}
## 関数 ble-decode/widget/call-interactively widget keys...
Expand Down Expand Up @@ -1630,6 +1634,7 @@ function ble-decode/start-keylog {
}
function ble-decode/end-keylog {
{
local IFS=$_ble_term_IFS
echo '===== bytes ====='
printf '%s\n' "${_ble_keylogger_bytes[*]}"
echo
Expand Down Expand Up @@ -2613,6 +2618,7 @@ function ble/builtin/bind/option:r {

function ble/builtin/bind/rlfunc2widget {
local kmap=$1 rlfunc=$2
local IFS=$_ble_term_IFS

local rlfunc_dict=
case $kmap in
Expand Down Expand Up @@ -2726,6 +2732,7 @@ function ble/builtin/bind/option:- {
}
function ble/builtin/bind/.process {
flags=
local IFS=$_ble_term_IFS
local opt_literal= opt_keymap= opt_print=
local -a opt_queries=()
while (($#)); do
Expand Down

0 comments on commit c33fad0

Please sign in to comment.