runmode-standalone.js 4.9 KB

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