diff --git a/README.md b/README.md index 82e4018..e688c86 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Macrothis.nvim -Macrothis.nvim was created since I had a basic need for storing and loading macros. A side effect is that it works on all registers. +Macrothis.nvim was created since I had a basic need for storing and loading macros. A side effect is that it works on all registers. It does most operations on register. Works with or without telescope. @@ -24,6 +24,7 @@ Works with or without telescope. { "kkq", function() require('macrothis').quickfix() end, desc = "run macro on all files in quickfix" }, { "kkr", function() require('macrothis').run() end, desc = "run macro" }, { "kks", function() require('macrothis').save() end, desc = "save" } + { "kkx", function() require('macrothis').register() end, desc = "edit register" } } }, ``` @@ -61,6 +62,7 @@ require("telescope").extensions = { | <C-q> | Run macro on files in quickfix list | | <C-r> | Run macro | | <C-s> | Save a macro/register | +| <C-x> | Edit register | Shortcuts, sorters and more can be overridden via telescope options for this plugin. diff --git a/doc/macrothis.txt b/doc/macrothis.txt index c6d20c2..5740972 100644 --- a/doc/macrothis.txt +++ b/doc/macrothis.txt @@ -43,6 +43,13 @@ Using Lazy plugin manager require('macrothis').run() end, desc = "run macro" + }, + { + "kkx", + function() + require('macrothis').register() + end, + desc = "edit register" } } }, @@ -104,6 +111,14 @@ Rename macro Usage~ `require('macrothis').rename()` +------------------------------------------------------------------------------ + *macrothis.register()* + `macrothis.register`() +Modify register + +Usage~ +require('macrothis').register() + ------------------------------------------------------------------------------ *default* `default` @@ -138,6 +153,7 @@ Default telescope options rename = "", edit = "", quickfix = "", + register = "", }, sorter = sorters.get_generic_fuzzy_sorter, items_display = { diff --git a/lua/macrothis/init.lua b/lua/macrothis/init.lua index ed79494..01cba4e 100644 --- a/lua/macrothis/init.lua +++ b/lua/macrothis/init.lua @@ -39,6 +39,13 @@ --- require('macrothis').run() --- end, --- desc = "run macro" +--- }, +--- { +--- "kkx", +--- function() +--- require('macrothis').register() +--- end, +--- desc = "edit register" --- } --- } --- }, @@ -232,7 +239,8 @@ macrothis.edit = function() end, }, function(description, _) if description then - local bufnr = utils.create_edit_window(macrothis.opts, description.label) + local bufnr = + utils.create_edit_window(macrothis.opts, description.label) local winopts = utils.get_winopts(macrothis.opts) vim.api.nvim_open_win(bufnr, true, winopts) @@ -270,6 +278,33 @@ macrothis.rename = function() end) end +--- Modify register +--- +---@usage require('macrothis').register() +macrothis.register = function() + local registers = macrothis.generate_register_items() + vim.ui.select(registers, { + prompt = "Edit register", + format_item = function(item) + return ("%s: %s: %s"):format(item.label, item.value, item.type) + end, + }, function(register, _) + if register then + local bufnr = utils.create_edit_register(register.label) + + local winopts = utils.get_winopts(macrothis.opts) + vim.api.nvim_open_win(bufnr, true, winopts) + vim.api.nvim_win_set_buf(0, bufnr) + + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, false, true), + "n", + false + ) + end + end) +end + local generate_register_list = function() local registers_table = { '"', "-", "#", "=", "/", "*", "+", ":", ".", "%" } diff --git a/lua/macrothis/utils.lua b/lua/macrothis/utils.lua index 35f5491..2a6a94a 100644 --- a/lua/macrothis/utils.lua +++ b/lua/macrothis/utils.lua @@ -98,6 +98,46 @@ utils.get_winopts = function(opts) return winopts end +utils.create_edit_register = function(register) + local bufnr = vim.api.nvim_create_buf(false, true) + + local entrycontent = vim.fn.getreg(register) + local entrytype = vim.fn.getregtype(register) + + entrycontent = type(entrycontent) == "string" + and entrycontent:gsub("\n", "\\n") + or entrycontent + + vim.api.nvim_buf_set_lines(bufnr, 0, 0, true, { entrycontent }) + + vim.api.nvim_buf_set_option(bufnr, "modifiable", true) + vim.api.nvim_buf_set_option(bufnr, "buftype", "nofile") + vim.api.nvim_buf_set_name(bufnr, "editing " .. register) + + vim.api.nvim_create_autocmd({ "BufWinLeave" }, { + callback = function(bufopts) + local bufcontent = + vim.api.nvim_buf_get_lines(bufopts.buf, 0, -1, true) + + bufcontent = table.concat(bufcontent, "") + + -- Re-add newlines + local newcontent = bufcontent:gsub("\\n", "\n") + + vim.fn.setreg(register, newcontent, entrytype) + + vim.api.nvim_buf_set_option(bufnr, "modifiable", false) + vim.api.nvim_win_close(0, true) + vim.schedule(function() + vim.cmd("bdelete! " .. bufnr) + end) + end, + buffer = bufnr, + }) + + return bufnr +end + utils.create_edit_window = function(opts, description) local data = utils.read_data(opts) diff --git a/lua/telescope/_extensions/macrothis.lua b/lua/telescope/_extensions/macrothis.lua index dc5b5c7..68f2f98 100644 --- a/lua/telescope/_extensions/macrothis.lua +++ b/lua/telescope/_extensions/macrothis.lua @@ -23,6 +23,7 @@ local default_telescope = { rename = "", edit = "", quickfix = "", + register = "", }, sorter = sorters.get_generic_fuzzy_sorter, items_display = { @@ -259,6 +260,51 @@ local edit_macro = function(prompt_bufnr) vim.api.nvim_win_set_buf(0, bufnr) end +local edit_register = function(_) + local opts = macrothis.telescope_config.opts + opts["ngram_len"] = 1 -- make sure we sort based on register name which is only one char + + pickers + .new({}, { + prompt_title = "Edit register", + finder = generate_new_finder_registers(), + sorter = macrothis.telescope_config.sorter(opts), + attach_mappings = function(_, map) + map( + "i", + macrothis.telescope_config.mappings.load, + function(prompt_bufnr) + local selected_register = + action_state.get_selected_entry() + + local bufnr = utils.create_edit_register( + selected_register.value.label + ) + + actions.close(prompt_bufnr) + + local winopts = utils.get_winopts(macrothis.opts) + vim.api.nvim_open_win(bufnr, true, winopts) + vim.api.nvim_win_set_buf(0, bufnr) + + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes( + "", + true, + false, + true + ), + "n", + false + ) + end + ) + return true + end, + }) + :find() +end + local run = function(opts) macrothis.telescope_config.opts = opts local picker = pickers.new(opts, { @@ -277,6 +323,11 @@ local run = function(opts) ) map("i", macrothis.telescope_config.mappings.rename, rename_macro) map("i", macrothis.telescope_config.mappings.edit, edit_macro) + map( + "i", + macrothis.telescope_config.mappings.register, + edit_register + ) return true end, })