diff src/fileio.c @ 30982:92b1338023ee v9.0.0826

patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place Commit: https://github.com/vim/vim/commit/3af982196b1b973e953c35351961f2a96fe34172 Author: K.Takata <kentkt@csc.jp> Date: Tue Nov 1 20:36:19 2022 +0000 patch 9.0.0826: if 'endofline' is set CTRL-Z may be written in a wrong place Problem: If 'endofline' is set the CTRL-Z may be written in the wrong place. Solution: Write CTRL-Z at the end of the file. Update the help to explain the possibilities better. (Ken Takata, closes #11486)
author Bram Moolenaar <Bram@vim.org>
date Wed, 02 Nov 2022 11:18:57 +0100
parents 35265d9d24df
children d8e7d725a666
line wrap: on
line diff
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2271,27 +2271,32 @@ failed:
     if (error && read_count == 0)
 	error = FALSE;
 
-    /*
-     * If we get EOF in the middle of a line, note the fact and
-     * complete the line ourselves.
-     * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
-     */
+    // In Dos format ignore a trailing CTRL-Z, unless 'binary' is set.
+    // In old days the file length was in sector count and the CTRL-Z the
+    // marker where the file really ended.  Assuming we write it to a file
+    // system that keeps file length properly the CTRL-Z should be dropped.
+    // Set the 'endoffile' option so the user can decide what to write later.
+    // In Unix format the CTRL-Z is just another character.
+    if (linerest != 0
+	    && !curbuf->b_p_bin
+	    && fileformat == EOL_DOS
+	    && ptr[-1] == Ctrl_Z)
+    {
+	ptr--;
+	linerest--;
+	if (set_options)
+	    curbuf->b_p_eof = TRUE;
+    }
+
+    // If we get EOF in the middle of a line, note the fact by resetting
+    // 'endofline' and add the line normally.
     if (!error
 	    && !got_int
-	    && linerest != 0
-	    // TODO: should we handle CTRL-Z differently here for 'endoffile'?
-	    && !(!curbuf->b_p_bin
-		&& fileformat == EOL_DOS
-		&& *line_start == Ctrl_Z
-		&& ptr == line_start + 1))
+	    && linerest != 0)
     {
 	// remember for when writing
 	if (set_options)
-	{
 	    curbuf->b_p_eol = FALSE;
-	    if (*line_start == Ctrl_Z && ptr == line_start + 1)
-		curbuf->b_p_eof = TRUE;
-	}
 	*ptr = NUL;
 	len = (colnr_T)(ptr - line_start + 1);
 	if (ml_append(lnum, line_start, len, newfile) == FAIL)