- added yanking/pasting (support for readline ^Y)

Tue, 16 Dec 2014 00:52:39 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 16 Dec 2014 00:52:39 +0200
changeset 53
8f1a6f40d1b4
parent 52
ae5b486f6882
child 54
e20e306c886d

- added yanking/pasting (support for readline ^Y)

sources/interface.cpp file | annotate | diff | comparison | revisions
sources/mystring.h file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Tue Dec 16 00:17:15 2014 +0200
+++ b/sources/interface.cpp	Tue Dec 16 00:52:39 2014 +0200
@@ -63,6 +63,7 @@
 static IPAddress g_address;
 static String g_statusBarText;
 static StringList g_playerNames;
+static String g_pasteBuffer;
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -627,6 +628,23 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+static FUNCTION
+interface_yank_and_delete (int a, int b) -> void
+{
+	if (a >= b)
+		return;
+
+	if (g_cursor > a and g_cursor <= b)
+		g_cursor = a;
+
+	String& input = mutable_current_input();
+	g_pasteBuffer = input.mid (a, b);
+	input.remove (a, b - a);
+	g_needInputRender = true;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 FUNCTION
 Interface::handle_input() -> void
 {
@@ -763,32 +781,28 @@
 	case 'U' - 'A' + 1: // readline ^U - delete from start to cursor
 		if (g_cursor > 0)
 		{
-			mutable_current_input().remove (0, g_cursor);
+			interface_yank_and_delete (0, g_cursor);
 			g_cursor = 0;
-			g_needInputRender = true;
 		}
 		break;
 
 	case 'K' - 'A' + 1: // readline ^K - delete from cursor to end
-		if (g_cursor < current_input().length())
-		{
-			String& input = mutable_current_input();
-			input.remove (g_cursor, input.length() - g_cursor);
-			g_needInputRender = true;
-		}
+		interface_yank_and_delete (g_cursor, mutable_current_input().length());
 		break;
 
 	case 'W' - 'A' + 1: // readline ^W - delete from previous word bounary to current
-		{
-			int start = interface_find_previous_word();
+		interface_yank_and_delete (interface_find_previous_word(), g_cursor);
+		break;
 
-			if (start != g_cursor)
-			{
-				mutable_current_input().remove (start, g_cursor - start);
-				g_cursor = start;
-				g_needInputRender = true;
-			}
+	case 'Y' - 'A' + 1: // readline ^Y - paste previously deleted text
+		if (not g_pasteBuffer.is_empty())
+		{
+			mutable_current_input().insert (g_cursor, g_pasteBuffer);
+			g_cursor += g_pasteBuffer.length();
+			g_needInputRender = true;
 		}
+		else
+			print ("paste buffer is empty\n");
 		break;
 
 	case '\n':
@@ -865,8 +879,7 @@
 			case 'd':
 			case 'D':
 				// readline alt-d - delete from here till next word boundary
-				mutable_current_input().remove (g_cursor, interface_find_next_word() - g_cursor);
-				g_needInputRender = true;
+				interface_yank_and_delete (g_cursor, interface_find_next_word());
 				break;
 			}
 		}
--- a/sources/mystring.h	Tue Dec 16 00:17:15 2014 +0200
+++ b/sources/mystring.h	Tue Dec 16 00:52:39 2014 +0200
@@ -73,6 +73,7 @@
 	       METHOD to_lowercase() const -> String;
 	inline METHOD index_difference (int a, int b) -> int;
 	inline METHOD insert (int pos, char c) -> void;
+	inline METHOD insert (int pos, const char* c) -> void;
 	inline METHOD is_empty() const -> bool;
 	       METHOD is_numeric() const -> bool;
 	       METHOD find_last (const char* c, int a = -1) const -> int;
@@ -242,6 +243,14 @@
 }
 
 // -------------------------------------------------------------------------------------------------
+//
+inline METHOD
+String::insert (int pos, const char* c) -> void
+{
+	m_string.insert (pos, c);
+}
+
+// -------------------------------------------------------------------------------------------------
 
 inline METHOD
 String::length() const -> int

mercurial