fix(startup): file buf is lost if stdin is empty and "-" is last #35402

Problem:
Running `echo dummy | nvim file1 file2` closes the file1 buffer if
file1 doesn't exist.

Solution:
Logic changed in 43e8ec9 such that stdin buffer may now be created *after*
file args. Handle that case.
This commit is contained in:
Cameron Ring
2025-08-20 20:34:49 -07:00
committed by GitHub
parent a8dad46e1c
commit 30dae87de4
2 changed files with 47 additions and 5 deletions

View File

@@ -1623,15 +1623,37 @@ static void read_stdin(void)
swap_exists_action = SEA_DIALOG;
no_wait_return = true;
bool save_msg_didany = msg_didany;
buf_T *prev_buf = NULL;
if (curbuf->b_ffname) {
// curbuf is already opened for a file, create a new buffer for stdin. #35269
buf_T *newbuf = buflist_new(NULL, NULL, 0, 0);
if (newbuf == NULL) {
semsg("Failed to create buffer for stdin");
return;
}
// remember the current buffer so we can go back to it
prev_buf = curbuf;
set_curbuf(newbuf, 0, false);
}
set_buflisted(true);
// Create memfile and read from stdin.
open_buffer(true, NULL, 0);
if (buf_is_empty(curbuf) && curbuf->b_next != NULL) {
// stdin was empty, go to buffer 2 (e.g. "echo file1 | xargs nvim"). #8561
do_cmdline_cmd("silent! bnext");
// Delete the empty stdin buffer.
do_cmdline_cmd("bwipeout 1");
if (buf_is_empty(curbuf)) {
// stdin was empty so we should wipe it (e.g. "echo file1 | xargs nvim"). #8561
// stdin buffer may be first or last ("echo foo | nvim file1 -"). #35269
if ((curbuf->b_next != NULL) || (curbuf->b_prev != NULL)) {
do_bufdel(DOBUF_WIPE, NULL, 0, 0, 0, 1);
}
}
// we had to switch buffers to load stdin, switch back
if (prev_buf) {
set_curbuf(prev_buf, 0, false);
}
no_wait_return = false;
msg_didany = save_msg_didany;
TIME_MSG("reading stdin");