stex.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Author: Constantin Jucovschi (c.jucovschi@jacobs-university.de)
  3. * Licence: MIT
  4. */
  5. (function(mod) {
  6. if (typeof exports == "object" && typeof module == "object") // CommonJS
  7. mod(require("../../lib/codemirror"));
  8. else if (typeof define == "function" && define.amd) // AMD
  9. define(["../../lib/codemirror"], mod);
  10. else // Plain browser env
  11. mod(CodeMirror);
  12. })(function(CodeMirror) {
  13. "use strict";
  14. CodeMirror.defineMode("stex", function() {
  15. "use strict";
  16. function pushCommand(state, command) {
  17. state.cmdState.push(command);
  18. }
  19. function peekCommand(state) {
  20. if (state.cmdState.length > 0) {
  21. return state.cmdState[state.cmdState.length - 1];
  22. } else {
  23. return null;
  24. }
  25. }
  26. function popCommand(state) {
  27. var plug = state.cmdState.pop();
  28. if (plug) {
  29. plug.closeBracket();
  30. }
  31. }
  32. // returns the non-default plugin closest to the end of the list
  33. function getMostPowerful(state) {
  34. var context = state.cmdState;
  35. for (var i = context.length - 1; i >= 0; i--) {
  36. var plug = context[i];
  37. if (plug.name == "DEFAULT") {
  38. continue;
  39. }
  40. return plug;
  41. }
  42. return { styleIdentifier: function() { return null; } };
  43. }
  44. function addPluginPattern(pluginName, cmdStyle, styles) {
  45. return function () {
  46. this.name = pluginName;
  47. this.bracketNo = 0;
  48. this.style = cmdStyle;
  49. this.styles = styles;
  50. this.argument = null; // \begin and \end have arguments that follow. These are stored in the plugin
  51. this.styleIdentifier = function() {
  52. return this.styles[this.bracketNo - 1] || null;
  53. };
  54. this.openBracket = function() {
  55. this.bracketNo++;
  56. return "bracket";
  57. };
  58. this.closeBracket = function() {};
  59. };
  60. }
  61. var plugins = {};
  62. plugins["importmodule"] = addPluginPattern("importmodule", "tag", ["string", "builtin"]);
  63. plugins["documentclass"] = addPluginPattern("documentclass", "tag", ["", "atom"]);
  64. plugins["usepackage"] = addPluginPattern("usepackage", "tag", ["atom"]);
  65. plugins["begin"] = addPluginPattern("begin", "tag", ["atom"]);
  66. plugins["end"] = addPluginPattern("end", "tag", ["atom"]);
  67. plugins["DEFAULT"] = function () {
  68. this.name = "DEFAULT";
  69. this.style = "tag";
  70. this.styleIdentifier = this.openBracket = this.closeBracket = function() {};
  71. };
  72. function setState(state, f) {
  73. state.f = f;
  74. }
  75. // called when in a normal (no environment) context
  76. function normal(source, state) {
  77. var plug;
  78. // Do we look like '\command' ? If so, attempt to apply the plugin 'command'
  79. if (source.match(/^\\[a-zA-Z@]+/)) {
  80. var cmdName = source.current().slice(1);
  81. plug = plugins[cmdName] || plugins["DEFAULT"];
  82. plug = new plug();
  83. pushCommand(state, plug);
  84. setState(state, beginParams);
  85. return plug.style;
  86. }
  87. // escape characters
  88. if (source.match(/^\\[$&%#{}_]/)) {
  89. return "tag";
  90. }
  91. // white space control characters
  92. if (source.match(/^\\[,;!\/\\]/)) {
  93. return "tag";
  94. }
  95. // find if we're starting various math modes
  96. if (source.match("\\[")) {
  97. setState(state, function(source, state){ return inMathMode(source, state, "\\]"); });
  98. return "keyword";
  99. }
  100. if (source.match("$$")) {
  101. setState(state, function(source, state){ return inMathMode(source, state, "$$"); });
  102. return "keyword";
  103. }
  104. if (source.match("$")) {
  105. setState(state, function(source, state){ return inMathMode(source, state, "$"); });
  106. return "keyword";
  107. }
  108. var ch = source.next();
  109. if (ch == "%") {
  110. // special case: % at end of its own line; stay in same state
  111. if (!source.eol()) {
  112. setState(state, inCComment);
  113. }
  114. return "comment";
  115. }
  116. else if (ch == '}' || ch == ']') {
  117. plug = peekCommand(state);
  118. if (plug) {
  119. plug.closeBracket(ch);
  120. setState(state, beginParams);
  121. } else {
  122. return "error";
  123. }
  124. return "bracket";
  125. } else if (ch == '{' || ch == '[') {
  126. plug = plugins["DEFAULT"];
  127. plug = new plug();
  128. pushCommand(state, plug);
  129. return "bracket";
  130. }
  131. else if (/\d/.test(ch)) {
  132. source.eatWhile(/[\w.%]/);
  133. return "atom";
  134. }
  135. else {
  136. source.eatWhile(/[\w\-_]/);
  137. plug = getMostPowerful(state);
  138. if (plug.name == 'begin') {
  139. plug.argument = source.current();
  140. }
  141. return plug.styleIdentifier();
  142. }
  143. }
  144. function inCComment(source, state) {
  145. source.skipToEnd();
  146. setState(state, normal);
  147. return "comment";
  148. }
  149. function inMathMode(source, state, endModeSeq) {
  150. if (source.eatSpace()) {
  151. return null;
  152. }
  153. if (source.match(endModeSeq)) {
  154. setState(state, normal);
  155. return "keyword";
  156. }
  157. if (source.match(/^\\[a-zA-Z@]+/)) {
  158. return "tag";
  159. }
  160. if (source.match(/^[a-zA-Z]+/)) {
  161. return "variable-2";
  162. }
  163. // escape characters
  164. if (source.match(/^\\[$&%#{}_]/)) {
  165. return "tag";
  166. }
  167. // white space control characters
  168. if (source.match(/^\\[,;!\/]/)) {
  169. return "tag";
  170. }
  171. // special math-mode characters
  172. if (source.match(/^[\^_&]/)) {
  173. return "tag";
  174. }
  175. // non-special characters
  176. if (source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) {
  177. return null;
  178. }
  179. if (source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) {
  180. return "number";
  181. }
  182. var ch = source.next();
  183. if (ch == "{" || ch == "}" || ch == "[" || ch == "]" || ch == "(" || ch == ")") {
  184. return "bracket";
  185. }
  186. // eat comments here, because inCComment returns us to normal state!
  187. if (ch == "%") {
  188. if (!source.eol()) {
  189. source.skipToEnd();
  190. }
  191. return "comment";
  192. }
  193. return "error";
  194. }
  195. function beginParams(source, state) {
  196. var ch = source.peek(), lastPlug;
  197. if (ch == '{' || ch == '[') {
  198. lastPlug = peekCommand(state);
  199. lastPlug.openBracket(ch);
  200. source.eat(ch);
  201. setState(state, normal);
  202. return "bracket";
  203. }
  204. if (/[ \t\r]/.test(ch)) {
  205. source.eat(ch);
  206. return null;
  207. }
  208. setState(state, normal);
  209. popCommand(state);
  210. return normal(source, state);
  211. }
  212. return {
  213. startState: function() {
  214. return {
  215. cmdState: [],
  216. f: normal
  217. };
  218. },
  219. copyState: function(s) {
  220. return {
  221. cmdState: s.cmdState.slice(),
  222. f: s.f
  223. };
  224. },
  225. token: function(stream, state) {
  226. return state.f(stream, state);
  227. }
  228. };
  229. });
  230. CodeMirror.defineMIME("text/x-stex", "stex");
  231. CodeMirror.defineMIME("text/x-latex", "stex");
  232. });