1 |
|
2 |
|
3 |
|
4 | (function(mod) {
|
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);
|
9 | else
|
10 | mod(CodeMirror);
|
11 | })(function(CodeMirror) {
|
12 | "use strict";
|
13 |
|
14 | CodeMirror.multiplexingMode = function(outer /*, others */) {
|
15 |
|
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 |
|
54 |
|
55 | var outerIndent = 0;
|
56 | if (outer.indent) {
|
57 | var possibleOuterIndent = outer.indent(state.outer, "", "");
|
58 | if (possibleOuterIndent !== CodeMirror.Pass) outerIndent = possibleOuterIndent;
|
59 | }
|
60 |
|
61 | state.inner = CodeMirror.startState(other.mode, outerIndent);
|
62 | return other.delimStyle && (other.delimStyle + " " + other.delimStyle + "-open");
|
63 | } else if (found != -1 && found < cutOff) {
|
64 | cutOff = found;
|
65 | }
|
66 | }
|
67 | if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);
|
68 | var outerToken = outer.token(stream, state.outer);
|
69 | if (cutOff != Infinity) stream.string = oldContent;
|
70 | return outerToken;
|
71 | } else {
|
72 | var curInner = state.innerActive, oldContent = stream.string;
|
73 | if (!curInner.close && stream.sol()) {
|
74 | state.innerActive = state.inner = null;
|
75 | return this.token(stream, state);
|
76 | }
|
77 | var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
|
78 | if (found == stream.pos && !curInner.parseDelimiters) {
|
79 | stream.match(curInner.close);
|
80 | state.innerActive = state.inner = null;
|
81 | return curInner.delimStyle && (curInner.delimStyle + " " + curInner.delimStyle + "-close");
|
82 | }
|
83 | if (found > -1) stream.string = oldContent.slice(0, found);
|
84 | var innerToken = curInner.mode.token(stream, state.inner);
|
85 | if (found > -1) stream.string = oldContent;
|
86 |
|
87 | if (found == stream.pos && curInner.parseDelimiters)
|
88 | state.innerActive = state.inner = null;
|
89 |
|
90 | if (curInner.innerStyle) {
|
91 | if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;
|
92 | else innerToken = curInner.innerStyle;
|
93 | }
|
94 |
|
95 | return innerToken;
|
96 | }
|
97 | },
|
98 |
|
99 | indent: function(state, textAfter, line) {
|
100 | var mode = state.innerActive ? state.innerActive.mode : outer;
|
101 | if (!mode.indent) return CodeMirror.Pass;
|
102 | return mode.indent(state.innerActive ? state.inner : state.outer, textAfter, line);
|
103 | },
|
104 |
|
105 | blankLine: function(state) {
|
106 | var mode = state.innerActive ? state.innerActive.mode : outer;
|
107 | if (mode.blankLine) {
|
108 | mode.blankLine(state.innerActive ? state.inner : state.outer);
|
109 | }
|
110 | if (!state.innerActive) {
|
111 | for (var i = 0; i < others.length; ++i) {
|
112 | var other = others[i];
|
113 | if (other.open === "\n") {
|
114 | state.innerActive = other;
|
115 | state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "", "") : 0);
|
116 | }
|
117 | }
|
118 | } else if (state.innerActive.close === "\n") {
|
119 | state.innerActive = state.inner = null;
|
120 | }
|
121 | },
|
122 |
|
123 | electricChars: outer.electricChars,
|
124 |
|
125 | innerMode: function(state) {
|
126 | return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer};
|
127 | }
|
128 | };
|
129 | };
|
130 |
|
131 | });
|