miscellaneous bug fixes, part 2


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1331 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
diff --git a/winio.c b/winio.c
index 21017b7..e15b4b4 100644
--- a/winio.c
+++ b/winio.c
@@ -210,7 +210,7 @@
     else
 	answer[0] = '\0';
 
-#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
+#if !defined(DISABLE_HELP) || (!defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION))
     currshortcut = s;
 #endif
 
@@ -261,13 +261,11 @@
 	case 545:		/* Right alt again */
 	    break;
 #endif
-#ifndef DISABLE_MOUSE
-#ifdef NCURSES_MOUSE_VERSION
+#if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION)
 	case KEY_MOUSE:
 	    do_mouse();
 	    break;
 #endif
-#endif
 	case NANO_HOME_KEY:
 	case KEY_HOME:
 	    x = 0;
@@ -515,7 +513,7 @@
 	    s = s->next;
 	    if (s == NULL)
 		goto break_completely_out;
-	}	
+	}
     }
 
   break_completely_out:
@@ -572,8 +570,8 @@
     const filestruct *ptr = edittop;
     size_t x;
 
-    /* Yuck.  This condition can be true after open_file when opening the
-     * first file. */
+    /* Yuck.  This condition can be true after open_file() when opening
+     * the first file. */
     if (edittop == NULL)
 	return;
 
@@ -640,8 +638,8 @@
 		while (k < start + COLS) {
 		    /* Note the fifth parameter to regexec.  It says not to
 		     * match the beginning-of-line character unless
-		     * k == 0.  If regexec returns non-zero, there are
-		     * no more matches in the line. */
+		     * k == 0.  If regexec returns nonzero, there are no
+		     * more matches in the line. */
 		    if (regexec(&start_regexp, &fileptr->data[k], 1,
 				&startmatch, k == 0 ? 0 : REG_NOTBOL))
 			break;
@@ -732,14 +730,12 @@
 		 * fileptr and after the start.  Is there an end after
 		 * the start at all?  We don't paint unterminated starts. */
 		end_line = fileptr;
-		while (end_line != NULL &&
-			regexec(&end_regexp, end_line->data, 1,
-				&endmatch, 0))
+		while (end_line != NULL && regexec(&end_regexp, end_line->data,
+				1, &endmatch, 0))
 		    end_line = end_line->next;
 
 		/* No end found, or it is too early. */
-		if (end_line == NULL ||
-			end_line->lineno < fileptr->lineno ||
+		if (end_line == NULL || end_line->lineno < fileptr->lineno ||
 			(end_line == fileptr && endmatch.rm_eo <= start))
 		    goto step_two;
 
@@ -801,9 +797,8 @@
 			/* There is no end on this line.  But we haven't
 			 * yet looked for one on later lines. */
 			end_line = fileptr->next;
-			while (end_line != NULL &&
-				regexec(&end_regexp, end_line->data, 1,
-				&endmatch, 0))
+			while (end_line != NULL && regexec(&end_regexp,
+				end_line->data, 1, &endmatch, 0))
 			    end_line = end_line->next;
 			if (end_line != NULL) {
 			    assert(0 <= x_start && x_start < COLS);
@@ -900,7 +895,7 @@
     size_t pos;
     size_t page_start;
 
-    if (!fileptr)
+    if (fileptr == NULL)
 	return;
 
     line = fileptr->lineno - edittop->lineno;
@@ -996,9 +991,6 @@
 /* Refresh the screen without changing the position of lines. */
 void edit_refresh(void)
 {
-    static int noloop = 0;
-    int nlines = 0, currentcheck = 0;
-
     /* Neither of these conditions should occur, but they do.  edittop is
      * NULL when you open an existing file on the command line, and
      * ENABLE_COLOR is defined.  Yuck. */
@@ -1007,40 +999,35 @@
     if (edittop == NULL)
 	edittop = current;
 
-    /* Don't make the cursor jump around the screen whilst updating */
-    leaveok(edit, TRUE);
-
-    editbot = edittop;
-    while (nlines < editwinrows) {
-	update_line(editbot, current_x);
-	if (editbot == current)
-	    currentcheck = 1;
-
-	nlines++;
-
-	if (editbot->next == NULL)
-	    break;
-	editbot = editbot->next;
-    }
-
-    /* If noloop == 1, then we already did an edit_update without finishing
-       this function.  So we don't run edit_update again */
-    if (!currentcheck && !noloop) {
-		/* Then current has run off the screen... */
+    if (current->lineno >= edittop->lineno + editwinrows)
+	/* Note that edit_update() changes edittop so that
+	 * current->lineno = edittop->lineno + editwinrows / 2.  Thus
+	 * when it then calls edit_refresh(), there is no danger of
+	 * getting an infinite loop. */
 	edit_update(current, CENTER);
-	noloop = 1;
-    } else if (noloop)
-	noloop = 0;
+    else {
+	int nlines = 0;
 
-    while (nlines < editwinrows) {
-	mvwaddstr(edit, nlines, 0, hblank);
-	nlines++;
+	/* Don't make the cursor jump around the screen whilst updating */
+	leaveok(edit, TRUE);
+
+	editbot = edittop;
+	while (nlines < editwinrows) {
+	    update_line(editbot, current_x);
+	    nlines++;
+	    if (editbot->next == NULL)
+		break;
+	    editbot = editbot->next;
+	}
+	while (nlines < editwinrows) {
+	    mvwaddstr(edit, nlines, 0, hblank);
+	    nlines++;
+	}
+	/* What the hell are we expecting to update the screen if this
+	   isn't here?  Luck?? */
+	wrefresh(edit);
+	leaveok(edit, FALSE);
     }
-
-    /* What the hell are we expecting to update the screen if this isn't 
-       here? Luck?? */
-    wrefresh(edit);
-    leaveok(edit, FALSE);
 }
 
 /*
@@ -1137,8 +1124,8 @@
 
 /*
  * Ask a simple yes/no question on the statusbar.  Returns 1 for Y, 0
- * for N, 2 for All (if all is non-zero when passed in) and -1 for
- * abort (^C).
+ * for N, 2 for All (if all is nonzero when passed in) and -1 for abort
+ * (^C).
  */
 int do_yesno(int all, int leavecursor, const char *msg, ...)
 {
@@ -1148,11 +1135,9 @@
     const char *yesstr;		/* String of yes characters accepted */
     const char *nostr;		/* Same for no */
     const char *allstr;		/* And all, surprise! */
-#ifndef DISABLE_MOUSE
-#ifdef NCURSES_MOUSE_VERSION
+#if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION)
     MEVENT mevent;
 #endif
-#endif
 
     /* Yes, no and all are strings of any length.  Each string consists of
        all characters accepted as a valid character for that value.
@@ -1204,8 +1189,7 @@
 	kbinput = wgetch(edit);
 
 	switch (kbinput) {
-#ifndef DISABLE_MOUSE
-#ifdef NCURSES_MOUSE_VERSION
+#if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION)
 	case KEY_MOUSE:
 
 	    /* Look ma!  We get to duplicate lots of code from do_mouse!! */
@@ -1233,7 +1217,6 @@
 	    }
 	    break;
 #endif
-#endif
 	case NANO_CONTROL_C:
 	    ok = -2;
 	    break;
@@ -1436,13 +1419,11 @@
 	const char *ptr = help_text;
 
 	switch (kbinput) {
-#ifndef DISABLE_MOUSE
-#ifdef NCURSES_MOUSE_VERSION
+#if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION)
 	case KEY_MOUSE:
 	    do_mouse();
 	    break;
 #endif
-#endif
 	case 27:
 	    kbinput = wgetch(edit);
 	    switch(kbinput) {
@@ -1562,7 +1543,7 @@
     return 1;
 }
 
-int keypad_on(WINDOW * win, int newval)
+int keypad_on(WINDOW *win, int newval)
 {
 /* This is taken right from aumix.  Don't sue me. */
 #ifdef HAVE_USEKEYPAD