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 | var n_others = others.length; |
---|
18 | |
---|
19 | function indexOf(string, pattern, from) { |
---|
20 | if (typeof pattern == "string") return string.indexOf(pattern, from); |
---|
21 | var m = pattern.exec(from ? string.slice(from) : string); |
---|
22 | return m ? m.index + from : -1; |
---|
23 | } |
---|
24 | |
---|
25 | return { |
---|
26 | startState: function() { |
---|
27 | return { |
---|
28 | outer: CodeMirror.startState(outer), |
---|
29 | innerActive: null, |
---|
30 | inner: null |
---|
31 | }; |
---|
32 | }, |
---|
33 | |
---|
34 | copyState: function(state) { |
---|
35 | return { |
---|
36 | outer: CodeMirror.copyState(outer, state.outer), |
---|
37 | innerActive: state.innerActive, |
---|
38 | inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner) |
---|
39 | }; |
---|
40 | }, |
---|
41 | |
---|
42 | token: function(stream, state) { |
---|
43 | if (!state.innerActive) { |
---|
44 | var cutOff = Infinity, oldContent = stream.string; |
---|
45 | for (var i = 0; i < n_others; ++i) { |
---|
46 | var other = others[i]; |
---|
47 | var found = indexOf(oldContent, other.open, stream.pos); |
---|
48 | if (found == stream.pos) { |
---|
49 | stream.match(other.open); |
---|
50 | state.innerActive = other; |
---|
51 | state.inner = CodeMirror.startState(other.mode, outer.indent ? outer.indent(state.outer, "") : 0); |
---|
52 | return other.delimStyle; |
---|
53 | } else if (found != -1 && found < cutOff) { |
---|
54 | cutOff = found; |
---|
55 | } |
---|
56 | } |
---|
57 | if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff); |
---|
58 | var outerToken = outer.token(stream, state.outer); |
---|
59 | if (cutOff != Infinity) stream.string = oldContent; |
---|
60 | return outerToken; |
---|
61 | } else { |
---|
62 | var curInner = state.innerActive, oldContent = stream.string; |
---|
63 | if (!curInner.close && stream.sol()) { |
---|
64 | state.innerActive = state.inner = null; |
---|
65 | return this.token(stream, state); |
---|
66 | } |
---|
67 | var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos) : -1; |
---|
68 | if (found == stream.pos) { |
---|
69 | stream.match(curInner.close); |
---|
70 | state.innerActive = state.inner = null; |
---|
71 | return curInner.delimStyle; |
---|
72 | } |
---|
73 | if (found > -1) stream.string = oldContent.slice(0, found); |
---|
74 | var innerToken = curInner.mode.token(stream, state.inner); |
---|
75 | if (found > -1) stream.string = oldContent; |
---|
76 | |
---|
77 | if (curInner.innerStyle) { |
---|
78 | if (innerToken) innerToken = innerToken + ' ' + curInner.innerStyle; |
---|
79 | else innerToken = curInner.innerStyle; |
---|
80 | } |
---|
81 | |
---|
82 | return innerToken; |
---|
83 | } |
---|
84 | }, |
---|
85 | |
---|
86 | indent: function(state, textAfter) { |
---|
87 | var mode = state.innerActive ? state.innerActive.mode : outer; |
---|
88 | if (!mode.indent) return CodeMirror.Pass; |
---|
89 | return mode.indent(state.innerActive ? state.inner : state.outer, textAfter); |
---|
90 | }, |
---|
91 | |
---|
92 | blankLine: function(state) { |
---|
93 | var mode = state.innerActive ? state.innerActive.mode : outer; |
---|
94 | if (mode.blankLine) { |
---|
95 | mode.blankLine(state.innerActive ? state.inner : state.outer); |
---|
96 | } |
---|
97 | if (!state.innerActive) { |
---|
98 | for (var i = 0; i < n_others; ++i) { |
---|
99 | var other = others[i]; |
---|
100 | if (other.open === "\n") { |
---|
101 | state.innerActive = other; |
---|
102 | state.inner = CodeMirror.startState(other.mode, mode.indent ? mode.indent(state.outer, "") : 0); |
---|
103 | } |
---|
104 | } |
---|
105 | } else if (state.innerActive.close === "\n") { |
---|
106 | state.innerActive = state.inner = null; |
---|
107 | } |
---|
108 | }, |
---|
109 | |
---|
110 | electricChars: outer.electricChars, |
---|
111 | |
---|
112 | innerMode: function(state) { |
---|
113 | return state.inner ? {state: state.inner, mode: state.innerActive.mode} : {state: state.outer, mode: outer}; |
---|
114 | } |
---|
115 | }; |
---|
116 | }; |
---|
117 | |
---|
118 | }); |
---|