From dd227d28636335042610067c8b33ad07cbfea3af Mon Sep 17 00:00:00 2001 From: boudin Date: Mon, 17 Nov 2014 00:46:41 +0100 Subject: [PATCH] Key callback helper --- lib/buffer.js | 16 +++++++++++++--- lib/controler.js | 34 ++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 478d9c9..69aea77 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -13,6 +13,7 @@ function Buffer (client, buffId, path) { this.title = undefined; var typeNum = 0; //WARNING : check if this should be unique to a buffer var serNum = 0; //WARNING : check if this should be unique to a buffer + var keysCallback = {}; /* COMMANDS */ @@ -182,12 +183,21 @@ function Buffer (client, buffId, path) { client.callFunction("remove", buffId, Types.position(off), length, callback); }; - var keysCallback = {}; - + //Register a key callback that will only be called on this buffer this.registerKey = function (key, callback) { - client.specialKeys(key); + client.sendCommand("specialKeys", 0, Types.string(key)); Object.defineProperty(keysCallback, key, { value: callback, configurable: true}); }; + + this.handleKey = function (key, line, col) { + if (keysCallback.hasOwnProperty(arguments[2])){ + keysCallback[arguments[2]].call(null, line, col); + return true; + } + else { + return false; + } + }; } util.inherits(Buffer, EventEmitter); diff --git a/lib/controler.js b/lib/controler.js index 549b9dd..07fa433 100644 --- a/lib/controler.js +++ b/lib/controler.js @@ -3,7 +3,7 @@ var EventEmitter = require("events").EventEmitter; var util = require("util"); var Types = require("./types.js"); var Buffer = require("./buffer.js"); -var event = require("./event.js"); +var events = require("./event.js"); //A stream Transform implementation returning full netbeans messages function MessageTransform(opts) { @@ -46,7 +46,6 @@ util.inherits(MessageTransform, Transform); function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) { "use strict"; - var self = this; var callbacks = {}; var currentSeqno = 0; var connected = false; @@ -266,31 +265,32 @@ function Controler(socket, authentication) { var client = new NetbeansClient(socket, authentication, function(buffId, name) { switch (name) { - case event.disconnect : + case events.disconnect : //Connexion closed by Vim socket.destroy(); self.emit("disconnected"); break; - case event.killed : + case events.killed : //Remove the buffer if it as been killed if (buffers.hasOwnProperty(buffId)) { buffers[buffId].emit.apply(buffers[buffId], Array.prototype.slice.call(arguments, 1)); delete buffers[buffId]; } break; - case event.keyAtPos : + case events.keyAtPos : //Call the key registered callback if it exists. - var buffer = getBuffer(buffId); + var buffer = self.getBuffer(buffId); - if (keysCallback.indexOf(arguments[2]) >= 0) { - keysCallback.call(null, buffer, arguments[3].split("/")[0], arguments[3].split("/")[1]); - } - else if (buffer === undefined) { - + var pos = arguments[3].split("/"); + + if ((!buffer || !buffer.handleKey(arguments, pos[0], pos[1])) + && keysCallback.hasOwnProperty(arguments[2])){ + + keysCallback[arguments[2]].call(null, buffer, pos[0], pos[1]); } break; - case event.fileOpened : + case events.fileOpened : //File opened. Check if it's already managed by controler. if (self.getBuffer(arguments[2]) === undefined) { unmanagedFiles.push(arguments[2]); @@ -319,6 +319,7 @@ function Controler(socket, authentication) { var buffId = 0; var buffers = {}; + //Internal function to create a new buffer function newBuffer(pathname) { if (pathname !== undefined) { var indexFile = unmanagedFiles.indexOf(pathname); @@ -347,6 +348,7 @@ function Controler(socket, authentication) { client.sendCommand("raise", 0); }; + //FAILING //Set an exiting delay, allowing the controler to handle things this.setExitDelay = function (seconds) { client.sendCommand("setExitDelay", 0, seconds); @@ -403,7 +405,7 @@ function Controler(socket, authentication) { client.callFunction("saveAndExit", 0, callback); }; - //Get the numbr of modified buffers + //Get the number of modified buffers this.getModified = function (callback) { client.callFunction("getModified", 0, callback); }; @@ -429,10 +431,10 @@ function Controler(socket, authentication) { //Get a buffer using its name or its id this.getBuffer = function(buffId) { - if (typeof(buffId) === "string" || buffId instanceof String) { + if (typeof(buffId) === "string" || buffId instanceof String) { for (var id in buffers) { if (buffers[id].name === buffId) { - return buffers[i]; + return buffers[id]; } } @@ -449,7 +451,7 @@ function Controler(socket, authentication) { this.registerKey = function (key, callback) { self.specialKeys(key); Object.defineProperty(keysCallback, key, { value: callback, configurable: true}); - } + }; } util.inherits(Controler, EventEmitter);