penguin/AstroJS

Javascript framework for my blog

commit c24f74f70c406205c57e71fc949d518863192fcd

author斟酌 鵬兄 <tgckpg@gmail.com>
date2016-03-31T10:52:20Z
subjectDelete (Tested): dd diw d{bracket} dG dgg
commit c24f74f70c406205c57e71fc949d518863192fcd
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2016-03-31T10:52:20Z

    Delete (Tested): dd diw d{bracket} dG dgg
---
 botanjs/src/Components/Vim/Actions/DELETE.js | 99 +++++++++++++++++++++++++---
 botanjs/src/Components/Vim/Cursor.js         |  9 ++-
 2 files changed, 98 insertions(+), 10 deletions(-)

diff --git a/botanjs/src/Components/Vim/Actions/DELETE.js b/botanjs/src/Components/Vim/Actions/DELETE.js
index b509fb5..f5af2e0 100644
--- a/botanjs/src/Components/Vim/Actions/DELETE.js
+++ b/botanjs/src/Components/Vim/Actions/DELETE.js
@@ -20,6 +20,7 @@
 		this.__cursor = Cursor;
 		this.__nline = 0;
 		this.__startX = Cursor.aPos;
+		this.__panY = this.__cursor.feeder.panY;
 	};
 
 	DELETE.prototype.allowMovement = true;
@@ -33,6 +34,7 @@
 	{
 		e.preventDefault();
 
+		if( e.ModKeys ) return;
 
 		/** @type {Components.Vim.State.Registers} */
 		var reg = e.target.registers;
@@ -44,18 +46,90 @@
 
 		if( sp == undefined )
 		{
-			if( this.__startX != cur.aPos )
-			{
-				Triggered = true;
+			Triggered = true;
+
+			sp = this.__startX;
 
-				if( e.kMap( "l" ) )
+			cur.suppressEvent();
+
+			var currAp = cur.aPos;
+			if( this.__startX != currAp )
+			{
+				// Remove to start
+				if( e.kMap( "^" ) )
+				{
+					sp --;
+				}
+				// Remove char in cursor
+				else if( e.kMap( "l" ) )
 				{
 					cur.moveX( -1 );
 				}
-
-				sp = this.__startX;
+				// Remove char before cursor
+				else if( e.kMap( "h" ) )
+				{
+					sp = currAp;
+				}
+				// Remove the current and the following line
+				else if( e.kMap( "j" ) )
+				{
+					cur.lineEnd( true );
+					sp = cur.aPos;
+					cur.moveY( -1 );
+					cur.lineStart();
+					this.__startX = cur.aPos;
+				}
+				// Remove the current and the preceding line
+				else if( e.kMap( "k" ) )
+				{
+					cur.moveY( 1 );
+					cur.lineEnd( true );
+					sp = cur.aPos;
+					cur.moveY( -1 );
+					cur.lineStart();
+				}
+				else if( this.__startX < currAp )
+				{
+					// Swap the movement
+					// This is to move the REDO / UNDO Cursor
+					// position to the earlier position
+					sp = currAp;
+					cur.moveTo( this.__startX );
+				}
 			}
-			else return;
+			// Remove the current line
+			else
+			{
+				if( e.kMap( "d" ) )
+				{
+					cur.lineEnd( true );
+					sp = cur.aPos;
+					cur.lineStart();
+				}
+				else if( e.range )
+				{
+					sp = e.range.close;
+					cur.moveTo( e.range.open, true );
+				}
+				else if( e.kMap( "^" ) )
+				{
+					// Do nothing as nothing can be removed
+					// since there is no successful movement
+					return true;
+				}
+				// this is the same as kMap( "h" ) above
+				else if( e.kMap( "$" ) )
+				{
+					sp = cur.aPos;
+				}
+				else
+				{
+					cur.unsuppressEvent();
+					return false;
+				}
+			}
+
+			cur.unsuppressEvent();
 		}
 
 		var c = feeder.content;
@@ -76,6 +150,15 @@
 
 		feeder.content = c.substring( 0, s ) + c.substring( e + 1 );
 
+		// Try to keep the original panning if possible
+		cur.feeder.pan( undefined
+			, this.__panY < cur.feeder.panY
+				? this.__panY - cur.feeder.panY
+				: undefined
+		);
+
+		cur.moveTo( s );
+
 		var stator = new Stator( cur, s );
 		var stack = new Stack();
 
@@ -96,8 +179,6 @@
 
 		cur.rec.record( stack );
 
-		feeder.pan();
-
 		return Triggered;
 	};
 
diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js
index ce66bd8..3f884cc 100644
--- a/botanjs/src/Components/Vim/Cursor.js
+++ b/botanjs/src/Components/Vim/Cursor.js
@@ -92,7 +92,14 @@
 	Cursor.prototype.moveTo = function( aPos, phantomSpace )
 	{
 		var content = this.feeder.content;
-		var lastLineNum = this.getLine().lineNum;
+		var pline = this.getLine();
+		var lastLineNum = pline.lineNum;
+
+		if( pline.placeholder )
+		{
+			lastLineNum = 0;
+			this.Y = 0;
+		}
 
 		var expLineNum = 0;
 		var lineStart = 0;