From c3362aefa2e762211409923cfff065541bebf9e7 Mon Sep 17 00:00:00 2001 From: Saumit Dinesan Date: Sun, 22 May 2022 00:37:40 +0530 Subject: ble.sh & z4h addition --- .local/src/blesh/lib/vim-surround.sh | 551 +++++++++++++++++++++++++++++++++++ 1 file changed, 551 insertions(+) create mode 100644 .local/src/blesh/lib/vim-surround.sh (limited to '.local/src/blesh/lib/vim-surround.sh') diff --git a/.local/src/blesh/lib/vim-surround.sh b/.local/src/blesh/lib/vim-surround.sh new file mode 100644 index 0000000..fc5002c --- /dev/null +++ b/.local/src/blesh/lib/vim-surround.sh @@ -0,0 +1,551 @@ +# this script is a part of blesh (https://github.com/akinomyoga/ble.sh) under BSD-3-Clause license +source "$_ble_base/keymap/vi.sh" +bleopt/declare -n vim_surround_45 $'$(\r)' # ysiw- +bleopt/declare -n vim_surround_61 $'$((\r))' # ysiw= +bleopt/declare -n vim_surround_q \" # ysiwQ +bleopt/declare -n vim_surround_Q \' # ysiwq +bleopt/declare -v vim_surround_omap_bind 1 +function ble/lib/vim-surround.sh/get-char-from-key { + local key=$1 + if ! ble-decode-key/ischar "$key"; then + local flag=$((key&_ble_decode_MaskFlag)) code=$((key&_ble_decode_MaskChar)) + if ((flag==_ble_decode_Ctrl&&63<=code&&code<128&&(code&0x1F)!=0)); then + ((key=code==63?127:code&0x1F)) + else + return 1 + fi + fi + ble/util/c2s "$key" + return 0 +} +function ble/lib/vim-surround.sh/async-inputtarget.hook { + local mode=$1 hook=${@:2:$#-2} key=${@:$#} ret + if ! ble/lib/vim-surround.sh/get-char-from-key "$key"; then + ble/widget/vi-command/bell + return 1 + fi + local c=$ret + if [[ :$mode: == *:digit:* && $c == [0-9] ]]; then + _ble_edit_arg=$_ble_edit_arg$c + _ble_decode_key__hook="ble/lib/vim-surround.sh/async-inputtarget.hook digit $hook" + return 147 + elif [[ :$mode: == *:init:* && $c == ' ' ]]; then + _ble_decode_key__hook="ble/lib/vim-surround.sh/async-inputtarget.hook space $hook" + return 147 + fi + if [[ $c == [$'\e\003'] ]]; then # C-[, C-c + ble/widget/vi-command/bell + return 1 + else + [[ $c == \' ]] && c="'\''" + [[ $mode == space ]] && c=' '$c + builtin eval -- "$hook '$c'" + 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 147 +} +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 147 +} +_ble_lib_vim_surround_previous_tag=html +function ble/lib/vim-surround.sh/load-template { + local ins=$1 + if [[ ${ins//[0-9]} && ! ${ins//[_0-9a-zA-Z]} ]]; then + local optname=bleopt_vim_surround_$ins + template=${!optname} + [[ $template ]] && return 0 + fi + local ret; ble/util/s2c "$ins" + local optname=bleopt_vim_surround_$ret + template=${!optname} + [[ $template ]] && return 0 + case "$ins" in + ([''} + _ble_lib_vim_surround_previous_tag=$tag + fi + local end_tag=${tag%%["$_ble_term_IFS"]*} + template="<$tag>"$'\r'"" ;; + ('(') template=$'( \r )' ;; + ('[') template=$'[ \r ]' ;; + ('{') template=$'{ \r }' ;; + (['b)']) template=$'(\r)' ;; + (['r]']) template=$'[\r]' ;; + (['B}']) template=$'{\r}' ;; + (['a>']) template=$'<\r>' ;; + ([a-zA-Z]) return 1 ;; + (*) template=$ins ;; + esac +} &>/dev/null +function ble/lib/vim-surround.sh/surround { + local text=$1 ins=$2 opts=$3 + local instype= + [[ $ins == $'\x1D' ]] && ins='}' instype=indent # C-], C-} + local has_space= + [[ $ins == ' '?* ]] && ins=${ins:1} has_space=1 + local template= + ble/lib/vim-surround.sh/load-template "$ins" || return 1 + local prefix= suffix= + if [[ $template == *$'\r'* ]]; then + prefix=${template%%$'\r'*} + suffix=${template#*$'\r'} + else + prefix=$template + suffix=$template + fi + if [[ $prefix == *' ' && $suffix == ' '* ]]; then + prefix=${prefix::${#prefix}-1} + suffix=${suffix:1} + has_space=1 + fi + if [[ $instype == indent || :$opts: == *:linewise:* ]]; then + ble-edit/content/find-logical-bol "$beg"; local bol=$ret + ble-edit/content/find-non-space "$bol"; local nol=$ret + local indent= + if [[ $instype == indent ]] || ((bol' + ble/widget/self-insert + ble/widget/vi_cmap/accept + ble/decode/widget/suppress-widget + fi +} +_ble_lib_vim_surround_ys_type= # ys | yS | vS | vgS +_ble_lib_vim_surround_ys_args=() +_ble_lib_vim_surround_ys_ranges=() +function ble/highlight/layer:region/mark:vi_surround/get-selection { + local type=$_ble_lib_vim_surround_ys_type + local context=${_ble_lib_vim_surround_ys_args[2]} + if [[ $context == block ]]; then + local -a sub_ranges + sub_ranges=("${_ble_lib_vim_surround_ys_ranges[@]}") + selection=() + local sub + for sub in "${sub_ranges[@]}"; do + ble/string#split sub : "$sub" + ((sub[0]]t']) obj1=i$del obj2=a$del ;; + (['({<[']) obj1=i$del obj2=a$del trim=1 ;; + ([a-zA-Z]) obj1=i$del obj2=a$del ;; + esac + local beg end + if [[ $obj1 && $obj2 ]]; then + local surround_beg=$_ble_edit_ind surround_end=$_ble_edit_ind + ble/keymap:vi/text-object.impl "$arg" surround-extract-region '' "$obj2" + beg=$surround_beg end=$surround_end + elif [[ $del == / ]]; then + local rex='(/\*([^/]|/[^*])*/?){1,'$arg'}$' + [[ ${_ble_edit_str::_ble_edit_ind+2} =~ $rex ]] || return 1 + beg=$((_ble_edit_ind+2-${#BASH_REMATCH})) + ble/string#index-of "${_ble_edit_str:beg+2}" '*/' || return 1 + end=$((beg+ret+4)) + elif [[ $del ]]; then + local ret + ble-edit/content/find-logical-bol; local bol=$ret + ble-edit/content/find-logical-eol; local eol=$ret + local line=${_ble_edit_str:bol:eol-bol} + local ind=$((_ble_edit_ind-bol)) + if ble/string#last-index-of "${line::ind}" "$del"; then + beg=$ret + elif local base=$((ind-(2*${#del}-1))); ((base>=0||(base=0))) + ble/string#index-of "${line:base:ind+${#del}-base}" "$del"; then + beg=$((base+ret)) + else + return 1 + fi + ble/string#index-of "${line:beg+${#del}}" "$del" || return 1 + end=$((beg+2*${#del}+ret)) + ((beg+=bol,end+=bol)) + fi + _ble_lib_vim_surround_cs[11]=$del + _ble_lib_vim_surround_cs[12]=$obj1 + _ble_lib_vim_surround_cs[13]=$obj2 + _ble_lib_vim_surround_cs[14]=$beg + _ble_lib_vim_surround_cs[15]=$end + _ble_lib_vim_surround_cs[16]=$arg + _ble_lib_vim_surround_cs[17]=$trim +} +function ble/widget/vim-surround.sh/nmap/csurround.replace { + local ins=$1 + local type=${_ble_lib_vim_surround_cs[0]} + local arg=${_ble_lib_vim_surround_cs[1]} + local reg=${_ble_lib_vim_surround_cs[2]} + local del=${_ble_lib_vim_surround_cs[3]} + local del2=${_ble_lib_vim_surround_cs[11]} + local obj1=${_ble_lib_vim_surround_cs[12]} + local obj2=${_ble_lib_vim_surround_cs[13]} + local beg=${_ble_lib_vim_surround_cs[14]} + local end=${_ble_lib_vim_surround_cs[15]} + local arg2=${_ble_lib_vim_surround_cs[16]} + local surround_ins=$ins + local surround_type=$type + local surround_trim=${_ble_lib_vim_surround_cs[17]} + if [[ $obj1 && $obj2 ]]; then + local ind=$_ble_edit_ind + local _ble_edit_kill_ring _ble_edit_kill_type + ble/keymap:vi/text-object.impl "$arg2" y '' "$obj1"; local ext=$? + _ble_edit_ind=$ind + ((ext!=0)) && return 1 + local surround_content=$_ble_edit_kill_ring + ble/keymap:vi/text-object.impl "$arg2" surround '' "$obj2" || return 1 + elif [[ $del2 == / ]]; then + local surround_content=${_ble_edit_str:beg+2:end-beg-4} + ble/keymap:vi/call-operator surround "$beg" "$end" char '' '' + _ble_edit_ind=$beg + elif [[ $del2 ]]; then + local surround_content=${_ble_edit_str:beg+${#del2}:end-beg-2*${#del2}} + ble/keymap:vi/call-operator surround "$beg" "$end" char '' '' + _ble_edit_ind=$beg + else + ble/widget/vi-command/bell + return 1 + fi + ble/widget/vim-surround.sh/nmap/csurround.record "$type" "$arg" "$reg" "$del" "$ins" + ble/keymap:vi/adjust-command-mode + return 0 +} +function ble/widget/vim-surround.sh/nmap/csurround.record { + [[ $_ble_keymap_vi_mark_suppress_edit ]] && return 0 + local type=$1 arg=$2 reg=$3 del=$4 ins=$5 + local WIDGET=ble/widget/vim-surround.sh/nmap/csurround.repeat ARG=$arg FLAG= REG=$reg + ble/keymap:vi/repeat/record + if [[ $_ble_decode_keymap == vi_imap ]]; then + _ble_keymap_vi_repeat_insert[10]=$type + _ble_keymap_vi_repeat_insert[11]=$del + _ble_keymap_vi_repeat_insert[12]=$ins + else + _ble_keymap_vi_repeat[10]=$type + _ble_keymap_vi_repeat[11]=$del + _ble_keymap_vi_repeat[12]=$ins + fi +} +function ble/widget/vim-surround.sh/nmap/csurround.repeat { + local ARG FLAG REG; ble/keymap:vi/get-arg 1 + local type=${_ble_keymap_vi_repeat[10]} + local del=${_ble_keymap_vi_repeat[11]} + local ins=${_ble_keymap_vi_repeat[12]} + ble/widget/vim-surround.sh/nmap/csurround.initialize "$type" "$ARG" "$REG" && + ble/widget/vim-surround.sh/nmap/csurround.set-delimiter "$del" && + ble/widget/vim-surround.sh/nmap/csurround.replace "$ins" && return 0 + ble/widget/vi-command/bell + return 1 +} +function ble/widget/vim-surround.sh/nmap/dsurround { + local ARG FLAG REG; ble/keymap:vi/get-arg 1 + ble/widget/vim-surround.sh/nmap/csurround.initialize ds "$ARG" "$REG" + ble/lib/vim-surround.sh/async-inputtarget ble/widget/vim-surround.sh/nmap/dsurround.hook +} +function ble/widget/vim-surround.sh/nmap/dsurround.hook { + local del=$1 + ble/widget/vim-surround.sh/nmap/csurround.set-delimiter "$del" && + ble/widget/vim-surround.sh/nmap/csurround.replace '' && return 0 + ble/widget/vi-command/bell + return 1 +} +function ble/highlight/layer:region/mark:vi_csurround/get-selection { + local beg=${_ble_lib_vim_surround_cs[14]} + local end=${_ble_lib_vim_surround_cs[15]} + selection=("$beg" "$end") +} +function ble/highlight/layer:region/mark:vi_csurround/get-face { + face=region_target +} +function ble/widget/vim-surround.sh/nmap/csurround { + ble/widget/vim-surround.sh/nmap/csurround.impl cs +} +function ble/widget/vim-surround.sh/nmap/cSurround { + ble/widget/vim-surround.sh/nmap/csurround.impl cS +} +function ble/widget/vim-surround.sh/nmap/csurround.impl { + local ARG FLAG REG; ble/keymap:vi/get-arg 1 + local type=$1 + ble/widget/vim-surround.sh/nmap/csurround.initialize "$type" "$ARG" "$REG" + ble/lib/vim-surround.sh/async-inputtarget ble/widget/vim-surround.sh/nmap/csurround.hook1 +} +function ble/widget/vim-surround.sh/nmap/csurround.hook1 { + local del=$1 + if [[ $del ]] && ble/widget/vim-surround.sh/nmap/csurround.set-delimiter "$del"; then + _ble_edit_mark_active=vi_csurround + ble/lib/vim-surround.sh/async-inputtarget-noarg ble/widget/vim-surround.sh/nmap/csurround.hook2 + return "$?" + fi + _ble_lib_vim_surround_cs=() + ble/widget/vi-command/bell + return 1 +} +function ble/widget/vim-surround.sh/nmap/csurround.hook2 { + local ins=$1 + if local rex='^ ?[