]>
Commit | Line | Data |
---|---|---|
06a3d686 BB |
1 | CodeMirror.multiplexingMode = function(outer /*, others */) { |
2 | // Others should be {open, close, mode [, delimStyle]} objects | |
3 | var others = Array.prototype.slice.call(arguments, 1); | |
4 | var n_others = others.length; | |
5 | ||
6 | function indexOf(string, pattern, from) { | |
7 | if (typeof pattern == "string") return string.indexOf(pattern, from); | |
8 | var m = pattern.exec(from ? string.slice(from) : string); | |
9 | return m ? m.index + from : -1; | |
10 | } | |
11 | ||
12 | return { | |
13 | startState: function() { | |
14 | return { | |
15 | outer: CodeMirror.startState(outer), | |
16 | innerActive: null, | |
17 | inner: null | |
18 | }; | |
19 | }, | |
20 | ||
21 | copyState: function(state) { | |
22 | return { | |
23 | outer: CodeMirror.copyState(outer, state.outer), | |
24 | innerActive: state.innerActive, | |
25 | inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner) | |
26 | }; | |
27 | }, | |
28 | ||
29 | token: function(stream, state) { | |
30 | if (!state.innerActive) { | |
31 | var cutOff = Infinity, oldContent = stream.string; | |
32 | for (var i = 0; i < n_others; ++i) { | |
33 | var other = others[i]; | |
34 | var found = indexOf(oldContent, other.open, stream.pos); | |
35 | if (found == stream.pos) { | |
36 | stream.match(other.open); | |
37 | state.innerActive = other; | |
38 | state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0); | |
39 | return other.delimStyle; | |
40 | } else if (found != -1 && found < cutOff) { | |
41 | cutOff = found; | |
42 | } | |
43 | } | |
44 | if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff); | |
45 | var outerToken = outer.token(stream, state.outer); | |
46 | if (cutOff != Infinity) stream.string = oldContent; | |
47 | return outerToken; | |
48 | } else { | |
49 | var curInner = state.innerActive, oldContent = stream.string; | |
50 | var found = indexOf(oldContent, curInner.close, stream.pos); | |
51 | if (found == stream.pos) { | |
52 | stream.match(curInner.close); | |
53 | state.innerActive = state.inner = null; | |
54 | return curInner.delimStyle; | |
55 | } | |
56 | if (found > -1) stream.string = oldContent.slice(0, found); | |
57 | var innerToken = curInner.mode.token(stream, state.inner); | |
58 | if (found > -1) stream.string = oldContent; | |
59 | var cur = stream.current(), found = cur.indexOf(curInner.close); | |
60 | if (found > -1) stream.backUp(cur.length - found); | |
61 | return innerToken; | |
62 | } | |
63 | }, | |
64 | ||
65 | indent: function(state, textAfter) { | |
66 | var mode = state.innerActive ? state.innerActive.mode : outer; | |
67 | if (!mode.indent) return CodeMirror.Pass; | |
68 | return mode.indent(state.innerActive ? state.inner : state.outer, textAfter); | |
69 | }, | |
70 | ||
71 | blankLine: function(state) { | |
72 | var mode = state.innerActive ? state.innerActive.mode : outer; | |
73 | if (mode.blankLine) { | |
74 | mode.blankLine(state.innerActive ? state.inner : state.outer); | |
75 | } | |
76 | if (!state.innerActive) { | |
77 | for (var i = 0; i < n_others; ++i) { | |
78 | var other = others[i]; | |
79 | if (other.open === "\n") { | |
80 | state.innerActive = other; | |
81 | state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0); | |
82 | } | |
83 | } | |
84 | } else if (mode.close === "\n") { | |
85 | state.innerActive = state.inner = null; | |
86 | } | |
87 | }, | |
88 | ||
89 | electricChars: outer.electricChars, | |
90 | ||
91 | innerMode: function(state) { | |
92 | return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer}; | |
93 | } | |
94 | }; | |
95 | }; |