5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode('shell', function() {
17
function define(style, dict) {
18
for(var i = 0; i < dict.length; i++) {
19
words[dict[i]] = style;
23
var commonAtoms = ["true", "false"];
24
var commonKeywords = ["if", "then", "do", "else", "elif", "while", "until", "for", "in", "esac", "fi",
25
"fin", "fil", "done", "exit", "set", "unset", "export", "function"];
26
var commonCommands = ["ab", "awk", "bash", "beep", "cat", "cc", "cd", "chown", "chmod", "chroot", "clear",
27
"cp", "curl", "cut", "diff", "echo", "find", "gawk", "gcc", "get", "git", "grep", "hg", "kill", "killall",
28
"ln", "ls", "make", "mkdir", "openssl", "mv", "nc", "nl", "node", "npm", "ping", "ps", "restart", "rm",
29
"rmdir", "sed", "service", "sh", "shopt", "shred", "source", "sort", "sleep", "ssh", "start", "stop",
30
"su", "sudo", "svn", "tee", "telnet", "top", "touch", "vi", "vim", "wall", "wc", "wget", "who", "write",
33
CodeMirror.registerHelper("hintWords", "shell", commonAtoms.concat(commonKeywords, commonCommands));
35
define('atom', commonAtoms);
36
define('keyword', commonKeywords);
37
define('builtin', commonCommands);
39
function tokenBase(stream, state) {
40
if (stream.eatSpace()) return null;
42
var sol = stream.sol();
43
var ch = stream.next();
49
if (ch === '\'' || ch === '"' || ch === '`') {
50
state.tokens.unshift(tokenString(ch, ch === "`" ? "quote" : "string"));
51
return tokenize(stream, state);
54
if (sol && stream.eat('!')) {
62
state.tokens.unshift(tokenDollar);
63
return tokenize(stream, state);
65
if (ch === '+' || ch === '=') {
70
stream.eatWhile(/\w/);
74
if (stream.match("<<")) return "operator"
75
var heredoc = stream.match(/^<-?\s*['"]?([^'"]*)['"]?/)
77
state.tokens.unshift(tokenHeredoc(heredoc[1]))
82
stream.eatWhile(/\d/);
83
if(stream.eol() || !/\w/.test(stream.peek())) {
87
stream.eatWhile(/[\w-]/);
88
var cur = stream.current();
89
if (stream.peek() === '=' && /\w+/.test(cur)) return 'def';
90
return words.hasOwnProperty(cur) ? words[cur] : null;
93
function tokenString(quote, style) {
94
var close = quote == "(" ? ")" : quote == "{" ? "}" : quote
95
return function(stream, state) {
96
var next, escaped = false;
97
while ((next = stream.next()) != null) {
98
if (next === close && !escaped) {
101
} else if (next === '$' && !escaped && quote !== "'" && stream.peek() != close) {
104
state.tokens.unshift(tokenDollar);
106
} else if (!escaped && quote !== close && next === quote) {
107
state.tokens.unshift(tokenString(quote, style))
108
return tokenize(stream, state)
109
} else if (!escaped && /['"]/.test(next) && !/['"]/.test(quote)) {
110
state.tokens.unshift(tokenStringStart(next, "string"));
114
escaped = !escaped && next === '\\';
120
function tokenStringStart(quote, style) {
121
return function(stream, state) {
122
state.tokens[0] = tokenString(quote, style)
124
return tokenize(stream, state)
128
var tokenDollar = function(stream, state) {
129
if (state.tokens.length > 1) stream.eat('$');
130
var ch = stream.next()
131
if (/['"({]/.test(ch)) {
132
state.tokens[0] = tokenString(ch, ch == "(" ? "quote" : ch == "{" ? "def" : "string");
133
return tokenize(stream, state);
135
if (!/\d/.test(ch)) stream.eatWhile(/\w/);
136
state.tokens.shift();
140
function tokenHeredoc(delim) {
141
return function(stream, state) {
142
if (stream.sol() && stream.string == delim) state.tokens.shift()
148
function tokenize(stream, state) {
149
return (state.tokens[0] || tokenBase) (stream, state);
153
startState: function() {return {tokens:[]};},
154
token: function(stream, state) {
155
return tokenize(stream, state);
157
closeBrackets: "()[]{}''\"\"``",
163
CodeMirror.defineMIME('text/x-sh', 'shell');
166
CodeMirror.defineMIME('application/x-sh', 'shell');