]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/LaTeXHighlighter.cpp
Remove bogus condition and add comments
[lyx.git] / src / frontends / qt / LaTeXHighlighter.cpp
1 /**
2  * \file LaTeXHighlighter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "LaTeXHighlighter.h"
14 #include "qt_helpers.h"
15
16 #include <QString>
17 #include <QTextDocument>
18
19 namespace lyx {
20 namespace frontend {
21
22
23 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent, bool at_letter, bool keyval)
24         : QSyntaxHighlighter(parent), at_letter_(at_letter), keyval_(keyval)
25 {
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);
31         };
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,
37                                                   QPalette::Text));
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));
44 }
45
46
47 void LaTeXHighlighter::highlightBlock(QString const & text)
48 {
49         // keyval
50         if (keyval_) {
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);
67                                 if (kvvindex > 0) {
68                                         int vlength = matchgval.capturedLength(0);
69                                         length += vlength;
70                                         setFormat(kvvindex, vlength, valFormat);
71                                 } else {
72                                         QRegularExpressionMatch matchval =
73                                                 exprKeyvalval.match(text, kvindex + length);
74                                         kvvindex = matchval.capturedStart(0);
75                                         if (kvvindex > 0) {
76                                                 int vlength = matchval.capturedLength(0);
77                                                 length += vlength;
78                                                 setFormat(kvvindex, vlength, valFormat);
79                                         }
80                                 }
81                         }
82                         matchkey = exprKeyvalkey.match(text, kvindex + length);
83                         kvindex = matchkey.capturedStart(0);
84                 }
85         }
86         // $ $
87         static const QRegularExpression exprMath("\\$[^\\$]*\\$");
88         QRegularExpressionMatch match = exprMath.match(text);
89         int index = match.capturedStart(0);
90         while (index >= 0) {
91                 int length = match.capturedLength(0);
92                 setFormat(index, length, mathFormat);
93                 match = exprMath.match(text, index + length);
94                 index = match.capturedStart(0);
95         }
96         // [ ]
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\\**\\}"
106                 ")");
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\\**\\}"
116                 ")");
117         int startIndex = 0;
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);
124         }
125         while (startIndex >= 0) {
126                 match = exprEndDispMath.match(text, startIndex);
127                 int endIndex = match.capturedStart(0);
128                 int length;
129                 if (endIndex == -1) {
130                         setCurrentBlockState(1);
131                         length = text.length() - startIndex;
132                 } else {
133                         length = endIndex - startIndex + match.capturedLength(0);
134                 }
135                 setFormat(startIndex, length, mathFormat);
136                 match = exprStartDispMath.match(text, startIndex + length);
137                 startIndex = match.capturedStart(0);
138         }
139         // \whatever
140         static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
141         // \wh@tever
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);
147         while (index >= 0) {
148                 int length = match.capturedLength(0);
149                 setFormat(index, length, keywordFormat);
150                 match = exprKeyword.match(text, index + length);
151                 index = match.capturedStart(0);
152         }
153         // %comment
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);
162         while (index >= 0) {
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);
168         }
169         // <LyX Warning: ...>
170         QString lyxwarn = qt_("LyX Warning: ");
171         QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
172         match = exprWarning.match(text);
173         index = match.capturedStart(0);
174         while (index >= 0) {
175                 int length = match.capturedLength(0);
176                 setFormat(index, length, warningFormat);
177                 match = exprWarning.match(text, index + length);
178                 index = match.capturedStart(0);
179         }
180 }
181
182 } // namespace frontend
183 } // namespace lyx