From fcbf85887bc445ba9131248ced7ef61b680be7c4 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Tue, 12 May 2020 17:17:28 +0200 Subject: [PATCH] files: never report a file as being of mixed format A file is always in either Unix or DOS or Mac format, and should by default be saved again in that same format. Any lone CRs or LFs after the first line should not change the format that was deduced from that very first line. --- src/files.c | 28 ++++++++++------------------ src/nano.h | 2 +- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/files.c b/src/files.c index e91dfa8f..10a2a601 100644 --- a/src/files.c +++ b/src/files.c @@ -641,7 +641,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) /* Whether the file is writable (in case we care). */ #ifndef NANO_TINY int format = 0; - /* 0 = *nix, 1 = DOS, 2 = Mac, 3 = both DOS and Mac. */ + /* 0 = Unix, 1 = DOS, 2 = Mac. */ #endif #ifndef NANO_TINY @@ -680,12 +680,12 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) * converting), then mark the format as DOS or Mac or a mixture. */ if (input == '\n') { #ifndef NANO_TINY - if ((num_lines == 0 || format != 0) && !ISSET(NO_CONVERT) && + if (num_lines == 0 && !ISSET(NO_CONVERT) && len > 0 && buf[len - 1] == '\r') - format |= 1; + format = 1; } else if ((num_lines == 0 || format == 2) && !ISSET(NO_CONVERT) && len > 0 && buf[len - 1] == '\r') { - format |= 2; + format = 2; #endif } else { /* Store the byte. */ @@ -762,16 +762,12 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) #ifndef NANO_TINY bool mac_line_needs_newline = FALSE; - /* If the final character is '\r', and file conversion isn't disabled, - * set format to Mac if we currently think the file is a *nix file, or - * to DOS-and-Mac if we currently think it is a DOS file. */ + /* If the final character is a CR and file conversion isn't disabled, + * strip this CR and indicate that an extra blank line is needed. */ if (buf[len - 1] == '\r' && !ISSET(NO_CONVERT)) { - format |= 2; - - /* Strip the carriage return. */ + if (num_lines == 0) + format = 2; buf[--len] = '\0'; - - /* Indicate we need to put a blank line in after this one. */ mac_line_needs_newline = TRUE; } #endif @@ -799,12 +795,8 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) if (!writable) statusline(ALERT, _("File '%s' is unwritable"), filename); #ifndef NANO_TINY - else if (format == 3) { - /* TRANSLATORS: Keep the next four messages at most 78 characters. */ - statusline(HUSH, P_("Read %zu line (Converted from DOS and Mac format)", - "Read %zu lines (Converted from DOS and Mac format)", - num_lines), num_lines); - } else if (format == 2) { + else if (format == 2) { + /* TRANSLATORS: Keep the next three messages at most 78 characters. */ openfile->fmt = MAC_FILE; statusline(HUSH, P_("Read %zu line (Converted from Mac format)", "Read %zu lines (Converted from Mac format)", diff --git a/src/nano.h b/src/nano.h index a6db4679..a54082e1 100644 --- a/src/nano.h +++ b/src/nano.h @@ -387,7 +387,7 @@ typedef struct openfilestruct { mark_type kind_of_mark; /* Whether it is a soft (with Shift) or a hard mark. */ format_type fmt; - /* The file's format -- Unix or DOS or Mac or mixed. */ + /* The file's format -- Unix or DOS or Mac. */ char *lock_filename; /* The path of the lockfile, if we created one. */ undostruct *undotop;