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, bool at_letter)
24 : QSyntaxHighlighter(parent), at_letter_(at_letter)
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);
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,
38 mathFormat.setForeground(blend(Qt::red, text_color));
39 warningFormat.setForeground(Qt::red);
40 warningFormat.setFontWeight(QFont::Bold);
44 void LaTeXHighlighter::highlightBlock(QString const & text)
47 static const QRegExp exprMath("\\$[^\\$]*\\$");
48 int index = exprMath.indexIn(text);
50 int length = exprMath.matchedLength();
51 setFormat(index, length, mathFormat);
52 index = exprMath.indexIn(text, index + length);
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\\**\\}"
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\\**\\}"
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);
85 setCurrentBlockState(1);
86 length = text.length() - startIndex;
88 length = endIndex - startIndex + exprEndDispMath.matchedLength();
90 setFormat(startIndex, length, mathFormat);
91 startIndex = exprStartDispMath.indexIn(text, startIndex + length);
94 static const QRegExp exprKeywordAtOther("\\\\[A-Za-z]+");
96 static const QRegExp exprKeywordAtLetter("\\\\[A-Za-z@]+");
97 QRegExp const & exprKeyword = at_letter_ ? exprKeywordAtLetter
99 index = exprKeyword.indexIn(text);
101 int length = exprKeyword.matchedLength();
102 setFormat(index, length, keywordFormat);
103 index = exprKeyword.indexIn(text, index + length);
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);
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);
121 // <LyX Warning: ...>
122 QString lyxwarn = qt_("LyX Warning: ");
123 QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
124 index = exprWarning.indexIn(text);
126 int length = exprWarning.matchedLength();
127 setFormat(index, length, warningFormat);
128 index = exprWarning.indexIn(text, index + length);
132 } // namespace frontend