diff --git a/lua/Trans/conf.lua b/lua/Trans/conf.lua index 8d4238f..12ca060 100644 --- a/lua/Trans/conf.lua +++ b/lua/Trans/conf.lua @@ -12,7 +12,7 @@ return { exchange = true, Trans_en = true, Trans_zh = true, - wrap = false, + wrap = true, }, view = { -- TODO: style: buffer | cursor | window @@ -25,7 +25,8 @@ return { star = '⭐', isOxford = '✔', notOxford = '' - } + }, + auto_close = true, -- TODO: async process -- async = false, diff --git a/lua/Trans/database.lua b/lua/Trans/database.lua index 6135418..092e451 100644 --- a/lua/Trans/database.lua +++ b/lua/Trans/database.lua @@ -1,5 +1,4 @@ local M = {} --- local db = require('Trans').db local dict = require("Trans").dict function M.query(arg) diff --git a/lua/Trans/display.lua b/lua/Trans/display.lua index 5a65d5d..a880fc1 100644 --- a/lua/Trans/display.lua +++ b/lua/Trans/display.lua @@ -1,12 +1,13 @@ -- local conf = require("Trans").conf local M = {} -local display = require("Trans.conf").display -local icon = require("Trans.conf").icon +local display = require("Trans").conf.display +local icon = require("Trans").conf.icon local buf = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_option(buf, 'filetype', 'Trans') +local win = 0 -- { @@ -25,7 +26,7 @@ vim.api.nvim_buf_set_option(buf, 'filetype', 'Trans') -- } local function show_win(width, height) - local win = vim.api.nvim_open_win(buf, false, { + win = vim.api.nvim_open_win(buf, false, { relative = 'cursor', title = 'Trans', title_pos = 'center', @@ -141,4 +142,11 @@ function M.toggle() -- TODO: wrap some function end +function M.close_win() + if win > 0 then + vim.api.nvim_win_close(win, true) + win = 0 + end +end + return M diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index 9efde8c..2becf6d 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -15,8 +15,5 @@ end M.db = require("sqlite.db") M.dict = M.db:open(M.conf.db_path) -M.query = require("Trans.display").query -M.query_cursor = require("Trans.display").query_cursor - return M diff --git a/lua/Trans/setup.lua b/lua/Trans/setup.lua index 1b65e67..2bfdabd 100644 --- a/lua/Trans/setup.lua +++ b/lua/Trans/setup.lua @@ -1,7 +1,11 @@ local db = require("Trans").db -- local conf = require("Trans").conf -local group = vim.api.nvim_create_augroup('closedb', { clear = true }) + +vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans.display").query_cursor, {}) + + +local group = vim.api.nvim_create_augroup("Trans", { clear = true }) vim.api.nvim_create_autocmd('VimLeave', { group = group, pattern = '*', @@ -11,8 +15,13 @@ vim.api.nvim_create_autocmd('VimLeave', { end end, }) - -vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans").query_cursor, {}) - -- TODO: set command to close preview window automatically - +local auto_close = require("Trans.conf").auto_close +if auto_close then + vim.api.nvim_create_autocmd( + { 'InsertEnter', 'CursorMoved', 'BufLeave', }, { + group = group, + pattern = '*', + callback = require('Trans.display').close_win + }) +end