kilo

Kilo: minimal text editor written in C
git clone git://git.dimitrijedobrota.com/kilo.git
Log | Files | Refs

commit 8c1bb48b9d7939d8e4552928ac958b3749512b47
parent c5abe8f4ebfc671d324e3e38dcf2f9352fda9ef8
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 11 Apr 2023 00:36:06 +0200

Highlight the number on the current line

Diffstat:
Mkilo.c | 35+++++++++++++++++++++++++----------
1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/kilo.c b/kilo.c @@ -86,6 +86,9 @@ enum editorHighlight { HL_CBRACKETS, HL_SEMICOLON, HL_COMMA, + + HL_LINEN_CRNT, + HL_LINEN, }; #define HL_HIGHLIGHT_NUMBERS (1 << 0) @@ -488,6 +491,10 @@ int editorSyntaxToColor(int hl) { case HL_CBRACKETS: return 93; case HL_SEMICOLON: return 93; case HL_COMMA: return 91; + + case HL_LINEN_CRNT: return 91; + case HL_LINEN: return 37; + default: return 37; } } @@ -1005,6 +1012,9 @@ void editorScroll() { } void editorDrawRows(struct abuf *ab) { + char num_buf[E.row_digits + 1]; + char color_buf[16]; + for (int y = 0; y < E.screenrows; y++) { int filerow = y + E.rowoff; if (filerow >= E.numrows) { @@ -1024,12 +1034,17 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { - char num_buf[E.row_digits + 1]; - int left = sprintf(num_buf, "%*d ", E.row_digits, filerow + 1); - abAppend(ab, num_buf, left); + int padding = sprintf(num_buf, "%*d ", E.row_digits, filerow + 1); + int clen = + snprintf(color_buf, sizeof(color_buf), "\x1b[%dm", + editorSyntaxToColor(E.cy == y ? HL_LINEN_CRNT : HL_LINEN)); + + abAppend(ab, color_buf, clen); + abAppend(ab, num_buf, padding); + abAppend(ab, "\x1b[39m", 5); int len = E.row[filerow].rsize - E.coloff; - CLAMP(len, 0, E.screencols - left); + CLAMP(len, 0, E.screencols - padding); char *c = &E.row[filerow].render[E.coloff]; unsigned char *hl = &E.row[filerow].hl[E.coloff]; int current_color = -1; @@ -1043,9 +1058,9 @@ void editorDrawRows(struct abuf *ab) { // restore current color as <esc>[m turns off all text formatting, // including colors if (current_color != -1) { - char buf[16]; - int clen = snprintf(buf, sizeof(buf), "\x1b[%dm", current_color); - abAppend(ab, buf, clen); + int clen = snprintf(color_buf, sizeof(color_buf), "\x1b[%dm", + current_color); + abAppend(ab, color_buf, clen); } } else if (hl[j] == HL_NORMAL) { if (current_color != -1) { @@ -1057,9 +1072,9 @@ void editorDrawRows(struct abuf *ab) { int color = editorSyntaxToColor(hl[j]); if (color != current_color) { current_color = color; - char buf[16]; - int clen = snprintf(buf, sizeof(buf), "\x1b[%dm", color); - abAppend(ab, buf, clen); + int clen = + snprintf(color_buf, sizeof(color_buf), "\x1b[%dm", color); + abAppend(ab, color_buf, clen); } abAppend(ab, &c[j], 1); }