]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/LaTeXHighlighter.cpp
Improve some debug messages
[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)
24         : QSyntaxHighlighter(parent), at_letter_(at_letter)
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 }
42
43
44 void LaTeXHighlighter::highlightBlock(QString const & text)
45 {
46         // $ $
47         static const QRegExp exprMath("\\$[^\\$]*\\$");
48         int index = exprMath.indexIn(text);
49         while (index >= 0) {
50                 int length = exprMath.matchedLength();
51                 setFormat(index, length, mathFormat);
52                 index = exprMath.indexIn(text, index + length);
53         }
54         // [ ]
55         static const QRegExp exprStartDispMath("(\\\\\\[|"
56                 "\\\\begin\\{equation\\**\\}|"
57                 "\\\\begin\\{eqnarray\\**\\}|"
58                 "\\\\begin\\{align(ed|at)*\\**\\}|"
59                 "\\\\begin\\{flalign\\**\\}|"
60                 "\\\\begin\\{gather\\**\\}|"
61                 "\\\\begin\\{multline\\**\\}|"
62                 "\\\\begin\\{array\\**\\}|"
63                 "\\\\begin\\{cases\\**\\}"
64                 ")");
65         static const QRegExp exprEndDispMath("(\\\\\\]|"
66                 "\\\\end\\{equation\\**\\}|"
67                 "\\\\end\\{eqnarray\\**\\}|"
68                 "\\\\end\\{align(ed|at)*\\**\\}|"
69                 "\\\\end\\{flalign\\**\\}|"
70                 "\\\\end\\{gather\\**\\}|"
71                 "\\\\end\\{multline\\**\\}|"
72                 "\\\\end\\{array\\**\\}|"
73                 "\\\\end\\{cases\\**\\}"
74                 ")");
75         int startIndex = 0;
76         // if previous block was in 'disp math'
77         // start search from 0 (for end disp math)
78         // otherwise, start search from 'begin disp math'
79         if (previousBlockState() != 1)
80                 startIndex = exprStartDispMath.indexIn(text);
81         while (startIndex >= 0) {
82                 int endIndex = exprEndDispMath.indexIn(text, startIndex);
83                 int length;
84                 if (endIndex == -1) {
85                         setCurrentBlockState(1);
86                         length = text.length() - startIndex;
87                 } else {
88                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
89                 }
90                 setFormat(startIndex, length, mathFormat);
91                 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
92         }
93         // \whatever
94         static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
95         // \wh@tever
96         static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
97         QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
98                                                  : exprKeywordAtOther;
99         index = exprKeyword.indexIn(text);
100         while (index >= 0) {
101                 int length = exprKeyword.matchedLength();
102                 setFormat(index, length, keywordFormat);
103                 index = exprKeyword.indexIn(text, index + length);
104         }
105         // %comment
106         // Treat a line as a comment starting at a percent sign
107         // * that is the first character in a line
108         // * that is preceded by
109         // ** an even number of backslashes
110         // ** any character other than a backslash
111         QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
112         exprComment.indexIn(text);
113         index = exprComment.pos(1);
114         while (index >= 0) {
115                 int const length = exprComment.matchedLength()
116                                  - (index - exprComment.pos(0));
117                 setFormat(index, length, commentFormat);
118                 exprComment.indexIn(text, index + length);
119                 index = exprComment.pos(1);
120         }
121         // <LyX Warning: ...>
122         QString lyxwarn = qt_("LyX Warning: ");
123         QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
124         index = exprWarning.indexIn(text);
125         while (index >= 0) {
126                 int length = exprWarning.matchedLength();
127                 setFormat(index, length, warningFormat);
128                 index = exprWarning.indexIn(text, index + length);
129         }
130 }
131
132 } // namespace frontend
133 } // namespace lyx