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

Code before:

    // The cursor position may be invalid if the buffer changed after last
    // using the window.
    check_cursor(curwin);
  }

  if (!was_floating) {
    // If last window has a status line now and we don't want one,
    // remove the status line. Do this before win_equal(), because
    // it may change the height of a window.
    last_status(false);

    if (!curwin->w_floating && p_ea && (*p_ead == 'b' || *p_ead == dir)) {
      // If the frame of the closed window contains the new current window,
      // only resize that frame.  Otherwise resize all windows.
      win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
    } else {
      win_comp_pos();
      win_fix_scroll(false);
    }
  }

  if (close_curwin) {
    win_enter_ext(wp, WEE_CURWIN_INVALID | WEE_TRIGGER_ENTER_AUTOCMDS
                  | WEE_TRIGGER_LEAVE_AUTOCMDS);
    if (other_buffer) {
      // careful: after this wp and win may be invalid!
      apply_autocmds(EVENT_BUFENTER, NULL, NULL, false, curbuf);
    }
  }

  if (ONE_WINDOW && curwin->w_locked && curbuf->b_locked_split
      && first_tabpage->tp_next != NULL) {
    // The new curwin is the last window in the current tab page, and it is
    // already being closed.  Trigger TabLeave now, as after its buffer is
    // removed it's no longer safe to do that.
    apply_autocmds(EVENT_TABLEAVE, NULL, NULL, false, curbuf);
  }

  split_disallowed--;

  // After closing the help or quickfix window, try restoring the window
  // layout from before it was opened.
  if (help_window || quickfix_window) {
    restore_snapshot(help_window ? SNAP_HELP_IDX : SNAP_QUICKFIX_IDX, close_curwin);
  }

  // If the window had 'diff' set and now there is only one window left in
  // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then
  // execute ":diffoff!".
  if (diffopt_closeoff() && had_diffmode && curtab == prev_curtab) {

Review comment (suggestion):
Might be nice if `bufref_valid()` always returned `false` if `br_buf` is `NULL`. (that's not currently the case if `buf_free_count` didn't change)

Code after:
      }
    }
    curbuf = curwin->w_buffer;
    close_curwin = true;

    // The cursor position may be invalid if the buffer changed after last
    // using the window.
    check_cursor(curwin);
  }

  if (!was_floating) {
    // If last window has a status line now and we don't want one,
    // remove the status line. Do this before win_equal(), because
    // it may change the height of a window.
    last_status(false);

    if (!curwin->w_floating && p_ea && (*p_ead == 'b' || *p_ead == dir)) {
      // If the frame of the closed window contains the new current window,
      // only resize that frame.  Otherwise resize all windows.
      win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
    } else {
      win_comp_pos();
      win_fix_scroll(false);
    }
  }
  if (bufref.br_buf && bufref_valid(&bufref) && bufref.br_buf->terminal) {
    terminal_check_size(bufref.br_buf->terminal);
  }

  if (close_curwin) {
    win_enter_ext(wp, WEE_CURWIN_INVALID | WEE_TRIGGER_ENTER_AUTOCMDS
                  | WEE_TRIGGER_LEAVE_AUTOCMDS);
    if (other_buffer) {
      // careful: after this wp and win may be invalid!
      apply_autocmds(EVENT_BUFENTER, NULL, NULL, false, curbuf);
    }
  }

  if (ONE_WINDOW && curwin->w_locked && curbuf->b_locked_split
      && first_tabpage->tp_next != NULL) {
    // The new curwin is the last window in the current tab page, and it is
    // already being closed.  Trigger TabLeave now, as after its buffer is
    // removed it's no longer safe to do that.
    apply_autocmds(EVENT_TABLEAVE, NULL, NULL, false, curbuf);
  }

  split_disallowed--;

  // After closing the help or quickfix window, try restoring the window
  // layout from before it was opened.
  if (help_window || quickfix_window) {