1 // Utility function that allows modes to be combined. The mode given
2 // as the base argument takes care of most of the normal mode
3 // functionality, but a second (typically simple) mode is used, which
4 // can override the style of text. Both modes get to parse all of the
5 // text, but when both assign a non-null style to a piece of code, the
6 // overlay wins, unless the combine argument was true, in which case
7 // the styles are combined.
10 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
11 mod(require("../../lib/codemirror"));
12 else if (typeof define
== "function" && define
.amd
) // AMD
13 define(["../../lib/codemirror"], mod
);
14 else // Plain browser env
16 })(function(CodeMirror
) {
19 CodeMirror
.overlayMode = function(base
, overlay
, combine
) {
21 startState: function() {
23 base
: CodeMirror
.startState(base
),
24 overlay
: CodeMirror
.startState(overlay
),
25 basePos
: 0, baseCur
: null,
26 overlayPos
: 0, overlayCur
: null
29 copyState: function(state
) {
31 base
: CodeMirror
.copyState(base
, state
.base
),
32 overlay
: CodeMirror
.copyState(overlay
, state
.overlay
),
33 basePos
: state
.basePos
, baseCur
: null,
34 overlayPos
: state
.overlayPos
, overlayCur
: null
38 token: function(stream
, state
) {
39 if (stream
.start
== state
.basePos
) {
40 state
.baseCur
= base
.token(stream
, state
.base
);
41 state
.basePos
= stream
.pos
;
43 if (stream
.start
== state
.overlayPos
) {
44 stream
.pos
= stream
.start
;
45 state
.overlayCur
= overlay
.token(stream
, state
.overlay
);
46 state
.overlayPos
= stream
.pos
;
48 stream
.pos
= Math
.min(state
.basePos
, state
.overlayPos
);
49 if (stream
.eol()) state
.basePos
= state
.overlayPos
= 0;
51 if (state
.overlayCur
== null) return state
.baseCur
;
52 if (state
.baseCur
!= null && combine
) return state
.baseCur
+ " " + state
.overlayCur
;
53 else return state
.overlayCur
;
56 indent
: base
.indent
&& function(state
, textAfter
) {
57 return base
.indent(state
.base
, textAfter
);
59 electricChars
: base
.electricChars
,
61 innerMode: function(state
) { return {state
: state
.base
, mode
: base
}; },
63 blankLine: function(state
) {
64 if (base
.blankLine
) base
.blankLine(state
.base
);
65 if (overlay
.blankLine
) overlay
.blankLine(state
.overlay
);