10
if (typeof exports == "object" && typeof module == "object")
11
mod(require("../../lib/codemirror"));
12
else if (typeof define == "function" && define.amd)
13
define(["../../lib/codemirror"], mod);
16
})(function(CodeMirror) {
19
CodeMirror.defineMode("stex", function(_config, parserConfig) {
22
function pushCommand(state, command) {
23
state.cmdState.push(command);
26
function peekCommand(state) {
27
if (state.cmdState.length > 0) {
28
return state.cmdState[state.cmdState.length - 1];
34
function popCommand(state) {
35
var plug = state.cmdState.pop();
42
function getMostPowerful(state) {
43
var context = state.cmdState;
44
for (var i = context.length - 1; i >= 0; i--) {
45
var plug = context[i];
46
if (plug.name == "DEFAULT") {
51
return { styleIdentifier: function() { return null; } };
54
function addPluginPattern(pluginName, cmdStyle, styles) {
56
this.name = pluginName;
58
this.style = cmdStyle;
62
this.styleIdentifier = function() {
63
return this.styles[this.bracketNo - 1] || null;
65
this.openBracket = function() {
69
this.closeBracket = function() {};
75
plugins["importmodule"] = addPluginPattern("importmodule", "tag", ["string", "builtin"]);
76
plugins["documentclass"] = addPluginPattern("documentclass", "tag", ["", "atom"]);
77
plugins["usepackage"] = addPluginPattern("usepackage", "tag", ["atom"]);
78
plugins["begin"] = addPluginPattern("begin", "tag", ["atom"]);
79
plugins["end"] = addPluginPattern("end", "tag", ["atom"]);
81
plugins["label" ] = addPluginPattern("label" , "tag", ["atom"]);
82
plugins["ref" ] = addPluginPattern("ref" , "tag", ["atom"]);
83
plugins["eqref" ] = addPluginPattern("eqref" , "tag", ["atom"]);
84
plugins["cite" ] = addPluginPattern("cite" , "tag", ["atom"]);
85
plugins["bibitem" ] = addPluginPattern("bibitem" , "tag", ["atom"]);
86
plugins["Bibitem" ] = addPluginPattern("Bibitem" , "tag", ["atom"]);
87
plugins["RBibitem" ] = addPluginPattern("RBibitem" , "tag", ["atom"]);
89
plugins["DEFAULT"] = function () {
90
this.name = "DEFAULT";
93
this.styleIdentifier = this.openBracket = this.closeBracket = function() {};
96
function setState(state, f) {
101
function normal(source, state) {
104
if (source.match(/^\\[a-zA-Z@]+/)) {
105
var cmdName = source.current().slice(1);
106
plug = plugins.hasOwnProperty(cmdName) ? plugins[cmdName] : plugins["DEFAULT"];
108
pushCommand(state, plug);
109
setState(state, beginParams);
114
if (source.match(/^\\[$&%#{}_]/)) {
119
if (source.match(/^\\[,;!\/\\]/)) {
124
if (source.match("\\[")) {
125
setState(state, function(source, state){ return inMathMode(source, state, "\\]"); });
128
if (source.match("\\(")) {
129
setState(state, function(source, state){ return inMathMode(source, state, "\\)"); });
132
if (source.match("$$")) {
133
setState(state, function(source, state){ return inMathMode(source, state, "$$"); });
136
if (source.match("$")) {
137
setState(state, function(source, state){ return inMathMode(source, state, "$"); });
141
var ch = source.next();
145
} else if (ch == '}' || ch == ']') {
146
plug = peekCommand(state);
148
plug.closeBracket(ch);
149
setState(state, beginParams);
154
} else if (ch == '{' || ch == '[') {
155
plug = plugins["DEFAULT"];
157
pushCommand(state, plug);
159
} else if (/\d/.test(ch)) {
160
source.eatWhile(/[\w.%]/);
163
source.eatWhile(/[\w\-_]/);
164
plug = getMostPowerful(state);
165
if (plug.name == 'begin') {
166
plug.argument = source.current();
168
return plug.styleIdentifier();
172
function inMathMode(source, state, endModeSeq) {
173
if (source.eatSpace()) {
176
if (endModeSeq && source.match(endModeSeq)) {
177
setState(state, normal);
180
if (source.match(/^\\[a-zA-Z@]+/)) {
183
if (source.match(/^[a-zA-Z]+/)) {
187
if (source.match(/^\\[$&%#{}_]/)) {
191
if (source.match(/^\\[,;!\/]/)) {
195
if (source.match(/^[\^_&]/)) {
199
if (source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) {
202
if (source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) {
205
var ch = source.next();
206
if (ch == "{" || ch == "}" || ch == "[" || ch == "]" || ch == "(" || ch == ")") {
217
function beginParams(source, state) {
218
var ch = source.peek(), lastPlug;
219
if (ch == '{' || ch == '[') {
220
lastPlug = peekCommand(state);
221
lastPlug.openBracket(ch);
223
setState(state, normal);
226
if (/[ \t\r]/.test(ch)) {
230
setState(state, normal);
233
return normal(source, state);
237
startState: function() {
238
var f = parserConfig.inMathMode ? function(source, state){ return inMathMode(source, state); } : normal;
244
copyState: function(s) {
246
cmdState: s.cmdState.slice(),
250
token: function(stream, state) {
251
return state.f(stream, state);
253
blankLine: function(state) {
255
state.cmdState.length = 0;
261
CodeMirror.defineMIME("text/x-stex", "stex");
262
CodeMirror.defineMIME("text/x-latex", "stex");