From 086b85215f9a02a65673d3d2e35cd4ed37d5800a Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sun, 17 Apr 2016 09:56:19 +0200
Subject: [PATCH] inserting: treat also the final \r of a Mac file as a newline

Until now (when not leaving files unconverted), nano would fumble and
drop the final carriage return of a Mac file, and would thus treat the
last line of such a file as an unterminated line and prepend it to the
current line of the buffer.  Correct that, and delete the dead piece
of code that was meant to do this.

This fixes https://savannah.gnu.org/bugs/?47716.
---
 src/files.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/src/files.c b/src/files.c
index f4949ab3..edbcd850 100644
--- a/src/files.c
+++ b/src/files.c
@@ -841,17 +841,6 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw
 	writable = is_file_writable(filename);
     }
 
-#ifndef NANO_TINY
-    /* If file conversion isn't disabled and the last character in this
-     * file is '\r', read it in properly as a Mac format line. */
-    if (len == 0 && !ISSET(NO_CONVERT) && input == '\r') {
-	len = 1;
-
-	buf[0] = input;
-	buf[1] = '\0';
-    }
-#endif
-
     /* Did we not get a newline and still have stuff to do? */
     if (len > 0) {
 #ifndef NANO_TINY
@@ -874,9 +863,9 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw
     /* Attach the file we got to the filestruct.  If we got a file of
      * zero bytes, don't do anything. */
     if (num_lines > 0) {
-	/* If the file we got doesn't end in a newline, tack its last
-	 * line onto the beginning of the line at current. */
-	if (len > 0) {
+	/* If the file we got doesn't end in a newline (nor in a Mac return),
+	 * tack its last line onto the beginning of the line at current. */
+	if (len > 0 && (input != '\r' || ISSET(NO_CONVERT))) {
 	    filestruct *dropline = fileptr;
 	    size_t current_len = strlen(openfile->current->data);
 
-- 
GitLab