Mercurial > octave-nkf
annotate gui/src/TerminalHighlighter.cpp @ 13666:68c50b393f1d
Improved highlighting and incremented version.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Sat, 10 Sep 2011 09:38:49 +0200 |
parents | ddc3c20d0e2f |
children | 9b74f97919e1 |
rev | line source |
---|---|
13665
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
1 /* OctaveGUI - A graphical user interface for Octave |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
2 * Copyright (C) 2011 Jacob Dawid |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
3 * jacob.dawid@googlemail.com |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
4 * |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
5 * This program is free software: you can redistribute it and/or modify |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
6 * it under the terms of the GNU General Public License as published by |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
7 * the Free Software Foundation, either version 3 of the License, or |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
8 * (at your option) any later version. |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
9 * |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
10 * This program is distributed in the hope that it will be useful, |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
13 * GNU General Public License for more details. |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
14 * |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
17 */ |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
18 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
19 #include "ResourceManager.h" |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
20 #include "TerminalHighlighter.h" |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
21 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
22 TerminalHighlighter::TerminalHighlighter(QTextDocument *parent) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
23 : QSyntaxHighlighter(parent) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
24 { |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
25 HighlightingRule rule; |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
26 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
27 keywordFormat.setForeground(Qt::darkBlue); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
28 QStringList keywordPatterns |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
29 = QString(ResourceManager::instance ()->octaveKeywords ()).split(" ", QString::SkipEmptyParts); |
13666
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
30 keywordPatterns << "GNU" << "Octave" << "OctaveGUI"; |
13665
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
31 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
32 for (int i = 0; i < keywordPatterns.size (); i++) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
33 keywordPatterns.replace(i, QString("\\b%1\\b").arg(keywordPatterns.at (i))); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
34 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
35 foreach (const QString &pattern, keywordPatterns) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
36 { |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
37 rule.pattern = QRegExp(pattern); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
38 rule.format = keywordFormat; |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
39 highlightingRules.append(rule); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
40 } |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
41 |
13666
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
42 numberFormat.setForeground(Qt::darkRed); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
43 rule.pattern = QRegExp("\\b[0-9\\.\\+\\-\\^]+\\b"); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
44 rule.format = numberFormat; |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
45 highlightingRules.append(rule); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
46 |
13665
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
47 doubleQouteFormat.setForeground(Qt::darkGreen); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
48 rule.pattern = QRegExp("\"[^\"]*\""); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
49 rule.format = doubleQouteFormat; |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
50 highlightingRules.append(rule); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
51 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
52 functionFormat.setFontItalic(true); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
53 functionFormat.setForeground(Qt::blue); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
54 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+\\s*(?=\\()"); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
55 rule.format = functionFormat; |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
56 highlightingRules.append(rule); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
57 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
58 urlFormat.setForeground(Qt::darkYellow); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
59 rule.pattern = QRegExp("((?:https?|ftp)://\\S+)"); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
60 rule.format = urlFormat; |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
61 highlightingRules.append(rule); |
13666
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
62 |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
63 subCaptionFormat.setForeground (Qt::black); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
64 subCaptionFormat.setFontItalic (true); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
65 rule.pattern = QRegExp("^\\s+\\*.+$"); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
66 rule.format = subCaptionFormat; |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
67 highlightingRules.append(rule); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
68 |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
69 captionFormat.setForeground(Qt::black); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
70 captionFormat.setFontWeight(QFont::Bold); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
71 rule.pattern = QRegExp("^\\s+\\*\\*.+$"); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
72 rule.format = captionFormat; |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
73 highlightingRules.append(rule); |
68c50b393f1d
Improved highlighting and incremented version.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13665
diff
changeset
|
74 |
13665
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
75 } |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
76 |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
77 void TerminalHighlighter::highlightBlock(const QString &text) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
78 { |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
79 foreach (const HighlightingRule &rule, highlightingRules) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
80 { |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
81 QRegExp expression(rule.pattern); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
82 int index = expression.indexIn(text); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
83 while (index >= 0) |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
84 { |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
85 int length = expression.matchedLength(); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
86 setFormat(index, length, rule.format); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
87 index = expression.indexIn(text, index + length); |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
88 } |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
89 } |
ddc3c20d0e2f
Implemented highlighting of octave keywords in terminal.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
diff
changeset
|
90 } |