patch 9.1.0002: a closing fold expr, may start a new fold

Commit: 
https://github.com/vim/vim/commit/0689b870595c687a23e102913ce5037f65d03748
Author: Shota Nozaki <[email protected]>
Date:   Wed Jan 3 19:18:43 2024 +0100

    patch 9.1.0002: a closing fold expr, may start a new fold
    
    Problem:  a closing fold expression may unexpectedly start a new fold
              when it should end a fold
              (reported by Shota Nozaki)
    Solution: if a fold hasn't started yet, do not immediately
              start a new fold with level 1
              (Shota Nozaki)
    
    fixes: #12768
    closes: #13748
    
    Signed-off-by: Shota Nozaki <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/src/fold.c b/src/fold.c
index be20991c7..5c41ed1b0 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -3362,7 +3362,9 @@ foldlevelExpr(fline_T *flp)
                  break;
 
        // "<1", "<2", .. : end a fold with a certain level
-       case '<': flp->lvl_next = n - 1;
+       case '<': // To prevent an unexpected start of a new fold, the next
+                 // level must not exceed the level of the current fold.
+                 flp->lvl_next = MIN(flp->lvl, n - 1);
                  flp->end = n;
                  break;
 
diff --git a/src/testdir/test_fold.vim b/src/testdir/test_fold.vim
index cb29d43d5..3c78e62c3 100644
--- a/src/testdir/test_fold.vim
+++ b/src/testdir/test_fold.vim
@@ -1778,4 +1778,17 @@ func Test_foldexpr_return_empty_string()
   bwipe!
 endfunc
 
+" Make sure that when ending a fold that hasn't been started, it does not
+" start a new fold.
+func Test_foldexpr_end_fold()
+  new
+  setlocal foldmethod=expr
+  let &l:foldexpr = 'v:lnum == 2 ? "<2" : "="'
+  call setline(1, range(1, 3))
+  redraw
+  call assert_equal([0, 0, 0], range(1, 3)->map('foldlevel(v:val)'))
+
+  bwipe!
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index c4e7fa3db..8c71b6864 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2,
 /**/
     1,
 /**/

-- 
-- 
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 on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1rL60A-00B8yq-HL%40256bit.org.

Raspunde prin e-mail lui