]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/LaTeXHighlighter.cpp
aced09f56f1ee7425657479d5e1dc46074122b7f
[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 #include "qt_helpers.h"
15
16 #include "support/lassert.h"
17
18 #include <QString>
19 #include <QTextDocument>
20
21
22 using namespace lyx::support;
23
24 namespace lyx {
25 namespace frontend {
26
27 LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent)
28         : QSyntaxHighlighter(parent)
29 {
30         keywordFormat.setForeground(Qt::darkBlue);
31         keywordFormat.setFontWeight(QFont::Bold);
32         commentFormat.setForeground(Qt::darkGray);
33         mathFormat.setForeground(Qt::red);
34         warningFormat.setForeground(Qt::red);
35         warningFormat.setFontWeight(QFont::Bold);
36 }
37
38
39 void LaTeXHighlighter::highlightBlock(QString const & text)
40 {
41         // $ $
42         static const QRegExp exprMath("\\$[^\\$]*\\$");
43         int index = text.indexOf(exprMath);
44         while (index >= 0) {
45                 int length = exprMath.matchedLength();
46                 setFormat(index, length, mathFormat);
47                 index = text.indexOf(exprMath, index + length);
48         }
49         // [ ]
50         static const QRegExp exprStartDispMath("(\\\\\\[|"
51                 "\\\\begin\\{equation\\**\\}|"
52                 "\\\\begin\\{eqnarray\\**\\}|"
53                 "\\\\begin\\{align(ed|at)*\\**\\}|"
54                 "\\\\begin\\{flalign\\**\\}|"
55                 "\\\\begin\\{gather\\**\\}|"
56                 "\\\\begin\\{multline\\**\\}|"
57                 "\\\\begin\\{array\\**\\}|"
58                 "\\\\begin\\{cases\\**\\}"
59                 ")");
60         static const QRegExp exprEndDispMath("(\\\\\\]|"
61                 "\\\\end\\{equation\\**\\}|"
62                 "\\\\end\\{eqnarray\\**\\}|"
63                 "\\\\end\\{align(ed|at)*\\**\\}|"
64                 "\\\\end\\{flalign\\**\\}|"
65                 "\\\\end\\{gather\\**\\}|"
66                 "\\\\end\\{multline\\**\\}|"
67                 "\\\\end\\{array\\**\\}|"
68                 "\\\\end\\{cases\\**\\}"
69                 ")");
70         int startIndex = 0;
71         // if previous block was in 'disp math'
72         // start search from 0 (for end disp math)
73         // otherwise, start search from 'begin disp math'
74         if (previousBlockState() != 1)
75                 startIndex = text.indexOf(exprStartDispMath);
76         // We try to avoid infinite loops...
77         static size_t max_loop = 1000;
78         for (size_t i = 0; i != max_loop; ++i) {
79                 int endIndex = text.indexOf(exprEndDispMath, startIndex);
80                 int length;
81                 if (endIndex == -1) {
82                         setCurrentBlockState(1);
83                         length = text.length() - startIndex;
84                 } else {
85                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
86                 }
87                 setFormat(startIndex, length, mathFormat);
88                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
89                 if (startIndex == -1)
90                         break;
91         }
92         LASSERT(startIndex >= 0, return);
93
94         // \whatever
95         static const QRegExp exprKeyword("\\\\[A-Za-z]+");
96         index = text.indexOf(exprKeyword);
97         for (size_t i = 0; i != max_loop; ++i) {
98                 int length = exprKeyword.matchedLength();
99                 setFormat(index, length, keywordFormat);
100                 index = text.indexOf(exprKeyword, index + length);
101                 if (index == -1)
102                         break;
103         }
104         LASSERT(index >= 0, return);
105
106         // %comment
107         // Treat a line as a comment starting at a percent sign
108         // * that is the first character in a line
109         // * that is preceded by 
110         // ** an even number of backslashes
111         // ** any character other than a backslash
112         QRegExp exprComment("(?:^|[^\\\\])(?:\\\\\\\\)*(%).*$"); 
113         text.indexOf(exprComment);
114         index = exprComment.pos(1);
115         for (size_t i = 0; i != max_loop; ++i) {
116                 int const length = exprComment.matchedLength() 
117                                  - (index - exprComment.pos(0));
118                 setFormat(index, length, commentFormat);
119                 text.indexOf(exprComment, index + length);
120                 index = exprComment.pos(1);
121                 if (index == -1)
122                         break;
123         }
124         LASSERT(index >= 0, return);
125
126         // <LyX Warning: ...>
127         QString lyxwarn = qt_("LyX Warning: ");
128         QRegExp exprWarning("<" + lyxwarn + "[^<]*>");
129         index = text.indexOf(exprWarning);
130         for (size_t i = 0; i != max_loop; ++i) {
131                 int length = exprWarning.matchedLength();
132                 setFormat(index, length, warningFormat);
133                 index = text.indexOf(exprWarning, index + length);
134                 if (index == -1)
135                         break;
136         }
137         LASSERT(index >= 0, return);
138 }
139
140 } // namespace frontend
141 } // namespace lyx