1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
---|
2 | // Distributed under an MIT license: http://codemirror.net/LICENSE |
---|
3 | |
---|
4 | (function(mod) { |
---|
5 | if (typeof exports == "object" && typeof module == "object") // CommonJS |
---|
6 | mod(require("../../lib/codemirror")); |
---|
7 | else if (typeof define == "function" && define.amd) // AMD |
---|
8 | define(["../../lib/codemirror"], mod); |
---|
9 | else // Plain browser env |
---|
10 | mod(CodeMirror); |
---|
11 | })(function(CodeMirror) { |
---|
12 | "use strict"; |
---|
13 | |
---|
14 | CodeMirror.multiplexingMode = function(outer /*, others */) { |
---|
15 | // Others should be {open, close, mode [, delimStyle] [, innerStyle]} objects |
---|
16 | var others = Array.prototype.slice.call(arguments, 1); |
---|
17 | |
---|
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; |
---|
22 | } |
---|
23 | var m = pattern.exec(from ? string.slice(from) : string); |
---|
24 | return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1; |
---|
25 | } |
---|
26 | |
---|
27 | return { |
---|
28 | startState: function() { |
---|
29 | return { |
---|
30 | outer: CodeMirror.startState(outer), |
---|
31 | innerActive: null, |
---|
32 | inner: null |
---|
33 | }; |
---|
34 | }, |
---|
35 | |
---|
36 | copyState: function(state) { |
---|
37 | return { |
---|
38 | outer: CodeMirror.copyState(outer, state.outer), |
---|
39 | innerActive: state.innerActive, |
---|
40 | inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner) |
---|
41 | }; |
---|
42 | }, |
---|
43 | |
---|
44 | token: function(stream, state) { |
---|
45 | if (!state.innerActive) { |
---|
46 | var cutOff = Infinity, oldContent = stream.string; |
---|
47 | for (var i = 0; i < others.length; ++i) { |
---|
48 | var other = others[i]; |
---|
49 | var found = indexOf(oldContent, other.open, stream.pos); |
---|
50 | if (found == stream.pos) { |
---|
51 | if (!other.parseDelimiters) stream.match(other.open); |
---|
52 | state.innerActive = other; |
---|
53 | state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0); |
---|
54 | return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open"); |
---|
55 | } else if (found != -1 && found < cutOff) { |
---|
56 | cutOff = found; |
---|
57 | } |
---|
58 | } |
---|
59 | if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff); |
---|
60 | var outerToken = outer.token(stream, state.outer); |
---|
61 | if (cutOff != Infinity) stream.string = oldContent; |
---|
62 | return outerToken; |
---|
63 | } else { |
---|
64 | var curInner = state.innerActive, oldContent = stream.string; |
---|
65 | if (!curInner.close && stream.sol()) { |
---|
66 | state.innerActive = state.inner = null; |
---|
67 | return this.token(stream, state); |
---|
68 | } |
---|
69 | var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1; |
---|
70 | if (found == stream.pos && !curInner.parseDelimiters) { |
---|
71 | stream.match(curInner.close); |
---|
72 | state.innerActive = state.inner = null; |
---|
73 | return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close"); |
---|
74 | } |
---|
75 | if (found > -1) stream.string = oldContent.slice(0, found); |
---|
76 | var innerToken = curInner.mode.token(stream, state.inner); |
---|
77 | if (found > -1) stream.string = oldContent; |
---|
78 | |
---|
79 | if (found == stream.pos && curInner.parseDelimiters) |
---|
80 | state.innerActive = state.inner = null; |
---|
81 | |
---|
82 | if (curInner.innerStyle) { |
---|
83 | if (innerToken) innerToken = innerToken + " " + curInner.innerStyle; |
---|
84 | else innerToken = curInner.innerStyle; |
---|
85 | } |
---|
86 | |
---|
87 | return innerToken; |
---|
88 | } |
---|
89 | }, |
---|
90 | |
---|
91 | indent: function(state, textAfter) { |
---|
92 | var mode = state.innerActive ? state.innerActive.mode : outer; |
---|
93 | if (!mode.indent) return CodeMirror.Pass; |
---|
94 | return mode.indent(state.innerActive ? state.inner : state.outer, textAfter); |
---|
95 | }, |
---|
96 | |
---|
97 | blankLine: function(state) { |
---|
98 | var mode = state.innerActive ? state.innerActive.mode : outer; |
---|
99 | if (mode.blankLine) { |
---|
100 | mode.blankLine(state.innerActive ? state.inner : state.outer); |
---|
101 | } |
---|
102 | if (!state.innerActive) { |
---|
103 | for (var i = 0; i < others.length; ++i) { |
---|
104 | var other = others[i]; |
---|
105 | if (other.open === "\n") { |
---|
106 | state.innerActive = other; |
---|
107 | state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0); |
---|
108 | } |
---|
109 | } |
---|
110 | } else if (state.innerActive.close === "\n") { |
---|
111 | state.innerActive = state.inner = null; |
---|
112 | } |
---|
113 | }, |
---|
114 | |
---|
115 | electricChars: outer.electricChars, |
---|
116 | |
---|
117 | innerMode: function(state) { |
---|
118 | return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer}; |
---|
119 | } |
---|
120 | }; |
---|
121 | }; |
---|
122 | |
---|
123 | }); |
---|