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>
23 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent,
25 : QSyntaxHighlighter(parent), at_letter_(at_letter)
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);
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,
39 mathFormat.setForeground(blend(Qt::red, text_color));
40 warningFormat.setForeground(Qt::red);
41 warningFormat.setFontWeight(QFont::Bold);
45 void LaTeXHighlighter::highlightBlock(QString const & text)
48 static const QRegExp exprMath("\\$[^\\$]*\\$");
49 int index = exprMath.indexIn(text);
51 int length = exprMath.matchedLength();
52 setFormat(index, length, mathFormat);
53 index = exprMath.indexIn(text, index + length);
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\\**\\}"
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\\**\\}"
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);
86 setCurrentBlockState(1);
87 length = text.length() - startIndex;
89 length = endIndex - startIndex + exprEndDispMath.matchedLength();
91 setFormat(startIndex, length, mathFormat);
92 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
95 static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
97 static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
98 QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
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