From a0e957bc0de55b6765cb2d47a7a5ed845202419e Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Tue, 24 Oct 2000 22:25:36 +0000
Subject: [PATCH] Rocco's findnextstr fixes, lost of other tweaks

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@242 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  13 ++++++
 po/nano.pot |  70 +++++++++++++++----------------
 search.c    | 119 +++++++++++++++++++++++++++++++---------------------
 winio.c     |  65 +++++++++-------------------
 4 files changed, 139 insertions(+), 128 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 96d876da..140877ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,10 +3,23 @@ CVS Code -
   toggle_init()
 	- Added #ifdef around toggle_regex_msg to get rid of compiler 
 	  warning.
+- search.c
+  findnexstr()
+	- New arg for begin_x variable, basically a rewrite that
+	  makes a little more sense and isn't quite as messy (Rocco Corsi).
+	- Update the line we're checking if not the whole screen, because
+	  it's quite possible the search team could exist somewhere way
+	  to the right on the same line, for example.
+  replace_abort()
+	- Add reset of placewewant, stops cursor from jumping when moving
+	  cursor after a replace.
 - winio.c
   nanogetstr()
 	- Added check for 343 in while loop to get rid of getting "locked"
 	  into statusbar" bug in odd $TERMs like iris-ansi.
+  nanoget_repaint()
+	- New function, removes about 30 lines of duplicate code in 
+	  nanogetstr().
   
 nano 0.9.19 - 10/02/2000
 - General
diff --git a/po/nano.pot b/po/nano.pot
index cc0cd512..d21d18a5 100644
--- a/po/nano.pot
+++ b/po/nano.pot
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
-"POT-Creation-Date: 2000-10-18 15:38-0400\n"
+"POT-Creation-Date: 2000-10-24 01:12-0400\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -32,7 +32,7 @@ msgstr ""
 msgid "Read %d lines"
 msgstr ""
 
-#: files.c:217 search.c:177
+#: files.c:217 search.c:159 search.c:194
 #, c-format
 msgid "\"%s\" not found"
 msgstr ""
@@ -380,7 +380,7 @@ msgid "Case Sens"
 msgstr ""
 
 #: global.c:342 global.c:361 global.c:371 global.c:387 global.c:391
-#: global.c:397 winio.c:1004
+#: global.c:397 winio.c:979
 msgid "Cancel"
 msgstr ""
 
@@ -725,79 +725,79 @@ msgstr ""
 msgid "I got Alt-%c! (%d)\n"
 msgstr ""
 
-#: search.c:75
+#: search.c:77
 #, c-format
 msgid "Case Sensitive Regexp Search%s%s"
 msgstr ""
 
-#: search.c:77
+#: search.c:79
 #, c-format
 msgid "Regexp Search%s%s"
 msgstr ""
 
-#: search.c:80
+#: search.c:82
 #, c-format
 msgid "Case Sensitive Search%s%s"
 msgstr ""
 
-#: search.c:82
+#: search.c:84
 #, c-format
 msgid "Search%s%s"
 msgstr ""
 
-#: search.c:85
+#: search.c:87
 msgid " (to replace)"
 msgstr ""
 
-#: search.c:93
+#: search.c:95
 msgid "Search Cancelled"
 msgstr ""
 
-#: search.c:191
+#: search.c:175
 msgid "Search Wrapped"
 msgstr ""
 
-#: search.c:242
+#: search.c:254
 #, c-format
 msgid "Replaced %d occurences"
 msgstr ""
 
-#: search.c:244
+#: search.c:256
 msgid "Replaced 1 occurence"
 msgstr ""
 
-#: search.c:379 search.c:400 search.c:423
+#: search.c:392 search.c:413 search.c:436
 msgid "Replace Cancelled"
 msgstr ""
 
-#: search.c:396
+#: search.c:409
 #, c-format
 msgid "Replace with [%s]"
 msgstr ""
 
 #. last_search is empty
-#: search.c:421
+#: search.c:434
 msgid "Replace with"
 msgstr ""
 
-#: search.c:462
+#: search.c:475
 msgid "Replace this instance?"
 msgstr ""
 
 #. Ask for it
-#: search.c:513
+#: search.c:536
 msgid "Enter line number"
 msgstr ""
 
-#: search.c:515
+#: search.c:538
 msgid "Aborted"
 msgstr ""
 
-#: search.c:535
+#: search.c:558
 msgid "Come on, be reasonable"
 msgstr ""
 
-#: search.c:540
+#: search.c:563
 #, c-format
 msgid "Only %d lines available, skipping to last line"
 msgstr ""
@@ -807,67 +807,67 @@ msgstr ""
 msgid "actual_x_from_start for xplus=%d returned %d\n"
 msgstr ""
 
-#: winio.c:415
+#: winio.c:390
 #, c-format
 msgid "input '%c' (%d)\n"
 msgstr ""
 
-#: winio.c:449
+#: winio.c:424
 msgid "New Buffer"
 msgstr ""
 
-#: winio.c:452
+#: winio.c:427
 msgid "  File: ..."
 msgstr ""
 
-#: winio.c:460
+#: winio.c:435
 msgid "Modified"
 msgstr ""
 
-#: winio.c:920
+#: winio.c:895
 #, c-format
 msgid "Moved to (%d, %d) in edit buffer\n"
 msgstr ""
 
-#: winio.c:931
+#: winio.c:906
 #, c-format
 msgid "current->data = \"%s\"\n"
 msgstr ""
 
-#: winio.c:974
+#: winio.c:949
 #, c-format
 msgid "I got \"%s\"\n"
 msgstr ""
 
-#: winio.c:999
+#: winio.c:974
 msgid "Yes"
 msgstr ""
 
-#: winio.c:1001
+#: winio.c:976
 msgid "All"
 msgstr ""
 
-#: winio.c:1003
+#: winio.c:978
 msgid "No"
 msgstr ""
 
-#: winio.c:1140
+#: winio.c:1115
 #, c-format
 msgid "do_cursorpos: linepct = %f, bytepct = %f\n"
 msgstr ""
 
-#: winio.c:1144
+#: winio.c:1119
 msgid "line %d of %d (%.0f%%), character %d of %d (%.0f%%)"
 msgstr ""
 
-#: winio.c:1272
+#: winio.c:1247
 msgid "Dumping file buffer to stderr...\n"
 msgstr ""
 
-#: winio.c:1274
+#: winio.c:1249
 msgid "Dumping cutbuffer to stderr...\n"
 msgstr ""
 
-#: winio.c:1276
+#: winio.c:1251
 msgid "Dumping a buffer to stderr...\n"
 msgstr ""
diff --git a/search.c b/search.c
index 783635c4..4825da65 100644
--- a/search.c
+++ b/search.c
@@ -36,6 +36,8 @@
 
 static char last_search[132] = "";	/* Last string we searched for */
 static char last_replace[132] = "";	/* Last replacement string */
+static int search_last_line;		
+
 
 /* Regular expression helper functions */
 
@@ -128,7 +130,7 @@ int search_init(int replacing)
     return 0;
 }
 
-filestruct *findnextstr(int quiet, filestruct * begin, char *needle)
+filestruct *findnextstr(int quiet, filestruct * begin, int beginx, char *needle)
 {
     filestruct *fileptr;
     char *searchstr, *found = NULL, *tmp;
@@ -136,62 +138,71 @@ filestruct *findnextstr(int quiet, filestruct * begin, char *needle)
 
     fileptr = current;
 
-    searchstr = &current->data[current_x + 1];
-    /* Look for searchstr until EOF */
-    while (fileptr != NULL &&
-	   (found = strstrwrapper(searchstr, needle)) == NULL) {
-
-	fileptr = fileptr->next;
+    current_x++;
 
-	if (!past_editbot && (fileptr == editbot))
-	    past_editbot = 1;
+    /* Are we searching the last line? (i.e. the line where search started) */
+    if ( (fileptr == begin) && (current_x < beginx) )
+	search_last_line = 1;
 
-	if (fileptr == begin)
-	    return NULL;
+    /* Make sure we haven't passed the end of the string */
+    if ( strlen(fileptr->data) < current_x )
+	current_x--;
 
-	if (fileptr != NULL)
-	    searchstr = fileptr->data;
-    }
+    searchstr = &fileptr->data[current_x];
 
-    /* If we're not at EOF, we found an instance */
-    if (fileptr != NULL) {
-	current = fileptr;
-	current_x = 0;
-	for (tmp = fileptr->data; tmp != found; tmp++)
-	    current_x++;
+    /* Look for needle in searchstr */
+    while (( found = strstrwrapper(searchstr, needle)) == NULL) {
 
-	if (past_editbot)
-	    edit_update(current, CENTER);
-	placewewant = xplustabs();
-	reset_cursor();
-    } else {			/* We're at EOF, go back to the top, once */
-
-	fileptr = fileage;
-
-	while (fileptr != begin->next &&
-	       (found = strstrwrapper(fileptr->data, needle)) == NULL)
-	    fileptr = fileptr->next;
-
-	if (fileptr == begin->next) {
+	/* finished processing file, get out */
+        if (search_last_line) {
 	    if (!quiet)
 		statusbar(_("\"%s\" not found"), needle);
-
 	    return NULL;
 	}
-	else {	/* We found something */
-	    current = fileptr;
-	    current_x = 0;
-	    for (tmp = fileptr->data; tmp != found; tmp++)
-		current_x++;
 
-	    edit_update(current, CENTER);
-	    reset_cursor();
+	fileptr = fileptr->next;
+
+	if (!past_editbot && (fileptr == editbot))
+	    past_editbot = 1;
+
+	/* EOF reached, wrap around once */
+	if (fileptr == NULL) {
+	    fileptr = fileage;
+
+	    past_editbot = 1;
 
 	    if (!quiet)
 		statusbar(_("Search Wrapped"));
-	} 
+	}
+
+	/* Original start line reached */
+	if (fileptr == begin)
+	    search_last_line = 1;
+
+	searchstr = fileptr->data;
     }
 
+    /* We found an instance */
+    current = fileptr;
+    current_x = 0;
+    for (tmp = fileptr->data; tmp != found; tmp++)
+	current_x++;
+
+    /* Ensure we haven't wrap around again! */
+    if ((search_last_line) && (current_x >= beginx)) {
+	if (!quiet)
+	    statusbar(_("\"%s\" not found"), needle);
+	return NULL;
+    }
+
+    if (past_editbot)
+	edit_update(fileptr, CENTER);
+    else
+	update_line(current, current_x);
+
+    placewewant = xplustabs();
+    reset_cursor();
+
     return fileptr;
 }
 
@@ -231,7 +242,8 @@ int do_search(void)
 	search_abort();
 	return 1;
     }
-    findnextstr(0, current, answer);
+    search_last_line = 0;
+    findnextstr(0, current, current_x, answer);
     search_abort();
     return 1;
 }
@@ -250,6 +262,7 @@ void replace_abort(void)
        does something different later, we can change it back.  For now
        it's just a waste to duplicat code */
     search_abort();
+    placewewant = xplustabs();
 }
 
 #ifdef HAVE_REGEX_H
@@ -421,7 +434,6 @@ int do_replace(void)
 	i = statusq(replace_list, REPLACE_LIST_LEN, "", _("Replace with"));
 	if (i == -1) {
 	    statusbar(_("Replace Cancelled"));
-	    reset_cursor();
 	    replace_abort();
 	    return 0;
 	} else if (i == 0)	/* They entered something new */
@@ -444,14 +456,15 @@ int do_replace(void)
 
     /* save where we are */
     begin = current;
-    beginx = current_x;
+    beginx = current_x + 1;
+    search_last_line = 0;
 
     while (1) {
 
 	if (replaceall)
-	    fileptr = findnextstr(1, begin, prevanswer);
+	    fileptr = findnextstr(1, begin, beginx, prevanswer);
 	else
-	    fileptr = findnextstr(0, begin, prevanswer);
+	    fileptr = findnextstr(0, begin, beginx, prevanswer);
 
 	/* No more matches.  Done! */
 	if (!fileptr)
@@ -479,6 +492,16 @@ int do_replace(void)
 	    /* Stop bug where we replace a substring of the replacement text */
 	    current_x += strlen(last_replace) - 1;
 
+	    /* Adjust the original cursor position - COULD BE IMPROVED */
+	    if (search_last_line) {
+		beginx += strlen(last_replace) - strlen(last_search);
+
+		/* For strings that cross the search start/end boundary */
+		/* Don't go outside of allocated memory */
+		if (beginx < 1)
+		    beginx = 1;
+	    }
+
 	    edit_refresh();
 	    set_modified();
 	    numreplaced++;
@@ -487,7 +510,7 @@ int do_replace(void)
     }
 
     current = begin;
-    current_x = beginx;
+    current_x = beginx - 1;
     renumber_all();
     edit_update(current, CENTER);
     print_replaced(numreplaced);
diff --git a/winio.c b/winio.c
index 0bc0c63a..15118d0a 100644
--- a/winio.c
+++ b/winio.c
@@ -218,6 +218,15 @@ void check_statblank(void)
     }
 }
 
+/* Repaint the statusbar when getting a character in nanogetstr */
+void nanoget_repaint(char *buf, char *inputbuf, int x)
+{
+    blank_statusbar();
+    mvwaddstr(bottomwin, 0, 0, buf);
+    waddstr(bottomwin, inputbuf);
+    wmove(bottomwin, 0, x);
+}
+
 /* Get the input from the kb, this should only be called from statusq */
 int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 {
@@ -257,17 +266,11 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 
 	case KEY_HOME:
 	    x = x_left;
-	    blank_statusbar();
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
+	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 	case KEY_END:
 	    x = x_left + strlen(inputbuf);
-	    blank_statusbar();
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
+	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 	case KEY_RIGHT:
 
@@ -282,19 +285,13 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 			strlen(inputbuf) - (x - x_left) - 1);
 		inputbuf[strlen(inputbuf) - 1] = 0;
 	    }
-	    blank_statusbar();
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
+	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 	case NANO_CONTROL_K:
 	case NANO_CONTROL_U:
 	    *inputbuf = 0;
 	    x = x_left;
-	    blank_statusbar();
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
+	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 	case KEY_BACKSPACE:
 	case KEY_DC:
@@ -328,17 +325,11 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 		switch (kbinput = wgetch(edit)) {
 		case 70:
 		    x = x_left + strlen(inputbuf);
-		    blank_statusbar();
-		    mvwaddstr(bottomwin, 0, 0, buf);
-		    waddstr(bottomwin, inputbuf);
-		    wmove(bottomwin, 0, x);
+		    nanoget_repaint(buf, inputbuf, x);
 		    break;
 		case 72:
 		    x = x_left;
-		    blank_statusbar();
-		    mvwaddstr(bottomwin, 0, 0, buf);
-		    waddstr(bottomwin, inputbuf);
-		    wmove(bottomwin, 0, x);
+		    nanoget_repaint(buf, inputbuf, x);
 		    break;
 		}
 		break;
@@ -356,10 +347,7 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 		    break;
 		case 49:
 		    x = x_left;
-		    blank_statusbar();
-		    mvwaddstr(bottomwin, 0, 0, buf);
-		    waddstr(bottomwin, inputbuf);
-		    wmove(bottomwin, 0, x);
+		    nanoget_repaint(buf, inputbuf, x);
 		    goto skip_126;
 		case 51:
 		    if (strlen(inputbuf) > 0
@@ -369,17 +357,11 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 				strlen(inputbuf) - (x - x_left) - 1);
 			inputbuf[strlen(inputbuf) - 1] = 0;
 		    }
-		    blank_statusbar();
-		    mvwaddstr(bottomwin, 0, 0, buf);
-		    waddstr(bottomwin, inputbuf);
-		    wmove(bottomwin, 0, x);
+		    nanoget_repaint(buf, inputbuf, x);
 		    goto skip_126;
 		case 52:
 		    x = x_left + strlen(inputbuf);
-		    blank_statusbar();
-		    mvwaddstr(bottomwin, 0, 0, buf);
-		    waddstr(bottomwin, inputbuf);
-		    wmove(bottomwin, 0, x);
+		    nanoget_repaint(buf, inputbuf, x);
 		    goto skip_126;
 		  skip_126:
 		    nodelay(edit, TRUE);
@@ -390,13 +372,9 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 		    break;
 		}
 	    }
-	    blank_statusbar();
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
+	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 
-
 	default:
 	    if (kbinput < 32)
 		break;
@@ -407,10 +385,7 @@ int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x)
 	    strcpy(inputbuf, inputstr);
 	    x++;
 
-	    mvwaddstr(bottomwin, 0, 0, buf);
-	    waddstr(bottomwin, inputbuf);
-	    wmove(bottomwin, 0, x);
-
+	    nanoget_repaint(buf, inputbuf, x);
 #ifdef DEBUG
 	    fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput);
 #endif
-- 
GitLab