From be1fc29d392d9d51a63ec6030e26ae6d69ca607d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Sun, 3 Apr 2016 07:50:19 +0800 Subject: [PATCH] Various basic editor commands --- botanjs/src/Components/Vim/Actions/BUFFERS.js | 78 +++++++++++++++++++ .../Components/Vim/Actions/EDITOR_COMMAND.js | 1 - botanjs/src/Components/Vim/Actions/QUIT.js | 60 ++++++++++++++ .../src/Components/Vim/Actions/REGISTERS.js | 64 +++++++++++++++ botanjs/src/Components/Vim/Actions/VERSION.js | 58 ++++++++++++++ botanjs/src/Components/Vim/Actions/WRITE.js | 49 ++++++++++++ botanjs/src/Components/Vim/LineFeeder.js | 1 - botanjs/src/Components/Vim/State/Recorder.js | 10 +++ botanjs/src/Components/Vim/VimArea.js | 75 ++++++++++++++---- botanjs/src/Components/Vim/_this.js | 5 +- .../externs/Components.Vim.State.Recorder.js | 5 ++ botanjs/src/externs/Components.Vim.VimArea.js | 10 ++- 12 files changed, 394 insertions(+), 22 deletions(-) create mode 100644 botanjs/src/Components/Vim/Actions/BUFFERS.js create mode 100644 botanjs/src/Components/Vim/Actions/QUIT.js create mode 100644 botanjs/src/Components/Vim/Actions/REGISTERS.js create mode 100644 botanjs/src/Components/Vim/Actions/VERSION.js create mode 100644 botanjs/src/Components/Vim/Actions/WRITE.js diff --git a/botanjs/src/Components/Vim/Actions/BUFFERS.js b/botanjs/src/Components/Vim/Actions/BUFFERS.js new file mode 100644 index 0000000..e02dc78 --- /dev/null +++ b/botanjs/src/Components/Vim/Actions/BUFFERS.js @@ -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 ); +})(); diff --git a/botanjs/src/Components/Vim/Actions/EDITOR_COMMAND.js b/botanjs/src/Components/Vim/Actions/EDITOR_COMMAND.js index 1b4450d..d4de6dd 100644 --- a/botanjs/src/Components/Vim/Actions/EDITOR_COMMAND.js +++ b/botanjs/src/Components/Vim/Actions/EDITOR_COMMAND.js @@ -66,7 +66,6 @@ case "buffers": case "ls": out[ CMD_TYPE ] = "BUFFERS"; - break; case "w": case "write": diff --git a/botanjs/src/Components/Vim/Actions/QUIT.js b/botanjs/src/Components/Vim/Actions/QUIT.js new file mode 100644 index 0000000..a32631d --- /dev/null +++ b/botanjs/src/Components/Vim/Actions/QUIT.js @@ -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 ); +})(); diff --git a/botanjs/src/Components/Vim/Actions/REGISTERS.js b/botanjs/src/Components/Vim/Actions/REGISTERS.js new file mode 100644 index 0000000..48f19d0 --- /dev/null +++ b/botanjs/src/Components/Vim/Actions/REGISTERS.js @@ -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 ); +})(); diff --git a/botanjs/src/Components/Vim/Actions/VERSION.js b/botanjs/src/Components/Vim/Actions/VERSION.js new file mode 100644 index 0000000..e87dde4 --- /dev/null +++ b/botanjs/src/Components/Vim/Actions/VERSION.js @@ -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 ); +})(); diff --git a/botanjs/src/Components/Vim/Actions/WRITE.js b/botanjs/src/Components/Vim/Actions/WRITE.js new file mode 100644 index 0000000..e961735 --- /dev/null +++ b/botanjs/src/Components/Vim/Actions/WRITE.js @@ -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 ); +})(); diff --git a/botanjs/src/Components/Vim/LineFeeder.js b/botanjs/src/Components/Vim/LineFeeder.js index ee9f881..07a2bec 100644 --- a/botanjs/src/Components/Vim/LineFeeder.js +++ b/botanjs/src/Components/Vim/LineFeeder.js @@ -245,7 +245,6 @@ } } - return pos; } ); diff --git a/botanjs/src/Components/Vim/State/Recorder.js b/botanjs/src/Components/Vim/State/Recorder.js index 8e095f4..b95a775 100644 --- a/botanjs/src/Components/Vim/State/Recorder.js +++ b/botanjs/src/Components/Vim/State/Recorder.js @@ -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 ); })(); diff --git a/botanjs/src/Components/Vim/VimArea.js b/botanjs/src/Components/Vim/VimArea.js index f00d1d2..3614809 100644 --- a/botanjs/src/Components/Vim/VimArea.js +++ b/botanjs/src/Components/Vim/VimArea.js @@ -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 ); diff --git a/botanjs/src/Components/Vim/_this.js b/botanjs/src/Components/Vim/_this.js index f97d14a..d55ec63 100644 --- a/botanjs/src/Components/Vim/_this.js +++ b/botanjs/src/Components/Vim/_this.js @@ -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" diff --git a/botanjs/src/externs/Components.Vim.State.Recorder.js b/botanjs/src/externs/Components.Vim.State.Recorder.js index b39cbf9..543b3d8 100644 --- a/botanjs/src/externs/Components.Vim.State.Recorder.js +++ b/botanjs/src/externs/Components.Vim.State.Recorder.js @@ -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; diff --git a/botanjs/src/externs/Components.Vim.VimArea.js b/botanjs/src/externs/Components.Vim.VimArea.js index 51c9825..6d40d52 100644 --- a/botanjs/src/externs/Components.Vim.VimArea.js +++ b/botanjs/src/externs/Components.Vim.VimArea.js @@ -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;