mode-latex.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ace.define("ace/mode/latex_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_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|end))({)(\\w*)(})"
  19. }, {
  20. token : "storage.type",
  21. regex : "\\\\[a-zA-Z]+"
  22. }, {
  23. token : "lparen",
  24. regex : "[[({]"
  25. }, {
  26. token : "rparen",
  27. regex : "[\\])}]"
  28. }, {
  29. token : "constant.character.escape",
  30. regex : "\\\\[^a-zA-Z]?"
  31. }, {
  32. token : "string",
  33. regex : "\\${1,2}",
  34. next : "equation"
  35. }],
  36. "equation" : [{
  37. token : "comment",
  38. regex : "%.*$"
  39. }, {
  40. token : "string",
  41. regex : "\\${1,2}",
  42. next : "start"
  43. }, {
  44. token : "constant.character.escape",
  45. regex : "\\\\(?:[^a-zA-Z]|[a-zA-Z]+)"
  46. }, {
  47. token : "error",
  48. regex : "^\\s*$",
  49. next : "start"
  50. }, {
  51. defaultToken : "string"
  52. }]
  53. };
  54. };
  55. oop.inherits(LatexHighlightRules, TextHighlightRules);
  56. exports.LatexHighlightRules = LatexHighlightRules;
  57. });
  58. ace.define("ace/mode/folding/latex",["require","exports","module","ace/lib/oop","ace/mode/folding/fold_mode","ace/range","ace/token_iterator"], function(require, exports, module) {
  59. "use strict";
  60. var oop = require("../../lib/oop");
  61. var BaseFoldMode = require("./fold_mode").FoldMode;
  62. var Range = require("../../range").Range;
  63. var TokenIterator = require("../../token_iterator").TokenIterator;
  64. var FoldMode = exports.FoldMode = function() {};
  65. oop.inherits(FoldMode, BaseFoldMode);
  66. (function() {
  67. this.foldingStartMarker = /^\s*\\(begin)|(section|subsection|paragraph)\b|{\s*$/;
  68. this.foldingStopMarker = /^\s*\\(end)\b|^\s*}/;
  69. this.getFoldWidgetRange = function(session, foldStyle, row) {
  70. var line = session.doc.getLine(row);
  71. var match = this.foldingStartMarker.exec(line);
  72. if (match) {
  73. if (match[1])
  74. return this.latexBlock(session, row, match[0].length - 1);
  75. if (match[2])
  76. return this.latexSection(session, row, match[0].length - 1);
  77. return this.openingBracketBlock(session, "{", row, match.index);
  78. }
  79. var match = this.foldingStopMarker.exec(line);
  80. if (match) {
  81. if (match[1])
  82. return this.latexBlock(session, row, match[0].length - 1);
  83. return this.closingBracketBlock(session, "}", row, match.index + match[0].length);
  84. }
  85. };
  86. this.latexBlock = function(session, row, column) {
  87. var keywords = {
  88. "\\begin": 1,
  89. "\\end": -1
  90. };
  91. var stream = new TokenIterator(session, row, column);
  92. var token = stream.getCurrentToken();
  93. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  94. return;
  95. var val = token.value;
  96. var dir = keywords[val];
  97. var getType = function() {
  98. var token = stream.stepForward();
  99. var type = token.type == "lparen" ?stream.stepForward().value : "";
  100. if (dir === -1) {
  101. stream.stepBackward();
  102. if (type)
  103. stream.stepBackward();
  104. }
  105. return type;
  106. };
  107. var stack = [getType()];
  108. var startColumn = dir === -1 ? stream.getCurrentTokenColumn() : session.getLine(row).length;
  109. var startRow = row;
  110. stream.step = dir === -1 ? stream.stepBackward : stream.stepForward;
  111. while(token = stream.step()) {
  112. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  113. continue;
  114. var level = keywords[token.value];
  115. if (!level)
  116. continue;
  117. var type = getType();
  118. if (level === dir)
  119. stack.unshift(type);
  120. else if (stack.shift() !== type || !stack.length)
  121. break;
  122. }
  123. if (stack.length)
  124. return;
  125. var row = stream.getCurrentTokenRow();
  126. if (dir === -1)
  127. return new Range(row, session.getLine(row).length, startRow, startColumn);
  128. stream.stepBackward();
  129. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  130. };
  131. this.latexSection = function(session, row, column) {
  132. var keywords = ["\\subsection", "\\section", "\\begin", "\\end", "\\paragraph"];
  133. var stream = new TokenIterator(session, row, column);
  134. var token = stream.getCurrentToken();
  135. if (!token || token.type != "storage.type")
  136. return;
  137. var startLevel = keywords.indexOf(token.value);
  138. var stackDepth = 0
  139. var endRow = row;
  140. while(token = stream.stepForward()) {
  141. if (token.type !== "storage.type")
  142. continue;
  143. var level = keywords.indexOf(token.value);
  144. if (level >= 2) {
  145. if (!stackDepth)
  146. endRow = stream.getCurrentTokenRow() - 1;
  147. stackDepth += level == 2 ? 1 : - 1;
  148. if (stackDepth < 0)
  149. break
  150. } else if (level >= startLevel)
  151. break;
  152. }
  153. if (!stackDepth)
  154. endRow = stream.getCurrentTokenRow() - 1;
  155. while (endRow > row && !/\S/.test(session.getLine(endRow)))
  156. endRow--;
  157. return new Range(
  158. row, session.getLine(row).length,
  159. endRow, session.getLine(endRow).length
  160. );
  161. };
  162. }).call(FoldMode.prototype);
  163. });
  164. ace.define("ace/mode/latex",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/latex_highlight_rules","ace/mode/folding/latex"], function(require, exports, module) {
  165. "use strict";
  166. var oop = require("../lib/oop");
  167. var TextMode = require("./text").Mode;
  168. var LatexHighlightRules = require("./latex_highlight_rules").LatexHighlightRules;
  169. var LatexFoldMode = require("./folding/latex").FoldMode;
  170. var Mode = function() {
  171. this.HighlightRules = LatexHighlightRules;
  172. this.foldingRules = new LatexFoldMode();
  173. this.$behaviour = this.$defaultBehaviour;
  174. };
  175. oop.inherits(Mode, TextMode);
  176. (function() {
  177. this.type = "text";
  178. this.lineCommentStart = "%";
  179. this.$id = "ace/mode/latex";
  180. }).call(Mode.prototype);
  181. exports.Mode = Mode;
  182. });