FIND command

This commit is contained in:
斟酌 鵬兄 2016-04-02 20:00:23 +08:00
parent 3ed7d2be84
commit 062e9d8bb9
5 changed files with 151 additions and 5 deletions

View File

@ -0,0 +1,127 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var Mesg = __import( "Components.Vim.Message" );
// Private static
var PATTERN = "";
var ParsePattern = function( pattern )
{
var parsed = "";
var l = pattern.length;
for( var i = 1; i < l; i ++ )
{
switch( pattern[ i ] )
{
case "^I":
parsed += "\t";
break;
case "\\":
var tok = pattern[ ++ i ];
debug.Error( "Unknown escaped token: " + tok );
++ i;
default:
parsed += pattern[ i ];
}
}
var RegEx = null;
try
{
var RegEx = new RegExp( parsed, "gm" );
}
catch( ex )
{
debug.Error( ex );
}
return RegEx;
};
/** @type {Components.Vim.Cursor.IAction} */
var FIND = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
FIND.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
FIND.prototype.handler = function( e, p )
{
e.preventDefault();
if( p ) PATTERN = p;
var search = ParsePattern( PATTERN );
var content = this.__cursor.feeder.content;
var cur = this.__cursor;
var p = cur.aPos;
var r;
var Hit;
var FirstHit;
var PrevStack = [];
while( ( r = search.exec( content ) ) !== null )
{
if( !FirstHit ) FirstHit = r.index;
if( p < r.index )
{
Hit = r.index;
break;
}
PrevStack.push( r.index );
}
if( e.kMap( "N" ) )
{
Hit = PrevStack[ PrevStack.length - 2 ];
if( Hit == undefined )
{
this.__msg = Mesg( "SEARCH_HIT_TOP" );
while( ( r = search.exec( content ) ) !== null ) Hit = r.index;
}
}
else if( FirstHit != undefined && Hit == undefined )
{
// Search Hit Bottom
Hit = FirstHit;
this.__msg = Mesg( "SEARCH_HIT_BOTTOM" );
}
else
{
this.__msg = PATTERN.join( "" )
}
if( Hit == undefined )
{
}
else
{
cur.moveTo( Hit );
}
};
FIND.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "FIND", FIND );
})();

View File

@ -1,8 +1,6 @@
(function(){ (function(){
var ns = __namespace( "Components.Vim.Actions" ); var ns = __namespace( "Components.Vim.Actions" );
/** @type {Components.Vim.State.Stack} */
var Stack = __import( "Components.Vim.State.Stack" );
/** @type {System.Debug} */ /** @type {System.Debug} */
var debug = __import( "System.Debug" ); var debug = __import( "System.Debug" );

View File

@ -441,6 +441,11 @@
}, _8 ); }, _8 );
break; break;
case SHIFT + N: // Next Search
case N: // Next Search
ccur.openRunAction( "FIND", e );
break;
case SLASH: // "/" Search movement case SLASH: // "/" Search movement
this.__cMovement = true; this.__cMovement = true;

View File

@ -8,7 +8,7 @@
/** @type {System.utils.Perf} */ /** @type {System.utils.Perf} */
var Perf = __import( "System.utils.Perf" ); var Perf = __import( "System.utils.Perf" );
/**j@type {Components.Vim.State.History} */ /** @type {Components.Vim.State.History} */
var History = __import( "Components.Vim.State.History" ); var History = __import( "Components.Vim.State.History" );
var Mesg = __import( "Components.Vim.Message" ); var Mesg = __import( "Components.Vim.Message" );
var beep = __import( "Components.Vim.Beep" ); var beep = __import( "Components.Vim.Beep" );
@ -134,7 +134,7 @@
} }
else if( e.kMap( "Enter" ) ) else if( e.kMap( "Enter" ) )
{ {
this.__process(); this.__process( e );
return true; return true;
} }
else if( e.kMap( "Left" ) ) else if( e.kMap( "Left" ) )
@ -206,9 +206,22 @@
feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) ); feeder.dispatcher.dispatchEvent( new BotanEvent( "VisualUpdate" ) );
}; };
Command.prototype.__process = function() Command.prototype.__process = function( e )
{ {
this.__hist.push( this.__command ); this.__hist.push( this.__command );
var action = "";
switch( this.__mode )
{
case "/":
action = "FIND";
break;
}
var cur = this.__cursor;
cur.suppressEvent();
this.__cursor.openRunAction( action, e, this.__command.slice() );
cur.unsuppressEvent();
}; };
ns[ NS_EXPORT ]( EX_CLASS, "Command", Command ); ns[ NS_EXPORT ]( EX_CLASS, "Command", Command );

View File

@ -21,6 +21,9 @@
, "LINES_FEWER": "%1 fewer line(s)" , "LINES_FEWER": "%1 fewer line(s)"
, "LINES_MORE": "%1 more line(s)" , "LINES_MORE": "%1 more line(s)"
, "LINES_YANKED": "%1 line(s) yanked" , "LINES_YANKED": "%1 line(s) yanked"
, "SEARCH_HIT_BOTTOM": "search hit BOTTOM, continuing at TOP"
, "SEARCH_HIT_TOP": "search hit TOP, continuing at BOTTOM"
}; };
var errors = { var errors = {