penguin/AstroJS

Javascript framework for my blog

commit 4529f4b4a1c3292ccc9eb05f81d2c6576a6475ee

author斟酌 鵬兄 <tgckpg@gmail.com>
date2016-04-03T11:46:35Z
subjectAdded f, t movements
commit 4529f4b4a1c3292ccc9eb05f81d2c6576a6475ee
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2016-04-03T11:46:35Z

    Added f, t movements
---
 botanjs/src/Components/Vim/Actions/TO.js | 82 ++++++++++++++++++++++++++++++++
 botanjs/src/Components/Vim/Controls.js   | 35 +++++++++++++-
 botanjs/src/Components/Vim/Cursor.js     |  1 -
 3 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/botanjs/src/Components/Vim/Actions/TO.js b/botanjs/src/Components/Vim/Actions/TO.js
new file mode 100644
index 0000000..b1a0dbc
--- /dev/null
+++ b/botanjs/src/Components/Vim/Actions/TO.js
@@ -0,0 +1,82 @@
+(function(){
+	var ns = __namespace( "Components.Vim.Actions" );
+
+	/** @type {System.Debug} */
+	var debug                                 = __import( "System.Debug" );
+
+	var beep = __import( "Components.Vim.Beep" );
+
+	/** @type {Components.Vim.IAction} */
+	var TO = function( Cursor )
+	{
+		/** @type {Components.Vim.Cursor} */
+		this.__cursor = Cursor;
+		this.__msg = "<TO COMMAND>";
+		Cursor.suppressEvent();
+	};
+
+	TO.prototype.dispose = function()
+	{
+		this.__cursor.unsuppressEvent();
+	};
+
+	TO.prototype.handler = function( em, et )
+	{
+		et.preventDefault();
+
+		var cur = this.__cursor;
+		var f = cur.feeder;
+		var n = cur.getLine().lineNum;
+
+		var p = f.content.indexOf( "\n" );
+		for( i = 1; p != -1 && i < n; i ++ )
+		{
+			p = f.content.indexOf( "\n", p + 1 );
+		}
+
+		var upperLimit = f.content.indexOf( "\n", p + 1 );
+
+		if( 0 < n ) p ++;
+
+		var lowerLimmit = p;
+
+		var cX = cur.X;
+		var tX = cX;
+
+		var Char = et.key;
+		if( et.kMap( "Tab" ) )
+		{
+			Char = "\t";
+		}
+
+		if( 1 < Char.length )
+		{
+			beep();
+			return;
+		}
+
+		// Forward
+		if( em.kMap( "t" ) || em.kMap( "f" ) )
+		{
+			tX = f.content.indexOf( Char, p + cX + 1 );
+		}
+		// backward
+		else
+		{
+			tX = f.content.lastIndexOf( Char, p + cX - 1 );
+		}
+
+		if( lowerLimmit <= tX && tX < upperLimit )
+		{
+			cur.moveX( tX - lowerLimmit - cX );
+		}
+		else beep();
+	};
+
+	TO.prototype.getMessage = function()
+	{
+		return this.__msg;
+	};
+
+	ns[ NS_EXPORT ]( EX_CLASS, "TO", TO );
+})();
diff --git a/botanjs/src/Components/Vim/Controls.js b/botanjs/src/Components/Vim/Controls.js
index 2ee60c4..6b9efe6 100644
--- a/botanjs/src/Components/Vim/Controls.js
+++ b/botanjs/src/Components/Vim/Controls.js
@@ -47,6 +47,8 @@
 	var COMMA = 188; var FULLSTOP = 190;
 	var SLASH = 191; var BACK_SLASH = 220;
 
+	var ANY_KEY = -1;
+
 	var __maps = {};
 	var Map = function( str )
 	{
@@ -169,8 +171,9 @@
 		{
 			var compReg = this.__compositeReg[i];
 			var keys = compReg.keys;
+			var key = keys[ compReg.i ++ ];
 
-			if( keys[ compReg.i ++ ] == kCode )
+			if( key == ANY_KEY || key == kCode )
 			{
 				if( compReg.i == keys.length )
 				{
@@ -405,8 +408,38 @@
 				);
 
 				break;
+
+
+			case SHIFT + T: // To
 			case T: // To
+				this.__cMovement = true;
+
+				this.__composite( e, function( e2 ) {
+					var oX = ccur.X;
+					ccur.openRunAction( "TO", e, e2 );
+
+					if( ccur.X < oX )
+					{
+						ccur.moveX( 1 );
+					}
+					else if( oX < ccur.X )
+					{
+						ccur.moveX( -1 );
+					}
+				}, ANY_KEY );
+
 				break;
+			case SHIFT + F: // To
+			case F: // To
+				this.__cMovement = true;
+
+				this.__composite( e, function( e2 ) {
+					ccur.openRunAction( "TO", e, e2 );
+				}, ANY_KEY );
+
+				break;
+
+
 			case I: // In between boundary
 				if( !ccur.action )
 				{
diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js
index 2fd9833..3eaaac0 100644
--- a/botanjs/src/Components/Vim/Cursor.js
+++ b/botanjs/src/Components/Vim/Cursor.js
@@ -137,7 +137,6 @@
 
 		this.moveX( - Number.MAX_VALUE );
 		this.moveX( jumpX, false, phantomSpace );
-
 	};
 
 	// 0 will be treated as default ( 1 )