]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
30a50f8123dc487b8b853833fa660420e32bd0aa
[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 <config.h>
12
13 #include "LaTeXHighlighter.h"
14
15 #include <QString>
16 #include <QTextDocument>
17
18 namespace lyx {
19 namespace frontend {
20
21 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
22         : QSyntaxHighlighter(parent)
23 {
24         keywordFormat.setForeground(Qt::darkBlue);
25         keywordFormat.setFontWeight(QFont::Bold);
26         commentFormat.setForeground(Qt::darkGray);
27         mathFormat.setForeground(Qt::red);
28 }
29
30
31 void LaTeXHighlighter::highlightBlock(QString const & text)
32 {
33         // $ $
34         static const QRegExp exprMath("\\$[^\\$]*\\$");
35         int index = text.indexOf(exprMath);
36         while (index >= 0) {
37                 int length = exprMath.matchedLength();
38                 setFormat(index, length, mathFormat);
39                 index = text.indexOf(exprMath, index + length);
40         }
41         // [ ]
42         static const QRegExp exprStartDispMath("(\\\\\\[|"
43                 "\\\\begin\\{equation\\**\\}|"
44                 "\\\\begin\\{eqnarray\\**\\}|"
45                 "\\\\begin\\{align(ed|at)*\\**\\}|"
46                 "\\\\begin\\{flalign\\**\\}|"
47                 "\\\\begin\\{gather\\**\\}|"
48                 "\\\\begin\\{multline\\**\\}|"
49                 "\\\\begin\\{array\\**\\}|"
50                 "\\\\begin\\{cases\\**\\}"
51                 ")");
52         static const QRegExp exprEndDispMath("(\\\\\\]|"
53                 "\\\\end\\{equation\\**\\}|"
54                 "\\\\end\\{eqnarray\\**\\}|"
55                 "\\\\end\\{align(ed|at)*\\**\\}|"
56                 "\\\\end\\{flalign\\**\\}|"
57                 "\\\\end\\{gather\\**\\}|"
58                 "\\\\end\\{multline\\**\\}|"
59                 "\\\\end\\{array\\**\\}|"
60                 "\\\\end\\{cases\\**\\}"
61                 ")");
62         int startIndex = 0;
63         // if previous block was in 'disp math'
64         // start search from 0 (for end disp math)
65         // otherwise, start search from 'begin disp math'
66         if (previousBlockState() != 1)
67                 startIndex = text.indexOf(exprStartDispMath);
68         while (startIndex >= 0) {
69                 int endIndex = text.indexOf(exprEndDispMath, startIndex);
70                 int length;
71                 if (endIndex == -1) {
72                         setCurrentBlockState(1);
73                         length = text.length() - startIndex;
74                 } else {
75                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
76                 }
77                 setFormat(startIndex, length, mathFormat);
78                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
79         }
80         // \whatever
81         static const QRegExp exprKeyword("\\\\[A-Za-z]+");
82         index = text.indexOf(exprKeyword);
83         while (index >= 0) {
84                 int length = exprKeyword.matchedLength();
85                 setFormat(index, length, keywordFormat);
86                 index = text.indexOf(exprKeyword, index + length);
87         }
88         // %comment
89         // Treat a line as a comment starting at a percent sign
90         // * that is the first character in a line
91         // * that is preceded by 
92         // ** an even number of backslashes
93         // ** any character other than a backslash                   
94         QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$"); 
95         text.indexOf(exprComment);
96         index = exprComment.pos(1);
97         while (index >= 0) {
98                 int const length = exprComment.matchedLength() 
99                                  - (index - exprComment.pos(0));
100                 setFormat(index, length, commentFormat);
101                 text.indexOf(exprComment, index + length);
102                 index = exprComment.pos(1);
103         }
104 }
105
106 } // namespace frontend
107 } // namespace lyx