Skip to content

Commit c934bfa

Browse files
committed
patch 9.0.0910: setting lines in another buffer may not work well
Problem: Setting lines in another buffer may not work well. Solution: Make sure the buffer being changed has a window. (issue #11558)
1 parent 9fda815 commit c934bfa

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/evalbuffer.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ set_buffer_lines(
137137
listitem_T *li = NULL;
138138
long added = 0;
139139
linenr_T append_lnum;
140-
buf_T *curbuf_save = NULL;
141140
win_T *curwin_save = NULL;
142-
int is_curbuf = buf == curbuf;
141+
aco_save_T aco;
142+
int using_aco = FALSE;
143143
int save_VIsual_active = VIsual_active;
144144

145145
// When using the current buffer ml_mfp will be set if needed. Useful when
146146
// setline() is used on startup. For other buffers the buffer must be
147147
// loaded.
148+
int is_curbuf = buf == curbuf;
148149
if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
149150
{
150151
rettv->vval.v_number = 1; // FAIL
@@ -155,11 +156,21 @@ set_buffer_lines(
155156

156157
if (!is_curbuf)
157158
{
159+
// Set "curbuf" to the buffer being changed. Then make sure there is a
160+
// window for it to handle any side effects.
158161
VIsual_active = FALSE;
159-
curbuf_save = curbuf;
160162
curwin_save = curwin;
161163
curbuf = buf;
162-
find_win_for_curbuf();
164+
find_win_for_curbuf(); // simplest: find existing window for "buf"
165+
166+
if (curwin->w_buffer != buf)
167+
{
168+
// No existing window for this buffer. It is dangerous to have
169+
// curwin->w_buffer differ from "curbuf", use the autocmd window.
170+
curbuf = curwin->w_buffer;
171+
aucmd_prepbuf(&aco, buf);
172+
using_aco = TRUE;
173+
}
163174
}
164175

165176
if (append)
@@ -262,8 +273,15 @@ set_buffer_lines(
262273
done:
263274
if (!is_curbuf)
264275
{
265-
curbuf = curbuf_save;
266-
curwin = curwin_save;
276+
if (using_aco)
277+
{
278+
aucmd_restbuf(&aco);
279+
}
280+
else
281+
{
282+
curwin = curwin_save;
283+
curbuf = curwin->w_buffer;
284+
}
267285
VIsual_active = save_VIsual_active;
268286
}
269287
}

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static char *(features[]) =
695695

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
910,
698700
/**/
699701
909,
700702
/**/

0 commit comments

Comments
 (0)