- added readline alt-b and alt-f support (skip past words)

Mon, 15 Dec 2014 21:28:24 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 15 Dec 2014 21:28:24 +0200
changeset 43
0bc07f54f522
parent 42
9699687081df
child 44
693d7751fff0

- added readline alt-b and alt-f support (skip past words)

sources/interface.cpp file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Mon Dec 15 21:00:57 2014 +0200
+++ b/sources/interface.cpp	Mon Dec 15 21:28:24 2014 +0200
@@ -584,6 +584,44 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+static FUNCTION
+interface_find_previous_word() -> int
+{
+	const String& input = current_input();
+	int pos = g_cursor;
+
+	// Move past whitespace
+	while (pos > 0 and isspace (input[pos - 1]))
+		pos--;
+
+	// Move past the word
+	while (pos > 0 and not isspace (input[pos - 1]))
+		pos--;
+
+	return pos;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+static FUNCTION
+interface_find_next_word() -> int
+{
+	const String& input = current_input();
+	int pos = g_cursor;
+
+	// Move past current whitespace
+	while (pos < input.length() and isspace (input[pos]))
+		pos++;
+
+	// Move past the word
+	while (input[pos] != '\0' and not isspace (input[pos]))
+		pos++;
+
+	return pos;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 FUNCTION
 Interface::handle_input() -> void
 {
@@ -649,13 +687,6 @@
 		}
 		break;
 
-	case '\e':
-		if (g_inputState == INPUTSTATE_PASSWORD)
-			set_input_state (INPUTSTATE_ADDRESS);
-		else if (g_inputState == INPUTSTATE_ADDRESS)
-			set_input_state (INPUTSTATE_NORMAL);
-		break;
-
 	case KEY_LEFT:
 	case 'B' - 'A' + 1: // readline ^B
 		if (g_cursor > 0)
@@ -790,6 +821,39 @@
 		if (g_inputState == INPUTSTATE_NORMAL)
 			safe_disconnect ([]() {set_input_state (INPUTSTATE_ADDRESS);});
 		break;
+
+	case '\e': // Escape
+		// We may have an alt key coming
+		ch = ::getch();
+
+		if (ch != ERR)
+		{
+			switch (ch)
+			{
+			case 'b':
+			case 'B':
+				// readline alt-b - move one word to the left
+				g_cursor = interface_find_previous_word();
+				g_needInputRender = true;
+				break;
+
+			case 'f':
+			case 'F':
+				// readline alt-f - move one word to the right
+				g_cursor = interface_find_next_word();
+				g_needInputRender = true;
+				break;
+			}
+		}
+		else
+		{
+			// No alt-key, handle pure escape
+			if (g_inputState == INPUTSTATE_PASSWORD)
+				set_input_state (INPUTSTATE_ADDRESS);
+			else if (g_inputState == INPUTSTATE_ADDRESS)
+				set_input_state (INPUTSTATE_NORMAL);
+		}
+		break;
 	}
 }
 

mercurial