vim-patch:9.2.0639: gq with 'formatprg' fails on an empty buffer (#40226)

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:  vim/vim#19061

aefbca2977
This commit is contained in:
glepnir
2026-06-14 08:45:16 +08:00
committed by GitHub
parent 37239e724e
commit ff0352fd4f
2 changed files with 29 additions and 2 deletions

View File

@@ -1415,8 +1415,14 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
// Adjust '[ and '] (set by buf_write()).
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
foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);