Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge remote-tracking branch 'jpallen/latex_highlighting' into gh-pages

Conflicts:
	demo/demo.js
	kitchen-sink.html
  • Loading branch information...
commit aaa7abe505fbc993df659dcda55beea5d13d82ec 2 parents f80b18e + 7ab31f2
Fabian Jakobs fjakobs authored
11 demo/demo.js
@@ -72,7 +72,7 @@ var TextileMode = require("ace/mode/textile").Mode;
72 72 var TextMode = require("ace/mode/text").Mode;
73 73 var GroovyMode = require("ace/mode/groovy").Mode;
74 74 var ScalaMode = require("ace/mode/scala").Mode;
75   -
  75 +var LatexMode = require("ace/mode/latex").Mode;
76 76 var UndoManager = require("ace/undomanager").UndoManager;
77 77
78 78 var vim = require("ace/keyboard/keybinding/vim").Vim;
@@ -197,6 +197,9 @@ exports.launch = function(env) {
197 197 docs.scala.setMode(new ScalaMode());
198 198 docs.scala.setUndoManager(new UndoManager());
199 199
  200 + docs.latex = new EditSession(require("ace/requirejs/text!demo/docs/latex.tex"));
  201 + docs.latex.setMode(new LatexMode());
  202 + docs.latex.setUndoManager(new UndoManager());
200 203
201 204 // Add a "name" property to all docs
202 205 for (var doc in docs) {
@@ -241,7 +244,8 @@ exports.launch = function(env) {
241 244 ocaml: new OcamlMode(),
242 245 csharp: new CSharpMode(),
243 246 groovy: new GroovyMode(),
244   - scala: new ScalaMode()
  247 + scala: new ScalaMode(),
  248 + latex: new LatexMode()
245 249 };
246 250
247 251 function getMode() {
@@ -345,6 +349,9 @@ exports.launch = function(env) {
345 349 else if (mode instanceof ScalaMode) {
346 350 modeEl.value = "scala";
347 351 }
  352 + else if (mode instanceof LatexMode) {
  353 + modeEl.value = "latex";
  354 + }
348 355 else {
349 356 modeEl.value = "text";
350 357 }
2  kitchen-sink.html
@@ -38,6 +38,7 @@
38 38 <option value="textile">Textile</option>
39 39 <option value="groovy">Groovy</option>
40 40 <option value="scala">Scala</option>
  41 + <option value="latex">LaTeX</option>
41 42 </select>
42 43 </td>
43 44 </tr>
@@ -69,6 +70,7 @@
69 70 <option value="textile">Textile</option>
70 71 <option value="groovy">Groovy</option>
71 72 <option value="scala">Scala</option>
  73 + <option value="latex">LaTeX</option>
72 74 </select>
73 75 </td>
74 76 </tr>
58 lib/ace/mode/latex.js
... ... @@ -0,0 +1,58 @@
  1 +define(function(require, exports, module) {
  2 +
  3 +var oop = require("pilot/oop");
  4 +var TextMode = require("ace/mode/text").Mode;
  5 +var Tokenizer = require("ace/tokenizer").Tokenizer;
  6 +var LatexHighlightRules = require("ace/mode/latex_highlight_rules").LatexHighlightRules;
  7 +var Range = require("ace/range").Range;
  8 +
  9 +var Mode = function()
  10 +{
  11 + this.$tokenizer = new Tokenizer(new LatexHighlightRules().getRules());
  12 +};
  13 +oop.inherits(Mode, TextMode);
  14 +
  15 +(function()
  16 +{
  17 + this.toggleCommentLines = function(state, doc, startRow, endRow) {
  18 + // This code is adapted from ruby.js
  19 + var outdent = true;
  20 + var outentedRows = [];
  21 +
  22 + // LaTeX comments begin with % and go to the end of the line
  23 + var commentRegEx = /^(\s*)\%/;
  24 +
  25 + for (var i = startRow; i <= endRow; i++) {
  26 + if (!commentRegEx.test(doc.getLine(i))) {
  27 + outdent = false;
  28 + break;
  29 + }
  30 + }
  31 +
  32 + if (outdent) {
  33 + var deleteRange = new Range(0, 0, 0, 0);
  34 + for (var i = startRow; i <= endRow; i++) {
  35 + var line = doc.getLine(i);
  36 + var m = line.match(commentRegEx);
  37 + deleteRange.start.row = i;
  38 + deleteRange.end.row = i;
  39 + deleteRange.end.column = m[0].length;
  40 + doc.replace(deleteRange, m[1]);
  41 + }
  42 + }
  43 + else {
  44 + doc.indentRows(startRow, endRow, "%");
  45 + }
  46 + };
  47 +
  48 + // There is no universally accepted way of indenting a tex document
  49 + // so just maintain the indentation of the previous line
  50 + this.getNextLineIndent = function(state, line, tab) {
  51 + return this.$getIndent(line);
  52 + };
  53 +
  54 +}).call(Mode.prototype);
  55 +
  56 +exports.Mode = Mode;
  57 +
  58 +});
39 lib/ace/mode/latex_highlight_rules.js
... ... @@ -0,0 +1,39 @@
  1 +define(function(require, exports, module) {
  2 +
  3 +var oop = require("pilot/oop");
  4 +var TextHighlightRules = require("ace/mode/text_highlight_rules").TextHighlightRules;
  5 +
  6 +var LatexHighlightRules = function()
  7 +{
  8 + this.$rules = {
  9 + "start" : [
  10 + {
  11 + // A tex command e.g. \foo
  12 + token : "keyword",
  13 + regex : "\\\\(?:[^a-zA-Z]|[a-zA-Z]+)",
  14 + },
  15 + {
  16 + // Curly and square braces
  17 + token : "keyword",
  18 + regex : "[\\{\\}\\]\\[]"
  19 + },
  20 + {
  21 + // Inline math between two $ symbols
  22 + token : "string",
  23 + regex : "\\$(?:(?:\\\\.)|(?:[^\\$\\\\]))*?\\$"
  24 + },
  25 + {
  26 + // A comment. Tex comments start with % and go to
  27 + // the end of the line
  28 + token : "comment",
  29 + regex : "%.*$"
  30 + }
  31 + ]
  32 + };
  33 +};
  34 +
  35 +oop.inherits(LatexHighlightRules, TextHighlightRules);
  36 +
  37 +exports.LatexHighlightRules = LatexHighlightRules;
  38 +
  39 +});

0 comments on commit aaa7abe

Please sign in to comment.
Something went wrong with that request. Please try again.