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, bool optsnippet)
24 : QSyntaxHighlighter(parent), at_letter_(at_letter), keyval_(keyval), optsnippet_(optsnippet)
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_in)
49 QString const text = (optsnippet_) ? '[' + text_in + ']' : text_in;
52 // Highlight key-val options. Used in some option widgets.
53 static const QRegularExpression exprKeyvalkey("[^=,]+");
54 static const QRegularExpression exprKeyvalval("[^,]+");
55 QRegularExpressionMatch matchkey = exprKeyvalkey.match(text);
56 int kvindex = matchkey.capturedStart(0);
57 while (kvindex >= 0) {
58 int length = matchkey.capturedLength(0);
59 setFormat(kvindex, length, keyFormat);
60 QRegularExpressionMatch matchval =
61 exprKeyvalval.match(text, kvindex + length);
62 int kvvindex = matchval.capturedStart(0);
64 length += matchval.capturedLength(0);
65 setFormat(kvvindex, length, valFormat);
67 matchkey = exprKeyvalkey.match(text, kvindex + length);
68 kvindex = matchkey.capturedStart(0);
72 static const QRegularExpression exprMath("\\$[^\\$]*\\$");
73 QRegularExpressionMatch match = exprMath.match(text);
74 int index = match.capturedStart(0);
76 int length = match.capturedLength(0);
77 setFormat(index, length, mathFormat);
78 match = exprMath.match(text, index + length);
79 index = match.capturedStart(0);
82 static const QRegularExpression exprStartDispMath("(\\\\\\[|"
83 "\\\\begin\\{equation\\**\\}|"
84 "\\\\begin\\{eqnarray\\**\\}|"
85 "\\\\begin\\{align(ed|at)*\\**\\}|"
86 "\\\\begin\\{flalign\\**\\}|"
87 "\\\\begin\\{gather\\**\\}|"
88 "\\\\begin\\{multline\\**\\}|"
89 "\\\\begin\\{array\\**\\}|"
90 "\\\\begin\\{cases\\**\\}"
92 static const QRegularExpression exprEndDispMath("(\\\\\\]|"
93 "\\\\end\\{equation\\**\\}|"
94 "\\\\end\\{eqnarray\\**\\}|"
95 "\\\\end\\{align(ed|at)*\\**\\}|"
96 "\\\\end\\{flalign\\**\\}|"
97 "\\\\end\\{gather\\**\\}|"
98 "\\\\end\\{multline\\**\\}|"
99 "\\\\end\\{array\\**\\}|"
100 "\\\\end\\{cases\\**\\}"
103 // if previous block was in 'disp math'
104 // start search from 0 (for end disp math)
105 // otherwise, start search from 'begin disp math'
106 if (previousBlockState() != 1) {
107 match = exprStartDispMath.match(text);
108 startIndex = match.capturedStart(0);
110 while (startIndex >= 0) {
111 match = exprEndDispMath.match(text, startIndex);
112 int endIndex = match.capturedStart(0);
114 if (endIndex == -1) {
115 setCurrentBlockState(1);
116 length = text.length() - startIndex;
118 length = endIndex - startIndex + match.capturedLength(0);
120 setFormat(startIndex, length, mathFormat);
121 match = exprStartDispMath.match(text, startIndex + length);
122 startIndex = match.capturedStart(0);
125 static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
127 static const QRegularExpression exprKeywordAtLetter("\\\\[A-Za-z@]+");
128 QRegularExpression const & exprKeyword = at_letter_
129 ? exprKeywordAtLetter : exprKeywordAtOther;
130 match = exprKeyword.match(text);
131 index = match.capturedStart(0);
133 int length = match.capturedLength(0);
134 setFormat(index, length, keywordFormat);
135 match = exprKeyword.match(text, index + length);
136 index = match.capturedStart(0);
139 // Treat a line as a comment starting at a percent sign
140 // * that is the first character in a line
141 // * that is preceded by
142 // ** an even number of backslashes
143 // ** any character other than a backslash
144 QRegularExpression exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
145 match = exprComment.match(text);
146 index = match.capturedStart(1);
148 int const length = match.capturedLength(0)
149 - (index - match.capturedStart(0));
150 setFormat(index, length, commentFormat);
151 match = exprComment.match(text, index + length);
152 index = match.capturedStart(1);
154 // <LyX Warning: ...>
155 QString lyxwarn = qt_("LyX Warning: ");
156 QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
157 match = exprWarning.match(text);
158 index = match.capturedStart(0);
160 int length = match.capturedLength(0);
161 setFormat(index, length, warningFormat);
162 match = exprWarning.match(text, index + length);
163 index = match.capturedStart(0);
167 } // namespace frontend