From 2ffdea4fc1ea3376174f078f3c5ad43557d05601 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Thu, 3 Nov 2005 21:08:39 +0000
Subject: [PATCH] tweak a few functions to remove the assumption that the file
 always ends in a magicline, and to rely on fileage and filebot instead of
 NULL for their checks to detect the top or bottom of the file

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3080 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  9 +++++++++
 src/cut.c    | 27 ++++++++++++++-------------
 src/move.c   | 33 ++++++++++++++++++---------------
 src/nano.c   |  8 +++++---
 src/search.c |  2 +-
 src/text.c   | 29 ++++++++++-------------------
 6 files changed, 57 insertions(+), 51 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e681b2c5..179fcdb8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -37,6 +37,15 @@ CVS code -
 	  source file, and adjust related variables accordingly.  New
 	  file help.c; changes to help_init(), help_line_len(), and
 	  do_help() (all moved to help.c). (DLR)
+	- Tweak a few functions to remove the assumption that the file
+	  always ends in a magicline.  Changes to do_cut_till_end(),
+	  do_next_word(), and do_wordlinechar_count(). (DLR)
+	- Tweak a few functions to rely on fileage and filebot instead
+	  of NULL for their checks to detect the top or bottom of the
+	  file.  Changes to cut_line(), cut_to_eol(), do_page_up(),
+	  do_page_down(), do_para_end(), do_next_word(), do_prev_word(),
+	  do_up(), do_down(), do_scroll_down(), do_right(), do_mouse(),
+	  do_gotolinecolumn(), do_delete(), and find_paragraph(). (DLR)
 - nano.h:
 	- Readd MIN_EDITOR_COLS #define. (DLR)
 - winio.c:
diff --git a/src/cut.c b/src/cut.c
index d8cb01be..79bbf8bb 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -38,12 +38,12 @@ void cutbuffer_reset(void)
     keep_cutbuffer = FALSE;
 }
 
-/* If we're not on the magicline, move all the text of the current line,
- * plus the newline at the end, to the cutbuffer, and set the current
- * place we want to where the line used to start. */
+/* If we're not on the last line of the file, move all the text of the
+ * current line, plus the newline at the end, to the cutbuffer, and set
+ * the current place we want to where the line used to start. */
 void cut_line(void)
 {
-    if (openfile->current->next != NULL) {
+    if (openfile->current != openfile->filebot) {
 	move_to_filestruct(&cutbuffer, &cutbottom, openfile->current, 0,
 		openfile->current->next, 0);
 	openfile->placewewant = xplustabs();
@@ -68,9 +68,9 @@ void cut_marked(void)
 /* If we're not at the end of the current line, move all the text from
  * the current cursor position to the end of the current line,
  * not counting the newline at the end, to the cutbuffer.  If we are,
- * and we're not on the magicline, move the newline at the end to the
- * cutbuffer, and set the current place we want to where the newline
- * used to be. */
+ * and we're not on the last line of the file, move the newline at the
+ * end to the cutbuffer, and set the current place we want to where the
+ * newline used to be. */
 void cut_to_eol(void)
 {
     size_t data_len = strlen(openfile->current->data);
@@ -83,11 +83,11 @@ void cut_to_eol(void)
 	 * the end, to the cutbuffer. */
 	move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
 		openfile->current_x, openfile->current, data_len);
-    else if (openfile->current->next != NULL) {
-	/* If we're at the end of the line, and it isn't the magicline,
-	 * move all the text from the current position up to the
-	 * beginning of the next line, i.e, the newline at the end, to
-	 * the cutbuffer. */
+    else if (openfile->current != openfile->filebot) {
+	/* If we're at the end of the line, and it isn't the last line
+	 * of the file, move all the text from the current position up
+	 * to the beginning of the next line, i.e, the newline at the
+	 * end, to the cutbuffer. */
 	move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
 		openfile->current_x, openfile->current->next, 0);
 	openfile->placewewant = xplustabs();
@@ -149,7 +149,8 @@ void do_cut_till_end(void)
     check_statusblank();
 
     move_to_filestruct(&cutbuffer, &cutbottom, openfile->current,
-	openfile->current_x, openfile->filebot, 0);
+	openfile->current_x, openfile->filebot,
+	strlen(openfile->filebot->data));
 
     edit_refresh();
     set_modified();
diff --git a/src/move.c b/src/move.c
index 00bafa70..fc86a3bc 100644
--- a/src/move.c
+++ b/src/move.c
@@ -86,8 +86,8 @@ void do_page_up(void)
     }
 #endif
 
-    for (i = editwinrows - 2; i > 0 && openfile->current->prev != NULL;
-	i--)
+    for (i = editwinrows - 2; i > 0 && openfile->current !=
+	openfile->fileage; i--)
 	openfile->current = openfile->current->prev;
 
     openfile->current_x = actual_x(openfile->current->data,
@@ -127,8 +127,8 @@ void do_page_down(void)
     }
 #endif
 
-    for (i = editwinrows - 2; i > 0 && openfile->current->next != NULL;
-	i--)
+    for (i = editwinrows - 2; i > 0 && openfile->current !=
+	openfile->filebot; i--)
 	openfile->current = openfile->current->next;
 
     openfile->current_x = actual_x(openfile->current->data,
@@ -151,7 +151,7 @@ void do_para_begin(bool allow_update)
     openfile->current_x = 0;
     openfile->placewewant = 0;
 
-    if (openfile->current->prev != NULL) {
+    if (openfile->current != openfile->fileage) {
 	do {
 	    openfile->current = openfile->current->prev;
 	    openfile->current_y--;
@@ -181,17 +181,18 @@ void do_para_end(bool allow_update)
     openfile->current_x = 0;
     openfile->placewewant = 0;
 
-    while (openfile->current->next != NULL && !inpar(openfile->current))
+    while (openfile->current != openfile->filebot &&
+	!inpar(openfile->current))
 	openfile->current = openfile->current->next;
 
-    while (openfile->current->next != NULL &&
+    while (openfile->current != openfile->filebot &&
 	inpar(openfile->current->next) &&
 	!begpar(openfile->current->next)) {
 	openfile->current = openfile->current->next;
 	openfile->current_y++;
     }
 
-    if (openfile->current->next != NULL)
+    if (openfile->current != openfile->filebot)
 	openfile->current = openfile->current->next;
 
     if (allow_update)
@@ -272,7 +273,7 @@ bool do_next_word(bool allow_punct, bool allow_update)
 	if (!end_line)
 	    break;
 
-	if (openfile->current->next != NULL) {
+	if (openfile->current != openfile->filebot) {
 	    end_line = FALSE;
 	    openfile->current_x = 0;
 	}
@@ -282,8 +283,10 @@ bool do_next_word(bool allow_punct, bool allow_update)
 
     /* If we haven't found it, leave the cursor at the end of the
      * file. */
-    if (openfile->current == NULL)
+    if (openfile->current == NULL) {
 	openfile->current = openfile->filebot;
+	openfile->current_x = strlen(openfile->filebot->data);
+    }
 
     openfile->placewewant = xplustabs();
 
@@ -372,7 +375,7 @@ bool do_prev_word(bool allow_punct, bool allow_update)
 	if (!begin_line)
 	    break;
 
-	if (openfile->current->prev != NULL) {
+	if (openfile->current != openfile->fileage) {
 	    begin_line = FALSE;
 	    openfile->current_x = strlen(openfile->current->prev->data);
 	}
@@ -483,7 +486,7 @@ void do_up(void)
 #endif
 
     /* If we're at the top of the file, get out. */
-    if (openfile->current->prev == NULL)
+    if (openfile->current == openfile->fileage)
 	return;
 
     assert(openfile->current_y == openfile->current->lineno - openfile->edittop->lineno);
@@ -546,7 +549,7 @@ void do_down(void)
 #endif
 
     /* If we're at the bottom of the file, get out. */
-    if (openfile->current->next == NULL)
+    if (openfile->current == openfile->filebot)
 	return;
 
     assert(openfile->current_y == openfile->current->lineno - openfile->edittop->lineno);
@@ -585,7 +588,7 @@ void do_scroll_down(void)
 #endif
 
     /* If we're at the bottom of the file, get out. */
-    if (openfile->current->next == NULL)
+    if (openfile->current == openfile->filebot)
 	return;
 
     assert(openfile->current_y == openfile->current->lineno - openfile->edittop->lineno);
@@ -631,7 +634,7 @@ void do_right(void)
     if (openfile->current->data[openfile->current_x] != '\0')
 	openfile->current_x = move_mbright(openfile->current->data,
 		openfile->current_x);
-    else if (openfile->current->next != NULL) {
+    else if (openfile->current != openfile->filebot) {
 	do_down();
 	openfile->current_x = 0;
     }
diff --git a/src/nano.c b/src/nano.c
index aaf17a12..83d3d61f 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -230,7 +230,7 @@ partition *partition_filestruct(filestruct *top, size_t top_x,
 }
 
 /* Unpartition a filestruct so it begins at (fileage, 0) and ends at
- * (filebot, strlen(filebot)) again. */
+ * (filebot, strlen(filebot->data)) again. */
 void unpartition_filestruct(partition **p)
 {
     char *tmp;
@@ -1412,10 +1412,12 @@ bool do_mouse(void)
 
 	    /* Move to where the click occurred. */
 	    for (; openfile->current_y < mouse_y &&
-		openfile->current->next != NULL; openfile->current_y++)
+		openfile->current != openfile->filebot;
+		openfile->current_y++)
 		openfile->current = openfile->current->next;
 	    for (; openfile->current_y > mouse_y &&
-		openfile->current->prev != NULL; openfile->current_y--)
+		openfile->current != openfile->fileage;
+		openfile->current_y--)
 		openfile->current = openfile->current->prev;
 
 	    openfile->current_x = actual_x(openfile->current->data,
diff --git a/src/search.c b/src/search.c
index 47070afc..60d21261 100644
--- a/src/search.c
+++ b/src/search.c
@@ -994,7 +994,7 @@ void do_gotolinecolumn(ssize_t line, ssize_t column, bool use_answer,
     }
 
     for (openfile->current = openfile->fileage;
-	openfile->current->next != NULL && line > 1; line--)
+	openfile->current != openfile->filebot && line > 1; line--)
 	openfile->current = openfile->current->next;
 
     openfile->current_x = actual_x(openfile->current->data, column - 1);
diff --git a/src/text.c b/src/text.c
index 5ed4cc9e..e3559d73 100644
--- a/src/text.c
+++ b/src/text.c
@@ -97,11 +97,7 @@ void do_delete(void)
 	    openfile->mark_begin_x -= char_buf_len;
 #endif
 	openfile->totsize--;
-    } else if (openfile->current != openfile->filebot &&
-	(openfile->current->next != openfile->filebot ||
-	openfile->current->data[0] == '\0')) {
-	/* We can delete the line before filebot only if it is blank: it
-	 * becomes the new magicline then. */
+    } else if (openfile->current != openfile->filebot) {
 	filestruct *foo = openfile->current->next;
 
 	assert(openfile->current_x == strlen(openfile->current->data));
@@ -132,6 +128,12 @@ void do_delete(void)
 #ifndef DISABLE_WRAPPING
 	wrap_reset();
 #endif
+
+	/* If we deleted the line before filebot, and the resulting
+	 * line at filebot isn't blank, add a new magicline. */
+	if (openfile->current == openfile->filebot &&
+		openfile->current->data[0] != '\0')
+	    new_magicline();
     } else
 	return;
 
@@ -1065,7 +1067,7 @@ bool find_paragraph(size_t *const quote, size_t *const par)
 	if (openfile->current == current_save ||
 		!inpar(openfile->current->prev))
 	    return FALSE;
-	if (openfile->current->prev != NULL)
+	if (openfile->current != openfile->fileage)
 	    openfile->current = openfile->current->prev;
     }
     if (!begpar(openfile->current))
@@ -2074,21 +2076,15 @@ void do_wordlinechar_count(void)
     size_t pww_save = openfile->placewewant;
     filestruct *current_save = openfile->current;
     bool old_mark_set = openfile->mark_set;
-    bool added_magicline = FALSE;
-	/* Whether we added a magicline after filebot. */
     filestruct *top, *bot;
     size_t top_x, bot_x;
 
     if (old_mark_set) {
 	/* If the mark is on, partition the filestruct so that it
-	 * contains only the marked text, keep track of whether the text
-	 * will need a magicline added while we're counting words, add
-	 * the magicline if necessary, and turn the mark off. */
+	 * contains only the marked text, and turn the mark off. */
 	mark_order((const filestruct **)&top, &top_x,
 	    (const filestruct **)&bot, &bot_x, NULL);
 	filepart = partition_filestruct(top, top_x, bot, bot_x);
-	if ((added_magicline = (openfile->filebot->data[0] != '\0')))
-	    new_magicline();
 	openfile->mark_set = FALSE;
     }
 
@@ -2102,7 +2098,7 @@ void do_wordlinechar_count(void)
      * until we reach the end of the file, incrementing the total word
      * count whenever we're on a word just before moving. */
     while (openfile->current != openfile->filebot ||
-	openfile->current_x != 0) {
+	openfile->current->data[openfile->current_x] != '\0') {
 	if (do_next_word(TRUE, FALSE))
 	    words++;
     }
@@ -2110,11 +2106,6 @@ void do_wordlinechar_count(void)
     /* Get the total line and character counts, as "wc -l"  and "wc -c"
      * do, but get the latter in multibyte characters. */
     if (old_mark_set) {
-	/* If the mark was on and we added a magicline, remove it
-	 * now. */
-	if (added_magicline)
-	    remove_magicline();
-
 	lines = openfile->filebot->lineno -
 		openfile->fileage->lineno + 1;
 	chars = get_totsize(openfile->fileage, openfile->filebot);
-- 
GitLab