Skip to content

Commit 2ef5e48

Browse files
committed
bash-completion (_comp_command_offset): perform fallback completion based on the given context
1 parent 8060b7a commit 2ef5e48

5 files changed

Lines changed: 47 additions & 6 deletions

File tree

contrib

docs/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
- integration/zoxide: fix the problem of unquoted filenames (reported by tessus) `#D2216` 430a1746
291291
- integration/{bash,fzf,skim}-completion: adjust dynamically loaded completion functions (motivated by tessus) `#D2327` 788dfd15
292292
- integration/fzf: suppress dynamic binding `#D2350` e9d5ca26
293+
- integration/bash-completion (`_comp_command_offset`): perform fallback completion based on the given context (motivated by Jai-JAP) `#D2377` xxxxxxxx
293294

294295
## Documentation
295296

lib/core-complete.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,6 +5980,18 @@ function ble/complete/source:option/.is-option-context {
59805980
return 0
59815981
}
59825982

5983+
## @fn ble/complete/source:option [opts]
5984+
## @param[in,opt] opts
5985+
## @opt empty
5986+
## Generate option names even when the current word is empty. By
5987+
## default, the generation of the options is enabled only when the
5988+
## current word starts with - or +.
5989+
## @opt reuse-comp_words
5990+
## When this option is specified, use the externally-specified variables
5991+
## "comp_words", "comp_line", "comp_point", and "comp_cword" instead of
5992+
## extracting them by the syntax analysis on the current point. This is
5993+
## used when the target command is intentionally different from the shell
5994+
## syntax, such as the case of "sudo command ...".
59835995
function ble/complete/source:option {
59845996
local opts=$1
59855997
if [[ :$opts: == *:empty:* ]]; then
@@ -5995,8 +6007,10 @@ function ble/complete/source:option {
59956007
ble/complete/source/reduce-compv-for-ambiguous-match
59966008
[[ :$comp_type: == *:[maA]:* ]] && local COMP2=$COMP1
59976009

5998-
local comp_words comp_line comp_point comp_cword
5999-
ble/syntax:bash/extract-command "$COMP2" || return 1
6010+
if [[ :$opts: != *:reuse-comp_words:* ]]; then
6011+
local comp_words comp_line comp_point comp_cword
6012+
ble/syntax:bash/extract-command "$COMP2" || return 1
6013+
fi
60006014

60016015
ble/complete/source:option/generate-for-command "${comp_words[@]::comp_cword}"
60026016
}
@@ -6181,6 +6195,19 @@ function ble/complete/source:argument/generate {
61816195
ble/complete/source:argument/.generate-user-defined-completion; local ext=$?
61826196
((ext==148||cand_count>old_cand_count)) && return "$ext"
61836197

6198+
ble/complete/source:argument/fallback
6199+
}
6200+
6201+
## @fn ble/complete/source:argument/fallback
6202+
## @param[in] opts
6203+
## @opt reuse-comp_words
6204+
## @var[in] comp_opts
6205+
## @opt ble/default
6206+
## @opt dirnames
6207+
## @opt default
6208+
function ble/complete/source:argument/fallback {
6209+
local opts=$1 old_cand_count=$cand_count
6210+
61846211
# When no completions are generated, we attempt "ble/default" argument
61856212
# completions in the following. If "ble/default" completions are disabled,
61866213
# we emulate Bash's behavior based on "-o default" and "-o dirnames".
@@ -6207,7 +6234,10 @@ function ble/complete/source:argument/generate {
62076234
# 2. Attempt built-in argument completion
62086235

62096236
# "-option" の時は complete options based on mandb
6210-
ble/complete/source:option; local ext=$?
6237+
local option_opts=
6238+
[[ :$opts: == *:reuse-comp_words:* ]] &&
6239+
option_opts=$option_opts:reuse-comp_words
6240+
ble/complete/source:option "$option_opts"; local ext=$?
62116241
((ext==148)) && return "$ext"
62126242

62136243
# When "-o dirnames" is specified, the directory names are first attempted,
@@ -6225,7 +6255,7 @@ function ble/complete/source:argument/generate {
62256255
fi
62266256

62276257
# 空文字列に対するオプション生成はファイル名よりも後で試みる
6228-
ble/complete/source:option empty; local ext=$?
6258+
ble/complete/source:option "$option_opts:empty"; local ext=$?
62296259
((ext==148||cand_count>old_cand_count)) && return "$ext"
62306260

62316261
#----------------------------------------------------------------------------

lib/core-debug.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ function ble/debug/xtrace.advice {
875875
set -x
876876
ble/function#advice/do 2>> "$filename"
877877
[[ $set == *x* ]] || set +x
878-
return
878+
return 0
879879
fi
880880

881881
local old_xtracefd=

note.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7950,6 +7950,16 @@ bash_tips
79507950
Done (実装ログ)
79517951
-------------------------------------------------------------------------------
79527952

7953+
2025-07-23
7954+
7955+
* integration/bash-completion: _comp_command_offset に対して fallback completion を適用する (motivated by Jai-JAP) [#D2377]
7956+
https://github.com/akinomyoga/ble.sh/discussions/551#discussioncomment-13847256
7957+
7958+
sudo に対して直接特別の実装することはないと言ったが、よく考えたら
7959+
bash-completion の _comp_command_offset に対して介入すれば、コマンド毎に対
7960+
応しなくても、sudo 等をつけずに直接補完を試みた時と同程度の振る舞いにはでき
7961+
る。
7962+
79537963
2025-07-22
79547964

79557965
* stty: bash out/ble.sh --install で stty を呼び出している (reported by LEI) [#D2376]

0 commit comments

Comments
 (0)