#!/hint/zshlocal dsuf dpre k _v filepath first_word show_group default_color prefix bs=$'\b'local -a list_colors group_colors tcandidates reply match mbegin mend
local -i same_word=1colorful=0local -Ua duplicate_groups=()local -A word_map=()(($#_ftb_compcap==0))&&return-ftb-zstyle -s show-group show_group ||show_group=full
-ftb-zstyle -s default-color default_color ||default_color=''-ftb-zstyle -s prefix prefix ||{ zstyle -m ':completion:*:descriptions' format '*'&&prefix='·'}-ftb-zstyle -a group-colors group_colors ||group_colors=($_ftb_group_colors)zstyle -a ":completion:$_ftb_curcontext" list-colors list_colors
# init colorizeif(( $+builtins[fzf-tab-candidates-generate])); then fzf-tab-candidates-generate -i list_colors
# Use external sort for module candidates to keep ordering stable.colorful=1elselocal -A namecolors=(${(@s:=:)${(@s.:.)list_colors}:#[[:alpha:]][[:alpha:]]=*})local -A modecolors=(${(@Ms:=:)${(@s.:.)list_colors}:#[[:alpha:]][[:alpha:]]=*})(($#namecolors==0&&$#modecolors==0))&&list_colors=()fiif(($#_ftb_groups==1)); then -ftb-zstyle -m single-group prefix ||prefix='' -ftb-zstyle -m single-group color ||group_colors=("$default_color")fiif(( $+builtins[fzf-tab-candidates-generate])); then fzf-tab-candidates-generate
elsefor k _v in"${(@ps:\2:)_ftb_compcap}"; dolocal -A v=("${(@0)_v}")[[$v[word]==${first_word:=$v[word]}]]||same_word=0# add character and color to describe the type of the filesdsuf=''dpre=''if(( $+v[realdir])); thenfilepath=$v[realdir]${(Q)v[word]}if[[ -d $filepath]]; thendsuf=/
fi# add color and resolve symlink if have list-colors# detail: http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fcomplist-Moduleif(($#list_colors ))&&[[ -a $filepath|| -L $filepath]]; then -ftb-colorize $filepathcolorful=1elif[[ -L $filepath]]; thendsuf=@
fiif[[$options[list_types]== off ]]; thendsuf=''fifi# add color to description if they have group indexif(( $+v[group])); thenlocalcolor=$group_colors[$v[group]]# add a hidden group index at start of string to keep group order when sorting# first group index is for builtin sort, sencond is for GNU sorttcandidates+=$v[group]$'\b'$color$prefix$dpre$'\0'$v[group]$'\b'$k$'\0'$dsufelsetcandidates+=$default_color$dpre$'\0'$k$'\0'$dsuffi# check group with duplicate memberif[[$show_group== brief ]]; thenif(( $+word_map[$v[word]]&& $+v[group])); thenduplicate_groups+=$v[group]# add this groupduplicate_groups+=$word_map[$v[word]]# add previous groupfi word_map[$v[word]]=$v[group]fidonefi(( same_word ))&& tcandidates[2,-1]=()# sort and remove sort group or other indexzstyle -T ":completion:$_ftb_curcontext" sort
if(($? !=1)); thenif(( colorful )); then# if enable list_colors, we should skip the first fieldif[[${commands[sort]:A:t} !=(|busybox*)]]; then# this is faster but doesn't work if `find` is from busyboxtcandidates=(${(f)"$(command sort -u -t '\0' -k 2<<<${(pj:\n:)tcandidates})"})else# slower but portabletcandidates=(${(@o)${(@)tcandidates:/(#b)([^$'\0']#)$'\0'(*)/$match[2]$'\0'$match[1]}})tcandidates=(${(@)tcandidates/(#b)(*)$'\0'([^$'\0']#)/$match[2]$'\0'$match[1]})fielsetcandidates=("${(@o)tcandidates}")fifitypeset -gUa _ftb_complist=("${(@)tcandidates//[0-9]#$bs}")# hide needless groupif(($#_ftb_groups )); thenlocal i to_hide indexs=({1..$#_ftb_groups})case$show_groupin brief)to_hide=(${indexs:|duplicate_groups}) ;;
none)to_hide=($indexs) ;;
esacfor i in$to_hide; do# NOTE: _ftb_groups is unique array _ftb_groups[i]="__hide__$i"donefi