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>
23 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent, bool at_letter, bool keyval)
24 : QSyntaxHighlighter(parent), at_letter_(at_letter), keyval_(keyval)
26 auto blend = [](QColor color1, QColor color2) {
27 int r = 0.5 * (color1.red() + color2.red());
28 int g = 0.5 * (color1.green() + color2.green());
29 int b = 0.5 * (color1.blue() + color2.blue());
30 return QColor(r, g, b);
32 QPalette palette = QPalette();
33 QColor text_color = palette.color(QPalette::Active, QPalette::Text);
34 keywordFormat.setForeground(blend(Qt::blue, text_color));
35 keywordFormat.setFontWeight(QFont::Bold);
36 commentFormat.setForeground(palette.color(QPalette::Disabled,
38 mathFormat.setForeground(blend(Qt::red, text_color));
39 warningFormat.setForeground(Qt::red);
40 warningFormat.setFontWeight(QFont::Bold);
41 keyFormat.setForeground(blend(Qt::darkRed, text_color));
42 keyFormat.setFontWeight(QFont::Bold);
43 valFormat.setForeground(blend(Qt::darkGreen, text_color));
47 void LaTeXHighlighter::highlightBlock(QString const & text)
49 #if QT_VERSION < 0x060000
52 // Highlight key-val options. Used in some option widgets.
53 static const QRegExp exprKeyvalkey("[^=,]+");
54 static const QRegExp exprKeyvalval("[^,]+");
55 int kvindex = exprKeyvalkey.indexIn(text);
56 while (kvindex >= 0) {
57 int length = exprKeyvalkey.matchedLength();
58 setFormat(kvindex, length, keyFormat);
59 int kvvindex = exprKeyvalval.indexIn(text, kvindex + length);
61 length += exprKeyvalval.matchedLength();
62 setFormat(kvvindex, length, valFormat);
64 kvindex = exprKeyvalkey.indexIn(text, kvindex + length);
68 static const QRegExp exprMath("\\$[^\\$]*\\$");
69 int index = exprMath.indexIn(text);
71 int length = exprMath.matchedLength();
72 setFormat(index, length, mathFormat);
73 index = exprMath.indexIn(text, index + length);
76 static const QRegExp exprStartDispMath("(\\\\\\[|"
77 "\\\\begin\\{equation\\**\\}|"
78 "\\\\begin\\{eqnarray\\**\\}|"
79 "\\\\begin\\{align(ed|at)*\\**\\}|"
80 "\\\\begin\\{flalign\\**\\}|"
81 "\\\\begin\\{gather\\**\\}|"
82 "\\\\begin\\{multline\\**\\}|"
83 "\\\\begin\\{array\\**\\}|"
84 "\\\\begin\\{cases\\**\\}"
86 static const QRegExp exprEndDispMath("(\\\\\\]|"
87 "\\\\end\\{equation\\**\\}|"
88 "\\\\end\\{eqnarray\\**\\}|"
89 "\\\\end\\{align(ed|at)*\\**\\}|"
90 "\\\\end\\{flalign\\**\\}|"
91 "\\\\end\\{gather\\**\\}|"
92 "\\\\end\\{multline\\**\\}|"
93 "\\\\end\\{array\\**\\}|"
94 "\\\\end\\{cases\\**\\}"
97 // if previous block was in 'disp math'
98 // start search from 0 (for end disp math)
99 // otherwise, start search from 'begin disp math'
100 if (previousBlockState() != 1)
101 startIndex = exprStartDispMath.indexIn(text);
102 while (startIndex >= 0) {
103 int endIndex = exprEndDispMath.indexIn(text, startIndex);
105 if (endIndex == -1) {
106 setCurrentBlockState(1);
107 length = text.length() - startIndex;
109 length = endIndex - startIndex + exprEndDispMath.matchedLength();
111 setFormat(startIndex, length, mathFormat);
112 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
115 static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
117 static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
118 QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
119 : exprKeywordAtOther;
120 index = exprKeyword.indexIn(text);
122 int length = exprKeyword.matchedLength();
123 setFormat(index, length, keywordFormat);
124 index = exprKeyword.indexIn(text, index + length);
127 // Treat a line as a comment starting at a percent sign
128 // * that is the first character in a line
129 // * that is preceded by
130 // ** an even number of backslashes
131 // ** any character other than a backslash
132 QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
133 exprComment.indexIn(text);
134 index = exprComment.pos(1);
136 int const length = exprComment.matchedLength()
137 - (index - exprComment.pos(0));
138 setFormat(index, length, commentFormat);
139 exprComment.indexIn(text, index + length);
140 index = exprComment.pos(1);
142 // <LyX Warning: ...>
143 QString lyxwarn = qt_("LyX Warning: ");
144 QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
145 index = exprWarning.indexIn(text);
147 int length = exprWarning.matchedLength();
148 setFormat(index, length, warningFormat);
149 index = exprWarning.indexIn(text, index + length);
154 // Highlight key-val options. Used in some option widgets.
155 static const QRegularExpression exprKeyvalkey("[^=,]+");
156 static const QRegularExpression exprKeyvalval("[^,]+");
157 QRegularExpressionMatch matchkey = exprKeyvalkey.match(text);
158 int kvindex = matchkey.capturedStart(0);
159 while (kvindex >= 0) {
160 int length = matchkey.capturedLength(0);
161 setFormat(kvindex, length, keyFormat);
162 QRegularExpressionMatch matchval =
163 exprKeyvalval.match(text, kvindex + length);
164 int kvvindex = matchval.capturedStart(0);
166 length += matchval.capturedLength(0);
167 setFormat(kvvindex, length, valFormat);
169 matchkey = exprKeyvalkey.match(text, kvindex + length);
170 kvindex = matchkey.capturedStart(0);
174 static const QRegularExpression exprMath("\\$[^\\$]*\\$");
175 QRegularExpressionMatch match = exprMath.match(text);
176 int index = match.capturedStart(0);
178 int length = match.capturedLength(0);
179 setFormat(index, length, mathFormat);
180 match = exprMath.match(text, index + length);
181 index = match.capturedStart(0);
184 static const QRegularExpression exprStartDispMath("(\\\\\\[|"
185 "\\\\begin\\{equation\\**\\}|"
186 "\\\\begin\\{eqnarray\\**\\}|"
187 "\\\\begin\\{align(ed|at)*\\**\\}|"
188 "\\\\begin\\{flalign\\**\\}|"
189 "\\\\begin\\{gather\\**\\}|"
190 "\\\\begin\\{multline\\**\\}|"
191 "\\\\begin\\{array\\**\\}|"
192 "\\\\begin\\{cases\\**\\}"
194 static const QRegularExpression exprEndDispMath("(\\\\\\]|"
195 "\\\\end\\{equation\\**\\}|"
196 "\\\\end\\{eqnarray\\**\\}|"
197 "\\\\end\\{align(ed|at)*\\**\\}|"
198 "\\\\end\\{flalign\\**\\}|"
199 "\\\\end\\{gather\\**\\}|"
200 "\\\\end\\{multline\\**\\}|"
201 "\\\\end\\{array\\**\\}|"
202 "\\\\end\\{cases\\**\\}"
205 // if previous block was in 'disp math'
206 // start search from 0 (for end disp math)
207 // otherwise, start search from 'begin disp math'
208 if (previousBlockState() != 1) {
209 match = exprStartDispMath.match(text);
210 startIndex = match.capturedStart(0);
212 while (startIndex >= 0) {
213 match = exprEndDispMath.match(text, startIndex);
214 int endIndex = match.capturedStart(0);
216 if (endIndex == -1) {
217 setCurrentBlockState(1);
218 length = text.length() - startIndex;
220 length = endIndex - startIndex + match.capturedLength(0);
222 setFormat(startIndex, length, mathFormat);
223 match = exprStartDispMath.match(text, startIndex + length);
224 startIndex = match.capturedStart(0);
227 static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
229 static const QRegularExpression exprKeywordAtLetter("\\\\[A-Za-z@]+");
230 QRegularExpression const & exprKeyword = at_letter_
231 ? exprKeywordAtLetter : exprKeywordAtOther;
232 match = exprKeyword.match(text);
233 index = match.capturedStart(0);
235 int length = match.capturedLength(0);
236 setFormat(index, length, keywordFormat);
237 match = exprKeyword.match(text, index + length);
238 index = match.capturedStart(0);
241 // Treat a line as a comment starting at a percent sign
242 // * that is the first character in a line
243 // * that is preceded by
244 // ** an even number of backslashes
245 // ** any character other than a backslash
246 QRegularExpression exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
247 match = exprComment.match(text);
248 index = match.capturedStart(1);
250 int const length = match.capturedLength(0)
251 - (index - match.capturedStart(0));
252 setFormat(index, length, commentFormat);
253 match = exprComment.match(text, index + length);
254 index = match.capturedStart(1);
256 // <LyX Warning: ...>
257 QString lyxwarn = qt_("LyX Warning: ");
258 QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
259 match = exprWarning.match(text);
260 index = match.capturedStart(0);
262 int length = match.capturedLength(0);
263 setFormat(index, length, warningFormat);
264 match = exprWarning.match(text, index + length);
265 index = match.capturedStart(0);
270 } // namespace frontend