-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
terminal.js
178 lines (177 loc) · 6.7 KB
/
terminal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* This is example of usuage of LIPS interprter with jQuery Terminal
*
* Copyright (C) Jakub T. Jankiewicz <https://jcubic.pl>
* Released under MIT license
*/
/* global jQuery, clearTimeout, setTimeout */
function terminal({selector, lips, dynamic = false, name = 'terminal'}) {
var position;
var timer;
var help = lips.env.get('help');
function doc(fn, doc) {
fn.__doc__ = doc;
return fn;
}
// -------------------------------------------------------------------------
var env = lips.env.inherit(name, {
stdout: {
write: function() {
var args = Array.from(arguments);
args.forEach(function(arg) {
term.echo(arg, {formatters: false});
});
}
},
// ---------------------------------------------------------------------
stdin: {
read: function() {
return term.read('');
}
},
// ---------------------------------------------------------------------
help: doc(new lips.Macro('help', function(code, { error }) {
const { evaluate, Pair, Symbol, nil } = lips;
var new_code = new Pair(new Symbol('__help'), code);
var doc = evaluate(new_code, { env: this, error });
term.echo(doc, { formatters: false });
}), lips.env.env.help.__doc__),
// ---------------------------------------------------------------------
error: doc(function(message) {
term.error(message);
}, lips.env.env.error.__doc__)
});
// hack so (let ((x lambda)) (help x))
env.env.__help = lips.env.env.help;
// -------------------------------------------------------------------------
var term = jQuery(selector).terminal(function(code, term) {
// format before executing mainly for strings in function docs
code = new lips.Formatter(code).format();
lips.exec(code, env, dynamic).then(function(ret) {
ret.forEach(function(ret) {
if (ret !== undefined) {
env.get('print').call(env, ret);
}
});
}).catch(function(e) {
term.error(e.message || e);
if (e.code) {
term.error(e.code.map((line, i) => `[${i+1}]: ${line}`).join('\n'));
}
});
}, {
name,
prompt: 'lips> ',
enabled: false,
greetings: false,
keymap: {
ENTER: function(e, original) {
if (lips.balanced_parenthesis(this.get_command())) {
original();
} else {
var code = term.before_cursor();
var prompt = this.get_prompt();
var formatter = new lips.Formatter(code);
var i = formatter.indent({
indent: 2,
offset: prompt.length
});
console.log({i, x: prompt.length});
this.insert('\n' + (new Array(i + 1).join(' ')));
}
}
},
onPaste: function(e) {
if (e.text) {
var code = e.text;
var prompt = this.get_prompt();
try {
var formatter = new lips.Formatter(code);
var output = formatter.format({
offset: prompt.length
});
} catch(e) {
// boken LIPS code
output = code;
}
return Promise.resolve(output);
}
},
keydown: function() {
if (position) {
term.set_position(position);
position = false;
}
},
mobileIngoreAutoSpace: [',', '.', ')'],
completionEscape: false,
wordAutocomplete: false,
doubleTab: function(string, maches, echo_command) {
echo_command();
this.echo(maches.map(command => {
return lips.tokenize(command).pop();
}));
},
completion: function(string) {
let tokens = lips.tokenize(this.before_cursor(), true);
tokens = tokens.map(token => token.token);
// Array.from is need to for jQuery terminal version <2.5.0
// when terminal is outside iframe and lips is inside
// jQuery Terminal was using instanceof that don't work between iframes
if (!tokens.length) {
return Array.from(env.get('env').apply(env).toArray());
}
const last = tokens.pop();
if (last.trim().length) {
const globals = Object.getOwnPropertyNames(window);
const prefix = tokens.join('');
const re = new RegExp('^' + jQuery.terminal.escape_regex(last));
var commands = env.get('env').apply(env).toArray().concat(globals).filter(name => {
return re.test(name);
}).map(name => prefix + name);
return Array.from(commands);
}
},
keypress: function(e) {
var term = this;
if (e.key == ')') {
setTimeout(function() {
position = term.get_position();
var command = term.get_command().substring(0, position);
var len = command.split(/\n/)[0].length;
var tokens = lips.tokenize(command, true);
var count = 1;
var token;
var i = tokens.length - 1;
while (count > 0) {
token = tokens[--i];
if (!token) {
return;
}
if (token.token === '(') {
count--;
} else if (token.token == ')') {
count++;
}
}
if (token.token == '(' && count === 0) {
clearTimeout(timer);
setTimeout(function() {
var offset = token.offset;
term.set_position(offset);
timer = setTimeout(function() {
term.set_position(position);
position = false;
}, 200);
}, 0);
}
}, 0);
} else {
position = false;
}
}
});
term.env = env;
env.set('term', term);
return term;
}