Enable multi-line edit when the line ends with a pipe (#1285)

This commit is contained in:
slama
2018-02-07 23:36:15 +09:00
committed by ridiculousfish
parent cefb9e6d03
commit 38418d6356
3 changed files with 54 additions and 1 deletions

View File

@@ -302,6 +302,7 @@ static volatile sig_atomic_t interrupted = 0;
// Prototypes for a bunch of functions defined later on.
static bool is_backslashed(const wcstring &str, size_t pos);
static wchar_t unescaped_quote(const wcstring &str, size_t pos);
static bool ends_with_pipe(const wcstring &str, size_t pos);
/// Mode on startup, which we restore on exit.
static struct termios terminal_mode_on_startup;
@@ -2302,6 +2303,19 @@ static bool is_backslashed(const wcstring &str, size_t pos) {
return (count % 2) == 1;
}
/// Test if the specified string ends with pipe. Whitespaces at the end are ignored. It returns
/// false if backslashed before the pipe because it should be treated as an escaped character.
static bool ends_with_pipe(const wcstring &str, size_t pos) {
if (pos > str.size()) return false;
while (pos--) {
wchar_t c = str.at(pos);
if (c == L'|') return !is_backslashed(str, pos);
if (!iswspace(c)) break;
}
return false;
}
static wchar_t unescaped_quote(const wcstring &str, size_t pos) {
wchar_t result = L'\0';
if (pos < str.size()) {
@@ -2751,6 +2765,12 @@ const wchar_t *reader_readline(int nchars) {
insert_char(el, '\n');
break;
}
// A newline is inserted if the line ends with a pipe (issue #1285).
if (ends_with_pipe(el->text, el->size())) {
el->position = el->size();
insert_char(el, '\n');
break;
}
// See if this command is valid.
int command_test_result = data->test_func(el->text.c_str());