From 3e180674604f41de70198c3aaa3b0bc5cddf0a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindstr=C3=B6m?= Date: Tue, 11 Jun 2024 19:49:55 +0200 Subject: [PATCH] Make whitespace.el cleanup add missing final newline * lisp/whitespace.el (whitespace-cleanup-region): if cleaning up at end of file, add missing newline if indicated by whitespace-style. --- lisp/whitespace.el | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lisp/whitespace.el b/lisp/whitespace.el index bc23a8794eb..6f8bd0b8585 100644 --- a/lisp/whitespace.el +++ b/lisp/whitespace.el @@ -1465,6 +1465,11 @@ defun whitespace-cleanup-region If `whitespace-style' includes the value `space-after-tab::space', replace TABs by SPACEs. +5. missing newline at end of file. + If `whitespace-style' includes the value `missing-newline-at-eof', + and the cleanup region includes the end of file, add a final newline + if it is not there already. + See `whitespace-style', `indent-tabs-mode' and `tab-width' for documentation." (interactive "@r") @@ -1545,7 +1550,14 @@ defun whitespace-cleanup-region ((memq 'space-before-tab::space whitespace-style) (whitespace-replace-action 'untabify rstart rend - whitespace-space-before-tab-regexp 2)))) + whitespace-space-before-tab-regexp 2))) + ;; PROBLEM 5: missing newline at end of file + (when (and (memq 'missing-newline-at-eof whitespace-style) + (= (point-max) (without-restriction (point-max)))) + (goto-char (point-max)) + (when (re-search-backward ".\\'" nil t) + (goto-char (point-max)) + (insert "\n")))) (set-marker rend nil)))) ; point marker to nowhere -- 2.45.2