diff --git a/README.md b/README.md index e8c6e55..ccbb244 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ use { - 安装后如果不能正常运行, 请尝试检查一下问题: - 本机是否已经安装了 `sqlite3` > Linux下安装: - > `sudo pacman -S sqlite # Arch` + > `sudo pacman -S sqlite # Arch` > `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu` > 后续会增加 `healthcheck` 进行检查 @@ -207,9 +207,53 @@ vim.keymap.set('n', 'mi', 'TranslateInput') ``` ## 高亮组 +> 默认定义 ```lua --- TODO : add explanation - +{ + TransWord = { + fg = '#7ee787', + bold = true, + }, + TransPhonetic = { + link = 'Linenr' + }, + TransTitle = { + fg = '#0f0f15', + bg = '#75beff', + bold = true, + }, + TransTitleRound = { + fg = '#75beff', + }, + TransTag = { + fg = '#e5c07b', + }, + TransExchange = { + link = 'TransTag', + }, + TransPos = { + link = 'TransTag', + }, + TransTranslation = { + link = 'TransWord', + }, + TransDefinition = { + link = 'Moremsg', + }, + TransWin = { + link = 'Normal', + }, + TransBorder = { + link = 'FloatBorder', + }, + TransCollins = { + fg = '#faf743', + bold = true, + }, + TransFailed = { + fg = '#7aa89f', + }, +} ``` ## 声明 - 本插件词典基于[ECDICT](https://github.com/skywind3000/ECDICT) diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index f394113..8570d4d 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -19,8 +19,9 @@ M.conf = { -- TODO : pageup = '[[', pagedown = ']]', - pin = '_', - close = '+', + pin = '+', + close = '_', + toggle_entry = '--', }, animation = { -- open = 'fold', @@ -28,7 +29,12 @@ M.conf = { open = 'slid', close = 'slid', interval = 12, - } + }, + auto_close_events = { + 'InsertEnter', + 'CursorMoved', + 'BufLeave', + }, }, float = { width = 0.8, diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua index 02e9f89..60cf74b 100644 --- a/lua/Trans/view/hover.lua +++ b/lua/Trans/view/hover.lua @@ -184,8 +184,8 @@ local try_del_keymap = function() end end - local action +local next action = { pageup = function() m_window:normal('gg') @@ -199,11 +199,8 @@ action = { if pin then error('too many window') end - if cmd_id > 0 then - api.nvim_del_autocmd(cmd_id) - cmd_id = -1 - end + pcall(api.nvim_del_autocmd, cmd_id) m_window:set('wrap', false) m_window:try_close(function() @@ -219,18 +216,20 @@ action = { vim.keymap.del('n', conf.hover.keymap.pin, { buffer = true }) - --- NOTE : 只允许存在一个pin窗口 local buf = m_window.bufnr pin = true - api.nvim_create_autocmd({ 'BufWipeOut', 'BufLeave' }, { + local toggle = conf.hover.keymap.toggle_entry + if toggle then + next = m_window.winid + vim.keymap.set('n', toggle, action.toggle_entry, { silent = true, buffer = buf }) + end + + api.nvim_create_autocmd('BufWipeOut', { callback = function(opt) - if opt.event == 'BufLeave' or opt.buf == buf then + if opt.buf == buf then pin = false api.nvim_del_autocmd(opt.id) - if opt.event == 'BufLeave' then - action.close() - end end end }) @@ -238,15 +237,21 @@ action = { end, close = function() - if cmd_id > 0 then - api.nvim_del_autocmd(cmd_id) - cmd_id = -1 - end - + pcall(api.nvim_del_autocmd, cmd_id) m_window:set('wrap', false) m_window:try_close() try_del_keymap() end, + + toggle_entry = function() + if pin and m_window:is_open() then + local prev = api.nvim_get_current_win() + api.nvim_set_current_win(next) + next = prev + else + vim.keymap.del('n', conf.hover.keymap.toggle_entry, { buffer = true }) + end + end } @@ -285,9 +290,11 @@ return function(word) m_window:set('wrap', true) end) + + -- Auto Close cmd_id = api.nvim_create_autocmd( - { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { + hover.auto_close_events, { buffer = 0, callback = function() m_window:set('wrap', false)