mode-latex.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ace.define("ace/mode/latex_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var LatexHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [{
  8. token : "comment",
  9. regex : "%.*$"
  10. }, {
  11. token : ["keyword", "lparen", "variable.parameter", "rparen", "lparen", "storage.type", "rparen"],
  12. regex : "(\\\\(?:documentclass|usepackage|input))(?:(\\[)([^\\]]*)(\\]))?({)([^}]*)(})"
  13. }, {
  14. token : ["keyword","lparen", "variable.parameter", "rparen"],
  15. regex : "(\\\\(?:label|v?ref|cite(?:[^{]*)))(?:({)([^}]*)(}))?"
  16. }, {
  17. token : ["storage.type", "lparen", "variable.parameter", "rparen"],
  18. regex : "(\\\\begin)({)(verbatim)(})",
  19. next : "verbatim"
  20. }, {
  21. token : ["storage.type", "lparen", "variable.parameter", "rparen"],
  22. regex : "(\\\\begin)({)(lstlisting)(})",
  23. next : "lstlisting"
  24. }, {
  25. token : ["storage.type", "lparen", "variable.parameter", "rparen"],
  26. regex : "(\\\\(?:begin|end))({)([\\w*]*)(})"
  27. }, {
  28. token : "storage.type",
  29. regex : /\\verb\b\*?/,
  30. next : [{
  31. token : ["keyword.operator", "string", "keyword.operator"],
  32. regex : "(.)(.*?)(\\1|$)|",
  33. next : "start"
  34. }]
  35. }, {
  36. token : "storage.type",
  37. regex : "\\\\[a-zA-Z]+"
  38. }, {
  39. token : "lparen",
  40. regex : "[[({]"
  41. }, {
  42. token : "rparen",
  43. regex : "[\\])}]"
  44. }, {
  45. token : "constant.character.escape",
  46. regex : "\\\\[^a-zA-Z]?"
  47. }, {
  48. token : "string",
  49. regex : "\\${1,2}",
  50. next : "equation"
  51. }],
  52. "equation" : [{
  53. token : "comment",
  54. regex : "%.*$"
  55. }, {
  56. token : "string",
  57. regex : "\\${1,2}",
  58. next : "start"
  59. }, {
  60. token : "constant.character.escape",
  61. regex : "\\\\(?:[^a-zA-Z]|[a-zA-Z]+)"
  62. }, {
  63. token : "error",
  64. regex : "^\\s*$",
  65. next : "start"
  66. }, {
  67. defaultToken : "string"
  68. }],
  69. "verbatim": [{
  70. token : ["storage.type", "lparen", "variable.parameter", "rparen"],
  71. regex : "(\\\\end)({)(verbatim)(})",
  72. next : "start"
  73. }, {
  74. defaultToken : "text"
  75. }],
  76. "lstlisting": [{
  77. token : ["storage.type", "lparen", "variable.parameter", "rparen"],
  78. regex : "(\\\\end)({)(lstlisting)(})",
  79. next : "start"
  80. }, {
  81. defaultToken : "text"
  82. }]
  83. };
  84. this.normalizeRules();
  85. };
  86. oop.inherits(LatexHighlightRules, TextHighlightRules);
  87. exports.LatexHighlightRules = LatexHighlightRules;
  88. });
  89. ace.define("ace/mode/folding/latex",[], function(require, exports, module) {
  90. "use strict";
  91. var oop = require("../../lib/oop");
  92. var BaseFoldMode = require("./fold_mode").FoldMode;
  93. var Range = require("../../range").Range;
  94. var TokenIterator = require("../../token_iterator").TokenIterator;
  95. var keywordLevels = {
  96. "\\subparagraph": 1,
  97. "\\paragraph": 2,
  98. "\\subsubsubsection": 3,
  99. "\\subsubsection": 4,
  100. "\\subsection": 5,
  101. "\\section": 6,
  102. "\\chapter": 7,
  103. "\\part": 8,
  104. "\\begin": 9,
  105. "\\end": 10
  106. };
  107. var FoldMode = exports.FoldMode = function() {};
  108. oop.inherits(FoldMode, BaseFoldMode);
  109. (function() {
  110. this.foldingStartMarker = /^\s*\\(begin)|\s*\\(part|chapter|(?:sub)*(?:section|paragraph))\b|{\s*$/;
  111. this.foldingStopMarker = /^\s*\\(end)\b|^\s*}/;
  112. this.getFoldWidgetRange = function(session, foldStyle, row) {
  113. var line = session.doc.getLine(row);
  114. var match = this.foldingStartMarker.exec(line);
  115. if (match) {
  116. if (match[1])
  117. return this.latexBlock(session, row, match[0].length - 1);
  118. if (match[2])
  119. return this.latexSection(session, row, match[0].length - 1);
  120. return this.openingBracketBlock(session, "{", row, match.index);
  121. }
  122. var match = this.foldingStopMarker.exec(line);
  123. if (match) {
  124. if (match[1])
  125. return this.latexBlock(session, row, match[0].length - 1);
  126. return this.closingBracketBlock(session, "}", row, match.index + match[0].length);
  127. }
  128. };
  129. this.latexBlock = function(session, row, column, returnRange) {
  130. var keywords = {
  131. "\\begin": 1,
  132. "\\end": -1
  133. };
  134. var stream = new TokenIterator(session, row, column);
  135. var token = stream.getCurrentToken();
  136. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  137. return;
  138. var val = token.value;
  139. var dir = keywords[val];
  140. var getType = function() {
  141. var token = stream.stepForward();
  142. var type = token.type == "lparen" ?stream.stepForward().value : "";
  143. if (dir === -1) {
  144. stream.stepBackward();
  145. if (type)
  146. stream.stepBackward();
  147. }
  148. return type;
  149. };
  150. var stack = [getType()];
  151. var startColumn = dir === -1 ? stream.getCurrentTokenColumn() : session.getLine(row).length;
  152. var startRow = row;
  153. stream.step = dir === -1 ? stream.stepBackward : stream.stepForward;
  154. while(token = stream.step()) {
  155. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  156. continue;
  157. var level = keywords[token.value];
  158. if (!level)
  159. continue;
  160. var type = getType();
  161. if (level === dir)
  162. stack.unshift(type);
  163. else if (stack.shift() !== type || !stack.length)
  164. break;
  165. }
  166. if (stack.length)
  167. return;
  168. if (dir == 1) {
  169. stream.stepBackward();
  170. stream.stepBackward();
  171. }
  172. if (returnRange)
  173. return stream.getCurrentTokenRange();
  174. var row = stream.getCurrentTokenRow();
  175. if (dir === -1)
  176. return new Range(row, session.getLine(row).length, startRow, startColumn);
  177. else
  178. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  179. };
  180. this.latexSection = function(session, row, column) {
  181. var stream = new TokenIterator(session, row, column);
  182. var token = stream.getCurrentToken();
  183. if (!token || token.type != "storage.type")
  184. return;
  185. var startLevel = keywordLevels[token.value] || 0;
  186. var stackDepth = 0;
  187. var endRow = row;
  188. while(token = stream.stepForward()) {
  189. if (token.type !== "storage.type")
  190. continue;
  191. var level = keywordLevels[token.value] || 0;
  192. if (level >= 9) {
  193. if (!stackDepth)
  194. endRow = stream.getCurrentTokenRow() - 1;
  195. stackDepth += level == 9 ? 1 : - 1;
  196. if (stackDepth < 0)
  197. break;
  198. } else if (level >= startLevel)
  199. break;
  200. }
  201. if (!stackDepth)
  202. endRow = stream.getCurrentTokenRow() - 1;
  203. while (endRow > row && !/\S/.test(session.getLine(endRow)))
  204. endRow--;
  205. return new Range(
  206. row, session.getLine(row).length,
  207. endRow, session.getLine(endRow).length
  208. );
  209. };
  210. }).call(FoldMode.prototype);
  211. });
  212. ace.define("ace/mode/latex",[], function(require, exports, module) {
  213. "use strict";
  214. var oop = require("../lib/oop");
  215. var TextMode = require("./text").Mode;
  216. var LatexHighlightRules = require("./latex_highlight_rules").LatexHighlightRules;
  217. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  218. var LatexFoldMode = require("./folding/latex").FoldMode;
  219. var Mode = function() {
  220. this.HighlightRules = LatexHighlightRules;
  221. this.foldingRules = new LatexFoldMode();
  222. this.$behaviour = new CstyleBehaviour({ braces: true });
  223. };
  224. oop.inherits(Mode, TextMode);
  225. (function() {
  226. this.type = "text";
  227. this.lineCommentStart = "%";
  228. this.$id = "ace/mode/latex";
  229. this.getMatching = function(session, row, column) {
  230. if (row == undefined)
  231. row = session.selection.lead;
  232. if (typeof row == "object") {
  233. column = row.column;
  234. row = row.row;
  235. }
  236. var startToken = session.getTokenAt(row, column);
  237. if (!startToken)
  238. return;
  239. if (startToken.value == "\\begin" || startToken.value == "\\end") {
  240. return this.foldingRules.latexBlock(session, row, column, true);
  241. }
  242. };
  243. }).call(Mode.prototype);
  244. exports.Mode = Mode;
  245. });
  246. (function() {
  247. ace.require(["ace/mode/latex"], function(m) {
  248. if (typeof module == "object" && typeof exports == "object" && module) {
  249. module.exports = m;
  250. }
  251. });
  252. })();