diff --git a/lua/telescope/_extensions/telescope_knot.lua b/lua/telescope/_extensions/telescope_knot.lua index 06565cc..8164187 100644 --- a/lua/telescope/_extensions/telescope_knot.lua +++ b/lua/telescope/_extensions/telescope_knot.lua @@ -66,47 +66,51 @@ telescope_knot.zones = function(opts) local record = _filter_records(entry, records) actions.close(prompt_bufnr) local buffer = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_name(buffer, entry.value) vim.api.nvim_buf_set_lines(buffer, 0, 0, false, record) local width = vim.api.nvim_win_get_width(0) local height = vim.api.nvim_win_get_height(0) - 10 - local _ = vim.api.nvim_open_win(buffer, true, + local win = vim.api.nvim_open_win(buffer, true, { relative = 'win', bufpos = { 0, 0 }, width = width, height = height, border = 'rounded' }) - vim.api.nvim_set_keymap("n", "q", "bd", {}) - vim.api.nvim_buf_set_keymap(buffer, "c", "w", "", {}) - vim.api.nvim_create_autocmd({ "BufWinLeave" }, { + vim.api.nvim_buf_set_keymap(buffer, "c", "w", + "lua vim.api.nvim_exec_autocmds('User', { pattern = 'KnotWrite' })", {}) + vim.api.nvim_create_autocmd({ "User" }, { buffer = buffer, - callback = function(_) - local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) - if new ~= record then - local cleanold = {} - local changed = {} - for _, v in ipairs(record) do - if v ~= nil and v ~= "" then - local key = v:match("%w+"):lower() - cleanold[key] = v:gsub("%w+%:%s", "") + callback = function(event) + if event.match == "KnotWrite" then + local new = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) + if new ~= record then + local cleanold = {} + local updated = {} + local did_update = false + for _, v in ipairs(record) do + if v ~= nil and v ~= "" then + local key = v:match("%w+"):lower() + cleanold[key] = v:gsub("%w+%:%s", "") + end end - end - local cleannew = {} - for _, v in ipairs(new) do - if v ~= nil and v ~= "" then - local key = v:match("%w+"):lower() - cleannew[key] = v:gsub("%w+%:%s", "") - if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then - table.insert(changed, " -a " .. key .. "=" .. cleannew[key]) + local cleannew = {} + for _, v in ipairs(new) do + if v ~= nil and v ~= "" then + local key = v:match("%w+"):lower() + cleannew[key] = v:gsub("%w+%:%s", "") + if cleanold[key] ~= cleannew[key] and (key ~= "url" or key ~= "zone") then + table.insert(updated, " -a " .. key .. "=" .. cleannew[key]) + did_update = true + end end end - end - if changed ~= {} then - local command = "knotctl update -z" .. cleanold.zone .. - " -t" .. cleanold.ttl .. - " -n" .. cleanold.name .. - " -r" .. cleanold.rtype .. - " -d" .. cleanold.data .. - table.concat(changed, " ") - local choice = vim.fn.confirm("Update record?", "&Yes\n&No", 2) - if choice == 1 then - log.info(vim.fn.system(command)) + if did_update then + local command = "knotctl update -z" .. cleanold.zone .. + " -t" .. cleanold.ttl .. + " -n" .. cleanold.name .. + " -r" .. cleanold.rtype .. + " -d" .. cleanold.data .. + table.concat(updated, " ") + local choice = vim.fn.confirm("Update record and close buffer?", "&Yes\n&No", 2) + if choice == 1 then + log.info(vim.fn.system(command)) + vim.api.nvim_buf_delete(buffer, {}) + end end end end