5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.multiplexingMode = function(outer ) {
16
var others = Array.prototype.slice.call(arguments, 1);
18
function indexOf(string, pattern, from, returnEnd) {
19
if (typeof pattern == "string") {
20
var found = string.indexOf(pattern, from);
21
return returnEnd && found > -1 ? found + pattern.length : found;
23
var m = pattern.exec(from ? string.slice(from) : string);
24
return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1;
28
startState: function() {
30
outer: CodeMirror.startState(outer),
37
copyState: function(state) {
39
outer: CodeMirror.copyState(outer, state.outer),
40
innerActive: state.innerActive,
41
inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner),
42
startingInner: state.startingInner
46
token: function(stream, state) {
47
if (!state.innerActive) {
48
var cutOff = Infinity, oldContent = stream.string;
49
for (var i = 0; i < others.length; ++i) {
50
var other = others[i];
51
var found = indexOf(oldContent, other.open, stream.pos);
52
if (found == stream.pos) {
53
if (!other.parseDelimiters) stream.match(other.open);
54
state.startingInner = !!other.parseDelimiters
55
state.innerActive = other;
60
var possibleOuterIndent = outer.indent(state.outer, "", "");
61
if (possibleOuterIndent !== CodeMirror.Pass) outerIndent = possibleOuterIndent;
64
state.inner = CodeMirror.startState(other.mode, outerIndent);
65
return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open");
66
} else if (found != -1 && found < cutOff) {
70
if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
71
var outerToken = outer.token(stream, state.outer);
72
if (cutOff != Infinity) stream.string = oldContent;
75
var curInner = state.innerActive, oldContent = stream.string;
76
if (!curInner.close && stream.sol()) {
77
state.innerActive = state.inner = null;
78
return this.token(stream, state);
80
var found = curInner.close && !state.startingInner ?
81
indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
82
if (found == stream.pos && !curInner.parseDelimiters) {
83
stream.match(curInner.close);
84
state.innerActive = state.inner = null;
85
return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close");
87
if (found > -1) stream.string = oldContent.slice(0, found);
88
var innerToken = curInner.mode.token(stream, state.inner);
89
if (found > -1) stream.string = oldContent;
90
else if (stream.pos > stream.start) state.startingInner = false
92
if (found == stream.pos && curInner.parseDelimiters)
93
state.innerActive = state.inner = null;
95
if (curInner.innerStyle) {
96
if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;
97
else innerToken = curInner.innerStyle;
104
indent: function(state, textAfter, line) {
105
var mode = state.innerActive ? state.innerActive.mode : outer;
106
if (!mode.indent) return CodeMirror.Pass;
107
return mode.indent(state.innerActive ? state.inner : state.outer, textAfter, line);
110
blankLine: function(state) {
111
var mode = state.innerActive ? state.innerActive.mode : outer;
112
if (mode.blankLine) {
113
mode.blankLine(state.innerActive ? state.inner : state.outer);
115
if (!state.innerActive) {
116
for (var i = 0; i < others.length; ++i) {
117
var other = others[i];
118
if (other.open === "\n") {
119
state.innerActive = other;
120
state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "", "") : 0);
123
} else if (state.innerActive.close === "\n") {
124
state.innerActive = state.inner = null;
128
electricChars: outer.electricChars,
130
innerMode: function(state) {
131
return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};