From ad7833134883b6cd2c155ee8be3ff8d324c72ef3 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Fri, 7 Feb 2020 16:27:02 -0800 Subject: [PATCH] Adds optional paren matching to keyboard input --- config.h | 3 +++ hermes.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/config.h b/config.h index d00e659..2bbe5b6 100644 --- a/config.h +++ b/config.h @@ -9,6 +9,9 @@ // Set global tab width for the editor here static const int tabwidth = 4; +// Turn on or off paren matching for: (, {, [ +static const int MATCH_PARENS = 1; + /* * Values from 1 to 256 representing 256 color/8-bit pallate * see: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit diff --git a/hermes.c b/hermes.c index 1d527a6..a436aa5 100644 --- a/hermes.c +++ b/hermes.c @@ -353,6 +353,19 @@ void editorUpdateSyntax(erow *row) { editorUpdateSyntax(&E.row[row->idx + 1]); } +char editorGetMatchingParen(char c) { + switch (c) { + case '{': + return '}'; + case '(': + return ')'; + case '[': + return ']'; + default: + return ' '; + } +} + int editorSyntaxToColor(int hl) { switch (hl) { case HL_NUMBER: return HL_NUMBER_COLOR; @@ -1152,6 +1165,16 @@ void editorInputKp(int c) { if (c == DEL_KEY) editorMoveCursor(ARROW_RIGHT); editorDeleteChar(); break; + case '(': + case '{': + case '[': + editorInsertChar(c); + if (MATCH_PARENS) { + char match = editorGetMatchingParen(c); + editorInsertChar(match); + editorMoveCursor(ARROW_LEFT); + } + break; default: editorInsertChar(c); break;