To: vim_dev@googlegroups.com Subject: Patch 7.4.2024 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2024 Problem: More buf_valid() calls can be optimized. Solution: Use bufref_valid() instead. Files: src/buffer.c, src/ex_cmds.c, src/structs.h, src/channel.c, src/diff.c, src/eval.c, src/ex_cmds2.c, src/ex_docmd.c, src/ex_getln.c, src/fileio.c, src/main.c, src/misc2.c, src/netbeans.c, src/quickfix.c, src/spell.c, src/term.c, src/if_py_both.h, src/window.c, src/proto/buffer.pro, src/proto/window.pro *** ../vim-7.4.2023/src/buffer.c 2016-07-10 20:27:27.423946553 +0200 --- src/buffer.c 2016-07-10 20:36:53.963431243 +0200 *************** *** 83,89 **** { int retval = OK; #ifdef FEAT_AUTOCMD ! buf_T *old_curbuf; #endif #ifdef FEAT_SYN_HL long old_tw = curbuf->b_p_tw; --- 83,89 ---- { int retval = OK; #ifdef FEAT_AUTOCMD ! bufref_T old_curbuf; #endif #ifdef FEAT_SYN_HL long old_tw = curbuf->b_p_tw; *************** *** 129,135 **** #ifdef FEAT_AUTOCMD /* The autocommands in readfile() may change the buffer, but only AFTER * reading the file. */ ! old_curbuf = curbuf; modified_was_set = FALSE; #endif --- 129,135 ---- #ifdef FEAT_AUTOCMD /* The autocommands in readfile() may change the buffer, but only AFTER * reading the file. */ ! set_bufref(&old_curbuf, curbuf); modified_was_set = FALSE; #endif *************** *** 284,295 **** * The autocommands may have changed the current buffer. Apply the * modelines to the correct buffer, if it still exists and is loaded. */ ! if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) { aco_save_T aco; /* Go to the buffer that was opened. */ ! aucmd_prepbuf(&aco, old_curbuf); #endif do_modelines(0); curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); --- 284,295 ---- * The autocommands may have changed the current buffer. Apply the * modelines to the correct buffer, if it still exists and is loaded. */ ! if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL) { aco_save_T aco; /* Go to the buffer that was opened. */ ! aucmd_prepbuf(&aco, old_curbuf.br_buf); #endif do_modelines(0); curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); *************** *** 809,815 **** int count) { # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) ! buf_T *old_curbuf = curbuf; swap_exists_action = SEA_DIALOG; # endif --- 809,817 ---- int count) { # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) ! bufref_T old_curbuf; ! ! set_bufref(&old_curbuf, curbuf); swap_exists_action = SEA_DIALOG; # endif *************** *** 838,844 **** # endif } else ! handle_swap_exists(old_curbuf); # endif } #endif --- 840,846 ---- # endif } else ! handle_swap_exists(&old_curbuf); # endif } #endif *************** *** 849,855 **** * It is allowed for "old_curbuf" to be NULL or invalid. */ void ! handle_swap_exists(buf_T *old_curbuf) { # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) cleanup_T cs; --- 851,857 ---- * It is allowed for "old_curbuf" to be NULL or invalid. */ void ! handle_swap_exists(bufref_T *old_curbuf) { # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) cleanup_T cs; *************** *** 857,862 **** --- 859,865 ---- #ifdef FEAT_SYN_HL long old_tw = curbuf->b_p_tw; #endif + buf_T *buf; if (swap_exists_action == SEA_QUIT) { *************** *** 872,882 **** swap_exists_action = SEA_NONE; /* don't want it again */ swap_exists_did_quit = TRUE; close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE); ! if (!buf_valid(old_curbuf) || old_curbuf == curbuf) ! old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED); ! if (old_curbuf != NULL) { ! enter_buffer(old_curbuf); #ifdef FEAT_SYN_HL if (old_tw != curbuf->b_p_tw) check_colorcolumn(curwin); --- 875,888 ---- swap_exists_action = SEA_NONE; /* don't want it again */ swap_exists_did_quit = TRUE; close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE); ! if (old_curbuf == NULL || !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 (buf != NULL) { ! enter_buffer(buf); #ifdef FEAT_SYN_HL if (old_tw != curbuf->b_p_tw) check_colorcolumn(curwin); *** ../vim-7.4.2023/src/ex_cmds.c 2016-07-10 19:03:47.699212552 +0200 --- src/ex_cmds.c 2016-07-10 20:39:58.872652801 +0200 *************** *** 3666,3672 **** buf_T *buf; bufref_T bufref; #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) ! buf_T *old_curbuf = curbuf; #endif char_u *free_fname = NULL; #ifdef FEAT_BROWSE --- 3666,3672 ---- buf_T *buf; bufref_T bufref; #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) ! bufref_T old_curbuf; #endif char_u *free_fname = NULL; #ifdef FEAT_BROWSE *************** *** 3691,3696 **** --- 3691,3699 ---- if (eap != NULL) command = eap->do_ecmd_cmd; + #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) + set_bufref(&old_curbuf, curbuf); + #endif if (fnum != 0) { *************** *** 3857,3863 **** /* autocommands may change curwin and curbuf */ if (oldwin != NULL) oldwin = curwin; ! old_curbuf = curbuf; #endif } if (buf == NULL) --- 3860,3866 ---- /* autocommands may change curwin and curbuf */ if (oldwin != NULL) oldwin = curwin; ! set_bufref(&old_curbuf, curbuf); #endif } if (buf == NULL) *************** *** 3875,3881 **** * buffer. */ if (!bufref_valid(&bufref) #ifdef FEAT_AUTOCMD ! || curbuf != old_curbuf #endif ) goto theend; --- 3878,3884 ---- * buffer. */ if (!bufref_valid(&bufref) #ifdef FEAT_AUTOCMD ! || curbuf != old_curbuf.br_buf #endif ) goto theend; *************** *** 3934,3940 **** auto_buf = TRUE; else { ! if (curbuf == old_curbuf) #endif buf_copy_options(buf, BCO_ENTER); --- 3937,3943 ---- auto_buf = TRUE; else { ! if (curbuf == old_curbuf.br_buf) #endif buf_copy_options(buf, BCO_ENTER); *************** *** 4196,4202 **** #if defined(HAS_SWAP_EXISTS_ACTION) if (swap_exists_action == SEA_QUIT) retval = FAIL; ! handle_swap_exists(old_curbuf); #endif } #ifdef FEAT_AUTOCMD --- 4199,4205 ---- #if defined(HAS_SWAP_EXISTS_ACTION) if (swap_exists_action == SEA_QUIT) retval = FAIL; ! handle_swap_exists(&old_curbuf); #endif } #ifdef FEAT_AUTOCMD *** ../vim-7.4.2023/src/structs.h 2016-07-10 18:21:45.724884205 +0200 --- src/structs.h 2016-07-10 21:23:43.885300214 +0200 *************** *** 1431,1437 **** char_u *ch_callback; /* call when a msg is not handled */ partial_T *ch_partial; ! buf_T *ch_buffer; /* buffer to read from or write to */ int ch_nomodifiable; /* TRUE when buffer can be 'nomodifiable' */ int ch_nomod_error; /* TRUE when e_modifiable was given */ int ch_buf_append; /* write appended lines instead top-bot */ --- 1431,1437 ---- char_u *ch_callback; /* call when a msg is not handled */ partial_T *ch_partial; ! bufref_T ch_bufref; /* buffer to read from or write to */ int ch_nomodifiable; /* TRUE when buffer can be 'nomodifiable' */ int ch_nomod_error; /* TRUE when e_modifiable was given */ int ch_buf_append; /* write appended lines instead top-bot */ *************** *** 2925,2931 **** int use_aucmd_win; /* using aucmd_win */ win_T *save_curwin; /* saved curwin */ win_T *new_curwin; /* new curwin */ ! buf_T *new_curbuf; /* new curbuf */ char_u *globaldir; /* saved value of globaldir */ #endif } aco_save_T; --- 2925,2931 ---- int use_aucmd_win; /* using aucmd_win */ win_T *save_curwin; /* saved curwin */ win_T *new_curwin; /* new curwin */ ! bufref_T new_curbuf; /* new curbuf */ char_u *globaldir; /* saved value of globaldir */ #endif } aco_save_T; *** ../vim-7.4.2023/src/channel.c 2016-07-07 20:45:00.740424639 +0200 --- src/channel.c 2016-07-10 20:52:13.293625314 +0200 *************** *** 362,368 **** return TRUE; /* If reading from or a buffer it's still useful. */ ! if (channel->ch_part[PART_IN].ch_buffer != NULL) return TRUE; /* If there is no callback then nobody can get readahead. If the fd is --- 362,368 ---- return TRUE; /* If reading from or a buffer it's still useful. */ ! if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL) return TRUE; /* If there is no callback then nobody can get readahead. If the fd is *************** *** 379,387 **** return (channel->ch_callback != NULL && (has_sock_msg || has_out_msg || has_err_msg)) || ((channel->ch_part[PART_OUT].ch_callback != NULL ! || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg) || ((channel->ch_part[PART_ERR].ch_callback != NULL ! || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg); } /* --- 379,389 ---- return (channel->ch_callback != NULL && (has_sock_msg || has_out_msg || has_err_msg)) || ((channel->ch_part[PART_OUT].ch_callback != NULL ! || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL) ! && has_out_msg) || ((channel->ch_part[PART_ERR].ch_callback != NULL ! || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL) ! && has_err_msg); } /* *************** *** 1046,1064 **** { chanpart_T *in_part = &channel->ch_part[PART_IN]; ! in_part->ch_buffer = job->jv_in_buf; ch_logs(channel, "reading from buffer '%s'", ! (char *)in_part->ch_buffer->b_ffname); if (options->jo_set & JO_IN_TOP) { if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT)) { /* Special mode: send last-but-one line when appending a line * to the buffer. */ ! in_part->ch_buffer->b_write_to_channel = TRUE; in_part->ch_buf_append = TRUE; in_part->ch_buf_top = ! in_part->ch_buffer->b_ml.ml_line_count + 1; } else in_part->ch_buf_top = options->jo_in_top; --- 1048,1066 ---- { chanpart_T *in_part = &channel->ch_part[PART_IN]; ! set_bufref(&in_part->ch_bufref, job->jv_in_buf); ch_logs(channel, "reading from buffer '%s'", ! (char *)in_part->ch_bufref.br_buf->b_ffname); if (options->jo_set & JO_IN_TOP) { if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT)) { /* Special mode: send last-but-one line when appending a line * to the buffer. */ ! in_part->ch_bufref.br_buf->b_write_to_channel = TRUE; in_part->ch_buf_append = TRUE; in_part->ch_buf_top = ! in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1; } else in_part->ch_buf_top = options->jo_in_top; *************** *** 1068,1074 **** if (options->jo_set & JO_IN_BOT) in_part->ch_buf_bot = options->jo_in_bot; else ! in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count; } } --- 1070,1076 ---- if (options->jo_set & JO_IN_BOT) in_part->ch_buf_bot = options->jo_in_bot; else ! in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count; } } *************** *** 1229,1235 **** { ch_logs(channel, "writing out to buffer '%s'", (char *)buf->b_ffname); ! channel->ch_part[PART_OUT].ch_buffer = buf; } } } --- 1231,1237 ---- { ch_logs(channel, "writing out to buffer '%s'", (char *)buf->b_ffname); ! set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf); } } } *************** *** 1244,1250 **** if (!(opt->jo_set & JO_ERR_MODE)) channel->ch_part[PART_ERR].ch_mode = MODE_NL; if (opt->jo_io[PART_ERR] == JIO_OUT) ! buf = channel->ch_part[PART_OUT].ch_buffer; else if (opt->jo_set & JO_ERR_BUF) { buf = buflist_findnr(opt->jo_io_buf[PART_ERR]); --- 1246,1252 ---- if (!(opt->jo_set & JO_ERR_MODE)) channel->ch_part[PART_ERR].ch_mode = MODE_NL; if (opt->jo_io[PART_ERR] == JIO_OUT) ! buf = channel->ch_part[PART_OUT].ch_bufref.br_buf; else if (opt->jo_set & JO_ERR_BUF) { buf = buflist_findnr(opt->jo_io_buf[PART_ERR]); *************** *** 1266,1272 **** { ch_logs(channel, "writing err to buffer '%s'", (char *)buf->b_ffname); ! channel->ch_part[PART_ERR].ch_buffer = buf; } } } --- 1268,1274 ---- { ch_logs(channel, "writing err to buffer '%s'", (char *)buf->b_ffname); ! set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf); } } } *************** *** 1407,1421 **** { chanpart_T *in_part = &channel->ch_part[PART_IN]; linenr_T lnum; ! buf_T *buf = in_part->ch_buffer; int written = 0; if (buf == NULL || in_part->ch_buf_append) return; /* no buffer or using appending */ ! if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL) { /* buffer was wiped out or unloaded */ ! in_part->ch_buffer = NULL; return; } --- 1409,1423 ---- { chanpart_T *in_part = &channel->ch_part[PART_IN]; linenr_T lnum; ! buf_T *buf = in_part->ch_bufref.br_buf; int written = 0; if (buf == NULL || in_part->ch_buf_append) return; /* no buffer or using appending */ ! if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL) { /* buffer was wiped out or unloaded */ ! in_part->ch_bufref.br_buf = NULL; return; } *************** *** 1437,1443 **** if (lnum > buf->b_ml.ml_line_count) { /* Writing is done, no longer need the buffer. */ ! in_part->ch_buffer = NULL; ch_log(channel, "Finished writing all lines to channel"); } else --- 1439,1445 ---- if (lnum > buf->b_ml.ml_line_count) { /* Writing is done, no longer need the buffer. */ ! in_part->ch_bufref.br_buf = NULL; ch_log(channel, "Finished writing all lines to channel"); } else *************** *** 1459,1469 **** { chanpart_T *ch_part = &channel->ch_part[part]; ! if (ch_part->ch_buffer == buf) { ch_logs(channel, "%s buffer has been wiped out", part_names[part]); ! ch_part->ch_buffer = NULL; } } } --- 1461,1471 ---- { chanpart_T *ch_part = &channel->ch_part[part]; ! if (ch_part->ch_bufref.br_buf == buf) { ch_logs(channel, "%s buffer has been wiped out", part_names[part]); ! ch_part->ch_bufref.br_buf = NULL; } } } *************** *** 1480,1489 **** { chanpart_T *in_part = &channel->ch_part[PART_IN]; ! if (in_part->ch_buffer != NULL) { if (in_part->ch_buf_append) ! channel_write_new_lines(in_part->ch_buffer); else channel_write_in(channel); } --- 1482,1491 ---- { chanpart_T *in_part = &channel->ch_part[PART_IN]; ! if (in_part->ch_bufref.br_buf != NULL) { if (in_part->ch_buf_append) ! channel_write_new_lines(in_part->ch_bufref.br_buf); else channel_write_in(channel); } *************** *** 1507,1513 **** linenr_T lnum; int written = 0; ! if (in_part->ch_buffer == buf && in_part->ch_buf_append) { if (in_part->ch_fd == INVALID_FD) continue; /* pipe was closed */ --- 1509,1515 ---- linenr_T lnum; int written = 0; ! if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append) { if (in_part->ch_fd == INVALID_FD) continue; /* pipe was closed */ *************** *** 2312,2318 **** { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_buffer == buffer) in_part->ch_buf_bot = buffer->b_ml.ml_line_count; } } --- 2314,2320 ---- { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_bufref.br_buf == buffer) in_part->ch_buf_bot = buffer->b_ml.ml_line_count; } } *************** *** 2374,2384 **** partial = channel->ch_partial; } ! buffer = channel->ch_part[part].ch_buffer; ! if (buffer != NULL && !buf_valid(buffer)) { /* buffer was wiped out */ ! channel->ch_part[part].ch_buffer = NULL; buffer = NULL; } --- 2376,2386 ---- partial = channel->ch_partial; } ! buffer = channel->ch_part[part].ch_bufref.br_buf; ! if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref)) { /* buffer was wiped out */ ! channel->ch_part[part].ch_bufref.br_buf = NULL; buffer = NULL; } *************** *** 2834,2840 **** { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL) { FD_SET((int)in_part->ch_fd, wfds); if ((int)in_part->ch_fd >= maxfd) --- 2836,2842 ---- { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL) { FD_SET((int)in_part->ch_fd, wfds); if ((int)in_part->ch_fd >= maxfd) *************** *** 2857,2863 **** { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL) { in_part->ch_poll_idx = nfd; fds[nfd].fd = in_part->ch_fd; --- 2859,2865 ---- { chanpart_T *in_part = &ch->ch_part[PART_IN]; ! if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL) { in_part->ch_poll_idx = nfd; fds[nfd].fd = in_part->ch_fd; *************** *** 3643,3650 **** { if (in_part->ch_buf_append) { ! if (in_part->ch_buffer != NULL) ! channel_write_new_lines(in_part->ch_buffer); } else channel_write_in(channel); --- 3645,3652 ---- { if (in_part->ch_buf_append) { ! if (in_part->ch_bufref.br_buf != NULL) ! channel_write_new_lines(in_part->ch_bufref.br_buf); } else channel_write_in(channel); *************** *** 3721,3728 **** { if (in_part->ch_buf_append) { ! if (in_part->ch_buffer != NULL) ! channel_write_new_lines(in_part->ch_buffer); } else channel_write_in(channel); --- 3723,3730 ---- { if (in_part->ch_buf_append) { ! if (in_part->ch_bufref.br_buf != NULL) ! channel_write_new_lines(in_part->ch_bufref.br_buf); } else channel_write_in(channel); *** ../vim-7.4.2023/src/diff.c 2016-07-01 17:17:13.270267053 +0200 --- src/diff.c 2016-07-10 21:36:07.862171869 +0200 *************** *** 1069,1076 **** ex_diffsplit(exarg_T *eap) { win_T *old_curwin = curwin; ! buf_T *old_curbuf = curbuf; #ifdef FEAT_GUI need_mouse_correct = TRUE; #endif --- 1069,1077 ---- ex_diffsplit(exarg_T *eap) { win_T *old_curwin = curwin; ! bufref_T old_curbuf; + set_bufref(&old_curbuf, curbuf); #ifdef FEAT_GUI need_mouse_correct = TRUE; #endif *************** *** 1092,1101 **** { diff_win_options(old_curwin, TRUE); ! if (buf_valid(old_curbuf)) /* Move the cursor position to that of the old window. */ curwin->w_cursor.lnum = diff_get_corresponding_line( ! old_curbuf, old_curwin->w_cursor.lnum, curbuf, curwin->w_cursor.lnum); --- 1093,1102 ---- { diff_win_options(old_curwin, TRUE); ! if (bufref_valid(&old_curbuf)) /* Move the cursor position to that of the old window. */ curwin->w_cursor.lnum = diff_get_corresponding_line( ! old_curbuf.br_buf, old_curwin->w_cursor.lnum, curbuf, curwin->w_cursor.lnum); *************** *** 1557,1563 **** /* Compare all lines. If they are equal the lines were inserted * in some buffers, deleted in others, but not changed. */ for (i = 0; i < DB_COUNT; ++i) ! if (i != idx && curtab->tp_diffbuf[i] != NULL && dp->df_count[i] != 0) if (!diff_equal_entry(dp, idx, i)) return -1; } --- 1558,1565 ---- /* Compare all lines. If they are equal the lines were inserted * in some buffers, deleted in others, but not changed. */ for (i = 0; i < DB_COUNT; ++i) ! if (i != idx && curtab->tp_diffbuf[i] != NULL ! && dp->df_count[i] != 0) if (!diff_equal_entry(dp, idx, i)) return -1; } *** ../vim-7.4.2023/src/eval.c 2016-07-10 19:22:49.194149187 +0200 --- src/eval.c 2016-07-10 20:50:22.415289865 +0200 *************** *** 10503,10510 **** part = PART_IN; else part = PART_SOCK; ! if (channel->ch_part[part].ch_buffer != NULL) ! rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum; } } --- 10503,10511 ---- part = PART_IN; else part = PART_SOCK; ! if (channel->ch_part[part].ch_bufref.br_buf != NULL) ! rettv->vval.v_number = ! channel->ch_part[part].ch_bufref.br_buf->b_fnum; } } *** ../vim-7.4.2023/src/ex_cmds2.c 2016-07-02 22:07:16.992688114 +0200 --- src/ex_cmds2.c 2016-07-10 21:01:34.013212165 +0200 *************** *** 1693,1698 **** --- 1693,1699 ---- autowrite(buf_T *buf, int forceit) { int r; + bufref_T bufref; if (!(p_aw || p_awa) || !p_write #ifdef FEAT_QUICKFIX *************** *** 1701,1711 **** #endif || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) return FAIL; r = buf_write_all(buf, forceit); /* Writing may succeed but the buffer still changed, e.g., when there is a * conversion error. We do want to return FAIL then. */ ! if (buf_valid(buf) && bufIsChanged(buf)) r = FAIL; return r; } --- 1702,1713 ---- #endif || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) return FAIL; + set_bufref(&bufref, buf); r = buf_write_all(buf, forceit); /* Writing may succeed but the buffer still changed, e.g., when there is a * conversion error. We do want to return FAIL then. */ ! if (bufref_valid(&bufref) && bufIsChanged(buf)) r = FAIL; return r; } *************** *** 1723,1732 **** for (buf = firstbuf; buf; buf = buf->b_next) if (bufIsChanged(buf) && !buf->b_p_ro) { (void)buf_write_all(buf, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!buf_valid(buf)) buf = firstbuf; #endif } --- 1725,1739 ---- for (buf = firstbuf; buf; buf = buf->b_next) if (bufIsChanged(buf) && !buf->b_p_ro) { + #ifdef FEAT_AUTOCMD + bufref_T bufref; + + set_bufref(&bufref, buf); + #endif (void)buf_write_all(buf, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!bufref_valid(&bufref)) buf = firstbuf; #endif } *************** *** 1739,1745 **** int check_changed(buf_T *buf, int flags) { ! int forceit = (flags & CCGD_FORCEIT); if ( !forceit && bufIsChanged(buf) --- 1746,1757 ---- int check_changed(buf_T *buf, int flags) { ! int forceit = (flags & CCGD_FORCEIT); ! #ifdef FEAT_AUTOCMD ! bufref_T bufref; ! ! set_bufref(&bufref, buf); ! #endif if ( !forceit && bufIsChanged(buf) *************** *** 1762,1774 **** )) ++count; # ifdef FEAT_AUTOCMD ! if (!buf_valid(buf)) /* Autocommand deleted buffer, oops! It's not changed now. */ return FALSE; # endif dialog_changed(buf, count > 1); # ifdef FEAT_AUTOCMD ! if (!buf_valid(buf)) /* Autocommand deleted buffer, oops! It's not changed now. */ return FALSE; # endif --- 1774,1786 ---- )) ++count; # ifdef FEAT_AUTOCMD ! if (!bufref_valid(&bufref)) /* Autocommand deleted buffer, oops! It's not changed now. */ return FALSE; # endif dialog_changed(buf, count > 1); # ifdef FEAT_AUTOCMD ! if (!bufref_valid(&bufref)) /* Autocommand deleted buffer, oops! It's not changed now. */ return FALSE; # endif *************** *** 1867,1872 **** --- 1879,1889 ---- ) && !buf2->b_p_ro) { + #ifdef FEAT_AUTOCMD + bufref_T bufref; + + set_bufref(&bufref, buf2); + #endif #ifdef FEAT_BROWSE /* May get file name, when there is none */ browse_save_fname(buf2); *************** *** 1877,1883 **** (void)buf_write_all(buf2, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!buf_valid(buf2)) buf2 = firstbuf; #endif } --- 1894,1900 ---- (void)buf_write_all(buf2, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!bufref_valid(&bufref)) buf2 = firstbuf; #endif } *************** *** 1983,1993 **** continue; if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf)) { /* Try auto-writing the buffer. If this fails but the buffer no * longer exists it's not changed, that's OK. */ if (check_changed(buf, (p_awa ? CCGD_AW : 0) | CCGD_MULTWIN ! | CCGD_ALLBUF) && buf_valid(buf)) break; /* didn't save - still changes */ } } --- 2000,2013 ---- continue; 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 * longer exists it's not changed, that's OK. */ if (check_changed(buf, (p_awa ? CCGD_AW : 0) | CCGD_MULTWIN ! | CCGD_ALLBUF) && bufref_valid(&bufref)) break; /* didn't save - still changes */ } } *************** *** 2030,2039 **** FOR_ALL_TAB_WINDOWS(tp, wp) if (wp->w_buffer == buf) { goto_tabpage_win(tp, wp); # ifdef FEAT_AUTOCMD /* Paranoia: did autocms wipe out the buffer with changes? */ ! if (!buf_valid(buf)) { goto theend; } --- 2050,2064 ---- FOR_ALL_TAB_WINDOWS(tp, wp) if (wp->w_buffer == buf) { + # ifdef FEAT_AUTOCMD + bufref_T bufref; + + set_bufref(&bufref, buf); + # endif goto_tabpage_win(tp, wp); # ifdef FEAT_AUTOCMD /* Paranoia: did autocms wipe out the buffer with changes? */ ! if (!bufref_valid(&bufref)) { goto theend; } *** ../vim-7.4.2023/src/ex_docmd.c 2016-07-09 18:49:47.202419946 +0200 --- src/ex_docmd.c 2016-07-10 20:59:58.014650090 +0200 *************** *** 7322,7329 **** # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) if ((p_confirm || cmdmod.confirm) && p_write) { dialog_changed(buf, FALSE); ! if (buf_valid(buf) && bufIsChanged(buf)) return; need_hide = FALSE; } --- 7322,7332 ---- # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) if ((p_confirm || cmdmod.confirm) && p_write) { + bufref_T bufref; + + set_bufref(&bufref, buf); dialog_changed(buf, FALSE); ! if (bufref_valid(&bufref) && bufIsChanged(buf)) return; need_hide = FALSE; } *** ../vim-7.4.2023/src/ex_getln.c 2016-07-01 18:16:47.489936307 +0200 --- src/ex_getln.c 2016-07-10 21:03:04.703853716 +0200 *************** *** 1666,1672 **** case Ctrl__: /* CTRL-_: switch language mode */ if (!p_ari) break; ! #ifdef FEAT_FKMAP if (p_altkeymap) { cmd_fkmap = !cmd_fkmap; --- 1666,1672 ---- case Ctrl__: /* CTRL-_: switch language mode */ if (!p_ari) break; ! # ifdef FEAT_FKMAP if (p_altkeymap) { cmd_fkmap = !cmd_fkmap; *************** *** 1674,1680 **** ccline.overstrike = FALSE; } else /* Hebrew is default */ ! #endif cmd_hkmap = !cmd_hkmap; goto cmdline_not_changed; #endif --- 1674,1680 ---- ccline.overstrike = FALSE; } else /* Hebrew is default */ ! # endif cmd_hkmap = !cmd_hkmap; goto cmdline_not_changed; #endif *************** *** 6655,6663 **** ex_window(void) { struct cmdline_info save_ccline; ! buf_T *old_curbuf = curbuf; win_T *old_curwin = curwin; ! buf_T *bp; win_T *wp; int i; linenr_T lnum; --- 6655,6663 ---- ex_window(void) { struct cmdline_info save_ccline; ! bufref_T old_curbuf; win_T *old_curwin = curwin; ! bufref_T bufref; win_T *wp; int i; linenr_T lnum; *************** *** 6686,6691 **** --- 6686,6692 ---- beep_flush(); return K_IGNORE; } + set_bufref(&old_curbuf, curbuf); /* Save current window sizes. */ win_size_save(&winsizes); *************** *** 6832,6838 **** /* Safety check: The old window or buffer was deleted: It's a bug when * this happens! */ ! if (!win_valid(old_curwin) || !buf_valid(old_curbuf)) { cmdwin_result = Ctrl_C; EMSG(_("E199: Active window or buffer deleted")); --- 6833,6839 ---- /* Safety check: The old window or buffer was deleted: It's a bug when * this happens! */ ! if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) { cmdwin_result = Ctrl_C; EMSG(_("E199: Active window or buffer deleted")); *************** *** 6903,6916 **** curwin->w_p_cole = 0; # endif wp = curwin; ! bp = curbuf; win_goto(old_curwin); win_close(wp, TRUE); /* win_close() may have already wiped the buffer when 'bh' is * set to 'wipe' */ ! if (buf_valid(bp)) ! close_buffer(NULL, bp, DOBUF_WIPE, FALSE); /* Restore window sizes. */ win_size_restore(&winsizes); --- 6904,6917 ---- curwin->w_p_cole = 0; # endif wp = curwin; ! set_bufref(&bufref, curbuf); win_goto(old_curwin); win_close(wp, TRUE); /* win_close() may have already wiped the buffer when 'bh' is * set to 'wipe' */ ! if (bufref_valid(&bufref)) ! close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); /* Restore window sizes. */ win_size_restore(&winsizes); *** ../vim-7.4.2023/src/fileio.c 2016-07-01 20:03:38.255919891 +0200 --- src/fileio.c 2016-07-10 21:26:38.902682733 +0200 *************** *** 3287,3292 **** --- 3287,3293 ---- int did_cmd = FALSE; int nofile_err = FALSE; int empty_memline = (buf->b_ml.ml_mfp == NULL); + bufref_T bufref; /* * Apply PRE autocommands. *************** *** 3304,3309 **** --- 3305,3311 ---- /* set curwin/curbuf to buf and save a few things */ aucmd_prepbuf(&aco, buf); + set_bufref(&bufref, buf); if (append) { *************** *** 3376,3382 **** * 2. The autocommands abort script processing. * 3. If one of the "Cmd" autocommands was executed. */ ! if (!buf_valid(buf)) buf = NULL; if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) || did_cmd || nofile_err --- 3378,3384 ---- * 2. The autocommands abort script processing. * 3. If one of the "Cmd" autocommands was executed. */ ! if (!bufref_valid(&bufref)) buf = NULL; if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) || did_cmd || nofile_err *************** *** 6675,6684 **** /* Only check buffers in a window. */ if (buf->b_nwindows > 0) { n = buf_check_timestamp(buf, focus); if (didit < n) didit = n; ! if (n > 0 && !buf_valid(buf)) { /* Autocommands have removed the buffer, start at the * first one again. */ --- 6677,6689 ---- /* Only check buffers in a window. */ if (buf->b_nwindows > 0) { + bufref_T bufref; + + set_bufref(&bufref, buf); n = buf_check_timestamp(buf, focus); if (didit < n) didit = n; ! if (n > 0 && !bufref_valid(&bufref)) { /* Autocommands have removed the buffer, start at the * first one again. */ *************** *** 6766,6771 **** --- 6771,6777 ---- char *mesg2 = ""; int helpmesg = FALSE; int reload = FALSE; + char *reason; #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) int can_reload = FALSE; #endif *************** *** 6778,6785 **** static int busy = FALSE; int n; char_u *s; #endif - char *reason; /* If there is no file name, the buffer is not loaded, 'buftype' is * set, we are in the middle of a save or being called recursively: ignore --- 6784,6793 ---- static int busy = FALSE; int n; char_u *s; + bufref_T bufref; + + set_bufref(&bufref, buf); #endif /* If there is no file name, the buffer is not loaded, 'buftype' is * set, we are in the middle of a save or being called recursively: ignore *************** *** 6868,6874 **** busy = FALSE; if (n) { ! if (!buf_valid(buf)) EMSG(_("E246: FileChangedShell autocommand deleted buffer")); # ifdef FEAT_EVAL s = get_vim_var_str(VV_FCS_CHOICE); --- 6876,6882 ---- busy = FALSE; if (n) { ! if (!bufref_valid(&bufref)) EMSG(_("E246: FileChangedShell autocommand deleted buffer")); # ifdef FEAT_EVAL s = get_vim_var_str(VV_FCS_CHOICE); *************** *** 7026,7032 **** #ifdef FEAT_AUTOCMD /* Trigger FileChangedShell when the file was changed in any way. */ ! if (buf_valid(buf) && retval != 0) (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, buf->b_fname, buf->b_fname, FALSE, buf); #endif --- 7034,7040 ---- #ifdef FEAT_AUTOCMD /* Trigger FileChangedShell when the file was changed in any way. */ ! if (bufref_valid(&bufref) && retval != 0) (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, buf->b_fname, buf->b_fname, FALSE, buf); #endif *************** *** 7053,7058 **** --- 7061,7067 ---- linenr_T old_topline; int old_ro = buf->b_p_ro; buf_T *savebuf; + bufref_T bufref; int saved = OK; aco_save_T aco; int flags = READ_NEW; *************** *** 7090,7095 **** --- 7099,7105 ---- { /* Allocate a buffer without putting it in the buffer list. */ savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); + set_bufref(&bufref, savebuf); if (savebuf != NULL && buf == curbuf) { /* Open the memline. */ *************** *** 7122,7128 **** if (!aborting()) #endif EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); ! if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) { /* Put the text back from the save buffer. First * delete any lines that readfile() added. */ --- 7132,7138 ---- if (!aborting()) #endif EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); ! if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) { /* Put the text back from the save buffer. First * delete any lines that readfile() added. */ *************** *** 7150,7156 **** } vim_free(ea.cmd); ! if (savebuf != NULL && buf_valid(savebuf)) wipe_buffer(savebuf, FALSE); #ifdef FEAT_DIFF --- 7160,7166 ---- } vim_free(ea.cmd); ! if (savebuf != NULL && bufref_valid(&bufref)) wipe_buffer(savebuf, FALSE); #ifdef FEAT_DIFF *************** *** 8739,8744 **** --- 8749,8755 ---- int retval; aco_save_T aco; buf_T *buf; + bufref_T bufref; char_u *arg = eap->arg; int call_do_modelines = check_nomodeline(&arg); int did_aucmd; *************** *** 8756,8761 **** --- 8767,8773 ---- { /* find a window for this buffer and save some values */ aucmd_prepbuf(&aco, buf); + set_bufref(&bufref, buf); /* execute the autocommands for this buffer */ retval = do_doautocmd(arg, FALSE, &did_aucmd); *************** *** 8772,8778 **** aucmd_restbuf(&aco); /* stop if there is some error or buffer was deleted */ ! if (retval == FAIL || !buf_valid(buf)) break; } } --- 8784,8790 ---- aucmd_restbuf(&aco); /* stop if there is some error or buffer was deleted */ ! if (retval == FAIL || !bufref_valid(&bufref)) break; } } *************** *** 8898,8904 **** } curbuf = buf; aco->new_curwin = curwin; ! aco->new_curbuf = curbuf; } /* --- 8910,8916 ---- } curbuf = buf; aco->new_curwin = curwin; ! set_bufref(&aco->new_curbuf, curbuf); } /* *************** *** 8992,9007 **** * it was changed, we are still the same window and the buffer is * valid. */ if (curwin == aco->new_curwin ! && curbuf != aco->new_curbuf ! && buf_valid(aco->new_curbuf) ! && aco->new_curbuf->b_ml.ml_mfp != NULL) { # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) if (curwin->w_s == &curbuf->b_s) ! curwin->w_s = &aco->new_curbuf->b_s; # endif --curbuf->b_nwindows; ! curbuf = aco->new_curbuf; curwin->w_buffer = curbuf; ++curbuf->b_nwindows; } --- 9004,9019 ---- * it was changed, we are still the same window and the buffer is * valid. */ 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 defined(FEAT_SYN_HL) || defined(FEAT_SPELL) if (curwin->w_s == &curbuf->b_s) ! curwin->w_s = &aco->new_curbuf.br_buf->b_s; # endif --curbuf->b_nwindows; ! curbuf = aco->new_curbuf.br_buf; curwin->w_buffer = curbuf; ++curbuf->b_nwindows; } *************** *** 9180,9185 **** --- 9192,9201 ---- return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); } + /* + * Execute autocommands for "event" and file name "fname". + * Return TRUE if some commands were executed. + */ static int apply_autocmds_group( event_T event, *** ../vim-7.4.2023/src/main.c 2016-07-03 17:47:21.858812593 +0200 --- src/main.c 2016-07-10 21:37:07.453280320 +0200 *************** *** 1460,1468 **** for (buf = firstbuf; buf != NULL; buf = buf->b_next) if (buf->b_ml.ml_mfp != NULL) { apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf); ! if (!buf_valid(buf)) /* autocmd may delete the buffer */ break; } apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf); --- 1460,1472 ---- for (buf = firstbuf; buf != NULL; buf = buf->b_next) if (buf->b_ml.ml_mfp != NULL) { + bufref_T bufref; + + set_bufref(&bufref, buf); apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf); ! if (!bufref_valid(&bufref)) ! /* autocmd deleted the buffer */ break; } apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf); *** ../vim-7.4.2023/src/misc2.c 2016-07-01 18:16:47.493936250 +0200 --- src/misc2.c 2016-07-10 21:37:43.108746863 +0200 *************** *** 1173,1181 **** #endif for (buf = firstbuf; buf != NULL; ) { nextbuf = buf->b_next; close_buffer(NULL, buf, DOBUF_WIPE, FALSE); ! if (buf_valid(buf)) buf = nextbuf; /* didn't work, try next one */ else buf = firstbuf; --- 1173,1184 ---- #endif for (buf = firstbuf; buf != NULL; ) { + bufref_T bufref; + + set_bufref(&bufref, buf); nextbuf = buf->b_next; close_buffer(NULL, buf, DOBUF_WIPE, FALSE); ! if (bufref_valid(&bufref)) buf = nextbuf; /* didn't work, try next one */ else buf = firstbuf; *** ../vim-7.4.2023/src/netbeans.c 2016-07-07 20:45:00.740424639 +0200 --- src/netbeans.c 2016-07-10 21:38:46.575797281 +0200 *************** *** 2177,2186 **** #endif ) { buf_write_all(buf->bufp, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!buf_valid(buf->bufp)) buf->bufp = NULL; #endif } --- 2177,2191 ---- #endif ) { + #ifdef FEAT_AUTOCMD + bufref_T bufref; + + set_bufref(&bufref, buf->bufp); + #endif buf_write_all(buf->bufp, FALSE); #ifdef FEAT_AUTOCMD /* an autocommand may have deleted the buffer */ ! if (!bufref_valid(&bufref)) buf->bufp = NULL; #endif } *** ../vim-7.4.2023/src/quickfix.c 2016-07-10 20:27:27.423946553 +0200 --- src/quickfix.c 2016-07-10 21:42:25.596520113 +0200 *************** *** 4242,4248 **** char_u *resulting_dir) /* out: new directory */ { buf_T *newbuf; ! buf_T *newbuf_to_wipe = NULL; int failed = TRUE; aco_save_T aco; --- 4242,4249 ---- char_u *resulting_dir) /* out: new directory */ { buf_T *newbuf; ! bufref_T newbufref; ! bufref_T newbuf_to_wipe; int failed = TRUE; aco_save_T aco; *************** *** 4250,4255 **** --- 4251,4257 ---- newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); if (newbuf == NULL) return NULL; + set_bufref(&newbufref, newbuf); /* Init the options. */ buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); *************** *** 4270,4275 **** --- 4272,4278 ---- * work. */ curbuf->b_flags &= ~BF_DUMMY; + newbuf_to_wipe.br_buf = NULL; if (readfile(fname, NULL, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW | READ_DUMMY) == OK *************** *** 4283,4297 **** * using netrw and editing a remote file. Use the current * buffer instead, delete the dummy one after restoring the * window stuff. */ ! newbuf_to_wipe = newbuf; newbuf = curbuf; } } /* restore curwin/curbuf and a few other things */ aucmd_restbuf(&aco); ! if (newbuf_to_wipe != NULL && buf_valid(newbuf_to_wipe)) ! wipe_buffer(newbuf_to_wipe, FALSE); /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't * skip it. */ --- 4286,4300 ---- * using netrw and editing a remote file. Use the current * buffer instead, delete the dummy one after restoring the * window stuff. */ ! set_bufref(&newbuf_to_wipe, newbuf); newbuf = curbuf; } } /* restore curwin/curbuf and a few other things */ aucmd_restbuf(&aco); ! if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) ! wipe_buffer(newbuf_to_wipe.br_buf, FALSE); /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't * skip it. */ *************** *** 4306,4312 **** mch_dirname(resulting_dir, MAXPATHL); restore_start_dir(dirname_start); ! if (!buf_valid(newbuf)) return NULL; if (failed) { --- 4309,4315 ---- mch_dirname(resulting_dir, MAXPATHL); restore_start_dir(dirname_start); ! if (!bufref_valid(&newbufref)) return NULL; if (failed) { *** ../vim-7.4.2023/src/spell.c 2016-07-01 17:17:13.294266702 +0200 --- src/spell.c 2016-07-10 21:46:01.041296134 +0200 *************** *** 4178,4183 **** --- 4178,4188 ---- static int recursive = FALSE; char_u *ret_msg = NULL; char_u *spl_copy; + #ifdef FEAT_AUTOCMD + bufref_T bufref; + + set_bufref(&bufref, wp->w_buffer); + #endif /* We don't want to do this recursively. May happen when a language is * not available and the SpellFileMissing autocommand opens a new buffer *************** *** 4278,4284 **** #ifdef FEAT_AUTOCMD /* SpellFileMissing autocommands may do anything, including * destroying the buffer we are using... */ ! if (!buf_valid(wp->w_buffer)) { ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer"; goto theend; --- 4283,4289 ---- #ifdef FEAT_AUTOCMD /* SpellFileMissing autocommands may do anything, including * destroying the buffer we are using... */ ! if (!bufref_valid(&bufref)) { ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer"; goto theend; *************** *** 15561,15567 **** set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); vim_free(spl); ! if (!bufempty() || !buf_valid(curbuf)) return; spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); --- 15566,15572 ---- set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); vim_free(spl); ! if (!bufempty()) return; spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); *** ../vim-7.4.2023/src/term.c 2016-05-25 22:00:01.337797601 +0200 --- src/term.c 2016-07-10 21:46:51.560540103 +0200 *************** *** 1908,1928 **** #ifdef FEAT_AUTOCMD { ! buf_T *old_curbuf; /* * Execute the TermChanged autocommands for each buffer that is * loaded. */ ! old_curbuf = curbuf; for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next) { if (curbuf->b_ml.ml_mfp != NULL) apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE, curbuf); } ! if (buf_valid(old_curbuf)) ! curbuf = old_curbuf; } #endif } --- 1908,1928 ---- #ifdef FEAT_AUTOCMD { ! bufref_T old_curbuf; /* * Execute the TermChanged autocommands for each buffer that is * loaded. */ ! set_bufref(&old_curbuf, curbuf); for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next) { if (curbuf->b_ml.ml_mfp != NULL) apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE, curbuf); } ! if (bufref_valid(&old_curbuf)) ! curbuf = old_curbuf.br_buf; } #endif } *** ../vim-7.4.2023/src/if_py_both.h 2016-06-01 20:38:29.377545387 +0200 --- src/if_py_both.h 2016-07-10 22:01:25.715489182 +0200 *************** *** 3409,3415 **** { win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; ! buf_T *save_curbuf = NULL; int set_ret = 0; VimTryStart(); --- 3409,3415 ---- { win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; ! bufref_T save_curbuf; int set_ret = 0; VimTryStart(); *************** *** 3431,3437 **** case SREQ_BUF: switch_buffer(&save_curbuf, (buf_T *)from); set_ret = set_option_value_err(key, numval, stringval, opt_flags); ! restore_buffer(save_curbuf); break; case SREQ_GLOBAL: set_ret = set_option_value_err(key, numval, stringval, opt_flags); --- 3431,3437 ---- case SREQ_BUF: switch_buffer(&save_curbuf, (buf_T *)from); set_ret = set_option_value_err(key, numval, stringval, opt_flags); ! restore_buffer(&save_curbuf); break; case SREQ_GLOBAL: set_ret = set_option_value_err(key, numval, stringval, opt_flags); *************** *** 4273,4289 **** buf_T *buf, win_T **save_curwinp, tabpage_T **save_curtabp, ! buf_T **save_curbufp) { win_T *wp; tabpage_T *tp; if (find_win_for_buf(buf, &wp, &tp) == FAIL) ! switch_buffer(save_curbufp, buf); else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL) { restore_win(*save_curwinp, *save_curtabp, TRUE); ! switch_buffer(save_curbufp, buf); } } --- 4273,4289 ---- buf_T *buf, win_T **save_curwinp, tabpage_T **save_curtabp, ! bufref_T *save_curbuf) { win_T *wp; tabpage_T *tp; if (find_win_for_buf(buf, &wp, &tp) == FAIL) ! switch_buffer(save_curbuf, buf); else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL) { restore_win(*save_curwinp, *save_curtabp, TRUE); ! switch_buffer(save_curbuf, buf); } } *************** *** 4291,4299 **** restore_win_for_buf( win_T *save_curwin, tabpage_T *save_curtab, ! buf_T *save_curbuf) { ! if (save_curbuf == NULL) restore_win(save_curwin, save_curtab, TRUE); else restore_buffer(save_curbuf); --- 4291,4299 ---- restore_win_for_buf( win_T *save_curwin, tabpage_T *save_curtab, ! bufref_T *save_curbuf) { ! if (save_curbuf->br_buf == NULL) restore_win(save_curwin, save_curtab, TRUE); else restore_buffer(save_curbuf); *************** *** 4311,4317 **** static int SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) { ! buf_T *save_curbuf = NULL; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; --- 4311,4317 ---- static int SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) { ! bufref_T save_curbuf = {NULL, 0}; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; *************** *** 4336,4348 **** { if (buf == curbuf) py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); ! if (save_curbuf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ deleted_lines_mark((linenr_T)n, 1L); } ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); if (VimTryEnd()) return FAIL; --- 4336,4348 ---- { if (buf == curbuf) py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); ! if (save_curbuf.br_buf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ deleted_lines_mark((linenr_T)n, 1L); } ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); if (VimTryEnd()) return FAIL; *************** *** 4378,4384 **** else changed_bytes((linenr_T)n, 0); ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); /* Check that the cursor is not beyond the end of the line now. */ if (buf == curbuf) --- 4378,4384 ---- else changed_bytes((linenr_T)n, 0); ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); /* Check that the cursor is not beyond the end of the line now. */ if (buf == curbuf) *************** *** 4415,4421 **** PyObject *list, PyInt *len_change) { ! buf_T *save_curbuf = NULL; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; --- 4415,4421 ---- PyObject *list, PyInt *len_change) { ! bufref_T save_curbuf = {NULL, 0}; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; *************** *** 4446,4462 **** break; } } ! if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL)) /* Using an existing window for the buffer, adjust the cursor * position. */ py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); ! if (save_curbuf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ deleted_lines_mark((linenr_T)lo, (long)i); } ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); if (VimTryEnd()) return FAIL; --- 4446,4463 ---- break; } } ! if (buf == curbuf && (save_curwin != NULL ! || save_curbuf.br_buf == NULL)) /* Using an existing window for the buffer, adjust the cursor * position. */ py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); ! if (save_curbuf.br_buf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ deleted_lines_mark((linenr_T)lo, (long)i); } ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); if (VimTryEnd()) return FAIL; *************** *** 4578,4584 **** * changed range, and move any in the remainder of the buffer. * Only adjust marks if we managed to switch to a window that holds * the buffer, otherwise line numbers will be invalid. */ ! if (save_curbuf == NULL) mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra); changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); --- 4579,4585 ---- * changed range, and move any in the remainder of the buffer. * Only adjust marks if we managed to switch to a window that holds * the buffer, otherwise line numbers will be invalid. */ ! if (save_curbuf.br_buf == NULL) mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), (long)MAXLNUM, (long)extra); changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); *************** *** 4587,4593 **** py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); /* END of region without "return". */ ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); if (VimTryEnd()) return FAIL; --- 4588,4594 ---- py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); /* END of region without "return". */ ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); if (VimTryEnd()) return FAIL; *************** *** 4615,4621 **** static int InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) { ! buf_T *save_curbuf = NULL; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; --- 4616,4622 ---- static int InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) { ! bufref_T save_curbuf = {NULL, 0}; win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; *************** *** 4637,4649 **** RAISE_UNDO_FAIL; else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) RAISE_INSERT_LINE_FAIL; ! else if (save_curbuf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ appended_lines_mark((linenr_T)n, 1L); vim_free(str); ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); update_screen(VALID); if (VimTryEnd()) --- 4638,4650 ---- RAISE_UNDO_FAIL; else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) RAISE_INSERT_LINE_FAIL; ! else if (save_curbuf.br_buf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ appended_lines_mark((linenr_T)n, 1L); vim_free(str); ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); update_screen(VALID); if (VimTryEnd()) *************** *** 4704,4710 **** } vim_free(array[i]); } ! if (i > 0 && save_curbuf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ appended_lines_mark((linenr_T)n, (long)i); --- 4705,4711 ---- } vim_free(array[i]); } ! if (i > 0 && save_curbuf.br_buf == NULL) /* Only adjust marks if we managed to switch to a window that * holds the buffer, otherwise line numbers will be invalid. */ appended_lines_mark((linenr_T)n, (long)i); *************** *** 4713,4719 **** /* Free the array of lines. All of its contents have now * been freed. */ PyMem_Free(array); ! restore_win_for_buf(save_curwin, save_curtab, save_curbuf); update_screen(VALID); --- 4714,4720 ---- /* Free the array of lines. All of its contents have now * been freed. */ PyMem_Free(array); ! restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); update_screen(VALID); *************** *** 5216,5222 **** pos_T *posp; char_u *pmark; char_u mark; ! buf_T *savebuf; PyObject *todecref; if (CheckBuffer(self)) --- 5217,5223 ---- pos_T *posp; char_u *pmark; char_u mark; ! bufref_T savebuf; PyObject *todecref; if (CheckBuffer(self)) *************** *** 5240,5246 **** VimTryStart(); switch_buffer(&savebuf, self->buf); posp = getmark(mark, FALSE); ! restore_buffer(savebuf); if (VimTryEnd()) return NULL; --- 5241,5247 ---- VimTryStart(); switch_buffer(&savebuf, self->buf); posp = getmark(mark, FALSE); ! restore_buffer(&savebuf); if (VimTryEnd()) return NULL; *** ../vim-7.4.2023/src/window.c 2016-06-26 17:11:15.655124127 +0200 --- src/window.c 2016-07-10 21:54:33.633629346 +0200 *************** *** 2340,2345 **** --- 2340,2348 ---- */ if (win->w_buffer != NULL) { + bufref_T bufref; + + set_bufref(&bufref, curbuf); #ifdef FEAT_AUTOCMD win->w_closing = TRUE; #endif *************** *** 2350,2356 **** #endif /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is * "wipe". */ ! if (!buf_valid(curbuf)) curbuf = firstbuf; } --- 2353,2359 ---- #endif /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is * "wipe". */ ! if (!bufref_valid(&bufref)) curbuf = firstbuf; } *************** *** 6632,6643 **** * No autocommands will be executed. Use aucmd_prepbuf() if there are any. */ void ! switch_buffer(buf_T **save_curbuf, buf_T *buf) { # ifdef FEAT_AUTOCMD block_autocmds(); # endif ! *save_curbuf = curbuf; --curbuf->b_nwindows; curbuf = buf; curwin->w_buffer = buf; --- 6635,6646 ---- * No autocommands will be executed. Use aucmd_prepbuf() if there are any. */ void ! switch_buffer(bufref_T *save_curbuf, buf_T *buf) { # ifdef FEAT_AUTOCMD block_autocmds(); # endif ! set_bufref(save_curbuf, curbuf); --curbuf->b_nwindows; curbuf = buf; curwin->w_buffer = buf; *************** *** 6648,6664 **** * Restore the current buffer after using switch_buffer(). */ void ! restore_buffer(buf_T *save_curbuf) { # ifdef FEAT_AUTOCMD unblock_autocmds(); # endif /* Check for valid buffer, just in case. */ ! if (buf_valid(save_curbuf)) { --curbuf->b_nwindows; ! curwin->w_buffer = save_curbuf; ! curbuf = save_curbuf; ++curbuf->b_nwindows; } } --- 6651,6667 ---- * Restore the current buffer after using switch_buffer(). */ void ! restore_buffer(bufref_T *save_curbuf) { # ifdef FEAT_AUTOCMD unblock_autocmds(); # endif /* Check for valid buffer, just in case. */ ! if (bufref_valid(save_curbuf)) { --curbuf->b_nwindows; ! curwin->w_buffer = save_curbuf->br_buf; ! curbuf = save_curbuf->br_buf; ++curbuf->b_nwindows; } } *** ../vim-7.4.2023/src/proto/buffer.pro 2016-07-10 18:21:45.728884145 +0200 --- src/proto/buffer.pro 2016-07-10 20:36:57.971371016 +0200 *************** *** 7,13 **** void buf_clear_file(buf_T *buf); void buf_freeall(buf_T *buf, int flags); void goto_buffer(exarg_T *eap, int start, int dir, int count); ! void handle_swap_exists(buf_T *old_curbuf); char_u *do_bufdel(int command, char_u *arg, int addr_count, int start_bnr, int end_bnr, int forceit); int do_buffer(int action, int start, int dir, int count, int forceit); void set_curbuf(buf_T *buf, int action); --- 7,13 ---- void buf_clear_file(buf_T *buf); void buf_freeall(buf_T *buf, int flags); void goto_buffer(exarg_T *eap, int start, int dir, int count); ! void handle_swap_exists(bufref_T *old_curbuf); char_u *do_bufdel(int command, char_u *arg, int addr_count, int start_bnr, int end_bnr, int forceit); int do_buffer(int action, int start, int dir, int count, int forceit); void set_curbuf(buf_T *buf, int action); *** ../vim-7.4.2023/src/proto/window.pro 2016-03-13 19:04:45.381224860 +0100 --- src/proto/window.pro 2016-07-10 21:54:36.465587158 +0200 *************** *** 74,81 **** void restore_snapshot(int idx, int close_curwin); int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, int no_display); void restore_win(win_T *save_curwin, tabpage_T *save_curtab, int no_display); ! void switch_buffer(buf_T **save_curbuf, buf_T *buf); ! void restore_buffer(buf_T *save_curbuf); int win_hasvertsplit(void); int match_add(win_T *wp, char_u *grp, char_u *pat, int prio, int id, list_T *pos_list, char_u *conceal_char); int match_delete(win_T *wp, int id, int perr); --- 74,81 ---- void restore_snapshot(int idx, int close_curwin); int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, int no_display); void restore_win(win_T *save_curwin, tabpage_T *save_curtab, int no_display); ! void switch_buffer(bufref_T *save_curbuf, buf_T *buf); ! void restore_buffer(bufref_T *save_curbuf); int win_hasvertsplit(void); int match_add(win_T *wp, char_u *grp, char_u *pat, int prio, int id, list_T *pos_list, char_u *conceal_char); int match_delete(win_T *wp, int id, int perr); *** ../vim-7.4.2023/src/version.c 2016-07-10 20:27:27.427946493 +0200 --- src/version.c 2016-07-10 22:02:19.370689525 +0200 *************** *** 760,761 **** --- 760,763 ---- { /* Add new patch number below this line */ + /**/ + 2024, /**/ -- hundred-and-one symptoms of being an internet addict: 271. You collect hilarious signatures from all 250 mailing lists you are subscribed to. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///