Skip to content

Commit

Permalink
feat(nvim_exec2): implement nvim_exec2()
Browse files Browse the repository at this point in the history
  • Loading branch information
echasnovski committed Jun 20, 2022
1 parent b2ed439 commit 6c3cec5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
24 changes: 24 additions & 0 deletions runtime/doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,30 @@ nvim_exec({src}, {output}) *nvim_exec()*
Output (non-error, non-shell |:!|) if `output` is true,
else empty string.

See also: ~
|execute()|
|nvim_command()|
|nvim_cmd()|

nvim_exec2({src}, {*opts}) *nvim_exec2()*
Executes Vimscript (multiline block of Ex commands), like
anonymous |:source|.

Unlike |nvim_command()| this function supports heredocs,
script-scope (s:), etc.

On execution error: fails with VimL error, updates v:errmsg.

Parameters: ~
{src} Vimscript code
{opts} Optional parameters.
• output: Capture and return all (non-error,
non-shell |:!|) output. Default: `false`.

Return: ~
Output (non-error, non-shell |:!|) if `opts.output` is
true, else empty string.

See also: ~
|execute()|
|nvim_command()|
Expand Down
3 changes: 3 additions & 0 deletions src/nvim/api/keysets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,8 @@ return {
cmd_opts = {
"output";
};
exec = {
"output";
};
}

45 changes: 41 additions & 4 deletions src/nvim/api/vimscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,25 @@
/// @see |nvim_cmd()|
///
/// @param src Vimscript code
/// @param output Capture and return all (non-error, non-shell |:!|) output
/// @param opts Optional parameters.
/// - output: Capture and return all (non-error, non-shell |:!|) output.
/// Default: `false`.
/// @param[out] err Error details (Vim error), if any
/// @return Output (non-error, non-shell |:!|) if `output` is true,
/// @return Output (non-error, non-shell |:!|) if `opts.output` is true,
/// else empty string.
String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
FUNC_API_SINCE(7)
String nvim_exec2(uint64_t channel_id, String src, Dict(exec) *opts, Error *err)
FUNC_API_SINCE(10)
{
Boolean output = false;
if (HAS_KEY(opts->output)) {
if (opts->output.type != kObjectTypeBoolean) {
api_set_error(err, kErrorTypeValidation, "'output' should be boolean");
return (String)STRING_INIT;
}

output = opts->output.data.boolean;
}

const int save_msg_silent = msg_silent;
garray_T *const save_capture_ga = capture_ga;
garray_T capture_local;
Expand Down Expand Up @@ -94,6 +106,31 @@ String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
return (String)STRING_INIT;
}

/// Executes Vimscript (multiline block of Ex commands), like anonymous
/// |:source|.
///
/// Unlike |nvim_command()| this function supports heredocs, script-scope (s:),
/// etc.
///
/// On execution error: fails with VimL error, updates v:errmsg.
///
/// @see |execute()|
/// @see |nvim_command()|
/// @see |nvim_cmd()|
///
/// @param src Vimscript code
/// @param output Capture and return all (non-error, non-shell |:!|) output
/// @param[out] err Error details (Vim error), if any
/// @return Output (non-error, non-shell |:!|) if `output` is true,
/// else empty string.
String nvim_exec(uint64_t channel_id, String src, Boolean output, Error *err)
FUNC_API_SINCE(7)
{
Dict(exec) opts = { 0 };
opts.output = BOOLEAN_OBJ(output);
return nvim_exec2(channel_id, src, &opts, err);
}

/// Executes an Ex command.
///
/// On execution error: fails with VimL error, updates v:errmsg.
Expand Down

0 comments on commit 6c3cec5

Please sign in to comment.