mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-27 11:52:25 +00:00
move LaTeXHighlighte
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20796 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
e9d2d9b9fb
commit
86af8a2811
@ -23,7 +23,7 @@
|
|||||||
// For the Branches module
|
// For the Branches module
|
||||||
#include "GuiBranches.h"
|
#include "GuiBranches.h"
|
||||||
|
|
||||||
#include "GuiViewSource.h" // For latexHighlighter use in the preamble.
|
#include "LaTeXHighlighter.h"
|
||||||
|
|
||||||
#include "frontend_helpers.h"
|
#include "frontend_helpers.h"
|
||||||
#include "BufferParams.h"
|
#include "BufferParams.h"
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include "GuiViewSource.h"
|
#include "GuiViewSource.h"
|
||||||
|
#include "LaTeXHighlighter.h"
|
||||||
#include "qt_helpers.h"
|
#include "qt_helpers.h"
|
||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
@ -86,92 +87,6 @@ void GuiViewSourceDialog::update(bool full_source)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// LaTeXHighlighter
|
|
||||||
//
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
|
|
||||||
: QSyntaxHighlighter(parent)
|
|
||||||
{
|
|
||||||
keywordFormat.setForeground(Qt::darkBlue);
|
|
||||||
keywordFormat.setFontWeight(QFont::Bold);
|
|
||||||
commentFormat.setForeground(Qt::darkGray);
|
|
||||||
mathFormat.setForeground(Qt::red);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void LaTeXHighlighter::highlightBlock(QString const & text)
|
|
||||||
{
|
|
||||||
// $ $
|
|
||||||
QRegExp exprMath("\\$[^\\$]*\\$");
|
|
||||||
int index = text.indexOf(exprMath);
|
|
||||||
while (index >= 0) {
|
|
||||||
int length = exprMath.matchedLength();
|
|
||||||
setFormat(index, length, mathFormat);
|
|
||||||
index = text.indexOf(exprMath, index + length);
|
|
||||||
}
|
|
||||||
// [ ]
|
|
||||||
QRegExp exprStartDispMath("(\\\\\\[|"
|
|
||||||
"\\\\begin\\{equation\\**\\}|"
|
|
||||||
"\\\\begin\\{eqnarray\\**\\}|"
|
|
||||||
"\\\\begin\\{align(ed|at)*\\**\\}|"
|
|
||||||
"\\\\begin\\{flalign\\**\\}|"
|
|
||||||
"\\\\begin\\{gather\\**\\}|"
|
|
||||||
"\\\\begin\\{multline\\**\\}|"
|
|
||||||
"\\\\begin\\{array\\**\\}|"
|
|
||||||
"\\\\begin\\{cases\\**\\}"
|
|
||||||
")");
|
|
||||||
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)
|
|
||||||
startIndex = text.indexOf(exprStartDispMath);
|
|
||||||
while (startIndex >= 0) {
|
|
||||||
int endIndex = text.indexOf(exprEndDispMath, startIndex);
|
|
||||||
int length;
|
|
||||||
if (endIndex == -1) {
|
|
||||||
setCurrentBlockState(1);
|
|
||||||
length = text.length() - startIndex;
|
|
||||||
} else {
|
|
||||||
length = endIndex - startIndex + exprEndDispMath.matchedLength();
|
|
||||||
}
|
|
||||||
setFormat(startIndex, length, mathFormat);
|
|
||||||
startIndex = text.indexOf(exprStartDispMath, startIndex + length);
|
|
||||||
}
|
|
||||||
// \whatever
|
|
||||||
QRegExp exprKeyword("\\\\[A-Za-z]+");
|
|
||||||
index = text.indexOf(exprKeyword);
|
|
||||||
while (index >= 0) {
|
|
||||||
int length = exprKeyword.matchedLength();
|
|
||||||
setFormat(index, length, keywordFormat);
|
|
||||||
index = text.indexOf(exprKeyword, index + length);
|
|
||||||
}
|
|
||||||
// comment
|
|
||||||
QRegExp exprComment("(^|[^\\\\])%.*$");
|
|
||||||
index = text.indexOf(exprComment);
|
|
||||||
while (index >= 0) {
|
|
||||||
int const length = exprComment.matchedLength();
|
|
||||||
setFormat(index, length, commentFormat);
|
|
||||||
index = text.indexOf(exprComment, index + length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ControlViewSource::ControlViewSource(Dialog & parent)
|
ControlViewSource::ControlViewSource(Dialog & parent)
|
||||||
: Controller(parent)
|
: Controller(parent)
|
||||||
{}
|
{}
|
||||||
|
@ -31,22 +31,8 @@ class QTextDocument;
|
|||||||
namespace lyx {
|
namespace lyx {
|
||||||
namespace frontend {
|
namespace frontend {
|
||||||
|
|
||||||
// used already twice...
|
|
||||||
class LaTeXHighlighter : public QSyntaxHighlighter
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
LaTeXHighlighter(QTextDocument * parent);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void highlightBlock(QString const & text);
|
|
||||||
|
|
||||||
private:
|
|
||||||
QTextCharFormat commentFormat;
|
|
||||||
QTextCharFormat keywordFormat;
|
|
||||||
QTextCharFormat mathFormat;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ControlViewSource;
|
class ControlViewSource;
|
||||||
|
class LaTeXHighlighter;
|
||||||
|
|
||||||
class GuiViewSourceDialog : public QWidget, public Ui::ViewSourceUi
|
class GuiViewSourceDialog : public QWidget, public Ui::ViewSourceUi
|
||||||
{
|
{
|
||||||
|
97
src/frontends/qt4/LaTeXHighlighter.cpp
Normal file
97
src/frontends/qt4/LaTeXHighlighter.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
/**
|
||||||
|
* \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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "LaTeXHighlighter.h"
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QTextDocument>
|
||||||
|
|
||||||
|
namespace lyx {
|
||||||
|
namespace frontend {
|
||||||
|
|
||||||
|
LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
|
||||||
|
: QSyntaxHighlighter(parent)
|
||||||
|
{
|
||||||
|
keywordFormat.setForeground(Qt::darkBlue);
|
||||||
|
keywordFormat.setFontWeight(QFont::Bold);
|
||||||
|
commentFormat.setForeground(Qt::darkGray);
|
||||||
|
mathFormat.setForeground(Qt::red);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LaTeXHighlighter::highlightBlock(QString const & text)
|
||||||
|
{
|
||||||
|
// $ $
|
||||||
|
static const QRegExp exprMath("\\$[^\\$]*\\$");
|
||||||
|
int index = text.indexOf(exprMath);
|
||||||
|
while (index >= 0) {
|
||||||
|
int length = exprMath.matchedLength();
|
||||||
|
setFormat(index, length, mathFormat);
|
||||||
|
index = text.indexOf(exprMath, index + length);
|
||||||
|
}
|
||||||
|
// [ ]
|
||||||
|
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)
|
||||||
|
startIndex = text.indexOf(exprStartDispMath);
|
||||||
|
while (startIndex >= 0) {
|
||||||
|
int endIndex = text.indexOf(exprEndDispMath, startIndex);
|
||||||
|
int length;
|
||||||
|
if (endIndex == -1) {
|
||||||
|
setCurrentBlockState(1);
|
||||||
|
length = text.length() - startIndex;
|
||||||
|
} else {
|
||||||
|
length = endIndex - startIndex + exprEndDispMath.matchedLength();
|
||||||
|
}
|
||||||
|
setFormat(startIndex, length, mathFormat);
|
||||||
|
startIndex = text.indexOf(exprStartDispMath, startIndex + length);
|
||||||
|
}
|
||||||
|
// \whatever
|
||||||
|
static const QRegExp exprKeyword("\\\\[A-Za-z]+");
|
||||||
|
index = text.indexOf(exprKeyword);
|
||||||
|
while (index >= 0) {
|
||||||
|
int length = exprKeyword.matchedLength();
|
||||||
|
setFormat(index, length, keywordFormat);
|
||||||
|
index = text.indexOf(exprKeyword, index + length);
|
||||||
|
}
|
||||||
|
// comment
|
||||||
|
static const QRegExp exprComment("(^|[^\\\\])%.*$");
|
||||||
|
index = text.indexOf(exprComment);
|
||||||
|
while (index >= 0) {
|
||||||
|
int const length = exprComment.matchedLength();
|
||||||
|
setFormat(index, length, commentFormat);
|
||||||
|
index = text.indexOf(exprComment, index + length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace frontend
|
||||||
|
} // namespace lyx
|
42
src/frontends/qt4/LaTeXHighlighter.h
Normal file
42
src/frontends/qt4/LaTeXHighlighter.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// -*- C++ -*-
|
||||||
|
/**
|
||||||
|
* \file LaTeXHighlighter.h
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LATEXHIGHLIGHTER_H
|
||||||
|
#define LATEXHIGHLIGHTER_H
|
||||||
|
|
||||||
|
#include <QSyntaxHighlighter>
|
||||||
|
#include <QTextCharFormat>
|
||||||
|
|
||||||
|
class QTextDocument;
|
||||||
|
class QString;
|
||||||
|
|
||||||
|
namespace lyx {
|
||||||
|
namespace frontend {
|
||||||
|
|
||||||
|
// used already twice...
|
||||||
|
class LaTeXHighlighter : public QSyntaxHighlighter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LaTeXHighlighter(QTextDocument * parent);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void highlightBlock(QString const & text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QTextCharFormat commentFormat;
|
||||||
|
QTextCharFormat keywordFormat;
|
||||||
|
QTextCharFormat mathFormat;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace frontend
|
||||||
|
} // namespace lyx
|
||||||
|
|
||||||
|
#endif // LATEXHIGHLIGHTER
|
@ -122,6 +122,7 @@ SOURCEFILES = \
|
|||||||
InsertTableWidget.cpp \
|
InsertTableWidget.cpp \
|
||||||
LengthCombo.cpp \
|
LengthCombo.cpp \
|
||||||
LyXFileDialog.cpp \
|
LyXFileDialog.cpp \
|
||||||
|
LaTeXHighlighter.cpp \
|
||||||
PanelStack.cpp \
|
PanelStack.cpp \
|
||||||
qt_helpers.cpp \
|
qt_helpers.cpp \
|
||||||
TocModel.cpp \
|
TocModel.cpp \
|
||||||
@ -206,6 +207,7 @@ MOCHEADER = \
|
|||||||
InsertTableWidget.h \
|
InsertTableWidget.h \
|
||||||
LengthCombo.h \
|
LengthCombo.h \
|
||||||
LyXFileDialog.h \
|
LyXFileDialog.h \
|
||||||
|
LaTeXHighlighter.h \
|
||||||
PanelStack.h \
|
PanelStack.h \
|
||||||
qlkey.h \
|
qlkey.h \
|
||||||
TocModel.h \
|
TocModel.h \
|
||||||
|
Loading…
Reference in New Issue
Block a user