aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/api/vim.c39
-rw-r--r--src/nvim/ex_cmds2.c19
-rw-r--r--src/nvim/ex_docmd.c9
-rw-r--r--test/functional/api/vim_spec.lua79
4 files changed, 72 insertions, 74 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index bf722b4f4e..6763a3a936 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -73,28 +73,29 @@ void api_vim_free_all_mem(void)
map_free(String, handle_T)(namespace_ids);
}
-/// Executes a multiline block of ex-commands from a string.
+/// Executes Vimscript (multiline block of Ex-commands), like anonymous
+/// |:source|.
///
-/// On execution error: fails with VimL error, does not update v:errmsg.
-///
-/// @param src String containing the ex-commands
-/// @param[out] err Error details (Vim error), if any
-void nvim_source(String src, Error *err) FUNC_API_SINCE(7)
-{
- try_start();
- do_source_str(src.data, "nvim_source(..)");
- try_end(err);
-}
-
-/// Executes a multiline block of ex-commands from a string and returns its
-/// (non-error) output. Shell |:!| output is not captured.
+/// Optionally returns (non-error, non-shell |:!|) output.
///
/// On execution error: fails with VimL error, does not update v:errmsg.
///
-/// @param src String containing the ex-commands
+/// @see |execute()|
+/// @see |nvim_command()|
+///
+/// @param src Vimscript code
+/// @param output Capture and return all (non-error, non-shell |:!|) output
/// @param[out] err Error details (Vim error), if any
-String nvim_source_output(String src, Error *err) FUNC_API_SINCE(7)
+String nvim_exec(String src, Boolean output, Error *err)
+ FUNC_API_SINCE(7)
{
+ if (!output) {
+ try_start();
+ do_source_str(src.data, "nvim_exec()");
+ try_end(err);
+ return (String)STRING_INIT;
+ }
+
const int save_msg_silent = msg_silent;
garray_T *const save_capture_ga = capture_ga;
garray_T capture_local;
@@ -103,7 +104,7 @@ String nvim_source_output(String src, Error *err) FUNC_API_SINCE(7)
try_start();
msg_silent++;
capture_ga = &capture_local;
- do_source_str(src.data, "nvim_source_output(..)");
+ do_source_str(src.data, "nvim_exec()");
capture_ga = save_capture_ga;
msg_silent = save_msg_silent;
try_end(err);
@@ -134,6 +135,8 @@ theend:
///
/// On execution error: fails with VimL error, does not update v:errmsg.
///
+/// @see |nvim_exec()|
+///
/// @param command Ex-command string
/// @param[out] err Error details (Vim error), if any
void nvim_command(String command, Error *err)
@@ -436,7 +439,7 @@ theend:
return (String)STRING_INIT;
}
-/// Evaluates a VimL expression (:help expression).
+/// Evaluates a VimL |expression|.
/// Dictionaries and Lists are recursively expanded.
///
/// On execution error: fails with VimL error, does not update v:errmsg.
diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c
index bda7b4e8b5..8479c15b40 100644
--- a/src/nvim/ex_cmds2.c
+++ b/src/nvim/ex_cmds2.c
@@ -3047,6 +3047,9 @@ static char_u *get_str_line(int c, void *cookie, int indent, bool do_concat)
return (char_u *)xstrdup(buf);
}
+/// Executes lines in `src` as Ex commands.
+///
+/// @see do_source()
int do_source_str(const char *cmd, const char *traceback_name)
{
char_u *save_sourcing_name = sourcing_name;
@@ -3055,7 +3058,7 @@ int do_source_str(const char *cmd, const char *traceback_name)
if (save_sourcing_name == NULL) {
sourcing_name = (char_u *)traceback_name;
} else {
- snprintf((char *)sourcing_name_buf, sizeof sourcing_name_buf,
+ snprintf((char *)sourcing_name_buf, sizeof(sourcing_name_buf),
"%s called at %s:%"PRIdLINENR, traceback_name, save_sourcing_name,
save_sourcing_lnum);
sourcing_name = sourcing_name_buf;
@@ -3070,24 +3073,20 @@ int do_source_str(const char *cmd, const char *traceback_name)
current_sctx.sc_sid = SID_STR;
current_sctx.sc_seq = 0;
current_sctx.sc_lnum = save_sourcing_lnum;
- int retval = FAIL;
- do_cmdline(NULL, get_str_line, (void *)&cookie,
- DOCMD_VERBOSE | DOCMD_NOWAIT | DOCMD_REPEAT);
- retval = OK;
- if (got_int) {
- EMSG(_(e_interr));
- }
-
+ int retval = do_cmdline(NULL, get_str_line, (void *)&cookie,
+ DOCMD_VERBOSE | DOCMD_NOWAIT | DOCMD_REPEAT);
current_sctx = save_current_sctx;
sourcing_lnum = save_sourcing_lnum;
sourcing_name = save_sourcing_name;
return retval;
}
-/// Read the file "fname" and execute its lines as EX commands.
+/// Reads the file `fname` and executes its lines as Ex commands.
///
/// This function may be called recursively!
///
+/// @see do_source_str
+///
/// @param fname
/// @param check_other check for .vimrc and _vimrc
/// @param is_vimrc DOSO_ value
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index 743286c64a..dc2726709f 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -421,13 +421,12 @@ int do_cmdline(char_u *cmdline, LineGetter fgetline,
// If force_abort is set, we cancel everything.
did_emsg = false;
- /*
- * KeyTyped is only set when calling vgetc(). Reset it here when not
- * calling vgetc() (sourced command lines).
- */
+ // KeyTyped is only set when calling vgetc(). Reset it here when not
+ // calling vgetc() (sourced command lines).
if (!(flags & DOCMD_KEYTYPED)
- && !getline_equal(fgetline, cookie, getexline))
+ && !getline_equal(fgetline, cookie, getexline)) {
KeyTyped = false;
+ }
/*
* Continue executing command lines:
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
index 9a7744142a..d7baf68a5b 100644
--- a/test/functional/api/vim_spec.lua
+++ b/test/functional/api/vim_spec.lua
@@ -16,6 +16,7 @@ local parse_context = helpers.parse_context
local request = helpers.request
local source = helpers.source
local next_msg = helpers.next_msg
+local tmpname = helpers.tmpname
local write_file = helpers.write_file
local pcall_err = helpers.pcall_err
@@ -75,131 +76,127 @@ describe('API', function()
eq({mode='i', blocking=false}, nvim("get_mode"))
end)
- describe('nvim_source', function()
+ describe('nvim_exec', function()
it('one-line input', function()
- nvim('source', "let x1 = 'a'")
+ nvim('exec', "let x1 = 'a'", false)
eq('a', nvim('get_var', 'x1'))
end)
it(':verbose set {option}?', function()
- nvim('source', 'set nowrap')
+ nvim('exec', 'set nowrap', false)
eq('nowrap\n\tLast set from :source (no file)',
- nvim('source_output', 'verbose set wrap?'))
+ nvim('exec', 'verbose set wrap?', true))
end)
it('multiline input', function()
-- Heredoc + empty lines.
- nvim('source', "let x2 = 'a'\n")
+ nvim('exec', "let x2 = 'a'\n", false)
eq('a', nvim('get_var', 'x2'))
- nvim('source','lua <<EOF\n\n\n\ny=3\n\n\nEOF')
+ nvim('exec','lua <<EOF\n\n\n\ny=3\n\n\nEOF', false)
eq(3, nvim('eval', "luaeval('y')"))
- nvim('source', 'lua <<EOF\ny=3\nEOF')
+ eq('', nvim('exec', 'lua <<EOF\ny=3\nEOF', false))
eq(3, nvim('eval', "luaeval('y')"))
-- Multiple statements
- nvim('source', 'let x1=1\nlet x2=2\nlet x3=3\n')
+ nvim('exec', 'let x1=1\nlet x2=2\nlet x3=3\n', false)
eq(1, nvim('eval', 'x1'))
eq(2, nvim('eval', 'x2'))
eq(3, nvim('eval', 'x3'))
-- Functions
- nvim('source', 'function Foo()\ncall setline(1,["xxx"])\nendfunction')
+ nvim('exec', 'function Foo()\ncall setline(1,["xxx"])\nendfunction', false)
eq(nvim('get_current_line'), '')
- nvim('source', 'call Foo()')
+ nvim('exec', 'call Foo()', false)
eq(nvim('get_current_line'), 'xxx')
-- Autocmds
- nvim('source','autocmd BufAdd * :let x1 = "Hello"')
+ nvim('exec','autocmd BufAdd * :let x1 = "Hello"', false)
nvim('command', 'new foo')
eq('Hello', request('nvim_eval', 'g:x1'))
end)
it('non-ASCII input', function()
- nvim('source', [=[
+ nvim('exec', [=[
new
exe "normal! i ax \n Ax "
:%s/ax/--a1234--/g | :%s/Ax/--A1234--/g
- ]=])
+ ]=], false)
nvim('command', '1')
eq(' --a1234-- ', nvim('get_current_line'))
nvim('command', '2')
eq(' --A1234-- ', nvim('get_current_line'))
- nvim('source', [[
+ nvim('exec', [[
new
call setline(1,['xxx'])
call feedkeys('r')
call feedkeys('ñ', 'xt')
- ]])
+ ]], false)
eq('ñxx', nvim('get_current_line'))
end)
it('execution error', function()
eq('Vim:E492: Not an editor command: bogus_command',
- pcall_err(request, 'nvim_source', 'bogus_command'))
+ pcall_err(request, 'nvim_exec', 'bogus_command', false))
eq('', nvim('eval', 'v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
eq('Vim(buffer):E86: Buffer 23487 does not exist',
- pcall_err(request, 'nvim_source', 'buffer 23487'))
+ pcall_err(request, 'nvim_exec', 'buffer 23487', false))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
end)
it('recursion', function()
- local fname = helpers.tmpname()
+ local fname = tmpname()
write_file(fname, 'let x1 = "set from :source file"\n')
- -- nvim_source
+ -- nvim_exec
-- :source
- -- nvim_source
- request('nvim_source', [[
+ -- nvim_exec
+ request('nvim_exec', [[
let x2 = substitute('foo','o','X','g')
let x4 = 'should be overwritten'
- call nvim_source("source ]]..fname..[[\nlet x3 = substitute('foo','foo','set by recursive nvim_source','g')\nlet x5='overwritten'\nlet x4=x5\n")
- ]])
+ call nvim_exec("source ]]..fname..[[\nlet x3 = substitute('foo','foo','set by recursive nvim_exec','g')\nlet x5='overwritten'\nlet x4=x5\n", v:false)
+ ]], false)
eq('set from :source file', request('nvim_get_var', 'x1'))
eq('fXX', request('nvim_get_var', 'x2'))
- eq('set by recursive nvim_source', request('nvim_get_var', 'x3'))
+ eq('set by recursive nvim_exec', request('nvim_get_var', 'x3'))
eq('overwritten', request('nvim_get_var', 'x4'))
eq('overwritten', request('nvim_get_var', 'x5'))
os.remove(fname)
end)
it('traceback', function()
- local fname = helpers.tmpname()
+ local fname = tmpname()
write_file(fname, 'echo "hello"\n')
- local sourcing_fname = helpers.tmpname()
- write_file(sourcing_fname, 'call nvim_source("source '..fname..'")\n')
- meths.source('set verbose=2')
- print()
+ local sourcing_fname = tmpname()
+ write_file(sourcing_fname, 'call nvim_exec("source '..fname..'", v:false)\n')
+ meths.exec('set verbose=2', false)
local traceback_output = 'line 0: sourcing "'..sourcing_fname..'"\n'..
'line 0: sourcing "'..fname..'"\n'..
'hello\n'..
'finished sourcing '..fname..'\n'..
- 'continuing in nvim_source(..) called at '..sourcing_fname..':1\n'..
+ 'continuing in nvim_exec() called at '..sourcing_fname..':1\n'..
'finished sourcing '..sourcing_fname..'\n'..
- 'continuing in nvim_source(..) called at nvim_source_output(..):0'
- eq(traceback_output, meths.source_output('call nvim_source("source '..sourcing_fname..'")'))
+ 'continuing in nvim_exec() called at nvim_exec():0'
+ eq(traceback_output,
+ meths.exec('call nvim_exec("source '..sourcing_fname..'", v:false)', true))
os.remove(fname)
os.remove(sourcing_fname)
end)
- end)
- describe('nvim_source_output', function()
- it('multiline input', function()
+ it('returns output', function()
eq('this is spinal tap',
- nvim('source_output', 'lua <<EOF\n\n\nprint("this is spinal tap")\n\n\nEOF'))
- end)
-
- it('empty output', function()
- eq('', nvim('source_output', 'echo'))
+ nvim('exec', 'lua <<EOF\n\n\nprint("this is spinal tap")\n\n\nEOF', true))
+ eq('', nvim('exec', 'echo', true))
+ eq('foo 42', nvim('exec', 'echo "foo" 42', true))
end)
end)
describe('nvim_command', function()
it('works', function()
- local fname = helpers.tmpname()
+ local fname = tmpname()
nvim('command', 'new')
nvim('command', 'edit '..fname)
nvim('command', 'normal itesting\napi')