src/nvim/fileio.c
/// Cleanup after executing autocommands for a (hidden) buffer.
/// Restore the window as it was (if possible).
///
/// @param aco  structure holding saved values
Cleanup after executing autocommands for a (hidden) buffer.
Restore the window as it was (if possible).
Cleanup after executing autocommands for a ( hidden ) buffer . Restore the window as it was ( if possible ) .
void aucmd_restbuf(aco_save_T *aco)
{
  if (aco->use_aucmd_win) {
    curbuf->b_nwindows--;
    block_autocmds();
    if (curwin != aucmd_win) {
      FOR_ALL_TAB_WINDOWS(tp, wp) {
        if (wp == aucmd_win) {
          if (tp != curtab) {
            goto_tabpage_tp(tp, true, true);
          }
          win_goto(aucmd_win);
          goto win_found;
        }
      }
    }
win_found:
    win_remove(curwin, NULL);
    handle_unregister_window(curwin);
    if (curwin->w_grid.chars != NULL) {
      ui_comp_remove_grid(&curwin->w_grid);
      ui_call_win_hide(curwin->w_grid.handle);
      grid_free(&curwin->w_grid);
    }
    aucmd_win_used = false;
    last_status(false);         
    if (!valid_tabpage_win(curtab)) {
      close_tabpage(curtab);
    }
    unblock_autocmds();
    if (win_valid(aco->save_curwin)) {
      curwin = aco->save_curwin;
    } else {
      curwin = firstwin;
    }
    prevwin = win_valid(aco->save_prevwin) ? aco->save_prevwin
              : firstwin;  
    vars_clear(&aucmd_win->w_vars->dv_hashtab);      
    hash_init(&aucmd_win->w_vars->dv_hashtab);       
    curbuf = curwin->w_buffer;
    xfree(globaldir);
    globaldir = aco->globaldir;
    check_cursor();
    if (curwin->w_topline > curbuf->b_ml.ml_line_count) {
      curwin->w_topline = curbuf->b_ml.ml_line_count;
      curwin->w_topfill = 0;
    }
  } else {
    if (win_valid(aco->save_curwin)) {
      if (curwin == aco->new_curwin
          && curbuf != aco->new_curbuf.br_buf
          && bufref_valid(&aco->new_curbuf)
          && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) {
        if (curwin->w_s == &curbuf->b_s) {
          curwin->w_s = &aco->new_curbuf.br_buf->b_s;
        }
        curbuf->b_nwindows--;
        curbuf = aco->new_curbuf.br_buf;
        curwin->w_buffer = curbuf;
        curbuf->b_nwindows++;
      }
      curwin = aco->save_curwin;
      prevwin = win_valid(aco->save_prevwin) ? aco->save_prevwin
                : firstwin;  
      curbuf = curwin->w_buffer;
      check_cursor();
    }
  }
}
void aucmd_restbuf ( aco_save_T * aco ) { if ( aco -> use_aucmd_win ) { curbuf -> b_nwindows -- ; block_autocmds ( ) ; if ( curwin != aucmd_win ) { FOR_ALL_TAB_WINDOWS ( tp , wp ) { if ( wp == aucmd_win ) { if ( tp != curtab ) { goto_tabpage_tp ( tp , true , true ) ; } win_goto ( aucmd_win ) ; goto win_found ; } } } win_found : win_remove ( curwin , NULL ) ; handle_unregister_window ( curwin ) ; if ( curwin -> w_grid . chars != NULL ) { ui_comp_remove_grid ( & curwin -> w_grid ) ; ui_call_win_hide ( curwin -> w_grid . handle ) ; grid_free ( & curwin -> w_grid ) ; } aucmd_win_used = false ; last_status ( false ) ; if ( ! valid_tabpage_win ( curtab ) ) { close_tabpage ( curtab ) ; } unblock_autocmds ( ) ; if ( win_valid ( aco -> save_curwin ) ) { curwin = aco -> save_curwin ; } else { curwin = firstwin ; } prevwin = win_valid ( aco -> save_prevwin ) ? aco -> save_prevwin : firstwin ; vars_clear ( & aucmd_win -> w_vars -> dv_hashtab ) ; hash_init ( & aucmd_win -> w_vars -> dv_hashtab ) ; curbuf = curwin -> w_buffer ; xfree ( globaldir ) ; globaldir = aco -> globaldir ; check_cursor ( ) ; if ( curwin -> w_topline > curbuf -> b_ml . ml_line_count ) { curwin -> w_topline = curbuf -> b_ml . ml_line_count ; curwin -> w_topfill = 0 ; } } else { if ( win_valid ( aco -> save_curwin ) ) { if ( curwin == aco -> new_curwin && curbuf != aco -> new_curbuf . br_buf && bufref_valid ( & aco -> new_curbuf ) && aco -> new_curbuf . br_buf -> b_ml . ml_mfp != NULL ) { if ( curwin -> w_s == & curbuf -> b_s ) { curwin -> w_s = & aco -> new_curbuf . br_buf -> b_s ; } curbuf -> b_nwindows -- ; curbuf = aco -> new_curbuf . br_buf ; curwin -> w_buffer = curbuf ; curbuf -> b_nwindows ++ ; } curwin = aco -> save_curwin ; prevwin = win_valid ( aco -> save_prevwin ) ? aco -> save_prevwin : firstwin ; curbuf = curwin -> w_buffer ; check_cursor ( ) ; } } }
Cleanup after executing autocommands for a (hidden) buffer.
Cleanup after executing autocommands for a ( hidden ) buffer .
// Find "aucmd_win", it can't be closed, but it may be in another tab page. // Do not trigger autocommands here. // may need to remove last status line // no valid window in current tabpage // Hmm, original window disappeared.  Just use the first one. // window disappeared? // free all w: variables // re-use the hashtab // the buffer contents may have changed // restore curwin // Restore the buffer which was previously edited by curwin, if it was // changed, we are still the same window and the buffer is valid. // window disappeared? // In case the autocommand moves the cursor to a position that does not // exist in curbuf