]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0535: newline escape wrong in ex mode v9.1.0535
authorMohamed Akram <mohd.akram@outlook.com>
Sat, 6 Jul 2024 15:12:09 +0000 (17:12 +0200)
committerChristian Brabandt <cb@256bit.org>
Sat, 6 Jul 2024 15:12:09 +0000 (17:12 +0200)
Problem:  newline escape wrong in ex mode (Konrad Schwarz)
Solution: partly revert patch 7.3.014, remove backslash in front of a
          newline when not in prompt mode in ex line mode
          (Mohamed Akram)

This fixes newline escaping to allow passing multiple commands to
":global", multiple lines to shell commands, and ending lines in append
mode with backslashes. This should fix a POSIX/(traditional) VI
incompatiblity.

This reverts a previous incorrect attempt at patch v7.3.014 to fix
append mode which removed half of trailing backslashes which lead to,
eg. the following two commands being parsed as having a different number
of backslashes:

```
!echo foo\\\
```

```
!echo foo\\ \
```

fixes: #6135
fixes: #7244
closes: #15120

Signed-off-by: Mohamed Akram <mohd.akram@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/ex_docmd.c
src/ex_getln.c
src/testdir/test_ex_mode.vim
src/version.c

index 59d3db4dd1850af26ed263e6fa41747a922f6504..1dc28bc668e981db8aab134a158eda76c7b93fcd 100644 (file)
@@ -2341,12 +2341,7 @@ do_one_cmd(
     {
        for (p = ea.arg; *p; ++p)
        {
-           // Remove one backslash before a newline, so that it's possible to
-           // pass a newline to the shell and also a newline that is preceded
-           // with a backslash.  This makes it impossible to end a shell
-           // command in a backslash, but that doesn't appear useful.
-           // Halving the number of backslashes is incompatible with previous
-           // versions.
+           // Remove one backslash before a newline
            if (*p == '\\' && p[1] == '\n')
                STRMOVE(p, p + 1);
            else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
index f05259bb9d0bcc962351f220e8cf6fd2b66bd0eb..51a38e583e11fc7f3b0ec8eac127f854c8cf132a 100644 (file)
@@ -3138,31 +3138,15 @@ redraw:
        windgoto(msg_row, msg_col);
        pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
 
-       // We are done when a NL is entered, but not when it comes after an
-       // odd number of backslashes, that results in a NUL.
-       if (line_ga.ga_len > 0 && pend[-1] == '\n')
+       // We are done when a NL is entered, but not when it comes after a
+       // backslash in prompt mode.
+       if (line_ga.ga_len > 0 && pend[-1] == '\n'
+               && (line_ga.ga_len <= 1 || pend[-2] != '\\' || !promptc))
        {
-           int bcount = 0;
-
-           while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
-               ++bcount;
-
-           if (bcount > 0)
-           {
-               // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
-               // "\NL", etc.
-               line_ga.ga_len -= (bcount + 1) / 2;
-               pend -= (bcount + 1) / 2;
-               pend[-1] = '\n';
-           }
-
-           if ((bcount & 1) == 0)
-           {
-               --line_ga.ga_len;
-               --pend;
-               *pend = NUL;
-               break;
-           }
+           --line_ga.ga_len;
+           --pend;
+           *pend = NUL;
+           break;
        }
     }
 
index 59c28f3836187079ac7a82f3d3f8105f8fb08019..12e983f4b8ff54ad28a5d51d33d0eb8201532da6 100644 (file)
@@ -165,6 +165,37 @@ func Test_Ex_global()
   call assert_equal('bax', getline(3))
   call assert_equal('bay', getline(5))
   bwipe!
+
+  new
+  call setline(1, ['foo', 'bar'])
+  call feedkeys("Qg/./i\\\na\\\n.\\\na\\\nb\\\n.", "xt")
+  call assert_equal(['a', 'b', 'foo', 'a', 'b', 'bar'], getline(1, '$'))
+  bwipe!
+endfunc
+
+func Test_Ex_shell()
+  CheckUnix
+
+  new
+  call feedkeys("Qr !echo foo\\\necho bar\n", 'xt')
+  call assert_equal(['', 'foo', 'bar'], getline(1, '$'))
+  bwipe!
+
+  new
+  call feedkeys("Qr !echo foo\\\\\nbar\n", 'xt')
+  call assert_equal(['', 'foobar'], getline(1, '$'))
+  bwipe!
+
+  new
+  call feedkeys("Qr !echo foo\\ \\\necho bar\n", 'xt')
+  call assert_equal(['', 'foo ', 'bar'], getline(1, '$'))
+  bwipe!
+
+  new
+  call setline(1, ['bar', 'baz'])
+  call feedkeys("Qg/./!echo \\\ns/b/c/", "xt")
+  call assert_equal(['car', 'caz'], getline(1, '$'))
+  bwipe!
 endfunc
 
 " Test for pressing Ctrl-C in :append inside a loop in Ex mode
@@ -204,18 +235,11 @@ func Test_Ex_append()
   call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt')
   call assert_equal(["\t   abc", "\t   pqr", "\t   xyz"], getline(1, '$'))
   close!
-endfunc
 
-" In Ex-mode, backslashes at the end of a command should be halved.
-func Test_Ex_echo_backslash()
-  " This test works only when the language is English
-  CheckEnglish
-  let bsl = '\\\\'
-  let bsl2 = '\\\'
-  call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")',
-        \ 'E15: Invalid expression: "\\"')
-  call assert_fails('call feedkeys("Qecho " .. bsl2 .. "\nm\nvisual\n", "xt")',
-        \ "E15: Invalid expression: \"\\\nm\"")
+  new
+  call feedkeys("Qappend\na\\\n.", 'xt')
+  call assert_equal(['a\'], getline(1, '$'))
+  close!
 endfunc
 
 func Test_ex_mode_errors()
@@ -314,5 +338,12 @@ func Test_empty_command_visual_mode()
   call delete('guidialogfile')
 endfunc
 
+" Test using backslash in ex-mode
+func Test_backslash_multiline()
+  new
+  call setline(1, 'enum')
+  call feedkeys('Qg/enum/i\\r\\r.', "xt")
+  call assert_equal(["", "enum"], getline(1, 2))
+endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
index 23713555802e19d24e3b32c4948c69d26cbe6c18..ffbc13eecac96ba3d35550232b18d5fe042a282d 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    535,
 /**/
     534,
 /**/