From 7a97e186673fb46fe0fb02430996d682b261ad42 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sat, 30 Oct 2004 01:03:15 +0000
Subject: [PATCH] make current_x a size_t instead of an int

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2034 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  7 +++++++
 src/cut.c    |  2 +-
 src/global.c |  7 ++++---
 src/move.c   |  2 +-
 src/nano.c   | 21 +++++++++++++--------
 src/nano.h   |  2 +-
 src/proto.h  |  6 ++++--
 src/search.c |  5 +++--
 src/winio.c  | 18 +++++++++---------
 9 files changed, 43 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 66793163..e3ee9c6b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -89,6 +89,10 @@ CVS code -
 	- When saving or changing file positions, be sure not to ignore
 	  placewewant.  Changes to do_int_spell_fix(), findnextstr(),
 	  do_replace_loop(), and do_replace(). (DLR)
+	- Convert current_x to a size_t, and convert some functions that
+	  use it as a parameter to use size_t as well.  Also change some
+	  current_x-related assertions to handle it. (David Benbennick
+	  and DLR)
 - files.c:
   do_insertfile()
 	- Simplify by reusing variables whereever possible, and add a
@@ -143,6 +147,9 @@ CVS code -
 	- Update the help text to mention replacing and spell checking
 	  only selected text, and also add a few cosmetic fixes to it.
 	  (DLR)
+  do_prev_word()
+	- Tweak to avoid an infinite loop, since current_x is now a
+	  size_t and hence is unsigned. (DLR)
   do_int_spell_fix()
 	- Move the REVERSE_SEARCH flag toggling into the NANO_SMALL
 	  #ifdef, since the tiny version of nano doesn't support reverse
diff --git a/src/cut.c b/src/cut.c
index 8b16dfaf..9fea6d92 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -232,7 +232,7 @@ void do_cut_text(void)
 
 #ifndef NANO_SMALL
     if (ISSET(CUT_TO_END) && !ISSET(MARK_ISSET)) {
-	assert(current_x >= 0 && current_x <= strlen(current->data));
+	assert(current_x <= strlen(current->data));
 
 	if (current->data[current_x] == '\0') {
 	    /* If the line is empty and we didn't just cut a non-blank
diff --git a/src/global.c b/src/global.c
index 1f4d42ab..e0ae6208 100644
--- a/src/global.c
+++ b/src/global.c
@@ -52,9 +52,10 @@ struct stat originalfilestat;	/* Stat for the file as we loaded it */
 int editwinrows = 0;		/* How many rows long is the edit
 				   window? */
 filestruct *current;		/* Current buffer pointer */
-int current_x = 0, current_y = 0;	/* Current position of X and Y in
-					   the editor - relative to edit
-					   window (0,0) */
+size_t current_x = 0;		/* Current x-coordinate in the edit
+				   window */
+int current_y = 0;		/* Current y-coordinate in the edit
+				   window */
 filestruct *fileage = NULL;	/* Our file buffer */
 filestruct *edittop = NULL;	/* Pointer to the top of the edit
 				   buffer with respect to the
diff --git a/src/move.c b/src/move.c
index bf524785..c3130984 100644
--- a/src/move.c
+++ b/src/move.c
@@ -54,7 +54,7 @@ void do_home(void)
     size_t old_pww = placewewant;
 #ifndef NANO_SMALL
     if (ISSET(SMART_HOME)) {
-	int old_current_x = current_x;
+	size_t old_current_x = current_x;
 
 	for (current_x = 0; isblank(current->data[current_x]) &&
 		current->data[current_x] != '\0'; current_x++)
diff --git a/src/nano.c b/src/nano.c
index 80b28ebd..d6ee2865 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1162,21 +1162,25 @@ void do_prev_word(void)
     const filestruct *old_current = current;
     assert(current != NULL && current->data != NULL);
 
+    current_x++;
+
     /* Skip letters in this word first. */
-    while (current_x >= 0 && isalnum(current->data[current_x]))
+    while (current_x > 0 && isalnum(current->data[current_x - 1]))
 	current_x--;
 
     for (; current != NULL; current = current->prev) {
-	while (current_x >= 0 && !isalnum(current->data[current_x]))
+	while (current_x > 0 && !isalnum(current->data[current_x - 1]))
 	    current_x--;
 
-	if (current_x >= 0)
+	if (current_x > 0)
 	    break;
 
 	if (current->prev != NULL)
-	    current_x = strlen(current->prev->data);
+	    current_x = strlen(current->prev->data) + 1;
     }
 
+    current_x--;
+
     if (current == NULL) {
 	current = fileage;
 	current_x = 0;
@@ -1463,7 +1467,7 @@ bool do_int_spell_fix(const char *word)
     /* Start from the top of the file. */
     edittop = fileage;
     current = fileage;
-    current_x = -1;
+    current_x = (size_t)-1;
     placewewant = 0;
 
     /* Find the first whole-word occurrence of word. */
@@ -1718,8 +1722,8 @@ const char *do_int_speller(const char *tempfile_name)
 const char *do_alt_speller(char *tempfile_name)
 {
     int alt_spell_status, lineno_cur = current->lineno;
-    int x_cur = current_x, y_cur = current_y;
-    size_t pww_cur = placewewant;
+    size_t x_cur = current_x, pww_cur = placewewant;
+    int y_cur = current_y;
     pid_t pid_spell;
     char *ptr;
     static int arglen = 3;
@@ -2355,7 +2359,8 @@ void do_justify(bool full_justify)
 
     /* We save these global variables to be restored if the user
      * unjustifies.  Note that we don't need to save totlines. */
-    int current_x_save = current_x, current_y_save = current_y;
+    size_t current_x_save = current_x;
+    int current_y_save = current_y;
     long flags_save = flags, totsize_save = totsize;
     filestruct *edittop_save = edittop, *current_save = current;
 #ifndef NANO_SMALL
diff --git a/src/nano.h b/src/nano.h
index eddd043d..f50e8ced 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -168,7 +168,7 @@ typedef struct openfilestruct {
     int file_mark_beginx;	/* Current file's beginning marked
 				 * line's x-coordinate position. */
 #endif
-    int file_current_x;		/* Current file's x-coordinate
+    size_t file_current_x;	/* Current file's x-coordinate
 				 * position. */
     int file_current_y;		/* Current file's y-coordinate
 				 * position. */
diff --git a/src/proto.h b/src/proto.h
index 62b628e2..cf0719e6 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -32,7 +32,9 @@
 extern ssize_t wrap_at;
 #endif
 extern int editwinrows;
-extern int current_x, current_y, totlines;
+extern size_t current_x;
+extern int current_y;
+extern int totlines;
 extern size_t placewewant;
 #ifndef NANO_SMALL
 extern int mark_beginx;
@@ -421,7 +423,7 @@ void do_replace(void);
 void do_gotoline(int line, bool save_pos);
 void do_gotoline_void(void);
 #if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
-void do_gotopos(int line, int pos_x, int pos_y, size_t pos_pww);
+void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww);
 #endif
 void do_find_bracket(void);
 #ifndef NANO_SMALL
diff --git a/src/search.c b/src/search.c
index ddd79937..0c9b6c0b 100644
--- a/src/search.c
+++ b/src/search.c
@@ -772,7 +772,8 @@ ssize_t do_replace_loop(const char *needle, filestruct *real_current,
 
 	    /* Set the cursor at the last character of the replacement
 	     * text, so searching will resume after the replacement
-	     * text.  Note that current_x might be set to -1 here. */
+	     * text.  Note that current_x might be set to (size_t)-1
+	     * here. */
 #ifndef NANO_SMALL
 	    if (!ISSET(REVERSE_SEARCH))
 #endif
@@ -965,7 +966,7 @@ void do_gotoline_void(void)
 }
 
 #if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
-void do_gotopos(int line, int pos_x, int pos_y, size_t pos_pww)
+void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww)
 {
     /* Since do_gotoline() resets the x-coordinate but not the
      * y-coordinate, set the coordinates up this way. */
diff --git a/src/winio.c b/src/winio.c
index c6f5bc4f..3c4221f7 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -1819,9 +1819,9 @@ int nanogetstr(bool allow_tabs, const char *buf, const char *def,
 {
     int kbinput;
     bool meta_key, func_key;
-    static int x = -1;
+    static size_t x = (size_t)-1;
 	/* the cursor position in 'answer' */
-    int xend;
+    size_t xend;
 	/* length of 'answer', the status bar text */
     bool tabbed = FALSE;
 	/* used by input_tab() */
@@ -1849,7 +1849,7 @@ int nanogetstr(bool allow_tabs, const char *buf, const char *def,
        resetstatuspos is TRUE.  Otherwise, leave it alone.  This is so
        the cursor position stays at the same place if a prompt-changing
        toggle is pressed. */
-    if (x == -1 || x > xend || resetstatuspos)
+    if (x == (size_t)-1 || x > xend || resetstatuspos)
 	x = xend;
 
     answer = charealloc(answer, xend + 1);
@@ -1906,7 +1906,7 @@ int nanogetstr(bool allow_tabs, const char *buf, const char *def,
 		return t->ctrlval;
 	    }
 	}
-	assert(0 <= x && x <= xend && xend == strlen(answer));
+	assert(x <= xend && xend == strlen(answer));
 
 	if (kbinput != '\t')
 	    tabbed = FALSE;
@@ -1926,7 +1926,7 @@ int nanogetstr(bool allow_tabs, const char *buf, const char *def,
 	case NANO_HOME_KEY:
 #ifndef NANO_SMALL
 	    if (ISSET(SMART_HOME)) {
-		int old_x = x;
+		size_t old_x = x;
 
 		for (x = 0; isblank(answer[x]) && x < xend; x++)
 		    ;
@@ -2137,7 +2137,7 @@ int nanogetstr(bool allow_tabs, const char *buf, const char *def,
     } /* while (kbinput ...) */
 
     /* We finished putting in an answer; reset x */
-    x = -1;
+    x = (size_t)-1;
 
     return kbinput;
 }
@@ -3265,8 +3265,8 @@ void display_main_list(void)
 void do_cursorpos(bool constant)
 {
     const filestruct *fileptr;
-    unsigned long i = 0;
-    static unsigned long old_i = 0;
+    size_t i = 0;
+    static size_t old_i = 0;
     static long old_totsize = -1;
 
     assert(current != NULL && fileage != NULL && totlines != 0);
@@ -3305,7 +3305,7 @@ void do_cursorpos(bool constant)
 	    _("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%ld (%d%%)"),
 		    current->lineno, totlines, linepct,
 		    (unsigned long)xpt, (unsigned long)cur_len, colpct,
-		    i, totsize, bytepct);
+		    (unsigned long)i, totsize, bytepct);
 	UNSET(DISABLE_CURPOS);
     }
 
-- 
GitLab