1 // Generated by CoffeeScript 1.10.0
3 var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, ref, replDefaults, runInContext, updateSyntaxError, vm;
7 path = require('path');
11 nodeREPL = require('repl');
13 CoffeeScript = require('./coffee-script');
15 ref = require('./helpers'), merge = ref.merge, updateSyntaxError = ref.updateSyntaxError;
19 historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
20 historyMaxInputSize: 10240,
21 "eval": function(input, context, filename, cb) {
22 var Assign, Block, Literal, Value, ast, err, error, js, ref1, referencedVars, token, tokens;
23 input = input.replace(/\uFF00/g, '\n');
24 input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
25 ref1 = require('./nodes'), Block = ref1.Block, Assign = ref1.Assign, Value = ref1.Value, Literal = ref1.Literal;
27 tokens = CoffeeScript.tokens(input);
28 referencedVars = (function() {
31 for (i = 0, len = tokens.length; i < len; i++) {
34 results.push(token[1]);
39 ast = CoffeeScript.nodes(tokens);
40 ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
43 locals: Object.keys(context),
44 referencedVars: referencedVars
46 return cb(null, runInContext(js, context, filename));
49 updateSyntaxError(err, input);
55 runInContext = function(js, context, filename) {
56 if (context === global) {
57 return vm.runInThisContext(js, filename);
59 return vm.runInContext(js, context, filename);
63 addMultilineHandler = function(repl) {
64 var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref1, rli;
65 rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
66 origPrompt = (ref1 = repl._prompt) != null ? ref1 : repl.prompt;
69 initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
70 return x.replace(/./g, '-');
72 prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
73 return x.replace(/./g, '.');
77 nodeLineListener = rli.listeners('line')[0];
78 rli.removeListener('line', nodeLineListener);
79 rli.on('line', function(cmd) {
80 if (multiline.enabled) {
81 multiline.buffer += cmd + "\n";
82 rli.setPrompt(multiline.prompt);
85 rli.setPrompt(origPrompt);
86 nodeLineListener(cmd);
89 return inputStream.on('keypress', function(char, key) {
90 if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
93 if (multiline.enabled) {
94 if (!multiline.buffer.match(/\n/)) {
95 multiline.enabled = !multiline.enabled;
96 rli.setPrompt(origPrompt);
100 if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
103 multiline.enabled = !multiline.enabled;
106 rli.output.cursorTo(0);
107 rli.output.clearLine(1);
108 multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
109 rli.emit('line', multiline.buffer);
110 multiline.buffer = '';
112 multiline.enabled = !multiline.enabled;
113 rli.setPrompt(multiline.initialPrompt);
119 addHistory = function(repl, filename, maxSize) {
120 var buffer, fd, lastLine, readFd, size, stat;
123 stat = fs.statSync(filename);
124 size = Math.min(maxSize, stat.size);
125 readFd = fs.openSync(filename, 'r');
126 buffer = new Buffer(size);
127 fs.readSync(readFd, buffer, 0, size, stat.size - size);
129 repl.rli.history = buffer.toString().split('\n').reverse();
130 if (stat.size > maxSize) {
131 repl.rli.history.pop();
133 if (repl.rli.history[0] === '') {
134 repl.rli.history.shift();
136 repl.rli.historyIndex = -1;
137 lastLine = repl.rli.history[0];
138 } catch (undefined) {}
139 fd = fs.openSync(filename, 'a');
140 repl.rli.addListener('line', function(code) {
141 if (code && code.length && code !== '.history' && lastLine !== code) {
142 fs.write(fd, code + "\n");
143 return lastLine = code;
146 repl.on('exit', function() {
149 return repl.commands[getCommandId(repl, 'history')] = {
150 help: 'Show command history',
152 repl.outputStream.write((repl.rli.history.slice(0).reverse().join('\n')) + "\n");
153 return repl.displayPrompt();
158 getCommandId = function(repl, commandName) {
159 var commandsHaveLeadingDot;
160 commandsHaveLeadingDot = repl.commands['.help'] != null;
161 if (commandsHaveLeadingDot) {
162 return "." + commandName;
169 start: function(opts) {
170 var build, major, minor, ref1, repl;
174 ref1 = process.versions.node.split('.').map(function(n) {
176 }), major = ref1[0], minor = ref1[1], build = ref1[2];
177 if (major === 0 && minor < 8) {
178 console.warn("Node 0.8.0+ required for CoffeeScript REPL");
181 CoffeeScript.register();
182 process.argv = ['coffee'].concat(process.argv.slice(2));
183 opts = merge(replDefaults, opts);
184 repl = nodeREPL.start(opts);
186 runInContext(opts.prelude, repl.context, 'prelude');
188 repl.on('exit', function() {
189 if (!repl.rli.closed) {
190 return repl.outputStream.write('\n');
193 addMultilineHandler(repl);
194 if (opts.historyFile) {
195 addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
197 repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';