local gcm = require'presser.context_manager' --local actions = require'presser.actions' 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.hyperlink_exec = function () local url = utils.clean_buf(read_buffer()[1]) actions.close() if url == "" then return nil end local _, cur_start, _, cur_end = utils.get_selection() local line = a.nvim_get_current_line() if line == "" then return end local row, col = unpack(a.nvim_win_get_cursor(0)) if cur_start == -1 then cur_start, cur_end = unpack(utils.subslice(line .. " ", col)) end actions.suburl( line, row, url, cur_start, cur_end ) end actions.suburl = function (line, row, url, cur_start, cur_end) local prefix = string.sub(line, 1, cur_start) -- the first part up to selection local suffix = string.sub(line, cur_end + 1, line:len()) -- the last part after selection local infix = "[" .. utils.clean_buf(string.sub(line, cur_start, cur_end)) .. "]" .. "(" .. url .. ")" a.nvim_buf_set_lines( 0, row-1, row, false, { prefix .. infix .. suffix } ) 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