runmode-standalone.js (5343B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 window.CodeMirror = {}; 5 6 (function() { 7 "use strict"; 8 9 function splitLines(string){ return string.split(/\r?\n|\r/); }; 10 11 function StringStream(string) { 12 this.pos = this.start = 0; 13 this.string = string; 14 this.lineStart = 0; 15 } 16 StringStream.prototype = { 17 eol: function() {return this.pos >= this.string.length;}, 18 sol: function() {return this.pos == 0;}, 19 peek: function() {return this.string.charAt(this.pos) || null;}, 20 next: function() { 21 if (this.pos < this.string.length) 22 return this.string.charAt(this.pos++); 23 }, 24 eat: function(match) { 25 var ch = this.string.charAt(this.pos); 26 if (typeof match == "string") var ok = ch == match; 27 else var ok = ch && (match.test ? match.test(ch) : match(ch)); 28 if (ok) {++this.pos; return ch;} 29 }, 30 eatWhile: function(match) { 31 var start = this.pos; 32 while (this.eat(match)){} 33 return this.pos > start; 34 }, 35 eatSpace: function() { 36 var start = this.pos; 37 while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos; 38 return this.pos > start; 39 }, 40 skipToEnd: function() {this.pos = this.string.length;}, 41 skipTo: function(ch) { 42 var found = this.string.indexOf(ch, this.pos); 43 if (found > -1) {this.pos = found; return true;} 44 }, 45 backUp: function(n) {this.pos -= n;}, 46 column: function() {return this.start - this.lineStart;}, 47 indentation: function() {return 0;}, 48 match: function(pattern, consume, caseInsensitive) { 49 if (typeof pattern == "string") { 50 var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;}; 51 var substr = this.string.substr(this.pos, pattern.length); 52 if (cased(substr) == cased(pattern)) { 53 if (consume !== false) this.pos += pattern.length; 54 return true; 55 } 56 } else { 57 var match = this.string.slice(this.pos).match(pattern); 58 if (match && match.index > 0) return null; 59 if (match && consume !== false) this.pos += match[0].length; 60 return match; 61 } 62 }, 63 current: function(){return this.string.slice(this.start, this.pos);}, 64 hideFirstChars: function(n, inner) { 65 this.lineStart += n; 66 try { return inner(); } 67 finally { this.lineStart -= n; } 68 }, 69 lookAhead: function() { return null } 70 }; 71 CodeMirror.StringStream = StringStream; 72 73 CodeMirror.startState = function (mode, a1, a2) { 74 return mode.startState ? mode.startState(a1, a2) : true; 75 }; 76 77 var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {}; 78 CodeMirror.defineMode = function (name, mode) { 79 if (arguments.length > 2) 80 mode.dependencies = Array.prototype.slice.call(arguments, 2); 81 modes[name] = mode; 82 }; 83 CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; }; 84 CodeMirror.resolveMode = function(spec) { 85 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) { 86 spec = mimeModes[spec]; 87 } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) { 88 spec = mimeModes[spec.name]; 89 } 90 if (typeof spec == "string") return {name: spec}; 91 else return spec || {name: "null"}; 92 }; 93 CodeMirror.getMode = function (options, spec) { 94 spec = CodeMirror.resolveMode(spec); 95 var mfactory = modes[spec.name]; 96 if (!mfactory) throw new Error("Unknown mode: " + spec); 97 return mfactory(options, spec); 98 }; 99 CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min; 100 CodeMirror.defineMode("null", function() { 101 return {token: function(stream) {stream.skipToEnd();}}; 102 }); 103 CodeMirror.defineMIME("text/plain", "null"); 104 105 CodeMirror.runMode = function (string, modespec, callback, options) { 106 var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec); 107 108 if (callback.nodeType == 1) { 109 var tabSize = (options && options.tabSize) || 4; 110 var node = callback, col = 0; 111 node.innerHTML = ""; 112 callback = function (text, style) { 113 if (text == "\n") { 114 node.appendChild(document.createElement("br")); 115 col = 0; 116 return; 117 } 118 var content = ""; 119 // replace tabs 120 for (var pos = 0; ;) { 121 var idx = text.indexOf("\t", pos); 122 if (idx == -1) { 123 content += text.slice(pos); 124 col += text.length - pos; 125 break; 126 } else { 127 col += idx - pos; 128 content += text.slice(pos, idx); 129 var size = tabSize - col % tabSize; 130 col += size; 131 for (var i = 0; i < size; ++i) content += " "; 132 pos = idx + 1; 133 } 134 } 135 136 if (style) { 137 var sp = node.appendChild(document.createElement("span")); 138 sp.className = "cm-" + style.replace(/ +/g, " cm-"); 139 sp.appendChild(document.createTextNode(content)); 140 } else { 141 node.appendChild(document.createTextNode(content)); 142 } 143 }; 144 } 145 146 var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode); 147 for (var i = 0, e = lines.length; i < e; ++i) { 148 if (i) callback("\n"); 149 var stream = new CodeMirror.StringStream(lines[i]); 150 if (!stream.string && mode.blankLine) mode.blankLine(state); 151 while (!stream.eol()) { 152 var style = mode.token(stream, state); 153 callback(stream.current(), style, i, stream.start, state); 154 stream.start = stream.pos; 155 } 156 } 157 }; 158 })();