From 94a78b8fb3ef45886349374b4c2001f42b2c771e Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Wed, 14 Mar 2001 08:28:48 +0000
Subject: [PATCH] Added check for samlinewrap case and cursor placement fixes,
 closing BUG #55 =-)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@558 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 BUGS      |  6 ++----
 ChangeLog |  5 +++++
 nano.c    | 57 +++++++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/BUGS b/BUGS
index 2bb68059..8bd13b50 100644
--- a/BUGS
+++ b/BUGS
@@ -97,11 +97,9 @@
 - Cut to end cutting (-k) causes segfaults (try cutting "- Backup making
   (filename~)?" line in TODO file) (discovered by
   higuita@cadernoverde.com) (54) [FIXED].
-
+- When using autoindent (-i), wrapped text does not get auto-indented
+  (55, discovered by Mark Senior) [FIXED].
 
 ** Open BUGS **
 
 $Id$
-
-- When using autoindent (-i), wrapped text does not get auto-indented
-  (55, discovered by Mark Senior).
diff --git a/ChangeLog b/ChangeLog
index 04865ba0..71d5dc2f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,9 @@
 CVS code -
+- nano.c:
+  do_wrap()
+	- Added case for autoindenting text causing new line (Adam).
+	- Added SAMELINE case to above.  Added checks to cases 1b and
+	  2b for placement of cursor.
 - search.c:
 	- s/occurence/occurrence typos (Jordi).
 - nano.texi:
diff --git a/nano.c b/nano.c
index e4bdfc14..6a7c82a2 100644
--- a/nano.c
+++ b/nano.c
@@ -768,6 +768,7 @@ void do_wrap(filestruct * inptr, char input_char)
 
     /* Category 1a: one word taking up the whole line with no beginning spaces. */
     if ((last_word_end == -1) && (!isspace((int) inptr->data[0]))) {
+fprintf(stderr, "1a\n");
 	for (i = current_word_end; i < len; i++) {
 	    if (!isspace((int) inptr->data[i]) && i < len) {
 		current_word_start = i;
@@ -805,6 +806,15 @@ void do_wrap(filestruct * inptr, char input_char)
 	if (current_x >= current_word_start) {
 	    right = current_x - current_word_start;
 	    current_x = 0;
+	    if (ISSET(AUTOINDENT)) {
+		int i = 0;
+		while ((inptr->next->data[i] == ' ' 
+	    		|| inptr->next->data[i] == '\t')
+	    		&& inptr->next->data[i] != 0) {
+		    i++;
+		    right++;
+		}			
+	    }
 	    down = 1;
 	}
 
@@ -818,7 +828,7 @@ void do_wrap(filestruct * inptr, char input_char)
 
     /* Category 2: two or more words on the line. */
     else {
-
+fprintf(stderr, "2a\n");
 	/* Case 2a: cursor before word at wrap point. */
 	if (current_x < current_word_start) {
 	    temp->data =
@@ -851,6 +861,16 @@ void do_wrap(filestruct * inptr, char input_char)
 	    down = 1;
 
 	    right = current_x - current_word_start;
+	    if (ISSET(AUTOINDENT)) {
+		int i = 0;
+		while ((inptr->next->data[i] == ' ' 
+	    		|| inptr->next->data[i] == '\t')
+	    		&& inptr->next->data[i] != 0) {
+		    i++;
+		    right++;
+		}			
+	    }
+
 	    i = current_word_start - 1;
 	    if (isspace((int) input_char)
 		&& (current_x == current_word_start)) {
@@ -871,6 +891,7 @@ void do_wrap(filestruct * inptr, char input_char)
 
 	/* Case 2c: cursor past word at wrap point. */
 	else {
+fprintf(stderr, "2c\n");
 	    temp->data =
 		nmalloc(strlen(&inptr->data[current_word_start]) + 1);
 	    strcpy(temp->data, &inptr->data[current_word_start]);
@@ -892,14 +913,35 @@ void do_wrap(filestruct * inptr, char input_char)
 
     /* We pre-pend wrapped part to next line. */
     if (ISSET(SAMELINEWRAP) && inptr->next) {
-	/* Plus one for the space which concatenates the two lines together plus 1 for \0. */
-	char *p =
-	    nmalloc(strlen(temp->data) + strlen(inptr->next->data) + 2);
 	int old_x = current_x, old_y = current_y;
 
-	strcpy(p, temp->data);
-	strcat(p, " ");
-	strcat(p, inptr->next->data);
+	/* Plus one for the space which concatenates the two lines together plus 1 for \0. */
+	char *p = nmalloc((strlen(temp->data) + strlen(inptr->next->data) + 2) 
+			* sizeof(char));
+
+	if (ISSET(AUTOINDENT)) {
+	    int non = 0;
+
+	     /* Grab the beginning of the next line until it's not a 
+		space or tab, then null terminate it so we can strcat it
+		to hell */
+	    while ((inptr->next->data[non] == ' ' 
+	    	|| inptr->next->data[non] == '\t')
+	    	&& inptr->next->data[non] != 0)
+	    	    p[non] = inptr->next->data[non++];
+
+	    p[non] = 0;
+	    strcat(p, temp->data);
+	    strcat(p, " ");
+
+	     /* Now tack on the rest of the next line after the spaces and
+		tabs */
+	    strcat(p, &inptr->next->data[non]);
+	} else {
+	    strcpy(p, temp->data);
+	    strcat(p, " ");
+	    strcat(p, inptr->next->data);
+	}
 
 	free(inptr->next->data);
 	inptr->next->data = p;
@@ -933,7 +975,6 @@ void do_wrap(filestruct * inptr, char input_char)
 	        while ((*spc == ' ') || (*spc == '\t')) {
 		    extra++;
 		    spc++;
-		    right++;
 		    totsize++;
 		}
 		t = nmalloc(strlen(temp->data) + extra + 1);
-- 
GitLab