Repository: neovim/neovim
File: src/nvim/main.c (C)

Code before:
static void handle_tag(char *tagname)
{
  if (tagname != NULL) {
    swap_exists_did_quit = false;

    vim_snprintf(IObuff, IOSIZE, "ta %s", tagname);
    do_cmdline_cmd(IObuff);
    TIME_MSG("jumping to tag");

    // If the user doesn't want to edit the file then we quit here.
    if (swap_exists_did_quit) {
      ui_call_error_exit(1);
      getout(1);
    }
  }
}

/// Read text from stdin.
static void read_stdin(void)
{
  // When getting the ATTENTION prompt here, use a dialog.
  swap_exists_action = SEA_DIALOG;
  no_wait_return = true;
  bool save_msg_didany = msg_didany;
  buf_T *prev_buf = NULL;
  buf_T *newbuf = NULL;

  if (curbuf->b_ffname) {
    // curbuf is already opened for a file, create a new buffer for stdin. #35269
    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);

  if (newbuf) {
    readfile(NULL, NULL, 0, 0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN, true);
  } else {
    open_buffer(true, NULL, 0);
  }

  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

Review comment (bug):
Maybe it would be cleaner to pull the `readfile` into the first case?

```c
  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, BLN_LISTED);
    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);
    readfile(NULL, NULL, 0, 0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN, true);
  } else {
    set_buflisted(true);
    open_buffer(true, NULL, 0);
  }
```

Code after:
static void handle_tag(char *tagname)
{
  if (tagname != NULL) {
    swap_exists_did_quit = false;

    vim_snprintf(IObuff, IOSIZE, "ta %s", tagname);
    do_cmdline_cmd(IObuff);
    TIME_MSG("jumping to tag");

    // If the user doesn't want to edit the file then we quit here.
    if (swap_exists_did_quit) {
      ui_call_error_exit(1);
      getout(1);
    }
  }
}

/// Read text from stdin.
static void read_stdin(void)
{
  // When getting the ATTENTION prompt here, use a dialog.
  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, BLN_LISTED);
    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);
    readfile(NULL, NULL, 0, 0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN, true);
  } else {
    set_buflisted(true);
    // Create memfile and read from stdin.
    open_buffer(true, NULL, 0);
  }

  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);
    }
  }