]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
Make latex highlighter colors compatible with dark theme
[lyx.git] / src / frontends / qt4 / 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 namespace {
23
24 QColor blend(QColor color1, QColor color2) {
25         int r = 0.5 * (color1.red() + color2.red());
26         int g = 0.5 * (color1.green() + color2.green());
27         int b = 0.5 * (color1.blue() + color2.blue());
28         return QColor(r, g, b);
29 };
30
31 }
32
33
34 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
35         : QSyntaxHighlighter(parent)
36 {
37         QPalette palette = QPalette();
38         QColor text_color = palette.color(QPalette::Active, QPalette::Text);
39         keywordFormat.setForeground(blend(Qt::blue, text_color));
40         keywordFormat.setFontWeight(QFont::Bold);
41         commentFormat.setForeground(palette.color(QPalette::Disabled,
42                                                   QPalette::Text));
43         mathFormat.setForeground(blend(Qt::red, text_color));
44         warningFormat.setForeground(Qt::red);
45         warningFormat.setFontWeight(QFont::Bold);
46 }
47
48
49 void LaTeXHighlighter::highlightBlock(QString const & text)
50 {
51         // $ $
52         static const QRegExp exprMath("\\$[^\\$]*\\$");
53         int index = exprMath.indexIn(text);
54         while (index >= 0) {
55                 int length = exprMath.matchedLength();
56                 setFormat(index, length, mathFormat);
57                 index = exprMath.indexIn(text, index + length);
58         }
59         // [ ]
60         static const QRegExp exprStartDispMath("(\\\\\\[|"
61                 "\\\\begin\\{equation\\**\\}|"
62                 "\\\\begin\\{eqnarray\\**\\}|"
63                 "\\\\begin\\{align(ed|at)*\\**\\}|"
64                 "\\\\begin\\{flalign\\**\\}|"
65                 "\\\\begin\\{gather\\**\\}|"
66                 "\\\\begin\\{multline\\**\\}|"
67                 "\\\\begin\\{array\\**\\}|"
68                 "\\\\begin\\{cases\\**\\}"
69                 ")");
70         static const QRegExp exprEndDispMath("(\\\\\\]|"
71                 "\\\\end\\{equation\\**\\}|"
72                 "\\\\end\\{eqnarray\\**\\}|"
73                 "\\\\end\\{align(ed|at)*\\**\\}|"
74                 "\\\\end\\{flalign\\**\\}|"
75                 "\\\\end\\{gather\\**\\}|"
76                 "\\\\end\\{multline\\**\\}|"
77                 "\\\\end\\{array\\**\\}|"
78                 "\\\\end\\{cases\\**\\}"
79                 ")");
80         int startIndex = 0;
81         // if previous block was in 'disp math'
82         // start search from 0 (for end disp math)
83         // otherwise, start search from 'begin disp math'
84         if (previousBlockState() != 1)
85                 startIndex = exprStartDispMath.indexIn(text);
86         while (startIndex >= 0) {
87                 int endIndex = exprEndDispMath.indexIn(text, startIndex);
88                 int length;
89                 if (endIndex == -1) {
90                         setCurrentBlockState(1);
91                         length = text.length() - startIndex;
92                 } else {
93                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
94                 }
95                 setFormat(startIndex, length, mathFormat);
96                 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
97         }
98         // \whatever
99         static const QRegExp exprKeyword("\\\\[A-Za-z]+");
100         index = exprKeyword.indexIn(text);
101         while (index >= 0) {
102                 int length = exprKeyword.matchedLength();
103                 setFormat(index, length, keywordFormat);
104                 index = exprKeyword.indexIn(text, index + length);
105         }
106         // %comment
107         // Treat a line as a comment starting at a percent sign
108         // * that is the first character in a line
109         // * that is preceded by 
110         // ** an even number of backslashes
111         // ** any character other than a backslash
112         QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$"); 
113         exprComment.indexIn(text);
114         index = exprComment.pos(1);
115         while (index >= 0) {
116                 int const length = exprComment.matchedLength() 
117                                  - (index - exprComment.pos(0));
118                 setFormat(index, length, commentFormat);
119                 exprComment.indexIn(text, index + length);
120                 index = exprComment.pos(1);
121         }
122         // <LyX Warning: ...>
123         QString lyxwarn = qt_("LyX Warning: ");
124         QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
125         index = exprWarning.indexIn(text);
126         while (index >= 0) {
127                 int length = exprWarning.matchedLength();
128                 setFormat(index, length, warningFormat);
129                 index = exprWarning.indexIn(text, index + length);
130         }
131 }
132
133 } // namespace frontend
134 } // namespace lyx