Skip to content

Commit 72b82ff

Browse files
authored
Merge pull request #9 from desdic/dev
feat: edit register before using them
2 parents 74f9079 + 1e81916 commit 72b82ff

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Macrothis.nvim
22

3-
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.
3+
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.
44

55
Works with or without telescope.
66

@@ -24,6 +24,7 @@ Works with or without telescope.
2424
{ "<Leader>kkq", function() require('macrothis').quickfix() end, desc = "run macro on all files in quickfix" },
2525
{ "<Leader>kkr", function() require('macrothis').run() end, desc = "run macro" },
2626
{ "<Leader>kks", function() require('macrothis').save() end, desc = "save" }
27+
{ "<Leader>kkx", function() require('macrothis').register() end, desc = "edit register" }
2728
}
2829
},
2930
```
@@ -61,6 +62,7 @@ require("telescope").extensions = {
6162
| &lt;C-q&gt; | Run macro on files in quickfix list |
6263
| &lt;C-r&gt; | Run macro |
6364
| &lt;C-s&gt; | Save a macro/register |
65+
| &lt;C-x&gt; | Edit register |
6466

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

doc/macrothis.txt

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Using Lazy plugin manager
4343
require('macrothis').run()
4444
end,
4545
desc = "run macro"
46+
},
47+
{
48+
"<Leader>kkx",
49+
function()
50+
require('macrothis').register()
51+
end,
52+
desc = "edit register"
4653
}
4754
}
4855
},
@@ -104,6 +111,14 @@ Rename macro
104111
Usage~
105112
`require('macrothis').rename()`
106113

114+
------------------------------------------------------------------------------
115+
*macrothis.register()*
116+
`macrothis.register`()
117+
Modify register
118+
119+
Usage~
120+
require('macrothis').register()
121+
107122
------------------------------------------------------------------------------
108123
*default*
109124
`default`
@@ -138,6 +153,7 @@ Default telescope options
138153
rename = "<C-n>",
139154
edit = "<C-e>",
140155
quickfix = "<C-q>",
156+
register = "<C-x>",
141157
},
142158
sorter = sorters.get_generic_fuzzy_sorter,
143159
items_display = {

lua/macrothis/init.lua

+36-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
--- require('macrothis').run()
4040
--- end,
4141
--- desc = "run macro"
42+
--- },
43+
--- {
44+
--- "<Leader>kkx",
45+
--- function()
46+
--- require('macrothis').register()
47+
--- end,
48+
--- desc = "edit register"
4249
--- }
4350
--- }
4451
--- },
@@ -232,7 +239,8 @@ macrothis.edit = function()
232239
end,
233240
}, function(description, _)
234241
if description then
235-
local bufnr = utils.create_edit_window(macrothis.opts, description.label)
242+
local bufnr =
243+
utils.create_edit_window(macrothis.opts, description.label)
236244

237245
local winopts = utils.get_winopts(macrothis.opts)
238246
vim.api.nvim_open_win(bufnr, true, winopts)
@@ -270,6 +278,33 @@ macrothis.rename = function()
270278
end)
271279
end
272280

281+
--- Modify register
282+
---
283+
---@usage require('macrothis').register()
284+
macrothis.register = function()
285+
local registers = macrothis.generate_register_items()
286+
vim.ui.select(registers, {
287+
prompt = "Edit register",
288+
format_item = function(item)
289+
return ("%s: %s: %s"):format(item.label, item.value, item.type)
290+
end,
291+
}, function(register, _)
292+
if register then
293+
local bufnr = utils.create_edit_register(register.label)
294+
295+
local winopts = utils.get_winopts(macrothis.opts)
296+
vim.api.nvim_open_win(bufnr, true, winopts)
297+
vim.api.nvim_win_set_buf(0, bufnr)
298+
299+
vim.api.nvim_feedkeys(
300+
vim.api.nvim_replace_termcodes("<ESC>", true, false, true),
301+
"n",
302+
false
303+
)
304+
end
305+
end)
306+
end
307+
273308
local generate_register_list = function()
274309
local registers_table = { '"', "-", "#", "=", "/", "*", "+", ":", ".", "%" }
275310

lua/macrothis/utils.lua

+40
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,46 @@ utils.get_winopts = function(opts)
9898
return winopts
9999
end
100100

101+
utils.create_edit_register = function(register)
102+
local bufnr = vim.api.nvim_create_buf(false, true)
103+
104+
local entrycontent = vim.fn.getreg(register)
105+
local entrytype = vim.fn.getregtype(register)
106+
107+
entrycontent = type(entrycontent) == "string"
108+
and entrycontent:gsub("\n", "\\n")
109+
or entrycontent
110+
111+
vim.api.nvim_buf_set_lines(bufnr, 0, 0, true, { entrycontent })
112+
113+
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
114+
vim.api.nvim_buf_set_option(bufnr, "buftype", "nofile")
115+
vim.api.nvim_buf_set_name(bufnr, "editing " .. register)
116+
117+
vim.api.nvim_create_autocmd({ "BufWinLeave" }, {
118+
callback = function(bufopts)
119+
local bufcontent =
120+
vim.api.nvim_buf_get_lines(bufopts.buf, 0, -1, true)
121+
122+
bufcontent = table.concat(bufcontent, "")
123+
124+
-- Re-add newlines
125+
local newcontent = bufcontent:gsub("\\n", "\n")
126+
127+
vim.fn.setreg(register, newcontent, entrytype)
128+
129+
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
130+
vim.api.nvim_win_close(0, true)
131+
vim.schedule(function()
132+
vim.cmd("bdelete! " .. bufnr)
133+
end)
134+
end,
135+
buffer = bufnr,
136+
})
137+
138+
return bufnr
139+
end
140+
101141
utils.create_edit_window = function(opts, description)
102142
local data = utils.read_data(opts)
103143

lua/telescope/_extensions/macrothis.lua

+51
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ local default_telescope = {
2323
rename = "<C-n>",
2424
edit = "<C-e>",
2525
quickfix = "<C-q>",
26+
register = "<C-x>",
2627
},
2728
sorter = sorters.get_generic_fuzzy_sorter,
2829
items_display = {
@@ -259,6 +260,51 @@ local edit_macro = function(prompt_bufnr)
259260
vim.api.nvim_win_set_buf(0, bufnr)
260261
end
261262

263+
local edit_register = function(_)
264+
local opts = macrothis.telescope_config.opts
265+
opts["ngram_len"] = 1 -- make sure we sort based on register name which is only one char
266+
267+
pickers
268+
.new({}, {
269+
prompt_title = "Edit register",
270+
finder = generate_new_finder_registers(),
271+
sorter = macrothis.telescope_config.sorter(opts),
272+
attach_mappings = function(_, map)
273+
map(
274+
"i",
275+
macrothis.telescope_config.mappings.load,
276+
function(prompt_bufnr)
277+
local selected_register =
278+
action_state.get_selected_entry()
279+
280+
local bufnr = utils.create_edit_register(
281+
selected_register.value.label
282+
)
283+
284+
actions.close(prompt_bufnr)
285+
286+
local winopts = utils.get_winopts(macrothis.opts)
287+
vim.api.nvim_open_win(bufnr, true, winopts)
288+
vim.api.nvim_win_set_buf(0, bufnr)
289+
290+
vim.api.nvim_feedkeys(
291+
vim.api.nvim_replace_termcodes(
292+
"<ESC>",
293+
true,
294+
false,
295+
true
296+
),
297+
"n",
298+
false
299+
)
300+
end
301+
)
302+
return true
303+
end,
304+
})
305+
:find()
306+
end
307+
262308
local run = function(opts)
263309
macrothis.telescope_config.opts = opts
264310
local picker = pickers.new(opts, {
@@ -277,6 +323,11 @@ local run = function(opts)
277323
)
278324
map("i", macrothis.telescope_config.mappings.rename, rename_macro)
279325
map("i", macrothis.telescope_config.mappings.edit, edit_macro)
326+
map(
327+
"i",
328+
macrothis.telescope_config.mappings.register,
329+
edit_register
330+
)
280331
return true
281332
end,
282333
})

0 commit comments

Comments
 (0)