1
0
forked from x/icebergs

add trans

This commit is contained in:
shaoying 2020-01-17 18:31:37 +08:00
parent 8f8a590d7a
commit 2ebe5294d3
3 changed files with 105 additions and 7 deletions

View File

@ -1,7 +1,19 @@
" 变量定义
func! ShyDefine(name, value)
if !exists("name") | exec "let " . a:name . " = \"" . a:value . "\"" | endif
endfunc
let ctx_url = (len($ctx_dev) > 1? $ctx_dev: "http://127.0.0.1:9095") . "/code/vim"
if !exists("g:ctx_sid") | let ctx_sid = "" | end
" 输出日志
" call ShyDefine("g:ShyLog", "/dev/null")
call ShyDefine("g:ShyLog", "shy.log")
fun! ShyLog(...)
call writefile([strftime("%Y-%m-%d %H:%M:%S ") . join(a:000, " ")], g:ShyLog, "a")
endfun
command -nargs=+ ShyLog call writefile([strftime("%Y-%m-%d %H:%M:%S ") . join([<f-args>], " ")], g:ShyLog, "a")
" 后端通信
call ShyDefine("g:ctx_sid", "")
call ShyDefine("g:ctx_url", (len($ctx_dev) > 1? $ctx_dev: "http://127.0.0.1:9020") . "/code/vim")
fun! ShySend(arg)
if has_key(a:arg, "sub") && a:arg["sub"] != ""
let temp = tempname()
@ -10,22 +22,33 @@ fun! ShySend(arg)
endif
let a:arg["buf"] = bufname("%")
let a:arg["pwd"] = getcwd()
let a:arg["sid"] = g:ctx_sid
let a:arg["pwd"] = getcwd()
let args = ""
for k in keys(a:arg)
for k in sort(keys(a:arg))
let args = args . " -F '" . k . "=" . a:arg[k] . "' "
endfor
return system("curl -s " . g:ctx_url . args . " 2>/dev/null")
endfun
fun! ShySends(...)
let args = {}
if len(a:000) > 0 | let args["cmd"] = a:000[0] | endif
if len(a:000) > 1 | let args["arg"] = a:000[1] | endif
if len(a:000) > 2 | let args["sub"] = a:000[2] | endif
return ShySend(args)
endfun
" 用户登录
fun! ShyLogout()
if g:ctx_sid == "" | return | endif
call ShySend({"cmd": "logout"})
call ShySends("logout")
endfun
fun! ShyLogin()
let g:ctx_sid = ShySend({"cmd": "login", "share": $ctx_share, "pid": getpid(), "pane": $TMUX_PANE, "hostname": hostname(), "username": $USER})
endfun
call ShyLogin()
" 收藏列表
fun! ShyFavor()
if !exists("g:favor_tab") | let g:favor_tab = "" | endif
if !exists("g:favor_note") | let g:favor_note = "" | endif
@ -46,6 +69,7 @@ fun! ShyFavors()
if note != "" | lexpr note | lopen | let note = "" | endif
endfun
" 数据同步
fun! ShySync(target)
if bufname("%") == "ControlP" | return | end
@ -77,9 +101,12 @@ fun! ShyCheck(target)
end
endfun
" 任务列表
fun! ShyTask()
call ShySend({"cmd": "tasklet", "arg": input("target: "), "sub": input("detail: ")})
endfun
" 标签列表
fun! ShyGrep(word)
if !exists("g:grep_dir") | let g:grep_dir = "./" | endif
let g:grep_dir = input("dir: ", g:grep_dir, "file")
@ -88,11 +115,42 @@ endfun
fun! ShyTag(word)
execute "tag " . a:word
endfun
" 输入转换
fun! ShyTrans(code)
return split(ShySend({"cmd": "trans", "arg": a:code, "pre": getline("."), "row": line("."), "col": col(".")}), "\n")
endfun
fun! ShyInput()
call ShyLog("input", v:char, line("."), col("."))
endfun
" 输入补全
fun! ShyComplete(firststart, base)
if a:firststart | let line = getline('.') | let start = col('.') - 1
" 命令位置
if match(trim(line), "ice ") == 0 | return match(line, "ice ") | endif
" 符号位置
if line[start-1] !~ "\a" | return start - 1 | end
" 单词位置
while start > 0 && line[start - 1] =~ '\a' | let start -= 1 | endwhile
return start
endif
" 符号转换
if a:base == "," | return ["", ","] | end
if a:base == "." | return ["。", "."] | end
" 单词转换
return ShyTrans(a:base)
endfun
set completefunc=ShyComplete
" 帮助信息
fun! ShyHelp()
echo ShySend({"cmd": "help"})
endfun
call ShyLogin()
" 事件回调
autocmd! VimLeave * call ShyLogout()
autocmd! BufReadPost * call ShySync("bufs")
autocmd! BufReadPost * call ShySync("read")
@ -100,9 +158,14 @@ autocmd! BufWritePre * call ShySync("write")
autocmd! CmdlineLeave * call ShySync("exec")
autocmd! QuickFixCmdPost * call ShyCheck("fixs")
autocmd! InsertLeave * call ShySync("insert")
autocmd! InsertCharPre * call ShyInput()
" 按键映射
nnoremap <C-G><C-G> :call ShyGrep(expand("<cword>"))<CR>
nnoremap <C-G><C-R> :call ShyCheck("cache")<CR>
nnoremap <C-G><C-F> :call ShyFavor()<CR>
nnoremap <C-G>f :call ShyFavors()<CR>
nnoremap <C-G><C-T> :call ShyTask()<CR>
inoremap <C-K> <C-X><C-U>
vnoremap <C-K> :call ShyTrans()

View File

@ -283,6 +283,28 @@ var Index = &ice.Context{Name: "code", Help: "编程中心",
case "tasklet":
m.Cmd(ice.APP_MISS, m.Option("arg"), m.Option("sub"))
case "trans":
if strings.HasPrefix(strings.TrimSpace(m.Option("arg")), "ice ") {
arg := kit.Split(strings.TrimPrefix(strings.TrimSpace(m.Option("arg")), "ice "))
switch arg[0] {
case "add":
m.Cmd("cli.input.input", "push", arg[1:])
m.Option("arg", arg[2])
default:
m.Set("append")
if m.Cmdy(arg).Table(); strings.TrimSpace(m.Result()) == "" {
m.Cmdy(ice.CLI_SYSTEM, arg)
}
m.Info("trans: --%s--", m.Result())
m.Push("_output", "result")
return
}
}
m.Cmd("cli.input.match", m.Option("arg")).Table(func(index int, value map[string]string, head []string) {
m.Echo("%s\n", value["text"])
m.Push("_output", "result")
})
case "favor":
if m.Options("arg") {
m.Cmd(ice.WEB_FAVOR, m.Option("tab"), "vimrc", m.Option("note"), m.Option("arg"),

View File

@ -18,12 +18,14 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
Configs: map[string]*ice.Config{
"input": {Name: "input", Help: "输入法", Value: kit.Data(
"store", "var/input/", "limit", "2000", "least", "1000",
kit.MDB_SHORT, "code",
)},
},
Commands: map[string]*ice.Command{
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd(ice.CTX_CONFIG, "load", "input.json")
}},
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmd(ice.CTX_CONFIG, "save", "input.json", "cli.input.input")
}},
@ -49,6 +51,10 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
))
}
}
case "push":
m.Rich("input", nil, kit.Dict(
"id", "0", "text", arg[1], "code", arg[2], "weight", kit.Select("99990000", arg, 3),
))
case "list":
// 词汇列表
m.Option("cache.offend", kit.Select("0", arg, 1))
@ -65,6 +71,11 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
return
}
m.Info("add %s: %s", arg[0], kit.Hashs(arg[0]))
m.Richs("input", nil, arg[0], func(key string, value map[string]interface{}) {
m.Push(key, value, []string{"id", "code", "text", "weight"})
})
// 搜索方法
method := "word"
if len(arg) > 1 {
@ -95,9 +106,10 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
// 搜索词汇
bio := csv.NewReader(bytes.NewBufferString(m.Cmdx(ice.CLI_SYSTEM, "grep", "-rh", arg[0], m.Conf("input", "meta.store"))))
for i := 0; i < 100; i++ {
for i := 0; i < kit.Int(kit.Select("100", arg, 2)); i++ {
if line, e := bio.Read(); e != nil {
break
} else if len(line) < 3 {
} else {
if method == "word" && i == 0 {
// 添加收藏
@ -112,6 +124,7 @@ var Index = &ice.Context{Name: "input", Help: "输入法",
}
}
}
m.Sort("weight", "int_r")
}},
},
}