vim-patch:7.4.2024

Problem:  More buf_valid() calls can be optimized.
Solution: Use bufref_valid() instead.

NOTE: Some changes related to channels and the Python and Netbeans interfaces
were obviously left out.

7c0a2f367f
This commit is contained in:
Marco Hinz
2017-01-09 14:35:04 +01:00
committed by James McCoy
parent e3b92c77da
commit c05e7f0fdd
14 changed files with 171 additions and 118 deletions

View File

@@ -94,7 +94,7 @@ open_buffer (
) )
{ {
int retval = OK; int retval = OK;
buf_T *old_curbuf; bufref_T old_curbuf;
long old_tw = curbuf->b_p_tw; long old_tw = curbuf->b_p_tw;
/* /*
@@ -136,10 +136,10 @@ open_buffer (
return FAIL; return FAIL;
} }
/* The autocommands in readfile() may change the buffer, but only AFTER // The autocommands in readfile() may change the buffer, but only AFTER
* reading the file. */ // reading the file.
old_curbuf = curbuf; set_bufref(&old_curbuf, curbuf);
modified_was_set = FALSE; modified_was_set = false;
/* mark cursor position as being invalid */ /* mark cursor position as being invalid */
curwin->w_valid = 0; curwin->w_valid = 0;
@@ -252,11 +252,11 @@ open_buffer (
* The autocommands may have changed the current buffer. Apply the * The autocommands may have changed the current buffer. Apply the
* modelines to the correct buffer, if it still exists and is loaded. * modelines to the correct buffer, if it still exists and is loaded.
*/ */
if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) { if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL) {
aco_save_T aco; aco_save_T aco;
/* Go to the buffer that was opened. */ // Go to the buffer that was opened.
aucmd_prepbuf(&aco, old_curbuf); aucmd_prepbuf(&aco, old_curbuf.br_buf);
do_modelines(0); do_modelines(0);
curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
@@ -720,7 +720,8 @@ void goto_buffer(exarg_T *eap, int start, int dir, int count)
{ {
(void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
start, dir, count, eap->forceit); start, dir, count, eap->forceit);
buf_T *old_curbuf = curbuf; bufref_T old_curbuf;
set_bufref(&old_curbuf, curbuf);
swap_exists_action = SEA_DIALOG; swap_exists_action = SEA_DIALOG;
if (swap_exists_action == SEA_QUIT && *eap->cmd == 's') { if (swap_exists_action == SEA_QUIT && *eap->cmd == 's') {
@@ -739,18 +740,20 @@ void goto_buffer(exarg_T *eap, int start, int dir, int count)
* new aborting error, interrupt, or uncaught exception. */ * new aborting error, interrupt, or uncaught exception. */
leave_cleanup(&cs); leave_cleanup(&cs);
} else { } else {
handle_swap_exists(old_curbuf); handle_swap_exists(&old_curbuf);
} }
} }
/* /// Handle the situation of swap_exists_action being set.
* Handle the situation of swap_exists_action being set. ///
* It is allowed for "old_curbuf" to be NULL or invalid. /// It is allowed for "old_curbuf" to be NULL or invalid.
*/ ///
void handle_swap_exists(buf_T *old_curbuf) /// @param old_curbuf The buffer to check for.
void handle_swap_exists(bufref_T *old_curbuf)
{ {
cleanup_T cs; cleanup_T cs;
long old_tw = curbuf->b_p_tw; long old_tw = curbuf->b_p_tw;
buf_T *buf;
if (swap_exists_action == SEA_QUIT) { if (swap_exists_action == SEA_QUIT) {
/* Reset the error/interrupt/exception state here so that /* Reset the error/interrupt/exception state here so that
@@ -763,14 +766,19 @@ void handle_swap_exists(buf_T *old_curbuf)
swap_exists_action = SEA_NONE; // don't want it again swap_exists_action = SEA_NONE; // don't want it again
swap_exists_did_quit = true; swap_exists_did_quit = true;
close_buffer(curwin, curbuf, DOBUF_UNLOAD, false); close_buffer(curwin, curbuf, DOBUF_UNLOAD, false);
if (!buf_valid(old_curbuf) || old_curbuf == curbuf) { if (old_curbuf == NULL
old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED); || !bufref_valid(old_curbuf)
|| old_curbuf->br_buf == curbuf) {
buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
} else {
buf = old_curbuf->br_buf;
} }
if (old_curbuf != NULL) { if (buf != NULL) {
enter_buffer(old_curbuf); enter_buffer(buf);
if (old_tw != curbuf->b_p_tw) if (old_tw != curbuf->b_p_tw) {
check_colorcolumn(curwin); check_colorcolumn(curwin);
} }
}
/* If "old_curbuf" is NULL we are in big trouble here... */ /* If "old_curbuf" is NULL we are in big trouble here... */
/* Restore the error/interrupt/exception state if not discarded by a /* Restore the error/interrupt/exception state if not discarded by a

View File

@@ -57,21 +57,22 @@ enum bfa_values {
static inline void switch_to_win_for_buf(buf_T *buf, static inline void switch_to_win_for_buf(buf_T *buf,
win_T **save_curwinp, win_T **save_curwinp,
tabpage_T **save_curtabp, tabpage_T **save_curtabp,
buf_T **save_curbufp) bufref_T *save_curbuf)
{ {
win_T *wp; win_T *wp;
tabpage_T *tp; tabpage_T *tp;
if (!find_win_for_buf(buf, &wp, &tp) if (!find_win_for_buf(buf, &wp, &tp)
|| switch_win(save_curwinp, save_curtabp, wp, tp, true) == FAIL) || switch_win(save_curwinp, save_curtabp, wp, tp, true) == FAIL) {
switch_buffer(save_curbufp, buf); switch_buffer(save_curbuf, buf);
}
} }
static inline void restore_win_for_buf(win_T *save_curwin, static inline void restore_win_for_buf(win_T *save_curwin,
tabpage_T *save_curtab, tabpage_T *save_curtab,
buf_T *save_curbuf) bufref_T *save_curbuf)
{ {
if (save_curbuf == NULL) { if (save_curbuf->br_buf == NULL) {
restore_win(save_curwin, save_curtab, true); restore_win(save_curwin, save_curtab, true);
} else { } else {
restore_buffer(save_curbuf); restore_buffer(save_curbuf);

View File

@@ -1004,7 +1004,8 @@ theend:
void ex_diffsplit(exarg_T *eap) void ex_diffsplit(exarg_T *eap)
{ {
win_T *old_curwin = curwin; win_T *old_curwin = curwin;
buf_T *old_curbuf = curbuf; bufref_T old_curbuf;
set_bufref(&old_curbuf, curbuf);
// don't use a new tab page, each tab page has its own diffs // don't use a new tab page, each tab page has its own diffs
cmdmod.tab = 0; cmdmod.tab = 0;
@@ -1022,10 +1023,10 @@ void ex_diffsplit(exarg_T *eap)
if (win_valid(old_curwin)) { if (win_valid(old_curwin)) {
diff_win_options(old_curwin, true); diff_win_options(old_curwin, true);
if (buf_valid(old_curbuf)) { if (bufref_valid(&old_curbuf)) {
// Move the cursor position to that of the old window. // Move the cursor position to that of the old window.
curwin->w_cursor.lnum = diff_get_corresponding_line( curwin->w_cursor.lnum = diff_get_corresponding_line(
old_curbuf, old_curbuf.br_buf,
old_curwin->w_cursor.lnum, old_curwin->w_cursor.lnum,
curbuf, curbuf,
curwin->w_cursor.lnum); curwin->w_cursor.lnum);

View File

@@ -2086,7 +2086,7 @@ int do_ecmd(
int did_set_swapcommand = FALSE; int did_set_swapcommand = FALSE;
buf_T *buf; buf_T *buf;
bufref_T bufref; bufref_T bufref;
buf_T *old_curbuf = curbuf; bufref_T old_curbuf;
char_u *free_fname = NULL; char_u *free_fname = NULL;
int retval = FAIL; int retval = FAIL;
long n; long n;
@@ -2103,6 +2103,8 @@ int do_ecmd(
if (eap != NULL) if (eap != NULL)
command = eap->do_ecmd_cmd; command = eap->do_ecmd_cmd;
set_bufref(&old_curbuf, curbuf);
if (fnum != 0) { if (fnum != 0) {
if (fnum == curbuf->b_fnum) /* file is already being edited */ if (fnum == curbuf->b_fnum) /* file is already being edited */
return OK; /* nothing to do */ return OK; /* nothing to do */
@@ -2214,7 +2216,7 @@ int do_ecmd(
if (oldwin != NULL) { if (oldwin != NULL) {
oldwin = curwin; oldwin = curwin;
} }
old_curbuf = curbuf; set_bufref(&old_curbuf, curbuf);
} }
if (buf == NULL) if (buf == NULL)
goto theend; goto theend;
@@ -2228,7 +2230,7 @@ int do_ecmd(
(void)buf_check_timestamp(buf, false); (void)buf_check_timestamp(buf, false);
// Check if autocommands made buffer invalid or changed the current // Check if autocommands made buffer invalid or changed the current
// buffer. // buffer.
if (!bufref_valid(&bufref) || curbuf != old_curbuf) { if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf) {
goto theend; goto theend;
} }
if (aborting()) { if (aborting()) {
@@ -2283,7 +2285,7 @@ int do_ecmd(
// Set the w_closing flag to avoid that autocommands close the window. // Set the w_closing flag to avoid that autocommands close the window.
the_curwin->w_closing = true; the_curwin->w_closing = true;
if (curbuf == old_curbuf) { if (curbuf == old_curbuf.br_buf) {
buf_copy_options(buf, BCO_ENTER); buf_copy_options(buf, BCO_ENTER);
} }
@@ -2487,7 +2489,7 @@ int do_ecmd(
if (swap_exists_action == SEA_QUIT) if (swap_exists_action == SEA_QUIT)
retval = FAIL; retval = FAIL;
handle_swap_exists(old_curbuf); handle_swap_exists(&old_curbuf);
} else { } else {
/* Read the modelines, but only to set window-local options. Any /* Read the modelines, but only to set window-local options. Any
* buffer-local options have already been set and may have been * buffer-local options have already been set and may have been

View File

@@ -1181,6 +1181,7 @@ bool prof_def_func(void)
int autowrite(buf_T *buf, int forceit) int autowrite(buf_T *buf, int forceit)
{ {
int r; int r;
bufref_T bufref;
if (!(p_aw || p_awa) || !p_write if (!(p_aw || p_awa) || !p_write
// never autowrite a "nofile" or "nowrite" buffer // never autowrite a "nofile" or "nowrite" buffer
@@ -1188,11 +1189,12 @@ int autowrite(buf_T *buf, int forceit)
|| (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) { || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) {
return FAIL; return FAIL;
} }
set_bufref(&bufref, buf);
r = buf_write_all(buf, forceit); r = buf_write_all(buf, forceit);
// Writing may succeed but the buffer still changed, e.g., when there is a // Writing may succeed but the buffer still changed, e.g., when there is a
// conversion error. We do want to return FAIL then. // conversion error. We do want to return FAIL then.
if (buf_valid(buf) && bufIsChanged(buf)) { if (bufref_valid(&bufref) && bufIsChanged(buf)) {
r = FAIL; r = FAIL;
} }
return r; return r;
@@ -1207,9 +1209,11 @@ void autowrite_all(void)
FOR_ALL_BUFFERS(buf) { FOR_ALL_BUFFERS(buf) {
if (bufIsChanged(buf) && !buf->b_p_ro) { if (bufIsChanged(buf) && !buf->b_p_ro) {
bufref_T bufref;
set_bufref(&bufref, buf);
(void)buf_write_all(buf, false); (void)buf_write_all(buf, false);
// an autocommand may have deleted the buffer // an autocommand may have deleted the buffer
if (!buf_valid(buf)) { if (!bufref_valid(&bufref)) {
buf = firstbuf; buf = firstbuf;
} }
} }
@@ -1221,6 +1225,8 @@ void autowrite_all(void)
bool check_changed(buf_T *buf, int flags) bool check_changed(buf_T *buf, int flags)
{ {
int forceit = (flags & CCGD_FORCEIT); int forceit = (flags & CCGD_FORCEIT);
bufref_T bufref;
set_bufref(&bufref, buf);
if (!forceit if (!forceit
&& bufIsChanged(buf) && bufIsChanged(buf)
@@ -1236,12 +1242,12 @@ bool check_changed(buf_T *buf, int flags)
} }
} }
} }
if (!buf_valid(buf)) { if (!bufref_valid(&bufref)) {
// Autocommand deleted buffer, oops! It's not changed now. // Autocommand deleted buffer, oops! It's not changed now.
return false; return false;
} }
dialog_changed(buf, count > 1); dialog_changed(buf, count > 1);
if (!buf_valid(buf)) { if (!bufref_valid(&bufref)) {
// Autocommand deleted buffer, oops! It's not changed now. // Autocommand deleted buffer, oops! It's not changed now.
return false; return false;
} }
@@ -1300,9 +1306,10 @@ void dialog_changed(buf_T *buf, int checkall)
// Skip readonly buffers, these need to be confirmed // Skip readonly buffers, these need to be confirmed
// individually. // individually.
FOR_ALL_BUFFERS(buf2) { FOR_ALL_BUFFERS(buf2) {
if (bufIsChanged(buf2) if (bufIsChanged(buf2) && (buf2->b_ffname != NULL) && !buf2->b_p_ro) {
&& (buf2->b_ffname != NULL) bufref_T bufref;
&& !buf2->b_p_ro) { set_bufref(&bufref, buf2);
if (buf2->b_fname != NULL if (buf2->b_fname != NULL
&& check_overwrite(&ea, buf2, buf2->b_fname, && check_overwrite(&ea, buf2, buf2->b_fname,
buf2->b_ffname, false) == OK) { buf2->b_ffname, false) == OK) {
@@ -1310,7 +1317,7 @@ void dialog_changed(buf_T *buf, int checkall)
(void)buf_write_all(buf2, false); (void)buf_write_all(buf2, false);
} }
// an autocommand may have deleted the buffer // an autocommand may have deleted the buffer
if (!buf_valid(buf2)) { if (!bufref_valid(&bufref)) {
buf2 = firstbuf; buf2 = firstbuf;
} }
} }
@@ -1407,11 +1414,14 @@ bool check_changed_any(bool hidden, bool unload)
continue; continue;
} }
if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf)) { if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf)) {
bufref_T bufref;
set_bufref(&bufref, buf);
// Try auto-writing the buffer. If this fails but the buffer no // Try auto-writing the buffer. If this fails but the buffer no
// longer exists it's not changed, that's OK. // longer exists it's not changed, that's OK.
if (check_changed(buf, (p_awa ? CCGD_AW : 0) if (check_changed(buf, (p_awa ? CCGD_AW : 0)
| CCGD_MULTWIN | CCGD_MULTWIN
| CCGD_ALLBUF) && buf_valid(buf)) { | CCGD_ALLBUF) && bufref_valid(&bufref)) {
break; // didn't save - still changes break; // didn't save - still changes
} }
} }
@@ -1447,9 +1457,11 @@ bool check_changed_any(bool hidden, bool unload)
if (buf != curbuf) { if (buf != curbuf) {
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
if (wp->w_buffer == buf) { if (wp->w_buffer == buf) {
bufref_T bufref;
set_bufref(&bufref, buf);
goto_tabpage_win(tp, wp); goto_tabpage_win(tp, wp);
// Paranoia: did autocmds wipe out the buffer with changes? // Paranoia: did autocmds wipe out the buffer with changes?
if (!buf_valid(buf)) { if (!bufref_valid(&bufref)) {
goto theend; goto theend;
} }
goto buf_found; goto buf_found;

View File

@@ -5914,10 +5914,13 @@ ex_win_close (
need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
if (need_hide && !P_HID(buf) && !forceit) { if (need_hide && !P_HID(buf) && !forceit) {
if ((p_confirm || cmdmod.confirm) && p_write) { if ((p_confirm || cmdmod.confirm) && p_write) {
dialog_changed(buf, FALSE); bufref_T bufref;
if (buf_valid(buf) && bufIsChanged(buf)) set_bufref(&bufref, buf);
dialog_changed(buf, false);
if (bufref_valid(&bufref) && bufIsChanged(buf)) {
return; return;
need_hide = FALSE; }
need_hide = false;
} else { } else {
EMSG(_(e_nowrtmsg)); EMSG(_(e_nowrtmsg));
return; return;

View File

@@ -5134,9 +5134,9 @@ int cmd_gchar(int offset)
static int ex_window(void) static int ex_window(void)
{ {
struct cmdline_info save_ccline; struct cmdline_info save_ccline;
buf_T *old_curbuf = curbuf; bufref_T old_curbuf;
bufref_T bufref;
win_T *old_curwin = curwin; win_T *old_curwin = curwin;
buf_T *bp;
win_T *wp; win_T *wp;
int i; int i;
linenr_T lnum; linenr_T lnum;
@@ -5155,6 +5155,8 @@ static int ex_window(void)
return K_IGNORE; return K_IGNORE;
} }
set_bufref(&old_curbuf, curbuf);
/* Save current window sizes. */ /* Save current window sizes. */
win_size_save(&winsizes); win_size_save(&winsizes);
@@ -5269,7 +5271,7 @@ static int ex_window(void)
/* Safety check: The old window or buffer was deleted: It's a bug when /* Safety check: The old window or buffer was deleted: It's a bug when
* this happens! */ * this happens! */
if (!win_valid(old_curwin) || !buf_valid(old_curbuf)) { if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) {
cmdwin_result = Ctrl_C; cmdwin_result = Ctrl_C;
EMSG(_("E199: Active window or buffer deleted")); EMSG(_("E199: Active window or buffer deleted"));
} else { } else {
@@ -5320,14 +5322,15 @@ static int ex_window(void)
// Avoid command-line window first character being concealed // Avoid command-line window first character being concealed
curwin->w_p_cole = 0; curwin->w_p_cole = 0;
wp = curwin; wp = curwin;
bp = curbuf; set_bufref(&bufref, curbuf);
win_goto(old_curwin); win_goto(old_curwin);
win_close(wp, TRUE); win_close(wp, TRUE);
/* win_close() may have already wiped the buffer when 'bh' is // win_close() may have already wiped the buffer when 'bh' is
* set to 'wipe' */ // set to 'wipe'.
if (buf_valid(bp)) if (bufref_valid(&bufref)) {
close_buffer(NULL, bp, DOBUF_WIPE, FALSE); close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, false);
}
/* Restore window sizes. */ /* Restore window sizes. */
win_size_restore(&winsizes); win_size_restore(&winsizes);

View File

@@ -2402,6 +2402,7 @@ buf_write (
int did_cmd = FALSE; int did_cmd = FALSE;
int nofile_err = FALSE; int nofile_err = FALSE;
int empty_memline = (buf->b_ml.ml_mfp == NULL); int empty_memline = (buf->b_ml.ml_mfp == NULL);
bufref_T bufref;
/* /*
* Apply PRE autocommands. * Apply PRE autocommands.
@@ -2417,8 +2418,9 @@ buf_write (
if (fname == buf->b_sfname) if (fname == buf->b_sfname)
buf_fname_s = TRUE; buf_fname_s = TRUE;
/* set curwin/curbuf to buf and save a few things */ // Set curwin/curbuf to buf and save a few things.
aucmd_prepbuf(&aco, buf); aucmd_prepbuf(&aco, buf);
set_bufref(&bufref, buf);
if (append) { if (append) {
if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
@@ -2466,14 +2468,13 @@ buf_write (
/* restore curwin/curbuf and a few other things */ /* restore curwin/curbuf and a few other things */
aucmd_restbuf(&aco); aucmd_restbuf(&aco);
/* // In three situations we return here and don't write the file:
* In three situations we return here and don't write the file: // 1. the autocommands deleted or unloaded the buffer.
* 1. the autocommands deleted or unloaded the buffer. // 2. The autocommands abort script processing.
* 2. The autocommands abort script processing. // 3. If one of the "Cmd" autocommands was executed.
* 3. If one of the "Cmd" autocommands was executed. if (!bufref_valid(&bufref)) {
*/
if (!buf_valid(buf))
buf = NULL; buf = NULL;
}
if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
|| did_cmd || nofile_err || did_cmd || nofile_err
|| aborting() || aborting()
@@ -4760,12 +4761,14 @@ check_timestamps (
for (buf = firstbuf; buf != NULL; ) { for (buf = firstbuf; buf != NULL; ) {
/* Only check buffers in a window. */ /* Only check buffers in a window. */
if (buf->b_nwindows > 0) { if (buf->b_nwindows > 0) {
bufref_T bufref;
set_bufref(&bufref, buf);
n = buf_check_timestamp(buf, focus); n = buf_check_timestamp(buf, focus);
if (didit < n) if (didit < n) {
didit = n; didit = n;
if (n > 0 && !buf_valid(buf)) { }
/* Autocommands have removed the buffer, start at the if (n > 0 && !bufref_valid(&bufref)) {
* first one again. */ // Autocommands have removed the buffer, start at the first one again.
buf = firstbuf; buf = firstbuf;
continue; continue;
} }
@@ -4850,6 +4853,9 @@ buf_check_timestamp (
char_u *s; char_u *s;
char *reason; char *reason;
bufref_T bufref;
set_bufref(&bufref, buf);
// If its a terminal, there is no file name, the buffer is not loaded, // If its a terminal, there is no file name, the buffer is not loaded,
// 'buftype' is set, we are in the middle of a save or being called // 'buftype' is set, we are in the middle of a save or being called
// recursively: ignore this buffer. // recursively: ignore this buffer.
@@ -4919,8 +4925,9 @@ buf_check_timestamp (
allbuf_lock--; allbuf_lock--;
busy = false; busy = false;
if (n) { if (n) {
if (!buf_valid(buf)) if (!bufref_valid(&bufref)) {
EMSG(_("E246: FileChangedShell autocommand deleted buffer")); EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
}
s = get_vim_var_str(VV_FCS_CHOICE); s = get_vim_var_str(VV_FCS_CHOICE);
if (STRCMP(s, "reload") == 0 && *reason != 'd') if (STRCMP(s, "reload") == 0 && *reason != 'd')
reload = TRUE; reload = TRUE;
@@ -5037,11 +5044,11 @@ buf_check_timestamp (
} }
} }
/* Trigger FileChangedShell when the file was changed in any way. */ // Trigger FileChangedShell when the file was changed in any way.
if (buf_valid(buf) && retval != 0) if (bufref_valid(&bufref) && retval != 0) {
(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, buf->b_fname, buf->b_fname,
buf->b_fname, buf->b_fname, FALSE, buf); false, buf);
}
return retval; return retval;
} }
@@ -5058,6 +5065,7 @@ void buf_reload(buf_T *buf, int orig_mode)
linenr_T old_topline; linenr_T old_topline;
int old_ro = buf->b_p_ro; int old_ro = buf->b_p_ro;
buf_T *savebuf; buf_T *savebuf;
bufref_T bufref;
int saved = OK; int saved = OK;
aco_save_T aco; aco_save_T aco;
int flags = READ_NEW; int flags = READ_NEW;
@@ -5093,6 +5101,7 @@ void buf_reload(buf_T *buf, int orig_mode)
} else { } else {
// Allocate a buffer without putting it in the buffer list. // Allocate a buffer without putting it in the buffer list.
savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
set_bufref(&bufref, savebuf);
if (savebuf != NULL && buf == curbuf) { if (savebuf != NULL && buf == curbuf) {
/* Open the memline. */ /* Open the memline. */
curbuf = savebuf; curbuf = savebuf;
@@ -5117,7 +5126,7 @@ void buf_reload(buf_T *buf, int orig_mode)
if (!aborting()) { if (!aborting()) {
EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
} }
if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) { if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) {
/* Put the text back from the save buffer. First /* Put the text back from the save buffer. First
* delete any lines that readfile() added. */ * delete any lines that readfile() added. */
while (!bufempty()) while (!bufempty())
@@ -5139,8 +5148,9 @@ void buf_reload(buf_T *buf, int orig_mode)
} }
xfree(ea.cmd); xfree(ea.cmd);
if (savebuf != NULL && buf_valid(savebuf)) if (savebuf != NULL && bufref_valid(&bufref)) {
wipe_buffer(savebuf, FALSE); wipe_buffer(savebuf, false);
}
/* Invalidate diff info if necessary. */ /* Invalidate diff info if necessary. */
diff_invalidate(curbuf); diff_invalidate(curbuf);
@@ -6288,6 +6298,7 @@ void ex_doautoall(exarg_T *eap)
aco_save_T aco; aco_save_T aco;
char_u *arg = eap->arg; char_u *arg = eap->arg;
int call_do_modelines = check_nomodeline(&arg); int call_do_modelines = check_nomodeline(&arg);
bufref_T bufref;
/* /*
* This is a bit tricky: For some commands curwin->w_buffer needs to be * This is a bit tricky: For some commands curwin->w_buffer needs to be
@@ -6300,8 +6311,9 @@ void ex_doautoall(exarg_T *eap)
if (buf->b_ml.ml_mfp == NULL) { if (buf->b_ml.ml_mfp == NULL) {
continue; continue;
} }
/* find a window for this buffer and save some values */ // Find a window for this buffer and save some values.
aucmd_prepbuf(&aco, buf); aucmd_prepbuf(&aco, buf);
set_bufref(&bufref, buf);
bool did_aucmd; bool did_aucmd;
// execute the autocommands for this buffer // execute the autocommands for this buffer
@@ -6318,7 +6330,7 @@ void ex_doautoall(exarg_T *eap)
aucmd_restbuf(&aco); aucmd_restbuf(&aco);
/* stop if there is some error or buffer was deleted */ /* stop if there is some error or buffer was deleted */
if (retval == FAIL || !buf_valid(buf)) if (retval == FAIL || !bufref_valid(&bufref))
break; break;
} }
@@ -6426,7 +6438,7 @@ aucmd_prepbuf (
} }
curbuf = buf; curbuf = buf;
aco->new_curwin = curwin; aco->new_curwin = curwin;
aco->new_curbuf = curbuf; set_bufref(&aco->new_curbuf, curbuf);
} }
/// Cleanup after executing autocommands for a (hidden) buffer. /// Cleanup after executing autocommands for a (hidden) buffer.
@@ -6489,14 +6501,14 @@ win_found:
// Restore the buffer which was previously edited by curwin, if it was // Restore the buffer which was previously edited by curwin, if it was
// changed, we are still the same window and the buffer is valid. // changed, we are still the same window and the buffer is valid.
if (curwin == aco->new_curwin if (curwin == aco->new_curwin
&& curbuf != aco->new_curbuf && curbuf != aco->new_curbuf.br_buf
&& buf_valid(aco->new_curbuf) && bufref_valid(&aco->new_curbuf)
&& aco->new_curbuf->b_ml.ml_mfp != NULL) { && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) {
if (curwin->w_s == &curbuf->b_s) { if (curwin->w_s == &curbuf->b_s) {
curwin->w_s = &aco->new_curbuf->b_s; curwin->w_s = &aco->new_curbuf.br_buf->b_s;
} }
curbuf->b_nwindows--; curbuf->b_nwindows--;
curbuf = aco->new_curbuf; curbuf = aco->new_curbuf.br_buf;
curwin->w_buffer = curbuf; curwin->w_buffer = curbuf;
curbuf->b_nwindows++; curbuf->b_nwindows++;
} }

View File

@@ -23,7 +23,7 @@ typedef struct {
int use_aucmd_win; /* using aucmd_win */ int use_aucmd_win; /* using aucmd_win */
win_T *save_curwin; /* saved curwin */ win_T *save_curwin; /* saved curwin */
win_T *new_curwin; /* new curwin */ win_T *new_curwin; /* new curwin */
buf_T *new_curbuf; /* new curbuf */ bufref_T new_curbuf; /* new curbuf */
char_u *globaldir; /* saved value of globaldir */ char_u *globaldir; /* saved value of globaldir */
} aco_save_T; } aco_save_T;

View File

@@ -590,12 +590,15 @@ void getout(int exitval)
/* Trigger BufUnload for buffers that are loaded */ /* Trigger BufUnload for buffers that are loaded */
FOR_ALL_BUFFERS(buf) { FOR_ALL_BUFFERS(buf) {
if (buf->b_ml.ml_mfp != NULL) { if (buf->b_ml.ml_mfp != NULL) {
apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, bufref_T bufref;
FALSE, buf); set_bufref(&bufref, buf);
if (!buf_valid(buf)) /* autocmd may delete the buffer */ apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, false, buf);
if (!bufref_valid(&bufref)) {
// Autocmd deleted the buffer.
break; break;
} }
} }
}
apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
} }

View File

@@ -629,12 +629,12 @@ void free_all_mem(void)
* were freed already. */ * were freed already. */
p_acd = false; p_acd = false;
for (buf = firstbuf; buf != NULL; ) { for (buf = firstbuf; buf != NULL; ) {
bufref_T bufref;
set_bufref(&bufref, buf);
nextbuf = buf->b_next; nextbuf = buf->b_next;
close_buffer(NULL, buf, DOBUF_WIPE, false); close_buffer(NULL, buf, DOBUF_WIPE, false);
if (buf_valid(buf)) // Didn't work, try next one.
buf = nextbuf; /* didn't work, try next one */ buf = bufref_valid(&bufref) ? nextbuf : firstbuf;
else
buf = firstbuf;
} }
free_cmdline_buf(); free_cmdline_buf();

View File

@@ -3734,7 +3734,8 @@ load_dummy_buffer (
) )
{ {
buf_T *newbuf; buf_T *newbuf;
buf_T *newbuf_to_wipe = NULL; bufref_T newbufref;
bufref_T newbuf_to_wipe;
int failed = TRUE; int failed = TRUE;
aco_save_T aco; aco_save_T aco;
@@ -3743,6 +3744,7 @@ load_dummy_buffer (
if (newbuf == NULL) { if (newbuf == NULL) {
return NULL; return NULL;
} }
set_bufref(&newbufref, newbuf);
/* Init the options. */ /* Init the options. */
buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
@@ -3762,6 +3764,7 @@ load_dummy_buffer (
* work. */ * work. */
curbuf->b_flags &= ~BF_DUMMY; curbuf->b_flags &= ~BF_DUMMY;
newbuf_to_wipe.br_buf = NULL;
if (readfile(fname, NULL, if (readfile(fname, NULL,
(linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
NULL, READ_NEW | READ_DUMMY) == OK NULL, READ_NEW | READ_DUMMY) == OK
@@ -3769,19 +3772,19 @@ load_dummy_buffer (
&& !(curbuf->b_flags & BF_NEW)) { && !(curbuf->b_flags & BF_NEW)) {
failed = FALSE; failed = FALSE;
if (curbuf != newbuf) { if (curbuf != newbuf) {
/* Bloody autocommands changed the buffer! Can happen when // Bloody autocommands changed the buffer! Can happen when
* using netrw and editing a remote file. Use the current // using netrw and editing a remote file. Use the current
* buffer instead, delete the dummy one after restoring the // buffer instead, delete the dummy one after restoring the
* window stuff. */ // window stuff.
newbuf_to_wipe = newbuf; set_bufref(&newbuf_to_wipe, newbuf);
newbuf = curbuf; newbuf = curbuf;
} }
} }
// Restore curwin/curbuf and a few other things. // Restore curwin/curbuf and a few other things.
aucmd_restbuf(&aco); aucmd_restbuf(&aco);
if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe)) { if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) {
wipe_buffer(newbuf_to_wipe, false); wipe_buffer(newbuf_to_wipe.br_buf, false);
} }
// Add back the "dummy" flag, otherwise buflist_findname_file_id() // Add back the "dummy" flag, otherwise buflist_findname_file_id()
@@ -3797,8 +3800,9 @@ load_dummy_buffer (
os_dirname(resulting_dir, MAXPATHL); os_dirname(resulting_dir, MAXPATHL);
restore_start_dir(dirname_start); restore_start_dir(dirname_start);
if (!buf_valid(newbuf)) if (!bufref_valid(&newbufref)) {
return NULL; return NULL;
}
if (failed) { if (failed) {
wipe_dummy_buffer(newbuf, dirname_start); wipe_dummy_buffer(newbuf, dirname_start);
return NULL; return NULL;

View File

@@ -3746,6 +3746,9 @@ char_u *did_set_spelllang(win_T *wp)
char_u *ret_msg = NULL; char_u *ret_msg = NULL;
char_u *spl_copy; char_u *spl_copy;
bufref_T bufref;
set_bufref(&bufref, wp->w_buffer);
// We don't want to do this recursively. May happen when a language is // We don't want to do this recursively. May happen when a language is
// not available and the SpellFileMissing autocommand opens a new buffer // not available and the SpellFileMissing autocommand opens a new buffer
// in which 'spell' is set. // in which 'spell' is set.
@@ -3824,7 +3827,7 @@ char_u *did_set_spelllang(win_T *wp)
spell_load_lang(lang); spell_load_lang(lang);
// SpellFileMissing autocommands may do anything, including // SpellFileMissing autocommands may do anything, including
// destroying the buffer we are using... // destroying the buffer we are using...
if (!buf_valid(wp->w_buffer)) { if (!bufref_valid(&bufref)) {
ret_msg = ret_msg =
(char_u *)"E797: SpellFileMissing autocommand deleted buffer"; (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
goto theend; goto theend;
@@ -13037,8 +13040,9 @@ void ex_spelldump(exarg_T *eap)
set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
xfree(spl); xfree(spl);
if (!bufempty() || !buf_valid(curbuf)) if (!bufempty()) {
return; return;
}
spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);

View File

@@ -1945,6 +1945,8 @@ int win_close(win_T *win, int free_buf)
* Close the link to the buffer. * Close the link to the buffer.
*/ */
if (win->w_buffer != NULL) { if (win->w_buffer != NULL) {
bufref_T bufref;
set_bufref(&bufref, curbuf);
win->w_closing = true; win->w_closing = true;
close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, true); close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, true);
if (win_valid_any_tab(win)) { if (win_valid_any_tab(win)) {
@@ -1953,7 +1955,7 @@ int win_close(win_T *win, int free_buf)
// Make sure curbuf is valid. It can become invalid if 'bufhidden' is // Make sure curbuf is valid. It can become invalid if 'bufhidden' is
// "wipe". // "wipe".
if (!buf_valid(curbuf)) { if (!bufref_valid(&bufref)) {
curbuf = firstbuf; curbuf = firstbuf;
} }
} }
@@ -5401,31 +5403,29 @@ void restore_win(win_T *save_curwin, tabpage_T *save_curtab, int no_display)
unblock_autocmds(); unblock_autocmds();
} }
/* /// Make "buf" the current buffer.
* Make "buf" the current buffer. restore_buffer() MUST be called to undo. ///
* No autocommands will be executed. Use aucmd_prepbuf() if there are any. /// restore_buffer() MUST be called to undo.
*/ /// No autocommands will be executed. Use aucmd_prepbuf() if there are any.
void switch_buffer(buf_T **save_curbuf, buf_T *buf) void switch_buffer(bufref_T *save_curbuf, buf_T *buf)
{ {
block_autocmds(); block_autocmds();
*save_curbuf = curbuf; set_bufref(save_curbuf, curbuf);
--curbuf->b_nwindows; --curbuf->b_nwindows;
curbuf = buf; curbuf = buf;
curwin->w_buffer = buf; curwin->w_buffer = buf;
++curbuf->b_nwindows; ++curbuf->b_nwindows;
} }
/* /// Restore the current buffer after using switch_buffer().
* Restore the current buffer after using switch_buffer(). void restore_buffer(bufref_T *save_curbuf)
*/
void restore_buffer(buf_T *save_curbuf)
{ {
unblock_autocmds(); unblock_autocmds();
/* Check for valid buffer, just in case. */ // Check for valid buffer, just in case.
if (buf_valid(save_curbuf)) { if (bufref_valid(save_curbuf)) {
--curbuf->b_nwindows; --curbuf->b_nwindows;
curwin->w_buffer = save_curbuf; curwin->w_buffer = save_curbuf->br_buf;
curbuf = save_curbuf; curbuf = save_curbuf->br_buf;
++curbuf->b_nwindows; ++curbuf->b_nwindows;
} }
} }