diff options
author | Valerio Virgillito | 2012-02-15 17:04:39 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-15 17:04:39 -0800 |
commit | fe71cb9aa1700199c0089166ad54fc56eb760644 (patch) | |
tree | b0c7c24b1f45a079e1c09312ca716e3d35aecad4 /imports/codemirror/mode/stex/stex.js | |
parent | 747616980cad14f0b65fbcc7f497ed9680a39d29 (diff) | |
parent | d366c0bd1af6471511217ed574083e15059519b5 (diff) | |
download | ninja-fe71cb9aa1700199c0089166ad54fc56eb760644.tar.gz |
Merge branch 'refs/heads/master' into components
Diffstat (limited to 'imports/codemirror/mode/stex/stex.js')
-rwxr-xr-x | imports/codemirror/mode/stex/stex.js | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/imports/codemirror/mode/stex/stex.js b/imports/codemirror/mode/stex/stex.js new file mode 100755 index 00000000..bb47fb45 --- /dev/null +++ b/imports/codemirror/mode/stex/stex.js | |||
@@ -0,0 +1,167 @@ | |||
1 | /* | ||
2 | * Author: Constantin Jucovschi (c.jucovschi@jacobs-university.de) | ||
3 | * Licence: MIT | ||
4 | */ | ||
5 | |||
6 | CodeMirror.defineMode("stex", function(cmCfg, modeCfg) | ||
7 | { | ||
8 | function pushCommand(state, command) { | ||
9 | state.cmdState.push(command); | ||
10 | } | ||
11 | |||
12 | function peekCommand(state) { | ||
13 | if (state.cmdState.length>0) | ||
14 | return state.cmdState[state.cmdState.length-1]; | ||
15 | else | ||
16 | return null; | ||
17 | } | ||
18 | |||
19 | function popCommand(state) { | ||
20 | if (state.cmdState.length>0) { | ||
21 | var plug = state.cmdState.pop(); | ||
22 | plug.closeBracket(); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | function applyMostPowerful(state) { | ||
27 | var context = state.cmdState; | ||
28 | for (var i = context.length - 1; i >= 0; i--) { | ||
29 | var plug = context[i]; | ||
30 | if (plug.name=="DEFAULT") | ||
31 | continue; | ||
32 | return plug.styleIdentifier(); | ||
33 | } | ||
34 | return null; | ||
35 | } | ||
36 | |||
37 | function addPluginPattern(pluginName, cmdStyle, brackets, styles) { | ||
38 | return function () { | ||
39 | this.name=pluginName; | ||
40 | this.bracketNo = 0; | ||
41 | this.style=cmdStyle; | ||
42 | this.styles = styles; | ||
43 | this.brackets = brackets; | ||
44 | |||
45 | this.styleIdentifier = function(content) { | ||
46 | if (this.bracketNo<=this.styles.length) | ||
47 | return this.styles[this.bracketNo-1]; | ||
48 | else | ||
49 | return null; | ||
50 | }; | ||
51 | this.openBracket = function(content) { | ||
52 | this.bracketNo++; | ||
53 | return "bracket"; | ||
54 | }; | ||
55 | this.closeBracket = function(content) { | ||
56 | }; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | var plugins = new Array(); | ||
61 | |||
62 | plugins["importmodule"] = addPluginPattern("importmodule", "tag", "{[", ["string", "builtin"]); | ||
63 | plugins["documentclass"] = addPluginPattern("documentclass", "tag", "{[", ["", "atom"]); | ||
64 | plugins["usepackage"] = addPluginPattern("documentclass", "tag", "[", ["atom"]); | ||
65 | plugins["begin"] = addPluginPattern("documentclass", "tag", "[", ["atom"]); | ||
66 | plugins["end"] = addPluginPattern("documentclass", "tag", "[", ["atom"]); | ||
67 | |||
68 | plugins["DEFAULT"] = function () { | ||
69 | this.name="DEFAULT"; | ||
70 | this.style="tag"; | ||
71 | |||
72 | this.styleIdentifier = function(content) { | ||
73 | }; | ||
74 | this.openBracket = function(content) { | ||
75 | }; | ||
76 | this.closeBracket = function(content) { | ||
77 | }; | ||
78 | }; | ||
79 | |||
80 | function setState(state, f) { | ||
81 | state.f = f; | ||
82 | } | ||
83 | |||
84 | function normal(source, state) { | ||
85 | if (source.match(/^\\[a-z]+/)) { | ||
86 | var cmdName = source.current(); | ||
87 | cmdName = cmdName.substr(1, cmdName.length-1); | ||
88 | var plug = plugins[cmdName]; | ||
89 | if (typeof(plug) == 'undefined') { | ||
90 | plug = plugins["DEFAULT"]; | ||
91 | } | ||
92 | plug = new plug(); | ||
93 | pushCommand(state, plug); | ||
94 | setState(state, beginParams); | ||
95 | return plug.style; | ||
96 | } | ||
97 | |||
98 | var ch = source.next(); | ||
99 | if (ch == "%") { | ||
100 | setState(state, inCComment); | ||
101 | return "comment"; | ||
102 | } | ||
103 | else if (ch=='}' || ch==']') { | ||
104 | plug = peekCommand(state); | ||
105 | if (plug) { | ||
106 | plug.closeBracket(ch); | ||
107 | setState(state, beginParams); | ||
108 | } else | ||
109 | return "error"; | ||
110 | return "bracket"; | ||
111 | } else if (ch=='{' || ch=='[') { | ||
112 | plug = plugins["DEFAULT"]; | ||
113 | plug = new plug(); | ||
114 | pushCommand(state, plug); | ||
115 | return "bracket"; | ||
116 | } | ||
117 | else if (/\d/.test(ch)) { | ||
118 | source.eatWhile(/[\w.%]/); | ||
119 | return "atom"; | ||
120 | } | ||
121 | else { | ||
122 | source.eatWhile(/[\w-_]/); | ||
123 | return applyMostPowerful(state); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | function inCComment(source, state) { | ||
128 | source.skipToEnd(); | ||
129 | setState(state, normal); | ||
130 | return "comment"; | ||
131 | } | ||
132 | |||
133 | function beginParams(source, state) { | ||
134 | var ch = source.peek(); | ||
135 | if (ch == '{' || ch == '[') { | ||
136 | var lastPlug = peekCommand(state); | ||
137 | var style = lastPlug.openBracket(ch); | ||
138 | source.eat(ch); | ||
139 | setState(state, normal); | ||
140 | return "bracket"; | ||
141 | } | ||
142 | if (/[ \t\r]/.test(ch)) { | ||
143 | source.eat(ch); | ||
144 | return null; | ||
145 | } | ||
146 | setState(state, normal); | ||
147 | lastPlug = peekCommand(state); | ||
148 | if (lastPlug) { | ||
149 | popCommand(state); | ||
150 | } | ||
151 | return normal(source, state); | ||
152 | } | ||
153 | |||
154 | return { | ||
155 | startState: function() { return { f:normal, cmdState:[] }; }, | ||
156 | copyState: function(s) { return { f: s.f, cmdState: s.cmdState.slice(0, s.cmdState.length) }; }, | ||
157 | |||
158 | token: function(stream, state) { | ||
159 | var t = state.f(stream, state); | ||
160 | var w = stream.current(); | ||
161 | return t; | ||
162 | } | ||
163 | }; | ||
164 | }); | ||
165 | |||
166 | |||
167 | CodeMirror.defineMIME("text/x-stex", "stex"); | ||