diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 8994c313b3a399..6caa42d1722e91 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -4024,10 +4024,22 @@ jobsend({job}, {data}) {Nvim} *jobsend()* :call jobsend(j, ["abc", "123\n456", ""]) < will send "abc123456". -jobstart({name}, {prog}[, {argv}]) {Nvim} *jobstart()* - Spawns {prog} as a job and associate it with the {name} string, - which will be used to match the "filename pattern" in - |JobActivity| events. It returns: +jobstart({argv}[, {opts}]) {Nvim} *jobstart()* + Spawns {argv}(list) as a job. If passed, {opts} must be a + dictionary with the any of the following keys: + - on_stdout: stdout event handler + - on_stderr: stderr event handler + - on_exit: exit event handler + - user: user data passed to all callbacks + - pty: If set, the job will be connected to a new pseudo + terminal, and the job streams are connected to the master + file descriptor. + - width: Width of the terminal screen(only if pty is set) + - height: Height of the terminal screen(only if pty is set) + - TERM: $TERM environment variable(only if pty is set) + Either funcrefs or function names can be passed as event + handlers. + Returns: - The job id on success, which is used by |jobsend()| and |jobstop()| - 0 when the job table is full or on invalid arguments diff --git a/runtime/doc/job_control.txt b/runtime/doc/job_control.txt index 000409597fe0e9..bf19619e0c5662 100644 --- a/runtime/doc/job_control.txt +++ b/runtime/doc/job_control.txt @@ -38,26 +38,28 @@ for details 2. Usage *job-control-usage* Job control is achieved by calling a combination of the |jobstart()|, -|jobsend()| and |jobstop()| functions, and by listening to the |JobActivity| -event. The best way to understand is with a complete example: +|jobsend()| and |jobstop()| functions. The best way to understand is with a +complete example: > - set nocp - let job1 = jobstart('shell1', 'bash') - let job2 = jobstart('shell2', 'bash', ['-c', 'for ((i = 0; i < 10; i++)); do echo hello $i!; sleep 1; done']) - - function JobHandler() - if v:job_data[1] == 'stdout' - let str = 'shell '. v:job_data[0].' stdout: '.join(v:job_data[2]) - elseif v:job_data[1] == 'stderr' - let str = 'shell '.v:job_data[0].' stderr: '.join(v:job_data[2]) + function s:JobHandler(job_id, data, user, event) + if a:event == 'stdout' + let str = a:user.' stdout: '.join(a:data) + elseif a:event == 'stderr' + let str = a:user.' stderr: '.join(a:data) else - let str = 'shell '.v:job_data[0].' exited' + let str = a:user.' exited' endif call append(line('$'), str) endfunction + let s:callbacks = { + \ 'on_stdout': function('s:JobHandler'), + \ 'on_stderr': function('s:JobHandler'), + \ 'on_exit': function('s:JobHandler') + \ } + let job1 = jobstart(['bash'], extend({'user': 'shell 1'}, s:callbacks)) + let job2 = jobstart(['bash', '-c', 'for ((i = 0; i < 10; i++)); do echo hello $i!; sleep 1; done'], extend({'user': 'shell 2'}, s:callbacks)) - au JobActivity shell* call JobHandler() < To test the above, copy it to the file ~/jobcontrol.vim and start with a clean nvim instance: @@ -72,15 +74,19 @@ Here's what is happening: - The second shell is started with the -c argument, causing it to execute a command then exit. In this case, the command is a for loop that will print 0 through 9 then exit. -- The |JobHandler()| function is called by the `JobActivity` autocommand (notice - how the shell* pattern matches the names `shell1` and `shell2` passed to - |jobstart()|), and it takes care of displaying stdout/stderr received from +- The `JobHandler()` function is a callback passed to |jobstart()| to handle + various job events. It takes care of displaying stdout/stderr received from the shells. -- The v:job_data is an array set by the JobActivity event. It has the - following elements: +- The arguments passed to `JobHandler()` are: + 0: The job id - 1: The kind of activity: one of "stdout", "stderr" or "exit" - 2: When "activity" is "stdout" or "stderr", this will contain a list of + 1: If the event is 'stdout' or 'stderr', a list with lines read from the + corresponding stream. For 'exit', it is the status returned by the + program. + 2: User data set in the "user" option passed to |jobstart()|. Can be any + vimscript object and is used to identify the context when multiple + |jobstart()| calls share a single set of callbacks. + 3: is "stdout" or "stderr", this will contain a list of lines read from stdout or stderr To send data to the job's stdin, one can use the |jobsend()| function, like diff --git a/src/nvim/eval.c b/src/nvim/eval.c index b0fbcd60cdd96d..d7a3fc55f9c02b 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -430,7 +430,6 @@ static struct vimvar { {VV_NAME("oldfiles", VAR_LIST), 0}, {VV_NAME("windowid", VAR_NUMBER), VV_RO}, {VV_NAME("progpath", VAR_STRING), VV_RO}, - {VV_NAME("job_data", VAR_LIST), 0}, {VV_NAME("command_output", VAR_STRING), 0} }; @@ -450,7 +449,8 @@ typedef struct { Terminal *term; bool exited; int refcount; - char *autocmd_file; + ufunc_T *on_stdout, *on_stderr, *on_exit; + typval_T user; } TerminalJobData; @@ -463,9 +463,12 @@ typedef struct { valid character */ // Memory pool for reusing JobEvent structures typedef struct { - int id; - char *name, *type; + int job_id; + TerminalJobData *data; + ufunc_T *callback; + const char *type; list_T *received; + int status; } JobEvent; #define JobEventFreer(x) KMEMPOOL_INIT(JobEventPool, JobEvent, JobEventFreer) @@ -5936,6 +5939,33 @@ dictitem_T *dict_find(dict_T *d, char_u *key, int len) return HI2DI(hi); } +// Get a function from a dictionary +static ufunc_T *get_dict_callback(dict_T *d, char *key) +{ + dictitem_T *di = dict_find(d, (uint8_t *)key, -1); + + if (di == NULL) { + return NULL; + } + + if (di->di_tv.v_type != VAR_FUNC && di->di_tv.v_type != VAR_STRING) { + EMSG(_("Argument is not a function or function name")); + return NULL; + } + + uint8_t *name = di->di_tv.vval.v_string; + name = trans_function_name(&name, false, TFN_INT|TFN_QUIET, NULL); + ufunc_T *rv = find_func(name); + free(name); + if (!rv) { + EMSG2(_("Function %s doesn't exist"), name); + return NULL; + } + rv->uf_refcount++; + + return rv; +} + /* * Get a string item from a dictionary. * When "save" is TRUE allocate memory for it. @@ -6500,7 +6530,7 @@ static struct fst { {"items", 1, 1, f_items}, {"jobresize", 3, 3, f_jobresize}, {"jobsend", 2, 2, f_jobsend}, - {"jobstart", 2, 4, f_jobstart}, + {"jobstart", 1, 2, f_jobstart}, {"jobstop", 1, 1, f_jobstop}, {"join", 1, 2, f_join}, {"keys", 1, 1, f_keys}, @@ -6920,24 +6950,9 @@ call_func ( else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) error = ERROR_DICT; else { - /* - * Call the user function. - * Save and restore search patterns, script variables and - * redo buffer. - */ - save_search_patterns(); - saveRedobuff(); - ++fp->uf_calls; - call_user_func(fp, argcount, argvars, rettv, - firstline, lastline, + // Call the user function. + call_user_func(fp, argcount, argvars, rettv, firstline, lastline, (fp->uf_flags & FC_DICT) ? selfdict : NULL); - if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name) - && fp->uf_refcount <= 0) - /* Function was unreferenced while being used, free it - * now. */ - func_free(fp); - restoreRedobuff(); - restore_search_patterns(); error = ERROR_NONE; } } @@ -10714,11 +10729,6 @@ static void f_jobresize(typval_T *argvars, typval_T *rettv) // "jobstart()" function static void f_jobstart(typval_T *argvars, typval_T *rettv) { - list_T *args = NULL; - listitem_T *arg; - int i, argvl, argsl; - char **argv = NULL; - rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -10726,55 +10736,62 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv) return; } - if (argvars[0].v_type != VAR_STRING - || argvars[1].v_type != VAR_STRING - || (argvars[2].v_type != VAR_LIST && argvars[2].v_type != VAR_UNKNOWN)) { + if (argvars[0].v_type != VAR_LIST + || (argvars[1].v_type != VAR_DICT && argvars[1].v_type != VAR_UNKNOWN)) { // Wrong argument types EMSG(_(e_invarg)); return; } - argsl = 0; - if (argvars[2].v_type == VAR_LIST) { - args = argvars[2].vval.v_list; - argsl = args->lv_len; - // Assert that all list items are strings - for (arg = args->lv_first; arg != NULL; arg = arg->li_next) { - if (arg->li_tv.v_type != VAR_STRING) { - EMSG(_(e_invarg)); - return; - } + list_T *args = argvars[0].vval.v_list; + // Assert that all list items are strings + for (listitem_T *arg = args->lv_first; arg != NULL; arg = arg->li_next) { + if (arg->li_tv.v_type != VAR_STRING) { + EMSG(_(e_invarg)); + return; } } - if (!os_can_exe(get_tv_string(&argvars[1]), NULL)) { - // String is not executable - EMSG2(e_jobexe, get_tv_string(&argvars[1])); + int argc = args->lv_len; + if (!argc) { + EMSG(_("Argument vector must have at least one item")); return; } - // Allocate extra memory for the argument vector and the NULL pointer - argvl = argsl + 2; - argv = xmalloc(sizeof(char_u *) * argvl); - - // Copy program name - argv[0] = xstrdup((char *)get_tv_string(&argvars[1])); + if (!os_can_exe(args->lv_first->li_tv.vval.v_string, NULL)) { + // String is not executable + EMSG2(e_jobexe, args->lv_first->li_tv.vval.v_string); + return; + } - i = 1; - // Copy arguments to the vector - if (argsl > 0) { - for (arg = args->lv_first; arg != NULL; arg = arg->li_next) { - argv[i++] = xstrdup((char *)get_tv_string(&arg->li_tv)); + typval_T user; + init_tv(&user); + dict_T *job_opts = NULL; + ufunc_T *on_stdout = NULL, *on_stderr = NULL, *on_exit = NULL; + if (argvars[1].v_type == VAR_DICT) { + job_opts = argvars[1].vval.v_dict; + common_job_callbacks(job_opts, &on_stdout, &on_stderr, &on_exit, &user); + if (did_emsg) { + return; } } - // The last item of argv must be NULL - argv[i] = NULL; - JobOptions opts = common_job_options(argv, (char *)argvars[0].vval.v_string); + // Build the argument vector + int i = 0; + char **argv = xcalloc(argc + 1, sizeof(char *)); + for (listitem_T *arg = args->lv_first; arg != NULL; arg = arg->li_next) { + argv[i++] = xstrdup((char *)arg->li_tv.vval.v_string); + } + + JobOptions opts = common_job_options(argv, on_stdout, on_stderr, on_exit, + user); - if (args && argvars[3].v_type == VAR_DICT) { - dict_T *job_opts = argvars[3].vval.v_dict; - opts.pty = true; + if (!job_opts) { + goto start; + } + + opts.pty = get_dict_number(job_opts, (uint8_t *)"pty"); + if (opts.pty) { uint16_t width = get_dict_number(job_opts, (uint8_t *)"width"); if (width > 0) { opts.width = width; @@ -10789,6 +10806,16 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv) } } +start: + if (!on_stdout) { + opts.stdout_cb = NULL; + } + if (!on_stderr) { + opts.stderr_cb = NULL; + } + if (!on_exit) { + opts.exit_cb = NULL; + } common_job_start(opts, rettv); } @@ -14890,15 +14917,26 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) } if (argvars[0].v_type != VAR_STRING - || (argvars[1].v_type != VAR_STRING - && argvars[1].v_type != VAR_UNKNOWN)) { + || (argvars[1].v_type != VAR_DICT && argvars[1].v_type != VAR_UNKNOWN)) { // Wrong argument types EMSG(_(e_invarg)); return; } + typval_T user; + init_tv(&user); + ufunc_T *on_stdout = NULL, *on_stderr = NULL, *on_exit = NULL; + if (argvars[1].v_type == VAR_DICT) { + dict_T *job_opts = argvars[1].vval.v_dict; + common_job_callbacks(job_opts, &on_stdout, &on_stderr, &on_exit, &user); + if (did_emsg) { + return; + } + } + char **argv = shell_build_argv((char *)argvars[0].vval.v_string, NULL); - JobOptions opts = common_job_options(argv, NULL); + JobOptions opts = common_job_options(argv, on_stdout, on_stderr, on_exit, + user); opts.pty = true; opts.width = curwin->w_width; opts.height = curwin->w_height; @@ -14930,7 +14968,6 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) snprintf(buf, sizeof(buf), "term://%s//%d:%s", cwd, pid, (char *)argvars[0].vval.v_string); (void)setfname(curbuf, (uint8_t *)buf, NULL, true); - data->autocmd_file = xstrdup(buf); // Save the job id and pid in b:terminal_job_{id,pid} Error err; dict_set_value(curbuf->b_vars, cstr_as_string("term_job_id"), @@ -17836,7 +17873,6 @@ void ex_function(exarg_T *eap) fudi.fd_di->di_tv.v_type = VAR_FUNC; fudi.fd_di->di_tv.v_lock = 0; fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); - fp->uf_refcount = 1; /* behave like "dict" was used */ flags |= FC_DICT; @@ -17846,6 +17882,7 @@ void ex_function(exarg_T *eap) STRCPY(fp->uf_name, name); hash_add(&func_hashtab, UF2HIKEY(fp)); } + fp->uf_refcount = 1; fp->uf_args = newargs; fp->uf_lines = newlines; fp->uf_tml_count = NULL; @@ -18519,6 +18556,11 @@ void ex_delfunction(exarg_T *eap) EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); return; } + if (fp->uf_refcount > 1) { + EMSG2(_("Cannot delete function %s: It is being used internally"), + eap->arg); + return; + } if (fudi.fd_dict != NULL) { /* Delete the dict item that refers to the function, it will @@ -18563,13 +18605,21 @@ void func_unref(char_u *name) if (name != NULL && isdigit(*name)) { fp = find_func(name); - if (fp == NULL) + if (fp == NULL) { EMSG2(_(e_intern2), "func_unref()"); - else if (--fp->uf_refcount <= 0) { - /* Only delete it when it's not being used. Otherwise it's done - * when "uf_calls" becomes zero. */ - if (fp->uf_calls == 0) - func_free(fp); + } else { + user_func_unref(fp); + } + } +} + +static void user_func_unref(ufunc_T *fp) +{ + if (--fp->uf_refcount <= 0) { + // Only delete it when it's not being used. Otherwise it's done + // when "uf_calls" becomes zero. + if (fp->uf_calls == 0) { + func_free(fp); } } } @@ -18626,9 +18676,13 @@ call_user_func ( return; } ++depth; - - line_breakcheck(); /* check for CTRL-C hit */ - + // Save search patterns and redo buffer. + save_search_patterns(); + saveRedobuff(); + ++fp->uf_calls; + // check for CTRL-C hit + line_breakcheck(); + // prepare the funccall_T structure fc = xmalloc(sizeof(funccall_T)); fc->caller = current_funccal; current_funccal = fc; @@ -18924,6 +18978,14 @@ call_user_func ( for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next) copy_tv(&li->li_tv, &li->li_tv); } + + if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name) && fp->uf_refcount <= 0) { + // Function was unreferenced while being used, free it now. + func_free(fp); + } + // restore search patterns and redo buffer + restoreRedobuff(); + restore_search_patterns(); } /* @@ -19814,12 +19876,14 @@ char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, char_u *flags) return ret; } -static inline JobOptions common_job_options(char **argv, char *autocmd_file) +static inline JobOptions common_job_options(char **argv, ufunc_T *on_stdout, + ufunc_T *on_stderr, ufunc_T *on_exit, typval_T user) { TerminalJobData *data = xcalloc(1, sizeof(TerminalJobData)); - if (autocmd_file) { - data->autocmd_file = xstrdup(autocmd_file); - } + data->on_stdout = on_stdout; + data->on_stderr = on_stderr; + data->on_exit = on_exit; + data->user = user; JobOptions opts = JOB_OPTIONS_INIT; opts.argv = argv; opts.data = data; @@ -19829,6 +19893,25 @@ static inline JobOptions common_job_options(char **argv, char *autocmd_file) return opts; } +static inline void common_job_callbacks(dict_T *vopts, ufunc_T **on_stdout, + ufunc_T **on_stderr, ufunc_T **on_exit, typval_T *user) +{ + *on_stdout = get_dict_callback(vopts, "on_stdout"); + *on_stderr = get_dict_callback(vopts, "on_stderr"); + *on_exit = get_dict_callback(vopts, "on_exit"); + if (did_emsg) { + return; + } + dictitem_T *di = dict_find(vopts, (uint8_t *)"user", -1); + if (di != NULL) { + copy_tv(&di->di_tv, user); + } else { + user->v_type = VAR_NUMBER; + user->v_lock = 0; + user->vval.v_number = 0; + } +} + static inline Job *common_job_start(JobOptions opts, typval_T *rettv) { TerminalJobData *data = opts.data; @@ -19839,8 +19922,7 @@ static inline Job *common_job_start(JobOptions opts, typval_T *rettv) if (rettv->vval.v_number == 0) { EMSG(_(e_jobtblfull)); free(opts.term_name); - free(data->autocmd_file); - free(data); + free_term_job_data(data); } else { EMSG(_(e_jobexe)); } @@ -19850,10 +19932,23 @@ static inline Job *common_job_start(JobOptions opts, typval_T *rettv) return job; } -// JobActivity autocommands will execute vimscript code, so it must be executed -// on Nvim main loop -static inline void push_job_event(Job *job, char *type, char *data, - size_t count) +static inline void free_term_job_data(TerminalJobData *data) { + if (data->on_stdout) { + user_func_unref(data->on_stdout); + } + if (data->on_stderr) { + user_func_unref(data->on_stderr); + } + if (data->on_exit) { + user_func_unref(data->on_exit); + } + clear_tv(&data->user); + free(data); +} + +// vimscript job callbacks must be executed on Nvim main loop +static inline void push_job_event(Job *job, ufunc_T *callback, + const char *type, char *data, size_t count, int status) { JobEvent *event_data = kmp_alloc(JobEventPool, job_event_pool); event_data->received = NULL; @@ -19880,10 +19975,12 @@ static inline void push_job_event(Job *job, char *type, char *data, off++; } list_append_string(event_data->received, (uint8_t *)ptr, off); + } else { + event_data->status = status; } - TerminalJobData *d = job_data(job); - event_data->id = job_id(job); - event_data->name = d->autocmd_file; + event_data->job_id = job_id(job); + event_data->data = job_data(job); + event_data->callback = callback; event_data->type = type; event_push((Event) { .handler = on_job_event, @@ -19891,17 +19988,20 @@ static inline void push_job_event(Job *job, char *type, char *data, }, true); } -static void on_job_stdout(RStream *rstream, void *data, bool eof) +static void on_job_stdout(RStream *rstream, void *job, bool eof) { - on_job_output(rstream, data, eof, "stdout"); + TerminalJobData *data = job_data(job); + on_job_output(rstream, job, eof, data->on_stdout, "stdout"); } -static void on_job_stderr(RStream *rstream, void *data, bool eof) +static void on_job_stderr(RStream *rstream, void *job, bool eof) { - on_job_output(rstream, data, eof, "stderr"); + TerminalJobData *data = job_data(job); + on_job_output(rstream, job, eof, data->on_stderr, "stderr"); } -static void on_job_output(RStream *rstream, Job *job, bool eof, char *type) +static void on_job_output(RStream *rstream, Job *job, bool eof, + ufunc_T *callback, const char *type) { if (eof) { return; @@ -19917,21 +20017,28 @@ static void on_job_output(RStream *rstream, Job *job, bool eof, char *type) terminal_receive(data->term, ptr, len); } - push_job_event(job, type, ptr, len); + if (callback) { + push_job_event(job, callback, type, ptr, len, 0); + } + rbuffer_consumed(rstream_buffer(rstream), len); } -static void on_job_exit(Job *job, void *d) +static void on_job_exit(Job *job, int status, void *d) { TerminalJobData *data = d; - push_job_event(job, "exit", NULL, 0); if (data->term && !data->exited) { data->exited = true; terminal_close(data->term, _("\r\n[Program exited, press any key to close]")); } - term_job_data_decref(data); + + if (data->on_exit) { + push_job_event(job, data->on_exit, "exit", NULL, 0, status); + } else { + term_job_data_decref(data); + } } static void term_write(char *buf, size_t size, void *data) @@ -19960,42 +20067,55 @@ static void term_close(void *d) static void term_job_data_decref(TerminalJobData *data) { if (!(--data->refcount)) { - free(data); + free_term_job_data(data); } } static void on_job_event(Event event) { - JobEvent *data = event.data; - apply_job_autocmds(data->id, data->name, data->type, data->received); - kmp_free(JobEventPool, job_event_pool, data); -} + JobEvent *ev = event.data; + typval_T argv[4]; + int argc = ev->callback->uf_args.ga_len; -static void apply_job_autocmds(int id, char *name, char *type, - list_T *received) -{ - // Create the list which will be set to v:job_data - list_T *list = list_alloc(); - list_append_number(list, id); - list_append_string(list, (uint8_t *)type, -1); + if (argc > 0) { + argv[0].v_type = VAR_NUMBER; + argv[0].v_lock = 0; + argv[0].vval.v_number = ev->job_id; + } + + if (argc > 1) { + if (ev->received) { + argv[1].v_type = VAR_LIST; + argv[1].v_lock = 0; + argv[1].vval.v_list = ev->received; + argv[1].vval.v_list->lv_refcount++; + } else { + argv[1].v_type = VAR_NUMBER; + argv[1].v_lock = 0; + argv[1].vval.v_number = ev->status; + } + } - if (received) { - listitem_T *str_slot = listitem_alloc(); - str_slot->li_tv.v_type = VAR_LIST; - str_slot->li_tv.v_lock = 0; - str_slot->li_tv.vval.v_list = received; - str_slot->li_tv.vval.v_list->lv_refcount++; - list_append(list, str_slot); + if (argc > 2) { + argv[2] = ev->data->user; } - // Update v:job_data for the autocommands - set_vim_var_list(VV_JOB_DATA, list); - // Call JobActivity autocommands - apply_autocmds(EVENT_JOBACTIVITY, (uint8_t *)name, NULL, TRUE, NULL); + if (argc > 3) { + argv[3].v_type = VAR_STRING; + argv[3].v_lock = 0; + argv[3].vval.v_string = (uint8_t *)ev->type; + } - if (!received) { - // This must be the exit event. Free the name. - free(name); + typval_T rettv; + init_tv(&rettv); + call_user_func(ev->callback, argc, argv, &rettv, curwin->w_cursor.lnum, + curwin->w_cursor.lnum, NULL); + clear_tv(&rettv); + kmp_free(JobEventPool, job_event_pool, ev); + + if (!ev->received) { + // exit event, safe to free job data now + term_job_data_decref(ev->data); } } diff --git a/src/nvim/eval.h b/src/nvim/eval.h index e96106dfb33b19..61a65df7c6fdea 100644 --- a/src/nvim/eval.h +++ b/src/nvim/eval.h @@ -63,7 +63,6 @@ enum { VV_OLDFILES, VV_WINDOWID, VV_PROGPATH, - VV_JOB_DATA, VV_COMMAND_OUTPUT, VV_LEN, /* number of v: vars */ }; diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 0c0b786405d176..923c4fd1b65a4f 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5187,7 +5187,6 @@ static struct event_name { {"InsertEnter", EVENT_INSERTENTER}, {"InsertLeave", EVENT_INSERTLEAVE}, {"InsertCharPre", EVENT_INSERTCHARPRE}, - {"JobActivity", EVENT_JOBACTIVITY}, {"MenuPopup", EVENT_MENUPOPUP}, {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, @@ -6595,7 +6594,6 @@ apply_autocmds_group ( || event == EVENT_QUICKFIXCMDPRE || event == EVENT_COLORSCHEME || event == EVENT_QUICKFIXCMDPOST - || event == EVENT_JOBACTIVITY || event == EVENT_TABCLOSED) fname = vim_strsave(fname); else diff --git a/src/nvim/fileio.h b/src/nvim/fileio.h index 6241cabd2aeff4..b77e2311fcf1c5 100644 --- a/src/nvim/fileio.h +++ b/src/nvim/fileio.h @@ -63,7 +63,6 @@ typedef enum auto_event { EVENT_INSERTCHANGE, /* when changing Insert/Replace mode */ EVENT_INSERTENTER, /* when entering Insert mode */ EVENT_INSERTLEAVE, /* when leaving Insert mode */ - EVENT_JOBACTIVITY, /* when job sent some data */ EVENT_MENUPOPUP, /* just before popup menu is displayed */ EVENT_QUICKFIXCMDPOST, /* after :make, :grep etc. */ EVENT_QUICKFIXCMDPRE, /* before :make, :grep etc. */ diff --git a/src/nvim/main.c b/src/nvim/main.c index 1e5c2df3946198..732c6c1351d616 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -293,8 +293,8 @@ int main(int argc, char **argv) "matchstr(expand(\"\"), " "'\\c\\mterm://\\%(.\\{-}//\\%(\\d\\+:\\)\\?\\)\\?\\zs.*'), " // capture the working directory - "get(matchlist(expand(\"\"), " - "'\\c\\mterm://\\(.\\{-}\\)//'), 1, ''))"); + "{'cwd': get(matchlist(expand(\"\"), " + "'\\c\\mterm://\\(.\\{-}\\)//'), 1, ''))}"); /* Execute --cmd arguments. */ exe_pre_commands(¶ms); diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c index 00b8cd072f427e..329b898824bed0 100644 --- a/src/nvim/msgpack_rpc/channel.c +++ b/src/nvim/msgpack_rpc/channel.c @@ -347,7 +347,7 @@ static void job_err(RStream *rstream, void *data, bool eof) } } -static void job_exit(Job *job, void *data) +static void job_exit(Job *job, int status, void *data) { decref(data); } diff --git a/src/nvim/os/job_defs.h b/src/nvim/os/job_defs.h index ac9a37b366ff17..200cf75e5914d9 100644 --- a/src/nvim/os/job_defs.h +++ b/src/nvim/os/job_defs.h @@ -11,7 +11,7 @@ typedef struct job Job; /// /// @param id The job id /// @param data Some data associated with the job by the caller -typedef void (*job_exit_cb)(Job *job, void *data); +typedef void (*job_exit_cb)(Job *job, int status, void *data); // Job startup options // job_exit_cb Callback that will be invoked when the job exits diff --git a/src/nvim/os/job_private.h b/src/nvim/os/job_private.h index b1d5e13feb7b48..af13d2e63604d4 100644 --- a/src/nvim/os/job_private.h +++ b/src/nvim/os/job_private.h @@ -88,7 +88,7 @@ static inline void job_exit_callback(Job *job) if (job->opts.exit_cb) { // Invoke the exit callback - job->opts.exit_cb(job, job->opts.data); + job->opts.exit_cb(job, job->status, job->opts.data); } if (stop_requests && !--stop_requests) { diff --git a/test/functional/job/job_spec.lua b/test/functional/job/job_spec.lua index 8981c497448099..ca91804cc112ef 100644 --- a/test/functional/job/job_spec.lua +++ b/test/functional/job/job_spec.lua @@ -1,10 +1,11 @@ local helpers = require('test.functional.helpers') -local clear, nvim, eq, neq, ok, expect, eval, next_message, run, stop, session +local clear, nvim, eq, neq, ok, expect, eval, next_msg, run, stop, session = helpers.clear, helpers.nvim, helpers.eq, helpers.neq, helpers.ok, helpers.expect, helpers.eval, helpers.next_message, helpers.run, helpers.stop, helpers.session local nvim_dir, insert = helpers.nvim_dir, helpers.insert +local source = helpers.source describe('jobs', function() @@ -13,46 +14,42 @@ describe('jobs', function() before_each(function() clear() channel = nvim('get_api_info')[1] + nvim('set_var', 'channel', channel) + source([[ + function! s:OnEvent(id, data, user, event) + call rpcnotify(g:channel, a:event, a:user, a:data) + endfunction + let g:job_opts = { + \ 'on_stdout': function('s:OnEvent'), + \ 'on_stderr': function('s:OnEvent'), + \ 'on_exit': function('s:OnEvent') + \ } + ]]) end) - -- Creates the string to make an autocmd to notify us. - local notify_str = function(expr1, expr2) - local str = "au! JobActivity xxx call rpcnotify("..channel..", "..expr1 - if expr2 ~= nil then - str = str..", "..expr2 - end - return str..")" - end - - local notify_job = function() - return "au! JobActivity xxx call rpcnotify("..channel..", 'j', v:job_data)" - end - it('returns 0 when it fails to start', function() - local status, rv = pcall(eval, "jobstart('', '')") + local status, rv = pcall(eval, "jobstart([])") eq(false, status) ok(rv ~= nil) end) - it('calls JobActivity when the job writes and exits', function() - nvim('command', notify_str('v:job_data[1]')) - nvim('command', "call jobstart('xxx', 'echo')") - eq({'notification', 'stdout', {}}, next_message()) - eq({'notification', 'exit', {}}, next_message()) + it('invokes callbacks when the job writes and exits', function() + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'stdout', {0, {'', ''}}}, next_msg()) + eq({'notification', 'exit', {0, 0}}, next_msg()) end) it('allows interactive commands', function() - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") neq(0, eval('j')) nvim('command', 'call jobsend(j, "abc\\n")') - eq({'notification', 'stdout', {{'abc', ''}}}, next_message()) + eq({'notification', 'stdout', {0, {'abc', ''}}}, next_msg()) nvim('command', 'call jobsend(j, "123\\nxyz\\n")') - eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message()) + eq({'notification', 'stdout', {0, {'123', 'xyz', ''}}}, next_msg()) nvim('command', 'call jobsend(j, [123, "xyz", ""])') - eq({'notification', 'stdout', {{'123', 'xyz', ''}}}, next_message()) + eq({'notification', 'stdout', {0, {'123', 'xyz', ''}}}, next_msg()) nvim('command', "call jobstop(j)") - eq({'notification', 'exit', {0}}, next_message()) + eq({'notification', 'exit', {0, 0}}, next_msg()) end) it('preserves NULs', function() @@ -63,90 +60,126 @@ describe('jobs', function() file:close() -- v:job_data preserves NULs. - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['"..filename.."'])") - eq({'notification', 'stdout', {{'abc\ndef', ''}}}, next_message()) - eq({'notification', 'exit', {0}}, next_message()) + nvim('command', "let j = jobstart(['cat', '"..filename.."'], g:job_opts)") + eq({'notification', 'stdout', {0, {'abc\ndef', ''}}}, next_msg()) + eq({'notification', 'exit', {0, 0}}, next_msg()) os.remove(filename) -- jobsend() preserves NULs. - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") nvim('command', [[call jobsend(j, ["123\n456",""])]]) - eq({'notification', 'stdout', {{'123\n456', ''}}}, next_message()) + eq({'notification', 'stdout', {0, {'123\n456', ''}}}, next_msg()) nvim('command', "call jobstop(j)") end) it('will not buffer data if it doesnt end in newlines', function() - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") nvim('command', 'call jobsend(j, "abc\\nxyz")') - eq({'notification', 'stdout', {{'abc', 'xyz'}}}, next_message()) + eq({'notification', 'stdout', {0, {'abc', 'xyz'}}}, next_msg()) nvim('command', "call jobstop(j)") - eq({'notification', 'exit', {0}}, next_message()) + eq({'notification', 'exit', {0, 0}}, next_msg()) end) it('can preserve newlines', function() - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") nvim('command', 'call jobsend(j, "a\\n\\nc\\n\\n\\n\\nb\\n\\n")') - eq({'notification', 'stdout', {{'a', '', 'c', '', '', '', 'b', '', ''}}}, - next_message()) + eq({'notification', 'stdout', + {0, {'a', '', 'c', '', '', '', 'b', '', ''}}}, next_msg()) end) it('can preserve nuls', function() - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") nvim('command', 'call jobsend(j, ["\n123\n", "abc\\nxyz\n", ""])') - eq({'notification', 'stdout', {{'\n123\n', 'abc\nxyz\n', ''}}}, - next_message()) + eq({'notification', 'stdout', {0, {'\n123\n', 'abc\nxyz\n', ''}}}, + next_msg()) nvim('command', "call jobstop(j)") - eq({'notification', 'exit', {0}}, next_message()) + eq({'notification', 'exit', {0, 0}}, next_msg()) end) it('can avoid sending final newline', function() - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") nvim('command', 'call jobsend(j, ["some data", "without\nfinal nl"])') - eq({'notification', 'stdout', {{'some data', 'without\nfinal nl'}}}, - next_message()) + eq({'notification', 'stdout', {0, {'some data', 'without\nfinal nl'}}}, + next_msg()) nvim('command', "call jobstop(j)") - eq({'notification', 'exit', {0}}, next_message()) + eq({'notification', 'exit', {0, 0}}, next_msg()) end) - it('will not allow jobsend/stop on a non-existent job', function() eq(false, pcall(eval, "jobsend(-1, 'lol')")) eq(false, pcall(eval, "jobstop(-1)")) end) it('will not allow jobstop twice on the same job', function() - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") neq(0, eval('j')) eq(true, pcall(eval, "jobstop(j)")) eq(false, pcall(eval, "jobstop(j)")) end) it('will not cause a memory leak if we leave a job running', function() - nvim('command', "call jobstart('xxx', 'cat', ['-'])") + nvim('command', "call jobstart(['cat', '-'], g:job_opts)") + end) + + it('can pass numbers as user data', function() + nvim('command', 'let g:job_opts.user = 5') + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'stdout', {5, {'', ''}}}, next_msg()) + eq({'notification', 'exit', {5, 0}}, next_msg()) + end) + + it('can pass strings as user data', function() + nvim('command', 'let g:job_opts.user = "str"') + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'stdout', {'str', {'', ''}}}, next_msg()) + eq({'notification', 'exit', {'str', 0}}, next_msg()) + end) + + it('can pass references as user data', function() + nvim('command', 'let g:job_opts.user = {"some": [0, "user data"]}') + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'stdout', {{some = {0, 'user data'}}, {'', ''}}}, + next_msg()) + eq({'notification', 'exit', {{some = {0, 'user data'}}, 0}}, next_msg()) + end) + + it('can omit data callbacks', function() + nvim('command', 'unlet g:job_opts.on_stdout') + nvim('command', 'unlet g:job_opts.on_stderr') + nvim('command', 'let g:job_opts.user = 5') + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'exit', {5, 0}}, next_msg()) + end) + + it('can omit exit callback', function() + nvim('command', 'unlet g:job_opts.on_exit') + nvim('command', 'let g:job_opts.user = 5') + nvim('command', "call jobstart(['echo'], g:job_opts)") + eq({'notification', 'stdout', {5, {'', ''}}}, next_msg()) + end) + + it('will pass return code with the exit event', function() + nvim('command', 'let g:job_opts.user = 5') + nvim('command', "call jobstart([&sh, '-c', 'return 55'], g:job_opts)") + eq({'notification', 'exit', {5, 55}}, next_msg()) end) -- FIXME need to wait until jobsend succeeds before calling jobstop pending('will only emit the "exit" event after "stdout" and "stderr"', function() - nvim('command', notify_job()) - nvim('command', "let j = jobstart('xxx', 'cat', ['-'])") + nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)") local jobid = nvim('eval', 'j') nvim('eval', 'jobsend(j, "abcdef")') nvim('eval', 'jobstop(j)') - eq({'notification', 'j', {{jobid, 'stdout', {'abcdef'}}}}, next_message()) - eq({'notification', 'j', {{jobid, 'exit'}}}, next_message()) + eq({'notification', 'j', {0, {jobid, 'stdout', {'abcdef'}}}}, next_msg()) + eq({'notification', 'j', {0, {jobid, 'exit'}}}, next_msg()) end) describe('running tty-test program', function() local function next_chunk() local rv = '' while true do - local msg = next_message() - local data = msg[3][1] + local msg = next_msg() + local data = msg[3][2] for i = 1, #data do data[i] = data[i]:gsub('\n', '\000') end @@ -166,9 +199,9 @@ describe('jobs', function() before_each(function() -- the full path to tty-test seems to be required when running on travis. insert(nvim_dir .. '/tty-test') - nvim('command', 'let exec = expand(":p")') - nvim('command', notify_str('v:job_data[1]', 'get(v:job_data, 2)')) - nvim('command', "let j = jobstart('xxx', exec, [], {})") + nvim('command', 'let g:job_opts.pty = 1') + nvim('command', 'let exec = [expand(":p")]') + nvim('command', "let j = jobstart(exec, g:job_opts)") eq('tty ready', next_chunk()) end)