headlesscodemirror.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function(window) {
  2. window.CodeMirror = {};
  3. function splitLines(string){ return string.split(/\r?\n|\r/); };
  4. function StringStream(string) {
  5. this.pos = this.start = 0;
  6. this.string = string;
  7. }
  8. StringStream.prototype = {
  9. eol: function() {return this.pos >= this.string.length;},
  10. sol: function() {return this.pos == 0;},
  11. peek: function() {return this.string.charAt(this.pos) || null;},
  12. next: function() {
  13. if (this.pos < this.string.length)
  14. return this.string.charAt(this.pos++);
  15. },
  16. eat: function(match) {
  17. var ch = this.string.charAt(this.pos);
  18. if (typeof match == "string") var ok = ch == match;
  19. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  20. if (ok) {++this.pos; return ch;}
  21. },
  22. eatWhile: function(match) {
  23. var start = this.pos;
  24. while (this.eat(match)){}
  25. return this.pos > start;
  26. },
  27. eatSpace: function() {
  28. var start = this.pos;
  29. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  30. return this.pos > start;
  31. },
  32. skipToEnd: function() {this.pos = this.string.length;},
  33. skipTo: function(ch) {
  34. var found = this.string.indexOf(ch, this.pos);
  35. if (found > -1) {this.pos = found; return true;}
  36. },
  37. backUp: function(n) {this.pos -= n;},
  38. column: function() {return this.start;},
  39. indentation: function() {return 0;},
  40. match: function(pattern, consume, caseInsensitive) {
  41. if (typeof pattern == "string") {
  42. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  43. var substr = this.string.substr(this.pos, pattern.length);
  44. if (cased(substr) == cased(pattern)) {
  45. if (consume !== false) this.pos += pattern.length;
  46. return true;
  47. }
  48. } else {
  49. var match = this.string.slice(this.pos).match(pattern);
  50. if (match && match.index > 0) return null;
  51. if (match && consume !== false) this.pos += match[0].length;
  52. return match;
  53. }
  54. },
  55. current: function(){return this.string.slice(this.start, this.pos);}
  56. };
  57. CodeMirror.StringStream = StringStream;
  58. CodeMirror.startState = function (mode, a1, a2) {
  59. return mode.startState ? mode.startState(a1, a2) : true;
  60. };
  61. var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  62. CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
  63. CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
  64. CodeMirror.defineMode("null", function() {
  65. return {token: function(stream) {stream.skipToEnd();}};
  66. });
  67. CodeMirror.defineMIME("text/plain", "null");
  68. CodeMirror.getMode = function (options, spec) {
  69. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
  70. spec = mimeModes[spec];
  71. if (typeof spec == "string")
  72. var mname = spec, config = {};
  73. else if (spec != null)
  74. var mname = spec.name, config = spec;
  75. var mfactory = modes[mname];
  76. if (!mfactory) throw new Error("Unknown mode: " + spec);
  77. return mfactory(options, config || {});
  78. };
  79. }(this));