1 CodeMirror.defineMode('shell', function() {
4 function define(style, string) {
5 var split = string.split(' ');
6 for(var i = 0; i < split.length; i++) {
7 words[split[i]] = style;
12 define('atom', 'true false');
15 define('keyword', 'if then do else elif while until for in esac fi fin ' +
16 'fil done exit set unset export function');
19 define('builtin', 'ab awk bash beep cat cc cd chown chmod chroot clear cp ' +
20 'curl cut diff echo find gawk gcc get git grep kill killall ln ls make ' +
21 'mkdir openssl mv nc node npm ping ps restart rm rmdir sed service sh ' +
22 'shopt shred source sort sleep ssh start stop su sudo tee telnet top ' +
23 'touch vi vim wall wc wget who write yes zsh');
25 function tokenBase(stream, state) {
27 var sol = stream.sol();
28 var ch = stream.next();
30 if (ch === '\'' || ch === '"' || ch === '`') {
31 state.tokens.unshift(tokenString(ch));
32 return tokenize(stream, state);
35 if (sol && stream.eat('!')) {
37 return 'meta'; // 'comment'?
43 state.tokens.unshift(tokenDollar);
44 return tokenize(stream, state);
46 if (ch === '+' || ch === '=') {
51 stream.eatWhile(/\w/);
55 stream.eatWhile(/\d/);
56 if(!/\w/.test(stream.peek())) {
60 stream.eatWhile(/[\w-]/);
61 var cur = stream.current();
62 if (stream.peek() === '=' && /\w+/.test(cur)) return 'def';
63 return words.hasOwnProperty(cur) ? words[cur] : null;
66 function tokenString(quote) {
67 return function(stream, state) {
68 var next, end = false, escaped = false;
69 while ((next = stream.next()) != null) {
70 if (next === quote && !escaped) {
74 if (next === '$' && !escaped && quote !== '\'') {
77 state.tokens.unshift(tokenDollar);
80 escaped = !escaped && next === '\\';
82 if (end || !escaped) {
85 return (quote === '`' || quote === ')' ? 'quote' : 'string');
89 var tokenDollar = function(stream, state) {
90 if (state.tokens.length > 1) stream.eat('$');
91 var ch = stream.next(), hungry = /\w/;
92 if (ch === '{') hungry = /[^}]/;
94 state.tokens[0] = tokenString(')');
95 return tokenize(stream, state);
98 stream.eatWhile(hungry);
101 state.tokens.shift();
105 function tokenize(stream, state) {
106 return (state.tokens[0] || tokenBase) (stream, state);
110 startState: function() {return {tokens:[]};},
111 token: function(stream, state) {
112 if (stream.eatSpace()) return null;
113 return tokenize(stream, state);
118 CodeMirror.defineMIME('text/x-sh', 'shell');