diff --git a/fzf-tab.zsh b/fzf-tab.zsh index b81716d..ae2b603 100644 --- a/fzf-tab.zsh +++ b/fzf-tab.zsh @@ -111,7 +111,7 @@ emulate -L zsh -o extended_glob - local _ftb_query _ftb_complist=() _ftb_headers=() command opts + local _ftb_query _ftb_complist=() _ftb_headers=() _ftb_query_prefix command opts -ftb-generate-complist # sets `_ftb_complist` -ftb-zstyle -s continuous-trigger continuous_trigger || { @@ -127,37 +127,57 @@ fi ;; *) - -ftb-generate-query # sets `_ftb_query` - -ftb-generate-header # sets `_ftb_headers` - -ftb-zstyle -s print-query print_query || print_query=alt-enter - -ftb-zstyle -s accept-line accept_line + -ftb-zstyle -s complete-common-prefix complete_common_prefix || complete_common_prefix=true - choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}") - ret=$? - # choices=(query_string expect_key returned_word) + -ftb-generate-query # sets `_ftb_query` and `_ftb_query_prefix` - # insert query string directly - if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then + local curr_prefix="${PREFIX##*/}" + if [[ "$complete_common_prefix" == true && $_ftb_query_prefix == 1 && "$curr_prefix" != "$_ftb_query"* ]]; then local -A v=("${(@0)${_ftb_compcap[1]}}") local -a args=("${(@ps:\1:)v[args]}") [[ -z $args[1] ]] && args=() # don't pass an empty string IPREFIX=$v[IPREFIX] PREFIX=$v[PREFIX] SUFFIX=$v[SUFFIX] ISUFFIX=$v[ISUFFIX] - # NOTE: should I use `-U` here?, ../f\tabcd -> ../abcd - builtin compadd "${args[@]:--Q}" -Q -- $choices[1] + builtin compadd "${${(@)args:#-f}[@]}" -- $_ftb_query compstate[list]= compstate[insert]= - if (( $#choices[1] > 0 )); then - compstate[insert]='2' - [[ $RBUFFER == ' '* ]] || compstate[insert]+=' ' + if (( $#_ftb_query > 0 )); then + compstate[insert]='2' + [[ $RBUFFER == ' '* ]] && compstate[insert]+=' ' fi - return $ret - fi - choices[1]=() + return 0 + else + -ftb-generate-header # sets `_ftb_headers` + -ftb-zstyle -s print-query print_query || print_query=alt-enter + -ftb-zstyle -s accept-line accept_line + + choices=("${(@f)"$(builtin print -rl -- $_ftb_headers $_ftb_complist | -ftb-fzf)"}") + ret=$? + # choices=(query_string expect_key returned_word) + + # insert query string directly + if [[ $choices[2] == $print_query ]] || [[ -n $choices[1] && $#choices == 1 ]] ; then + local -A v=("${(@0)${_ftb_compcap[1]}}") + local -a args=("${(@ps:\1:)v[args]}") + [[ -z $args[1] ]] && args=() # don't pass an empty string + IPREFIX=$v[IPREFIX] PREFIX=$v[PREFIX] SUFFIX=$v[SUFFIX] ISUFFIX=$v[ISUFFIX] + # NOTE: should I use `-U` here?, ../f\tabcd -> ../abcd + builtin compadd "${args[@]:--Q}" -Q -- $choices[1] + + compstate[list]= + compstate[insert]= + if (( $#choices[1] > 0 )); then + compstate[insert]='2' + [[ $RBUFFER == ' '* ]] || compstate[insert]+=' ' + fi + return $ret + fi + choices[1]=() - choices=("${(@)${(@)choices%$nul*}#*$nul}") + choices=("${(@)${(@)choices%$nul*}#*$nul}") - unset CTXT + unset CTXT + fi ;; esac diff --git a/lib/-ftb-generate-query b/lib/-ftb-generate-query index 4f54604..0675dac 100644 --- a/lib/-ftb-generate-query +++ b/lib/-ftb-generate-query @@ -6,6 +6,7 @@ fi local key qtype tmp query_string typeset -g _ftb_query= +typeset -g _ftb_query_prefix=0 -ftb-zstyle -a query-string query_string || query_string=(prefix input first) for qtype in $query_string; do if [[ $qtype == prefix ]]; then @@ -23,6 +24,7 @@ for qtype in $query_string; do tmp[$#MATCH/2+1,-1]="" prefix[$#MATCH/2+1,-1]=() done + _ftb_query_prefix=1 elif [[ $qtype == input ]]; then local fv=${_ftb_compcap[1]#*$'\2'} local -A v=("${(@0)fv}") @@ -31,6 +33,7 @@ for qtype in $query_string; do tmp=${tmp/%$v[SUFFIX]} fi tmp=${${tmp#$v[hpre]}#$v[apre]} + _ftb_query_prefix=0 fi if (( $query_string[(I)longest] )); then (( $#tmp > $#_ftb_query )) && _ftb_query=$tmp diff --git a/test/fzftab.ztst b/test/fzftab.ztst index 98028f3..d7d5657 100644 --- a/test/fzftab.ztst +++ b/test/fzftab.ztst @@ -60,14 +60,20 @@ >C1:{file1} >C1:{file2} + comptesteval "zstyle ':fzf-tab:*' complete-common-prefix false" comptest $': d\t' -0:prefix +0:prefix (complete-common disabled) >line: {: dir1/}{} >QUERY:{dir} >DESCRIPTION:{file} >C1:{dir1/} >C1:{dir2/} + comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true" + comptest $': d\t' +0:prefix (complete-common enabled) +>line: {: dir}{} + comptesteval '_tst () { compadd d c b a }' comptest $'tst \t' 0:normal @@ -125,14 +131,34 @@ 0:complete in word(with known bug) >line: {: ./abc\ def}{ def} + comptesteval "zstyle ':fzf-tab:*' complete-common-prefix false" comptesteval 'mkdir -p abc/def/hij abc/dfe/hij' comptest $': ./a/d/h\t' comptesteval 'rm -rd abc' -0:nested directory +0:nested directory (common prefix disabled) >line: {: ./abc/def/h}{} >QUERY:{d} >DESCRIPTION:{file} >C1:{def/} +>C1:{dfe/} + + comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true" + comptesteval 'mkdir -p abc/def/hij abc/dfe/hij' + comptest $': ./a/d/h\t' + comptesteval 'rm -rd abc' +0:nested directory, 1 tab (common prefix enabled) +>line: {: ./abc/d}{/h} + + comptesteval "zstyle ':fzf-tab:*' complete-common-prefix true" + comptesteval 'mkdir -p abc/def/hij abc/dfe/hij' + comptest $': ./a/d/h\t\t' + comptesteval 'rm -rd abc' +0:nested directory, 2 tabs (common prefix enabled) +>line: {: ./abc/d}{/h} +>line: {: ./abc/d}{/h} +>QUERY:{d} +>DESCRIPTION:{file} +>C1:{def/} >C1:{dfe/} comptesteval '_tst() { a=(a); _describe "group1" a; a=(b); _describe "group2" a }'