Skip to content

Commit 8b62d87

Browse files
committed
patch 8.1.0692: if a buffer was deleted a channel can't write to it
Problem: If a buffer was deleted a channel can't write to it. Solution: When the buffer exists but was unloaded, prepare it for writing. (closes #3764)
1 parent 4164bb2 commit 8b62d87

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

src/channel.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,25 @@ channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
10981098
}
10991099
}
11001100

1101+
/*
1102+
* Prepare buffer "buf" for writing channel output to.
1103+
*/
1104+
static void
1105+
prepare_buffer(buf_T *buf)
1106+
{
1107+
buf_T *save_curbuf = curbuf;
1108+
1109+
buf_copy_options(buf, BCO_ENTER);
1110+
curbuf = buf;
1111+
#ifdef FEAT_QUICKFIX
1112+
set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1113+
set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1114+
#endif
1115+
if (curbuf->b_ml.ml_mfp == NULL)
1116+
ml_open(curbuf);
1117+
curbuf = save_curbuf;
1118+
}
1119+
11011120
/*
11021121
* Find a buffer matching "name" or create a new one.
11031122
* Returns NULL if there is something very wrong (error already reported).
@@ -1120,14 +1139,9 @@ find_buffer(char_u *name, int err, int msg)
11201139
NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
11211140
if (buf == NULL)
11221141
return NULL;
1123-
buf_copy_options(buf, BCO_ENTER);
1142+
prepare_buffer(buf);
1143+
11241144
curbuf = buf;
1125-
#ifdef FEAT_QUICKFIX
1126-
set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1127-
set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1128-
#endif
1129-
if (curbuf->b_ml.ml_mfp == NULL)
1130-
ml_open(curbuf);
11311145
if (msg)
11321146
ml_replace(1, (char_u *)(err ? "Reading from channel error..."
11331147
: "Reading from channel output..."), TRUE);
@@ -1244,6 +1258,9 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
12441258
ch_log(channel, "writing out to buffer '%s'",
12451259
(char *)buf->b_ffname);
12461260
set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1261+
// if the buffer was deleted or unloaded resurrect it
1262+
if (buf->b_ml.ml_mfp == NULL)
1263+
prepare_buffer(buf);
12471264
}
12481265
}
12491266
}
@@ -1287,6 +1304,9 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
12871304
ch_log(channel, "writing err to buffer '%s'",
12881305
(char *)buf->b_ffname);
12891306
set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1307+
// if the buffer was deleted or unloaded resurrect it
1308+
if (buf->b_ml.ml_mfp == NULL)
1309+
prepare_buffer(buf);
12901310
}
12911311
}
12921312
}

src/testdir/test_channel.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,28 @@ func Test_collapse_buffers()
16451645
bwipe!
16461646
endfunc
16471647

1648+
func Test_write_to_deleted_buffer()
1649+
if !executable('echo') || !has('job')
1650+
return
1651+
endif
1652+
let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
1653+
call WaitForAssert({-> assert_equal("dead", job_status(job))})
1654+
let bufnr = bufnr('test_buffer')
1655+
call assert_equal(['hello'], getbufline(bufnr, 1, '$'))
1656+
call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1657+
call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1658+
bdel test_buffer
1659+
call assert_equal([], getbufline(bufnr, 1, '$'))
1660+
1661+
let job = job_start('echo hello', {'out_io': 'buffer', 'out_name': 'test_buffer', 'out_msg': 0})
1662+
call WaitForAssert({-> assert_equal("dead", job_status(job))})
1663+
call assert_equal(['hello'], getbufline(bufnr, 1, '$'))
1664+
call assert_equal('nofile', getbufvar(bufnr, '&buftype'))
1665+
call assert_equal('hide', getbufvar(bufnr, '&bufhidden'))
1666+
1667+
bwipe! test_buffer
1668+
endfunc
1669+
16481670
func Test_cmd_parsing()
16491671
if !has('unix')
16501672
return

src/version.c

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

800800
static int included_patches[] =
801801
{ /* Add new patch number below this line */
802+
/**/
803+
692,
802804
/**/
803805
691,
804806
/**/

0 commit comments

Comments
 (0)