2 * \file LaTeXHighlighter.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
11 #include "LaTeXHighlighter.h"
14 #include <QTextDocument>
19 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
20 : QSyntaxHighlighter(parent)
22 keywordFormat.setForeground(Qt::darkBlue);
23 keywordFormat.setFontWeight(QFont::Bold);
24 commentFormat.setForeground(Qt::darkGray);
25 mathFormat.setForeground(Qt::red);
29 void LaTeXHighlighter::highlightBlock(QString const & text)
32 static const QRegExp exprMath("\\$[^\\$]*\\$");
33 int index = text.indexOf(exprMath);
35 int length = exprMath.matchedLength();
36 setFormat(index, length, mathFormat);
37 index = text.indexOf(exprMath, index + length);
40 static const QRegExp exprStartDispMath("(\\\\\\[|"
41 "\\\\begin\\{equation\\**\\}|"
42 "\\\\begin\\{eqnarray\\**\\}|"
43 "\\\\begin\\{align(ed|at)*\\**\\}|"
44 "\\\\begin\\{flalign\\**\\}|"
45 "\\\\begin\\{gather\\**\\}|"
46 "\\\\begin\\{multline\\**\\}|"
47 "\\\\begin\\{array\\**\\}|"
48 "\\\\begin\\{cases\\**\\}"
50 static const QRegExp exprEndDispMath("(\\\\\\]|"
51 "\\\\end\\{equation\\**\\}|"
52 "\\\\end\\{eqnarray\\**\\}|"
53 "\\\\end\\{align(ed|at)*\\**\\}|"
54 "\\\\end\\{flalign\\**\\}|"
55 "\\\\end\\{gather\\**\\}|"
56 "\\\\end\\{multline\\**\\}|"
57 "\\\\end\\{array\\**\\}|"
58 "\\\\end\\{cases\\**\\}"
61 // if previous block was in 'disp math'
62 // start search from 0 (for end disp math)
63 // otherwise, start search from 'begin disp math'
64 if (previousBlockState() != 1)
65 startIndex = text.indexOf(exprStartDispMath);
66 while (startIndex >= 0) {
67 int endIndex = text.indexOf(exprEndDispMath, startIndex);
70 setCurrentBlockState(1);
71 length = text.length() - startIndex;
73 length = endIndex - startIndex + exprEndDispMath.matchedLength();
75 setFormat(startIndex, length, mathFormat);
76 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
79 static const QRegExp exprKeyword("\\\\[A-Za-z]+");
80 index = text.indexOf(exprKeyword);
82 int length = exprKeyword.matchedLength();
83 setFormat(index, length, keywordFormat);
84 index = text.indexOf(exprKeyword, index + length);
87 static const QRegExp exprComment("(^|[^\\\\])%.*$");
88 index = text.indexOf(exprComment);
90 int const length = exprComment.matchedLength();
91 setFormat(index, length, commentFormat);
92 index = text.indexOf(exprComment, index + length);
96 } // namespace frontend