8000 mini.nvim/lua/mini/operators.lua at main · vfaberd/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1321 lines (1126 loc) · 49.8 KB

operators.lua

File metadata and controls

1321 lines (1126 loc) · 49.8 KB
local f = function()
-- Yank into "z" register and return its blockwise type
H.set_mark('x', markcoords_from)
H.set_mark('y', markcoords_to)
local yank_data = { mark_from = 'x', mark_to = 'y', submode = H.submode_keys.block, mode = 'block', register = 't' }
H.do_between_marks('y', yank_data)
return vim.fn.getregtype('t')
end
return H.with_temp_context({ buf_id = 0, marks = { 'x', 'y' }, registers = { 't' } }, f)
end
H.exchange_stop = function()
H.exchange_del_stop_mapping()
local cur, ns_id = H.cache.exchange, H.ns_id.exchange
if cur.step_one ~= nil then pcall(vim.api.nvim_buf_clear_namespace, cur.step_one.buf_id, ns_id, 0, -1) end
if cur.step_two ~= nil then pcall(vim.api.nvim_buf_clear_namespace, cur.step_two.buf_id, ns_id, 0, -1) end
H.cache.exchange = {}
end
H.exchange_set_stop_mapping = function()
local lhs = '<C-c>'
H.cache.exchange.stop_restore_map_data = vim.fn.maparg(lhs, 'n', false, true)
vim.keymap.set('n', lhs, H.exchange_stop, { desc = 'Stop exchange' })
end
H.exchange_del_stop_mapping = function()
local map_data = H.cache.exchange.stop_restore_map_data
if map_data == nil then return end
-- Try restore previous mapping if it was set
if vim.tbl_count(map_data) > 0 then
vim.fn.mapset('n', false, map_data)
else
vim.keymap.del('n', map_data.lhs or '<C-c>')
end
end
H.exchange_is_same_steps = function(step_one, step_two)
if step_one.buf_id ~= step_two.buf_id or step_one.submode ~= step_two.submode then return false end
-- Region's start and end should be the same
local one, two = H.exchange_get_region_extmark(step_one), H.exchange_get_region_extmark(step_two)
return one[1] == two[1] and one[2] == two[2] and one[3].end_row == two[3].end_row and one[3].end_col == two[3].end_col
end
-- Multiply -------------------------------------------------------------------
H.multiply_get_ref_coords = function(mark_from, mark_to, submode)
local markcoords_from, markcoords_to = H.get_mark(mark_from), H.get_mark(mark_to)
if mark_to == '>' and vim.o.selection == 'exclusive' then markcoords_to[2] = markcoords_to[2] - 1 end
if submode ~= H.submode_keys.block then return markcoords_to end
-- In blockwise selection go to top right corner (allowing for presence of
-- multibyte characters)
local row = math.min(markcoords_from[1], markcoords_to[1])
-- - "from"/"to" may not only be "top-left"/"bottom-right" but also
-- "top-right" and "bottom-left"
local virtcol_from = vim.fn.virtcol({ markcoords_from[1], markcoords_from[2] + 1 })
local virtcol_to = vim.fn.virtcol({ markcoords_to[1], markcoords_to[2] + 1 })
local virtcol = math.max(virtcol_from, virtcol_to)
local col = vim.fn.virtcol2col(0, row, virtcol)
return { row, col - 1 }
end
-- Replace --------------------------------------------------------------------
--- Delete region between two marks and paste from register
---
---@param data table Fields:
--- - <count> (optional) - Number of times to paste.
--- - <mark_from> - Name of "from" mark.
--- - <mark_to> - Name of "to" mark.
--- - <mode> - Operator mode. One of 'visual', 'char', 'line', 'block'.
--- - <register> - Name of register from which to paste.
0