]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/LaTeXHighlighter.cpp
Improve keyval highlighter
[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, bool optsnippet)
24         : QSyntaxHighlighter(parent), at_letter_(at_letter), keyval_(keyval), optsnippet_(optsnippet)
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                 static QRegularExpression exprKeyvalkey("[^=,]+");
53                 static QRegularExpression exprKeyvalval("[^,]+");
54                 if (optsnippet_) {
55                         static QRegularExpression exprKeyvalkey("^=,+");
56                         static QRegularExpression exprKeyvalval("^,+");
57                 }
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 matchval =
65                                         exprKeyvalval.match(text, kvindex + length);
66                                 int kvvindex = matchval.capturedStart(0);
67                                 if (kvvindex > 0) {
68                                         int vlength = matchval.capturedLength(0);
69                                         length += vlength;
70                                         setFormat(kvvindex, vlength, valFormat);
71                                 }
72                         }
73                         matchkey = exprKeyvalkey.match(text, kvindex + length);
74                         kvindex = matchkey.capturedStart(0);
75                 }
76         }
77         // $ $
78         static const QRegularExpression exprMath("\\$[^\\$]*\\$");
79         QRegularExpressionMatch match = exprMath.match(text);
80         int index = match.capturedStart(0);
81         while (index >= 0) {
82                 int length = match.capturedLength(0);
83                 setFormat(index, length, mathFormat);
84                 match = exprMath.match(text, index + length);
85                 index = match.capturedStart(0);
86         }
87         // [ ]
88         static const QRegularExpression exprStartDispMath("(\\\\\\[|"
89                 "\\\\begin\\{equation\\**\\}|"
90                 "\\\\begin\\{eqnarray\\**\\}|"
91                 "\\\\begin\\{align(ed|at)*\\**\\}|"
92                 "\\\\begin\\{flalign\\**\\}|"
93                 "\\\\begin\\{gather\\**\\}|"
94                 "\\\\begin\\{multline\\**\\}|"
95                 "\\\\begin\\{array\\**\\}|"
96                 "\\\\begin\\{cases\\**\\}"
97                 ")");
98         static const QRegularExpression exprEndDispMath("(\\\\\\]|"
99                 "\\\\end\\{equation\\**\\}|"
100                 "\\\\end\\{eqnarray\\**\\}|"
101                 "\\\\end\\{align(ed|at)*\\**\\}|"
102                 "\\\\end\\{flalign\\**\\}|"
103                 "\\\\end\\{gather\\**\\}|"
104                 "\\\\end\\{multline\\**\\}|"
105                 "\\\\end\\{array\\**\\}|"
106                 "\\\\end\\{cases\\**\\}"
107                 ")");
108         int startIndex = 0;
109         // if previous block was in 'disp math'
110         // start search from 0 (for end disp math)
111         // otherwise, start search from 'begin disp math'
112         if (previousBlockState() != 1) {
113                 match = exprStartDispMath.match(text);
114                 startIndex = match.capturedStart(0);
115         }
116         while (startIndex >= 0) {
117                 match = exprEndDispMath.match(text, startIndex);
118                 int endIndex = match.capturedStart(0);
119                 int length;
120                 if (endIndex == -1) {
121                         setCurrentBlockState(1);
122                         length = text.length() - startIndex;
123                 } else {
124                         length = endIndex - startIndex + match.capturedLength(0);
125                 }
126                 setFormat(startIndex, length, mathFormat);
127                 match = exprStartDispMath.match(text, startIndex + length);
128                 startIndex = match.capturedStart(0);
129         }
130         // \whatever
131         static const QRegularExpression exprKeywordAtOther("\\\\[A-Za-z]+");
132         // \wh@tever
133         static const QRegularExpression exprKeywordAtLetter("\\\\[A-Za-z@]+");
134         QRegularExpression const & exprKeyword = at_letter_
135                         ? exprKeywordAtLetter : exprKeywordAtOther;
136         match = exprKeyword.match(text);
137         index = match.capturedStart(0);
138         while (index >= 0) {
139                 int length = match.capturedLength(0);
140                 setFormat(index, length, keywordFormat);
141                 match = exprKeyword.match(text, index + length);
142                 index = match.capturedStart(0);
143         }
144         // %comment
145         // Treat a line as a comment starting at a percent sign
146         // * that is the first character in a line
147         // * that is preceded by
148         // ** an even number of backslashes
149         // ** any character other than a backslash
150         QRegularExpression exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
151         match = exprComment.match(text);
152         index = match.capturedStart(1);
153         while (index >= 0) {
154                 int const length = match.capturedLength(0)
155                                  - (index - match.capturedStart(0));
156                 setFormat(index, length, commentFormat);
157                 match = exprComment.match(text, index + length);
158                 index = match.capturedStart(1);
159         }
160         // <LyX Warning: ...>
161         QString lyxwarn = qt_("LyX Warning: ");
162         QRegularExpression exprWarning("<" + lyxwarn + "[^<]*>");
163         match = exprWarning.match(text);
164         index = match.capturedStart(0);
165         while (index >= 0) {
166                 int length = match.capturedLength(0);
167                 setFormat(index, length, warningFormat);
168                 match = exprWarning.match(text, index + length);
169                 index = match.capturedStart(0);
170         }
171 }
172
173 } // namespace frontend
174 } // namespace lyx