From 028b34bdda3277c09f25fb3ccb583bd223ef0101 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Fri, 13 Jan 2023 16:13:21 +0000 Subject: [PATCH] Added new actions module All functions originally defined in presser/init.lua and part of table@presser which were related to performing a specific action, have be migrated to this file. Function definitions have remained the same - presser@start_buffer has been renamed actions@buf_put_cursor_at() to allow for more generic use-cases which the function name did not imply. Call this function to place the cursor into any buffer in the GCM by its label name. --- presser/actions/init.lua | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 presser/actions/init.lua 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