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