Adds optional paren matching to keyboard input

This commit is contained in:
Brian Evans 2020-02-07 16:27:02 -08:00
parent 7893ea5cd4
commit ad78331348
2 changed files with 26 additions and 0 deletions

View File

@ -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

View File

@ -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;