]> git.lyx.org Git - features.git/blob - src/frontends/qt/LaTeXHighlighter.cpp
Fix broken Apple speller interface
[features.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 #if QT_VERSION < 0x060000
50         // keyval
51         if (keyval_) {
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);
60                         if (kvvindex > 0) {
61                                 length += exprKeyvalval.matchedLength();
62                                 setFormat(kvvindex, length, valFormat);
63                         }
64                         kvindex = exprKeyvalkey.indexIn(text, kvindex + length);
65                 }
66         }
67         // $ $
68         static const QRegExp exprMath("\\$[^\\$]*\\$");
69         int index = exprMath.indexIn(text);
70         while (index >= 0) {
71                 int length = exprMath.matchedLength();
72                 setFormat(index, length, mathFormat);
73                 index = exprMath.indexIn(text, index + length);
74         }
75         // [ ]
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\\**\\}"
85                 ")");
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\\**\\}"
95                 ")");
96         int startIndex = 0;
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);
104                 int length;
105                 if (endIndex == -1) {
106                         setCurrentBlockState(1);
107                         length = text.length() - startIndex;
108                 } else {
109                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
110                 }
111                 setFormat(startIndex, length, mathFormat);
112                 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
113         }
114         // \whatever
115         static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
116         // \wh@tever
117         static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
118         QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
119                                                  : exprKeywordAtOther;
120         index = exprKeyword.indexIn(text);
121         while (index >= 0) {
122                 int length = exprKeyword.matchedLength();
123                 setFormat(index, length, keywordFormat);
124                 index = exprKeyword.indexIn(text, index + length);
125         }
126         // %comment
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);
135         while (index >= 0) {
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);
141         }
142         // <LyX Warning: ...>
143         QString lyxwarn = qt_("LyX Warning: ");
144         QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
145         index = exprWarning.indexIn(text);
146         while (index >= 0) {
147                 int length = exprWarning.matchedLength();
148                 setFormat(index, length, warningFormat);
149                 index = exprWarning.indexIn(text, index + length);
150         }
151 #else
152         // keyval
153         if (keyval_) {
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);
165                         if (kvvindex > 0) {
166                                 length += matchval.capturedLength(0);
167                                 setFormat(kvvindex, length, valFormat);
168                         }
169                         matchkey = exprKeyvalkey.match(text, kvindex + length);
170                         kvindex = matchkey.capturedStart(0);
171                 }
172         }
173         // $ $
174         static const QRegularExpression exprMath("\\$[^\\$]*\\$");
175         QRegularExpressionMatch match = exprMath.match(text);
176         int index = match.capturedStart(0);
177         while (index >= 0) {
178                 int length = match.capturedLength(0);
179                 setFormat(index, length, mathFormat);
180                 match = exprMath.match(text, index + length);
181                 index = match.capturedStart(0);
182         }
183         // [ ]
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\\**\\}"
193                 ")");
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\\**\\}"
203                 ")");
204         int startIndex = 0;
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);
211         }
212         while (startIndex >= 0) {
213                 match = exprEndDispMath.match(text, startIndex);
214                 int endIndex = match.capturedStart(0);
215                 int length;
216                 if (endIndex == -1) {
217                         setCurrentBlockState(1);
218                         length = text.length() - startIndex;
219                 } else {
220                         length = endIndex - startIndex + match.capturedLength(0);
221                 }
222                 setFormat(startIndex, length, mathFormat);
223                 match = exprStartDispMath.match(text, startIndex + length);
224                 startIndex = match.capturedStart(0);
225         }
226         // \whatever
227         static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
228         // \wh@tever
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);
234         while (index >= 0) {
235                 int length = match.capturedLength(0);
236                 setFormat(index, length, keywordFormat);
237                 match = exprKeyword.match(text, index + length);
238                 index = match.capturedStart(0);
239         }
240         // %comment
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);
249         while (index >= 0) {
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);
255         }
256         // <LyX Warning: ...>
257         QString lyxwarn = qt_("LyX Warning: ");
258         QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
259         match = exprWarning.match(text);
260         index = match.capturedStart(0);
261         while (index >= 0) {
262                 int length = match.capturedLength(0);
263                 setFormat(index, length, warningFormat);
264                 match = exprWarning.match(text, index + length);
265                 index = match.capturedStart(0);
266         }
267 #endif
268 }
269
270 } // namespace frontend
271 } // namespace lyx