Skip to content

Commit

Permalink
edit: combine multiple kills and copies
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 22, 2021
1 parent 40ae242 commit 66564e1
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 38 deletions.
38 changes: 19 additions & 19 deletions keymap/vi.sh
Expand Up @@ -1336,7 +1336,7 @@ function ble/keymap:vi/operator:d {
for rep in "${arep[@]}"; do
smin=${rep%%:*}; rep=${rep:${#smin}+1}
smax=${rep%%:*}; rep=${rep:${#smax}+1}
ble/widget/.replace-range "$smin" "$smax" "$rep" 1
ble/widget/.replace-range "$smin" "$smax" "$rep"
done
((beg+=slpad)) # fix start position
else
Expand All @@ -1357,7 +1357,7 @@ function ble/keymap:vi/operator:d {
fi

ble/keymap:vi/register#set-edit "$reg" '' "${_ble_edit_str:beg:end-beg}" || return 1
ble/widget/.delete-range "$beg" "$end" 0
ble/widget/.delete-range "$beg" "$end"
fi
fi
return 0
Expand All @@ -1374,7 +1374,7 @@ function ble/keymap:vi/operator:c {
ble-edit/content/find-non-space "$beg"; local nol=$ret
((beg<nol)) && indent=${_ble_edit_str:beg:nol-beg}

ble/widget/.replace-range "$beg" "$end2" "$indent" 1
ble/widget/.replace-range "$beg" "$end2" "$indent"
ble/widget/vi_nmap/.insert-mode
elif [[ $context == block ]]; then
ble/keymap:vi/operator:d "$@" || return 1 # @var beg will be overwritten here
Expand Down Expand Up @@ -1435,11 +1435,11 @@ function ble/keymap:vi/operator:tr.impl {
ble/string#split sub : "${sub_ranges[isub]}"
local smin=${sub[0]} smax=${sub[1]}
local ret; "$filter" "${_ble_edit_str:smin:smax-smin}"
ble/widget/.replace-range "$smin" "$smax" "$ret" 1
ble/widget/.replace-range "$smin" "$smax" "$ret"
done
else
local ret; "$filter" "${_ble_edit_str:beg:end-beg}"
ble/widget/.replace-range "$beg" "$end" "$ret" 1
ble/widget/.replace-range "$beg" "$end" "$ret"
fi
return 0
}
Expand Down Expand Up @@ -1536,7 +1536,7 @@ function ble/keymap:vi/operator:indent.impl/increase-block-indent {
ble/string#split sub : "${sub_ranges[isub]}"
smin=${sub[0]} slpad=${sub[2]}
ble/string#repeat ' ' $((slpad+width))
ble/widget/.replace-range "$smin" "$smin" "$ret" 1
ble/widget/.replace-range "$smin" "$smin" "$ret"
done
}
## 関数 ble/keymap:vi/operator:indent.impl/decrease-graphical-block-indent width
Expand Down Expand Up @@ -1579,7 +1579,7 @@ function ble/keymap:vi/operator:indent.impl/decrease-graphical-block-indent {
local rep
for rep in "${replaces[@]}"; do
ble/string#split rep : "$rep"
ble/widget/.replace-range "${rep[@]::3}" 1
ble/widget/.replace-range "${rep[@]::3}"
done
}
## 関数 ble/keymap:vi/operator:indent.impl/decrease-logical-block-indent width
Expand Down Expand Up @@ -1614,7 +1614,7 @@ function ble/keymap:vi/operator:indent.impl/decrease-logical-block-indent {

local padding=
((pad)) && { ble/string#repeat ' ' "$pad"; padding=$ret; }
ble/widget/.replace-range "$smin" "$nsp" "$padding" 1
ble/widget/.replace-range "$smin" "$nsp" "$padding"
done
}
function ble/keymap:vi/operator:indent.impl {
Expand All @@ -1634,7 +1634,7 @@ function ble/keymap:vi/operator:indent.impl {

local ret
ble/keymap:vi/string#increase-indent "${_ble_edit_str:beg:end-beg}" "$delta"; local content=$ret
ble/widget/.replace-range "$beg" "$end" "$content" 1
ble/widget/.replace-range "$beg" "$end" "$content"

if [[ $context == char ]]; then
ble-edit/content/find-non-space "$beg"
Expand Down Expand Up @@ -1799,7 +1799,7 @@ function ble/keymap:vi/operator:fold.impl {

local cols=${COLUMNS:-80}; ((cols>80&&(cols=80)))
ble/keymap:vi/operator:fold/.fold-paragraphwise "$old" "$cols"; local new=$ret
ble/widget/.replace-range "$beg" "$end" "$new" 1
ble/widget/.replace-range "$beg" "$end" "$new"

# 変換後のカーソル位置を修正。
if [[ :$opts: == *:preserve_point:* ]]; then
Expand Down Expand Up @@ -1903,7 +1903,7 @@ function ble/keymap:vi/operator:filter/.hook {
return 1
fi
new=${new%$'\n'}$'\n'
ble/widget/.replace-range "$beg" "$end" "$new" 1
ble/widget/.replace-range "$beg" "$end" "$new"

_ble_edit_ind=$beg
if [[ $context == line ]]; then
Expand Down Expand Up @@ -2785,7 +2785,7 @@ function ble/widget/vi_nmap/forward-char-toggle-case {

local index=$((_ble_edit_ind+len))
local ret; ble/string#toggle-case "${_ble_edit_str:_ble_edit_ind:len}"
ble/widget/.replace-range "$_ble_edit_ind" "$index" "$ret" 1
ble/widget/.replace-range "$_ble_edit_ind" "$index" "$ret"
ble/keymap:vi/mark/set-previous-edit-area "$_ble_edit_ind" "$index"
ble/keymap:vi/repeat/record
ble/keymap:vi/needs-eol-fix "$index" && ((index--))
Expand Down Expand Up @@ -3595,7 +3595,7 @@ function ble/widget/vi_nmap/paste.impl/block {
local i=${#ins_beg[@]}
while ((i--)); do
local ibeg=${ins_beg[i]} iend=${ins_end[i]} text=${ins_text[i]}
ble/widget/.replace-range "$ibeg" "$iend" "$text" 1
ble/widget/.replace-range "$ibeg" "$iend" "$text"
done
ble/keymap:vi/mark/end-edit-area
ble/keymap:vi/repeat/record
Expand Down Expand Up @@ -3631,7 +3631,7 @@ function ble/widget/vi_nmap/paste.impl {
((index=ret,dbeg=index,dend=index+${#content}-1))
fi

ble/widget/.replace-range "$index" "$index" "$content" 1
ble/widget/.replace-range "$index" "$index" "$content"
_ble_edit_ind=$dbeg
ble/keymap:vi/mark/set-previous-edit-area "$dbeg" "$dend"
ble/keymap:vi/repeat/record
Expand Down Expand Up @@ -5717,7 +5717,7 @@ function ble/widget/vi_nmap/increment.impl {
fi
fi

ble/widget/.replace-range "$beg" "$end" "$number" 1
ble/widget/.replace-range "$beg" "$end" "$number"
ble/keymap:vi/mark/set-previous-edit-area "$beg" $((beg+${#number}))
ble/keymap:vi/repeat/record
_ble_edit_ind=$((beg+${#number}-1))
Expand Down Expand Up @@ -6843,7 +6843,7 @@ function ble/widget/vi_xmap/visual-replace-char.hook {
((sfill)) && ins1=${ins1::(width-sfill)/w}
((slpad)) && { ble/string#repeat ' ' "$slpad"; ins1=$ret$ins1; }
((srpad)) && { ble/string#repeat ' ' "$srpad"; ins1=$ins1$ret; }
ble/widget/.replace-range "$smin" "$smax" "$ins1" 1
ble/widget/.replace-range "$smin" "$smax" "$ins1"
done
local beg=$smin
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--))
Expand All @@ -6862,7 +6862,7 @@ function ble/widget/vi_xmap/visual-replace-char.hook {

local ins=${_ble_edit_str:beg:end-beg}
ins=${ins//[!$'\n']/"$s"}
ble/widget/.replace-range "$beg" "$end" "$ins" 1
ble/widget/.replace-range "$beg" "$end" "$ins"
ble/keymap:vi/needs-eol-fix "$beg" && ((beg--))
_ble_edit_ind=$beg
ble/keymap:vi/mark/set-previous-edit-area "$beg" "$end"
Expand Down Expand Up @@ -7121,7 +7121,7 @@ function ble/widget/vi_xmap/block-insert-mode.onleave {
ble/keymap:vi/mark/commit-edit-area "$p1" "$p2"
while ((i--)); do
local index=${ins_beg[i]} text=${ins_text[i]}
ble/widget/.replace-range "$index" "$index" "$text" 1
ble/widget/.replace-range "$index" "$index" "$text"
done
ble/keymap:vi/mark/end-edit-area
# Note: この編集は record-insert 経由で記録されるので
Expand Down Expand Up @@ -7375,7 +7375,7 @@ function ble/widget/vi_xmap/increment.impl {
local smin=${sub%%:*}
local beg=$((shift+smin+offset))
local end=$((beg+length))
ble/widget/.replace-range "$beg" "$end" "$number" 1
ble/widget/.replace-range "$beg" "$end" "$number"
((shift+=${#number}-length,
dmin<0&&(dmin=beg),
dmax=beg+${#number}))
Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete.sh
Expand Up @@ -4106,7 +4106,7 @@ function ble/complete/insert {
fi

local ins=$insert$suffix
ble/widget/.replace-range "$insert_beg" "$insert_end" "$ins" 1
ble/widget/.replace-range "$insert_beg" "$insert_end" "$ins"
((_ble_edit_ind=insert_beg+${#ins},
_ble_edit_ind>${#_ble_edit_str}&&
(_ble_edit_ind=${#_ble_edit_str})))
Expand Down
4 changes: 2 additions & 2 deletions lib/vim-surround.sh
Expand Up @@ -361,7 +361,7 @@ function ble/widget/vim-surround.sh/ysurround.core {

((slpad)) && { ble/string#repeat ' ' "$slpad"; stext=$ret$stext; }
((srpad)) && { ble/string#repeat ' ' "$srpad"; stext=$stext$ret; }
ble/widget/.replace-range "$smin" "$smax" "$stext" 1
ble/widget/.replace-range "$smin" "$smax" "$stext"
done

else
Expand Down Expand Up @@ -391,7 +391,7 @@ function ble/widget/vim-surround.sh/ysurround.core {
fi
local text=$ret

ble/widget/.replace-range "$beg" "$end" "$text" 1
ble/widget/.replace-range "$beg" "$end" "$text"
fi

_ble_edit_ind=$beg
Expand Down
5 changes: 3 additions & 2 deletions memo/ChangeLog.md
Expand Up @@ -7,7 +7,7 @@

- complete/mandb: support mandb in FreeBSD `#D1432` 6c54f79
- decode (`ble-decode-kbd`): support various specifications of key sequences `#D1439` 0f01cab
- edit: support new option `bleopt edit_line_type={logical,graphical}` (motivated by 3ximus) `#D1442` 0000000
- edit: support new option `bleopt edit_line_type={logical,graphical}` (motivated by 3ximus) `#D1442` 40ae242

## Changes

Expand All @@ -23,7 +23,8 @@
- edit: work around the wrong job information of Bash in trap handlers (reported by 3ximus) `#D1435` `#D1436` bc4735e
- edit (command-help): work around the Bash bug that tempenv vanishes with `builtin eval` `#D1438` 8379d4a
- global: suppress missing locale errors (reported by 3ximus) `#D1440` 4d3c595
- edit (sword): fix definition of `sword` (shell words) (motivated by 3ximus) `#D1441` 0000000
- edit (sword): fix definition of `sword` (shell words) `#D1441` f923388
- edit (kill/copy): combine multiple kills and copies (suggested by 3ximus) `#D1443` 0000000

## Internal changes and fixes

Expand Down
83 changes: 83 additions & 0 deletions note.txt
Expand Up @@ -1200,6 +1200,17 @@ bash_tips
ToDo
-------------------------------------------------------------------------------

2021-01-22

* syntax: 引数が沢山あると cygwin で滅茶苦茶遅い

これは様々な種類のパス名展開を試そうとするのが原因だろうか。
command 名と同様にキャッシュする様にしても良いのではないだろうか。
でも少しずつ微妙に異なる引数が沢山ある場合には結局遅い。
それよりは着色自体の高速化について考えた方が良いのではないか。

* chat でも遅くなるかどうかについて確認する。

2021-01-08

* syntax: 算術式の quote が変である
Expand Down Expand Up @@ -3719,6 +3730,78 @@ bash_tips

2021-01-22

* edit: C-w で kill-ring への追加を実装するという事 (suggested by 3ximus) [#D1443]
https://github.com/akinomyoga/ble.sh/issues/83#issuecomment-764893198

これについては先ず初めに仕様を確定しなければならない。

例えば前方を切り取るコマンドの場合には必ず左に挿入する事にする? と思ったが、
そうすると C-k を連続で使用した時に、切り取られる行の順序が逆になってしまう。

? というよりどの様なコマンドが存在しているだろうか。

| 調べると C-k, C-u, C-w, M-d, M-h で kill-... を実行している。
|
| widget 名で言うと
| - kill-{forward,backward}-{?word,{graphical-,logical-,}line,text}
| これの対応は簡単。backward に削除する場合には prepend し、
| forward に削除する場合には append すれば良い。
| - kill-{?word,{graphical-,logical-,}line}
| これは微妙。emacs でどの様に振る舞っているだろうかと考えたが、
| よく考えてみたらその様に振る舞う物は存在しない気がする。
|
| emacs.rlfunc.txt で対応表を見てみたが、kill-line に対応する
| rlfunc kill-whole-line しか振る舞いを確認できる物が存在しない。そして、
| この rlfunc kill-whole-line は kill-line と違って、
| コマンドライン全体を切り取ってしまうのでやはり振る舞いとして異なる。
|
| と思ったが、実際に readline で実行してみると kill-whole-line は
| 前回の内容に append する様である。取り敢えず ble.sh でもその様に振る舞う事にする。
|
| - kill-region
| これは前回の内容は完全に忘れるという振る舞いで良い。
|
| - vi_imap/delete-backward-word
| これは実は kill-ring には何も影響を与えない様だ。取り敢えず無視する事にする。
| readline の vi mode の時には kill するが combine はしない様だ。

→結論としては、kill-region,kill-region-or 以外の kill-* は全て対象という事。
kill-backward-* に関しては prepend で、それ以外については append という事。

? ok: append/prepend をした時に元々あった内容を上書きするのか、
新しく項目が追加されるのか。
→試して見た所、上書きする様である。

? done: /.*-range の allow_empty という引数は必要なのだろうか。
kill,copy,delete に関しては決して指定される事はない。
replace に関しては大体指定されている。
指定されていない物は意図的な物かどうか確認する必要がある。

- ./keymap/vi.sh:3995: ble/widget/.replace-range "$eol1" "$eol2" "$text"
- ./keymap/vi.sh:4016: ble/widget/.replace-range "$eol1" "$bol2" "$text"
上記二つに関しては eol1<eol2 が保証されているのでどちらでも良い。
- ./lib/core-complete.sh:5832: ble/widget/.replace-range "$pos" "$comp_index" "$value"
- ./lib/core-complete.sh:5865: ble/widget/.replace-range "$pos" "$comp_index" "$value"
- ./lib/core-complete.sh:5871: ble/widget/.replace-range "$pos" "$comp_index" ''
上記3つに関しても同様に pos<comp_index が保証されている。
- ./lib/vim-surround.sh:509: ble/widget/.replace-range "$beg" "$end" "$content"
operator なので幅0になる事は余りない気がするが、例えば編集文字列が空の時?
→うーん。その様な場合であっても cs 経由でしかこの operator は呼び出されないので、
結局、delimiter を使って範囲を切り出そうとする段階で失敗してしまう。
結局、この operator が空文字列に対して呼び出される事はない気がする。

もし仮に呼び出される事があったとしても、allow_empty を指定する方が自然。

replace-range については常に allow_empty という事にする。
kill,copy,delete については常に not allow_empty という事にする。
→書き換えた。

? done: copy でも同様に振る舞う必要があるだろうか。
→実際に試してみた所、同様に振る舞う様である。。。

動かしていて気づいたが、C-k で行末の改行を削除できていない…。
これは別 commit での bugfix にする事にする。

* edit: support "bleopt edit_line_type" (motivated by 3ximus) [#D1442]
https://github.com/akinomyoga/ble.sh/issues/83

Expand Down

0 comments on commit 66564e1

Please sign in to comment.