diff --git a/configs/nvim/lua/gpt/init.lua b/configs/nvim/lua/gpt/init.lua index 9b92ca7..787dfa1 100644 --- a/configs/nvim/lua/gpt/init.lua +++ b/configs/nvim/lua/gpt/init.lua @@ -47,8 +47,6 @@ P.add_event_listener = function (opts) if P.hanging then vim.api.nvim_win_close(P.winid, true) P.winid = nil - else - vim.cmd('startinsert!') end end) end @@ -66,6 +64,61 @@ P.add_event_listener = function (opts) }) end +P.bind_keymaps = function (bid) + local call = function (fn) + return function () + vim.api.nvim_buf_call(bid, fn) + end + end + + local feed_normal = function (key) + return function () + local esc_key = vim.api.nvim_replace_termcodes(key, false, false, true) + vim.cmd.stopinsert({ bang = true }) + vim.api.nvim_feedkeys(esc_key, 'n', false) + end + end + + local buf_feed = function (key) + return call(feed_normal(key)) + end + + local keymap = { + t = { + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = call(function () + vim.cmd.stopinsert({ bang = true }) + end) + }, + n = { + [''] = buf_feed(''), + [''] = buf_feed(''), + [''] = buf_feed('zh'), + [''] = buf_feed('zl'), + [''] = call(function () + vim.cmd.startinsert({ bang = true }) + end) + } + } + + for mode, map in pairs(keymap) do + for lhs, rhs in pairs(map) do + vim.api.nvim_buf_set_keymap(bid, mode, lhs, '', { + noremap = true, + silent = true, + callback = rhs + }) + end + end +end + M.create = function (options) local opts = options or {} local cmd = { 'gpt' } @@ -90,9 +143,7 @@ M.create = function (options) end) P.add_event_listener(opts) - - -- TODO - -- Keymap to scroll up/down + P.bind_keymaps(P.bufid) P.winid = vim.api.nvim_open_win(P.bufid, true, P.layout(opts)) end