presser.nvim/presser/actions/init.lua
TheOnePath b22ec2e733 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.
2023-01-13 17:18:56 +00:00

73 lines
1.7 KiB
Lua

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