1 window
.CodeMirror
= {};
6 function splitLines(string
){ return string
.split(/\r?\n|\r/); };
8 function StringStream(string
) {
9 this.pos
= this.start
= 0;
13 StringStream
.prototype = {
14 eol: function() {return this.pos
>= this.string
.length
;},
15 sol: function() {return this.pos
== 0;},
16 peek: function() {return this.string
.charAt(this.pos
) || null;},
18 if (this.pos
< this.string
.length
)
19 return this.string
.charAt(this.pos
++);
21 eat: function(match
) {
22 var ch
= this.string
.charAt(this.pos
);
23 if (typeof match
== "string") var ok
= ch
== match
;
24 else var ok
= ch
&& (match
.test
? match
.test(ch
) : match(ch
));
25 if (ok
) {++this.pos
; return ch
;}
27 eatWhile: function(match
) {
29 while (this.eat(match
)){}
30 return this.pos
> start
;
32 eatSpace: function() {
34 while (/[\s\u00a0]/.test(this.string
.charAt(this.pos
))) ++this.pos
;
35 return this.pos
> start
;
37 skipToEnd: function() {this.pos
= this.string
.length
;},
38 skipTo: function(ch
) {
39 var found
= this.string
.indexOf(ch
, this.pos
);
40 if (found
> -1) {this.pos
= found
; return true;}
42 backUp: function(n
) {this.pos
-= n
;},
43 column: function() {return this.start
- this.lineStart
;},
44 indentation: function() {return 0;},
45 match: function(pattern
, consume
, caseInsensitive
) {
46 if (typeof pattern
== "string") {
47 var cased = function(str
) {return caseInsensitive
? str
.toLowerCase() : str
;};
48 var substr
= this.string
.substr(this.pos
, pattern
.length
);
49 if (cased(substr
) == cased(pattern
)) {
50 if (consume
!== false) this.pos
+= pattern
.length
;
54 var match
= this.string
.slice(this.pos
).match(pattern
);
55 if (match
&& match
.index
> 0) return null;
56 if (match
&& consume
!== false) this.pos
+= match
[0].length
;
60 current: function(){return this.string
.slice(this.start
, this.pos
);},
61 hideFirstChars: function(n
, inner
) {
63 try { return inner(); }
64 finally { this.lineStart
-= n
; }
67 CodeMirror
.StringStream
= StringStream
;
69 CodeMirror
.startState = function (mode
, a1
, a2
) {
70 return mode
.startState
? mode
.startState(a1
, a2
) : true;
73 var modes
= CodeMirror
.modes
= {}, mimeModes
= CodeMirror
.mimeModes
= {};
74 CodeMirror
.defineMode = function (name
, mode
) { modes
[name
] = mode
; };
75 CodeMirror
.defineMIME = function (mime
, spec
) { mimeModes
[mime
] = spec
; };
76 CodeMirror
.resolveMode = function(spec
) {
77 if (typeof spec
== "string" && mimeModes
.hasOwnProperty(spec
)) {
78 spec
= mimeModes
[spec
];
79 } else if (spec
&& typeof spec
.name
== "string" && mimeModes
.hasOwnProperty(spec
.name
)) {
80 spec
= mimeModes
[spec
.name
];
82 if (typeof spec
== "string") return {name
: spec
};
83 else return spec
|| {name
: "null"};
85 CodeMirror
.getMode = function (options
, spec
) {
86 spec
= CodeMirror
.resolveMode(spec
);
87 var mfactory
= modes
[spec
.name
];
88 if (!mfactory
) throw new Error("Unknown mode: " + spec
);
89 return mfactory(options
, spec
);
91 CodeMirror
.registerHelper
= CodeMirror
.registerGlobalHelper
= Math
.min
;
92 CodeMirror
.defineMode("null", function() {
93 return {token: function(stream
) {stream
.skipToEnd();}};
95 CodeMirror
.defineMIME("text/plain", "null");
97 CodeMirror
.runMode = function (string
, modespec
, callback
, options
) {
98 var mode
= CodeMirror
.getMode({ indentUnit
: 2 }, modespec
);
100 if (callback
.nodeType
== 1) {
101 var tabSize
= (options
&& options
.tabSize
) || 4;
102 var node
= callback
, col
= 0;
104 callback = function (text
, style
) {
106 node
.appendChild(document
.createElement("br"));
112 for (var pos
= 0; ;) {
113 var idx
= text
.indexOf("\t", pos
);
115 content
+= text
.slice(pos
);
116 col
+= text
.length
- pos
;
120 content
+= text
.slice(pos
, idx
);
121 var size
= tabSize
- col
% tabSize
;
123 for (var i
= 0; i
< size
; ++i
) content
+= " ";
129 var sp
= node
.appendChild(document
.createElement("span"));
130 sp
.className
= "cm-" + style
.replace(/ +/g
, " cm-");
131 sp
.appendChild(document
.createTextNode(content
));
133 node
.appendChild(document
.createTextNode(content
));
138 var lines
= splitLines(string
), state
= (options
&& options
.state
) || CodeMirror
.startState(mode
);
139 for (var i
= 0, e
= lines
.length
; i
< e
; ++i
) {
140 if (i
) callback("\n");
141 var stream
= new CodeMirror
.StringStream(lines
[i
]);
142 while (!stream
.eol()) {
143 var style
= mode
.token(stream
, state
);
144 callback(stream
.current(), style
, i
, stream
.start
, state
);
145 stream
.start
= stream
.pos
;