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

Code before:
  ftbuf->b_p_swf = false;
  ftbuf->b_p_ml = false;
  ftbuf->b_p_ft = xstrdup(filetype);

  if (!has_event(EVENT_FILETYPE)) {
    return ftbuf;  // Nothing more to do.
  }

  bool did_au_ft = false;
  TRY_WRAP(err, {
    did_au_ft = do_filetype_autocmd(ftbuf, true);
  });

  if (!bufref_valid(&bufref)) {
    if (!ERROR_SET(err)) {
      api_set_error(err, kErrorTypeException, "Internal buffer was deleted");
    }
    return NULL;
  }

  if (!did_au_ft && !ERROR_SET(err)) {
    api_set_error(err, kErrorTypeException, "Could not execute FileType autocommands");
  }
  return ftbuf;
}

static void wipe_ft_buf(buf_T *buf)
  FUNC_ATTR_NONNULL_ALL
{
  block_autocmds();

  bufref_T bufref;
  set_bufref(&bufref, buf);

  close_windows(buf, false);
  // Autocommands are blocked, but 'bufhidden' may have wiped it already.
  // Also can't wipe if the buffer is somehow still in a window or current.
  if (bufref_valid(&bufref) && buf != curbuf && buf->b_nwindows == 0) {
    close_buffer(NULL, buf, DOBUF_WIPE, false, true);
  }
  if (bufref_valid(&bufref)) {
    buf->b_flags &= ~BF_DUMMY;  // Couldn't wipe; keep it instead.
  }

  unblock_autocmds();
}

/// Gets the value of an option. The behavior of this function matches that of
/// |:set|: the local value of an option is returned if it exists; otherwise,
/// the global value is returned. Local values always correspond to the current
/// buffer or window, unless "buf" or "win" is set in {opts}.

Review comment (suggestion):
Dunno if it's too heavy-handed to bother closing windows, but it's similar to the static [`wipe_dummy_buffer()` in quickfix.c](https://github.com/neovim/neovim/blob/dddc359213be522026d60ffd89867a0f52f19256/src/nvim/quickfix.c#L6012). Not sure why that one isn't using `close_windows`, though.

Code after:
  ftbuf->b_p_ml = false;
  ftbuf->b_p_ft = xstrdup(filetype);

  TRY_WRAP(err, {
    do_filetype_autocmd(ftbuf, false);
  });

  if (!bufref_valid(&bufref)) {
    if (!ERROR_SET(err)) {
      api_set_error(err, kErrorTypeException, "Internal buffer was deleted");
    }
    return NULL;
  }

  return ftbuf;
}

static void wipe_ft_buf(buf_T *buf)
  FUNC_ATTR_NONNULL_ALL
{
  block_autocmds();

  bufref_T bufref;
  set_bufref(&bufref, buf);

  close_windows(buf, false);
  // Autocommands are blocked, but 'bufhidden' may have wiped it already.
  // Also can't wipe if the buffer is somehow still in a window or current.
  if (bufref_valid(&bufref) && buf != curbuf && buf->b_nwindows == 0) {
    wipe_buffer(buf, false);
  }
  if (bufref_valid(&bufref)) {
    buf->b_flags &= ~BF_DUMMY;  // Couldn't wipe; keep it instead.
  }

  unblock_autocmds();
}

/// Gets the value of an option. The behavior of this function matches that of
/// |:set|: the local value of an option is returned if it exists; otherwise,
/// the global value is returned. Local values always correspond to the current
/// buffer or window, unless "buf" or "win" is set in {opts}.
///
/// @param name      Option name
/// @param opts      Optional parameters
///                  - scope: One of "global" or "local". Analogous to
///                  |:setglobal| and |:setlocal|, respectively.
///                  - win: |window-ID|. Used for getting window local options.
///                  - buf: Buffer number. Used for getting buffer local options.
///                         Implies {scope} is "local".
///                  - filetype: |filetype|. Used to get the default option for a