3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "QViewSource.h"
16 #include "QViewSourceDialog.h"
17 #include "qt_helpers.h"
23 latexHighlighter::latexHighlighter(QTextDocument * parent) :
24 QSyntaxHighlighter(parent)
26 keywordFormat.setForeground(Qt::darkBlue);
27 keywordFormat.setFontWeight(QFont::Bold);
28 commentFormat.setForeground(Qt::gray);
29 mathFormat.setForeground(Qt::red);
33 void latexHighlighter::highlightBlock(QString const & text)
36 QRegExp exprMath("\\$[^\\$]*\\$");
37 int index = text.indexOf(exprMath);
39 int length = exprMath.matchedLength();
40 setFormat(index, length, mathFormat);
41 index = text.indexOf(exprMath, index + length);
44 QRegExp exprStartDispMath("(\\\\\\[|"
45 "\\\\begin\\{equation\\**\\}|"
46 "\\\\begin\\{eqnarray\\**\\}|"
47 "\\\\begin\\{align(ed|at)*\\**\\}|"
48 "\\\\begin\\{flalign\\**\\}|"
49 "\\\\begin\\{gather\\**\\}|"
50 "\\\\begin\\{multline\\**\\}|"
51 "\\\\begin\\{array\\**\\}|"
52 "\\\\begin\\{cases\\**\\}"
54 QRegExp exprEndDispMath("(\\\\\\]|"
55 "\\\\end\\{equation\\**\\}|"
56 "\\\\end\\{eqnarray\\**\\}|"
57 "\\\\end\\{align(ed|at)*\\**\\}|"
58 "\\\\end\\{flalign\\**\\}|"
59 "\\\\end\\{gather\\**\\}|"
60 "\\\\end\\{multline\\**\\}|"
61 "\\\\end\\{array\\**\\}|"
62 "\\\\end\\{cases\\**\\}"
65 // if previous block was in 'disp math'
66 // start search from 0 (for end disp math)
67 // otherwise, start search from 'begin disp math'
68 if (previousBlockState() != 1)
69 startIndex = text.indexOf(exprStartDispMath);
70 while (startIndex >= 0) {
71 int endIndex = text.indexOf(exprEndDispMath, startIndex);
74 setCurrentBlockState(1);
75 length = text.length() - startIndex;
77 length = endIndex - startIndex + exprEndDispMath.matchedLength();
79 setFormat(startIndex, length, mathFormat);
80 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
83 QRegExp exprKeyword("\\\\[A-Za-z]+");
84 index = text.indexOf(exprKeyword);
86 int length = exprKeyword.matchedLength();
87 setFormat(index, length, keywordFormat);
88 index = text.indexOf(exprKeyword, index + length);
91 QRegExp exprComment("(^|[^\\\\])%.*$");
92 index = text.indexOf(exprComment);
94 int length = exprComment.matchedLength();
95 setFormat(index, length, commentFormat);
96 index = text.indexOf(exprComment, index + length);
101 QViewSource::QViewSource(Dialog & parent)
102 : ControlViewSource(parent)
104 document_ = new QTextDocument(this);
105 // set syntex highlighting
106 highlighter_ = new latexHighlighter(document_);
110 void QViewSource::update(bool full_source)
112 document_->setPlainText(toqstr(updateContent(full_source)));
116 } // namespace frontend
119 #include "QViewSource_moc.cpp"