Skip to content

Commit 6ff02c9

Browse files
committed
patch 7.4.1522
Problem: Cannot write channel err to a buffer. Solution: Implement it.
1 parent 8322e1f commit 6ff02c9

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

src/channel.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
871871
* Find a buffer matching "name" or create a new one.
872872
*/
873873
static buf_T *
874-
find_buffer(char_u *name)
874+
find_buffer(char_u *name, int err)
875875
{
876876
buf_T *buf = NULL;
877877
buf_T *save_curbuf = curbuf;
@@ -890,7 +890,8 @@ find_buffer(char_u *name)
890890
curbuf = buf;
891891
if (curbuf->b_ml.ml_mfp == NULL)
892892
ml_open(curbuf);
893-
ml_replace(1, (char_u *)"Reading from channel output...", TRUE);
893+
ml_replace(1, (char_u *)(err ? "Reading from channel error..."
894+
: "Reading from channel output..."), TRUE);
894895
changed_bytes(1, 0);
895896
curbuf = save_curbuf;
896897
}
@@ -968,10 +969,27 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
968969
if (!(opt->jo_set & JO_OUT_MODE))
969970
channel->ch_part[PART_OUT].ch_mode = MODE_NL;
970971
channel->ch_part[PART_OUT].ch_buffer =
971-
find_buffer(opt->jo_io_name[PART_OUT]);
972-
ch_logs(channel, "writing to buffer '%s'",
972+
find_buffer(opt->jo_io_name[PART_OUT], FALSE);
973+
ch_logs(channel, "writing out to buffer '%s'",
973974
(char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
974975
}
976+
977+
if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
978+
|| (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
979+
&& opt->jo_io[PART_OUT] == JIO_BUFFER)))
980+
{
981+
/* writing err to a buffer. Default mode is NL. */
982+
if (!(opt->jo_set & JO_ERR_MODE))
983+
channel->ch_part[PART_ERR].ch_mode = MODE_NL;
984+
if (opt->jo_io[PART_ERR] == JIO_OUT)
985+
channel->ch_part[PART_ERR].ch_buffer =
986+
channel->ch_part[PART_OUT].ch_buffer;
987+
else
988+
channel->ch_part[PART_ERR].ch_buffer =
989+
find_buffer(opt->jo_io_name[PART_ERR], TRUE);
990+
ch_logs(channel, "writing err to buffer '%s'",
991+
(char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
992+
}
975993
}
976994

977995
/*

src/testdir/test_channel.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,53 @@ func Test_pipe_to_buffer()
633633
endtry
634634
endfunc
635635

636+
func Test_pipe_err_to_buffer()
637+
if !has('job')
638+
return
639+
endif
640+
call ch_log('Test_pipe_err_to_buffer()')
641+
let job = job_start(s:python . " test_channel_pipe.py",
642+
\ {'err-io': 'buffer', 'err-name': 'pipe-err'})
643+
call assert_equal("run", job_status(job))
644+
try
645+
let handle = job_getchannel(job)
646+
call ch_sendraw(handle, "echoerr line one\n")
647+
call ch_sendraw(handle, "echoerr line two\n")
648+
call ch_sendraw(handle, "doubleerr this\n")
649+
call ch_sendraw(handle, "quit\n")
650+
sp pipe-err
651+
call s:waitFor('line("$") >= 5')
652+
call assert_equal(['Reading from channel error...', 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
653+
bwipe!
654+
finally
655+
call job_stop(job)
656+
endtry
657+
endfunc
658+
659+
func Test_pipe_both_to_buffer()
660+
if !has('job')
661+
return
662+
endif
663+
call ch_log('Test_pipe_both_to_buffer()')
664+
let job = job_start(s:python . " test_channel_pipe.py",
665+
\ {'out-io': 'buffer', 'out-name': 'pipe-err', 'err-io': 'out'})
666+
call assert_equal("run", job_status(job))
667+
try
668+
let handle = job_getchannel(job)
669+
call ch_sendraw(handle, "echo line one\n")
670+
call ch_sendraw(handle, "echoerr line two\n")
671+
call ch_sendraw(handle, "double this\n")
672+
call ch_sendraw(handle, "doubleerr that\n")
673+
call ch_sendraw(handle, "quit\n")
674+
sp pipe-err
675+
call s:waitFor('line("$") >= 7')
676+
call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
677+
bwipe!
678+
finally
679+
call job_stop(job)
680+
endtry
681+
endfunc
682+
636683
func Test_pipe_from_buffer()
637684
if !has('job')
638685
return

src/version.c

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

744744
static int included_patches[] =
745745
{ /* Add new patch number below this line */
746+
/**/
747+
1522,
746748
/**/
747749
1521,
748750
/**/

0 commit comments

Comments
 (0)