1
// =============================================================================
3
// --- kvi_syntaxhighlighter.cpp ---
5
// This file is part of the KVIrc IRC client distribution
6
// Copyright (C) 2003 Robin Verduijn <robin@debian.org>
8
// This program is FREE software. You can redistribute it and/or
9
// modify it under the terms of the GNU General Public License
10
// as published by the Free Software Foundation; either version 2
11
// of the License, or (at your opinion) any later version.
13
// This program is distributed in the HOPE that it will be USEFUL,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
// See the GNU General Public License for more details.
18
// You should have received a copy of the GNU General Public License
19
// along with this program. If not, write to the Free Software Foundation,
20
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
// =============================================================================
24
#define _KVI_DEBUG_CHECK_RANGE_
25
#define _KVI_DEBUG_CLASS_NAME_ "KviSyntaxHighlighter"
27
#define _KVI_SCRIPTEDITORWIDGET_CPP_
29
#include "kvi_config.h"
30
#include "kvi_options.h"
31
#include "kvi_syntaxhighlighter.h"
33
#define KVI_SYNTAX_STATE_NEWCOMMAND 0
34
#define KVI_SYNTAX_STATE_INCOMMAND 1
35
#define KVI_SYNTAX_STATE_INSTRING 2
36
#define KVI_SYNTAX_STATE_INCOMMENT 3
38
KviSyntaxHighlighter::KviSyntaxHighlighter(QTextEdit *textEdit)
39
: QSyntaxHighlighter(textEdit)
44
KviSyntaxHighlighter::~KviSyntaxHighlighter()
49
int KviSyntaxHighlighter::highlightParagraph(const QString &text, int /* ignored */)
51
const char *pC = text.ascii();
54
m_textState = KVI_SYNTAX_STATE_NEWCOMMAND;
58
while( (*pC) && (*pC != '\n') ) {
59
len = extractNextWord(pC);
60
clr = getColor(pC, len);
63
setFormat(current, len, *clr);
67
return KVI_SYNTAX_STATE_NEWCOMMAND;
70
int KviSyntaxHighlighter::extractNextWord(const char *s)
74
if( (*s == ' ') || (*s == '\t') ) {
77
while( ( *s == ' ') || (*s == '\t') ) {
84
if( (*s == '#') && m_textState == KVI_SYNTAX_STATE_NEWCOMMAND ) {
87
while( (*s) && (*s != ' ') && (*s != '\t') && (*s != ';') && (*s != '\n') ) {
94
if( m_textState == KVI_SYNTAX_STATE_INCOMMENT ) {
95
while( (*s) && (*s != ' ') && (*s != '\t') && (*s != ';') && (*s != '\n') ) {
106
if( *s && (*s != '\n') && (*s != ' ') && (*s != '\t') )
114
if( isalnum(*s) || (*s == '_') || (*s == '.') ) {
115
// Command or normal text
118
while( *s && (isalnum(*s) || (*s == '_') || (*s == '.')) ) {
125
if( (*s == '%') || (*s == '$') ) {
126
// Variable or identifier
129
while( *s && (isalnum(*s) || (*s == '_') || (*s == '.') || (*s == ':')) ) {
136
if( (*s == '{') || (*s == '}') )
139
if( (*s == '(') || (*s == ')') || (*s == '[') ||
140
(*s == ']') || (*s == ',') || (*s == ':') ||
141
(*s == ';') || (*s == '=') || (*s == '-') ||
142
(*s == '<') || (*s == '>') || (*s == '&') ||
143
(*s == '|') || (*s == '+') || (*s == '*') ||
144
(*s == '/') || (*s == '!') || (*s == '^')
149
(*s == '(') || (*s == ')') || (*s == '[') ||
150
(*s == ']') || (*s == ',') || (*s == ':') ||
151
(*s == ';') || (*s == '=') || (*s == '-') ||
152
(*s == '<') || (*s == '>') || (*s == '&') ||
153
(*s == '|') || (*s == '+') || (*s == '*') ||
154
(*s == '/') || (*s == '!') || (*s == '^')
165
while( (*s) && (isalnum(*s)) ) {
172
QColor *KviSyntaxHighlighter::getColor(const char *s, int len)
174
if( (*s == ' ') || (*s == '\t') )
177
if( m_textState == KVI_SYNTAX_STATE_NEWCOMMAND ) {
179
m_textState = KVI_SYNTAX_STATE_INCOMMENT;
180
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_COMMENT];
182
m_textState = KVI_SYNTAX_STATE_INCOMMAND;
183
if( isalnum(*s) || (*s == '_') || (*s == '.') )
184
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_COMMAND];
186
} else if( m_textState == KVI_SYNTAX_STATE_INCOMMENT ) {
187
if( (*s != ';') && (*s != '\n') )
188
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_COMMENT];
192
if( m_textState != KVI_SYNTAX_STATE_INSTRING )
193
m_textState = KVI_SYNTAX_STATE_INSTRING;
195
m_textState = KVI_SYNTAX_STATE_INCOMMAND;
196
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_STRING];
199
if( m_textState == KVI_SYNTAX_STATE_INSTRING )
200
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_STRING];
203
m_textState = KVI_SYNTAX_STATE_NEWCOMMAND;
206
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_ESCAPE];
208
if( (*s == '{') || (*s == '}') )
209
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_BRACE];
212
if( (!*s) && (!(isalnum(*s))) && (*s != '_') && (*s != '.') )
213
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_OPERATOR];
215
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_LOCVAR]; // Local variable
216
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_GLOBVAR];
221
if( len && (*s == 't') || (*s == 'T') ) {
224
if( len && (*s == 'h') || (*s == 'H') ) {
227
if( kvi_strEqualCIN(s, "is", len) )
228
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_THIS];
231
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_IDENT];
234
if( (*s == '(') || (*s == ')') || (*s == '[') ||
235
(*s == ']') || (*s == ',') || (*s == ':') ||
236
(*s == ';') || (*s == '=') || (*s == '-') ||
237
(*s == '<') || (*s == '>') || (*s == '&') ||
238
(*s == '|') || (*s == '+') || (*s == '*') ||
239
(*s == '/') || (*s == '!') || (*s == '^')
241
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_OPERATOR];
243
return g_pOptions->m_pScriptEditorClr[KVI_SCRIPTEDITOR_CLR_TEXT];