Kate
katesyntaxmanager.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net> 00003 Copyright (C) 2003, 2004 Anders Lund <anders@alweb.dk> 00004 Copyright (C) 2003 Hamish Rodda <rodda@kde.org> 00005 Copyright (C) 2001,2002 Joseph Wenninger <jowenn@kde.org> 00006 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> 00007 Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de> 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Library General Public 00011 License version 2 as published by the Free Software Foundation. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 //BEGIN INCLUDES 00025 #include "katesyntaxmanager.h" 00026 #include "katesyntaxmanager.moc" 00027 00028 #include "katetextline.h" 00029 #include "katedocument.h" 00030 #include "katesyntaxdocument.h" 00031 #include "katerenderer.h" 00032 #include "kateglobal.h" 00033 #include "kateschema.h" 00034 #include "kateconfig.h" 00035 #include "kateextendedattribute.h" 00036 #include "katehighlight.h" 00037 00038 #include <kconfig.h> 00039 #include <kglobal.h> 00040 #include <kcomponentdata.h> 00041 #include <kmimetype.h> 00042 #include <klocale.h> 00043 #include <kmenu.h> 00044 #include <kcolorscheme.h> 00045 #include <kcolorutils.h> 00046 #include <kdebug.h> 00047 #include <kstandarddirs.h> 00048 #include <kmessagebox.h> 00049 #include <kapplication.h> 00050 00051 #include <QtCore/QSet> 00052 #include <QtGui/QAction> 00053 #include <QtCore/QStringList> 00054 #include <QtCore/QTextStream> 00055 //END 00056 00057 using namespace KTextEditor; 00058 00059 //BEGIN KateHlManager 00060 KateHlManager::KateHlManager() 00061 : QObject() 00062 , m_config ("katesyntaxhighlightingrc", KConfig::NoGlobals) 00063 , commonSuffixes (QString(".orig;.new;~;.bak;.BAK").split(';')) 00064 , syntax (new KateSyntaxDocument(&m_config)) 00065 , dynamicCtxsCount(0) 00066 , forceNoDCReset(false) 00067 { 00068 KateSyntaxModeList modeList = syntax->modeList(); 00069 for (int i=0; i < modeList.count(); i++) 00070 { 00071 KateHighlighting *hl = new KateHighlighting(modeList[i]); 00072 00073 int insert = 0; 00074 for (; insert <= hlList.count(); insert++) 00075 { 00076 if (insert == hlList.count()) 00077 break; 00078 00079 if ( QString(hlList.at(insert)->section() + hlList.at(insert)->nameTranslated()).toLower() 00080 > QString(hl->section() + hl->nameTranslated()).toLower() ) 00081 break; 00082 } 00083 00084 hlList.insert (insert, hl); 00085 hlDict.insert (hl->name(), hl); 00086 } 00087 00088 // Normal HL 00089 KateHighlighting *hl = new KateHighlighting(0); 00090 hlList.prepend (hl); 00091 hlDict.insert (hl->name(), hl); 00092 00093 lastCtxsReset.start(); 00094 } 00095 00096 KateHlManager::~KateHlManager() 00097 { 00098 delete syntax; 00099 qDeleteAll(hlList); 00100 } 00101 00102 KateHlManager *KateHlManager::self() 00103 { 00104 return KateGlobal::self ()->hlManager (); 00105 } 00106 00107 KateHighlighting *KateHlManager::getHl(int n) 00108 { 00109 if (n < 0 || n >= hlList.count()) 00110 n = 0; 00111 00112 return hlList.at(n); 00113 } 00114 00115 int KateHlManager::nameFind(const QString &name) 00116 { 00117 const QString lower_name = name.toLower(); 00118 int z (hlList.count() - 1); 00119 for (; z > 0; z--) 00120 if (hlList.at(z)->name().toLower() == lower_name) 00121 return z; 00122 00123 return z; 00124 } 00125 00126 uint KateHlManager::defaultStyles() 00127 { 00128 return 14; 00129 } 00130 00131 QString KateHlManager::defaultStyleName(int n, bool translateNames) 00132 { 00133 static QStringList names; 00134 static QStringList translatedNames; 00135 00136 if (names.isEmpty()) 00137 { 00138 names << "Normal"; 00139 names << "Keyword"; 00140 names << "Data Type"; 00141 names << "Decimal/Value"; 00142 names << "Base-N Integer"; 00143 names << "Floating Point"; 00144 names << "Character"; 00145 names << "String"; 00146 names << "Comment"; 00147 names << "Others"; 00148 names << "Alert"; 00149 names << "Function"; 00150 // this next one is for denoting the beginning/end of a user defined folding region 00151 names << "Region Marker"; 00152 // this one is for marking invalid input 00153 names << "Error"; 00154 00155 translatedNames << i18nc("@item:intable Text context", "Normal"); 00156 translatedNames << i18nc("@item:intable Text context", "Keyword"); 00157 translatedNames << i18nc("@item:intable Text context", "Data Type"); 00158 translatedNames << i18nc("@item:intable Text context", "Decimal/Value"); 00159 translatedNames << i18nc("@item:intable Text context", "Base-N Integer"); 00160 translatedNames << i18nc("@item:intable Text context", "Floating Point"); 00161 translatedNames << i18nc("@item:intable Text context", "Character"); 00162 translatedNames << i18nc("@item:intable Text context", "String"); 00163 translatedNames << i18nc("@item:intable Text context", "Comment"); 00164 translatedNames << i18nc("@item:intable Text context", "Others"); 00165 translatedNames << i18nc("@item:intable Text context", "Alert"); 00166 translatedNames << i18nc("@item:intable Text context", "Function"); 00167 // this next one is for denoting the beginning/end of a user defined folding region 00168 translatedNames << i18nc("@item:intable Text context", "Region Marker"); 00169 // this one is for marking invalid input 00170 translatedNames << i18nc("@item:intable Text context", "Error"); 00171 } 00172 00173 return translateNames ? translatedNames[n] : names[n]; 00174 } 00175 00176 void KateHlManager::getDefaults(const QString &schema, KateAttributeList &list) 00177 { 00178 KColorScheme scheme(QPalette::Active, KColorScheme::View); 00179 KColorScheme schemeSelected(QPalette::Active, KColorScheme::Selection); 00180 00183 00184 { // dsNormal 00185 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00186 attrib->setForeground( scheme.foreground().color() ); 00187 attrib->setSelectedForeground( schemeSelected.foreground().color() ); 00188 list.append(attrib); 00189 } 00190 { // dsKeyword 00191 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00192 attrib->setForeground( scheme.foreground().color() ); 00193 attrib->setSelectedForeground( schemeSelected.foreground().color() ); 00194 attrib->setFontBold(true); 00195 list.append(attrib); 00196 } 00197 { // dsDataType 00198 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00199 attrib->setForeground( scheme.foreground(KColorScheme::LinkText).color() ); 00200 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() ); 00201 list.append(attrib); 00202 } 00203 { // dsDecVal 00204 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00205 attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() ); 00206 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() ); 00207 list.append(attrib); 00208 } 00209 { // dsBaseN 00210 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00211 attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() ); 00212 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() ); 00213 list.append(attrib); 00214 } 00215 { // dsFloat 00216 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00217 attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() ); 00218 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() ); 00219 list.append(attrib); 00220 } 00221 { // dsChar 00222 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00223 attrib->setForeground( scheme.foreground(KColorScheme::ActiveText).color() ); 00224 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::ActiveText).color() ); 00225 list.append(attrib); 00226 } 00227 { // dsString 00228 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00229 attrib->setForeground( scheme.foreground(KColorScheme::NegativeText).color() ); 00230 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() ); 00231 list.append(attrib); 00232 } 00233 { // dsComment 00234 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00235 attrib->setForeground( scheme.foreground(KColorScheme::InactiveText).color() ); 00236 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::InactiveText).color() ); 00237 attrib->setFontItalic(true); 00238 list.append(attrib); 00239 } 00240 { // dsOthers 00241 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00242 attrib->setForeground( scheme.foreground(KColorScheme::PositiveText).color() ); 00243 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::PositiveText).color() ); 00244 list.append(attrib); 00245 } 00246 { // dsAlert 00247 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00248 attrib->setForeground( scheme.foreground(KColorScheme::NegativeText).color() ); 00249 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() ); 00250 attrib->setFontBold(true); 00251 attrib->setBackground( scheme.background(KColorScheme::NegativeBackground).color() ); 00252 list.append(attrib); 00253 } 00254 { // dsFunction 00255 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00256 attrib->setForeground( scheme.foreground(KColorScheme::VisitedText).color() ); 00257 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::VisitedText).color() ); 00258 list.append(attrib); 00259 } 00260 { // dsRegionMarker 00261 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00262 attrib->setForeground( scheme.foreground(KColorScheme::LinkText).color() ); 00263 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() ); 00264 attrib->setBackground( scheme.background(KColorScheme::LinkBackground).color() ); 00265 list.append(attrib); 00266 } 00267 { // dsError 00268 Attribute::Ptr attrib(new KTextEditor::Attribute()); 00269 attrib->setForeground( scheme.foreground(KColorScheme::NegativeText) ); 00270 attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() ); 00271 attrib->setFontUnderline(true); 00272 list.append(attrib); 00273 } 00274 00275 KConfigGroup config(KateHlManager::self()->self()->getKConfig(), 00276 "Default Item Styles - Schema " + schema); 00277 00278 for (uint z = 0; z < defaultStyles(); z++) 00279 { 00280 KTextEditor::Attribute::Ptr i = list.at(z); 00281 QStringList s = config.readEntry(defaultStyleName(z), QStringList()); 00282 if (!s.isEmpty()) 00283 { 00284 while( s.count()<9) 00285 s << ""; 00286 00287 QString tmp; 00288 QRgb col; 00289 00290 tmp=s[0]; if (!tmp.isEmpty()) { 00291 col=tmp.toUInt(0,16); i->setForeground(QColor(col)); } 00292 00293 tmp=s[1]; if (!tmp.isEmpty()) { 00294 col=tmp.toUInt(0,16); i->setSelectedForeground(QColor(col)); } 00295 00296 tmp=s[2]; if (!tmp.isEmpty()) i->setFontBold(tmp!="0"); 00297 00298 tmp=s[3]; if (!tmp.isEmpty()) i->setFontItalic(tmp!="0"); 00299 00300 tmp=s[4]; if (!tmp.isEmpty()) i->setFontStrikeOut(tmp!="0"); 00301 00302 tmp=s[5]; if (!tmp.isEmpty()) i->setFontUnderline(tmp!="0"); 00303 00304 tmp=s[6]; if (!tmp.isEmpty()) { 00305 if ( tmp != "-" ) 00306 { 00307 col=tmp.toUInt(0,16); 00308 i->setBackground(QColor(col)); 00309 } 00310 else 00311 i->clearBackground(); 00312 } 00313 tmp=s[7]; if (!tmp.isEmpty()) { 00314 if ( tmp != "-" ) 00315 { 00316 col=tmp.toUInt(0,16); 00317 i->setSelectedBackground(QColor(col)); 00318 } 00319 else 00320 i->clearProperty(KTextEditor::Attribute::SelectedBackground); 00321 } 00322 tmp=s[8]; if (!tmp.isEmpty() && tmp!=QLatin1String("---")) i->setFontFamily(tmp); 00323 } 00324 } 00325 } 00326 00327 void KateHlManager::setDefaults(const QString &schema, KateAttributeList &list) 00328 { 00329 KConfigGroup config(KateHlManager::self()->self()->getKConfig(), 00330 "Default Item Styles - Schema " + schema); 00331 00332 for (uint z = 0; z < defaultStyles(); z++) 00333 { 00334 QStringList settings; 00335 KTextEditor::Attribute::Ptr p = list.at(z); 00336 00337 settings<<(p->hasProperty(QTextFormat::ForegroundBrush)?QString::number(p->foreground().color().rgb(),16):""); 00338 settings<<(p->hasProperty(KTextEditor::Attribute::SelectedForeground)?QString::number(p->selectedForeground().color().rgb(),16):""); 00339 settings<<(p->hasProperty(QTextFormat::FontWeight)?(p->fontBold()?"1":"0"):""); 00340 settings<<(p->hasProperty(QTextFormat::FontItalic)?(p->fontItalic()?"1":"0"):""); 00341 settings<<(p->hasProperty(QTextFormat::FontStrikeOut)?(p->fontStrikeOut()?"1":"0"):""); 00342 settings<<(p->hasProperty(QTextFormat::FontUnderline)?(p->fontUnderline()?"1":"0"):""); 00343 settings<<(p->hasProperty(QTextFormat::BackgroundBrush)?QString::number(p->background().color().rgb(),16):""); 00344 settings<<(p->hasProperty(KTextEditor::Attribute::SelectedBackground)?QString::number(p->selectedBackground().color().rgb(),16):""); 00345 settings<<(p->hasProperty(QTextFormat::FontFamily)?(p->fontFamily()):QString()); 00346 settings<<"---"; 00347 00348 config.writeEntry(defaultStyleName(z),settings); 00349 } 00350 00351 emit changed(); 00352 } 00353 00354 int KateHlManager::highlights() 00355 { 00356 return (int) hlList.count(); 00357 } 00358 00359 QString KateHlManager::hlName(int n) 00360 { 00361 return hlList.at(n)->name(); 00362 } 00363 00364 QString KateHlManager::hlNameTranslated(int n) 00365 { 00366 return hlList.at(n)->nameTranslated(); 00367 } 00368 00369 QString KateHlManager::hlSection(int n) 00370 { 00371 return hlList.at(n)->section(); 00372 } 00373 00374 bool KateHlManager::hlHidden(int n) 00375 { 00376 return hlList.at(n)->hidden(); 00377 } 00378 00379 QString KateHlManager::identifierForName(const QString& name) 00380 { 00381 KateHighlighting *hl = 0; 00382 00383 if ((hl = hlDict[name])) 00384 return hl->getIdentifier (); 00385 00386 return QString(); 00387 } 00388 00389 QString KateHlManager::nameForIdentifier(const QString& identifier) 00390 { 00391 for ( QHash<QString, KateHighlighting*>::iterator it = hlDict.begin(); 00392 it != hlDict.end(); ++it ) 00393 { 00394 if ( (*it)->getIdentifier() == identifier ) { 00395 return it.key(); 00396 } 00397 } 00398 00399 return QString(); 00400 } 00401 00402 bool KateHlManager::resetDynamicCtxs() 00403 { 00404 if (forceNoDCReset) 00405 return false; 00406 00407 if (lastCtxsReset.elapsed() < KATE_DYNAMIC_CONTEXTS_RESET_DELAY) 00408 return false; 00409 00410 foreach (KateHighlighting *hl, hlList) 00411 hl->dropDynamicContexts(); 00412 00413 dynamicCtxsCount = 0; 00414 lastCtxsReset.start(); 00415 00416 return true; 00417 } 00418 //END 00419 00420 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference