]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
ee32cd002a14ab7ae7f5a14af21566f026475c0b
[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                                    bool const at_letter)
25         : QSyntaxHighlighter(parent), at_letter_(at_letter)
26 {
27         auto blend = [](QColor color1, QColor color2) {
28                 int r = 0.5 * (color1.red() + color2.red());
29                 int g = 0.5 * (color1.green() + color2.green());
30                 int b = 0.5 * (color1.blue() + color2.blue());
31                 return QColor(r, g, b);
32         };
33         QPalette palette = QPalette();
34         QColor text_color = palette.color(QPalette::Active, QPalette::Text);
35         keywordFormat.setForeground(blend(Qt::blue, text_color));
36         keywordFormat.setFontWeight(QFont::Bold);
37         commentFormat.setForeground(palette.color(QPalette::Disabled,
38                                                   QPalette::Text));
39         mathFormat.setForeground(blend(Qt::red, text_color));
40         warningFormat.setForeground(Qt::red);
41         warningFormat.setFontWeight(QFont::Bold);
42 }
43
44
45 void LaTeXHighlighter::highlightBlock(QString const & text)
46 {
47         // $ $
48         static const QRegExp exprMath("\\$[^\\$]*\\$");
49         int index = exprMath.indexIn(text);
50         while (index >= 0) {
51                 int length = exprMath.matchedLength();
52                 setFormat(index, length, mathFormat);
53                 index = exprMath.indexIn(text, index + length);
54         }
55         // [ ]
56         static const QRegExp exprStartDispMath("(\\\\\\[|"
57                 "\\\\begin\\{equation\\**\\}|"
58                 "\\\\begin\\{eqnarray\\**\\}|"
59                 "\\\\begin\\{align(ed|at)*\\**\\}|"
60                 "\\\\begin\\{flalign\\**\\}|"
61                 "\\\\begin\\{gather\\**\\}|"
62                 "\\\\begin\\{multline\\**\\}|"
63                 "\\\\begin\\{array\\**\\}|"
64                 "\\\\begin\\{cases\\**\\}"
65                 ")");
66         static const QRegExp exprEndDispMath("(\\\\\\]|"
67                 "\\\\end\\{equation\\**\\}|"
68                 "\\\\end\\{eqnarray\\**\\}|"
69                 "\\\\end\\{align(ed|at)*\\**\\}|"
70                 "\\\\end\\{flalign\\**\\}|"
71                 "\\\\end\\{gather\\**\\}|"
72                 "\\\\end\\{multline\\**\\}|"
73                 "\\\\end\\{array\\**\\}|"
74                 "\\\\end\\{cases\\**\\}"
75                 ")");
76         int startIndex = 0;
77         // if previous block was in 'disp math'
78         // start search from 0 (for end disp math)
79         // otherwise, start search from 'begin disp math'
80         if (previousBlockState() != 1)
81                 startIndex = exprStartDispMath.indexIn(text);
82         while (startIndex >= 0) {
83                 int endIndex = exprEndDispMath.indexIn(text, startIndex);
84                 int length;
85                 if (endIndex == -1) {
86                         setCurrentBlockState(1);
87                         length = text.length() - startIndex;
88                 } else {
89                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
90                 }
91                 setFormat(startIndex, length, mathFormat);
92                 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
93         }
94         // \whatever
95         static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
96         // \wh@tever
97         static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
98         QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
99                                                  : exprKeywordAtOther;
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