Key callback helper

This commit is contained in:
2014-11-17 00:46:41 +01:00
parent 169b8397dc
commit dd227d2863
2 changed files with 31 additions and 19 deletions

View File

@@ -13,6 +13,7 @@ function Buffer (client, buffId, path) {
this.title = undefined; this.title = undefined;
var typeNum = 0; //WARNING : check if this should be unique to a buffer 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 serNum = 0; //WARNING : check if this should be unique to a buffer
var keysCallback = {};
/* COMMANDS */ /* COMMANDS */
@@ -182,12 +183,21 @@ function Buffer (client, buffId, path) {
client.callFunction("remove", buffId, Types.position(off), length, callback); 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) { this.registerKey = function (key, callback) {
client.specialKeys(key); client.sendCommand("specialKeys", 0, Types.string(key));
Object.defineProperty(keysCallback, key, { value: callback, configurable: true}); 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); util.inherits(Buffer, EventEmitter);

View File

@@ -3,7 +3,7 @@ var EventEmitter = require("events").EventEmitter;
var util = require("util"); var util = require("util");
var Types = require("./types.js"); var Types = require("./types.js");
var Buffer = require("./buffer.js"); var Buffer = require("./buffer.js");
var event = require("./event.js"); var events = require("./event.js");
//A stream Transform implementation returning full netbeans messages //A stream Transform implementation returning full netbeans messages
function MessageTransform(opts) { function MessageTransform(opts) {
@@ -46,7 +46,6 @@ util.inherits(MessageTransform, Transform);
function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) { function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) {
"use strict"; "use strict";
var self = this;
var callbacks = {}; var callbacks = {};
var currentSeqno = 0; var currentSeqno = 0;
var connected = false; var connected = false;
@@ -266,31 +265,32 @@ function Controler(socket, authentication) {
var client = new NetbeansClient(socket, authentication, function(buffId, name) { var client = new NetbeansClient(socket, authentication, function(buffId, name) {
switch (name) { switch (name) {
case event.disconnect : case events.disconnect :
//Connexion closed by Vim //Connexion closed by Vim
socket.destroy(); socket.destroy();
self.emit("disconnected"); self.emit("disconnected");
break; break;
case event.killed : case events.killed :
//Remove the buffer if it as been killed //Remove the buffer if it as been killed
if (buffers.hasOwnProperty(buffId)) { if (buffers.hasOwnProperty(buffId)) {
buffers[buffId].emit.apply(buffers[buffId], Array.prototype.slice.call(arguments, 1)); buffers[buffId].emit.apply(buffers[buffId], Array.prototype.slice.call(arguments, 1));
delete buffers[buffId]; delete buffers[buffId];
} }
break; break;
case event.keyAtPos : case events.keyAtPos :
//Call the key registered callback if it exists. //Call the key registered callback if it exists.
var buffer = getBuffer(buffId); var buffer = self.getBuffer(buffId);
if (keysCallback.indexOf(arguments[2]) >= 0) { var pos = arguments[3].split("/");
keysCallback.call(null, buffer, arguments[3].split("/")[0], arguments[3].split("/")[1]);
} if ((!buffer || !buffer.handleKey(arguments, pos[0], pos[1]))
else if (buffer === undefined) { && keysCallback.hasOwnProperty(arguments[2])){
keysCallback[arguments[2]].call(null, buffer, pos[0], pos[1]);
} }
break; break;
case event.fileOpened : case events.fileOpened :
//File opened. Check if it's already managed by controler. //File opened. Check if it's already managed by controler.
if (self.getBuffer(arguments[2]) === undefined) { if (self.getBuffer(arguments[2]) === undefined) {
unmanagedFiles.push(arguments[2]); unmanagedFiles.push(arguments[2]);
@@ -319,6 +319,7 @@ function Controler(socket, authentication) {
var buffId = 0; var buffId = 0;
var buffers = {}; var buffers = {};
//Internal function to create a new buffer
function newBuffer(pathname) { function newBuffer(pathname) {
if (pathname !== undefined) { if (pathname !== undefined) {
var indexFile = unmanagedFiles.indexOf(pathname); var indexFile = unmanagedFiles.indexOf(pathname);
@@ -347,6 +348,7 @@ function Controler(socket, authentication) {
client.sendCommand("raise", 0); client.sendCommand("raise", 0);
}; };
//FAILING
//Set an exiting delay, allowing the controler to handle things //Set an exiting delay, allowing the controler to handle things
this.setExitDelay = function (seconds) { this.setExitDelay = function (seconds) {
client.sendCommand("setExitDelay", 0, seconds); client.sendCommand("setExitDelay", 0, seconds);
@@ -403,7 +405,7 @@ function Controler(socket, authentication) {
client.callFunction("saveAndExit", 0, callback); client.callFunction("saveAndExit", 0, callback);
}; };
//Get the numbr of modified buffers //Get the number of modified buffers
this.getModified = function (callback) { this.getModified = function (callback) {
client.callFunction("getModified", 0, callback); client.callFunction("getModified", 0, callback);
}; };
@@ -429,10 +431,10 @@ function Controler(socket, authentication) {
//Get a buffer using its name or its id //Get a buffer using its name or its id
this.getBuffer = function(buffId) { this.getBuffer = function(buffId) {
if (typeof(buffId) === "string" || buffId instanceof String) { if (typeof(buffId) === "string" || buffId instanceof String) {
for (var id in buffers) { for (var id in buffers) {
if (buffers[id].name === buffId) { if (buffers[id].name === buffId) {
return buffers[i]; return buffers[id];
} }
} }
@@ -449,7 +451,7 @@ function Controler(socket, authentication) {
this.registerKey = function (key, callback) { this.registerKey = function (key, callback) {
self.specialKeys(key); self.specialKeys(key);
Object.defineProperty(keysCallback, key, { value: callback, configurable: true}); Object.defineProperty(keysCallback, key, { value: callback, configurable: true});
} };
} }
util.inherits(Controler, EventEmitter); util.inherits(Controler, EventEmitter);