Skip to content

Commit

Permalink
Merge pull request #3 from desdic/dev
Browse files Browse the repository at this point in the history
Run macros/Run on files in quickfix list
  • Loading branch information
desdic authored Aug 28, 2023
2 parents dec796b + b61bdef commit bfcf145
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ require("telescope").extensions = {
| <CR> | Load selected entry into register |
| <C-d> | Delete selected entry or delete all marked entries |
| <C-s> | Save a macro/register |
| <C-r> | Run macro |
| <C-q> | Run macro on files in quickfix list |

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

Expand Down
3 changes: 3 additions & 0 deletions doc/macrothis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Default telescope options
load = "<CR>",
save = "<C-s>",
delete = "<C-d>",
run = "<C-r>",
quickfix = "<C-q>",
},
sorter = sorters.get_generic_fuzzy_sorter,
items_display = {
Expand All @@ -99,6 +101,7 @@ Default telescope options
{ remaining = true },
},
},
run_register = "z", -- content of register z is replaced when running a macro
}
<

Expand Down
20 changes: 20 additions & 0 deletions lua/macrothis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ utils.remove_entry = function(opts, description)
utils.save_data(opts, data)
end

utils.run_macro = function(opts, register, description)
utils.load_register(opts, register, description)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("@" .. register, true, false, true),
"n",
false
)
end

utils.run_macro_on_quickfixlist = function(opts, register, description)
utils.load_register(opts, register, description)
vim.cmd(":cdo norm! @" .. register)
-- Make sure we are back into normal mode after replacement
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<ESC>", true, false, true),
"n",
false
)
end

return utils
34 changes: 34 additions & 0 deletions lua/telescope/_extensions/macrothis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local default_telescope = {
load = "<CR>",
save = "<C-s>",
delete = "<C-d>",
run = "<C-r>",
quickfix = "<C-q>",
},
sorter = sorters.get_generic_fuzzy_sorter,
items_display = {
Expand All @@ -37,6 +39,7 @@ local default_telescope = {
{ remaining = true },
},
},
run_register = "z", -- content of register z is replaced when running a macro
}
--minidoc_afterlines_end

Expand Down Expand Up @@ -190,6 +193,31 @@ local save_macro = function(_)
:find()
end

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

actions.close(prompt_bufnr)

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

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

actions.close(prompt_bufnr)
vim.cmd("cclose")

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

local run = function(opts)
macrothis.telescope_config.opts = opts
local picker = pickers.new(opts, {
Expand All @@ -200,6 +228,12 @@ local run = function(opts)
map("i", macrothis.telescope_config.mappings.load, load_macro)
map("i", macrothis.telescope_config.mappings.delete, delete_macro)
map("i", macrothis.telescope_config.mappings.save, save_macro)
map("i", macrothis.telescope_config.mappings.run, run_macro)
map(
"i",
macrothis.telescope_config.mappings.quickfix,
run_macro_on_quickfixlist
)
return true
end,
})
Expand Down

0 comments on commit bfcf145

Please sign in to comment.