Skip to content

Instantly share code, notes, and snippets.

@leoleoasd
Created January 7, 2021 04:56
Show Gist options
  • Save leoleoasd/8f63b0c8af0ce96157f989340ad70bfa to your computer and use it in GitHub Desktop.
Save leoleoasd/8f63b0c8af0ce96157f989340ad70bfa to your computer and use it in GitHub Desktop.
zsh拼音自动补全
# use pinyin-comp to perform completion based upon pinyin acronym
function _pinyin_comp()
{
# chsdir print one candidate per line
# this looks weird, bug IFS='\n' does not work in interactive shell
local IFS=$'\n'
reply = ($(~/.oh-my-zsh/pinyin-test 0 $*))
echo reply
# reply=($(pinyin-comp 0 $*))
}
# pinyin-comp is performed as one part of user-expand
zstyle ':completion:*' user-expand _pinyin_comp
# omit origianl when showing the resul of user-expand
zstyle ':completion:*:user-expand:*' tag-order '!original'
# make use-expand perform as last, when needed
zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _match _user_expand
# vim:set ft=zsh :
#!/usr/bin/env python
from pypinyin import pinyin, Style
from glob import glob
import unicodedata
import os
import sys
if len(sys.argv) != 3:
exit(-1)
path = sys.argv[1]
typed = sys.argv[2]
result = []
def match(filename, pyfilename):
py = pinyin(filename, style=Style.NORMAL, heteronym=True, errors="default")
while len(pyfilename) != 0 and len(py) != 0:
now = py[0]
flag = False
for to_match in now:
if len(pyfilename) > len(to_match):
if pyfilename.startswith(to_match):
pyfilename = pyfilename[len(to_match):]
flag = True
break
elif to_match.startswith(pyfilename):
pyfilename = ""
flag = True
break
if pyfilename[0] == to_match[0] and (b'a' <= to_match[0].encode("ascii") <= b'z'):
pyfilename = pyfilename[1:]
flag = True
break
if not flag: return False
py = py[1:]
return True
for i in glob(os.path.join(path, "*")):
if match(i[len(os.path.join(path, "")):], typed):
result.append(os.path.basename(i))
print("\n".join(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment