startup: fix -es/-Es so they are actually silent

silent-mode (-es/-Es) has been broken for years.  The workaround up to
now was to include --headless.  But --headless is not equivalent because
it prints all messages, not the limited subset defined by silent-mode.
This commit is contained in:
Justin M. Keyes
2018-05-27 03:41:02 +02:00
parent 787ae1b38b
commit 63058fb5b0
4 changed files with 64 additions and 28 deletions

View File

@@ -580,10 +580,6 @@ EXTERN int sandbox INIT(= 0);
/// Batch-mode: "-es" or "-Es" commandline argument was given.
EXTERN int silent_mode INIT(= false);
/// Set to true when sourcing of startup scripts (init.vim) is done.
/// Used for options that cannot be changed after startup scripts.
EXTERN bool did_source_startup_scripts INIT(= false);
/// Start position of active Visual selection.
EXTERN pos_T VIsual;
/// Whether Visual mode is active.

View File

@@ -158,6 +158,7 @@ void event_init(void)
bool event_teardown(void)
{
if (!main_loop.events) {
input_stop();
return true;
}
@@ -298,17 +299,18 @@ int main(int argc, char **argv)
// Set the break level after the terminal is initialized.
debug_break_level = params.use_debug_break_level;
bool reading_excmds = exmode_active == EXMODE_NORMAL;
bool reading_input = !headless_mode
&& (params.input_isatty || params.output_isatty
|| params.err_isatty);
if (reading_input) {
if (reading_input || reading_excmds) {
// One of the startup commands (arguments, sourced scripts or plugins) may
// prompt the user, so start reading from a tty now.
int fd = fileno(stdin);
if (!params.input_isatty || params.edit_type == EDIT_STDIN) {
// Use stderr or stdout since stdin is not a tty and/or could be used to
// read the "-" file (eg: cat file | nvim -)
if (!reading_excmds
&& (!params.input_isatty || params.edit_type == EDIT_STDIN)) {
// Use stderr or stdout since stdin is being used to read commands.
fd = params.err_isatty ? fileno(stderr) : fileno(stdout);
}
input_start(fd);
@@ -332,7 +334,9 @@ int main(int argc, char **argv)
// Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
// Allows for setting 'loadplugins' there.
if (params.use_vimrc != NULL && strequal(params.use_vimrc, "NONE")) {
if (params.use_vimrc != NULL && strequal(params.use_vimrc, "NONE")
// && !silent_mode // XXX: avoid hang with "nvim -es -u NONE".
) {
p_lpl = false;
}
@@ -369,17 +373,21 @@ int main(int argc, char **argv)
mch_exit(0);
}
// Set a few option defaults after reading vimrc files:
// 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
// Set a few option defaults after reading vimrc files: 'title', 'icon',
// 'shellpipe', 'shellredir'.
set_init_3();
TIME_MSG("inits 3");
/*
* "-n" argument: Disable swap file by setting 'updatecount' to 0.
* Note that this overrides anything from a vimrc file.
*/
if (params.no_swap_file)
// "-n" argument: Disable swap file by setting 'updatecount' to 0.
// Note that this overrides anything from a vimrc file.
if (params.no_swap_file) {
p_uc = 0;
}
// XXX: Minimize 'updatetime' for -es/-Es. #7679
if (silent_mode) {
p_ut = 1;
}
if (curwin->w_p_rl && p_altkeymap) {
p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
@@ -437,18 +445,17 @@ int main(int argc, char **argv)
wait_return(true);
}
if (!headless_mode) {
// Stop reading from input stream. UI (if any) will take over.
input_stop();
if (!headless_mode && !silent_mode) {
input_stop(); // Stop reading from input stream. UI will take over.
ui_builtin_start();
}
setmouse(); // may start using the mouse
ui_reset_scroll_region(); // In case Rows changed
if (exmode_active)
if (exmode_active) {
must_redraw = CLEAR; // Don't clear the screen when starting in Ex mode.
else {
} else {
screenclear(); // clear screen
TIME_MSG("clearing screen");
}
@@ -1769,7 +1776,7 @@ static void source_startup_scripts(const mparm_T *const parmp)
|| strequal(parmp->use_vimrc, "NORC")) {
// Do nothing.
} else {
if (do_source((char_u *)parmp->use_vimrc, FALSE, DOSO_NONE) != OK) {
if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) {
EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
}
}
@@ -1810,7 +1817,6 @@ static void source_startup_scripts(const mparm_T *const parmp)
}
secure = 0;
}
did_source_startup_scripts = true;
TIME_MSG("sourcing vimrc file(s)");
}

View File

@@ -64,7 +64,7 @@ void input_start(int fd)
global_fd = fd;
rstream_init_fd(&main_loop, &read_stream, fd, READ_BUFFER_SIZE);
rstream_start(&read_stream, read_cb, NULL);
rstream_start(&read_stream, input_read_cb, NULL);
}
void input_stop(void)
@@ -108,6 +108,11 @@ int os_inchar(uint8_t *buf, int maxlen, int ms, int tb_change_cnt)
}
} else {
if ((result = inbuf_poll((int)p_ut)) == kInputNone) {
if (read_stream.closed && silent_mode) {
// Input drained and eventloop drained: exit silent/batch-mode (-es).
read_error_exit();
}
if (trigger_cursorhold() && !typebuf_changed(tb_change_cnt)) {
create_cursorhold_event();
} else {
@@ -376,11 +381,11 @@ static InbufPollResult inbuf_poll(int ms)
return input_eof ? kInputEof : kInputNone;
}
static void read_cb(Stream *stream, RBuffer *buf, size_t c, void *data,
bool at_eof)
static void input_read_cb(Stream *stream, RBuffer *buf, size_t c, void *data,
bool at_eof)
{
if (at_eof) {
input_eof = true;
input_done();
}
assert(rbuffer_space(input_buffer) >= rbuffer_size(buf));

View File

@@ -109,13 +109,14 @@ describe('startup', function()
]])
end)
it('input from pipe (implicit) + file args #7679', function()
if helpers.pending_win32(pending) then return end
local screen = Screen.new(25, 3)
screen:attach()
if iswin() then
command([[set shellcmdflag=/s\ /c shellxquote=\"]])
end
command([[exe "terminal echo ohyeah | "]] -- Input from a pipe.
..[[.shellescape(v:progpath)." -u NONE -i NONE --cmd \"]]
..[[.shellescape(v:progpath)." -n -u NONE -i NONE --cmd \"]]
..nvim_set..[[\"]]
..[[ --cmd \"set shortmess+=I\"]]
..[[ -c \"echo has('ttyin') has('ttyout') 'bufs='.bufnr('$')\"]]
@@ -128,5 +129,33 @@ describe('startup', function()
|
]])
end)
it('stdin with -es, -Es #7679', function()
local input = { 'append', 'line1', 'line2', '.', '%print', '' }
local inputstr = table.concat(input, '\n')
--
-- -Es: read stdin as text
--
if not iswin() then
eq('partylikeits1999\n',
funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '-Es', '+.print', 'test/functional/fixtures/tty-test.c' },
{ 'partylikeits1999' }))
eq(inputstr,
funcs.system({nvim_prog, '-i', 'NONE', '-Es', '+%print', '-' },
input))
end
--
-- -es: read stdin as ex-commands
--
eq(' encoding=utf-8\n',
funcs.system({nvim_prog, '-n', '-u', 'NONE', '-i', 'NONE', '-es', 'test/functional/fixtures/tty-test.c' },
{ 'set encoding', '' }))
eq('line1\nline2\n',
funcs.system({nvim_prog, '-i', 'NONE', '-es', '-' },
input))
end)
end)