]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QViewSource.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / QViewSource.C
1 /**
2  * \file QViewSource.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Bo Peng
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QViewSource.h"
16 #include "QViewSourceDialog.h"
17 #include "qt_helpers.h"
18
19 namespace lyx {
20 namespace frontend {
21
22
23 latexHighlighter::latexHighlighter(QTextDocument * parent) :
24         QSyntaxHighlighter(parent)
25 {
26         keywordFormat.setForeground(Qt::darkBlue);
27         keywordFormat.setFontWeight(QFont::Bold);
28         commentFormat.setForeground(Qt::darkGray);
29         mathFormat.setForeground(Qt::red);
30 }
31
32
33 void latexHighlighter::highlightBlock(QString const & text)
34 {
35         // $ $ 
36         QRegExp exprMath("\\$[^\\$]*\\$");
37         int index = text.indexOf(exprMath);
38         while (index >= 0) {
39                 int length = exprMath.matchedLength();
40                 setFormat(index, length, mathFormat);
41                 index = text.indexOf(exprMath, index + length);
42         }
43         // [ ]
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\\**\\}"
53                 ")");
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\\**\\}"
63                 ")");
64         int startIndex = 0;
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);
72                 int length;
73                 if (endIndex == -1) {
74                         setCurrentBlockState(1);
75                         length = text.length() - startIndex;
76                 } else {
77                         length = endIndex - startIndex + exprEndDispMath.matchedLength();
78                 }
79                 setFormat(startIndex, length, mathFormat);
80                 startIndex = text.indexOf(exprStartDispMath, startIndex + length);
81         }
82         // \whatever
83         QRegExp exprKeyword("\\\\[A-Za-z]+");
84         index = text.indexOf(exprKeyword);
85         while (index >= 0) {
86                 int length = exprKeyword.matchedLength();
87                 setFormat(index, length, keywordFormat);
88                 index = text.indexOf(exprKeyword, index + length);
89         }
90         // comment
91         QRegExp exprComment("(^|[^\\\\])%.*$");
92         index = text.indexOf(exprComment);
93         while (index >= 0) {
94                 int length = exprComment.matchedLength();
95                 setFormat(index, length, commentFormat);
96                 index = text.indexOf(exprComment, index + length);
97         }
98 }
99
100
101 QViewSource::QViewSource(Dialog & parent)
102         : ControlViewSource(parent)
103 {
104         document_ = new QTextDocument(this);
105         // set syntex highlighting
106         highlighter_ = new latexHighlighter(document_);
107 }
108
109
110 void QViewSource::update(bool full_source)
111 {
112         document_->setPlainText(toqstr(updateContent(full_source)));
113 }
114
115
116 } // namespace frontend
117 } // namespace lyx
118
119 #include "QViewSource_moc.cpp"