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.
13 #include "LaTeXHighlighter.h"
14 #include "qt_helpers.h"
17 #include <QTextDocument>
22 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
23 : QSyntaxHighlighter(parent)
25 keywordFormat.setForeground(Qt::darkBlue);
26 keywordFormat.setFontWeight(QFont::Bold);
27 commentFormat.setForeground(Qt::darkGray);
28 mathFormat.setForeground(Qt::red);
29 warningFormat.setForeground(Qt::red);
30 warningFormat.setFontWeight(QFont::Bold);
34 void LaTeXHighlighter::highlightBlock(QString const & text)
37 static const QRegExp exprMath("\\$[^\\$]*\\$");
38 int index = exprMath.indexIn(text);
40 int length = exprMath.matchedLength();
41 setFormat(index, length, mathFormat);
42 index = exprMath.indexIn(text, index + length);
45 static const QRegExp exprStartDispMath("(\\\\\\[|"
46 "\\\\begin\\{equation\\**\\}|"
47 "\\\\begin\\{eqnarray\\**\\}|"
48 "\\\\begin\\{align(ed|at)*\\**\\}|"
49 "\\\\begin\\{flalign\\**\\}|"
50 "\\\\begin\\{gather\\**\\}|"
51 "\\\\begin\\{multline\\**\\}|"
52 "\\\\begin\\{array\\**\\}|"
53 "\\\\begin\\{cases\\**\\}"
55 static const QRegExp exprEndDispMath("(\\\\\\]|"
56 "\\\\end\\{equation\\**\\}|"
57 "\\\\end\\{eqnarray\\**\\}|"
58 "\\\\end\\{align(ed|at)*\\**\\}|"
59 "\\\\end\\{flalign\\**\\}|"
60 "\\\\end\\{gather\\**\\}|"
61 "\\\\end\\{multline\\**\\}|"
62 "\\\\end\\{array\\**\\}|"
63 "\\\\end\\{cases\\**\\}"
66 // if previous block was in 'disp math'
67 // start search from 0 (for end disp math)
68 // otherwise, start search from 'begin disp math'
69 if (previousBlockState() != 1)
70 startIndex = exprStartDispMath.indexIn(text);
71 while (startIndex >= 0) {
72 int endIndex = exprEndDispMath.indexIn(text, startIndex);
75 setCurrentBlockState(1);
76 length = text.length() - startIndex;
78 length = endIndex - startIndex + exprEndDispMath.matchedLength();
80 setFormat(startIndex, length, mathFormat);
81 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
84 static const QRegExp exprKeyword("\\\\[A-Za-z]+");
85 index = exprKeyword.indexIn(text);
87 int length = exprKeyword.matchedLength();
88 setFormat(index, length, keywordFormat);
89 index = exprKeyword.indexIn(text, index + length);
92 // Treat a line as a comment starting at a percent sign
93 // * that is the first character in a line
94 // * that is preceded by
95 // ** an even number of backslashes
96 // ** any character other than a backslash
97 QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
98 exprComment.indexIn(text);
99 index = exprComment.pos(1);
101 int const length = exprComment.matchedLength()
102 - (index - exprComment.pos(0));
103 setFormat(index, length, commentFormat);
104 exprComment.indexIn(text, index + length);
105 index = exprComment.pos(1);
107 // <LyX Warning: ...>
108 QString lyxwarn = qt_("LyX Warning: ");
109 QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
110 index = exprWarning.indexIn(text);
112 int length = exprWarning.matchedLength();
113 setFormat(index, length, warningFormat);
114 index = exprWarning.indexIn(text, index + length);
118 } // namespace frontend