2 * \file LaTeXHighlighter.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "LaTeXHighlighter.h"
14 #include "qt_helpers.h"
17 #include <QTextDocument>
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);
34 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
35 : QSyntaxHighlighter(parent)
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,
43 mathFormat.setForeground(blend(Qt::red, text_color));
44 warningFormat.setForeground(Qt::red);
45 warningFormat.setFontWeight(QFont::Bold);
49 void LaTeXHighlighter::highlightBlock(QString const & text)
52 static const QRegExp exprMath("\\$[^\\$]*\\$");
53 int index = exprMath.indexIn(text);
55 int length = exprMath.matchedLength();
56 setFormat(index, length, mathFormat);
57 index = exprMath.indexIn(text, index + length);
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\\**\\}"
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\\**\\}"
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);
90 setCurrentBlockState(1);
91 length = text.length() - startIndex;
93 length = endIndex - startIndex + exprEndDispMath.matchedLength();
95 setFormat(startIndex, length, mathFormat);
96 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
99 static const QRegExp exprKeyword("\\\\[A-Za-z]+");
100 index = exprKeyword.indexIn(text);
102 int length = exprKeyword.matchedLength();
103 setFormat(index, length, keywordFormat);
104 index = exprKeyword.indexIn(text, index + length);
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);
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);
122 // <LyX Warning: ...>
123 QString lyxwarn = qt_("LyX Warning: ");
124 QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
125 index = exprWarning.indexIn(text);
127 int length = exprWarning.matchedLength();
128 setFormat(index, length, warningFormat);
129 index = exprWarning.indexIn(text, index + length);
133 } // namespace frontend