patch 9.2.0124: auto-format may swallow white space

Commit: 
https://github.com/vim/vim/commit/24fd6980457a942d58c2fe74b945d951081bf8ab
Author: Daniel Müller <[email protected]>
Date:   Sun Mar 8 20:24:10 2026 +0000

    patch 9.2.0124: auto-format may swallow white space
    
    Problem:  With auto paragraph formatting enabled, when a user makes an
              attempt to add a new word before the end of a line and with
              the following space bringing the line width over 'textwidth',
              the space ends up just getting swallowed by the editor.
    Solution: Detect such a constellation and do not auto-format in that
              case (Daniel Müller).
    
    closes: #19593
    
    Signed-off-by: Daniel Müller <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/src/testdir/test_textformat.vim b/src/testdir/test_textformat.vim
index f4dedfacb..91fe943c1 100644
--- a/src/testdir/test_textformat.vim
+++ b/src/testdir/test_textformat.vim
@@ -1155,6 +1155,32 @@ func Test_fo_a_w()
   %bw!
 endfunc
 
+" Test that auto-format ('a' flag) preserves spaces typed in the middle of a 
line
+func Test_fo_a_midline_space()
+  new
+  let lines = [
+    \ 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa it 
but',
+    \ 'Lorem Ipsum is simply dummy text of the printing and typesetting 
dasddd',
+    \ 'industry.',
+    \ ]
+  call setline(1, lines)
+  set fo=ta tw=70
+
+  " Prevent INPUT_BUFLEN batching so auto_format runs between keystrokes
+  autocmd InsertCharPre * " nothing
+
+  " Position at 't' of 'it' (col 68) and type space then Z
+  call cursor(1, 68)
+  call feedkeys("a Z\<Esc>", 'xt')
+
+  " The space between 'it' and 'Z' must be preserved
+  call assert_match('it Z', getline(1))
+
+  autocmd! InsertCharPre
+  set fo& tw&
+  bw!
+endfunc
+
 " Test for formatting lines using gq in visual mode
 func Test_visual_gq_format()
   new
diff --git a/src/textformat.c b/src/textformat.c
index 8f4f2dd08..024c80b68 100644
--- a/src/textformat.c
+++ b/src/textformat.c
@@ -703,6 +703,24 @@ auto_format(
        curwin->w_cursor = pos;
     }
 
+    // Also skip formatting when the user just typed whitespace in the
+    // middle of the line.  Reformatting would join all paragraph lines and
+    // re-wrap, consuming the space at the line break point via
+    // OPENLINE_DELSPACES.  By deferring, the next non-whitespace character
+    // will be inserted adjacent to the space, keeping it protected from
+    // being consumed at a line break.  auto_format() will then reformat
+    // properly on the next keystroke.
+    if (*old != NUL && !trailblank && !wasatend && pos.col > 0
+           && (State & MODE_INSERT))
+    {
+       char_u *line = ml_get_curline();
+       if (WHITECHAR(line[pos.col - 1]))
+       {
+           curwin->w_cursor = pos;
+           return;
+       }
+    }
+
     // With the 'c' flag in 'formatoptions' and 't' missing: only format
     // comments.
     if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
diff --git a/src/version.c b/src/version.c
index 446c47fe0..5b78d544e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    124,
 /**/
     123,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1vzKlH-00DKMY-Vr%40256bit.org.

Raspunde prin e-mail lui