insilmaril@435: /**************************************************************************** insilmaril@435: ** insilmaril@435: ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved. insilmaril@435: ** insilmaril@435: ** This file is part of the example classes of the Qt Toolkit. insilmaril@435: ** insilmaril@435: ** This file may be used under the terms of the GNU General Public insilmaril@435: ** License version 2.0 as published by the Free Software Foundation insilmaril@435: ** and appearing in the file LICENSE.GPL included in the packaging of insilmaril@435: ** this file. Please review the following information to ensure GNU insilmaril@435: ** General Public Licensing requirements will be met: insilmaril@435: ** http://www.trolltech.com/products/qt/opensource.html insilmaril@435: ** insilmaril@435: ** If you are unsure which license is appropriate for your use, please insilmaril@435: ** review the following information: insilmaril@435: ** http://www.trolltech.com/products/qt/licensing.html or contact the insilmaril@435: ** sales department at sales@trolltech.com. insilmaril@435: ** insilmaril@435: ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE insilmaril@435: ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. insilmaril@435: ** insilmaril@435: ****************************************************************************/ insilmaril@435: insilmaril@435: // highlighting rules have been adapted by Uwe Drechsel to match vym syntax insilmaril@435: insilmaril@435: insilmaril@435: #include insilmaril@435: insilmaril@435: #include "highlighter.h" insilmaril@435: insilmaril@435: Highlighter::Highlighter(QTextDocument *parent) insilmaril@435: : QSyntaxHighlighter(parent) insilmaril@435: { insilmaril@435: HighlightingRule rule; insilmaril@435: insilmaril@435: keywordFormat.setForeground(Qt::darkBlue); insilmaril@435: keywordFormat.setFontWeight(QFont::Bold); insilmaril@435: QStringList keywordPatterns; insilmaril@447: keywordPatterns << "\\baddBranch\\b" insilmaril@447: << "\\baddBranchBefore\\b" insilmaril@447: << "\\baddMapInsert\\b" insilmaril@447: << "\\baddMapReplace\\b" insilmaril@447: << "\\bcolorBranch\\b" insilmaril@447: << "\\bcolorSubtree\\b" insilmaril@502: << "\\bcopy\\b" insilmaril@447: << "\\bcut\\b" insilmaril@447: << "\\bdelete\\b" insilmaril@502: << "\\bdeleteKeepChilds\\b" insilmaril@502: << "\\bdeleteChilds\\b" insilmaril@517: << "\\bexportASCII\\b" insilmaril@517: << "\\bexportImage\\b" insilmaril@524: << "\\bexportXHTML\\b" insilmaril@524: << "\\bexportXML\\b" insilmaril@488: << "\\bimportDir\\b" insilmaril@447: << "\\blinkTo\\b" insilmaril@447: << "\\bloadImage\\b" insilmaril@447: << "\\bmoveBranchUp\\b" insilmaril@447: << "\\bmoveBranchDown\\b" insilmaril@447: << "\\bmove\\b" insilmaril@447: << "\\bmoveRel\\b" insilmaril@502: << "\\bnop\\b" insilmaril@435: << "\\bpaste\\b" insilmaril@502: << "\\bqa\\b" insilmaril@435: << "\\bsaveImage\\b" insilmaril@435: << "\\bscroll\\b" insilmaril@447: << "\\bselect\\b" insilmaril@447: << "\\bselectLastBranch\\b" insilmaril@447: << "\\bselectLastImage\\b" insilmaril@482: << "\\bsetFrameType\\b" insilmaril@482: << "\\bsetFramePenColor\\b" insilmaril@482: << "\\bsetFrameBrushColor\\b" insilmaril@482: << "\\bsetFramePadding\\b" insilmaril@482: << "\\bsetFrameBorderWidth\\b" insilmaril@488: << "\\bsetHideLinkUnselected\\b" insilmaril@435: << "\\bsetMapAuthor\\b" insilmaril@435: << "\\bsetMapComment\\b" insilmaril@435: << "\\bsetMapBackgroundColor\\b" insilmaril@435: << "\\bsetMapDefLinkColor\\b" insilmaril@435: << "\\bsetMapDefLinkStyle\\b" insilmaril@435: << "\\bsetHeading\\b" insilmaril@435: << "\\bsetHideExport\\b" insilmaril@435: << "\\bsetIncludeImagesHorizontally\\b" insilmaril@435: << "\\bsetIncludeImagesVertically\\b" insilmaril@435: << "\\bsetURL\\b" insilmaril@435: << "\\bsetVymLink\\b" insilmaril@435: << "\\bsetFlag\\b" insilmaril@447: << "\\btoggleFlag\\b" insilmaril@435: << "\\bunscroll\\b" insilmaril@471: << "\\bunscrollChilds\\b" insilmaril@435: << "\\bunsetFlag\\b" insilmaril@435: ; insilmaril@435: foreach (QString pattern, keywordPatterns) { insilmaril@435: rule.pattern = QRegExp(pattern); insilmaril@435: rule.format = keywordFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: } insilmaril@435: insilmaril@435: // QT keywords insilmaril@435: /* insilmaril@435: classFormat.setFontWeight(QFont::Bold); insilmaril@435: classFormat.setForeground(Qt::darkMagenta); insilmaril@435: rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); insilmaril@435: rule.format = classFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: */ insilmaril@435: insilmaril@435: // Single line comments insilmaril@435: singleLineCommentFormat.setForeground(Qt::red); insilmaril@435: rule.pattern = QRegExp("#[^\n]*"); insilmaril@435: rule.format = singleLineCommentFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: insilmaril@447: // multiline comments insilmaril@435: multiLineCommentFormat.setForeground(Qt::red); insilmaril@435: commentStartExpression = QRegExp("/\\*"); insilmaril@435: commentEndExpression = QRegExp("\\*/"); insilmaril@435: insilmaril@435: // Quotations insilmaril@435: quotationFormat.setForeground(Qt::darkGreen); insilmaril@435: rule.pattern = QRegExp("\".*\""); insilmaril@435: rule.format = quotationFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: insilmaril@435: QStringList valuePatterns; insilmaril@435: valuePatterns << "\\btrue\\b" << "\\bfalse\\b"; insilmaril@435: foreach (QString pattern, valuePatterns) { insilmaril@435: rule.pattern = QRegExp(pattern); insilmaril@435: rule.format = quotationFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: } insilmaril@435: insilmaril@435: insilmaril@435: insilmaril@435: // Funtions insilmaril@435: /* insilmaril@435: functionFormat.setFontItalic(true); insilmaril@435: functionFormat.setForeground(Qt::blue); insilmaril@435: rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); insilmaril@435: rule.format = functionFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: */ insilmaril@435: insilmaril@435: } insilmaril@435: insilmaril@435: void Highlighter::highlightBlock(const QString &text) insilmaril@435: { insilmaril@435: foreach (HighlightingRule rule, highlightingRules) { insilmaril@435: QRegExp expression(rule.pattern); insilmaril@435: int index = text.indexOf(expression); insilmaril@435: while (index >= 0) { insilmaril@435: int length = expression.matchedLength(); insilmaril@435: setFormat(index, length, rule.format); insilmaril@435: index = text.indexOf(expression, index + length); insilmaril@435: } insilmaril@435: } insilmaril@435: setCurrentBlockState(0); insilmaril@435: insilmaril@435: int startIndex = 0; insilmaril@435: if (previousBlockState() != 1) insilmaril@435: startIndex = text.indexOf(commentStartExpression); insilmaril@435: insilmaril@435: while (startIndex >= 0) { insilmaril@435: int endIndex = text.indexOf(commentEndExpression, startIndex); insilmaril@435: int commentLength; insilmaril@435: if (endIndex == -1) { insilmaril@435: setCurrentBlockState(1); insilmaril@435: commentLength = text.length() - startIndex; insilmaril@435: } else { insilmaril@435: commentLength = endIndex - startIndex insilmaril@435: + commentEndExpression.matchedLength(); insilmaril@435: } insilmaril@435: setFormat(startIndex, commentLength, multiLineCommentFormat); insilmaril@435: startIndex = text.indexOf(commentStartExpression, insilmaril@435: startIndex + commentLength); insilmaril@435: } insilmaril@435: }