Permalink
Browse files

patch 8.0.0860: side effects when channel appends to a buffer

Problem:    There may be side effects when a channel appends to a buffer that
            is not the current buffer.
Solution:   Properly switch to another buffer before appending. (Yasuhiro
            Matsumoto, closes #1926, closes #1937)
  • Loading branch information...
brammool committed Aug 4, 2017
1 parent 8e5eece commit 6b7355a30ddd294c19cd9be924d487d592ccfae1
Showing with 59 additions and 44 deletions.
  1. +44 −1 src/buffer.c
  2. +11 −6 src/channel.c
  3. +0 −37 src/if_py_both.h
  4. +2 −0 src/proto/buffer.pro
  5. +2 −0 src/version.c
View
@@ -5794,9 +5794,52 @@ buf_spname(buf_T *buf)
return NULL;
}
#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
#if defined(FEAT_JOB_CHANNEL) \
|| defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
|| defined(PROTO)
# define SWITCH_TO_WIN
/*
* Find a window that contains "buf" and switch to it.
* If there is no such window, use the current window and change "curbuf".
* Caller must initialize save_curbuf to NULL.
* restore_win_for_buf() MUST be called later!
*/
void
switch_to_win_for_buf(
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);
}
}
void
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);
}
#endif
#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
|| defined(SWITCH_TO_WIN) \
|| defined(PROTO)
/*
* Find a window for buffer "buf".
* If found OK is returned and "wp" and "tp" are set to the window and tabpage.
View
@@ -2284,7 +2284,9 @@ invoke_one_time_callback(
static void
append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
{
buf_T *save_curbuf = curbuf;
bufref_T save_curbuf = {NULL, 0, 0};
win_T *save_curwin = NULL;
tabpage_T *save_curtab = NULL;
linenr_T lnum = buffer->b_ml.ml_line_count;
int save_write_to = buffer->b_write_to_channel;
chanpart_T *ch_part = &channel->ch_part[part];
@@ -2313,8 +2315,10 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
buffer->b_p_ma = TRUE;
curbuf = buffer;
curwin->w_buffer = curbuf;
/* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
u_sync(TRUE);
/* ignore undo failure, undo is not very useful here */
ignored = u_save(lnum - empty, lnum + 1);
@@ -2328,8 +2332,10 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
else
ml_append(lnum, msg, 0, FALSE);
appended_lines_mark(lnum, 1L);
curbuf = save_curbuf;
curwin->w_buffer = curbuf;
/* Restore curbuf/curwin/curtab */
restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
if (ch_part->ch_nomodifiable)
buffer->b_p_ma = FALSE;
else
@@ -2338,7 +2344,6 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
if (buffer->b_nwindows > 0)
{
win_T *wp;
win_T *save_curwin;
FOR_ALL_WINDOWS(wp)
{
View
@@ -4262,43 +4262,6 @@ py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
invalidate_botline();
}
/*
* Find a window that contains "buf" and switch to it.
* If there is no such window, use the current window and change "curbuf".
* Caller must initialize save_curbuf to NULL.
* restore_win_for_buf() MUST be called later!
*/
static void
switch_to_win_for_buf(
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);
}
}
static void
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);
}
/*
* Replace a line in the specified buffer. The line number is
* in Vim format (1-based). The replacement line is given as
View
@@ -61,6 +61,8 @@ int bt_dontwrite(buf_T *buf);
int bt_dontwrite_msg(buf_T *buf);
int buf_hide(buf_T *buf);
char_u *buf_spname(buf_T *buf);
void switch_to_win_for_buf(buf_T *buf, win_T **save_curwinp, tabpage_T **save_curtabp, bufref_T *save_curbuf);
void restore_win_for_buf(win_T *save_curwin, tabpage_T *save_curtab, bufref_T *save_curbuf);
int find_win_for_buf(buf_T *buf, win_T **wp, tabpage_T **tp);
void buf_addsign(buf_T *buf, int id, linenr_T lnum, int typenr);
linenr_T buf_change_sign_type(buf_T *buf, int markId, int typenr);
View
@@ -769,6 +769,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
860,
/**/
859,
/**/

0 comments on commit 6b7355a

Please sign in to comment.