Skip to content

Commit

Permalink
sabbrev: add widget "magic-slash" to approximate Zsh named directories
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 9, 2023
1 parent 41faa49 commit e6b9581
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 11 deletions.
13 changes: 10 additions & 3 deletions blerc.template
Expand Up @@ -874,13 +874,20 @@
## Keybindings


## The default mapping of <SP> is magic-space which performs history and
## sabbrev expansion before inserting a space. If you want to insert just a
## space without expansions as Bash's default, use the following setting:
## The default mapping of <SP> in ble.sh is magic-space which performs history
## and sabbrev expansion before inserting a space. If you want to insert just
## a space without expansions as Bash's default, use the following setting:

#ble-bind -f 'SP' 'self-insert'


## The default mapping of `/' (<slash>) in ble.sh is magic-slash which performs
## sabbrev expansions of the name ` ~*'. If you want to insert just a slash
## without expansions as Bash's default, use the following setting:

#ble-bind -f '/' 'self-insert'


## If you want to search the already input string using <up> and <down> keys,
## use the following setting:

Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -136,6 +136,7 @@
- syntax: support new parameter transformation `"${arr@k}"` `#D1998` 1dd7e385
- edit: support a user command `ble append-line` (requested by mozirilla213) `#D2001` 2a524f34
- decode: accept isolated <kbd>ESC \<char\></kbd> (requested by mozirilla213) `#D2004` xxxxxxxx
- sabbrev: add widget `magic-slash` to approximate Zsh named directories (motivated by mozirilla213) `#D2008` xxxxxxxx

## Changes

Expand Down
3 changes: 2 additions & 1 deletion keymap/emacs.sh
Expand Up @@ -26,7 +26,7 @@ _ble_keymap_emacs_white_list=(
self-insert
batch-insert
nop
magic-space
magic-space magic-slash
copy{,-forward,-backward}-{c,f,s,u}word
copy-region{,-or}
clear-screen
Expand Down Expand Up @@ -207,6 +207,7 @@ function ble-decode/keymap:emacs/define {
# history
ble-bind -f 'M-^' history-expand-line
ble-bind -f 'SP' magic-space
ble-bind -f '/' magic-slash

#----------------------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion keymap/vi.sh
Expand Up @@ -221,7 +221,7 @@ _ble_keymap_vi_imap_white_list=(
self-insert
batch-insert
nop
magic-space
magic-space magic-slash
delete-backward-{c,f,s,u}word
copy{,-forward,-backward}-{c,f,s,u}word
copy-region{,-or}
Expand Down Expand Up @@ -7890,6 +7890,7 @@ function ble-decode/keymap:vi_imap/define {
ble-bind -f 'C-d' 'delete-region-or delete-forward-char-or-exit'

ble-bind -f 'SP' 'magic-space'
ble-bind -f '/' 'magic-slash'
# ble-bind -f 'M-^' 'history-expand-line'

# ble-bind -f 'C-c' 'discard-line'
Expand Down Expand Up @@ -8187,6 +8188,7 @@ function ble-decode/keymap:vi_cmap/define {
# command-history
# ble-bind -f 'M-^' history-expand-line
# ble-bind -f 'SP' magic-space
# ble-bind -f '/' magic-slash

ble-bind -f 'C-\' bell
ble-bind -f 'C-^' bell
Expand Down
36 changes: 30 additions & 6 deletions lib/core-complete.sh
Expand Up @@ -7922,7 +7922,7 @@ function ble/complete/auto-complete.idle {
[[ $_ble_decode_keymap == emacs || $_ble_decode_keymap == vi_[ic]map ]] || return 0

case $_ble_decode_widget_last in
(ble/widget/self-insert|ble/widget/magic-space) ;;
(ble/widget/self-insert|ble/widget/magic-space|ble/widget/magic-slash) ;;
(ble/widget/complete|ble/widget/vi_imap/complete)
[[ :$bleopt_complete_auto_complete_opts: == *:suppress-after-complete:* ]] && return 0 ;;
(*) return 0 ;;
Expand All @@ -7943,7 +7943,7 @@ function ble/complete/auto-menu.idle {
((bleopt_complete_auto_menu>0)) || return 1

case $_ble_decode_widget_last in
(ble/widget/self-insert) ;;
(ble/widget/self-insert|ble/widget/magic-slash) ;;
(ble/widget/complete) ;;
(ble/widget/vi_imap/complete) ;;
(ble/widget/auto_complete/self-insert) ;;
Expand Down Expand Up @@ -8019,7 +8019,8 @@ function ble/widget/auto_complete/self-insert/.is-magic-space {

local dicthead=_ble_decode_${_ble_decode_keymap_stack[ikeymap]}_kmap_
builtin eval "local ent=\${$dicthead$_ble_decode_key__seq[KEYS[0]]-}"
[[ ${ent#*:} == ble/widget/magic-space ]]
local command=${ent#*:}
[[ $command == ble/widget/magic-space || $command == ble/widget/magic-slash ]]
}

function ble/widget/auto_complete/self-insert {
Expand Down Expand Up @@ -8396,16 +8397,38 @@ function ble/complete/sabbrev/locate-key {
((pos<comp_index))
}

## @fn ble/complete/sabbrev/expand [opts]
## @param[in,opt] opts
## コロン区切りのオプションです。
##
## pattern=PATTERN
## これが一つ以上指定されていた時は何れかの PATTERN で指定された名前を持
## つ sabbrev だけ有効にします。
##
## strip-slash
## 展開後の末尾に含まれる / を削除します。
##
function ble/complete/sabbrev/expand {
local pos comp_index=$_ble_edit_ind comp_text=$_ble_edit_str
local pos comp_index=$_ble_edit_ind comp_text=$_ble_edit_str opts=$1
ble/complete/sabbrev/locate-key 'file|command|argument|variable:w|wordlist:.*|sabbrev|rhs' || return 1

local key=${_ble_edit_str:pos:comp_index-pos}
local ret; ble/complete/sabbrev/get "$key" || return 1
local ret; ble/complete/sabbrev/get "$key" || return 1; local ent=$ret

if ble/opts#extract-all-optargs "$opts" pattern; then
local pattern found=
for pattern in "${ret[@]}"; do
[[ $key == $pattern ]] || continue
found=1
break
done
[[ $found ]] || return 1
fi

local type=${ret%%:*} value=${ret#*:}
local type=${ent%%:*} value=${ent#*:}
case $type in
(s)
[[ :$opts: == *:strip-slash:* ]] && value=${value%/}
ble/widget/.replace-range "$pos" "$comp_index" "$value"
((_ble_edit_ind=pos+${#value})) ;;
(m)
Expand Down Expand Up @@ -8442,6 +8465,7 @@ function ble/complete/sabbrev/expand {
return 1
elif ((cand_count==1)); then
local value=${cand_word[0]}
[[ :$opts: == *:strip-slash:* ]] && value=${value%/}
ble/widget/.replace-range "$pos" "$comp_index" "$value"
((_ble_edit_ind=pos+${#value}))
return 0
Expand Down
50 changes: 50 additions & 0 deletions note.txt
Expand Up @@ -6630,6 +6630,56 @@ bash_tips

2023-03-08

* edit: ~name による "named directory" 展開機能? [#D2008]
https://github.com/akinomyoga/ble.sh/discussions/299

sabbrev の新しい展開の種類として実装する可能性? チルダ展開と同じ文脈で活性
化して、それが直前の単語に含まれていた場合に展開する。コマンド実行前に展開
する機能は混乱の元なので実行したくない気もするが…。

* 然し ~xxx の状態で展開されていないと補完が効かない気がする。補完する時に
内部で ~xxx も考慮に入れて候補を生成するのは大変だ。なので補完を試みる前
に展開するべきの気がする。/ を入力する瞬間か或いは補完を実行する直前か。
うーん。というか magic-insert という widget を作る方が良い気がして来た。
それで / も magic-insert にしてしまう。

* 同時にコマンド実行の瞬間に全体をスキャンして展開する仕組みを作っても良い
気がする。

% 取り敢えず magic-insert は実装する。実装した。
%
% さて、ここでの疑問は既定で / magic-insert を設定しておくべきかどうかという
% 事。というか / で履歴展開が実行されると困るのでは。なので、一部の展開だけ行
% うべきの気がする。というか alias 展開も実行したくない。
%
% やっぱり現在の振る舞いは space だからこそ動くのである。magic-insert はやめ
% る事にする。

色々考えると / に対しては sabbrev だけ行う関数を定義するべきの気がする。
magic-slash を実装してみた。

x done: 然し、tilde 展開を装っている場合に普通と違うのは変数代入形式の引数
の右辺で : 区切りの位置の最初から sabbrev expansion を試みる必要があると
いう事。

と思ったがこれは通常の sabbrev expansion でも対応するべきの気がする。うー
ん。対応した。

ここでの疑問はこの magic-slash を既定の binding として登録するべきかどうか
という事である。或いは ~ で始まる物だけを magic-slash で変換の対象とする?
或いは / に感応する sabbrev の category を新しく作る? うーん。~ で始まる物
だけを特別扱いするのが良い気がする。

* done: wiki

* done: slash-strip: magic-slash で挿入を行う場合は元から sabbrev の定義に
含まれる末尾の / は削除する。

* done: slash-strip: -m の方も対応する必要がある? と思ったが実際にやってみ
ると menu-complete が始まった後に / が挿入されて変な事になる。これはちゃ
んと magic-space でやっているのと同様に処理する必要がある。一意確定の時に
は slash-strip を自前で処理する。

* syntax: echo alpha[0]+=~ がチルダ展開として認識されていない [#D2007]
これは単純なミスだった。修正した。

Expand Down
9 changes: 9 additions & 0 deletions src/edit.sh
Expand Up @@ -7850,6 +7850,13 @@ function ble/widget/magic-space {
_ble_edit_arg=$arg
ble/widget/self-insert
}
function ble/widget/magic-slash {
ble/complete/sabbrev/expand pattern='~*':strip-slash
(($?==147)) && return 147 # sabbrev/expand の中でメニュー補完に入った時など。

local -a KEYS=(47) # /
ble/widget/self-insert
}

#
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -9322,6 +9329,7 @@ function ble-decode/keymap:safe/define {
ble-bind -f 'C-d' 'delete-region-or delete-forward-char-or-exit'

ble-bind -f 'SP' magic-space
ble-bind -f '/' magic-slash
ble-bind -f 'M-^' history-expand-line

ble-bind -f __attach__ safe/__attach__
Expand Down Expand Up @@ -9444,6 +9452,7 @@ function ble-decode/keymap:read/define {
# command-history
# ble-bind -f 'M-^' history-expand-line
# ble-bind -f 'SP' magic-space
# ble-bind -f '/' magic-slash

# ble-bind -f 'C-[' bell # unbound for "bleopt decode_isolated_esc=auto"
ble-bind -f 'C-^' bell
Expand Down

0 comments on commit e6b9581

Please sign in to comment.