Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dict support #444

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion autoload/EasyMotion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,12 @@ function! s:convertRegep(input) "{{{
" 3. smartsign
" 4. smartcase
let use_migemo = s:should_use_migemo(a:input)
let re = use_migemo || s:should_use_regexp() ? a:input : s:escape_regexp_char(a:input)
let use_dict = g:EasyMotion_dict
let re = use_migemo || !empty(use_dict) || s:should_use_regexp() ? a:input : s:escape_regexp_char(a:input)

if !empty(use_dict)
let re = EasyMotion#dict#convert(re, use_dict)
endif

" Convert space to match only start of spaces
if re ==# ' '
Expand Down
12 changes: 12 additions & 0 deletions autoload/EasyMotion/dict.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

function! EasyMotion#dict#convert(input, dict)
if a:dict =~ '^zh'
let pat = join(map(split(a:input, '\zs'),
\ 'get(g:EasyMotion#dict#zh#utf8#' . split(a:dict, '-')[1] . ', v:val, v:val)'), '')
if !empty(pat)
return a:input . '\|' . pat
endif
endif

return a:input
endfunction
79 changes: 79 additions & 0 deletions autoload/EasyMotion/dict/zh/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#A parser for the CC-Cedict. Convert the Chinese-English dictionary into a list of python dictionaries with "traditional","simplified", "pinyin", and "english" keys.

#Make sure that the cedict_ts.u8 file is in the same folder as this file, and that the name matches the file name on line 13.

#Before starting, open the CEDICT text file and delete the copyright information at the top. Otherwise the program will try to parse it and you will get an error message.

#Characters that are commonly used as surnames have two entries in CC-CEDICT. This program will remove the surname entry if there is another entry for the character. If you want to include the surnames, simply delete lines 59 and 60.

#This code was written by Franki Allegra in February 2020.

#Get cedict_ts.u8 from https://www.mdbg.net/chinese/dictionary?page=cedict

def parse_line(line):
parsed = {}
line = line.rstrip('/')
line = line.split('/')
if len(line) <= 1:
return None
english = line[1]
char_and_pinyin = line[0].split('[')
characters = char_and_pinyin[0]
characters = characters.split()
traditional = characters[0]
simplified = characters[1]
pinyin = char_and_pinyin[1]
pinyin = pinyin.rstrip()
pinyin = pinyin.rstrip(']')
parsed['traditional'] = traditional
parsed['simplified'] = simplified
parsed['pinyin'] = pinyin
parsed['english'] = english
return parsed

def get_st():
s = {}
t = {}
e = [chr(x) for x in range(ord('a'), ord('z') + 1)]
e += [x.upper() for x in e]
e += [chr(x) for x in range(ord('0'), ord('9') + 1)]
# print(e)
for line in open('cedict_ts.u8', 'r', encoding='utf-8'):
if line[0] == '#':
continue
parsed = parse_line(line)
if parsed:
# if len(parsed['simplified']) == 1:
# try:
# print(parsed)
# except Exception as e:
# pass
if len(parsed['simplified']) == 1 and parsed['simplified'] not in e and ord(parsed['simplified']) > 255:
k = parsed['pinyin'][0].lower()
s[k] = s.get(k, '') + parsed['simplified']
if len(parsed['traditional']) == 1 and parsed['traditional'] not in e and ord(parsed['traditional']) > 255:
k = parsed['pinyin'][0].lower()
t[k] = t.get(k, '') + parsed['traditional']
return (s, t)


s, t = get_st()

with open('utf8.vim', 'w', encoding='utf-8', newline='\n') as fp:
cnt = 0
fp.write("let g:EasyMotion#dict#zh#utf8#cn = {\n")
for c in range(ord('a'), ord('z') + 1):
if s.get(chr(c)):
cnt += len(s[chr(c)])
fp.write("\t\\ '{:}' : '[{}]',\n".format(chr(c), s[chr(c)]))
fp.write("\t\\}\n")
print('cnt: ', cnt)

cnt = 0
fp.write("let g:EasyMotion#dict#zh#utf8#tw = {\n")
for c in range(ord('a'), ord('z') + 1):
if t.get(chr(c)):
cnt += len(t[chr(c)])
fp.write("\t\\ '{:}' : '[{}]',\n".format(chr(c), t[chr(c)]))
fp.write("\t\\}\n")
print('cnt: ', cnt)
50 changes: 50 additions & 0 deletions autoload/EasyMotion/dict/zh/utf8.vim

Large diffs are not rendered by default.

30 changes: 27 additions & 3 deletions autoload/vital/_easymotion/HitAHint/Motion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,33 @@ endfunction
" window for both direction excluding poses in fold.
" @return {{list<list<(number,number))>}}
function! s:gather_poses(pattern) abort
let f = s:gather_visible_matched_poses(a:pattern, s:DIRECTION.forward, s:TRUE)
let b = s:gather_visible_matched_poses(a:pattern, s:DIRECTION.backward, s:FALSE)
return filter(f + b, '!s:is_in_fold(v:val[0])')
"let f = s:gather_visible_matched_poses(a:pattern, s:DIRECTION.forward, s:TRUE)
"let b = s:gather_visible_matched_poses(a:pattern, s:DIRECTION.backward, s:FALSE)
"return filter(f + b, '!s:is_in_fold(v:val[0])')
"return f

let poses = []
let view = winsaveview()

let lnum = line('w0')
let stop_line = line('w$')
while lnum <= stop_line
call cursor(lnum, 1)
let fold_end = foldclosedend(lnum)
if fold_end == -1
keepjumps let pos = searchpos(a:pattern, 'c', lnum)
while pos != [0, 0]
let poses += [pos]
keepjumps let pos = searchpos(a:pattern, '', lnum)
endwhile
let lnum += 1
else
let lnum = fold_end + 1
endif
endwhile

call winrestview(view)
return poses
endfunction

" s:gather_visible_matched_poses() aggregates pattern matched positions in visible current
Expand Down
24 changes: 24 additions & 0 deletions doc/easymotion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CONTENTS *easymotion-contents*
Requirements ....................... |easymotion-requirements|
Configuration ...................... |easymotion-configuration|
EasyMotion_keys ................. |EasyMotion_keys|
EasyMotion_dict ................. |EasyMotion_dict|
EasyMotion_do_shade ............. |EasyMotion_do_shade|
EasyMotion_do_mapping ........... |EasyMotion_do_mapping|
EasyMotion_grouping ............. |EasyMotion_grouping|
Expand Down Expand Up @@ -756,6 +757,29 @@ Example:
'asdfghjkl;qwertyuiopzxcvbnmèòàùì'
<

------------------------------------------------------------------------------
EasyMotion_dict *EasyMotion_dict*
*g:EasyMotion_dict*
Set the dict to extend motion targets.

Supported dict currently:

>
'zh-cn': simplified chinese
'zh-tw': traditional chinese
<

Default: ''

Example:

Text below with motion key `a`:

>
abcdef啊测试
^ ^
<

------------------------------------------------------------------------------
EasyMotion_do_shade *EasyMotion_do_shade*
*g:EasyMotion_do_shade*
Expand Down
1 change: 1 addition & 0 deletions plugin/EasyMotion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set cpo&vim
let g:EasyMotion_keys = get(g:,
\ 'EasyMotion_keys', 'asdghklqwertyuiopzxcvbnmfj;')
" \ 'EasyMotion_keys', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
let g:EasyMotion_dict = get(g: , 'EasyMotion_dict' , '')
let g:EasyMotion_do_mapping = get(g: , 'EasyMotion_do_mapping' , 1)
let g:EasyMotion_do_shade = get(g: , 'EasyMotion_do_shade' , 1)
let g:EasyMotion_grouping = get(g: , 'EasyMotion_grouping' , 1)
Expand Down