Skip to content

Commit

Permalink
complete (requote): requote from optarg/rhs starting point
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 4, 2022
1 parent 70277d0 commit 93c2786
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
7 changes: 4 additions & 3 deletions docs/ChangeLog.md
Expand Up @@ -84,6 +84,7 @@
- term: let <kbd>DECSCUSR</kbd> pass through terminal multiplexers (motivated by cmplstofB) `#D1697` a3349e4
- util: refactor `_ble_term_TERM` `#D1746` 63fba6b
- complete: requote for more compact representations on full completions `#D1700` a1859b6
- complete (requote): requote from optarg/rhs starting point `#D1786` 0000000
- complete: improve support for `declare` and `[[ ... ]]` `#D1701` da38404
- syntax: fix completion and highlighting of `declare` with assignment arguments `#D1704` `#D1705` e12bae4
- cmdspec: refactor `{mandb => cmdspec}_opts` `#D1706` `#D1707` 0786e92
Expand Down Expand Up @@ -146,7 +147,7 @@
- util (`ble/util/buffer`): hide cursor in rendering `#D1758` e332dc5
- complete (`action:file`): always suffix `/` to complete symlinked directory names (reported by SuperSandro2000) `#D1759` 397ac1f
- edit (command-help): show source files for functions `#D1779` 7683ab9
- edit (`ble/builtin/exit`): defer exit in trap handlers (motivated by SuperSandro2000) `#D1782` f62fc04 0000000
- edit (`ble/builtin/exit`): defer exit in trap handlers (motivated by SuperSandro2000) `#D1782` f62fc04 6fdabf3

## Fixes

Expand Down Expand Up @@ -241,7 +242,7 @@
- progcomp: retry completions on `$? == 124` also for non-default completions (reported by SuperSandro2000) `#D1759` 82b9c01
- app: work around data corruption by WINCH on intermediate state `#D1762` 5065fda
- util (`ble/util/import`): work around filenames with bash special characters `#D1763` b27f758
- edit: fix the restore failure of `PS1` and `PROMPT_COMMAND` on `ble-detach` `#D1784` 0000000
- edit: fix the restore failure of `PS1` and `PROMPT_COMMAND` on `ble-detach` `#D1784` b9fdaab

## Documentation

Expand Down Expand Up @@ -326,7 +327,7 @@
- edit (TRAPDEBUG): preserve original `DEBUG` trap and enabled it in `PROMPT_COMMAND` (motivated by ammarooo) `#D1772` `#D1773` ec2a67a
- main, trap: fix initialization order of `{save,restore}-BASH_REMATCH` (reported by SuperSandro2000) `#D1780` 689534d
- global: work around bash-3.0 bug that single quotas remains for `"${v-$''}"` `#D1774` 9b96578
- util: work around old `vte` not supporting `DECSCUSR` yet setting `TERM=xterm` (reported by dongxi8) `#D1785` 0000000
- util: work around old `vte` not supporting `DECSCUSR` yet setting `TERM=xterm` (reported by dongxi8) `#D1785` 70277d0

## Internal changes and fixes

Expand Down
41 changes: 32 additions & 9 deletions lib/core-complete.sh
Expand Up @@ -1523,14 +1523,35 @@ function ble/complete/action/quote-insert.batch {
}

function ble/complete/action/requote-final-insert {
local comps_prefix= check_optarg=
if [[ $insert == "$COMPS"* ]]; then
[[ $comps_flags == *[SEDI]* ]] && return 0
local comps_prefix=$COMPS

# Note: 以下の設定は遡って書き換える事を許す事になる
[[ $COMPS != *[!':/={,'] ]] && comps_prefix=$COMPS
check_optarg=$COMPS
else
# 遡って書き換える場合 (中途半端な quote 状態ではないと仮定)
local comps_prefix=
[[ $comps_fixed ]] &&
comps_prefix=${COMPS::${comps_fixed%%:*}}
check_optarg=$insert
fi

# Note: --prefix='/usr/local', PREFIX='/usr/local', -L'/usr/local/share/lib'
# 等、オプション・変数代入の右辺などの quote は、その開始点と思われる箇所から
# 始める。
if [[ $check_optarg ]]; then
if ble/string#match "$check_optarg" '^([_a-zA-Z][_a-zA-Z0-9]*|-[-a-zA-Z0-9.]+)=(([^\'\''"`${}]*|\\.)*:)?'; then
# --prefix= や PREFIX=, PATH=xxxx: 等があった場合には = や : の直後から quote する。
comps_prefix=$BASH_REMATCH
elif [[ $COMP_PREFIX == -[!'-=:/\'\''"$`{};&|<>!^{}'] && $check_optarg == "$COMP_PREFIX"* ]]; then
# -L'/path/to/library' 等。COMP_PREFIX=-L かつ COMPS が -L で始まっている時のみ。
comps_prefix=${check_optarg::2}
fi
fi

if [[ $comps_fixed ]]; then
local comps_fixed_part=${COMPS::${comps_fixed%%:*}}
[[ $comps_prefix == "$comps_fixed_part"* ]] ||
comps_prefix=$comps_fixed_part
fi

if [[ $insert == "$comps_prefix"* && $comps_prefix != *[!':/={,'] ]]; then
Expand All @@ -1541,7 +1562,9 @@ function ble/complete/action/requote-final-insert {
((${#ret[@]}==1))
then
ble/string#quote-word "$ret" quote-empty
((${#ret}<=${#ins})) && insert=$comps_prefix$ret
((${#ret}<=${#ins})) || return 0
insert=$comps_prefix$ret
[[ $insert == "$COMPS"* ]] || insert_flags=r$insert_flags # 遡って書き換えた
fi
fi
return 0
Expand Down Expand Up @@ -6257,10 +6280,10 @@ function ble/complete/insert-common {
## @var[out] cand_count cand_cand cand_word cand_pack
function ble/complete/insert-all {
local "${_ble_complete_cand_varnames[@]/%/=}" # WA #D1570 checked
local pack beg=$COMP1 end=$COMP2 insert= suffix= index=0
local pack beg=$COMP1 end=$COMP2 insert= suffix= insert_flags= index=0
for pack in "${cand_pack[@]}"; do
ble/complete/cand/unpack "$pack"
insert=$INSERT suffix=
insert=$INSERT suffix= insert_flags=

if ble/is-function ble/complete/action:"$ACTION"/complete; then
ble/complete/action:"$ACTION"/complete
Expand Down Expand Up @@ -7440,9 +7463,9 @@ function ble/complete/auto-complete/.check-context {
fi

local type=
if [[ $word == "$COMPS"* ]]; then
if [[ $insert == "$COMPS"* ]]; then
# 入力候補が既に続きに入力されている時は提示しない
[[ ${comp_text:COMP1} == "$word"* ]] && return 1
[[ ${comp_text:COMP1} == "$insert"* ]] && return 1

type=c
ble/complete/auto-complete/.insert "${insert:${#COMPS}}"
Expand Down
7 changes: 7 additions & 0 deletions note.txt
Expand Up @@ -6178,6 +6178,13 @@ bash_tips

2022-03-01

* complete (requote): --prefix='a b c d e' の様に prefix 部分は requote から除外 [#D1786]
Ref #D1787

全体を quote し直す時にオプション的な部分・もしくは変数代入的な部分は除外す
る様にする。例えば --option=hello\ 1\ 2\ 3 が '--option=hello 1 2 3' になる
のではなく、--option='hello 1 2 3' になって欲しいという事。

* edit: 古い terminator で表示が乱れる (reported by dongxi8) [#D1785]
https://github.com/ohmybash/oh-my-bash/issues/314

Expand Down

0 comments on commit 93c2786

Please sign in to comment.