forked from adaptee/pinyin-completion
-
Notifications
You must be signed in to change notification settings - Fork 13
/
pinyin-completion.plugin.zsh
92 lines (86 loc) · 1.81 KB
/
pinyin-completion.plugin.zsh
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
# shellcheck disable=all
# https://github.com/zdharma-continuum/Zsh-100-Commits-Club/blob/master/Zsh-Plugin-Standard.adoc
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0:#/*}:-$PWD/$0}"
local -A _punctuation_map=(
[~]=~
[!]=!
[@]=@
[#]='#'
[$]=$
[%]=%
[&]='&'
[*]=*
[(]='('
[)]=')'
[_]=_
[-]=-
[+]=+
[[]='['
[]]=']'
[「]='['
[」]=']'
[【]='['
[】]=']'
[〔]='['
[〕]=']'
[<]='<'
[>]='>'
[《]='<'
[》]='>'
[«]='<'
[»]='>'
[‹]='<'
[›]='>'
[?]=?
[,]=,
[。]=.
[/]=/
[\]=\\
[、]=\\
[…]=...
)
_pinyin_comp()
{
# [:ascii:] need
setopt rematchpcre
# unix file names can contain '\n', so use '\0' to separate them
local IFS=$'\0' suffix result file k v
local -i i=1
local -a files results
if [ "$words[1]" = cd ] ; then
suffix=/
else
suffix=''
fi
# print -N use '\0' to separate outputs
for file in $(print -nN ${1:h}/*"$suffix"); do
file="${file#./}"
if [[ $file =~ [^[:ascii:]] ]]; then
files+=($file)
else
result="$file"
if [[ $result == $1* ]]; then
reply+=(${(q)file})
fi
fi
done
if (( $#files )); then
results=(${(f)$(pypinyin -fslug -sz -p= $files)})
for result in $results; do
for k v in ${(kv)FUZZY} ${(kv)_punctuation_map}; do
result="${result//$k/$v}"
done
if [[ $result == $1* ]]; then
reply+=(${(q)${files[i]}})
fi
i=$((i + 1))
done
fi
}
# pinyin-comp is performed as one part of user-expand
zstyle ':completion:*' user-expand _pinyin_comp
# omit original and all expansions when showing the result of user-expand
zstyle ':completion:*:user-expand:*' tag-order expansions
# make use-expand perform as last, when needed
zstyle ':completion:*' completer _oldlist _expand _complete _match _user_expand