From b7ca9458773ee18c0b0bb4de55b670e06b70ecc5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 4 Nov 2015 12:29:33 +0000 Subject: [PATCH] lib/mp-readline: Make it easy to exit auto-indent mode by pressing enter. This patch allows you to stop auto-indent by pressing enter on a second blank line. Easier than having to use backspace, and prevents new users from getting stuck in auto-indent mode. --- docs/reference/repl.rst | 4 ++++ lib/mp-readline/readline.c | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/docs/reference/repl.rst b/docs/reference/repl.rst index c03cbfd38..0e481996f 100644 --- a/docs/reference/repl.rst +++ b/docs/reference/repl.rst @@ -49,6 +49,10 @@ Finally type ``print(i)``, press RETURN, press BACKSPACE and press RETURN again: 3 >>> +Auto-indent won't be applied if the previous two lines were all spaces. This +means that you can finish entering a compound statment by pressing RETURN +twice, and then a third press will finish and execute. + Auto-completion --------------- diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index b7c64aec3..cbb99cc94 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -370,6 +370,18 @@ STATIC void readline_auto_indent(void) { } } // i=start of line; j=first non-space + if (i > 0 && j + 1 == line->len) { + // previous line is not first line and is all spaces + for (size_t k = i - 1; k > 0; --k) { + if (line->buf[k - 1] == '\n') { + // don't auto-indent if last 2 lines are all spaces + return; + } else if (line->buf[k - 1] != ' ') { + // 2nd previous line is not all spaces + break; + } + } + } int n = (j - i) / 4; if (line->buf[line->len - 2] == ':') { n += 1;