jashkenas--coffeescript/lib/coffee-script/repl.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2013-01-04 14:35:20 -05:00
// Generated by CoffeeScript 1.5.0-pre
(function() {
2013-01-16 02:31:40 -05:00
var CoffeeScript, addMultilineHandler, merge, nodeREPL, replDefaults, vm;
vm = require('vm');
2013-01-16 02:31:40 -05:00
nodeREPL = require('repl');
CoffeeScript = require('./coffee-script');
merge = require('./helpers').merge;
replDefaults = {
2013-01-16 02:31:40 -05:00
prompt: 'coffee> ',
"eval": function(code, context, file, cb) {
try {
2013-01-15 01:22:28 -05:00
if (/^\(\s+\)$/.test(code)) {
2013-01-16 02:31:40 -05:00
return cb(null);
2013-01-15 01:22:28 -05:00
}
2013-01-16 02:31:40 -05:00
code = code.replace(/子/mg, '\n');
code = CoffeeScript.compile(code, {
filename: file,
bare: true
});
return cb(null, vm.runInContext(code, context, file));
} catch (err) {
return cb(err);
}
}
};
2013-01-16 02:31:40 -05:00
addMultilineHandler = function(repl) {
var inputStream, multiline, nodeLineListener, outputStream, rli;
rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
multiline = {
enabled: false,
prompt: new Array(repl.prompt.length).join('.') + ' ',
buffer: ''
};
nodeLineListener = rli.listeners('line')[0];
rli.removeListener('line', nodeLineListener);
rli.on('line', function(cmd) {
if (multiline.enabled === true) {
multiline.buffer += "" + cmd + "\n";
return rli.prompt(true);
} else {
return nodeLineListener(cmd);
}
});
return inputStream.on('keypress', function(char, key) {
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
return;
}
multiline.enabled = !multiline.enabled;
if (multiline.enabled === false) {
if (!multiline.buffer.match(/\n/)) {
rli.setPrompt(repl.prompt);
rli.prompt(true);
return;
}
multiline.buffer = multiline.buffer.replace(/\n/mg, '子');
rli.emit('line', multiline.buffer);
return multiline.buffer = '';
} else {
rli.setPrompt(multiline.prompt);
return rli.prompt(true);
}
});
};
module.exports = {
start: function(opts) {
2013-01-16 02:31:40 -05:00
var repl;
if (opts == null) {
opts = {};
2012-04-10 14:57:45 -04:00
}
2013-01-16 02:31:40 -05:00
opts = merge(replDefaults, opts);
repl = nodeREPL.start(opts);
addMultilineHandler(repl);
return repl;
}
};
}).call(this);