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"
16 #include "support/lassert.h"
19 #include <QTextDocument>
22 using namespace lyx::support;
27 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
28 : QSyntaxHighlighter(parent)
30 keywordFormat.setForeground(Qt::darkBlue);
31 keywordFormat.setFontWeight(QFont::Bold);
32 commentFormat.setForeground(Qt::darkGray);
33 mathFormat.setForeground(Qt::red);
34 warningFormat.setForeground(Qt::red);
35 warningFormat.setFontWeight(QFont::Bold);
39 void LaTeXHighlighter::highlightBlock(QString const & text)
42 static const QRegExp exprMath("\\$[^\\$]*\\$");
43 int index = text.indexOf(exprMath);
45 int length = exprMath.matchedLength();
46 setFormat(index, length, mathFormat);
47 index = text.indexOf(exprMath, index + length);
50 static const QRegExp exprStartDispMath("(\\\\\\[|"
51 "\\\\begin\\{equation\\**\\}|"
52 "\\\\begin\\{eqnarray\\**\\}|"
53 "\\\\begin\\{align(ed|at)*\\**\\}|"
54 "\\\\begin\\{flalign\\**\\}|"
55 "\\\\begin\\{gather\\**\\}|"
56 "\\\\begin\\{multline\\**\\}|"
57 "\\\\begin\\{array\\**\\}|"
58 "\\\\begin\\{cases\\**\\}"
60 static const QRegExp exprEndDispMath("(\\\\\\]|"
61 "\\\\end\\{equation\\**\\}|"
62 "\\\\end\\{eqnarray\\**\\}|"
63 "\\\\end\\{align(ed|at)*\\**\\}|"
64 "\\\\end\\{flalign\\**\\}|"
65 "\\\\end\\{gather\\**\\}|"
66 "\\\\end\\{multline\\**\\}|"
67 "\\\\end\\{array\\**\\}|"
68 "\\\\end\\{cases\\**\\}"
71 // if previous block was in 'disp math'
72 // start search from 0 (for end disp math)
73 // otherwise, start search from 'begin disp math'
74 if (previousBlockState() != 1)
75 startIndex = text.indexOf(exprStartDispMath);
76 // We try to avoid infinite loops...
77 static size_t max_loop = 1000;
78 for (size_t i = 0; i != max_loop; ++i) {
79 int endIndex = text.indexOf(exprEndDispMath, startIndex);
82 setCurrentBlockState(1);
83 length = text.length() - startIndex;
85 length = endIndex - startIndex + exprEndDispMath.matchedLength();
87 setFormat(startIndex, length, mathFormat);
88 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
92 LASSERT(startIndex == -1, return);
95 static const QRegExp exprKeyword("\\\\[A-Za-z]+");
96 index = text.indexOf(exprKeyword);
97 for (size_t i = 0; i != max_loop; ++i) {
98 int length = exprKeyword.matchedLength();
99 setFormat(index, length, keywordFormat);
100 index = text.indexOf(exprKeyword, index + length);
104 LASSERT(index == -1, return);
107 // Treat a line as a comment starting at a percent sign
108 // * that is the first character in a line
109 // * that is preceded by
110 // ** an even number of backslashes
111 // ** any character other than a backslash
112 QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
113 text.indexOf(exprComment);
114 index = exprComment.pos(1);
115 for (size_t i = 0; i != max_loop; ++i) {
116 int const length = exprComment.matchedLength()
117 - (index - exprComment.pos(0));
118 setFormat(index, length, commentFormat);
119 text.indexOf(exprComment, index + length);
120 index = exprComment.pos(1);
124 LASSERT(index == -1, return);
126 // <LyX Warning: ...>
127 QString lyxwarn = qt_("LyX Warning: ");
128 QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
129 index = text.indexOf(exprWarning);
130 for (size_t i = 0; i != max_loop; ++i) {
131 int length = exprWarning.matchedLength();
132 setFormat(index, length, warningFormat);
133 index = text.indexOf(exprWarning, index + length);
137 LASSERT(index == -1, return);
140 } // namespace frontend