Updated presser/init.lua
File has been moved to subdirectory. Note: TBR = To Be Refactored/Restructured. - changed `ctx` to `gcm` as import for context manager module. - gcm/GCM = global context manager - all associations with this variable have been updated respectfully. - func@new is no longer responsible for creating a new context in the GCM. This is now handled per built-in. - call to gcm@update now respects newly modified GCM structure. - Added presser@move_next_buffer: - temporary function to experiment moving between buffers which are marked as `allowed = true` in the GCM. - TBR. - Added func@read_buffer: - reads the contents of all buffers in the GCM. - returns a table storing all collected buffer contents. - TBR. - Added presser@execute: - temporary function which calls func@read_buffer and performs a vim substitute as per the built-in presser@find_replace. - TBR. - Added func@start_buffer: - place the cursor in an initial buffer by name. - TBR. Should be func@put_cursor_in_buf (or similar). - Updated keymap to store more keys for experimenting. - Calculate the centre of the current window height (should check for split windows in future, or determine terminal size). - Changed ordering of window creation. Windows created in order they are rendered.
This commit is contained in:
parent
4049e5c2d0
commit
d96c5b2028
104
presser/init.lua
104
presser/init.lua
|
|
@ -1,6 +1,6 @@
|
||||||
local popup = require'plenary.popup'
|
local popup = require'plenary.popup'
|
||||||
local utils = require'presser.utils'
|
local utils = require'presser.utils'
|
||||||
local ctx = require('presser.context_manager')
|
local gcm = require('presser.context_manager')
|
||||||
|
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
|
|
||||||
|
|
@ -9,7 +9,7 @@ local presser = {} -- list of functions to be exported
|
||||||
|
|
||||||
-- :@Dev: close all Presser windows that may be open for all contexts
|
-- :@Dev: close all Presser windows that may be open for all contexts
|
||||||
presser.close = function ()
|
presser.close = function ()
|
||||||
ctx.flush() -- invoke the context manager to flush the table
|
gcm.flush() -- invoke the context manager to flush the table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,9 +26,6 @@ end
|
||||||
-- C++, this function is akin to that behaviour and is responsible for constructing the window/buffer and ensuring it
|
-- C++, this function is akin to that behaviour and is responsible for constructing the window/buffer and ensuring it
|
||||||
-- can be tracked by Vim itself.
|
-- can be tracked by Vim itself.
|
||||||
local new = function ( _ctx, label, opts )
|
local new = function ( _ctx, label, opts )
|
||||||
-- create new context in the manager if it doesn't already exist
|
|
||||||
ctx.create( _ctx )
|
|
||||||
|
|
||||||
local opts = opts or {}
|
local opts = opts or {}
|
||||||
if not opts.placeholder then
|
if not opts.placeholder then
|
||||||
opts["placeholder"] = ""
|
opts["placeholder"] = ""
|
||||||
|
|
@ -46,7 +43,7 @@ local new = function ( _ctx, label, opts )
|
||||||
end
|
end
|
||||||
|
|
||||||
local obj = popup.create( opts.placeholder, buf_opts )
|
local obj = popup.create( opts.placeholder, buf_opts )
|
||||||
ctx.update( _ctx, obj )
|
gcm.update( _ctx, { id = obj, what = label, allowed = opts.allowed } )
|
||||||
-- :TODO: call update function in context manager
|
-- :TODO: call update function in context manager
|
||||||
-- table.insert( manager[_type], obj )
|
-- table.insert( manager[_type], obj )
|
||||||
-- g.presser_buf_ctx = manager
|
-- g.presser_buf_ctx = manager
|
||||||
|
|
@ -71,6 +68,69 @@ local new = function ( _ctx, label, opts )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
presser.move_next_buffer = function ()
|
||||||
|
local ctxs = 0
|
||||||
|
local c = vim.g.presser_buf_ctx
|
||||||
|
local buf = api.nvim_get_current_buf()
|
||||||
|
|
||||||
|
for _, v in pairs( c ) do for idx, _ in pairs( v ) do ctxs = idx end end
|
||||||
|
|
||||||
|
for ctx, ctx_obj in pairs( c ) do
|
||||||
|
for idx, obj in pairs( ctx_obj ) do
|
||||||
|
if api.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
|
||||||
|
api.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, ctx_obj in pairs( c ) do
|
||||||
|
for idx, obj in pairs( ctx_obj ) do
|
||||||
|
if obj.allowed then
|
||||||
|
local buf = api.nvim_win_get_buf( obj.id )
|
||||||
|
table.insert(b, utils.clean_buf( api.nvim_buf_get_lines( buf, 0, -1, false )[1] ))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return b
|
||||||
|
end
|
||||||
|
|
||||||
|
presser.execute = function ()
|
||||||
|
local result = read_buffer()
|
||||||
|
presser.close()
|
||||||
|
|
||||||
|
local cmd = ":%s/" .. result[1] .. "/" .. result[2] .. "/g"
|
||||||
|
api.nvim_exec( cmd, false )
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local start_buffer = function ( label )
|
||||||
|
for ctx, ctx_obj in pairs( vim.g.presser_buf_ctx ) do
|
||||||
|
for idx, record in pairs( ctx_obj ) do
|
||||||
|
if record.what == label then
|
||||||
|
api.nvim_set_current_win( record.id )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- @Description: Find and replace words within the current buffer.
|
-- @Description: Find and replace words within the current buffer.
|
||||||
-- @Params:
|
-- @Params:
|
||||||
-- @Returns: nil.
|
-- @Returns: nil.
|
||||||
|
|
@ -82,43 +142,57 @@ end
|
||||||
function presser.find_replace()
|
function presser.find_replace()
|
||||||
-- define the context which these windows will belong to in the context manager.
|
-- define the context which these windows will belong to in the context manager.
|
||||||
local ctx = "find_replace"
|
local ctx = "find_replace"
|
||||||
|
gcm.create( ctx )
|
||||||
|
|
||||||
local keymap = {
|
local keymap = {
|
||||||
n = {
|
n = {
|
||||||
["<esc>"] = "<cmd>lua require'presser'.close()<CR>",
|
["<esc>"] = "<cmd>lua require'presser'.close()<CR>",
|
||||||
|
["<A-a>"] = "<cmd>lua require'presser'.execute()<CR>",
|
||||||
},
|
},
|
||||||
|
i = {
|
||||||
|
["<Tab>"] = "<cmd>lua require'presser'.move_next_buffer()<CR>",
|
||||||
|
["<A-a>"] = "<cmd>lua require'presser'.execute()<CR>",
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-- get the centre of the current buffer
|
||||||
|
local c_cols = math.floor( api.nvim_win_get_width(0) / 2 )
|
||||||
|
local c_lines = math.floor( api.nvim_win_get_height(0) / 2 )
|
||||||
|
|
||||||
|
-- print(c_cols, c_lines)
|
||||||
|
|
||||||
-- options for the title buffer of the built-in
|
-- options for the title buffer of the built-in
|
||||||
local opts = {
|
local opts = {
|
||||||
placeholder = "Find & Replace",
|
placeholder = "Find & Replace",
|
||||||
|
allowed = false,
|
||||||
window = {
|
window = {
|
||||||
line = 12,
|
line = c_lines - 3,
|
||||||
border = false,
|
border = false,
|
||||||
minwidth = 82,
|
minwidth = 82,
|
||||||
padding = { 0, 1, 1, 1 }
|
padding = { 0, 1, 1, 1 }
|
||||||
},
|
},
|
||||||
keybinds = keymap,
|
|
||||||
}
|
}
|
||||||
local tab_title = new( ctx, "presser_fr_title", opts )
|
local tab_title = new( ctx, "presser_fr_title", opts )
|
||||||
|
|
||||||
-- options for the replace buffer
|
-- options for the replace buffer
|
||||||
local opts = {
|
local opts = {
|
||||||
window = {
|
window = {
|
||||||
line = 18,
|
line = c_lines,
|
||||||
title = "Replace",
|
title = "Find",
|
||||||
},
|
},
|
||||||
keybinds = keymap,
|
keybinds = keymap,
|
||||||
}
|
}
|
||||||
local replace = new( ctx, "replace_buf", opts )
|
|
||||||
|
|
||||||
-- modify options for find buffer
|
|
||||||
opts["window"]["line"] = 15
|
|
||||||
opts["window"]["title"] = "Find"
|
|
||||||
local find = new ( ctx, "find_buf", opts )
|
local find = new ( ctx, "find_buf", opts )
|
||||||
|
|
||||||
|
-- modify options for find buffer
|
||||||
|
opts["window"]["line"] = c_lines+3
|
||||||
|
opts["window"]["title"] = "Replace"
|
||||||
|
local replace = new( ctx, "replace_buf", opts )
|
||||||
|
|
||||||
|
local result = start_buffer( "find_buf" )
|
||||||
api.nvim_feedkeys('A', 'n', false)
|
api.nvim_feedkeys('A', 'n', false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--presser.find_replace()
|
||||||
|
|
||||||
return presser
|
return presser
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user