Skip to content

Commit 29fd038

Browse files
committed
patch 7.4.1529
Problem: Specifying buffer number for channel not implemented yet. Solution: Implement passing a buffer number.
1 parent af1a0e3 commit 29fd038

5 files changed

Lines changed: 116 additions & 20 deletions

File tree

src/channel.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,11 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
987987
/* writing output to a buffer. Default mode is NL. */
988988
if (!(opt->jo_set & JO_OUT_MODE))
989989
channel->ch_part[PART_OUT].ch_mode = MODE_NL;
990-
channel->ch_part[PART_OUT].ch_buffer =
990+
if (opt->jo_set & JO_OUT_BUF)
991+
channel->ch_part[PART_OUT].ch_buffer =
992+
buflist_findnr(opt->jo_io_buf[PART_OUT]);
993+
else
994+
channel->ch_part[PART_OUT].ch_buffer =
991995
find_buffer(opt->jo_io_name[PART_OUT], FALSE);
992996
ch_logs(channel, "writing out to buffer '%s'",
993997
(char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
@@ -1003,6 +1007,9 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
10031007
if (opt->jo_io[PART_ERR] == JIO_OUT)
10041008
channel->ch_part[PART_ERR].ch_buffer =
10051009
channel->ch_part[PART_OUT].ch_buffer;
1010+
else if (opt->jo_set & JO_ERR_BUF)
1011+
channel->ch_part[PART_ERR].ch_buffer =
1012+
buflist_findnr(opt->jo_io_buf[PART_ERR]);
10061013
else
10071014
channel->ch_part[PART_ERR].ch_buffer =
10081015
find_buffer(opt->jo_io_name[PART_ERR], TRUE);

src/eval.c

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10119,6 +10119,27 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
1011910119
opt->jo_io_name[part] =
1012010120
get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
1012110121
}
10122+
else if (STRCMP(hi->hi_key, "in-buf") == 0
10123+
|| STRCMP(hi->hi_key, "out-buf") == 0
10124+
|| STRCMP(hi->hi_key, "err-buf") == 0)
10125+
{
10126+
part = part_from_char(*hi->hi_key);
10127+
10128+
if (!(supported & JO_OUT_IO))
10129+
break;
10130+
opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
10131+
opt->jo_io_buf[part] = get_tv_number(item);
10132+
if (opt->jo_io_buf[part] <= 0)
10133+
{
10134+
EMSG2(_(e_invarg2), get_tv_string(item));
10135+
return FAIL;
10136+
}
10137+
if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
10138+
{
10139+
EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
10140+
return FAIL;
10141+
}
10142+
}
1012210143
else if (STRCMP(hi->hi_key, "in-top") == 0
1012310144
|| STRCMP(hi->hi_key, "in-bot") == 0)
1012410145
{
@@ -15156,21 +15177,36 @@ f_job_start(typval_T *argvars, typval_T *rettv)
1515615177

1515715178
if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
1515815179
{
15159-
buf_T *buf;
15180+
buf_T *buf = NULL;
1516015181

1516115182
/* check that we can find the buffer before starting the job */
15162-
if (!(opt.jo_set & JO_IN_NAME))
15183+
if (opt.jo_set & JO_IN_BUF)
1516315184
{
15164-
EMSG(_("E915: in-io buffer requires in-name to be set"));
15165-
return;
15185+
buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
15186+
if (buf == NULL)
15187+
EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
1516615188
}
15167-
buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
15189+
else if (!(opt.jo_set & JO_IN_NAME))
15190+
{
15191+
EMSG(_("E915: in-io buffer requires in-buf or in-name to be set"));
15192+
}
15193+
else
15194+
buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
1516815195
if (buf == NULL)
1516915196
return;
1517015197
if (buf->b_ml.ml_mfp == NULL)
1517115198
{
15172-
EMSG2(_("E918: buffer must be loaded: %s"),
15173-
opt.jo_io_name[PART_IN]);
15199+
char_u buf[NUMBUFLEN];
15200+
char_u *s;
15201+
15202+
if (opt.jo_set & JO_IN_BUF)
15203+
{
15204+
sprintf((char *)buf, "%d", opt.jo_io_buf[PART_IN]);
15205+
s = buf;
15206+
}
15207+
else
15208+
s = opt.jo_io_name[PART_IN];
15209+
EMSG2(_("E918: buffer must be loaded: %s"), s);
1517415210
return;
1517515211
}
1517615212
job->jv_in_buf = buf;

src/structs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,10 @@ struct channel_S {
14091409
#define JO_IN_NAME 0x200000 /* "in-name" (JO_OUT_NAME << 2) */
14101410
#define JO_IN_TOP 0x400000 /* "in-top" */
14111411
#define JO_IN_BOT 0x800000 /* "in-bot" */
1412-
#define JO_ALL 0xffffff
1412+
#define JO_OUT_BUF 0x1000000 /* "out-buf" */
1413+
#define JO_ERR_BUF 0x2000000 /* "err-buf" (JO_OUT_BUF << 1) */
1414+
#define JO_IN_BUF 0x4000000 /* "in-buf" (JO_OUT_BUF << 2) */
1415+
#define JO_ALL 0xfffffff
14131416

14141417
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
14151418
#define JO_CB_ALL \
@@ -1439,6 +1442,7 @@ typedef struct
14391442
job_io_T jo_io[4]; /* PART_OUT, PART_ERR, PART_IN */
14401443
char_u jo_io_name_buf[4][NUMBUFLEN];
14411444
char_u *jo_io_name[4]; /* not allocated! */
1445+
int jo_io_buf[4];
14421446

14431447
linenr_T jo_in_top;
14441448
linenr_T jo_in_bot;

src/testdir/test_channel.vim

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,22 @@ func Test_nl_write_both_file()
610610
endtry
611611
endfunc
612612

613-
func Test_pipe_to_buffer()
613+
func Run_test_pipe_to_buffer(use_name)
614614
if !has('job')
615615
return
616616
endif
617617
call ch_log('Test_pipe_to_buffer()')
618-
let job = job_start(s:python . " test_channel_pipe.py",
619-
\ {'out-io': 'buffer', 'out-name': 'pipe-output'})
618+
let options = {'out-io': 'buffer'}
619+
if a:use_name
620+
let options['out-name'] = 'pipe-output'
621+
let firstline = 'Reading from channel output...'
622+
else
623+
sp pipe-output
624+
let options['out-buf'] = bufnr('%')
625+
quit
626+
let firstline = ''
627+
endif
628+
let job = job_start(s:python . " test_channel_pipe.py", options)
620629
call assert_equal("run", job_status(job))
621630
try
622631
let handle = job_getchannel(job)
@@ -626,20 +635,37 @@ func Test_pipe_to_buffer()
626635
call ch_sendraw(handle, "quit\n")
627636
sp pipe-output
628637
call s:waitFor('line("$") >= 6')
629-
call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
638+
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
630639
bwipe!
631640
finally
632641
call job_stop(job)
633642
endtry
634643
endfunc
635644

636-
func Test_pipe_err_to_buffer()
645+
func Test_pipe_to_buffer_name()
646+
call Run_test_pipe_to_buffer(1)
647+
endfunc
648+
649+
func Test_pipe_to_buffer_nr()
650+
call Run_test_pipe_to_buffer(0)
651+
endfunc
652+
653+
func Run_test_pipe_err_to_buffer(use_name)
637654
if !has('job')
638655
return
639656
endif
640657
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'})
658+
let options = {'err-io': 'buffer'}
659+
if a:use_name
660+
let options['err-name'] = 'pipe-err'
661+
let firstline = 'Reading from channel error...'
662+
else
663+
sp pipe-err
664+
let options['err-buf'] = bufnr('%')
665+
quit
666+
let firstline = ''
667+
endif
668+
let job = job_start(s:python . " test_channel_pipe.py", options)
643669
call assert_equal("run", job_status(job))
644670
try
645671
let handle = job_getchannel(job)
@@ -649,13 +675,21 @@ func Test_pipe_err_to_buffer()
649675
call ch_sendraw(handle, "quit\n")
650676
sp pipe-err
651677
call s:waitFor('line("$") >= 5')
652-
call assert_equal(['Reading from channel error...', 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
678+
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
653679
bwipe!
654680
finally
655681
call job_stop(job)
656682
endtry
657683
endfunc
658684

685+
func Test_pipe_err_to_buffer_name()
686+
call Run_test_pipe_err_to_buffer(1)
687+
endfunc
688+
689+
func Test_pipe_err_to_buffer_nr()
690+
call Run_test_pipe_err_to_buffer(0)
691+
endfunc
692+
659693
func Test_pipe_both_to_buffer()
660694
if !has('job')
661695
return
@@ -680,17 +714,22 @@ func Test_pipe_both_to_buffer()
680714
endtry
681715
endfunc
682716

683-
func Test_pipe_from_buffer()
717+
func Run_test_pipe_from_buffer(use_name)
684718
if !has('job')
685719
return
686720
endif
687721
call ch_log('Test_pipe_from_buffer()')
688722

689723
sp pipe-input
690724
call setline(1, ['echo one', 'echo two', 'echo three'])
725+
let options = {'in-io': 'buffer'}
726+
if a:use_name
727+
let options['in-name'] = 'pipe-input'
728+
else
729+
let options['in-buf'] = bufnr('%')
730+
endif
691731

692-
let job = job_start(s:python . " test_channel_pipe.py",
693-
\ {'in-io': 'buffer', 'in-name': 'pipe-input'})
732+
let job = job_start(s:python . " test_channel_pipe.py", options)
694733
call assert_equal("run", job_status(job))
695734
try
696735
let handle = job_getchannel(job)
@@ -703,6 +742,14 @@ func Test_pipe_from_buffer()
703742
endtry
704743
endfunc
705744

745+
func Test_pipe_from_buffer_name()
746+
call Run_test_pipe_from_buffer(1)
747+
endfunc
748+
749+
func Test_pipe_from_buffer_nr()
750+
call Run_test_pipe_from_buffer(0)
751+
endfunc
752+
706753
func Test_pipe_to_nameless_buffer()
707754
if !has('job')
708755
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+
1529,
746748
/**/
747749
1528,
748750
/**/

0 commit comments

Comments
 (0)