From 64fc78c0f1836ee666a2511fd9c817900e8b5320 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Sun, 26 Jan 2003 19:57:44 +0000
Subject: [PATCH] - nano.c:do_int_spell_fix(), do_int_speller() - Fix crashes
 with mark position, current_x position and edit_update args (David
 Benbennick)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1397 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog |  3 +++
 nano.c    | 80 +++++++++++++++++++++++--------------------------------
 2 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e60754c2..79a6917c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -20,6 +20,9 @@ CVS Code -
 - nano.c:
   do_preserve_msg():
 	- Unsplit error message into a single fprintf call (Jordi).
+  do_int_spell_fix(), do_int_speller()
+	- Fix crashes with mark position, current_x position, 
+	  and edit_update args (David Benbennick).
   main()
 	- Call load_file with arg 0 for insert, as we aren't really
 	  doing an insert, allows new_file() to run if we open a 
diff --git a/nano.c b/nano.c
index 5281e925..35fb80cc 100644
--- a/nano.c
+++ b/nano.c
@@ -1605,32 +1605,29 @@ int do_wrap(filestruct *inptr)
 #endif /* !DISABLE_WRAPPING */
 
 #ifndef DISABLE_SPELLER
+/* word is misspelled in the file.  Let the user replace it.  We return
+   False if the user cancels. */
 int do_int_spell_fix(const char *word)
 {
     char *save_search;
     char *save_replace;
-    filestruct *begin;
-    int i = 0, j = 0, beginx, beginx_top, reverse_search_set, case_sens_set;
+    filestruct *current_save = current;
+    int current_x_save = current_x;
+    filestruct *edittop_save = edittop;
+	/* Save where we are. */
+    int i = 0;
+	/* The return value. */
+    int reverse_search_set = ISSET(REVERSE_SEARCH);
 #ifndef NANO_SMALL
-    int mark_set;
-#endif
-
-    /* save where we are */
-    begin = current;
-    beginx = current_x + 1;
-
-    /* Make sure Spell Check goes forward only */
-    reverse_search_set = ISSET(REVERSE_SEARCH);
-    UNSET(REVERSE_SEARCH);
+    int case_sens_set = ISSET(CASE_SENSITIVE);
+    int mark_set = ISSET(MARK_ISSET);
 
-    case_sens_set = ISSET(CASE_SENSITIVE);
     SET(CASE_SENSITIVE);
-
-#ifndef NANO_SMALL
     /* Make sure the marking highlight is off during Spell Check */
-    mark_set = ISSET(MARK_ISSET);
     UNSET(MARK_ISSET);
 #endif
+    /* Make sure Spell Check goes forward only */
+    UNSET(REVERSE_SEARCH);
 
     /* save the current search/replace strings */
     search_init_globals();
@@ -1643,69 +1640,60 @@ int do_int_spell_fix(const char *word)
 
     /* start from the top of file */
     current = fileage;
-    current_x = beginx_top = -1;
+    current_x = -1;
 
     search_last_line = FALSE;
 
-    while (1) {
-	/* make sure word is still mis-spelt (i.e. when multi-errors) */
-	if (findnextstr(TRUE, FALSE, fileage, beginx_top, word)) {
-
-	    /* find whole words only */
-	    if (!is_whole_word(current_x, current->data, word))
-		continue;
+    /* We find the first whole-word occurrence of word. */
+    while (findnextstr(TRUE, TRUE, fileage, -1, word))
+	if (is_whole_word(current_x, current->data, word)) {
+	    edit_refresh();
 
-	    edit_update(current, current_x);
 	    do_replace_highlight(TRUE, word);
 
 	    /* allow replace word to be corrected */
-	    i = statusq(0, spell_list, last_replace,
+	    i = statusq(0, spell_list, word,
 #ifndef NANO_SMALL
-		0,
+			NULL,
 #endif
-		_("Edit a replacement"));
+			 _("Edit a replacement"));
 
 	    do_replace_highlight(FALSE, word);
 
-	    /* start from the start of this line again */
-	    current = fileage;
-	    current_x = beginx_top;
-
-	    search_last_line = FALSE;
+	    if (i != -1 && strcmp(word, answer)) {
+		int j = 0;
 
-	    if (strcmp(word, answer)) {
-		j = i;
-		do_replace_loop(word, fileage, &beginx_top, TRUE, &j);
+		search_last_line = FALSE;
+		current_x--;
+		do_replace_loop(word, current_save, &current_x_save, TRUE, &j);
 	    }
+
+	    break;
 	}
-	break;
-    }
 
     /* restore the search/replace strings */
     free(last_search);    last_search=save_search;
     free(last_replace);   last_replace=save_replace;
 
     /* restore where we were */
-    current = begin;
-    current_x = beginx - 1;
+    current = current_save;
+    current_x = current_x_save;
+    edittop = edittop_save;
 
     /* restore Search/Replace direction */
     if (reverse_search_set)
 	SET(REVERSE_SEARCH);
 
+#ifndef NANO_SMALL
     if (!case_sens_set)
 	UNSET(CASE_SENSITIVE);
 
-#ifndef NANO_SMALL
     /* restore marking highlight */
     if (mark_set)
 	SET(MARK_ISSET);
 #endif
 
-    if (i == -1)
-	return FALSE;
-
-    return TRUE;
+    return i != -1;
 }
 
 /* Integrated spell checking using 'spell' program.  Return value: NULL
@@ -1862,7 +1850,7 @@ char *do_int_speller(char *tempfile_name)
 
     free(read_buff);
     replace_abort();
-    edit_update(current, current_x);
+    edit_refresh();
 
     /* Process end of spell process */
 
-- 
GitLab