1 // Content of the function is equal to runmode-standalone.js file
2 // from CodeMirror distribution
4 // CodeMirror, copyright (c) by Marijn Haverbeke and others
5 // Distributed under an MIT license: http://codemirror.net/LICENSE
7 window
.CodeMirror
= {};
12 function splitLines(string
){ return string
.split(/\r?\n|\r/); };
14 function StringStream(string
) {
15 this.pos
= this.start
= 0;
19 StringStream
.prototype = {
20 eol: function() {return this.pos
>= this.string
.length
;},
21 sol: function() {return this.pos
== 0;},
22 peek: function() {return this.string
.charAt(this.pos
) || null;},
24 if (this.pos
< this.string
.length
)
25 return this.string
.charAt(this.pos
++);
27 eat: function(match
) {
28 var ch
= this.string
.charAt(this.pos
);
29 if (typeof match
== "string") var ok
= ch
== match
;
30 else var ok
= ch
&& (match
.test
? match
.test(ch
) : match(ch
));
31 if (ok
) {++this.pos
; return ch
;}
33 eatWhile: function(match
) {
35 while (this.eat(match
)){}
36 return this.pos
> start
;
38 eatSpace: function() {
40 while (/[\s\u00a0]/.test(this.string
.charAt(this.pos
))) ++this.pos
;
41 return this.pos
> start
;
43 skipToEnd: function() {this.pos
= this.string
.length
;},
44 skipTo: function(ch
) {
45 var found
= this.string
.indexOf(ch
, this.pos
);
46 if (found
> -1) {this.pos
= found
; return true;}
48 backUp: function(n
) {this.pos
-= n
;},
49 column: function() {return this.start
- this.lineStart
;},
50 indentation: function() {return 0;},
51 match: function(pattern
, consume
, caseInsensitive
) {
52 if (typeof pattern
== "string") {
53 var cased = function(str
) {return caseInsensitive
? str
.toLowerCase() : str
;};
54 var substr
= this.string
.substr(this.pos
, pattern
.length
);
55 if (cased(substr
) == cased(pattern
)) {
56 if (consume
!== false) this.pos
+= pattern
.length
;
60 var match
= this.string
.slice(this.pos
).match(pattern
);
61 if (match
&& match
.index
> 0) return null;
62 if (match
&& consume
!== false) this.pos
+= match
[0].length
;
66 current: function(){return this.string
.slice(this.start
, this.pos
);},
67 hideFirstChars: function(n
, inner
) {
69 try { return inner(); }
70 finally { this.lineStart
-= n
; }
73 CodeMirror
.StringStream
= StringStream
;
75 CodeMirror
.startState = function (mode
, a1
, a2
) {
76 return mode
.startState
? mode
.startState(a1
, a2
) : true;
79 var modes
= CodeMirror
.modes
= {}, mimeModes
= CodeMirror
.mimeModes
= {};
80 CodeMirror
.defineMode = function (name
, mode
) {
81 if (arguments
.length
> 2)
82 mode
.dependencies
= Array
.prototype.slice
.call(arguments
, 2);
85 CodeMirror
.defineMIME = function (mime
, spec
) { mimeModes
[mime
] = spec
; };
86 CodeMirror
.resolveMode = function(spec
) {
87 if (typeof spec
== "string" && mimeModes
.hasOwnProperty(spec
)) {
88 spec
= mimeModes
[spec
];
89 } else if (spec
&& typeof spec
.name
== "string" && mimeModes
.hasOwnProperty(spec
.name
)) {
90 spec
= mimeModes
[spec
.name
];
92 if (typeof spec
== "string") return {name
: spec
};
93 else return spec
|| {name
: "null"};
95 CodeMirror
.getMode = function (options
, spec
) {
96 spec
= CodeMirror
.resolveMode(spec
);
97 var mfactory
= modes
[spec
.name
];
98 if (!mfactory
) throw new Error("Unknown mode: " + spec
);
99 return mfactory(options
, spec
);
101 CodeMirror
.registerHelper
= CodeMirror
.registerGlobalHelper
= Math
.min
;
102 CodeMirror
.defineMode("null", function() {
103 return {token: function(stream
) {stream
.skipToEnd();}};
105 CodeMirror
.defineMIME("text/plain", "null");
107 CodeMirror
.runMode = function (string
, modespec
, callback
, options
) {
108 var mode
= CodeMirror
.getMode({ indentUnit
: 2 }, modespec
);
110 if (callback
.nodeType
== 1) {
111 var tabSize
= (options
&& options
.tabSize
) || 4;
112 var node
= callback
, col
= 0;
114 callback = function (text
, style
) {
116 node
.appendChild(document
.createElement("br"));
122 for (var pos
= 0; ;) {
123 var idx
= text
.indexOf("\t", pos
);
125 content
+= text
.slice(pos
);
126 col
+= text
.length
- pos
;
130 content
+= text
.slice(pos
, idx
);
131 var size
= tabSize
- col
% tabSize
;
133 for (var i
= 0; i
< size
; ++i
) content
+= " ";
139 var sp
= node
.appendChild(document
.createElement("span"));
140 sp
.className
= "cm-" + style
.replace(/ +/g
, " cm-");
141 sp
.appendChild(document
.createTextNode(content
));
143 node
.appendChild(document
.createTextNode(content
));
148 var lines
= splitLines(string
), state
= (options
&& options
.state
) || CodeMirror
.startState(mode
);
149 for (var i
= 0, e
= lines
.length
; i
< e
; ++i
) {
150 if (i
) callback("\n");
151 var stream
= new CodeMirror
.StringStream(lines
[i
]);
152 if (!stream
.string
&& mode
.blankLine
) mode
.blankLine(state
);
153 while (!stream
.eol()) {
154 var style
= mode
.token(stream
, state
);
155 callback(stream
.current(), style
, i
, stream
.start
, state
);
156 stream
.start
= stream
.pos
;