src/nvim/fileio.c
/// Prepare for executing autocommands for (hidden) buffer `buf`.
/// If the current buffer is not in any visible window, put it in a temporary
/// floating window `aucmd_win`.
/// Set `curbuf` and `curwin` to match `buf`.
///
/// @param aco  structure to save values in
/// @param buf  new curbuf
Prepare for executing autocommands for (hidden) buffer `buf`.
If the current buffer is not in any visible window, put it in a temporary
floating window `aucmd_win`.
Prepare for executing autocommands for ( hidden ) buffer ` buf ` . If the current buffer is not in any visible window put it in a temporary floating window ` aucmd_win ` .
void aucmd_prepbuf(aco_save_T *aco, buf_T *buf)
{
  win_T *win;
  bool need_append = true;  
  if (buf == curbuf) {          
    win = curwin;
  } else {
    win = NULL;
    FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
      if (wp->w_buffer == buf) {
        win = wp;
        break;
      }
    }
  }
  if (win == NULL && aucmd_win == NULL) {
    win_alloc_aucmd_win();
    need_append = false;
  }
  if (win == NULL && aucmd_win_used)
    win = curwin;
  aco->save_curwin = curwin;
  aco->save_prevwin = prevwin;
  aco->save_curbuf = curbuf;
  if (win != NULL) {
    aco->use_aucmd_win = FALSE;
    curwin = win;
  } else {
    aco->use_aucmd_win = TRUE;
    aucmd_win_used = TRUE;
    aucmd_win->w_buffer = buf;
    aucmd_win->w_s = &buf->b_s;
    ++buf->b_nwindows;
    win_init_empty(aucmd_win);     
    XFREE_CLEAR(aucmd_win->w_localdir);
    aco->globaldir = globaldir;
    globaldir = NULL;
    block_autocmds();  
    if (need_append) {
      win_append(lastwin, aucmd_win);
      handle_register_window(aucmd_win);
      win_config_float(aucmd_win, aucmd_win->w_float_config);
    }
    int save_acd = p_acd;
    p_acd = false;
    win_enter(aucmd_win, false);
    p_acd = save_acd;
    unblock_autocmds();
    curwin = aucmd_win;
  }
  curbuf = buf;
  aco->new_curwin = curwin;
  set_bufref(&aco->new_curbuf, curbuf);
}
void aucmd_prepbuf ( aco_save_T * aco , buf_T * buf ) { win_T * win ; bool need_append = true ; if ( buf == curbuf ) { win = curwin ; } else { win = NULL ; FOR_ALL_WINDOWS_IN_TAB ( wp , curtab ) { if ( wp -> w_buffer == buf ) { win = wp ; break ; } } } if ( win == NULL && aucmd_win == NULL ) { win_alloc_aucmd_win ( ) ; need_append = false ; } if ( win == NULL && aucmd_win_used ) win = curwin ; aco -> save_curwin = curwin ; aco -> save_prevwin = prevwin ; aco -> save_curbuf = curbuf ; if ( win != NULL ) { aco -> use_aucmd_win = FALSE ; curwin = win ; } else { aco -> use_aucmd_win = TRUE ; aucmd_win_used = TRUE ; aucmd_win -> w_buffer = buf ; aucmd_win -> w_s = & buf -> b_s ; ++ buf -> b_nwindows ; win_init_empty ( aucmd_win ) ; XFREE_CLEAR ( aucmd_win -> w_localdir ) ; aco -> globaldir = globaldir ; globaldir = NULL ; block_autocmds ( ) ; if ( need_append ) { win_append ( lastwin , aucmd_win ) ; handle_register_window ( aucmd_win ) ; win_config_float ( aucmd_win , aucmd_win -> w_float_config ) ; } int save_acd = p_acd ; p_acd = false ; win_enter ( aucmd_win , false ) ; p_acd = save_acd ; unblock_autocmds ( ) ; curwin = aucmd_win ; } curbuf = buf ; aco -> new_curwin = curwin ; set_bufref ( & aco -> new_curbuf , curbuf ) ; }
Prepare for executing autocommands for (hidden) buffer `buf`.
Prepare for executing autocommands for ( hidden ) buffer ` buf ` .
// Append `aucmd_win` to the window list. /* Find a window that is for the new buffer */ /* be quick when buf is curbuf */ // Allocate the `aucmd_win` dummy floating window. /* Strange recursive autocommand, fall back to using the current
     * window.  Expect a few side effects... */ /* There is a window for "buf" in the current tab page, make it the
     * curwin.  This is preferred, it has the least side effects (esp. if
     * "buf" is curbuf). */ /* There is no window for "buf", use "aucmd_win".  To minimize the side
     * effects, insert it in the current tab page.
     * Anything related to a window (e.g., setting folds) may have
     * unexpected results. */ /* set cursor and topline to safe values */ /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in
     * win_enter_ext(). */ // We don't want BufEnter/WinEnter autocommands. // Prevent chdir() call in win_enter_ext(), through do_autochdir()