bugfix: get_visual_selection with dirty buffers(closes issue #54)

main
bhagwan 3 years ago
parent 29844d7299
commit ba77d4912d

@ -186,13 +186,30 @@ for color, escseq in pairs(M.ansi_colors) do
end
function M.get_visual_selection()
-- must exit visual mode or program croaks
-- :visual leaves ex-mode back to normal mode
-- this will exit visual mode
-- use 'gv' to reselect the text
vim.cmd[[visual]]
-- M.feed_keys_termcodes("<CR>")
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
local _, csrow, cscol, cerow, cecol
local mode = vim.fn.mode()
if mode == 'v' or mode == 'V' or mode == '' then
-- if we are in visual mode use the live position
_, csrow, cscol, _ = unpack(vim.fn.getpos("."))
_, cerow, cecol, _ = unpack(vim.fn.getpos("v"))
if mode == 'V' then
-- visual line doesn't provide columns
cscol, cecol = 0, 999
end
-- exit visual mode
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<Esc>",
true, false, true), 'n', true)
else
-- otherwise, use the last known visual position
_, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
_, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
end
-- swap vars if needed
if cerow < csrow then csrow, cerow = cerow, csrow end
if cecol < cscol then cscol, cecol = cecol, cscol end
local lines = vim.fn.getline(csrow, cerow)
-- local n = cerow-csrow+1
local n = M.tbl_length(lines)

Loading…
Cancel
Save