Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0fde454

Browse files
author
no5ix
committedAug 23, 2022
optimize && add enhanced cd
1 parent 54754fc commit 0fde454

File tree

1 file changed

+57
-50
lines changed

1 file changed

+57
-50
lines changed
 

‎zsh-tab-any.plugin.zsh ‎tab-any-path.plugin.zsh

+57-50
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
# 依赖 fd 和 fzf
66

77

8-
__zic_fzf_prog() {
9-
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] \
10-
&& echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
11-
}
12-
13-
__zic_matched_subdir_list() {
8+
__do_pre_gen_subdir_res() {
149
local dir length seg typeArg
1510
if [[ "$1" == */ ]]; then
1611
# 注意下方代码的 $(echo 4) 比如得这么写不然会和 zsh 不兼容导致错误, 这么写是最好的兼容性
@@ -56,7 +51,7 @@ __zic_matched_subdir_list() {
5651
# if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
5752
# continue
5853
# fi
59-
# if [ "$zic_case_insensitive" = "true" ]; then
54+
# if [ "$tab_case_insensitive" = "true" ]; then
6055
if [[ "$line:u" == *"$seg:u"* ]]; then
6156
echo "$line"
6257
fi
@@ -89,7 +84,7 @@ __zic_matched_subdir_list() {
8984
# # if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
9085
# # continue
9186
# # fi
92-
## if [ "$zic_case_insensitive" = "true" ]; then
87+
## if [ "$tab_case_insensitive" = "true" ]; then
9388
# if [[ "$line:u" == "$seg:u"* ]]; then
9489
# echo "$line"
9590
# fi
@@ -119,7 +114,7 @@ __zic_matched_subdir_list() {
119114
# if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
120115
# continue
121116
# fi
122-
# if [ "$zic_case_insensitive" = "true" ]; then
117+
# if [ "$tab_case_insensitive" = "true" ]; then
123118
if [[ "$line:u" == *"$seg:u"* ]]; then
124119
echo "$line"
125120
fi
@@ -133,9 +128,9 @@ __zic_matched_subdir_list() {
133128
fi
134129
}
135130

136-
__zic_fzf_bindings() {
131+
__tab_fzf_bindings() {
137132
autoload is-at-least
138-
fzf=$(__zic_fzf_prog)
133+
fzf=$(__fzfcmdex)
139134

140135
if $(is-at-least '0.21.0' $(${=fzf} --version)); then
141136
echo 'shift-tab:up,tab:down,bspace:backward-delete-char/eof'
@@ -144,7 +139,7 @@ __zic_fzf_bindings() {
144139
fi
145140
}
146141

147-
_zic_list_generator() {
142+
__pre_gen_subdir_res() {
148143
tokens=(${(z)LBUFFER})
149144
cmd=${tokens[1]}
150145

@@ -167,7 +162,6 @@ _zic_list_generator() {
167162
cmd_to_slash_fd_type["cd"]="directory"
168163
cmd_to_slash_fd_type["vim"]="directory file"
169164

170-
171165
# echo "before cmd: "${cmd}
172166
if [ -z ${cmd_to_fd_type["$cmd"]} ]; then
173167
cmd="other"
@@ -176,20 +170,20 @@ _zic_list_generator() {
176170
# echo "alfter cmd_to_fd_type cmd: "${cmd_to_fd_type["$cmd"]}
177171
# echo "alfter cmd_to_fd_depth cmd: "${cmd_to_fd_depth["$cmd"]}
178172
# echo "alfter cmd_to_slash_fd_type cmd: "${cmd_to_slash_fd_type["$cmd"]}
179-
__zic_matched_subdir_list "${(Q)@[-1]}" ${cmd_to_fd_type["$cmd"]} ${cmd_to_fd_depth["$cmd"]} ${cmd_to_slash_fd_type["$cmd"]} | sort
180-
# __zic_matched_subdir_list "${(Q)@[-1]}" | sort
173+
__do_pre_gen_subdir_res "${(Q)@[-1]}" ${cmd_to_fd_type["$cmd"]} ${cmd_to_fd_depth["$cmd"]} ${cmd_to_slash_fd_type["$cmd"]} | sort
174+
# __do_pre_gen_subdir_res "${(Q)@[-1]}" | sort
181175
}
182176

183-
# 这个是提前fd生成好结果供挑选, 有一定延迟, 不适合深度搜索, depth 在 5 层以下比较适合
184-
_zic_complete() {
177+
# 这个是提前fd生成好结果供挑选, 有一定延迟, 不适合深度搜索, depth 在 5 层以下比较适合, 目前配的是1层
178+
_tab_complete() {
185179
setopt localoptions nonomatch
186180
local l matches fzf tokens base
187181

188182
if [ -e "${(Q)@[-1]}" ]; then # 如果用户要搜索的东西已经存在了, 用户还是按了tab, 那说明不是用户想要的结果, 那就继续递归搜索下面的所有的
189183
__fzf_file_widget_ex $@
190184
return
191185
else
192-
l=$(_zic_list_generator $@)
186+
l=$(__pre_gen_subdir_res $@)
193187
# 如果检测当前文件夹的只有一个返回结果, 而且不为空字符串则直接补全, 否则在子文件夹里递归搜索
194188
if [[ $(echo $l | wc -l) -eq 1 && -n "$l" ]]; then
195189
# echo "\n only 1: "$l
@@ -375,7 +369,7 @@ __get_fd_result() {
375369
# echo "\n"
376370
# local cmd="command fd . --follow -HI --exclude '.git' --exclude '.svn' --type f --max-depth 5 2>/dev/null"
377371
local cmd="$1"
378-
fzf_bindings=$(__zic_fzf_bindings)
372+
fzf_bindings=$(__tab_fzf_bindings)
379373
setopt localoptions pipefail no_aliases 2> /dev/null
380374
local item
381375
# eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmdex) -m "$@" | while read item; do
@@ -392,7 +386,7 @@ __fzfcmdex() {
392386
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
393387
}
394388

395-
# 这个不是提前fd生成好结果供挑选, 而是直接 fzf 动态 fd 的, 适合深度搜索, depth 在 1 层的用 _zic_complete 函数比较适合
389+
# 这个不是提前fd生成好结果供挑选, 而是直接 fzf 动态 fd 的, 适合深度搜索, depth 在 1 层的用 _tab_complete 函数比较适合
396390
__fzf_file_widget_ex() {
397391
local fd_cmd fd_res tokens base seg
398392

@@ -408,7 +402,11 @@ __fzf_file_widget_ex() {
408402
if [ -e $base ]; then
409403
seg=""
410404
else
411-
seg=$(basename -- "$base") # 比如: [root@web-01 ~]# basename /usr/bin/sort 得到sort
405+
if [[ "${base}" == "-"* || "${base}" == "--"* ]]; then # 比如类似 `ln -s` 或者 `ln -s ` 则 seg应该要为空才对
406+
seg=""
407+
else
408+
seg=$(basename -- "$base") # 比如: [root@web-01 ~]# basename /usr/bin/sort 得到sort
409+
fi
412410
fi
413411

414412
# echo "\n __fzf_file_widget_ex seg: "${seg}
@@ -489,50 +487,59 @@ __fzf_file_widget_ex() {
489487
return $ret
490488
}
491489

492-
zic-completion() {
490+
# cd into the directory of the selected file
491+
cd() {
492+
if [ -z $1 ]; then
493+
# echo "\n z : "$1
494+
builtin cd
495+
return
496+
fi
497+
if [ -d $1 ]; then
498+
builtin cd "$1"
499+
else
500+
local dir=$(dirname "$1")
501+
# echo "\n dir : "${dir}
502+
builtin cd "$dir"
503+
fi
504+
}
505+
506+
tab-completion() {
493507
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
494-
# local tokens cmd
495-
local tokens
496-
#
508+
local tokens cmd base
509+
497510
tokens=(${(z)LBUFFER})
498-
## cmd=${tokens[1]}
499-
#
500-
# _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
511+
cmd=${tokens[1]}
501512

502-
if [[ "$cmd" = cd || "$cmd" = vim || "$cmd" = vi ]]; then
503-
_zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
504-
else
505-
zle ${__zic_default_completion:-expand-or-complete}
513+
if [[ "${LBUFFER}" == *" -" || "${LBUFFER}" == *" --" ]]; then
514+
zle ${__tab_default_completion:-expand-or-complete}
515+
return
506516
fi
507517

508-
# if [[ "$LBUFFER" =~ "^\ *cd$" && "$LBUFFER" =~ "^\ *vim$" ]]; then
509-
# zle ${__zic_default_completion:-expand-or-complete}
510-
# elif [ "$cmd" = cd ]; then
511-
# _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
512-
# elif [[ "$LBUFFER" =~ "^\ *vim$" ]]; then
513-
# zle ${__zic_default_completion:-expand-or-complete}
514-
# elif [ "$cmd" = vim ]; then
515-
# _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
516-
# else
517-
# zle ${__zic_default_completion:-expand-or-complete}
518-
# _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
519-
# fi
518+
takeover_arr=(cd vim vi ls ll l cp mv ln cat rm chmod chown)
519+
for i in ${takeover_arr[@]}; do
520+
if [ "$cmd" = "$i" ]; then
521+
_tab_complete ${tokens[2,${#tokens}]/#\~/$HOME}
522+
return
523+
fi
524+
done
525+
526+
zle ${__tab_default_completion:-expand-or-complete}
520527
}
521528

522-
[ -z "$__zic_default_completion" ] && {
529+
[ -z "$__tab_default_completion" ] && {
523530
binding=$(bindkey '^I')
524531
# $binding[(s: :w)2]
525532
# The command substitution and following word splitting to determine the
526533
# default zle widget for ^I formerly only works if the IFS parameter contains
527534
# a space via $binding[(w)2]. Now it specifically splits at spaces, regardless
528535
# of IFS.
529-
[[ $binding =~ 'undefined-key' ]] || __zic_default_completion=$binding[(s: :w)2]
536+
[[ $binding =~ 'undefined-key' ]] || __tab_default_completion=$binding[(s: :w)2]
530537
unset binding
531538
}
532539

533-
zle -N zic-completion
534-
if [ -z $zic_custom_binding ]; then
535-
zic_custom_binding='^I'
540+
zle -N tab-completion
541+
if [ -z $tab_custom_binding ]; then
542+
tab_custom_binding='^I'
536543
fi
537-
bindkey "${zic_custom_binding}" zic-completion
544+
bindkey "${tab_custom_binding}" tab-completion
538545

0 commit comments

Comments
 (0)
Please sign in to comment.