Various basic editor commands

This commit is contained in:
斟酌 鵬兄 2016-04-03 07:50:19 +08:00
parent f4dc0e7d9c
commit be1fc29d39
12 changed files with 394 additions and 22 deletions

View File

@ -0,0 +1,78 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
/** @type {Dandelion} */
var Dand = __import( "Dandelion" );
var Mesg = __import( "Components.Vim.Message" );
var occurance = __import( "System.utils.Perf.CountSubstr" );
var shadowImport = __import;
var ESCAPE = function( reg )
{
var str = reg.toString();
return str.replace( "\t", "^I" ).replace( "\n", "^J" );
};
/** @type {Components.Vim.IAction} */
var BUFFERS = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
BUFFERS.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
BUFFERS.prototype.handler = function( e, p )
{
e.preventDefault();
var areas = Dand.tag( "textarea" );
var cur = this.__cursor;
var Vim = cur.Vim;
var Insts = VimArea.Instances;
var msg = ":buffers";
for( var i in Insts )
{
/** @type {Components.Vim.VimArea} */
var inst = Insts[ i ];
var b = inst.index + " ";
var icur = inst.contentFeeder.cursor;
b += ( inst == Vim ? "%a" : " " ) + " ";
b += ( icur.rec.changed ? "+" : " " ) + " ";
b += "\"" + inst.stage.element.id + "\"" + " line " + ( icur.getLine().lineNum + 1 );
msg += "\n " + b;
}
var lastLine = Mesg( "WAIT_FOR_INPUT" );
var l = this.__cursor.feeder.firstBuffer.cols;
for( var i = msg.length; i < l; i ++ ) msg += " ";
this.__msg = msg + "\n" + lastLine;
};
BUFFERS.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "BUFFERS", BUFFERS );
})();

View File

@ -66,7 +66,6 @@
case "buffers":
case "ls":
out[ CMD_TYPE ] = "BUFFERS";
break;
case "w":
case "write":

View File

@ -0,0 +1,60 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var VimError = __import( "Components.Vim.Error" );
var occurance = __import( "System.utils.Perf.CountSubstr" );
var ESCAPE = function( reg )
{
var str = reg.toString();
return str.replace( "\t", "^I" ).replace( "\n", "^J" );
};
/** @type {Components.Vim.IAction} */
var QUIT = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
QUIT.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
QUIT.prototype.handler = function( e, p )
{
e.preventDefault();
var cur = this.__cursor;
var Vim = cur.Vim;
if( cur.rec.changed )
{
var msg = VimError( "E37" );
var l = this.__cursor.feeder.firstBuffer.cols;
for( var i = msg.length; i < l; i ++ ) msg += " ";
this.__msg = msg;
}
else
{
Vim.dispose();
return true;
}
};
QUIT.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "QUIT", QUIT );
})();

View File

@ -0,0 +1,64 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var VimError = __import( "Components.Vim.Error" );
var Mesg = __import( "Components.Vim.Message" );
var ESCAPE = function( reg )
{
var str = reg.toString();
return str.replace( "\t", "^I" ).replace( "\n", "^J" );
};
/** @type {Components.Vim.IAction} */
var REGISTERS = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
REGISTERS.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
REGISTERS.prototype.handler = function( e, p )
{
e.preventDefault();
/** @type {Components.Vim.State.Registers} */
var reg = e.target.registers;
var msg = ":register";
msg += "\n" + Mesg( "REGISTERS" );
var regs = "\"0123456789-.:%/=";
for( var i = 0, j = regs[ i ]; j != undefined; i ++, j = regs[ i ] )
{
var r = reg.get( j );
if( r )
{
msg += "\n\"" + j + " " + ESCAPE( r );
}
}
var lastLine = Mesg( "WAIT_FOR_INPUT" );
var l = this.__cursor.feeder.firstBuffer.cols;
for( var i = msg.length; i < l; i ++ ) msg += " ";
this.__msg = msg + "\n" + lastLine;
};
REGISTERS.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "REGISTERS", REGISTERS );
})();

View File

@ -0,0 +1,58 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var VimError = __import( "Components.Vim.Error" );
var Mesg = __import( "Components.Vim.Message" );
var ESCAPE = function( reg )
{
var str = reg.toString();
return str.replace( "\t", "^I" ).replace( "\n", "^J" );
};
/** @type {Components.Vim.IAction} */
var VERSION = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
VERSION.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
VERSION.prototype.handler = function( e, p )
{
e.preventDefault();
/** @type {Components.Vim.State.Registers} */
var reg = e.target.registers;
var msg = ":version";
msg += "\nVim;Re - Vim; Reverse Engineered for textarea v" + VIMRE_VERSION;
msg += "\n + BotanJS - v" + BOTANJS_VERSION;
msg += "\nProject home - https://github.com/tgckpg/BotanJS-vim";
msg += "\n by \u659F\u914C\u9D6C\u5144 (penguin) - https://blog.astropenguin.net/";
msg += "\n";
var lastLine = Mesg( "WAIT_FOR_INPUT" );
var l = this.__cursor.feeder.firstBuffer.cols;
for( var i = msg.length; i < l; i ++ ) msg += " ";
this.__msg = msg + "\n" + lastLine;
};
VERSION.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "VERSION", VERSION );
})();

View File

@ -0,0 +1,49 @@
(function(){
var ns = __namespace( "Components.Vim.Actions" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var Mesg = __import( "Components.Vim.Message" );
var occurance = __import( "System.utils.Perf.CountSubstr" );
/** @type {Components.Vim.IAction} */
var WRITE = function( Cursor )
{
/** @type {Components.Vim.Cursor} */
this.__cursor = Cursor;
this.__msg = "";
Cursor.suppressEvent();
};
WRITE.prototype.dispose = function()
{
this.__cursor.unsuppressEvent();
};
WRITE.prototype.handler = function( e, p )
{
e.preventDefault();
var cur = this.__cursor;
var Vim = cur.Vim;
Vim.content = cur.feeder.content.slice( 0, -1 );
var msg = Mesg( "WRITE", Vim.stage.element.id, occurance( Vim.content, "\n" ), Vim.content.length );
cur.rec.save();
var l = this.__cursor.feeder.firstBuffer.cols;
for( var i = msg.length; i < l; i ++ ) msg += " ";
this.__msg = msg;
};
WRITE.prototype.getMessage = function()
{
return this.__msg;
};
ns[ NS_EXPORT ]( EX_CLASS, "WRITE", WRITE );
})();

View File

@ -245,7 +245,6 @@
}
}
return pos;
} );

View File

@ -7,6 +7,7 @@
this.__stacks = [];
this.__i = 0;
this.__j = 0;
this.__saved = 0;
};
Recorder.prototype.undo = function()
@ -31,6 +32,11 @@
return null;
};
Recorder.prototype.save = function()
{
this.__saved = this.__i;
};
Recorder.prototype.record = function( StateObj )
{
this.__steps[ this.__i ++ ] = StateObj;
@ -41,5 +47,9 @@
StateObj.id = this.__j;
};
__readOnly( Recorder.prototype, "changed", function() {
return this.__saved != this.__i;
} );
ns[ NS_EXPORT ]( EX_CLASS, "Recorder", Recorder );
})();

View File

@ -2,18 +2,20 @@
var ns = __namespace( "Components.Vim" );
/** @type {Dandelion.IDOMElement} */
var IDOMElement = __import( "Dandelion.IDOMElement" );
var IDOMElement = __import( "Dandelion.IDOMElement" );
/** @type {System.utils.DataKey} */
var DataKey = __import( "System.utils.DataKey" );
var DataKey = __import( "System.utils.DataKey" );
/** @type {System.utils.EventKey} */
var EventKey = __import( "System.utils.EventKey" );
/** @type {System.Cycle} */
var Cycle = __import( "System.Cycle" );
var Cycle = __import( "System.Cycle" );
/** @type {System.Debug} */
var debug = __import( "System.Debug" );
var debug = __import( "System.Debug" );
/** @type {Components.Vim.State.Registers} */
var Registers = __import( "Components.Vim.State.Registers" );
var Registers = __import( "Components.Vim.State.Registers" );
/** @type {Components.Vim.Syntax.Analyzer} */
var SyntaxAnalyzer = __import( "Components.Vim.Syntax.Analyzer" );
var SyntaxAnalyzer = __import( "Components.Vim.Syntax.Analyzer" );
/** @type {Components.Vim.LineFeeder} */
var LineFeeder = ns[ NS_INVOKE ]( "LineFeeder" );
@ -25,6 +27,7 @@
var mesg = ns[ NS_INVOKE ]( "Message" );
var Insts = [];
var InstIndex = 0;
var KeyHandler = function( sender, handler )
{
@ -61,14 +64,21 @@
var _self = this;
stage.addEventListener( "Focus", function() { _self.__active = true; } );
stage.addEventListener( "Blur", function() { _self.__active = false; } );
this.__stagedEvents = [
new EventKey( "Focus", function() { _self.__active = true; } )
, new EventKey( "Blur", function() { _self.__active = false; } )
];
this.__removeText
// Init
this.VisualizeVimFrame( element.value );
// Set buffer index
this.__instIndex = InstIndex ++;
// Push this instance
Insts.push( this );
Insts[ this.__instIndex ] = this;
};
VimArea.prototype.select = function( sel )
@ -86,6 +96,7 @@
VimArea.prototype.VisualizeVimFrame = function( content )
{
var _self = this;
this.content = content;
var element = this.stage.element;
var r = this.rows;
@ -135,6 +146,8 @@
cfeeder.dispatcher.addEventListener( "VisualUpdate", Update );
Update();
this.__visualUpdate = Update;
this.contentFeeder = cfeeder;
this.contentAnalyzer = contentAnalyzer;
this.statusFeeder = sfeeder;
@ -154,18 +167,48 @@
}, 600 );
var controls = new VimControls( this );
this.stage.addEventListener(
"KeyDown"
, KeyHandler( this, controls.handler.bind( controls ) )
this.__stagedEvents.push(
new EventKey( "KeyDown", KeyHandler( this, controls.handler.bind( controls ) ) )
);
this.stage.addEventListeners( this.__stagedEvents );
};
__readOnly( VimArea, "Instances", function() {
return Insts.slice();
VimArea.prototype.dispose = function()
{
var stage = this.stage;
var evts = this.__stagedEvents;
var feeder = this.contentFeeder;
var id = stage.element.id;
debug.Info( "Destroy instance: " + id );
feeder.dispatcher.removeEventListener( "VisualUpdate", this.__visualUpdate );
stage.removeAttribute( "data-vimarea" );
Cycle.permaRemove( "VimCursorBlinkCycle" + id );
for( var i in evts )
{
stage.removeEventListener( evts[ i ] );
}
stage.element.value = this.content;
delete Insts[ this.__instIndex ];
};
__readOnly( VimArea.prototype, "index", function()
{
return this.__instIndex + 1;
} );
__readOnly( VimArea.prototype, "content", function() {
return this.contentFeeder.content.slice( 0, -1 );
__readOnly( VimArea, "Instances", function() {
var clone = [];
for( var i in Insts ) clone.push( Insts[ i ] );
return clone;
} );
ns[ NS_EXPORT ]( EX_CLASS, "VimArea", VimArea );

View File

@ -1,3 +1,4 @@
VIMRE_VERSION = "1.0.0b";
(function(){
var ns = __namespace( "Components.Vim" );
@ -7,8 +8,9 @@
, "MORE": "-- MORE --"
, "VISUAL": "-- VISUAL --"
, "VISLINE": "-- VISUAL LINE --"
, "REGISTERS": "--- Registers ---"
, "WRITE": "\"%1\" %2L, %3C written"
, "CONTINUE": "Press ENTER or type command to continue"
, "WAIT_FOR_INPUT": "Press ENTER or type command to continue"
, "SEARCH_HIT_BOTTOM": "Seach hit BOTTOM, contining at TOP"
, "TOP": "Top"
, "BOTTOM": "Bot"
@ -29,6 +31,7 @@
var errors = {
"E35": "No previous regular expression"
, "E37": "No write since last change (add ! to override)"
, "E481": "No range allowed"
, "E492": "Not an editor command: %1"
, "E486": "Pattern not found: %1"

View File

@ -7,3 +7,8 @@ Components.Vim.State.Recorder.undo;
Components.Vim.State.Recorder.redo;
/** @type Function */
Components.Vim.State.Recorder.record;
/** @type Function */
Components.Vim.State.Recorder.save;
/** @type Boolean */
Components.Vim.State.Recorder.changed;

View File

@ -10,9 +10,13 @@ Components.Vim.VimArea.statusFeeder;
/** @type {Components.Vim.StatusBar} */
Components.Vim.VimArea.statusBar;
/** @type {Number} */
/** @type Number */
Components.Vim.VimArea.index;
/** @type Number */
Components.Vim.VimArea.rows;
/** @type {Number} */
/** @type Number */
Components.Vim.VimArea.cols;
/** @type {Array} */
/** @type Array */
Components.Vim.VimArea.Instances;
/** @type Function */
Components.Vim.VimArea.dispose;