Skip to content

Commit

Permalink
Merge pull request #9 from desdic/dev
Browse files Browse the repository at this point in the history
feat: edit register before using them
  • Loading branch information
desdic authored Sep 3, 2023
2 parents 74f9079 + 1e81916 commit 72b82ff
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -24,6 +24,7 @@ Works with or without telescope.
{ "<Leader>kkq", function() require('macrothis').quickfix() end, desc = "run macro on all files in quickfix" },
{ "<Leader>kkr", function() require('macrothis').run() end, desc = "run macro" },
{ "<Leader>kks", function() require('macrothis').save() end, desc = "save" }
{ "<Leader>kkx", function() require('macrothis').register() end, desc = "edit register" }
}
},
```
Expand Down Expand Up @@ -61,6 +62,7 @@ require("telescope").extensions = {
| &lt;C-q&gt; | Run macro on files in quickfix list |
| &lt;C-r&gt; | Run macro |
| &lt;C-s&gt; | Save a macro/register |
| &lt;C-x&gt; | Edit register |

Shortcuts, sorters and more can be overridden via telescope options for this plugin.

Expand Down
16 changes: 16 additions & 0 deletions doc/macrothis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Using Lazy plugin manager
require('macrothis').run()
end,
desc = "run macro"
},
{
"<Leader>kkx",
function()
require('macrothis').register()
end,
desc = "edit register"
}
}
},
Expand Down Expand Up @@ -104,6 +111,14 @@ Rename macro
Usage~
`require('macrothis').rename()`

------------------------------------------------------------------------------
*macrothis.register()*
`macrothis.register`()
Modify register

Usage~
require('macrothis').register()

------------------------------------------------------------------------------
*default*
`default`
Expand Down Expand Up @@ -138,6 +153,7 @@ Default telescope options
rename = "<C-n>",
edit = "<C-e>",
quickfix = "<C-q>",
register = "<C-x>",
},
sorter = sorters.get_generic_fuzzy_sorter,
items_display = {
Expand Down
37 changes: 36 additions & 1 deletion lua/macrothis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
--- require('macrothis').run()
--- end,
--- desc = "run macro"
--- },
--- {
--- "<Leader>kkx",
--- function()
--- require('macrothis').register()
--- end,
--- desc = "edit register"
--- }
--- }
--- },
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("<ESC>", true, false, true),
"n",
false
)
end
end)
end

local generate_register_list = function()
local registers_table = { '"', "-", "#", "=", "/", "*", "+", ":", ".", "%" }

Expand Down
40 changes: 40 additions & 0 deletions lua/macrothis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
51 changes: 51 additions & 0 deletions lua/telescope/_extensions/macrothis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local default_telescope = {
rename = "<C-n>",
edit = "<C-e>",
quickfix = "<C-q>",
register = "<C-x>",
},
sorter = sorters.get_generic_fuzzy_sorter,
items_display = {
Expand Down Expand Up @@ -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(
"<ESC>",
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, {
Expand All @@ -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,
})
Expand Down

0 comments on commit 72b82ff

Please sign in to comment.