Update config/vis/visrc.lua

This commit is contained in:
2026-10-07 06:42:19 +00:00
parent ee1bb98515
commit 6617102c5b
+28
View File
@@ -78,3 +78,31 @@ if vis.ftdetect then
end
end
-- ---------------------------------------------------------------------------
-- On save: strip trailing spaces/tabs and extra blank lines at the end of
-- the file. Skips files where trailing whitespace means something.
-- ---------------------------------------------------------------------------
local keep_whitespace = { md = true, markdown = true, diff = true, patch = true }
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function(file, path)
local ext = path and path:match('%.(%w+)$')
if ext and keep_whitespace[ext:lower()] then return true end
-- trailing spaces/tabs (not \r, so CRLF files stay CRLF);
-- read every line first, then rewrite only the ones that changed
local trimmed, n = {}, 0
for line in file:lines_iterator() do
n = n + 1
local s = line:find('[ \t]+$')
if s then trimmed[n] = line:sub(1, s - 1) end
end
for i, line in pairs(trimmed) do file.lines[i] = line end
-- blank lines at the end: keep exactly one final newline
local size = file.size
local tail = file:content(math.max(0, size - 256), math.min(size, 256))
local extra = tail:match('\n(\n+)$')
if extra then file:delete(size - #extra, #extra) end
return true
end)