2007-10-06 19:03:41 +00:00
|
|
|
/**
|
|
|
|
* \file LaTeXHighlighter.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Bo Peng
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2007-10-09 06:39:53 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-06 19:03:41 +00:00
|
|
|
#include "LaTeXHighlighter.h"
|
2007-12-24 13:55:01 +00:00
|
|
|
#include "qt_helpers.h"
|
2007-10-06 19:03:41 +00:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2016-06-14 20:45:47 +00:00
|
|
|
|
2007-10-06 19:03:41 +00:00
|
|
|
LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
|
|
|
|
: QSyntaxHighlighter(parent)
|
|
|
|
{
|
2016-06-14 20:45:47 +00:00
|
|
|
auto blend = [](QColor color1, QColor color2) {
|
|
|
|
int r = 0.5 * (color1.red() + color2.red());
|
|
|
|
int g = 0.5 * (color1.green() + color2.green());
|
|
|
|
int b = 0.5 * (color1.blue() + color2.blue());
|
|
|
|
return QColor(r, g, b);
|
|
|
|
};
|
|
|
|
QPalette palette = QPalette();
|
|
|
|
QColor text_color = palette.color(QPalette::Active, QPalette::Text);
|
|
|
|
keywordFormat.setForeground(blend(Qt::blue, text_color));
|
2007-10-06 19:03:41 +00:00
|
|
|
keywordFormat.setFontWeight(QFont::Bold);
|
2016-06-14 20:45:47 +00:00
|
|
|
commentFormat.setForeground(palette.color(QPalette::Disabled,
|
|
|
|
QPalette::Text));
|
|
|
|
mathFormat.setForeground(blend(Qt::red, text_color));
|
2007-12-24 13:55:01 +00:00
|
|
|
warningFormat.setForeground(Qt::red);
|
|
|
|
warningFormat.setFontWeight(QFont::Bold);
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LaTeXHighlighter::highlightBlock(QString const & text)
|
|
|
|
{
|
|
|
|
// $ $
|
|
|
|
static const QRegExp exprMath("\\$[^\\$]*\\$");
|
2009-03-09 19:54:47 +00:00
|
|
|
int index = exprMath.indexIn(text);
|
2007-10-06 19:03:41 +00:00
|
|
|
while (index >= 0) {
|
|
|
|
int length = exprMath.matchedLength();
|
|
|
|
setFormat(index, length, mathFormat);
|
2009-03-09 19:54:47 +00:00
|
|
|
index = exprMath.indexIn(text, index + length);
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
|
|
|
// [ ]
|
|
|
|
static const QRegExp exprStartDispMath("(\\\\\\[|"
|
|
|
|
"\\\\begin\\{equation\\**\\}|"
|
|
|
|
"\\\\begin\\{eqnarray\\**\\}|"
|
|
|
|
"\\\\begin\\{align(ed|at)*\\**\\}|"
|
|
|
|
"\\\\begin\\{flalign\\**\\}|"
|
|
|
|
"\\\\begin\\{gather\\**\\}|"
|
|
|
|
"\\\\begin\\{multline\\**\\}|"
|
|
|
|
"\\\\begin\\{array\\**\\}|"
|
|
|
|
"\\\\begin\\{cases\\**\\}"
|
|
|
|
")");
|
|
|
|
static const QRegExp exprEndDispMath("(\\\\\\]|"
|
|
|
|
"\\\\end\\{equation\\**\\}|"
|
|
|
|
"\\\\end\\{eqnarray\\**\\}|"
|
|
|
|
"\\\\end\\{align(ed|at)*\\**\\}|"
|
|
|
|
"\\\\end\\{flalign\\**\\}|"
|
|
|
|
"\\\\end\\{gather\\**\\}|"
|
|
|
|
"\\\\end\\{multline\\**\\}|"
|
|
|
|
"\\\\end\\{array\\**\\}|"
|
|
|
|
"\\\\end\\{cases\\**\\}"
|
|
|
|
")");
|
|
|
|
int startIndex = 0;
|
|
|
|
// if previous block was in 'disp math'
|
|
|
|
// start search from 0 (for end disp math)
|
|
|
|
// otherwise, start search from 'begin disp math'
|
|
|
|
if (previousBlockState() != 1)
|
2009-03-09 19:54:47 +00:00
|
|
|
startIndex = exprStartDispMath.indexIn(text);
|
2009-03-09 19:47:46 +00:00
|
|
|
while (startIndex >= 0) {
|
2009-03-09 19:54:47 +00:00
|
|
|
int endIndex = exprEndDispMath.indexIn(text, startIndex);
|
2007-10-06 19:03:41 +00:00
|
|
|
int length;
|
|
|
|
if (endIndex == -1) {
|
|
|
|
setCurrentBlockState(1);
|
|
|
|
length = text.length() - startIndex;
|
|
|
|
} else {
|
|
|
|
length = endIndex - startIndex + exprEndDispMath.matchedLength();
|
|
|
|
}
|
|
|
|
setFormat(startIndex, length, mathFormat);
|
2009-03-09 19:54:47 +00:00
|
|
|
startIndex = exprStartDispMath.indexIn(text, startIndex + length);
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
|
|
|
// \whatever
|
|
|
|
static const QRegExp exprKeyword("\\\\[A-Za-z]+");
|
2009-03-09 19:54:47 +00:00
|
|
|
index = exprKeyword.indexIn(text);
|
2009-03-09 19:47:46 +00:00
|
|
|
while (index >= 0) {
|
2007-10-06 19:03:41 +00:00
|
|
|
int length = exprKeyword.matchedLength();
|
|
|
|
setFormat(index, length, keywordFormat);
|
2009-03-09 19:54:47 +00:00
|
|
|
index = exprKeyword.indexIn(text, index + length);
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
2007-11-26 21:48:41 +00:00
|
|
|
// %comment
|
|
|
|
// Treat a line as a comment starting at a percent sign
|
|
|
|
// * that is the first character in a line
|
|
|
|
// * that is preceded by
|
|
|
|
// ** an even number of backslashes
|
2007-12-24 13:55:01 +00:00
|
|
|
// ** any character other than a backslash
|
2007-11-26 21:48:41 +00:00
|
|
|
QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$");
|
2009-03-09 19:54:47 +00:00
|
|
|
exprComment.indexIn(text);
|
2007-11-26 21:48:41 +00:00
|
|
|
index = exprComment.pos(1);
|
2009-03-09 19:47:46 +00:00
|
|
|
while (index >= 0) {
|
2007-11-26 21:48:41 +00:00
|
|
|
int const length = exprComment.matchedLength()
|
|
|
|
- (index - exprComment.pos(0));
|
2007-10-06 19:03:41 +00:00
|
|
|
setFormat(index, length, commentFormat);
|
2009-03-09 19:54:47 +00:00
|
|
|
exprComment.indexIn(text, index + length);
|
2007-11-26 21:48:41 +00:00
|
|
|
index = exprComment.pos(1);
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
2007-12-30 21:28:38 +00:00
|
|
|
// <LyX Warning: ...>
|
|
|
|
QString lyxwarn = qt_("LyX Warning: ");
|
|
|
|
QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
|
2009-03-09 19:54:47 +00:00
|
|
|
index = exprWarning.indexIn(text);
|
2009-03-09 19:47:46 +00:00
|
|
|
while (index >= 0) {
|
2007-12-24 13:55:01 +00:00
|
|
|
int length = exprWarning.matchedLength();
|
|
|
|
setFormat(index, length, warningFormat);
|
2009-03-09 19:54:47 +00:00
|
|
|
index = exprWarning.indexIn(text, index + length);
|
2007-12-24 13:55:01 +00:00
|
|
|
}
|
2007-10-06 19:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|