~yerinalexey/dotfiles

b6cb1e417f880f1e16fd0ce83c8f3cc7f11a29d5 — Alexey Yerin 9 months ago 16b6f65
vis: guard trim-whitespace behind an option
1 files changed, 22 insertions(+), 12 deletions(-)

M vis/visrc.lua
M vis/visrc.lua => vis/visrc.lua +22 -12
@@ 90,27 90,23 @@ end)
-- This requires my custom vis fork: https://git.sr.ht/~yerinalexey/vis
vis:map(vis.modes.NORMAL, "<C-w>J", function(keys)
	local win = vis.win
	if win:next() ~= nil then
		win:swap(win:next())
		win:focus_next()
	local target = win:next()
	if target ~= nil then
		win:swap(target)
		win:focus()
	end
end)

vis:map(vis.modes.NORMAL, "<C-w>K", function(keys)
	local win = vis.win
	if win:previous() ~= nil then
		win:swap(win:previous())
		win:focus_previous()
	local target = win:previous()
	if target ~= nil then
		win:swap(target)
		win:focus()
	end
end)

-- Delete trailing whitespace
function trim_whitespace()
	local name = vis.win.file.name
	if name and (name:match("%.patch$") or name:match("%.eml$")) then
		return
	end

	local vp = vis.win.viewport

	local sel = vis.win.selection


@@ 129,6 125,20 @@ function trim_whitespace()
	vis.win.viewport = vp
end

vis.events.subscribe(vis.events.WIN_OPEN, function(win)
	win.trim_whitespace = true
end)

vis:option_register("trim-whitespace", "bool", function(value, toggle)
	if not vis.win then return false end
	vis.win.trim_whitespace = toggle and not vis.win.trim_whitespace or value
	return true
end, "Whether trailing whitespace should be erased when saving")

vis.events.subscribe(vis.events.FILE_SAVE_PRE, function()
	local name = vis.win.file.name
	if (name and (name:match("%.patch$") or name:match("%.eml$"))) or not vis.win.trim_whitespace then
		return
	end
	trim_whitespace()
end)