From 7f2d295b6e85bec215d355c2dd347fda019b7e22 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Mon, 17 Dec 2018 19:57:30 +0100
Subject: [PATCH] possible new feature: add option --edgecolumn that shows a
 vertical guiding bar
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Option -e (--edgecolumn) takes a column number as argument and then
shows a vertical, colored bar over the entire height of the buffer,
to help the user control the width of the text when the terminal is
wider than this desired width.

[The color needs to become configurable, and would by default just
change the background color, not the foreground color.  But that is
something for a later patch.  It also needs to add documentation, of
course.  Suggestions for a better name for the option are welcome.]

This fulfills https://bugs.debian.org/916392.
Requested-by: Arturo Borrero González <arturo@debian.org>
And fulfills https://savannah.gnu.org/bugs/?55315.
Requested-by: Bryan Christ <bryan.christ@gmail.com>
---
 src/global.c |  2 ++
 src/nano.c   | 18 ++++++++++++++++--
 src/proto.h  |  1 +
 src/rcfile.c |  8 ++++++++
 src/winio.c  |  9 +++++++++
 5 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/src/global.c b/src/global.c
index 0368132f..baef8fa9 100644
--- a/src/global.c
+++ b/src/global.c
@@ -111,6 +111,8 @@ int editwincols = -1;
 		/* The number of usable columns in the edit window: COLS - margin. */
 int margin = 0;
 		/* The amount of space reserved at the left for line numbers. */
+ssize_t edge_col = 0;
+		/* The column at which a vertical bar of color will be drawn. */
 
 filestruct *cutbuffer = NULL;
 		/* The buffer where we store cut text. */
diff --git a/src/nano.c b/src/nano.c
index e4f91896..58f140c2 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -851,6 +851,10 @@ void usage(void)
 	print_opt("-c", "--constantshow", N_("Constantly show cursor position"));
 	print_opt("-d", "--rebinddelete",
 					N_("Fix Backspace/Delete confusion problem"));
+#ifndef NANO_TINY
+	print_opt("-e <number>", "--edgecolumn=<number>",
+					N_("Show a guiding bar at this column"));
+#endif
 #ifdef ENABLE_BROWSER
 	if (!ISSET(RESTRICTED))
 		print_opt("-g", "--showcursor", N_("Show cursor in file browser & help text"));
@@ -2022,6 +2026,7 @@ int main(int argc, char **argv)
 		{"wordchars", 1, NULL, 'X'},
 		{"zap", 0, NULL, 'Z'},
 		{"atblanks", 0, NULL, 'a'},
+		{"edgecolumn", 1, NULL, 'e'},
 		{"autoindent", 0, NULL, 'i'},
 		{"cutfromcursor", 0, NULL, 'k'},
 		{"unix", 0, NULL, 'u'},
@@ -2084,7 +2089,7 @@ int main(int argc, char **argv)
 
 	while ((optchr =
 		getopt_long(argc, argv,
-				"ABC:DEFGHIKLMNOPQ:RST:UVWX:Y:Zabcdefghijklmno:pqr:s:tuvwxyz$",
+				"ABC:DEFGHIKLMNOPQ:RST:UVWX:Y:Zabcde:fghijklmno:pqr:s:tuvwxyz$",
 				long_options, NULL)) != -1) {
 		switch (optchr) {
 #ifndef NANO_TINY
@@ -2203,6 +2208,13 @@ int main(int argc, char **argv)
 			case 'd':
 				SET(REBIND_DELETE);
 				break;
+			case 'e':
+				if (!parse_num(optarg, &edge_col) || edge_col <= 0) {
+					fprintf(stderr, _("Edge column \"%s\" is invalid"), optarg);
+					fprintf(stderr, "\n");
+					exit(1);
+				}
+				break;
 			case 'g':
 				SET(SHOW_CURSOR);
 				break;
@@ -2294,7 +2306,6 @@ int main(int argc, char **argv)
 				break;
 #endif
 			case 'b':  /* Pico compatibility flags. */
-			case 'e':
 			case 'f':
 			case 'j':
 			case 'q':
@@ -2316,6 +2327,7 @@ int main(int argc, char **argv)
 		ssize_t fill_cmdline = fill;
 #endif
 #ifndef NANO_TINY
+		size_t edgecol_cmdline = edge_col;
 		char *backup_dir_cmdline = backup_dir;
 		char *word_chars_cmdline = word_chars;
 #endif
@@ -2362,6 +2374,8 @@ int main(int argc, char **argv)
 			fill = fill_cmdline;
 #endif
 #ifndef NANO_TINY
+		if (edgecol_cmdline > 0)
+			edge_col = edgecol_cmdline;
 		if (backup_dir_cmdline != NULL) {
 			free(backup_dir);
 			backup_dir = backup_dir_cmdline;
diff --git a/src/proto.h b/src/proto.h
index 65890134..09e7464a 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -88,6 +88,7 @@ extern WINDOW *bottomwin;
 extern int editwinrows;
 extern int editwincols;
 extern int margin;
+extern ssize_t edge_col;
 
 extern filestruct *cutbuffer;
 extern filestruct *cutbottom;
diff --git a/src/rcfile.c b/src/rcfile.c
index 8718a30e..9b3ba77a 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -96,6 +96,7 @@ static const rcoption rcopts[] = {
 	{"casesensitive", CASE_SENSITIVE},
 	{"cut", CUT_FROM_CURSOR},  /* deprecated form, remove end of 2018 */
 	{"cutfromcursor", CUT_FROM_CURSOR},
+	{"edgecolumn", 0},
 	{"justifytrim", TRIM_BLANKS},  /* deprecated form, remove end of 2018 */
 	{"locking", LOCKING},
 	{"matchbrackets", 0},
@@ -1111,6 +1112,13 @@ void parse_rcfile(FILE *rcstream, bool syntax_only)
 		} else
 #endif
 #ifndef NANO_TINY
+		if (strcasecmp(rcopts[i].name, "edgecolumn") == 0) {
+			if (!parse_num(option, &edge_col) || edge_col <= 0) {
+				rcfile_error(N_("Edge column \"%s\" is invalid"), option);
+				edge_col = 0;
+			}
+			free(option);
+		}
 		if (strcasecmp(rcopts[i].name, "matchbrackets") == 0) {
 			matchbrackets = option;
 			if (has_blank_mbchars(matchbrackets)) {
diff --git a/src/winio.c b/src/winio.c
index e2042d9e..048d1cee 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2677,6 +2677,15 @@ void edit_draw(filestruct *fileptr, const char *converted,
 	}
 #endif /* ENABLE_COLOR */
 
+	if (edge_col > 0) {
+		const char *text = converted +  actual_x(converted, edge_col - 1);;
+		const char *edge_char = (*text == '\0') ? " " : text;
+
+		wattron(edit, interface_color_pair[ERROR_MESSAGE]);
+		mvwaddnstr(edit, row, margin + edge_col - 1, edge_char, 1);
+		wattroff(edit, interface_color_pair[ERROR_MESSAGE]);
+	}
+
 #ifndef NANO_TINY
 	/* If the mark is on, and fileptr is at least partially selected, we
 	 * need to paint it. */
-- 
2.19.2

