From f0630c9c828058d8792445f7c1f9760d8f367e8f Mon Sep 17 00:00:00 2001
From: Consolatis <35009135+Consolatis@users.noreply.github.com>
Date: Tue, 19 Oct 2021 05:56:15 +0200
Subject: [PATCH] [RFC] use numbercolorcurrent for linenumber of currently
 active line

---
 src/color.c       |  4 +++-
 src/definitions.h |  3 +++
 src/files.c       |  1 +
 src/nano.c        |  4 ++++
 src/rcfile.c      |  3 +++
 src/winio.c       | 43 +++++++++++++++++++++++++++++++++++--------
 6 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/src/color.c b/src/color.c
index ad46d67f..a9dc51a5 100644
--- a/src/color.c
+++ b/src/color.c
@@ -66,7 +66,9 @@ void set_interface_colorpairs(void)
 			else if (index == ERROR_MESSAGE) {
 				init_pair(index + 1, COLOR_WHITE, COLOR_RED);
 				interface_color_pair[index] = COLOR_PAIR(index + 1) | A_BOLD;
-			} else
+			} else if (index == LINE_NUMBER_CURRENT)
+				interface_color_pair[index] = interface_color_pair[LINE_NUMBER] | A_BOLD;
+			else
 				interface_color_pair[index] = hilite_attribute;
 		}
 
diff --git a/src/definitions.h b/src/definitions.h
index eed5f105..4b91526e 100644
--- a/src/definitions.h
+++ b/src/definitions.h
@@ -294,6 +294,7 @@ typedef enum {
 enum {
 	TITLE_BAR = 0,
 	LINE_NUMBER,
+	LINE_NUMBER_CURRENT,
 	GUIDE_STRIPE,
 	SCROLL_BAR,
 	SELECTED_TEXT,
@@ -533,6 +534,8 @@ typedef struct openfilestruct {
 		/* The current top of the edit window for this file. */
 	linestruct *current;
 		/* The current line for this file. */
+	linestruct *last;
+		/* The last current line for this file. */
 	size_t totsize;
 		/* The file's total number of characters. */
 	size_t firstcolumn;
diff --git a/src/files.c b/src/files.c
index 9c41936f..d9c9fa7b 100644
--- a/src/files.c
+++ b/src/files.c
@@ -70,6 +70,7 @@ void make_new_buffer(void)
 	openfile->current_x = 0;
 	openfile->placewewant = 0;
 	openfile->current_y = 0;
+	openfile->last = NULL;
 
 	openfile->edittop = openfile->filetop;
 	openfile->firstcolumn = 0;
diff --git a/src/nano.c b/src/nano.c
index 9f614c6b..7f009b90 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1673,6 +1673,10 @@ void process_a_keystroke(void)
 	if (!refresh_needed && (shortcut->func == do_delete ||
 							shortcut->func == do_backspace))
 		update_line(openfile->current, openfile->current_x);
+#ifdef ENABLE_LINENUMBERS
+	if (!refresh_needed && openfile->current != was_current && margin > 0)
+		update_line(openfile->current, openfile->current_x);
+#endif
 
 #ifndef NANO_TINY
 	if (bracketed_paste)
diff --git a/src/rcfile.c b/src/rcfile.c
index 52351d09..72b70da0 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -127,6 +127,7 @@ static const rcoption rcopts[] = {
 #ifdef ENABLE_COLOR
 	{"titlecolor", 0},
 	{"numbercolor", 0},
+	{"numbercolorcurrent", 0},
 	{"stripecolor", 0},
 	{"scrollercolor", 0},
 	{"selectedcolor", 0},
@@ -1573,6 +1574,8 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only)
 #ifdef ENABLE_COLOR
 		if (strcmp(option, "titlecolor") == 0)
 			color_combo[TITLE_BAR] = parse_interface_color(argument);
+		else if (strcmp(option, "numbercolorcurrent") == 0)
+			color_combo[LINE_NUMBER_CURRENT] = parse_interface_color(argument);
 		else if (strcmp(option, "numbercolor") == 0)
 			color_combo[LINE_NUMBER] = parse_interface_color(argument);
 		else if (strcmp(option, "stripecolor") == 0)
diff --git a/src/winio.c b/src/winio.c
index 22eb2afd..3cd38743 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2482,6 +2482,25 @@ void place_the_cursor(void)
 	openfile->current_y = row;
 }
 
+#ifdef ENABLE_LINENUMBERS
+void draw_linenumbers(int row, linestruct *line, size_t from_col) {
+	wattron(edit, interface_color_pair[
+			line == openfile->current ? LINE_NUMBER_CURRENT : LINE_NUMBER]);
+#ifndef NANO_TINY
+	if (ISSET(SOFTWRAP) && from_col != 0)
+		mvwprintw(edit, row, 0, "%*s", margin - 1, " ");
+	else
+#endif
+	{
+		mvwprintw(edit, row, 0, "%*zd", margin - 1, line->lineno);
+		if (openfile && openfile->current == line)
+			openfile->last = line;
+	}
+	wattroff(edit, interface_color_pair[
+			line == openfile->current ? LINE_NUMBER_CURRENT : LINE_NUMBER]);
+}
+#endif
+
 /* Draw the given text on the given row of the edit window.  line is the
  * line to be drawn, and converted is the actual string to be written with
  * tabs and control characters replaced by strings of regular characters.
@@ -2492,14 +2511,22 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col)
 	/* If line numbering is switched on, put a line number in front of
 	 * the text -- but only for the parts that are not softwrapped. */
 	if (margin > 0) {
-		wattron(edit, interface_color_pair[LINE_NUMBER]);
-#ifndef NANO_TINY
-		if (ISSET(SOFTWRAP) && from_col != 0)
-			mvwprintw(edit, row, 0, "%*s", margin - 1, " ");
-		else
-#endif
-			mvwprintw(edit, row, 0, "%*zd", margin - 1, line->lineno);
-		wattroff(edit, interface_color_pair[LINE_NUMBER]);
+		if (openfile && openfile->current == line && openfile->last && openfile->last != line) {
+			/* While drawing the current line restore color of the previous current line */
+			int last_row = openfile->last->lineno - openfile->edittop->lineno;
+			if (ISSET(SOFTWRAP) && last_row >= 0 && last_row < editwinrows) {
+				/* Maybe add extra rows caused by softwrapping */
+				last_row = -chunk_for(openfile->firstcolumn, openfile->edittop);
+				linestruct *tmpline = openfile->edittop;
+				while (tmpline && tmpline != openfile->last && last_row < editwinrows) {
+					last_row += 1 + extra_chunks_in(tmpline);
+					tmpline = tmpline->next;
+				}
+			}
+			if (last_row >= 0 && last_row < editwinrows)
+				draw_linenumbers(last_row, openfile->last, 0);
+		}
+		draw_linenumbers(row, line, from_col);
 #ifndef NANO_TINY
 		if (line->has_anchor && (from_col == 0 || !ISSET(SOFTWRAP)))
 #ifdef ENABLE_UTF8
-- 
2.30.2

