2018-02-19 22:07:04 +08:00
|
|
|
|
"=============================================================================
|
|
|
|
|
" chinese.vim --- SpaceVim chinese layer
|
2023-03-26 13:44:47 +08:00
|
|
|
|
" Copyright (c) 2016-2023 Wang Shidong & Contributors
|
2022-03-27 13:38:54 +08:00
|
|
|
|
" Author: Wang Shidong < wsdjeg@outlook.com >
|
2018-02-19 22:07:04 +08:00
|
|
|
|
" URL: https://spacevim.org
|
|
|
|
|
" License: GPLv3
|
|
|
|
|
"=============================================================================
|
|
|
|
|
|
2022-01-03 13:09:19 +08:00
|
|
|
|
""
|
|
|
|
|
" @section Chinese, layers-chinese
|
|
|
|
|
" @parentsection layers
|
|
|
|
|
" `chinese` layer provides Chinese specific function for SpaceVim.
|
|
|
|
|
" This layer is not loaded by default, to use this layer, add following
|
|
|
|
|
" snippet into your SpaceVim configuration file.
|
|
|
|
|
" >
|
|
|
|
|
" [[layers]]
|
|
|
|
|
" name = 'chinese'
|
|
|
|
|
" <
|
|
|
|
|
"
|
|
|
|
|
" @subsection key bindings
|
|
|
|
|
" >
|
|
|
|
|
" Key binding Description
|
|
|
|
|
" SPC l c check with ChineseLinter
|
|
|
|
|
" SPC x g t translate current word
|
|
|
|
|
" SPC n c d convert chinese number to digit
|
|
|
|
|
" <
|
|
|
|
|
"
|
|
|
|
|
|
2018-02-19 22:07:04 +08:00
|
|
|
|
|
2017-01-14 13:00:03 +08:00
|
|
|
|
function! SpaceVim#layers#chinese#plugins() abort
|
2018-07-12 20:57:06 +08:00
|
|
|
|
let plugins = [
|
2019-01-21 22:19:33 +08:00
|
|
|
|
\ ['yianwillis/vimcdoc' , {'merged' : 0}],
|
2021-05-02 21:20:03 +08:00
|
|
|
|
\ ['voldikss/vim-translator' , {'merged' : 0, 'on_cmd' : ['Translate', 'TranslateW', 'TranslateR', 'TranslateX']}],
|
2019-04-18 20:31:42 +08:00
|
|
|
|
\ ['wsdjeg/ChineseLinter.vim' , {'merged' : 0, 'on_cmd' : 'CheckChinese', 'on_ft' : ['markdown', 'text']}],
|
2018-07-12 20:57:06 +08:00
|
|
|
|
\ ]
|
|
|
|
|
if SpaceVim#layers#isLoaded('ctrlp')
|
|
|
|
|
call add(plugins, ['vimcn/ctrlp.cnx', {'merged' : 0}])
|
|
|
|
|
endif
|
|
|
|
|
return plugins
|
2017-01-14 13:00:03 +08:00
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
function! SpaceVim#layers#chinese#config() abort
|
2018-07-12 20:57:06 +08:00
|
|
|
|
let g:_spacevim_mappings_space.x.g = {'name' : '+translate'}
|
2019-05-05 22:20:18 +08:00
|
|
|
|
call SpaceVim#mapping#space#def('nnoremap', ['x', 'g', 't'], 'Translate' , 'translate current word' , 1)
|
2019-01-21 22:19:33 +08:00
|
|
|
|
call SpaceVim#mapping#space#def('nnoremap', ['l', 'c'] , 'CheckChinese', 'Check with ChineseLinter', 1)
|
2021-12-13 21:50:38 +08:00
|
|
|
|
let g:_spacevim_mappings_space.n.c = {'name' : '+Convert'}
|
2022-03-19 20:08:35 +08:00
|
|
|
|
call SpaceVim#mapping#space#def('nmap', ['n', 'c', 'd'], '<Plug>ConvertChineseNumberToDigit', 'convert Chinese number to digit', 0, 1)
|
2018-09-29 23:16:47 +08:00
|
|
|
|
" do not load vimcdoc plugin
|
|
|
|
|
let g:loaded_vimcdoc = 1
|
2017-01-14 13:00:03 +08:00
|
|
|
|
endfunction
|
2021-08-10 19:59:42 +08:00
|
|
|
|
|
|
|
|
|
function! SpaceVim#layers#chinese#health() abort
|
|
|
|
|
call SpaceVim#layers#chinese#plugins()
|
|
|
|
|
call SpaceVim#layers#chinese#config()
|
|
|
|
|
return 1
|
|
|
|
|
endfunction
|
2021-12-13 21:50:38 +08:00
|
|
|
|
|
2022-03-19 20:08:35 +08:00
|
|
|
|
command! -nargs=0 -range ConvertChineseNumberToDigit :<line1>,<line2>call s:ConvertChineseNumberToDigit()
|
|
|
|
|
nnoremap <silent> <Plug>ConvertChineseNumberToDigit :ConvertChineseNumberToDigit<cr>
|
|
|
|
|
vnoremap <silent> <Plug>ConvertChineseNumberToDigit :ConvertChineseNumberToDigit<cr>
|
|
|
|
|
function! s:ConvertChineseNumberToDigit() range
|
|
|
|
|
let save_cursor = getcurpos()
|
|
|
|
|
let ChineseNumberPattern = '[〇一二三四五六七八九十百千万亿兆零壹贰叁肆伍陆柒捌玖拾佰仟萬億两点]\+'
|
|
|
|
|
if mode() ==? 'n' && a:firstline == a:lastline
|
|
|
|
|
let cword = expand('<cword>')
|
|
|
|
|
let cword = substitute(cword, ChineseNumberPattern, '\=s:Chinese2Digit(submatch(0))', "g")
|
|
|
|
|
let save_register_k = getreg("k")
|
|
|
|
|
call setreg("k", cword)
|
2021-12-13 21:50:38 +08:00
|
|
|
|
normal! viw"kp
|
2022-03-19 20:08:35 +08:00
|
|
|
|
call setreg("k", save_register_k)
|
|
|
|
|
else
|
|
|
|
|
silent execute a:firstline . "," . a:lastline . 'substitute/' . ChineseNumberPattern . '/\=s:Chinese2Digit(submatch(0))/g'
|
2021-12-13 21:50:38 +08:00
|
|
|
|
endif
|
2022-03-19 20:08:35 +08:00
|
|
|
|
call setpos('.', save_cursor)
|
2021-12-13 21:50:38 +08:00
|
|
|
|
endfunction
|
|
|
|
|
|
2022-03-19 20:08:35 +08:00
|
|
|
|
function! s:Chinese2Digit(cnDigitString)
|
2021-12-13 21:50:38 +08:00
|
|
|
|
let CN_NUM = {
|
2022-01-03 13:09:19 +08:00
|
|
|
|
\ '〇': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
|
|
|
|
|
\ '零': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9,
|
2022-03-19 20:08:35 +08:00
|
|
|
|
\ '貮': 2, '两': 2, '点': '.'
|
2022-01-03 13:09:19 +08:00
|
|
|
|
\ }
|
2021-12-13 21:50:38 +08:00
|
|
|
|
let CN_UNIT = {
|
2022-01-03 13:09:19 +08:00
|
|
|
|
\ '十': 10, '拾': 10, '百': 100, '佰': 100, '千': 1000, '仟': 1000, '万': 10000, '萬': 10000,
|
|
|
|
|
\ '亿': 100000000, '億': 100000000, '兆': 1000000000000
|
|
|
|
|
\ }
|
2021-12-13 21:50:38 +08:00
|
|
|
|
|
2022-03-19 20:08:35 +08:00
|
|
|
|
if a:cnDigitString =~ '^[点两貮〇一二三四五六七八九零壹贰叁肆伍陆柒捌玖]\+$'
|
|
|
|
|
let result = substitute(a:cnDigitString, ".", {m -> CN_NUM[m[0]]}, 'g')
|
|
|
|
|
else
|
|
|
|
|
let cnList = split(a:cnDigitString, "点")
|
|
|
|
|
let integer = map(str2list(cnList[0]), 'nr2char(v:val)') " 整数部分
|
|
|
|
|
let decimal = len(cnList) == 2 ? cnList[1] : [] " 小数部分
|
|
|
|
|
let unit = 0 " 当前单位
|
|
|
|
|
let parse = [] " 解析数组
|
|
|
|
|
while !empty(integer)
|
|
|
|
|
let x = remove(integer, -1)
|
|
|
|
|
if has_key(CN_UNIT, x)
|
|
|
|
|
" 当前字符是单位
|
|
|
|
|
let unit = CN_UNIT[x]
|
|
|
|
|
if unit == 10000 " 万位
|
|
|
|
|
call add(parse, "w")
|
|
|
|
|
let unit = 1
|
|
|
|
|
elseif unit == 100000000 " 亿位
|
|
|
|
|
call add(parse, "y")
|
|
|
|
|
let unit = 1
|
|
|
|
|
elseif unit == 1000000000000 " 兆位
|
|
|
|
|
call add (parse, "z")
|
|
|
|
|
let unit = 1
|
|
|
|
|
endif
|
2022-01-03 13:09:19 +08:00
|
|
|
|
continue
|
2022-03-19 20:08:35 +08:00
|
|
|
|
else
|
|
|
|
|
" 当前字符是数字
|
|
|
|
|
let dig = CN_NUM[x]
|
|
|
|
|
if unit
|
|
|
|
|
let dig *= unit
|
|
|
|
|
let unit = 0
|
|
|
|
|
endif
|
|
|
|
|
call add(parse, dig)
|
2022-01-03 13:09:19 +08:00
|
|
|
|
endif
|
2022-03-19 20:08:35 +08:00
|
|
|
|
endwhile
|
|
|
|
|
if unit == 10 " 处理10-19的数字
|
|
|
|
|
call add(parse, 10)
|
2021-12-13 21:50:38 +08:00
|
|
|
|
endif
|
2022-03-19 20:08:35 +08:00
|
|
|
|
let result = 0
|
|
|
|
|
let tmp = 0
|
|
|
|
|
while !empty(parse)
|
|
|
|
|
let x = remove(parse, -1)
|
|
|
|
|
if type(x) == type("")
|
|
|
|
|
if x == 'w'
|
|
|
|
|
let tmp *= 10000
|
|
|
|
|
let result += tmp
|
|
|
|
|
let tmp = 0
|
|
|
|
|
elseif x == 'y'
|
|
|
|
|
let tmp *= 100000000
|
|
|
|
|
let result += tmp
|
|
|
|
|
let tmp = 0
|
|
|
|
|
elseif x == 'z'
|
|
|
|
|
let tmp *= 1000000000000
|
|
|
|
|
let result += tmp
|
|
|
|
|
let tmp = 0
|
|
|
|
|
endif
|
|
|
|
|
else
|
|
|
|
|
let tmp += x
|
|
|
|
|
endif
|
|
|
|
|
endwhile
|
2021-12-13 21:50:38 +08:00
|
|
|
|
let result += tmp
|
2022-03-19 20:08:35 +08:00
|
|
|
|
if !empty(decimal)
|
|
|
|
|
let decimal = substitute(decimal, ".", {m -> CN_NUM[m[0]]}, 'g')
|
|
|
|
|
let result .= "." . decimal
|
|
|
|
|
endif
|
2021-12-13 21:50:38 +08:00
|
|
|
|
endif
|
|
|
|
|
return result
|
|
|
|
|
endfunction
|
2022-01-03 13:11:33 +08:00
|
|
|
|
|
|
|
|
|
" function() wrapper
|
|
|
|
|
if v:version > 703 || v:version == 703 && has('patch1170')
|
|
|
|
|
function! s:_function(fstr) abort
|
|
|
|
|
return function(a:fstr)
|
|
|
|
|
endfunction
|
|
|
|
|
else
|
|
|
|
|
function! s:_SID() abort
|
|
|
|
|
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
|
|
|
|
|
endfunction
|
|
|
|
|
let s:_s = '<SNR>' . s:_SID() . '_'
|
|
|
|
|
function! s:_function(fstr) abort
|
|
|
|
|
return function(substitute(a:fstr, 's:', s:_s, 'g'))
|
|
|
|
|
endfunction
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" vim:set et nowrap sw=2 cc=80:
|