Skip to content

Commit 014069a

Browse files
committed
patch 7.4.1485
Problem: Job input from buffer is not implemented. Solution: Implement it. Add "in-top" and "in-bot" options.
1 parent c25558b commit 014069a

8 files changed

Lines changed: 175 additions & 22 deletions

File tree

src/channel.c

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,32 @@ channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
819819
#endif
820820

821821
/*
822-
* Sets the job the channel is associated with.
822+
* Sets the job the channel is associated with and associated options.
823823
* This does not keep a refcount, when the job is freed ch_job is cleared.
824824
*/
825825
void
826-
channel_set_job(channel_T *channel, job_T *job)
826+
channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
827827
{
828828
channel->ch_job = job;
829+
830+
channel_set_options(channel, options);
831+
832+
if (job->jv_in_buf != NULL)
833+
{
834+
chanpart_T *in_part = &channel->ch_part[PART_IN];
835+
836+
in_part->ch_buffer = job->jv_in_buf;
837+
ch_logs(channel, "reading from buffer '%s'",
838+
(char *)in_part->ch_buffer->b_ffname);
839+
if (options->jo_set & JO_IN_TOP)
840+
in_part->ch_buf_top = options->jo_in_top;
841+
else
842+
in_part->ch_buf_top = 1;
843+
if (options->jo_set & JO_IN_BOT)
844+
in_part->ch_buf_bot = options->jo_in_bot;
845+
else
846+
in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
847+
}
829848
}
830849

831850
/*
@@ -963,6 +982,47 @@ channel_set_req_callback(
963982
}
964983
}
965984

985+
/*
986+
* Write any lines to the in channel.
987+
*/
988+
void
989+
channel_write_in(channel_T *channel)
990+
{
991+
chanpart_T *in_part = &channel->ch_part[PART_IN];
992+
linenr_T lnum;
993+
buf_T *buf = in_part->ch_buffer;
994+
995+
if (buf == NULL)
996+
return;
997+
if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
998+
{
999+
/* buffer was wiped out or unloaded */
1000+
in_part->ch_buffer = NULL;
1001+
return;
1002+
}
1003+
if (in_part->ch_fd == INVALID_FD)
1004+
/* pipe was closed */
1005+
return;
1006+
1007+
for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1008+
&& lnum <= buf->b_ml.ml_line_count; ++lnum)
1009+
{
1010+
char_u *line = ml_get_buf(buf, lnum, FALSE);
1011+
int len = STRLEN(line);
1012+
char_u *p;
1013+
1014+
/* TODO: check if channel can be written to */
1015+
if ((p = alloc(len + 2)) == NULL)
1016+
break;
1017+
STRCPY(p, line);
1018+
p[len] = NL;
1019+
p[len + 1] = NUL;
1020+
channel_send(channel, PART_IN, p, "channel_write_in()");
1021+
vim_free(p);
1022+
}
1023+
in_part->ch_buf_top = lnum;
1024+
}
1025+
9661026
/*
9671027
* Invoke the "callback" on channel "channel".
9681028
*/

src/eval.c

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9662,7 +9662,26 @@ f_bufloaded(typval_T *argvars, typval_T *rettv)
96629662
rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
96639663
}
96649664

9665-
static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
9665+
static buf_T *
9666+
buflist_find_by_name(char_u *name, int curtab_only)
9667+
{
9668+
int save_magic;
9669+
char_u *save_cpo;
9670+
buf_T *buf;
9671+
9672+
/* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9673+
save_magic = p_magic;
9674+
p_magic = TRUE;
9675+
save_cpo = p_cpo;
9676+
p_cpo = (char_u *)"";
9677+
9678+
buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9679+
TRUE, FALSE, curtab_only));
9680+
9681+
p_magic = save_magic;
9682+
p_cpo = save_cpo;
9683+
return buf;
9684+
}
96669685

96679686
/*
96689687
* Get buffer by number or pattern.
@@ -9671,8 +9690,6 @@ static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
96719690
get_buf_tv(typval_T *tv, int curtab_only)
96729691
{
96739692
char_u *name = tv->vval.v_string;
9674-
int save_magic;
9675-
char_u *save_cpo;
96769693
buf_T *buf;
96779694

96789695
if (tv->v_type == VAR_NUMBER)
@@ -9684,17 +9701,7 @@ get_buf_tv(typval_T *tv, int curtab_only)
96849701
if (name[0] == '$' && name[1] == NUL)
96859702
return lastbuf;
96869703

9687-
/* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9688-
save_magic = p_magic;
9689-
p_magic = TRUE;
9690-
save_cpo = p_cpo;
9691-
p_cpo = (char_u *)"";
9692-
9693-
buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9694-
TRUE, FALSE, curtab_only));
9695-
9696-
p_magic = save_magic;
9697-
p_cpo = save_cpo;
9704+
buf = buflist_find_by_name(name, curtab_only);
96989705

96999706
/* If not found, try expanding the name, like done for bufexists(). */
97009707
if (buf == NULL)
@@ -10110,6 +10117,30 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
1011010117
opt->jo_io_name[part] =
1011110118
get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
1011210119
}
10120+
else if (STRCMP(hi->hi_key, "in-top") == 0
10121+
|| STRCMP(hi->hi_key, "in-bot") == 0)
10122+
{
10123+
linenr_T *lp;
10124+
10125+
if (!(supported & JO_OUT_IO))
10126+
break;
10127+
if (hi->hi_key[3] == 't')
10128+
{
10129+
lp = &opt->jo_in_top;
10130+
opt->jo_set |= JO_IN_TOP;
10131+
}
10132+
else
10133+
{
10134+
lp = &opt->jo_in_bot;
10135+
opt->jo_set |= JO_IN_BOT;
10136+
}
10137+
*lp = get_tv_number(item);
10138+
if (*lp < 0)
10139+
{
10140+
EMSG2(_(e_invarg2), get_tv_string(item));
10141+
return FAIL;
10142+
}
10143+
}
1011310144
else if (STRCMP(hi->hi_key, "callback") == 0)
1011410145
{
1011510146
if (!(supported & JO_CALLBACK))
@@ -15103,6 +15134,29 @@ f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
1510315134
JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
1510415135
+ JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
1510515136
return;
15137+
15138+
if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15139+
{
15140+
buf_T *buf;
15141+
15142+
/* check that we can find the buffer before starting the job */
15143+
if (!(opt.jo_set & JO_IN_NAME))
15144+
{
15145+
EMSG(_("E915: in-io buffer requires in-name to be set"));
15146+
return;
15147+
}
15148+
buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
15149+
if (buf == NULL)
15150+
return;
15151+
if (buf->b_ml.ml_mfp == NULL)
15152+
{
15153+
EMSG2(_("E918: buffer must be loaded: %s"),
15154+
opt.jo_io_name[PART_IN]);
15155+
return;
15156+
}
15157+
job->jv_in_buf = buf;
15158+
}
15159+
1510615160
job_set_options(job, &opt);
1510715161

1510815162
#ifndef USE_ARGV
@@ -15194,6 +15248,10 @@ f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
1519415248
mch_start_job((char *)cmd, job, &opt);
1519515249
#endif
1519615250

15251+
#ifdef FEAT_CHANNEL
15252+
channel_write_in(job->jv_channel);
15253+
#endif
15254+
1519715255
theend:
1519815256
#ifdef USE_ARGV
1519915257
vim_free(argv);

src/os_unix.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,8 +5141,7 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options)
51415141
# ifdef FEAT_CHANNEL
51425142
channel_set_pipes(channel, fd_in[1], fd_out[0],
51435143
use_out_for_err ? INVALID_FD : fd_err[0]);
5144-
channel_set_job(channel, job);
5145-
channel_set_options(channel, options);
5144+
channel_set_job(channel, job, options);
51465145
# ifdef FEAT_GUI
51475146
channel_gui_register(channel);
51485147
# endif

src/os_win32.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,9 +5083,7 @@ mch_start_job(char *cmd, job_T *job, jobopt_T *options)
50835083
job->jv_channel = channel;
50845084
channel_set_pipes(channel, (sock_T)ifd[1], (sock_T)ofd[0],
50855085
use_out_for_err ? INVALID_FD : (sock_T)efd[0]);
5086-
channel_set_job(channel, job);
5087-
channel_set_options(channel, options);
5088-
5086+
channel_set_job(channel, job, options);
50895087
# ifdef FEAT_GUI
50905088
channel_gui_register(channel);
50915089
# endif

src/proto/channel.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ void channel_gui_register(channel_T *channel);
1010
void channel_gui_register_all(void);
1111
channel_T *channel_open(char *hostname, int port_in, int waittime, void (*nb_close_cb)(void));
1212
void channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err);
13-
void channel_set_job(channel_T *channel, job_T *job);
13+
void channel_set_job(channel_T *channel, job_T *job, jobopt_T *options);
1414
void channel_set_options(channel_T *channel, jobopt_T *opt);
1515
void channel_set_req_callback(channel_T *channel, int part, char_u *callback, int id);
16+
void channel_write_in(channel_T *channel);
1617
char_u *channel_get(channel_T *channel, int part);
1718
int channel_collapse(channel_T *channel, int part);
1819
int channel_can_write_to(channel_T *channel);

src/structs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,8 @@ struct jobvar_S
12671267
int jv_exitval;
12681268
char_u *jv_exit_cb; /* allocated */
12691269

1270+
buf_T *jv_in_buf; /* buffer from "in-name" */
1271+
12701272
int jv_refcount; /* reference count */
12711273
channel_T *jv_channel; /* channel for I/O, reference counted */
12721274
};
@@ -1347,7 +1349,10 @@ typedef struct {
13471349

13481350
cbq_T ch_cb_head; /* dummy node for per-request callbacks */
13491351
char_u *ch_callback; /* call when a msg is not handled */
1352+
13501353
buf_T *ch_buffer; /* buffer to read from or write to */
1354+
linenr_T ch_buf_top; /* next line to send */
1355+
linenr_T ch_buf_bot; /* last line to send */
13511356
} chanpart_T;
13521357

13531358
struct channel_S {
@@ -1402,6 +1407,8 @@ struct channel_S {
14021407
#define JO_OUT_NAME 0x80000 /* "out-name" */
14031408
#define JO_ERR_NAME 0x100000 /* "err-name" (JO_OUT_NAME << 1) */
14041409
#define JO_IN_NAME 0x200000 /* "in-name" (JO_OUT_NAME << 2) */
1410+
#define JO_IN_TOP 0x400000 /* "in-top" */
1411+
#define JO_IN_BOT 0x800000 /* "in-bot" */
14051412
#define JO_ALL 0xffffff
14061413

14071414
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
@@ -1433,6 +1440,9 @@ typedef struct
14331440
char_u jo_io_name_buf[4][NUMBUFLEN];
14341441
char_u *jo_io_name[4]; /* not allocated! */
14351442

1443+
linenr_T jo_in_top;
1444+
linenr_T jo_in_bot;
1445+
14361446
char_u *jo_callback; /* not allocated! */
14371447
char_u *jo_out_cb; /* not allocated! */
14381448
char_u *jo_err_cb; /* not allocated! */

src/testdir/test_channel.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,31 @@ func Test_pipe_to_buffer()
479479
endtry
480480
endfunc
481481

482+
func Test_pipe_from_buffer()
483+
if !has('job')
484+
return
485+
endif
486+
call ch_logfile('channellog', 'w')
487+
call ch_log('Test_pipe_from_buffer()')
488+
489+
sp pipe-input
490+
call setline(1, ['echo one', 'echo two', 'echo three'])
491+
492+
let job = job_start(s:python . " test_channel_pipe.py",
493+
\ {'in-io': 'buffer', 'in-name': 'pipe-input'})
494+
call assert_equal("run", job_status(job))
495+
try
496+
let handle = job_getchannel(job)
497+
call assert_equal('one', ch_read(handle))
498+
call assert_equal('two', ch_read(handle))
499+
call assert_equal('three', ch_read(handle))
500+
bwipe!
501+
finally
502+
call job_stop(job)
503+
endtry
504+
call ch_logfile('')
505+
endfunc
506+
482507
func Test_pipe_to_nameless_buffer()
483508
if !has('job')
484509
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+
1485,
746748
/**/
747749
1484,
748750
/**/

0 commit comments

Comments
 (0)