Skip to content

Commit

Permalink
edit (magic-space): support "bleopt edit_magic_expand=...:alias"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 8, 2022
1 parent 6a21ebb commit 63da2ac
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 20 deletions.
7 changes: 7 additions & 0 deletions blerc.template
Expand Up @@ -112,6 +112,13 @@
#bleopt edit_line_type=graphical


## The following option specifies the set of expansions performed by
## magic-space with a colon-separated list of expansion types. "history",
## "sabbrev", and "alias" can be specified.

#bleopt edit_magic_expand=history:sabbrev


## The following option controls the position of the info pane where completion
## menu, mode names, and other information are shown. When the value "top" is
## specified, the info pane is shown just below the command line. When the
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -113,6 +113,7 @@
- color: allow setting color filter by `_ble_color_color2sgr_filter` `#D1902` 88e74cc
- auto-complete: add `bleopt complete_auto_complete_opts` (motivated by DUOLabs333) `#D1901` `#D1911` 1478a04 xxxxxxx
- menu-complete: add `bleopt complete_menu_complete_opts` (requested by DUOLabs333) `#D1911` xxxxxxx
- edit (`magic-space`): support `bleopt edit_magic_expand=...:alias` (requested by telometto) `#D1912` xxxxxxx

## Changes

Expand Down
3 changes: 2 additions & 1 deletion lib/core-complete-def.sh
Expand Up @@ -108,7 +108,8 @@ ble/util/autoload "$_ble_base/lib/core-complete.sh" \
ble-decode/keymap:auto_complete/define \
ble-decode/keymap:menu_complete/define \
ble-decode/keymap:dabbrev/define \
ble/complete/sabbrev/expand
ble/complete/sabbrev/expand \
ble/complete/alias/expand

#------------------------------------------------------------------------------
# 描画設定
Expand Down
33 changes: 26 additions & 7 deletions lib/core-complete.sh
Expand Up @@ -8188,18 +8188,25 @@ function ble-sabbrev {
return "$ext"
}

function ble/complete/sabbrev/expand {
local sources comp_index=$_ble_edit_ind comp_text=$_ble_edit_str
## @fn ble/complete/sabbrev/locate-key rex_source_type
## @var[out] pos
## @var[in] comp_index comp_text
function ble/complete/sabbrev/locate-key {
pos=$comp_index
local rex_source_type='^('$1')$'
local sources src asrc
ble/complete/context:syntax/generate-sources
local src asrc pos=$comp_index
for src in "${sources[@]}"; do
ble/string#split-words asrc "$src"
case ${asrc[0]} in
(file|command|argument|variable:w|wordlist:*|sabbrev)
((asrc[1]<pos)) && pos=${asrc[1]} ;;
esac
[[ ${asrc[0]} =~ $rex_source_type ]] &&
((asrc[1]<pos)) &&
pos=${asrc[1]}
done
}

function ble/complete/sabbrev/expand {
local pos comp_index=$_ble_edit_ind comp_text=$_ble_edit_str
ble/complete/sabbrev/locate-key 'file|command|argument|variable:w|wordlist:.*|sabbrev'
((pos<comp_index)) || return 1

local key=${_ble_edit_str:pos:comp_index-pos}
Expand Down Expand Up @@ -8307,6 +8314,18 @@ function ble/complete/source:sabbrev {
done
}

function ble/complete/alias/expand {
local pos comp_index=$_ble_edit_ind comp_text=$_ble_edit_str
ble/complete/sabbrev/locate-key 'command'
((pos<comp_index)) || return 1

local word=${_ble_edit_str:pos:comp_index-pos}
local ret; ble/alias#expand "$word"
[[ $ret != "$word" ]] || return 1
ble/widget/.replace-range "$pos" "$comp_index" "$ret"
return 0
}

#------------------------------------------------------------------------------
#
# dabbrev
Expand Down
53 changes: 45 additions & 8 deletions note.txt
Expand Up @@ -3082,8 +3082,25 @@ bash_tips

2021-09-20

* sabbrev を accept-line の時にも展開する可能性について。
zsh-abbr の振る舞いについても確認する。
* sabbrev: accept-line の時にも展開する可能性について (requested by pl643)
https://github.com/akinomyoga/ble.sh/discussions/138

* zsh-abbr の振る舞いについても確認する。

* 振る舞いについての考察

a グローバルに全ての合致する単語を置換する。

x 先ず、意図しない物まで展開されてしまう可能性がある。

x 展開を意図する単語があったとしても、通常は magic-space によって既に展
開されていると期待するべき。なのでわざわざグローバルな展開を試みるま
でもない。

b 或いはその場所にある単語だけを展開する magic-accept 的な物。

元の要求が結局どのような物だったかは返事がないので不明だが、考えて見るに
b の方が良い。

2021-09-15

Expand Down Expand Up @@ -3913,12 +3930,6 @@ bash_tips

2021-02-28

* magic-space で alias 展開する機能があっても良いのでは?

というか magic-space で何を展開するかの集合を設定できる様にするべき?
bleopt edit_magic_expand=sabbrev:history:alias という具合に。
流石にコマンド置換等まで展開するのはやりすぎの様に思われる。

* canvas/trace: wordwrap に対応する。つまり、単語の途中で改行しない様にする。

これは今の所具体的な用途もないので取り敢えずこの儘にしておく。
Expand Down Expand Up @@ -6671,6 +6682,32 @@ bash_tips

2022-12-08

* 2021-02-28 magic-space で alias 展開する機能があっても良いのでは? (requested by telometto) [#D1912]

というか magic-space で何を展開するかの集合を設定できる様にするべき?
bleopt edit_magic_expand=sabbrev:history:alias という具合に。
流石にコマンド置換等まで展開するのはやりすぎの様に思われる。

2022-12-08 https://github.com/akinomyoga/ble.sh/discussions/260

まず最初に bleopt を実装する事にする。実装した。

alias expansion についても sabbrev expansion を元にして実装した。

? ok: ところで他の discussion でも同様の物があった様な気がする。

https://github.com/akinomyoga/ble.sh/discussions/138

ここにある。と思ったが、これは alias expansion ではなくて、magic-space を
コマンド実行時にも呼び出すという話なのだった。というかこの項目が todo に
なかったので新しく項目を作って置く事にした。と思ったら項目自体はあって単
に GitHub#138 へのリンクが記録されていなかっただけだった。

x auto-complete が有効になっていると ' ' が magic-space として働かない。
→これは別に項目が存在していた筈なので別に処理する事にする。

* done: blerc/wiki: bleopt edit_magic_expand の説明。

* 2022-12-03 menu-complete: 一時挿入をしない設定 (requested by DUOLabs333) [#D1911]
https://github.com/akinomyoga/ble.sh/discussions/252#discussioncomment-4293518

Expand Down
25 changes: 21 additions & 4 deletions src/edit.sh
Expand Up @@ -94,6 +94,8 @@ bleopt/declare -v undo_point end
##
bleopt/declare -n edit_forced_textmap 1

bleopt/declare -n edit_magic_expand history:sabbrev

function ble/edit/use-textmap {
ble/textmap#is-up-to-date && return 0
((bleopt_edit_forced_textmap)) || return 1
Expand Down Expand Up @@ -7656,10 +7658,25 @@ function ble/widget/magic-space {
local oind=$_ble_edit_ind ostr=$_ble_edit_str

local arg; ble-edit/content/get-arg ''
ble/widget/history-expand-backward-line ||
ble/complete/sabbrev/expand
local ext=$?
((ext==147)) && return 147 # sabbrev/expand でメニュー補完に入った時など。

local expanded=
# (1) history expansion
if [[ :$bleopt_edit_magic_expand: == *:history:* ]]; then
ble/widget/history-expand-backward-line && expanded=1
fi
# (2) sabbrev expansion
if [[ ! $expanded && :$bleopt_edit_magic_expand: == *:sabbrev:* ]]; then
ble/complete/sabbrev/expand; local ext=$?
if ((ext==147)); then
return 147 # sabbrev/expand の中でメニュー補完に入った時など。
elif ((ext==0)); then
expanded=1
fi
fi
# (3) alias expansion
if [[ ! $expanded && :$bleopt_edit_magic_expand: == *:alias:* ]]; then
ble/complete/alias/expand && expanded=1
fi

# keymap/vi.sh
[[ $_ble_decode_keymap == vi_imap ]] &&
Expand Down

0 comments on commit 63da2ac

Please sign in to comment.