diff --git a/lua/Trans/conf.lua b/lua/Trans/conf.lua new file mode 100644 index 0000000..fc0049f --- /dev/null +++ b/lua/Trans/conf.lua @@ -0,0 +1,39 @@ +return { + display = { + style = 'default', + phnoetic = true, + collins_star = true, + tag = true, + oxford = true, + history = false, + }, + default_keymap = true, + map = { + + }, + + view = { + -- TODO: style: buffer | cursor | window + -- style = 'buffer', + -- buffer_pos = 'bottom', -- only works when view.style == 'buffer' + }, + + db_path = '/home/zoran/project/neovim/ecdict-sqlite-28/stardict.db', -- FIXME: change the path + + -- TODO: async process + -- async = false, + + -- TODO: add online translate engine + -- online_search = { + -- enable = false, + -- engine = {}, + -- } + + -- TODO: precise match or return closest match result + -- precise_match = true, + + -- TODO: leamma search + -- leamma = false, + + -- TODO: register word +} diff --git a/lua/Trans/database.lua b/lua/Trans/database.lua new file mode 100644 index 0000000..295dbf9 --- /dev/null +++ b/lua/Trans/database.lua @@ -0,0 +1,22 @@ +local M = {} +-- local db = require('Trans').db +local dict = require("Trans").dict + +function M.query(arg) + -- TODO: return type: a result table: + local res = {} + if type(arg) == 'string' then + res = dict:select('stardict', { + where = { word = arg }, + }) + elseif type(arg) == 'table' then + res = dict:select('stardict', arg) + else + vim.notify('query argument error!') + end + + vim.pretty_print(res) + return res[1] +end + +return M diff --git a/lua/Trans/display.lua b/lua/Trans/display.lua new file mode 100644 index 0000000..448da83 --- /dev/null +++ b/lua/Trans/display.lua @@ -0,0 +1,28 @@ +-- local conf = require("Trans").conf +local M = {} +-- { +-- audio = "", +-- bnc = 5222, +-- definition = "n. slang for sexual intercourse", +-- exchange = "d:fucked/p:fucked/i:fucking/3:fucks/s:fucks", +-- frq = 5040, +-- id = 1180286, +-- phonetic = "fʌk", +-- pos = "n:37/v:63", +-- sw = "fuck", +-- tag = "", +-- translation = "vt. 与...性交, 欺骗, 诅咒\nvi. 性交\nn. 性交, 些微, 杂种\ninterj. 他妈的, 混帐", +-- word = "fuck" +-- } +-- + +function M.query_cursor() + -- TODO: +end + + +function M.query() + -- TODO: +end + +return M diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index ccde006..9efde8c 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -1,6 +1,5 @@ local M = {} - -- NOTE: -- Style: -- [minimal]: one line with '/' @@ -8,54 +7,16 @@ local M = {} -- [full]: show all description in different lines -- TODO: other style - -local default_conf = -{ - display = { - style = 'default', - phnoetic = true, - collins_star = true, - tag = true, - oxford = true, - -- TODO: frequency - -- frequency = false, - history = false, - }, - default_keymap = true, - map = { - - }, - - view = { - -- TODO: style: buffer | cursor | window - -- style = 'buffer', - -- buffer_pos = 'bottom', -- only works when view.style == 'buffer' - }, - - db_path = '/home/zoran/project/neovim/ecdict-sqlite-28/stardict.db', -- FIXME: change the path - - -- TODO: async process - async = false, - - -- TODO: add online translate engine - -- online_search = { - -- enable = false, - -- engine = {}, - -- } - - -- TODO: precise match or return closest match result - -- precise_match = true, - - -- TODO: leamma search - -- leamma = false, - - -- TODO: register word -} - -M.conf = default_conf - -function M:setup(conf) - self.config = vim.tbl_extend('force', default_conf, conf) +M.conf = require("Trans.conf") +function M.setup(conf) + M.conf = vim.tbl_extend('force', M.conf, conf) + require("Trans.setup") 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/query.lua b/lua/Trans/query.lua deleted file mode 100644 index 6aba8b4..0000000 --- a/lua/Trans/query.lua +++ /dev/null @@ -1,6 +0,0 @@ -local sqlite = require("sqlite") -local conf = require('Trans').conf - -local path = conf.db_path - -local db = sqlite.new(conf.db_path) diff --git a/lua/Trans/setup.lua b/lua/Trans/setup.lua new file mode 100644 index 0000000..cf570ec --- /dev/null +++ b/lua/Trans/setup.lua @@ -0,0 +1,22 @@ +local db = require("Trans").db +local conf = require("Trans").conf + +local group = vim.api.nvim_create_augroup('closedb', { clear = true }) +vim.api.nvim_create_autocmd('VimLeave', { + group = group, + pattern = '*', + callback = function() + if db:isopen() then + db:close() + end + end, +}) + +vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans").query_cursor, { + +}) + + +if conf.default_keymap then + --- TODO: keymap +end