From 228148b87ace35f4d81adbce8c06f5e8aba13857 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Thu, 27 May 2004 20:09:52 +0000
Subject: [PATCH] add DB's tweaks to do_enter() and remove the now-unused
 center_cursor()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1769 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  4 ++-
 src/nano.c  | 80 +++++++++++++++++++++--------------------------------
 src/proto.h |  1 -
 src/winio.c | 22 ---------------
 4 files changed, 35 insertions(+), 72 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 24a874a6..ba419530 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -92,7 +92,7 @@ CVS code -
 	- Call enable_signals() at the beginning and disable_signals()
 	  at the end, so that we get a SIGINT when Ctrl-C is pressed
 	  during wait() and can then call cancel_fork() properly. (DLR)
-  do_delete()
+  do_delete(), do_enter()
 	- Tweak for efficiency. (David Benbennick)
   do_prev_word()
 	- Switch the last test (current != NULL or not) around to match
@@ -275,6 +275,8 @@ CVS code -
 	- Overhaul for efficiency, (David Benbennick)  DLR: Add
 	  reset_cursor() call to ensure that the cursor is always in the
 	  right place.
+  update_cursor()
+	- Removed, as it's no longer called anywhere. (David Benbennick)
   edit_refresh()
 	- Remove apparently unneeded leaveok() calls. (David Benbennick)
   edit_refresh_clearok(), center_cursor()
diff --git a/src/nano.c b/src/nano.c
index 35e42603..76c0a5d0 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1074,77 +1074,61 @@ int do_tab(void)
 /* Someone hits return *gasp!* */
 int do_enter(void)
 {
-    filestruct *newnode;
-    char *tmp;
+    filestruct *newnode = make_new_node(current);
+    size_t extra = 0;
 
-    newnode = make_new_node(current);
     assert(current != NULL && current->data != NULL);
-    tmp = &current->data[current_x];
 
 #ifndef NANO_SMALL
     /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
     if (ISSET(AUTOINDENT)) {
-	int extra = 0;
-	const char *spc = current->data;
-
-	while (isblank(*spc)) {
-	    extra++;
-	    spc++;
-	}
-	/* If current_x < extra, then we are breaking the line in the
-	 * indentation.  Autoindenting should add only current_x
-	 * characters of indentation. */
-	if (current_x < extra)
+	/* If we are breaking the line in the indentation, the new
+	 * indentation should have only current_x characters, and
+	 * current_x should not change. */
+	extra = indent_length(current->data);
+	if (extra > current_x)
 	    extra = current_x;
-	else
-	    current_x = extra;
 	totsize += extra;
-
-	newnode->data = charalloc(strlen(tmp) + extra + 1);
+    }
+#endif
+    newnode->data = charalloc(strlen(current->data + current_x) +
+	extra + 1);
+    strcpy(&newnode->data[extra], current->data + current_x);
+#ifndef NANO_SMALL
+    if (ISSET(AUTOINDENT))
 	strncpy(newnode->data, current->data, extra);
-	strcpy(&newnode->data[extra], tmp);
-    } else 
 #endif
-    {
-	current_x = 0;
-	newnode->data = charalloc(strlen(tmp) + 1);
-	strcpy(newnode->data, tmp);
+    null_at(&current->data, current_x);
+#ifndef NANO_SMALL
+    if (current == mark_beginbuf && current_x < mark_beginx) {
+	mark_beginbuf = newnode;
+	mark_beginx += extra - current_x;
     }
-    *tmp = '\0';
+#endif
+    current_x = extra;
 
-    if (current->next == NULL)
+    if (current == filebot)
 	filebot = newnode;
     splice_node(current, newnode, current->next);
 
     totsize++;
     renumber(current);
     current = newnode;
-    align(&current->data);
-
-    /* The logic here is as follows:
-     *    -> If we are at the bottom of the buffer, we want to recenter
-     *       (read: rebuild) the screen and forcibly move the cursor.
-     *    -> otherwise, we want simply to redraw the screen and update
-     *       where we think the cursor is.
-     */
-    if (current_y == editwinrows - 1) {
-#ifndef NANO_SMALL
-	if (ISSET(SMOOTHSCROLL))
-	    edit_update(current, NONE);
-	else
+
+#ifndef NANO_SMALL
+    /* If we're in smooth scrolling mode and we're on the last line of
+     * the edit window, move edittop down one line so that current is
+     * onscreen.  This prevents edit_refresh() from centering the
+     * screen. */
+    if (ISSET(SMOOTHSCROLL) && current_y == editwinrows - 1)
+	edittop = edittop->next;
 #endif
-	    edit_update(current, CENTER);
-	reset_cursor();
-    } else {
-	current_y++;
-	edit_refresh();
-	update_cursor();
-    }
+    edit_refresh();
 
     totlines++;
     set_modified();
-
     placewewant = xplustabs();
+
     return 1;
 }
 
diff --git a/src/proto.h b/src/proto.h
index 082e8d3f..2680642b 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -513,7 +513,6 @@ void reset_cursor(void);
 void edit_add(const filestruct *fileptr, const char *converted, int
 	yval, size_t start);
 void update_line(const filestruct *fileptr, size_t index);
-void update_cursor(void);
 void edit_refresh(void);
 void edit_update(filestruct *fileptr, topmidnone location);
 int statusq(int allowtabs, const shortcut *s, const char *def,
diff --git a/src/winio.c b/src/winio.c
index c85530a8..3eea2908 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2195,28 +2195,6 @@ void update_line(const filestruct *fileptr, size_t index)
 	mvwaddch(edit, line, COLS - 1, '$');
 }
 
-/* This function updates current, based on where current_y is;
- * reset_cursor() does the opposite. */
-void update_cursor(void)
-{
-    int i = 0;
-
-#ifdef DEBUG
-    fprintf(stderr, "Moved to (%d, %d) in edit buffer\n", current_y,
-	    current_x);
-#endif
-
-    current = edittop;
-    while (i < current_y && current->next != NULL) {
-	current = current->next;
-	i++;
-    }
-
-#ifdef DEBUG
-    fprintf(stderr, "current->data = \"%s\"\n", current->data);
-#endif
-}
-
 /* Refresh the screen without changing the position of lines. */
 void edit_refresh(void)
 {
-- 
GitLab