Moved extension functions to meta keys, new macro TOGGLE() for toggling flags


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@686 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
diff --git a/BUGS b/BUGS
index 932e140..76bea52 100644
--- a/BUGS
+++ b/BUGS
@@ -120,4 +120,8 @@
 
 ** Open BUGS **
 
+Informal note - when using marked write to file, if there's only one
+line of text hilighted, it writes the whole rest of the ifle to disk and
+goes bonkers.  Delete this message when fixed.
+
 $Id$
diff --git a/ChangeLog b/ChangeLog
index edeadaf..953b21d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -40,6 +40,17 @@
 	  nano_reverse_msg, new functions revstrstr and revstrcasestr,
 	  many changes to search functions.  Not too big a code size
 	  increase!
+	- Moved extension functions (Case Sensitive, Regexp, and Backwards
+	  Search, Append key in write file function) to Meta keys, as
+	  people are complaining loudly about nano not being control-key
+	  compatible with Pico, which is a Bag Thing (TM).  Changes to
+	  shortcut_init, toggle_init, new toggles for backwards and regexp
+	  (and you can now toggle all search options including regexp at
+	  the Search: prompt!)  Changes to nanogetstr to enable Meta
+	  keys to be grabbed, changes to onekey to print M-style shortcuts. 
+	- New macro TOGGLE which just toggles, no more silly checking
+	  ISSET and then using SET or UNSET when we want a simple toggle
+	  for a flag.
 - configure.in:
 	- New option, --enable-nanorc, which allows people to have a .nanorc
 	  initialization file and set options normally used on the command
diff --git a/global.c b/global.c
index 7dea333..0290320 100644
--- a/global.c
+++ b/global.c
@@ -139,7 +139,8 @@
 #ifndef NANO_SMALL
     char *toggle_const_msg, *toggle_autoindent_msg, *toggle_suspend_msg,
 	*toggle_nohelp_msg, *toggle_picomode_msg, *toggle_mouse_msg,
-	*toggle_cuttoend_msg, *toggle_wrap_msg;
+	*toggle_cuttoend_msg, *toggle_wrap_msg, *toggle_case_msg, 
+	*toggle_backwards_msg;
 #ifdef HAVE_REGEX_H
     char *toggle_regexp_msg;
 #endif
@@ -151,6 +152,8 @@
     toggle_picomode_msg = _("Pico mode");
     toggle_mouse_msg = _("Mouse support");
     toggle_cuttoend_msg = _("Cut to end");
+    toggle_backwards_msg = _("Backwards Search");
+    toggle_case_msg = _("Case Sensitive Search");
 #ifdef HAVE_REGEX_H
     toggle_regexp_msg = _("Regular expressions");
 #endif
@@ -172,8 +175,12 @@
 		    USE_MOUSE);
     toggle_init_one(&toggles[7], TOGGLE_CUTTOEND_KEY, toggle_cuttoend_msg,
 		    CUT_TO_END);
+    toggle_init_one(&toggles[8], TOGGLE_BACKWARDS_KEY, toggle_backwards_msg,
+		    REVERSE_SEARCH);
+    toggle_init_one(&toggles[9], TOGGLE_CASE_KEY, toggle_case_msg,
+		    CASE_SENSITIVE);    
 #ifdef HAVE_REGEX_H
-    toggle_init_one(&toggles[8], TOGGLE_REGEXP_KEY, toggle_regexp_msg,
+    toggle_init_one(&toggles[10], TOGGLE_REGEXP_KEY, toggle_regexp_msg,
 		    USE_REGEXP);
 #endif
 #endif
@@ -194,7 +201,8 @@
 	"", *nano_backspace_msg = "", *nano_tab_msg =
 	"", *nano_enter_msg = "", *nano_case_msg =
 	"", *nano_cancel_msg = "", *nano_unjustify_msg = 
-	"", *nano_append_msg = "", *nano_reverse_msg = "";
+	"", *nano_append_msg = "", *nano_reverse_msg = 
+	"", *nano_regexp_msg = "";
 
 #ifndef NANO_SMALL
     char *nano_tofiles_msg = "";
@@ -237,6 +245,7 @@
     nano_cancel_msg = _("Cancel the current function");
     nano_append_msg = _("Append to the current file");
     nano_reverse_msg = _("Search Backwards");
+    nano_regexp_msg = _("Use Regular Expressions");
 #endif
 
 	sc_init_one(&main_list[0], NANO_HELP_KEY, _("Get Help"),
@@ -359,22 +368,26 @@
     sc_init_one(&whereis_list[1], NANO_LASTLINE_KEY, _("Last Line"),
 		nano_lastline_msg, 0, 0, 0, VIEW, do_last_line);
 
-    sc_init_one(&whereis_list[2], NANO_CASE_KEY, _("Case Sens"),
-		nano_case_msg, 0, 0, 0, VIEW, 0);
-
-
-    sc_init_one(&whereis_list[3], NANO_OTHERSEARCH_KEY, _("Replace"),
+    sc_init_one(&whereis_list[2], NANO_OTHERSEARCH_KEY, _("Replace"),
 		nano_replace_msg, 0, 0, 0, VIEW, do_replace);
 
-    sc_init_one(&whereis_list[4], NANO_FROMSEARCHTOGOTO_KEY,
+    sc_init_one(&whereis_list[3], NANO_FROMSEARCHTOGOTO_KEY,
 		_("Goto Line"), nano_goto_msg, 0, 0, 0, VIEW,
 		do_gotoline_void);
 
-    sc_init_one(&whereis_list[5], NANO_REVERSESEARCH_KEY, _("Backward"),
+    sc_init_one(&whereis_list[4], NANO_CANCEL_KEY, _("Cancel"),
+		nano_cancel_msg, 0, 0, 0, VIEW, 0);
+
+    sc_init_one(&whereis_list[5], TOGGLE_CASE_KEY, _("Case Sens"),
+		nano_case_msg, 0, 0, 0, VIEW, 0);
+
+    sc_init_one(&whereis_list[6], TOGGLE_BACKWARDS_KEY, _("Backward"),
 		nano_reverse_msg, 0, 0, 0, VIEW, 0);
 
-    sc_init_one(&whereis_list[6], NANO_CANCEL_KEY, _("Cancel"),
-		nano_cancel_msg, 0, 0, 0, VIEW, 0);
+#ifdef HAVE_REGEX_H
+    sc_init_one(&whereis_list[7], TOGGLE_REGEXP_KEY, _("Regexp"),
+		nano_regexp_msg, 0, 0, 0, VIEW, 0);
+#endif
 
     sc_init_one(&replace_list[0], NANO_FIRSTLINE_KEY, _("First Line"),
 		nano_firstline_msg, 0, 0, 0, VIEW, do_first_line);
@@ -382,21 +395,26 @@
     sc_init_one(&replace_list[1], NANO_LASTLINE_KEY, _("Last Line"),
 		nano_lastline_msg, 0, 0, 0, VIEW, do_last_line);
 
-    sc_init_one(&replace_list[2], NANO_CASE_KEY, _("Case Sens"),
-		nano_case_msg, 0, 0, 0, VIEW, 0);
-
-    sc_init_one(&replace_list[3], NANO_OTHERSEARCH_KEY, _("No Replace"),
+    sc_init_one(&replace_list[2], NANO_OTHERSEARCH_KEY, _("No Replace"),
 		nano_whereis_msg, 0, 0, 0, VIEW, do_search);
 
-    sc_init_one(&replace_list[4], NANO_FROMSEARCHTOGOTO_KEY,
+    sc_init_one(&replace_list[3], NANO_FROMSEARCHTOGOTO_KEY,
 		_("Goto Line"), nano_goto_msg, 0, 0, 0, VIEW,
 		do_gotoline_void);
 
-    sc_init_one(&replace_list[5], NANO_REVERSESEARCH_KEY, _("Backward"),
+    sc_init_one(&replace_list[4], NANO_CANCEL_KEY, _("Cancel"),
+		nano_cancel_msg, 0, 0, 0, VIEW, 0);
+
+    sc_init_one(&replace_list[5], TOGGLE_CASE_KEY, _("Case Sens"),
+		nano_case_msg, 0, 0, 0, VIEW, 0);
+
+    sc_init_one(&replace_list[6], TOGGLE_BACKWARDS_KEY, _("Backward"),
 		nano_reverse_msg, 0, 0, 0, VIEW, 0);
 
-    sc_init_one(&replace_list[6], NANO_CANCEL_KEY, _("Cancel"),
-		nano_cancel_msg, 0, 0, 0, VIEW, 0);
+#ifdef HAVE_REGEX_H
+    sc_init_one(&replace_list[7], TOGGLE_REGEXP_KEY, _("Regexp"),
+		nano_regexp_msg, 0, 0, 0, VIEW, 0);
+#endif
 
 
     sc_init_one(&replace_list_2[0], NANO_FIRSTLINE_KEY, _("First Line"),
diff --git a/nano.c b/nano.c
index bed6938..f605dde 100644
--- a/nano.c
+++ b/nano.c
@@ -2146,10 +2146,8 @@
     char *enabled = _("enabled");
     char *disabled = _("disabled");
 
-    if (ISSET(toggles[which].flag))
-	UNSET(toggles[which].flag);
-    else
-	SET(toggles[which].flag);
+    /* Even easier! */
+    TOGGLE(toggles[which].flag);
 
     switch (toggles[which].val) {
     case TOGGLE_PICOMODE_KEY:
diff --git a/nano.h b/nano.h
index 23c928f..b381d02 100644
--- a/nano.h
+++ b/nano.h
@@ -34,6 +34,7 @@
 #define SET(bit) flags |= bit
 #define UNSET(bit) flags &= ~bit
 #define ISSET(bit) (flags & bit)
+#define TOGGLE(bit) flags ^= bit
 
 
 #ifdef USE_SLANG	/* Slang support enabled */
@@ -206,7 +207,6 @@
 #define NANO_REPLACE_FKEY	KEY_F(14)
 #define NANO_ALT_REPLACE_KEY	NANO_ALT_R
 #define NANO_OTHERSEARCH_KEY	NANO_CONTROL_R
-#define NANO_REVERSESEARCH_KEY NANO_CONTROL_B
 #define NANO_PREVPAGE_KEY	NANO_CONTROL_Y
 #define NANO_PREVPAGE_FKEY	KEY_F(7)
 #define NANO_NEXTPAGE_KEY	NANO_CONTROL_V
@@ -222,7 +222,6 @@
 #define NANO_FIRSTLINE_KEY	NANO_PREVPAGE_KEY
 #define NANO_LASTLINE_KEY	NANO_NEXTPAGE_KEY
 #define NANO_CANCEL_KEY		NANO_CONTROL_C
-#define NANO_CASE_KEY		NANO_CONTROL_A
 #define NANO_REFRESH_KEY	NANO_CONTROL_L
 #define NANO_JUSTIFY_KEY	NANO_CONTROL_J
 #define NANO_JUSTIFY_FKEY	KEY_F(4)
@@ -241,7 +240,7 @@
 #define NANO_ENTER_KEY		NANO_CONTROL_M
 #define NANO_FROMSEARCHTOGOTO_KEY NANO_CONTROL_T
 #define NANO_TOFILES_KEY	NANO_CONTROL_T
-#define NANO_APPEND_KEY		NANO_CONTROL_A
+#define NANO_APPEND_KEY		NANO_ALT_A
 
 #define TOGGLE_CONST_KEY	NANO_ALT_C
 #define TOGGLE_AUTOINDENT_KEY	NANO_ALT_I
@@ -252,11 +251,23 @@
 #define TOGGLE_CUTTOEND_KEY	NANO_ALT_K
 #define TOGGLE_REGEXP_KEY	NANO_ALT_E
 #define TOGGLE_WRAP_KEY		NANO_ALT_W
+#define TOGGLE_BACKWARDS_KEY	NANO_ALT_B
+#define TOGGLE_CASE_KEY		NANO_ALT_A
+
+/* Toggle stuff, these static lengths need to go away RSN */
+
+#ifdef HAVE_REGEX_H
+#define TOGGLE_LEN 11
+#define WHEREIS_LIST_LEN 8
+#define REPLACE_LIST_LEN 8
+#else
+#define TOGGLE_LEN 10
+#define WHEREIS_LIST_LEN 7
+#define REPLACE_LIST_LEN 7
+#endif
 
 #define MAIN_LIST_LEN 26
 #define MAIN_VISIBLE 12
-#define WHEREIS_LIST_LEN 7
-#define REPLACE_LIST_LEN 7
 #define REPLACE_LIST_2_LEN 3
 #define GOTO_LIST_LEN 3
 #define GOTODIR_LIST_LEN 1
@@ -272,12 +283,6 @@
 #define IMSERTFILE_LIST_LEN 1
 #endif
 
-#ifdef HAVE_REGEX_H
-#define TOGGLE_LEN 9
-#else
-#define TOGGLE_LEN 8
-#endif
-
 #define VIEW 1
 #define NOVIEW 0
 
diff --git a/search.c b/search.c
index 584c138..74cb74c 100644
--- a/search.c
+++ b/search.c
@@ -72,7 +72,7 @@
 */
 int search_init(int replacing)
 {
-    int i = 0;
+    int i = 0, j;
     char *buf;
     char *prompt;
     static char *backupstring = NULL;
@@ -137,7 +137,10 @@
 	free(backupstring);
 	backupstring = NULL;
 	return -1;
-    } else if (i == -2) {	/* Same string */
+    } else 
+    switch (i) {
+
+    case -2:	/* Same string */
 #ifdef HAVE_REGEX_H
 	if (ISSET(USE_REGEXP)) {
 
@@ -147,10 +150,9 @@
 	    else
 		regexp_init(answer);
 	}
-#else
-	;
 #endif
-    } else if (i == 0) {	/* They entered something new */
+	break;
+    case 0:		/* They entered something new */
 #ifdef HAVE_REGEX_H
 	if (ISSET(USE_REGEXP))
 	    regexp_init(answer);
@@ -158,37 +160,41 @@
 	free(backupstring);
 	backupstring = NULL;
 	last_replace[0] = '\0';
-    } else if (i == NANO_CASE_KEY) {	/* They want it case sensitive */
+	break;
+    case TOGGLE_CASE_KEY:
+    case TOGGLE_BACKWARDS_KEY:
+#ifdef HAVE_REGEX_H
+    case TOGGLE_REGEXP_KEY:
+#endif
 	free(backupstring);
 	backupstring = NULL;
 	backupstring = mallocstrcpy(backupstring, answer);
 
-	if (ISSET(CASE_SENSITIVE))
-	    UNSET(CASE_SENSITIVE);
-	else
-	    SET(CASE_SENSITIVE);
+	for (j = 0; j <= TOGGLE_LEN - 1; j++)
+	    if (i == toggles[j].val)
+		TOGGLE(toggles[j].flag);
 
 	return 1;
-    } else if (i == NANO_OTHERSEARCH_KEY) {
+    case NANO_OTHERSEARCH_KEY:
 	backupstring = mallocstrcpy(backupstring, answer);
 	return -2;		/* Call the opposite search function */
+/*
     } else if (i == NANO_REVERSESEARCH_KEY) {
 	free(backupstring);
 	backupstring = NULL;
 	backupstring = mallocstrcpy(backupstring, answer);
 
-	if (ISSET(REVERSE_SEARCH))
-	    UNSET(REVERSE_SEARCH);
-	else
-	    SET(REVERSE_SEARCH);
+	TOGGLE(REVERSE_SEARCH);
 
 	return 1;
     } else if (i == NANO_FROMSEARCHTOGOTO_KEY) {
+*/
+    case NANO_FROMSEARCHTOGOTO_KEY:
 	free(backupstring);
 	backupstring = NULL;
 	do_gotoline_void();
 	return -3;
-    } else {			/* First line key, etc. */
+    default:
 	do_early_abort();
 	free(backupstring);
 	backupstring = NULL;
diff --git a/winio.c b/winio.c
index 5c6726a..5c1c124 100644
--- a/winio.c
+++ b/winio.c
@@ -291,7 +291,7 @@
 	    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput, kbinput);
 #endif
 
-	    if (kbinput == s[j].val) {
+	    if (kbinput == s[j].val && kbinput < 32) {
 
 		/* We shouldn't discard the answer it gave, just because
 		   we hit a keystroke, GEEZ! */
@@ -327,10 +327,12 @@
 	    break;
 #endif
 #endif
+	case NANO_HOME_KEY:
 	case KEY_HOME:
 	    x = x_left;
 	    nanoget_repaint(buf, inputbuf, x);
 	    break;
+	case NANO_END_KEY:
 	case KEY_END:
 	    x = x_left + strlen(inputbuf);
 	    nanoget_repaint(buf, inputbuf, x);
@@ -450,11 +452,29 @@
 		    nodelay(edit, FALSE);
 		    break;
 		}
+	    default:
+
+		for (j = 0; j <= slen - 1; j++) {
+#ifdef DEBUG
+	    fprintf(stderr, _("Aha! \'%c\' (%d)\n"), kbinput, kbinput);
+#endif
+		    if (kbinput == s[j].val || kbinput == s[j].val - 32) {
+
+			/* We hit an Alt key.   Do like above.  We don't
+			   just ungetch the letter and let it get caught
+			   above cause that screws the keypad... */
+			answer = mallocstrcpy(answer, inputbuf);
+			free(inputbuf);
+			return s[j].val;
+		    }
+		}
+
 	    }
 	    nanoget_repaint(buf, inputbuf, x);
 	    break;
 
 	default:
+
 	    if (kbinput < 32)
 		break;
 
@@ -553,7 +573,7 @@
 {
     char description[80];
 
-    snprintf(description, 12, " %-10s", desc);
+    snprintf(description, 12 - (strlen(keystroke) - 2), " %-10s", desc);
     wattron(bottomwin, A_REVERSE);
     waddstr(bottomwin, keystroke);
     wattroff(bottomwin, A_REVERSE);
@@ -586,8 +606,14 @@
 
     clear_bottomwin();
     wmove(bottomwin, 1, 0);
-    for (i = 0; i <= slen - 1; i += 2) {
-	snprintf(keystr, 10, "^%c", s[i].val + 64);
+
+    for (i = 0; i <= slen - 2; i += 2) {
+
+	if (s[i].val < 97)
+	    snprintf(keystr, 10, "^%c", s[i].val + 64);
+	else
+	    snprintf(keystr, 10, "M-%c", s[i].val - 32);
+
 	onekey(keystr, s[i].desc);
 
 	for (j = 0; j < k; j++)
@@ -596,7 +622,12 @@
 
     wmove(bottomwin, 2, 0);
     for (i = 1; i <= slen - 1; i += 2) {
-	snprintf(keystr, 10, "^%c", s[i].val + 64);
+
+	if (s[i].val < 97)
+	    snprintf(keystr, 10, "^%c", s[i].val + 64);
+	else
+	    snprintf(keystr, 10, "M-%c", s[i].val - 32);
+
 	onekey(keystr, s[i].desc);
 
 	for (j = 0; j < k; j++)