]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
move LaTeXHighlighte
[lyx.git] / src / frontends / qt4 / LaTeXHighlighter.cpp
1 /**
2  * \file LaTeXHighlighter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include "LaTeXHighlighter.h"
12
13 #include <QString>
14 #include <QTextDocument>
15
16 namespace lyx {
17 namespace frontend {
18
19 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
20         : QSyntaxHighlighter(parent)
21 {
22         keywordFormat.setForeground(Qt::darkBlue);
23         keywordFormat.setFontWeight(QFont::Bold);
24         commentFormat.setForeground(Qt::darkGray);
25         mathFormat.setForeground(Qt::red);
26 }
27
28
29 void LaTeXHighlighter::highlightBlock(QString const & text)
30 {
31         // $ $
32         static const QRegExp exprMath("\\$[^\\$]*\\$");
33         int index = text.indexOf(exprMath);
34         while (index >= 0) {
35                 int length = exprMath.matchedLength();
36                 setFormat(index, length, mathFormat);
37                 index = text.indexOf(exprMath, index + length);
38         }
39         // [ ]
40         static const QRegExp exprStartDispMath("(\\\\\\[|"
41                 "\\\\begin\\{equation\\**\\}|"
42                 "\\\\begin\\{eqnarray\\**\\}|"
43                 "\\\\begin\\{align(ed|at)*\\**\\}|"
44                 "\\\\begin\\{flalign\\**\\}|"
45                 "\\\\begin\\{gather\\**\\}|"
46                 "\\\\begin\\{multline\\**\\}|"
47                 "\\\\begin\\{array\\**\\}|"
48                 "\\\\begin\\{cases\\**\\}"
49                 ")");
50         static const QRegExp exprEndDispMath("(\\\\\\]|"
51                 "\\\\end\\{equation\\**\\}|"
52                 "\\\\end\\{eqnarray\\**\\}|"
53                 "\\\\end\\{align(ed|at)*\\**\\}|"
54                 "\\\\end\\{flalign\\**\\}|"
55                 "\\\\end\\{gather\\**\\}|"
56                 "\\\\end\\{multline\\**\\}|"
57                 "\\\\end\\{array\\**\\}|"
58                 "\\\\end\\{cases\\**\\}"
59                 ")");
60         int startIndex = 0;
61         // if previous block was in 'disp math'
62         // start search from 0 (for end disp math)
63         // otherwise, start search from 'begin disp math'
64         if (previousBlockState() != 1)
65                 startIndex = text.indexOf(exprStartDispMath);
66         while (startIndex >= 0) {
67                 int endIndex = text.indexOf(exprEndDispMath, startIndex);
68                 int length;
69                 if (endIndex == -1) {
70                         setCurrentBlockState(1);
71                         length = text.length() - startIndex;
72                 } else {
73                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
74                 }
75                 setFormat(startIndex, length, mathFormat);
76                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
77         }
78         // \whatever
79         static const QRegExp exprKeyword("\\\\[A-Za-z]+");
80         index = text.indexOf(exprKeyword);
81         while (index >= 0) {
82                 int length = exprKeyword.matchedLength();
83                 setFormat(index, length, keywordFormat);
84                 index = text.indexOf(exprKeyword, index + length);
85         }
86         // comment
87         static const QRegExp exprComment("(^|[^\\\\])%.*$");
88         index = text.indexOf(exprComment);
89         while (index >= 0) {
90                 int const length = exprComment.matchedLength();
91                 setFormat(index, length, commentFormat);
92                 index = text.indexOf(exprComment, index + length);
93         }
94 }
95
96 } // namespace frontend
97 } // namespace lyx