]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
Be a bit more careful about setting and restoring the last reference
[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
23 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
24         : QSyntaxHighlighter(parent)
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 exprKeyword("\\\\[A-Za-z]+");
95         index = exprKeyword.indexIn(text);
96         while (index >= 0) {
97                 int length = exprKeyword.matchedLength();
98                 setFormat(index, length, keywordFormat);
99                 index = exprKeyword.indexIn(text, index + length);
100         }
101         // %comment
102         // Treat a line as a comment starting at a percent sign
103         // * that is the first character in a line
104         // * that is preceded by 
105         // ** an even number of backslashes
106         // ** any character other than a backslash
107         QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$"); 
108         exprComment.indexIn(text);
109         index = exprComment.pos(1);
110         while (index >= 0) {
111                 int const length = exprComment.matchedLength() 
112                                  - (index - exprComment.pos(0));
113                 setFormat(index, length, commentFormat);
114                 exprComment.indexIn(text, index + length);
115                 index = exprComment.pos(1);
116         }
117         // <LyX Warning: ...>
118         QString lyxwarn = qt_("LyX Warning: ");
119         QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
120         index = exprWarning.indexIn(text);
121         while (index >= 0) {
122                 int length = exprWarning.matchedLength();
123                 setFormat(index, length, warningFormat);
124                 index = exprWarning.indexIn(text, index + length);
125         }
126 }
127
128 } // namespace frontend
129 } // namespace lyx