forked from emptyhua/bash-pinyin-completion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinyin_completion
executable file
·136 lines (113 loc) · 3.4 KB
/
pinyin_completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
#参考:https://github.com/emptyhua/bash-pinyin-completion
#参考:http://linuxtoy.org/archives/chsdir.html
#参考:/etc/bash_completion
#用于保存覆盖之前的函数
#from http://mivok.net/2009/09/20/bashfunctionoverride.html
__logfile=/Users/whxx/test/x/1
# __logfile=/dev/null
__MYLOG() {
echo "$@" >> ${__logfile}
}
_py_save_function() {
local ORIG_FUNC=$(declare -f $1)
local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
eval "$NEWNAME_FUNC"
}
#兼容旧版本的bash-completion
if test -z "$(typeset -f _compopt_o_filenames)"; then
# This function turns on "-o filenames" behavior dynamically. It is present
# for bash < 4 reasons. See http://bugs.debian.org/272660#64 for info about
# the bash < 4 compgen hack.
_compopt_o_filenames() {
# We test for compopt availability first because directly invoking it on
# bash < 4 at this point may cause terminal echo to be turned off for some
# reason, see https://bugzilla.redhat.com/653669 for more info.
type compopt &>/dev/null && compopt -o filenames 2>/dev/null || \
compgen -f /non-existing-dir/ >/dev/null
}
fi
#中文补齐函数
_py_completion() {
local IFS=$'\t\n'
local cur
#兼容旧版本的bash-completion
test -z "$(typeset -f _get_comp_words_by_ref)" && cur="$(_get_cword)" || _get_comp_words_by_ref cur
_expand || return 0
local dname_real bname_real dname bname tmp tmp2 sep
local -a rt
#目录名部分
dname="$(dirname "$cur")";
#文件名部分
bname="$(basename "$cur")";
#cd "./
if [[ "${dname:0:1}" == "\"" ]]; then
dname="${dname:1}"
fi
if [[ "${bname:0:1}" == "\"" ]]; then
bname="${bname:1}"
fi
#eval 用于消除转义符 例如 \[ 将变成 [
eval dname_real="$dname"
eval bname_real="$bname"
test -z "$bname_real" && return 0;
if [[ "$dname" == "/" ]];then
sep=""
else
sep="/"
fi
test ! -d "$dname_real" && return 0;
cd $dname_real
if [[ "$1" == "-d" ]];then
rt=($(
compgen -d|pinyinmatch "$bname_real"|while read -r tmp;do
# printf '%s%s%s\n' "$dname_real" "$sep" "$tmp"
printf '%s%\n' "$dname_real"
done
))
else
rt=($(compgen -f|pinyinmatch "$bname_real" 2>>${__logfile}))
# [ ${#rt[@]} -ne 0 ] && _compopt_o_filenames
_compopt_o_filenames
fi
cd $OLDPWD
__MYLOG keyword="$bname_real"
__MYLOG rt="${rt[@]}"
__MYLOG pre COMPREPLY="${COMPREPLY[@]}"
COMPREPLY=( "${COMPREPLY[@]}" "${rt[@]}" )
# 去重
local -a list
local var var1 found len
for var in "${COMPREPLY[@]}";do
found=0
for var1 in "${list[@]}";do
if [ "$var" = "$var1" ];then
found=1
fi
done
if [ "$found" = "0" ];then
list=( "${list[@]}" "${var}" );
fi
done
__MYLOG list="${list[@]}"
len=${#list[@]}
if [ ${len} -eq 1 ];then
if [ "$dname_real" != "." ];then
COMPREPLY=( "${dname_real}${sep}${list[0]}" )
fi
elif [ ${len} -ne 0 ];then
COMPREPLY=( "${list[@]}" "+[1-${len}]选择:" )
fi
__MYLOG COMPREPLY="${COMPREPLY[@]}"
}
_py_save_function _filedir _py_bak_filedir
_filedir() {
_py_bak_filedir $@
_py_completion $@
}
_py_save_function _filedir_xspec _py_bak_filedir_xspec
_filedir_xspec () {
_py_bak_filedir_xspec $@
_py_completion $@
}
unset -f _py_save_function