API: nvim_source

This commit is contained in:
Siddhant Gupta
2019-10-06 13:37:54 -07:00
committed by Justin M. Keyes
parent 70b6061666
commit 6aa03e86da
3 changed files with 127 additions and 1 deletions

View File

@@ -25,6 +25,7 @@
#include "nvim/highlight.h"
#include "nvim/window.h"
#include "nvim/types.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/screen.h"
#include "nvim/memline.h"
@@ -72,6 +73,15 @@ void api_vim_free_all_mem(void)
map_free(String, handle_T)(namespace_ids);
}
void nvim_source(String command, Error *err)
FUNC_API_SINCE(5)
{
try_start();
do_source_str((char_u *)command.data);
update_screen(VALID);
try_end(err);
}
/// Executes an ex-command.
///
/// On execution error: fails with VimL error, does not update v:errmsg.

View File

@@ -1193,7 +1193,7 @@ static void script_dump_profile(FILE *fd)
/// profiled.
bool prof_def_func(void)
{
if (current_sctx.sc_sid > 0) {
if (current_sctx.sc_sid > 0 && current_SID < 999999) {
return SCRIPT_ITEM(current_sctx.sc_sid).sn_pr_force;
}
return false;
@@ -3015,6 +3015,44 @@ static FILE *fopen_noinh_readbin(char *filename)
return fdopen(fd_tmp, READBIN);
}
typedef struct {
char_u *buf;
size_t offset;
} GetStrLineCookie;
static char_u *get_str_line(int c, void *cookie, int ident)
{
GetStrLineCookie *p = cookie;
size_t i = p->offset;
if (strlen((char *)p->buf) <= p->offset) {
return NULL;
}
while (!(p->buf[i] == '\n' || p->buf[i] == '\0')) {
i++;
}
char buf[2046];
char *dst;
dst = xstpncpy(buf, (char *)p->buf+p->offset, i - p->offset);
if ((uint32_t)(dst - buf) != i - p->offset) {
smsg(_("nvim_source error parsing command %s"), p->buf);
}
buf[i-p->offset]='\0';
p->offset = i + 1;
return (char_u *)xstrdup(buf);
}
int do_source_str(char_u *cmd)
{
int retval;
GetStrLineCookie cookie = {
.buf = cmd,
.offset = 0,
};
current_SID = 999999;
retval = do_cmdline(NULL, get_str_line, (void *)&cookie,
DOCMD_NOWAIT);
return retval;
}
/// Read the file "fname" and execute its lines as EX commands.
///