unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Björn Lindström" <bkhl@elektrubadur.se>
To: 71499@debbugs.gnu.org
Subject: bug#71499: [PATCH] Make whitespace.el cleanup add missing final newline
Date: Wed, 12 Jun 2024 07:21:04 +0200	[thread overview]
Message-ID: <09bb792d-3a3c-46b0-8e42-193f62bc11c2@app.fastmail.com> (raw)
In-Reply-To: <9453e7ef-dc31-4074-bba7-55a192313ccd@app.fastmail.com>

[-- Attachment #1: Type: text/plain, Size: 722 bytes --]

On Tue, Jun 11, 2024, at 20:16, Björn Lindström wrote:
> 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
> Attachments:
> * 0001-Make-whitespace.el-cleanup-add-missing-final-newline.patch

Sorry, somehow messed up the first patch I sent, now attaching a corrected one. Attaching the correct one here.

Best wishes,
Björn

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-whitespace.el-cleanup-add-missing-final-newline-2.patch --]
[-- Type: text/x-patch; name="0001-Make-whitespace.el-cleanup-add-missing-final-newline-2.patch", Size: 1805 bytes --]

From 3e180674604f41de70198c3aaa3b0bc5cddf0a68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Lindstr=C3=B6m?= <bkhl@elektrubadur.se>
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


  reply	other threads:[~2024-06-12  5:21 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-11 18:16 bug#71499: [PATCH] Make whitespace.el cleanup add missing final newline Björn Lindström
2024-06-12  5:21 ` Björn Lindström [this message]
2024-06-12  7:46 ` Eli Zaretskii
2024-06-12  9:04   ` Björn Lindström
2024-06-12  9:41     ` Eli Zaretskii
2024-06-12 12:38       ` Stefan Kangas
2024-06-13  7:38         ` Andrea Corallo
2024-06-13  8:30           ` Eli Zaretskii
2024-06-14 12:23             ` Robert Pluim
2024-06-14 12:50               ` Eli Zaretskii
2024-06-20  7:55                 ` Stefan Kangas
2024-06-20  8:22                   ` Robert Pluim
2024-06-20  8:55                     ` Stefan Kangas
2024-06-20  9:45                       ` Robert Pluim
2024-06-20 11:23                         ` Stefan Kangas
2024-06-27  7:37             ` Eli Zaretskii
2024-06-29 11:59               ` Björn Lindström
2024-06-29 13:05                 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=09bb792d-3a3c-46b0-8e42-193f62bc11c2@app.fastmail.com \
    --to=bkhl@elektrubadur.se \
    --cc=71499@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).