From: martin f. krafft Date: Tue, 20 May 2025 13:54:25 +0000 (+0200) Subject: Implement squashing lines as a function X-Git-Url: https://git.madduck.net/etc/lazyvim.git/commitdiff_plain/1a417133323246b65fb5421bd150135811364fce?ds=inline Implement squashing lines as a function --- diff --git a/.config/lazyvim/after/ftplugin/mail.lua b/.config/lazyvim/after/ftplugin/mail.lua index 176382c..8513a6d 100644 --- a/.config/lazyvim/after/ftplugin/mail.lua +++ b/.config/lazyvim/after/ftplugin/mail.lua @@ -27,7 +27,29 @@ vim.opt_local.commentstring = "> %s" -- commenting means quoting in mails vim.api.nvim_create_autocmd({ "BufWrite" }, { buffer = 0, group = vim.api.nvim_create_augroup("squashemptylines", { clear = true }), - command = [[%s/\v(\n)\n+$/\1/ge]], + callback = function() + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) + local ret = {} + local count = 0 + local squash = false + for _, line in ipairs(lines) do + if line == "" then + if not squash then + squash = true + table.insert(ret, line) + else + count = count + 1 + end + else + squash = false + table.insert(ret, line) + end + end + if count > 0 then + vim.notify("Squashed " .. count .. " empty line(s)") + end + vim.api.nvim_buf_set_lines(0, 0, -1, false, ret) + end, }) -- }}}