]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0639: gq with 'formatprg' fails on an empty buffer v9.2.0639
authorglepnir <glephunter@gmail.com>
Sat, 13 Jun 2026 19:32:44 +0000 (19:32 +0000)
committerChristian Brabandt <cb@256bit.org>
Sat, 13 Jun 2026 19:32:44 +0000 (19:32 +0000)
Problem:  gq (and other filters) on an empty buffer fail with
          "E20: Mark not set": when the filter produces no output,
          do_filter() still subtracts the line count from '[ and '],
          pushing '] to line 0.
Solution: when the filter produces no output, put '[ and '] on a valid
          line instead of subtracting past line 1 (glepnir).

related: neovim/neovim#30593
closes:  #19061

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/ex_cmds.c
src/testdir/test_marks.vim
src/version.c

index b2fc85c99e10a585bea37df822b7ae0a8a65e217..803a10a2d7f67b016d9c9d184e0018e6ab5c9b72 100644 (file)
@@ -1516,8 +1516,18 @@ do_filter(
             */
            curwin->w_cursor.lnum = line1;
            del_lines(linecount, TRUE);
-           curbuf->b_op_start.lnum -= linecount;       // adjust '[
-           curbuf->b_op_end.lnum -= linecount;         // adjust ']
+           if (read_linecount == 0)
+           {
+               // no filter output: clamp '[ and '] to a valid line
+               curbuf->b_op_start.lnum = curbuf->b_op_end.lnum =
+                                       MIN(line1, curbuf->b_ml.ml_line_count);
+               curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
+           }
+           else
+           {
+               curbuf->b_op_start.lnum -= linecount;   // adjust '[
+               curbuf->b_op_end.lnum -= linecount;     // adjust ']
+           }
            write_lnum_adjust(-linecount);              // adjust last line
                                                        // for next write
 #ifdef FEAT_FOLDING
index 61bfade6272ea4df132052025268ec2130f54f8d..d4b01dfda3c9d0557d4b28870a825d39c61fafef 100644 (file)
@@ -329,5 +329,26 @@ func Test_jump_mark_autocmd()
   bwipe!
 endfunc
 
+func Test_mark_formatprg_on_empty()
+  new
+  if has('win32')
+    setl formatprg=more
+  else
+    setl formatprg=cat
+  endif
+  call assert_equal([0, 0], [line("'["), col("'[")])
+  call assert_equal([0, 0], [line("']"), col("']")])
+  let v:errmsg = ''
+  try
+    norm! gqG
+  catch
+    call assert_report('gqG on empty buffer should not fail: ' .. v:exception)
+  endtry
+  call assert_true(empty(v:errmsg))
+  " col() is 1-based, so 1 == first column (:marks shows it as 0)
+  call assert_equal([1, 1], [line("'["), col("'[")])
+  call assert_equal([1, 1], [line("']"), col("']")])
+  bwipe!
+endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
index d4d8e477fa9d1c279c8fb62304deb8ad465d0f35..1fdf8d1b01f3de82eceec61f16bcb4a69feb7e06 100644 (file)
@@ -759,6 +759,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    639,
 /**/
     638,
 /**/