From 3d12f0f53d10c8d819d39ca6c39d7bfc9ef3f6e1 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Wed, 26 Oct 2005 23:14:59 +0000
Subject: [PATCH] refactor nanoget_repaint() to split out the new function
 get_statusbar_page_start(), the statusbar prompt's equivalent of
 get_page_start(); also make sure that the minimum allowed terminal size in
 columns is 4, as the statusbar prompt code relies on this assumption and will
 crash otherwise

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3051 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    | 13 +++++++++++
 src/nano.c   |  2 +-
 src/nano.h   |  4 +++-
 src/proto.h  |  1 +
 src/search.c |  2 +-
 src/winio.c  | 65 +++++++++++++++++++++++++++-------------------------
 6 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c6e1f4d9..a05ff095 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,19 @@ CVS code -
 	  is disabled when NANO_SMALL is defined.  New functions
 	  do_scroll_up() and do_scroll_down(); changes to
 	  shortcut_init(). (DLR, suggested by Mike Frysinger)
+	- Since the statusbar prompt code needs at least 4 columns in
+	  order to work properly, make that the minimum number of
+	  columns nano requires to run, and remove assertions and code
+	  that make use of a smaller number.  Changes to window_init(),
+	  nanoget_repaint(), titlebar(), statusbar(), and
+	  get_page_start(). (DLR)
+- nano.h:
+	- Readd MIN_EDITOR_COLS #define. (DLR)
+- winio.c:
+  nanoget_repaint()
+	- Move the code to determine the statusbar equivalent of
+	  get_page_start() into the new function
+	  get_statusbar_page_start(). (DLR)
 
 GNU nano 1.3.9 - 2005.10.23
 - General:
diff --git a/src/nano.c b/src/nano.c
index e5a6f2ec..803d3ed3 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -634,7 +634,7 @@ void window_init(void)
 {
     /* If the screen height is too small, get out. */
     editwinrows = LINES - 5 + no_more_space() + no_help();
-    if (editwinrows < MIN_EDITOR_ROWS)
+    if (COLS < MIN_EDITOR_COLS || editwinrows < MIN_EDITOR_ROWS)
 	die(_("Window size is too small for nano...\n"));
 
 #ifndef DISABLE_WRAPJUSTIFY
diff --git a/src/nano.h b/src/nano.h
index e2c35f20..a19cb602 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -522,7 +522,9 @@ typedef struct rcoption {
 #define VIEW TRUE
 #define NOVIEW FALSE
 
-/* Minimum editor window rows required for nano to work correctly. */
+/* Minimum editor window columns and rows required for nano to work
+ * correctly. */
+#define MIN_EDITOR_COLS 4
 #define MIN_EDITOR_ROWS 1
 
 /* Default number of characters from end-of-line where text wrapping
diff --git a/src/proto.h b/src/proto.h
index 871a684c..01cb4ffd 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -632,6 +632,7 @@ bool do_statusbar_next_word(bool allow_punct);
 bool do_statusbar_prev_word(bool allow_punct);
 #endif
 void do_statusbar_verbatim_input(bool *got_enter);
+size_t get_statusbar_page_start(size_t start_col, size_t column);
 size_t xplustabs(void);
 size_t actual_x(const char *str, size_t xplus);
 size_t strnlenpt(const char *buf, size_t size);
diff --git a/src/search.c b/src/search.c
index 926352c9..47070afc 100644
--- a/src/search.c
+++ b/src/search.c
@@ -155,7 +155,7 @@ int search_init(bool replacing, bool use_answer)
 	/* We use (COLS / 3) here because we need to see more on the
 	 * line. */
 	sprintf(buf, " [%s%s]", disp,
-		strlenpt(last_search) > COLS / 3 ? "..." : "");
+		(strlenpt(last_search) > COLS / 3) ? "..." : "");
 	free(disp);
     } else
 	buf = mallocstrcpy(NULL, "");
diff --git a/src/winio.c b/src/winio.c
index be0b832a..f70b743e 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2174,6 +2174,20 @@ void do_statusbar_verbatim_input(bool *got_enter)
     free(output);
 }
 
+/* nano scrolls horizontally within a line in chunks.  This function
+ * returns the column number of the first character displayed in the
+ * statusbar prompt when the cursor is at the given column with the
+ * prompt ending at start_col.  Note that (0 <= column -
+ * get_statusbar_page_start(column) < COLS). */
+size_t get_statusbar_page_start(size_t start_col, size_t column)
+{
+    if (column == start_col || column < COLS - 1)
+	return 0;
+    else
+	return column - start_col - (column - start_col) % (COLS -
+		start_col - 1);
+}
+
 /* Return the placewewant associated with current_x, i.e, the zero-based
  * column position of the cursor.  The value will be no smaller than
  * current_x. */
@@ -2453,39 +2467,33 @@ char *display_string(const char *buf, size_t start_col, size_t len, bool
     return converted;
 }
 
-/* Repaint the statusbar when getting a character in nanogetstr().  buf
- * should be no longer than max(0, COLS - 4).
- *
- * Note that we must turn on A_REVERSE here, since do_help() turns it
- * off! */
+/* Repaint the statusbar when getting a character in nanogetstr().  Note
+ * that we must turn on A_REVERSE here, since do_help() turns it off! */
 void nanoget_repaint(const char *buf, const char *inputbuf, size_t x)
 {
-    size_t x_real = strnlenpt(inputbuf, x);
-    int wid = COLS - strlenpt(buf) - 2;
+    size_t start_col, xpt, page_start;
+    char *expanded;
 
     assert(x <= strlen(inputbuf));
 
+    start_col = strlenpt(buf) + 1;
+    xpt = strnlenpt(inputbuf, x);
+    page_start = get_statusbar_page_start(start_col, start_col + xpt);
+
     wattron(bottomwin, A_REVERSE);
+
     blank_statusbar();
 
     mvwaddnstr(bottomwin, 0, 0, buf, actual_x(buf, COLS - 2));
     waddch(bottomwin, ':');
+    waddch(bottomwin, (page_start == 0) ? ' ' : '$');
 
-    if (COLS > 1)
-	waddch(bottomwin, x_real < wid ? ' ' : '$');
-    if (COLS > 2) {
-	size_t page_start = x_real - x_real % wid;
-	char *expanded = display_string(inputbuf, page_start, wid,
-		FALSE);
+    expanded = display_string(inputbuf, page_start, COLS - start_col -
+	1, FALSE);
+    waddstr(bottomwin, expanded);
+    free(expanded);
 
-	assert(wid > 0);
-	assert(strlenpt(expanded) <= wid);
-
-	waddstr(bottomwin, expanded);
-	free(expanded);
-	wmove(bottomwin, 0, COLS - wid + x_real - page_start);
-    } else
-	wmove(bottomwin, 0, COLS - 1);
+    wmove(bottomwin, 0, start_col + xpt + 1 - page_start);
 
     wattroff(bottomwin, A_REVERSE);
 }
@@ -2769,7 +2777,6 @@ void titlebar(const char *path)
 	/* Do we put an ellipsis before the path? */
 
     assert(path != NULL || openfile->filename != NULL);
-    assert(COLS >= 0);
 
     wattron(topwin, A_REVERSE);
     blank_titlebar();
@@ -2885,7 +2892,7 @@ void titlebar(const char *path)
     free(exppath);
 
     if (state[0] != '\0') {
-	if (COLS <= 1 || statelen >= COLS - 1)
+	if (statelen >= COLS - 1)
 	    mvwaddnstr(topwin, 0, 0, state, actual_x(state, COLS));
 	else {
 	    assert(COLS - statelen - 1 >= 0);
@@ -2932,7 +2939,7 @@ void statusbar(const char *msg, ...)
     /* Blank out the line. */
     blank_statusbar();
 
-    if (COLS >= 4) {
+    {
 	char *bar, *foo;
 	size_t start_x = 0, foo_len;
 #if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
@@ -3060,20 +3067,16 @@ void onekey(const char *keystroke, const char *desc, size_t len)
 
 /* nano scrolls horizontally within a line in chunks.  This function
  * returns the column number of the first character displayed in the
- * window when the cursor is at the given column.  Note that
- * 0 <= column - get_page_start(column) < COLS. */
+ * edit window when the cursor is at the given column.  Note that (0 <=
+ * column - get_page_start(column) < COLS). */
 size_t get_page_start(size_t column)
 {
-    assert(COLS > 0);
-
     if (column == 0 || column < COLS - 1)
 	return 0;
     else if (COLS > 9)
 	return column - 7 - (column - 7) % (COLS - 8);
-    else if (COLS > 2)
-	return column - (COLS - 2);
     else
-	return column - (COLS - 1);
+	return column - (COLS - 2);
 }
 
 /* Resets current_y, based on the position of current, and puts the
-- 
GitLab