Skip to content

Commit 0f8f912

Browse files
authored
Merge pull request #3 from kr40/dev
Feat: Delete Macro!
2 parents 45307ac + 0d9c019 commit 0f8f912

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lua/nvim-macros/init.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,47 @@ function M.save_macro(register)
166166
print("Macro saved as " .. name)
167167
end
168168

169+
function M.delete_macro()
170+
local macros = handle_json_file("r")
171+
if not macros or not macros.macros or #macros.macros == 0 then
172+
print_error("No macros found.")
173+
return
174+
end
175+
176+
local choices = {}
177+
local name_to_index_map = {}
178+
for index, macro in ipairs(macros.macros) do
179+
if macro.name then
180+
local display_text = macro.name .. " | " .. string.sub(macro.content, 1, 150)
181+
table.insert(choices, display_text)
182+
name_to_index_map[display_text] = index
183+
end
184+
end
185+
186+
if next(choices) == nil then
187+
print_error("No valid macros to select for deletion.")
188+
return
189+
end
190+
191+
vim.ui.select(choices, { prompt = "Select a macro to delete:" }, function(choice)
192+
if not choice then
193+
print_error("No macro selected for deletion.")
194+
return
195+
end
196+
197+
local macro_index = name_to_index_map[choice]
198+
if not macro_index then
199+
print_error("Selected macro is not valid.")
200+
return
201+
end
202+
203+
-- Remove the selected macro from the list
204+
table.remove(macros.macros, macro_index)
205+
handle_json_file("w", macros) -- Write the updated list back to the JSON file
206+
print("Macro deleted: " .. choice:match("^[^|]+"))
207+
end)
208+
end
209+
169210
-- Select and yank macro from JSON file (Yanks raw or escaped termcodes)
170211
function M.select_and_yank_macro()
171212
local macros = handle_json_file("r")

plugin/nvim-macros.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ end, { nargs = "*" })
99
vim.api.nvim_create_user_command("MacroSelect", function()
1010
require("nvim-macros").select_and_yank_macro()
1111
end, {})
12+
13+
vim.api.nvim_create_user_command("MacroDelete", function()
14+
require("nvim-macros").delete_macro()
15+
end, {})

0 commit comments

Comments
 (0)