Skip to content

Commit 910b8aa

Browse files
committed
patch 7.4.1341
Problem: It's difficult to add more arguments to ch_sendraw() and ch_sendexpr(). Solution: Make the third option a dictionary.
1 parent 7d63f62 commit 910b8aa

10 files changed

Lines changed: 91 additions & 54 deletions

File tree

runtime/doc/channel.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*channel.txt* For Vim version 7.4. Last change: 2016 Feb 15
1+
*channel.txt* For Vim version 7.4. Last change: 2016 Feb 16
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -117,7 +117,7 @@ Use |ch_status()| to see if the channel could be opened.
117117

118118
"mode" can be: *channel-mode*
119119
"json" - Use JSON, see below; most convenient way. Default.
120-
"js" - Use JavaScript encoding, more efficient than JSON.
120+
"js" - Use JS (JavaScript) encoding, more efficient than JSON.
121121
"nl" - Use messages that end in a NL character
122122
"raw" - Use raw messages
123123

@@ -188,11 +188,11 @@ If there is an error reading or writing a channel it will be closed.
188188
==============================================================================
189189
4. Using a JSON or JS channel *channel-use*
190190

191-
If {mode} is "json" then a message can be sent synchronously like this: >
191+
If mode is JSON then a message can be sent synchronously like this: >
192192
let response = ch_sendexpr(channel, {expr})
193193
This awaits a response from the other side.
194194

195-
When {mode} is "js" this works the same, except that the messages use
195+
When mode is JS this works the same, except that the messages use
196196
JavaScript encoding. See |js_encode()| for the difference.
197197

198198
To send a message, without handling a response: >
@@ -242,7 +242,7 @@ is then completely responsible for correct encoding and decoding.
242242
==============================================================================
243243
5. Channel commands *channel-commands*
244244

245-
With a "json" channel the process can send commands to Vim that will be
245+
With a JSON channel the process can send commands to Vim that will be
246246
handled by Vim internally, it does not require a handler for the channel.
247247

248248
Possible commands are: *E903* *E904* *E905*
@@ -316,14 +316,15 @@ Example:
316316
==============================================================================
317317
6. Using a RAW or NL channel *channel-raw*
318318

319-
If {mode} is "raw" then a message can be send like this: >
319+
If mode is RAW or NL then a message can be send like this: >
320320
let response = ch_sendraw(channel, {string})
321+
321322
The {string} is sent as-is. The response will be what can be read from the
322323
channel right away. Since Vim doesn't know how to recognize the end of the
323324
message you need to take care of it yourself. The timeout applies for reading
324325
the first byte, after that it will not wait for anything more.
325326

326-
If {mode} is "nl" you can send a message in a similar way. You are expected
327+
If mode is "nl" you can send a message in a similar way. You are expected
327328
to put in the NL after each message. Thus you can also send several messages
328329
ending in a NL at once. The response will be the text up to and including the
329330
first NL. This can also be just the NL for an empty response.
@@ -450,6 +451,7 @@ The {options} argument in job_start() is a dictionary. All entries are
450451
optional. The same options can be used with job_setoptions(job, {options}).
451452

452453
TODO: *job-out-cb*
454+
"callback": handler
453455
"out-cb": handler Callback for when there is something to read on
454456
stdout.
455457
TODO: *job-err-cb*
@@ -484,7 +486,7 @@ TODO: *job-out-io*
484486
"out-buffer": "name" buffer to append to
485487

486488
TODO: *job-err-io*
487-
"err-io": "out" same as stdout (default)
489+
"err-io": "out" same type as stdout (default)
488490
"err-io": "null" disconnect stderr
489491
"err-io": "pipe" stderr is connected to the channel
490492
"err-io": "file" stderr writes to a file

runtime/doc/eval.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 7.4. Last change: 2016 Feb 13
1+
*eval.txt* For Vim version 7.4. Last change: 2016 Feb 16
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1821,9 +1821,9 @@ ch_close( {handle}) none close a channel
18211821
ch_logfile( {fname} [, {mode}]) none start logging channel activity
18221822
ch_open( {address} [, {argdict})] Number open a channel to {address}
18231823
ch_readraw( {handle}) String read from channel {handle}
1824-
ch_sendexpr( {handle}, {expr} [, {callback}])
1824+
ch_sendexpr( {handle}, {expr} [, {options}])
18251825
any send {expr} over JSON channel {handle}
1826-
ch_sendraw( {handle}, {string} [, {callback}])
1826+
ch_sendraw( {handle}, {string} [, {options}])
18271827
any send {string} over raw channel {handle}
18281828
ch_status( {handle}) String status of channel {handle}
18291829
changenr() Number current change number
@@ -2725,28 +2725,32 @@ ch_readraw({handle}) *ch_readraw()*
27252725
within that time an empty string is returned.
27262726
TODO: depends on channel mode.
27272727

2728-
ch_sendexpr({handle}, {expr} [, {callback}]) *ch_sendexpr()*
2728+
ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
27292729
Send {expr} over channel {handle}. The {expr} is encoded
27302730
according to the type of channel. The function cannot be used
27312731
with a raw channel. See |channel-use|. *E912*
27322732

2733-
When {callback} is given returns immediately. Without
2734-
{callback} waits for a response and returns the decoded
2735-
expression. When there is an error or timeout returns an
2736-
empty string.
2733+
{options} must be a Dictionary.
2734+
When "callback" is a Funcref or the name of a function,
2735+
ch_sendexpr() returns immediately. The callback is invoked
2736+
when the response is received. See |channel-callback|.
2737+
2738+
Without "callback" ch_sendexpr() waits for a response and
2739+
returns the decoded expression. When there is an error or
2740+
timeout it returns an empty string.
27372741

2738-
When {callback} is zero no response is expected.
2739-
Otherwise {callback} must be a Funcref or the name of a
2740-
function. It is called when the response is received. See
2741-
|channel-callback|.
2742+
When "callback" is zero no response is expected.
27422743

27432744
{only available when compiled with the |+channel| feature}
27442745

2745-
ch_sendraw({handle}, {string} [, {callback}]) *ch_sendraw()*
2746+
ch_sendraw({handle}, {string} [, {options}]) *ch_sendraw()*
27462747
Send {string} over channel {handle}.
27472748
Works like |ch_sendexpr()|, but does not encode the request or
27482749
decode the response. The caller is responsible for the
2749-
correct contents. See |channel-use|.
2750+
correct contents. Also does not add a newline for a channel
2751+
in NL mode, the caller must do that. The NL in the response
2752+
is removed.
2753+
See |channel-use|.
27502754

27512755
{only available when compiled with the |+channel| feature}
27522756

@@ -7274,7 +7278,7 @@ listcmds Compiled with commands for the buffer list |:files|
72747278
and the argument list |arglist|.
72757279
localmap Compiled with local mappings and abbr. |:map-local|
72767280
lua Compiled with Lua interface |Lua|.
7277-
mac Macintosh version of Vim.
7281+
mac Any Macintosh version of Vim.
72787282
macunix Compiled for OS X, with darwin
72797283
osx Compiled for OS X, with or without darwin
72807284
menu Compiled with support for |:menu|.

src/channel.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,18 @@ channel_set_callback(channel_T *channel, char_u *callback)
696696
channel->ch_callback = vim_strsave(callback);
697697
}
698698

699+
/*
700+
* Set various properties from an "options" argument.
701+
*/
702+
void
703+
channel_set_options(channel_T *channel, jobopt_T *options)
704+
{
705+
channel_set_mode(channel, options->jo_mode);
706+
707+
if (options->jo_callback != NULL && *options->jo_callback != NUL)
708+
channel_set_callback(channel, options->jo_callback);
709+
}
710+
699711
/*
700712
* Set the callback for channel "channel" for the response with "id".
701713
*/

src/eval.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9930,15 +9930,18 @@ f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
99309930
}
99319931

99329932
/*
9933-
* Get the "mode" entry from "dict", if it exists, and parse the mode name.
9934-
* If the mode is invalide return FAIL.
9933+
* Get the option entries from "dict", and parse them.
9934+
* If an option value is invalid return FAIL.
99359935
*/
99369936
static int
9937-
get_mode_arg(dict_T *dict, jobopt_T *opt)
9937+
get_job_options(dict_T *dict, jobopt_T *opt)
99389938
{
99399939
dictitem_T *item;
99409940
char_u *mode;
99419941

9942+
if (dict == NULL)
9943+
return OK;
9944+
99429945
if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
99439946
{
99449947
mode = get_tv_string(&item->di_tv);
@@ -9956,6 +9959,17 @@ get_mode_arg(dict_T *dict, jobopt_T *opt)
99569959
return FAIL;
99579960
}
99589961
}
9962+
9963+
if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9964+
{
9965+
opt->jo_callback = get_callback(&item->di_tv);
9966+
if (opt->jo_callback == NULL)
9967+
{
9968+
EMSG2(_(e_invarg2), "callback");
9969+
return FAIL;
9970+
}
9971+
}
9972+
99599973
return OK;
99609974
}
99619975

@@ -9966,7 +9980,6 @@ get_mode_arg(dict_T *dict, jobopt_T *opt)
99669980
f_ch_open(typval_T *argvars, typval_T *rettv)
99679981
{
99689982
char_u *address;
9969-
char_u *callback = NULL;
99709983
char_u *p;
99719984
char *rest;
99729985
int port;
@@ -10004,20 +10017,19 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
1000410017
}
1000510018

1000610019
options.jo_mode = MODE_JSON;
10020+
options.jo_callback = NULL;
1000710021
if (argvars[1].v_type == VAR_DICT)
1000810022
{
1000910023
dict_T *dict = argvars[1].vval.v_dict;
1001010024
dictitem_T *item;
1001110025

1001210026
/* parse argdict */
10013-
if (get_mode_arg(dict, &options) == FAIL)
10027+
if (get_job_options(dict, &options) == FAIL)
1001410028
return;
1001510029
if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
1001610030
waittime = get_tv_number(&item->di_tv);
1001710031
if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
1001810032
timeout = get_tv_number(&item->di_tv);
10019-
if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
10020-
callback = get_callback(&item->di_tv);
1002110033
}
1002210034
if (waittime < 0 || timeout < 0)
1002310035
{
@@ -10029,10 +10041,8 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
1002910041
if (channel != NULL)
1003010042
{
1003110043
rettv->vval.v_channel = channel;
10032-
channel_set_mode(channel, options.jo_mode);
10044+
channel_set_options(channel, &options);
1003310045
channel_set_timeout(channel, timeout);
10034-
if (callback != NULL && *callback != NUL)
10035-
channel_set_callback(channel, callback);
1003610046
}
1003710047
}
1003810048

@@ -10082,16 +10092,23 @@ send_common(typval_T *argvars, char_u *text, int id, char *fun)
1008210092
{
1008310093
channel_T *channel;
1008410094
char_u *callback = NULL;
10095+
jobopt_T options;
1008510096

1008610097
channel = get_channel_arg(&argvars[0]);
1008710098
if (channel == NULL)
1008810099
return NULL;
1008910100

1009010101
if (argvars[2].v_type != VAR_UNKNOWN)
1009110102
{
10092-
callback = get_callback(&argvars[2]);
10093-
if (callback == NULL)
10103+
if (argvars[2].v_type != VAR_DICT)
10104+
{
10105+
EMSG(_(e_invarg));
10106+
return NULL;
10107+
}
10108+
options.jo_callback = NULL;
10109+
if (get_job_options(argvars[2].vval.v_dict, &options) == FAIL)
1009410110
return NULL;
10111+
callback = options.jo_callback;
1009510112
}
1009610113
/* Set the callback. An empty callback means no callback and not reading
1009710114
* the response. */
@@ -14511,17 +14528,15 @@ f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
1451114528

1451214529
/* Default mode is NL. */
1451314530
options.jo_mode = MODE_NL;
14531+
options.jo_callback = NULL;
1451414532
if (argvars[1].v_type != VAR_UNKNOWN)
1451514533
{
14516-
dict_T *dict;
14517-
1451814534
if (argvars[1].v_type != VAR_DICT)
1451914535
{
1452014536
EMSG(_(e_invarg));
1452114537
return;
1452214538
}
14523-
dict = argvars[1].vval.v_dict;
14524-
if (get_mode_arg(dict, &options) == FAIL)
14539+
if (get_job_options(argvars[1].vval.v_dict, &options) == FAIL)
1452514540
return;
1452614541
}
1452714542

src/os_unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5127,7 +5127,7 @@ mch_start_job(char **argv, job_T *job, jobopt_T *options)
51275127
# ifdef FEAT_CHANNEL
51285128
channel_set_pipes(channel, fd_in[1], fd_out[0], fd_err[0]);
51295129
channel_set_job(channel, job);
5130-
channel_set_mode(channel, options->jo_mode);
5130+
channel_set_options(channel, options);
51315131
# ifdef FEAT_GUI
51325132
channel_gui_register(channel);
51335133
# endif

src/os_win32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5125,7 +5125,7 @@ mch_start_job(char *cmd, job_T *job, jobopt_T *options)
51255125
job->jv_channel = channel;
51265126
channel_set_pipes(channel, (sock_T)ifd[1], (sock_T)ofd[0], (sock_T)efd[0]);
51275127
channel_set_job(channel, job);
5128-
channel_set_mode(channel, options->jo_mode);
5128+
channel_set_options(channel, options);
51295129

51305130
# ifdef FEAT_GUI
51315131
channel_gui_register(channel);

src/proto/channel.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ void channel_gui_register_all(void);
77
channel_T *channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void));
88
void channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err);
99
void channel_set_job(channel_T *channel, job_T *job);
10-
void channel_set_mode(channel_T *channel, ch_mode_T ch_mode);
10+
void channel_set_mode(channel_T *channel, ch_mode_T mode);
1111
void channel_set_timeout(channel_T *channel, int timeout);
1212
void channel_set_callback(channel_T *channel, char_u *callback);
13+
void channel_set_options(channel_T *channel, jobopt_T *options);
1314
void channel_set_req_callback(channel_T *channel, char_u *callback, int id);
1415
char_u *channel_get(channel_T *channel);
1516
int channel_collapse(channel_T *channel);

src/structs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,11 +1373,12 @@ struct channel_S {
13731373
};
13741374

13751375
/*
1376-
* Options for job commands.
1376+
* Options for job and channel commands.
13771377
*/
13781378
typedef struct
13791379
{
1380-
ch_mode_T jo_mode;
1380+
ch_mode_T jo_mode; /* "mode" */
1381+
char_u *jo_callback; /* "callback", not allocated! */
13811382
} jobopt_T;
13821383

13831384

0 commit comments

Comments
 (0)