Skip to content

Commit

Permalink
Merge pull request #8 from desdic/dev
Browse files Browse the repository at this point in the history
Edit and rename added
  • Loading branch information
desdic authored Sep 3, 2023
2 parents 216f9b2 + dec66e3 commit 5f1f1ec
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 33 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ Works with or without telescope.
"desdic/macrothis.nvim",
opts = {},
keys = {
{ "<Leader>kks", function() require('macrothis').save() end, desc = "save register" },
{ "<Leader>kkl", function() require('macrothis').load() end, desc = "load register" }
{ "<Leader>kkd", function() require('macrothis').delete() end, desc = "delete register" }
{ "<Leader>kkr", function() require('macrothis').run() end, desc = "run macro" }
{ "<Leader>kkq", function() require('macrothis').quickfix() end, desc = "run macro on all files in quickfix" }
{ "<Leader>kkd", function() require('macrothis').delete() end, desc = "delete" },
{ "<Leader>kke", function() require('macrothis').edit() end, desc = "edit" },
{ "<Leader>kkl", function() require('macrothis').load() end, desc = "load" },
{ "<Leader>kkn", function() require('macrothis').rename() end, desc = "rename" },
{ "<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" }
}
},
```
Expand Down Expand Up @@ -54,9 +56,11 @@ require("telescope").extensions = {
| :--- | :--- |
| &lt;CR&gt; | Load selected entry into register |
| &lt;C-d&gt; | Delete selected entry or delete all marked entries |
| &lt;C-s&gt; | Save a macro/register |
| &lt;C-r&gt; | Run macro |
| &lt;C-e&gt; | Edit content of macro |
| &lt;C-n&gt; | Rename selected entry |
| &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 |

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

Expand Down
26 changes: 25 additions & 1 deletion doc/macrothis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ Run macro on all in quickfix list
Usage~
`require('macrothis').quickfix()`

------------------------------------------------------------------------------
*macrothis.edit()*
`macrothis.edit`()
Edit macro

Usage~
`require('macrothis').edit()`

------------------------------------------------------------------------------
*macrothis.rename()*
`macrothis.rename`()
Rename macro

Usage~
`require('macrothis').rename()`

------------------------------------------------------------------------------
*default*
`default`
Expand All @@ -96,7 +112,13 @@ Default options
local default = {
datafile = vim.fn.stdpath("data") .. "/macrothis.json",
registers = generate_register_list(),
run_register = "z", -- content of register z is replaced when running a macro
run_register = "z", -- content of register z is replaced when running/editing a macro
editor = { -- Edit window
width = 100,
height = 2,
style = "minimal",
border = "rounded",
},
}
<

Expand All @@ -113,6 +135,8 @@ Default telescope options
save = "<C-s>",
delete = "<C-d>",
run = "<C-r>",
rename = "<C-n>",
edit = "<C-e>",
quickfix = "<C-q>",
},
sorter = sorters.get_generic_fuzzy_sorter,
Expand Down
59 changes: 58 additions & 1 deletion lua/macrothis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,57 @@ macrothis.quickfix = function()
end)
end

--- Edit macro
---
---@usage `require('macrothis').edit()`
macrothis.edit = function()
local menuelem = macrothis.generate_menu_items()

vim.ui.select(menuelem, {
prompt = "Edit",
format_item = function(item)
return ("%s: %s"):format(item.label, item.value)
end,
}, function(description, _)
if description then
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)
vim.api.nvim_win_set_buf(0, bufnr)
end
end)
end

--- Rename macro
---
---@usage `require('macrothis').rename()`
macrothis.rename = function()
local menuelem = macrothis.generate_menu_items()

vim.ui.select(menuelem, {
prompt = "Rename",
format_item = function(item)
return ("%s: %s"):format(item.label, item.value)
end,
}, function(description, _)
if description then
vim.ui.input({
prompt = "New description: ",
default = description.label,
}, function(newdescription)
if newdescription then
utils.rename_macro(
macrothis.opts,
description.label,
newdescription
)
end
end)
end
end)
end

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

Expand All @@ -240,7 +291,13 @@ end
local default = {
datafile = vim.fn.stdpath("data") .. "/macrothis.json",
registers = generate_register_list(),
run_register = "z", -- content of register z is replaced when running a macro
run_register = "z", -- content of register z is replaced when running/editing a macro
editor = { -- Edit window
width = 100,
height = 2,
style = "minimal",
border = "rounded",
},
}
--minidoc_afterlines_end

Expand Down
76 changes: 76 additions & 0 deletions lua/macrothis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ utils.store_register = function(opts, register, description)
utils.save_data(opts, data)
end

utils.rename_macro = function(opts, olddescription, newdescription)
local data = utils.read_data(opts)

data[newdescription] = {}
data[newdescription]["value"] = data[olddescription]["value"]
data[newdescription]["type"] = data[olddescription]["type"]
data[olddescription] = nil

utils.save_data(opts, data)
end

utils.load_register = function(opts, register, description)
local data = utils.read_data(opts)
local content = data[description]
Expand Down Expand Up @@ -68,4 +79,69 @@ utils.run_macro_on_quickfixlist = function(opts, register, description)
)
end

utils.get_winopts = function(opts)
local width = opts.editor.width
local height = opts.editor.height

local ui = vim.api.nvim_list_uis()[1]
local winopts = {
relative = "editor",
width = width,
height = height,
col = (ui.width - width) / 2,
row = (ui.height - height) / 2,
style = opts.editor.style,
border = opts.editor.border,
focusable = true,
}

return winopts
end

utils.create_edit_window = function(opts, description)
local data = utils.read_data(opts)
print(vim.inspect(description))

local entrylabel = description
local entrycontent = base64.dec(data[entrylabel]["value"])
local entrytype = data[entrylabel]["type"]

local bufnr = vim.api.nvim_create_buf(false, true)

-- Replace newlines if found (telescope does not do multi line)
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, entrylabel)

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(opts.run_register, newcontent, entrytype)
utils.store_register(opts, opts.run_register, entrylabel)

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

return utils
93 changes: 69 additions & 24 deletions lua/telescope/_extensions/macrothis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ local default_telescope = {
save = "<C-s>",
delete = "<C-d>",
run = "<C-r>",
rename = "<C-n>",
edit = "<C-e>",
quickfix = "<C-q>",
},
sorter = sorters.get_generic_fuzzy_sorter,
Expand Down Expand Up @@ -146,19 +148,24 @@ local load_macro = function(_)
end

local delete_macro = function(prompt_bufnr)
local confirmation = vim.fn.input("Confirm deletion? [y/n]: ")
if
string.len(confirmation) == 0
or string.sub(string.lower(confirmation), 0, 1) ~= "y"
then
print("cancelled")
return
end
-- use input while we wait for vim.ui.confirm
vim.ui.input(
{ prompt = "Confirm deletion? [y/n]: " },
function(confirmation)
if
string.len(confirmation) == 0
or string.sub(string.lower(confirmation), 0, 1) ~= "y"
then
print("cancelled")
return
end

local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:delete_selection(function(selection)
utils.remove_entry(macrothis.opts, selection.value.label)
end)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:delete_selection(function(selection)
utils.remove_entry(macrothis.opts, selection.value.label)
end)
end
)
end

local save_macro = function(_)
Expand All @@ -176,14 +183,18 @@ local save_macro = function(_)
function(newprompt_bufnr)
local selected_register =
action_state.get_selected_entry()
local description =
vim.fn.input("Enter description: ", "")
utils.store_register(
macrothis.opts,
selected_register.value.label,
description
)
actions.close(newprompt_bufnr)
vim.ui.input({
prompt = "Enter description: ",
}, function(description)
if description then
utils.store_register(
macrothis.opts,
selected_register.value.label,
description
)
actions.close(newprompt_bufnr)
end
end)
end
)
return true
Expand All @@ -193,29 +204,61 @@ local save_macro = function(_)
end

local run_macro = function(prompt_bufnr)
local selected_register = action_state.get_selected_entry()
local selected_macro = action_state.get_selected_entry()

actions.close(prompt_bufnr)

utils.run_macro(
macrothis.opts,
macrothis.opts.run_register,
selected_register.value.label
selected_macro.value.label
)
end

local run_macro_on_quickfixlist = function(prompt_bufnr)
local selected_register = action_state.get_selected_entry()
local selected_macro = action_state.get_selected_entry()

actions.close(prompt_bufnr)

utils.run_macro_on_quickfixlist(
macrothis.opts,
macrothis.opts.run_register,
selected_register.value.label
selected_macro.value.label
)
end

local rename_macro = function(prompt_bufnr)
local selected_register = action_state.get_selected_entry()

-- TODO would be nice if we didn't close the menu and just updated telescope dynamically
vim.ui.input({
prompt = "New description: ",
default = selected_register.value.label,
}, function(newdescription)
if newdescription then
utils.rename_macro(
macrothis.opts,
selected_register.value.label,
newdescription
)
actions.close(prompt_bufnr)
end
end)
end

local edit_macro = function(prompt_bufnr)
local selected_item = action_state.get_selected_entry()

local bufnr =
utils.create_edit_window(macrothis.opts, selected_item.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)
end

local run = function(opts)
macrothis.telescope_config.opts = opts
local picker = pickers.new(opts, {
Expand All @@ -232,6 +275,8 @@ local run = function(opts)
macrothis.telescope_config.mappings.quickfix,
run_macro_on_quickfixlist
)
map("i", macrothis.telescope_config.mappings.rename, rename_macro)
map("i", macrothis.telescope_config.mappings.edit, edit_macro)
return true
end,
})
Expand Down

0 comments on commit 5f1f1ec

Please sign in to comment.