5
5
# 依赖 fd 和 fzf
6
6
7
7
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 () {
14
9
local dir length seg typeArg
15
10
if [[ " $1 " == * / ]]; then
16
11
# 注意下方代码的 $(echo 4) 比如得这么写不然会和 zsh 不兼容导致错误, 这么写是最好的兼容性
@@ -56,7 +51,7 @@ __zic_matched_subdir_list() {
56
51
# if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
57
52
# continue
58
53
# fi
59
- # if [ "$zic_case_insensitive " = "true" ]; then
54
+ # if [ "$tab_case_insensitive " = "true" ]; then
60
55
if [[ " $line :u" == * " $seg :u" * ]]; then
61
56
echo " $line "
62
57
fi
@@ -89,7 +84,7 @@ __zic_matched_subdir_list() {
89
84
# # if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
90
85
# # continue
91
86
# # fi
92
- # # if [ "$zic_case_insensitive " = "true" ]; then
87
+ # # if [ "$tab_case_insensitive " = "true" ]; then
93
88
# if [[ "$line:u" == "$seg:u"* ]]; then
94
89
# echo "$line"
95
90
# fi
@@ -119,7 +114,7 @@ __zic_matched_subdir_list() {
119
114
# if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
120
115
# continue
121
116
# fi
122
- # if [ "$zic_case_insensitive " = "true" ]; then
117
+ # if [ "$tab_case_insensitive " = "true" ]; then
123
118
if [[ " $line :u" == * " $seg :u" * ]]; then
124
119
echo " $line "
125
120
fi
@@ -133,9 +128,9 @@ __zic_matched_subdir_list() {
133
128
fi
134
129
}
135
130
136
- __zic_fzf_bindings () {
131
+ __tab_fzf_bindings () {
137
132
autoload is-at-least
138
- fzf=$( __zic_fzf_prog )
133
+ fzf=$( __fzfcmdex )
139
134
140
135
if $( is-at-least ' 0.21.0' $( ${=fzf} --version) ) ; then
141
136
echo ' shift-tab:up,tab:down,bspace:backward-delete-char/eof'
@@ -144,7 +139,7 @@ __zic_fzf_bindings() {
144
139
fi
145
140
}
146
141
147
- _zic_list_generator () {
142
+ __pre_gen_subdir_res () {
148
143
tokens=(${(z)LBUFFER} )
149
144
cmd=${tokens[1]}
150
145
@@ -167,7 +162,6 @@ _zic_list_generator() {
167
162
cmd_to_slash_fd_type[" cd" ]=" directory"
168
163
cmd_to_slash_fd_type[" vim" ]=" directory file"
169
164
170
-
171
165
# echo "before cmd: "${cmd}
172
166
if [ -z ${cmd_to_fd_type["$cmd"]} ]; then
173
167
cmd=" other"
@@ -176,20 +170,20 @@ _zic_list_generator() {
176
170
# echo "alfter cmd_to_fd_type cmd: "${cmd_to_fd_type["$cmd"]}
177
171
# echo "alfter cmd_to_fd_depth cmd: "${cmd_to_fd_depth["$cmd"]}
178
172
# 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
181
175
}
182
176
183
- # 这个是提前fd生成好结果供挑选, 有一定延迟, 不适合深度搜索, depth 在 5 层以下比较适合
184
- _zic_complete () {
177
+ # 这个是提前fd生成好结果供挑选, 有一定延迟, 不适合深度搜索, depth 在 5 层以下比较适合, 目前配的是1层
178
+ _tab_complete () {
185
179
setopt localoptions nonomatch
186
180
local l matches fzf tokens base
187
181
188
182
if [ -e " ${(Q)@ [-1]} " ]; then # 如果用户要搜索的东西已经存在了, 用户还是按了tab, 那说明不是用户想要的结果, 那就继续递归搜索下面的所有的
189
183
__fzf_file_widget_ex $@
190
184
return
191
185
else
192
- l=$( _zic_list_generator $@ )
186
+ l=$( __pre_gen_subdir_res $@ )
193
187
# 如果检测当前文件夹的只有一个返回结果, 而且不为空字符串则直接补全, 否则在子文件夹里递归搜索
194
188
if [[ $( echo $l | wc -l) -eq 1 && -n " $l " ]]; then
195
189
# echo "\n only 1: "$l
@@ -375,7 +369,7 @@ __get_fd_result() {
375
369
# echo "\n"
376
370
# local cmd="command fd . --follow -HI --exclude '.git' --exclude '.svn' --type f --max-depth 5 2>/dev/null"
377
371
local cmd=" $1 "
378
- fzf_bindings=$( __zic_fzf_bindings )
372
+ fzf_bindings=$( __tab_fzf_bindings )
379
373
setopt localoptions pipefail no_aliases 2> /dev/null
380
374
local item
381
375
# 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() {
392
386
echo " fzf-tmux ${FZF_TMUX_OPTS:- -d${FZF_TMUX_HEIGHT:- 40% } } -- " || echo " fzf"
393
387
}
394
388
395
- # 这个不是提前fd生成好结果供挑选, 而是直接 fzf 动态 fd 的, 适合深度搜索, depth 在 1 层的用 _zic_complete 函数比较适合
389
+ # 这个不是提前fd生成好结果供挑选, 而是直接 fzf 动态 fd 的, 适合深度搜索, depth 在 1 层的用 _tab_complete 函数比较适合
396
390
__fzf_file_widget_ex () {
397
391
local fd_cmd fd_res tokens base seg
398
392
@@ -408,7 +402,11 @@ __fzf_file_widget_ex() {
408
402
if [ -e $base ]; then
409
403
seg=" "
410
404
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
412
410
fi
413
411
414
412
# echo "\n __fzf_file_widget_ex seg: "${seg}
@@ -489,50 +487,59 @@ __fzf_file_widget_ex() {
489
487
return $ret
490
488
}
491
489
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 () {
493
507
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
494
- # local tokens cmd
495
- local tokens
496
- #
508
+ local tokens cmd base
509
+
497
510
tokens=(${(z)LBUFFER} )
498
- # # cmd=${tokens[1]}
499
- #
500
- # _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
511
+ cmd=${tokens[1]}
501
512
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
506
516
fi
507
517
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}
520
527
}
521
528
522
- [ -z " $__zic_default_completion " ] && {
529
+ [ -z " $__tab_default_completion " ] && {
523
530
binding=$( bindkey ' ^I' )
524
531
# $binding[(s: :w)2]
525
532
# The command substitution and following word splitting to determine the
526
533
# default zle widget for ^I formerly only works if the IFS parameter contains
527
534
# a space via $binding[(w)2]. Now it specifically splits at spaces, regardless
528
535
# of IFS.
529
- [[ $binding =~ ' undefined-key' ]] || __zic_default_completion =$binding [(s: :w)2]
536
+ [[ $binding =~ ' undefined-key' ]] || __tab_default_completion =$binding [(s: :w)2]
530
537
unset binding
531
538
}
532
539
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'
536
543
fi
537
- bindkey " ${zic_custom_binding } " zic -completion
544
+ bindkey " ${tab_custom_binding } " tab -completion
538
545
0 commit comments