Skip to content

Commit

Permalink
edit: support "bleopt edit_magic_{expand,accept}=autocd"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 11, 2024
1 parent 0c42c8b commit b6344b3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 38 deletions.
24 changes: 12 additions & 12 deletions blerc.template
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

## 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.
## "sabbrev", "alias", and "autocd" can be specified.

#bleopt edit_magic_expand=history:sabbrev

Expand All @@ -135,17 +135,17 @@

## This option specifies the expansions performed on accept-line by a
## colon-separated list. The expansion is performed in a similar way as Bash's
## history expansion. When "sabbrev", "alias", and "history" is specified, the
## respective expansions are attempted on the command line. When "verify" is
## specified, if sabbrev or alias expansions changed the command line, the
## execution of the command line is canceled so the user can examine or
## continue to edit the expanded line. The history expansion can be controlled
## by "shopt -s histverify" in a similar manner. When "verify-syntax" is
## specified and any expansions change the command string, a syntax check is
## performed. The command execution is canceled when the command string is not
## syntactically complete. When "history-line" is specified, the history
## expansion replaces the command line instead of just printing the expansion
## result. The default value of this option is empty.
## history expansion. When "sabbrev", "alias", "autocd", and "history" is
## specified, the respective expansions are attempted on the command line.
## When "verify" is specified, if sabbrev, alias, or autocd expansions changed
## the command line, the execution of the command line is canceled so the user
## can examine or continue to edit the expanded line. The history expansion
## can be controlled by "shopt -s histverify" in a similar manner. When
## "verify-syntax" is specified and any expansions change the command string, a
## syntax check is performed. The command execution is canceled when the
## command string is not syntactically complete. When "history-line" is
## specified, the history expansion replaces the command line instead of just
## printing the expansion result. The default value of this option is empty.

#bleopt edit_magic_accept=sabbrev

Expand Down
6 changes: 4 additions & 2 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
- main: support `bash ble.sh --install` `#D2169` 986d26a3 3801a87e
- util(stty): support `bleopt term_stty_restore` (requested by TheFantasticWarrior) `#D2170` e64b02b7
- util: update workaround of Bash 5.2 `checkwinsize` for `term_stty_restore` (reported by TheFantasticWarrior) `#D2184` ef8272a4
- edit: support `bleopt edit_magic_accept` (requested by pl643, bkerin) `#D2175` 3e9d8907
- magic expansions
- edit: support `bleopt edit_magic_accept` (requested by pl643, bkerin) `#D2175` 3e9d8907
- edit: support `bleopt edit_magic_accept=verify-syntax` `#D2178` ac84c153
- edit: support `bleopt edit_magic_{expand,accept}=autocd` (motivated by Jai-JAP) `#D2187` xxxxxxxx
- main: support shell variable `BLE_VER` `#D2177` a12dedab
- edit: support `bleopt edit_magic_accept=verify-syntax` `#D2178` ac84c153
- util(bleopt, blehook, ble-face): support wildcards `*` and `?` and change `@` to match an empty string `#D2182` bf595293

## Changes
Expand Down
3 changes: 2 additions & 1 deletion lib/core-complete-def.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ ble/util/autoload "$_ble_base/lib/core-complete.sh" \
ble-decode/keymap:menu_complete/define \
ble-decode/keymap:dabbrev/define \
ble/complete/sabbrev/expand \
ble/complete/alias/expand
ble/complete/expand:alias \
ble/complete/expand:autocd

bleopt/declare -v complete_source_sabbrev_opts ''
bleopt/declare -v complete_source_sabbrev_ignore ''
Expand Down
22 changes: 21 additions & 1 deletion lib/core-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9391,7 +9391,7 @@ function ble/complete/source:sabbrev {
done
}

function ble/complete/alias/expand {
function ble/complete/expand:alias {
local pos comp_index=$_ble_edit_ind comp_text=$_ble_edit_str
ble/complete/sabbrev/locate-key 'command'
((pos<comp_index)) || return 1
Expand All @@ -9403,6 +9403,26 @@ function ble/complete/alias/expand {
return 0
}

function ble/complete/expand:autocd {
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} ret
ble/syntax:bash/simple-word/safe-eval "$word" nonull || return 1

local dir=$ret
[[ $dir && -d $dir ]] && ! ble/bin#has "$dir" || return 1

if [[ $dir == -* ]]; then
dir="cd -- $word"
else
dir="cd $word"
fi
ble/widget/.replace-range "$pos" "$comp_index" "$dir"
return 0
}

#------------------------------------------------------------------------------
#
# dabbrev
Expand Down
7 changes: 7 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7340,6 +7340,13 @@ bash_tips

2024-03-10

* complete: autocd を magic expansion の一部に組み込む (motivated by Jai-JAP) [#D2187]
https://github.com/akinomyoga/ble.sh/discussions/421

? cdable_vars を設定している時、ディレクトリ名を含んでいる変数名だけでも移
動できたりするか? → 移動できない。なので cdable_vars に関しては気にしな
くて良い。

* 2024-02-13 github: Node.js 20 に伴う action 更新 [#D2186]
https://github.com/akinomyoga/ble.sh/actions/runs/7883231577

Expand Down
62 changes: 40 additions & 22 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7588,17 +7588,20 @@ function ble/widget/default/accept-line {
fi

# エイリアス展開
if [[ :$bleopt_edit_magic_accept: == *:alias:* ]]; then
local old_str=$_ble_edit_str old_ind=$_ble_edit_ind
if ble/complete/alias/expand; then
if [[ :$bleopt_edit_magic_accept: == *:verify:* ]]; then
ble/widget/default/accept-line/.prepare-verify "$_ble_edit_str" "$_ble_edit_ind"
return 0
local expand_type
for expand_type in alias autocd; do
if [[ :$bleopt_edit_magic_accept: == *:"$expand_type":* ]]; then
local old_str=$_ble_edit_str old_ind=$_ble_edit_ind
if ble/complete/expand:"$expand_type"; then
if [[ :$bleopt_edit_magic_accept: == *:verify:* ]]; then
ble/widget/default/accept-line/.prepare-verify "$_ble_edit_str" "$_ble_edit_ind"
return 0
fi
command=$_ble_edit_str
is_line_expanded=1
fi
command=$_ble_edit_str
is_line_expanded=1
fi
fi
done

# 履歴展開
if [[ -o histexpand || :$bleopt_edit_magic_accept: == *:history:* ]]; then
Expand Down Expand Up @@ -8361,36 +8364,51 @@ function ble/widget/history-expand-backward-line {
}
## @widget magic-space
## 履歴展開と静的略語展開を実行してから空白を挿入します。
function ble/widget/magic-space {
# keymap/vi.sh
[[ $_ble_decode_keymap == vi_imap ]] &&
local oind=$_ble_edit_ind ostr=$_ble_edit_str

local arg; ble-edit/content/get-arg ''
function ble/widget/magic-space/.expand {
local type=$bleopt_edit_magic_expand
local opts=$bleopt_edit_magic_opts

local expanded= opt_noinsert=
# (1) history expansion
if [[ :$bleopt_edit_magic_expand: == *:history:* ]]; then
ble/widget/history-expand-backward-line && expanded=1
if [[ :$type: == *:history:* ]]; then
ble/widget/history-expand-backward-line && return 0
fi

# (2) sabbrev expansion
if [[ ! $expanded && :$bleopt_edit_magic_expand: == *:sabbrev:* ]]; then
if [[ :$type: == *:sabbrev:* ]]; then
ble/complete/sabbrev/expand type-status; local ext=$?
if ((ext==0||32<=ext&&ext<=126)); then
expanded=1
((ext==105)) && # 105 = 'i' (inline sabbrev)
[[ :$opts: == *:inline-sabbrev-no-insert:* ]] &&
opt_noinsert=1
return 0
elif ((ext==147)); then
return 147 # メニュー補完に入った時
fi
fi

# (3) alias expansion
if [[ ! $expanded && :$bleopt_edit_magic_expand: == *:alias:* ]]; then
ble/complete/alias/expand && expanded=1
if [[ :$type: == *:alias:* ]]; then
ble/complete/expand:alias && return 0
fi

# (4) autocd
if [[ :$type: == *:autocd:* ]]; then
ble/complete/expand:autocd && return 0
fi

return 1
}
function ble/widget/magic-space {
# keymap/vi.sh
[[ $_ble_decode_keymap == vi_imap ]] &&
local oind=$_ble_edit_ind ostr=$_ble_edit_str

local arg; ble-edit/content/get-arg ''

local opt_noinsert=
ble/widget/magic-space/.expand; local ext=$?
((ext==147)) && return "$ext"

# keymap/vi.sh
if [[ $_ble_decode_keymap == vi_imap && $ostr != "$_ble_edit_str" ]]; then
_ble_edit_ind=$oind _ble_edit_str=$ostr ble/keymap:vi/undo/add more
Expand Down

0 comments on commit b6344b3

Please sign in to comment.