Subject: bug#71499: closed (Re: bug#71499: [PATCH] Make whitespace.el cleanup add missing final newline)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 29 Jun 2024 13:06:02 +0000

Your bug report

#71499: [PATCH] Make whitespace.el cleanup add missing final newline

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
71499: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=71499
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#71499: [PATCH] Make whitespace.el cleanup add missing final newline
From: Eli Zaretskii <[email-masked]>
To: Björn Lindström <[email-masked]>
Cc: [email-masked]
Date: Sat, 29 Jun 2024 16:05:23 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> Date: Sat, 29 Jun 2024 13:59:03 +0200
> From: Björn Lindström <[email-masked]>
> 
> updated patch attached, which ads a note to NEWS, and has a changed implementation of the insertion of missing newline inspired by the one from files.el. It's still slightly different to account for it also being possible to use on a region.
> 
> This time there's also an added test.

Thanks, installed on the master branch, and closing the bug.

Subject: [PATCH] Make whitespace.el cleanup add missing final newline
From: Björn Lindström <[email-masked]>
To: [email-masked]
Date: Tue, 11 Jun 2024 20:16:03 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello,

attaching patch to make the whitespace-cleanup and whitespace-cleanup-region functions add a final newline to a file if whitespace-style contains `missing-newline-at-eof

I'm aware this somewhat replicates what setting `require-final-newline would do, but I think since whitespace.el with this configuration highlights this as an error, it should also clean it up when asked.

Best wishes,
Björn

From 73e20b34c1669a9d52cbc70f4c02cc26fa23cf5b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Lindstr=C3=B6m?= <[email-masked]>
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 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index bc23a8794eb..a41a7520915 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,13 @@ 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)
+                   (= (point-max) (without-restriction (point-max))))
+          (goto-char (point-max))
+          (when (re-search-backward ".\\'" nil t)
+            (insert "\n")))
       (set-marker rend nil))))		; point marker to nowhere

-- 
2.45.2