From 6617102c5bba3dbf06ea36d3ea824ab6de51417d Mon Sep 17 00:00:00 2001 From: jasonhilder Date: Wed, 7 Oct 2026 06:42:19 +0000 Subject: [PATCH] Update config/vis/visrc.lua --- config/vis/visrc.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/config/vis/visrc.lua b/config/vis/visrc.lua index 6fddfc4..d65150e 100644 --- a/config/vis/visrc.lua +++ b/config/vis/visrc.lua @@ -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) \ No newline at end of file