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)
51 // Highlight key-val options. Used in some option widgets.
52 // 1. The keys. Might or might not have values
53 static QRegularExpression exprKeyvalkey("[^=,}]+");
54 // 2. These are grouped values such as "key1={val,val},key2=val"
55 static QRegularExpression exprKeyvalgval("[^=,{]+{[^}]+}");
56 // 3. And normal values if we don't find grouped ones
57 static QRegularExpression exprKeyvalval("[^,]+");
58 QRegularExpressionMatch matchkey = exprKeyvalkey.match(text);
59 int kvindex = matchkey.capturedStart(0);
60 while (kvindex >= 0) {
61 int length = matchkey.capturedLength(0);
62 setFormat(kvindex, length, keyFormat);
63 if (text.size() > kvindex + length && text.at(kvindex + length) == '=') {
64 QRegularExpressionMatch matchgval =
65 exprKeyvalgval.match(text, kvindex + length);
66 int kvvindex = matchgval.capturedStart(0);
68 int vlength = matchgval.capturedLength(0);
70 setFormat(kvvindex, vlength, valFormat);
72 QRegularExpressionMatch matchval =
73 exprKeyvalval.match(text, kvindex + length);
74 kvvindex = matchval.capturedStart(0);
76 int vlength = matchval.capturedLength(0);
78 setFormat(kvvindex, vlength, valFormat);
82 matchkey = exprKeyvalkey.match(text, kvindex + length);
83 kvindex = matchkey.capturedStart(0);
87 static const QRegularExpression exprMath("\\$[^\\$]*\\$");
88 QRegularExpressionMatch match = exprMath.match(text);
89 int index = match.capturedStart(0);
91 int length = match.capturedLength(0);
92 setFormat(index, length, mathFormat);
93 match = exprMath.match(text, index + length);
94 index = match.capturedStart(0);
97 static const QRegularExpression exprStartDispMath("(\\\\\\[|"
98 "\\\\begin\\{equation\\**\\}|"
99 "\\\\begin\\{eqnarray\\**\\}|"
100 "\\\\begin\\{align(ed|at)*\\**\\}|"
101 "\\\\begin\\{flalign\\**\\}|"
102 "\\\\begin\\{gather\\**\\}|"
103 "\\\\begin\\{multline\\**\\}|"
104 "\\\\begin\\{array\\**\\}|"
105 "\\\\begin\\{cases\\**\\}"
107 static const QRegularExpression exprEndDispMath("(\\\\\\]|"
108 "\\\\end\\{equation\\**\\}|"
109 "\\\\end\\{eqnarray\\**\\}|"
110 "\\\\end\\{align(ed|at)*\\**\\}|"
111 "\\\\end\\{flalign\\**\\}|"
112 "\\\\end\\{gather\\**\\}|"
113 "\\\\end\\{multline\\**\\}|"
114 "\\\\end\\{array\\**\\}|"
115 "\\\\end\\{cases\\**\\}"
118 // if previous block was in 'disp math'
119 // start search from 0 (for end disp math)
120 // otherwise, start search from 'begin disp math'
121 if (previousBlockState() != 1) {
122 match = exprStartDispMath.match(text);
123 startIndex = match.capturedStart(0);
125 while (startIndex >= 0) {
126 match = exprEndDispMath.match(text, startIndex);
127 int endIndex = match.capturedStart(0);
129 if (endIndex == -1) {
130 setCurrentBlockState(1);
131 length = text.length() - startIndex;
133 length = endIndex - startIndex + match.capturedLength(0);
135 setFormat(startIndex, length, mathFormat);
136 match = exprStartDispMath.match(text, startIndex + length);
137 startIndex = match.capturedStart(0);
140 static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
142 static const QRegularExpression exprKeywordAtLetter("\\\\[A-Za-z@]+");
143 QRegularExpression const & exprKeyword = at_letter_
144 ? exprKeywordAtLetter : exprKeywordAtOther;
145 match = exprKeyword.match(text);
146 index = match.capturedStart(0);
148 int length = match.capturedLength(0);
149 setFormat(index, length, keywordFormat);
150 match = exprKeyword.match(text, index + length);
151 index = match.capturedStart(0);
154 // Treat a line as a comment starting at a percent sign
155 // * that is the first character in a line
156 // * that is preceded by
157 // ** an even number of backslashes
158 // ** any character other than a backslash
159 QRegularExpression exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
160 match = exprComment.match(text);
161 index = match.capturedStart(1);
163 int const length = match.capturedLength(0)
164 - (index - match.capturedStart(0));
165 setFormat(index, length, commentFormat);
166 match = exprComment.match(text, index + length);
167 index = match.capturedStart(1);
169 // <LyX Warning: ...>
170 QString lyxwarn = qt_("LyX Warning: ");
171 QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
172 match = exprWarning.match(text);
173 index = match.capturedStart(0);
175 int length = match.capturedLength(0);
176 setFormat(index, length, warningFormat);
177 match = exprWarning.match(text, index + length);
178 index = match.capturedStart(0);
182 } // namespace frontend