diff --git a/presser/actions/init.lua b/presser/actions/init.lua new file mode 100644 index 0000000..f2e09db --- /dev/null +++ b/presser/actions/init.lua @@ -0,0 +1,72 @@ +local gcm = require'presser.context_manager' +local utils = require'presser.utils' + +local a = vim.api + +local actions = {} + +-- :@Dev: close all Presser windows that may be open for all contexts +actions.close = function () + gcm.flush() -- invoke the context manager to flush the table +end + +actions.move_next_buffer = function () + local ctxs = 0 + local c = gcm.get_global_context_manager() + local buf = a.nvim_get_current_buf() + + for _, v in pairs( c ) do for idx, _ in pairs( v ) do ctxs = idx end end + + for _, ctx_obj in pairs( c ) do + for idx, obj in pairs( ctx_obj ) do + if a.nvim_win_get_buf( obj.id ) == buf then + -- get the next window, and put cursor there + local s = idx + repeat + local win = ctx_obj[(idx % ctxs) + 1] + if win.allowed then + a.nvim_set_current_win( ctx_obj[(idx % ctxs) + 1].id ) + break + end + idx = (idx % ctxs) + 1 + until s == idx + end + end + end +end + +local read_buffer = function () + local c = vim.g.presser_buf_ctx + + local b = {} + for _, ctx_obj in pairs( c ) do + for _, obj in pairs( ctx_obj ) do + if obj.allowed then + local buf = a.nvim_win_get_buf( obj.id ) + table.insert(b, utils.clean_buf( a.nvim_buf_get_lines( buf, 0, -1, false )[1] )) + end + end + end + + return b +end + +actions.execute = function () + local result = read_buffer() + actions.close() + + local cmd = ":%s/" .. result[1] .. "/" .. result[2] .. "/g" + a.nvim_exec( cmd, false ) +end + +actions.buf_put_curser_at = function ( label ) + for _, ctx_obj in pairs( vim.g.presser_buf_ctx ) do + for _, record in pairs( ctx_obj ) do + if record.what == label then + a.nvim_set_current_win( record.id ) + end + end + end +end + +return actions