Kate
katesyntaxmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "katesyntaxmanager.h"
00025 #include "katesyntaxmanager.moc"
00026
00027 #include "katetextline.h"
00028 #include "katedocument.h"
00029 #include "katesyntaxdocument.h"
00030 #include "katerenderer.h"
00031 #include "kateglobal.h"
00032 #include "kateschema.h"
00033 #include "kateconfig.h"
00034 #include "kateextendedattribute.h"
00035 #include "katehighlight.h"
00036
00037 #include <kconfig.h>
00038 #include <kglobal.h>
00039 #include <kcomponentdata.h>
00040 #include <kmimetype.h>
00041 #include <klocale.h>
00042 #include <kmenu.h>
00043 #include <kcolorscheme.h>
00044 #include <kcolorutils.h>
00045 #include <kdebug.h>
00046 #include <kstandarddirs.h>
00047 #include <kmessagebox.h>
00048 #include <kapplication.h>
00049
00050 #include <QtCore/QSet>
00051 #include <QtGui/QAction>
00052 #include <QtCore/QStringList>
00053 #include <QtCore/QTextStream>
00054
00055
00056
00057 KateHlManager::KateHlManager()
00058 : QObject()
00059 , m_config ("katesyntaxhighlightingrc", KConfig::NoGlobals)
00060 , commonSuffixes (QString(".orig;.new;~;.bak;.BAK").split(';'))
00061 , syntax (new KateSyntaxDocument(&m_config))
00062 , dynamicCtxsCount(0)
00063 , forceNoDCReset(false)
00064 {
00065 KateSyntaxModeList modeList = syntax->modeList();
00066 for (int i=0; i < modeList.count(); i++)
00067 {
00068 KateHighlighting *hl = new KateHighlighting(modeList[i]);
00069
00070 int insert = 0;
00071 for (; insert <= hlList.count(); insert++)
00072 {
00073 if (insert == hlList.count())
00074 break;
00075
00076 if ( QString(hlList.at(insert)->section() + hlList.at(insert)->nameTranslated()).toLower()
00077 > QString(hl->section() + hl->nameTranslated()).toLower() )
00078 break;
00079 }
00080
00081 hlList.insert (insert, hl);
00082 hlDict.insert (hl->name(), hl);
00083 }
00084
00085
00086 KateHighlighting *hl = new KateHighlighting(0);
00087 hlList.prepend (hl);
00088 hlDict.insert (hl->name(), hl);
00089
00090 lastCtxsReset.start();
00091 }
00092
00093 KateHlManager::~KateHlManager()
00094 {
00095 delete syntax;
00096 qDeleteAll(hlList);
00097 }
00098
00099 KateHlManager *KateHlManager::self()
00100 {
00101 return KateGlobal::self ()->hlManager ();
00102 }
00103
00104 KateHighlighting *KateHlManager::getHl(int n)
00105 {
00106 if (n < 0 || n >= hlList.count())
00107 n = 0;
00108
00109 return hlList.at(n);
00110 }
00111
00112 int KateHlManager::nameFind(const QString &name)
00113 {
00114 const QString lower_name = name.toLower();
00115 int z (hlList.count() - 1);
00116 for (; z > 0; z--)
00117 if (hlList.at(z)->name().toLower() == lower_name)
00118 return z;
00119
00120 return z;
00121 }
00122
00123 uint KateHlManager::defaultStyles()
00124 {
00125 return 14;
00126 }
00127
00128 QString KateHlManager::defaultStyleName(int n, bool translateNames)
00129 {
00130 static QStringList names;
00131 static QStringList translatedNames;
00132
00133 if (names.isEmpty())
00134 {
00135 names << "Normal";
00136 names << "Keyword";
00137 names << "Data Type";
00138 names << "Decimal/Value";
00139 names << "Base-N Integer";
00140 names << "Floating Point";
00141 names << "Character";
00142 names << "String";
00143 names << "Comment";
00144 names << "Others";
00145 names << "Alert";
00146 names << "Function";
00147
00148 names << "Region Marker";
00149
00150 names << "Error";
00151
00152 translatedNames << i18nc("@item:intable Text context", "Normal");
00153 translatedNames << i18nc("@item:intable Text context", "Keyword");
00154 translatedNames << i18nc("@item:intable Text context", "Data Type");
00155 translatedNames << i18nc("@item:intable Text context", "Decimal/Value");
00156 translatedNames << i18nc("@item:intable Text context", "Base-N Integer");
00157 translatedNames << i18nc("@item:intable Text context", "Floating Point");
00158 translatedNames << i18nc("@item:intable Text context", "Character");
00159 translatedNames << i18nc("@item:intable Text context", "String");
00160 translatedNames << i18nc("@item:intable Text context", "Comment");
00161 translatedNames << i18nc("@item:intable Text context", "Others");
00162 translatedNames << i18nc("@item:intable Text context", "Alert");
00163 translatedNames << i18nc("@item:intable Text context", "Function");
00164
00165 translatedNames << i18nc("@item:intable Text context", "Region Marker");
00166
00167 translatedNames << i18nc("@item:intable Text context", "Error");
00168 }
00169
00170 return translateNames ? translatedNames[n] : names[n];
00171 }
00172
00173 void KateHlManager::getDefaults(const QString &schema, KateAttributeList &list)
00174 {
00175 KColorScheme scheme(QPalette::Active, KColorScheme::View);
00176 KColorScheme schemeSelected(QPalette::Active, KColorScheme::Selection);
00177
00178 KTextEditor::Attribute::Ptr normal(new KTextEditor::Attribute());
00179 normal->setForeground( scheme.foreground().color() );
00180 normal->setSelectedForeground( schemeSelected.foreground().color() );
00181 list.append(normal);
00182
00183 KTextEditor::Attribute::Ptr keyword(new KTextEditor::Attribute());
00184 keyword->setForeground( scheme.foreground().color() );
00185 keyword->setSelectedForeground( schemeSelected.foreground().color() );
00186 keyword->setFontBold(true);
00187 list.append(keyword);
00188
00189 KTextEditor::Attribute::Ptr dataType(new KTextEditor::Attribute());
00190 dataType->setForeground( scheme.foreground(KColorScheme::LinkText).color() );
00191 dataType->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() );
00192 list.append(dataType);
00193
00194 KTextEditor::Attribute::Ptr decimal(new KTextEditor::Attribute());
00195 decimal->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
00196 decimal->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
00197 list.append(decimal);
00198
00199 KTextEditor::Attribute::Ptr basen(new KTextEditor::Attribute());
00200 basen->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
00201 basen->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
00202 list.append(basen);
00203
00204 KTextEditor::Attribute::Ptr floatAttribute(new KTextEditor::Attribute());
00205 floatAttribute->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
00206 floatAttribute->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
00207 list.append(floatAttribute);
00208
00209 KTextEditor::Attribute::Ptr charAttribute(new KTextEditor::Attribute());
00210 charAttribute->setForeground( scheme.foreground(KColorScheme::ActiveText).color() );
00211 charAttribute->setSelectedForeground( schemeSelected.foreground(KColorScheme::ActiveText).color() );
00212 list.append(charAttribute);
00213
00214 KTextEditor::Attribute::Ptr string(new KTextEditor::Attribute());
00215 string->setForeground( scheme.foreground(KColorScheme::NegativeText).color() );
00216 string->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
00217 list.append(string);
00218
00219 KTextEditor::Attribute::Ptr comment(new KTextEditor::Attribute());
00220 comment->setForeground( scheme.foreground(KColorScheme::InactiveText).color() );
00221 comment->setSelectedForeground( schemeSelected.foreground(KColorScheme::InactiveText).color() );
00222 comment->setFontItalic(true);
00223 list.append(comment);
00224
00225 KTextEditor::Attribute::Ptr others(new KTextEditor::Attribute());
00226 others->setForeground( scheme.foreground(KColorScheme::PositiveText).color() );
00227 others->setSelectedForeground( schemeSelected.foreground(KColorScheme::PositiveText).color() );
00228 list.append(others);
00229
00230 KTextEditor::Attribute::Ptr alert(new KTextEditor::Attribute());
00231 alert->setForeground( scheme.foreground(KColorScheme::NegativeText).color() );
00232 alert->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
00233 alert->setFontBold(true);
00234 alert->setBackground( scheme.background(KColorScheme::NegativeBackground).color() );
00235 list.append(alert);
00236
00237 KTextEditor::Attribute::Ptr functionAttribute(new KTextEditor::Attribute());
00238 functionAttribute->setForeground( scheme.foreground(KColorScheme::VisitedText).color() );
00239 functionAttribute->setSelectedForeground( schemeSelected.foreground(KColorScheme::VisitedText).color() );
00240 list.append(functionAttribute);
00241
00242 KTextEditor::Attribute::Ptr regionmarker(new KTextEditor::Attribute());
00243 regionmarker->setForeground( scheme.foreground(KColorScheme::LinkText).color() );
00244 regionmarker->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() );
00245 regionmarker->setBackground( scheme.background(KColorScheme::LinkBackground).color() );
00246 list.append(regionmarker);
00247
00248 KTextEditor::Attribute::Ptr error(new KTextEditor::Attribute());
00249 error->setForeground( scheme.foreground(KColorScheme::NegativeText) );
00250 error->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
00251 error->setFontUnderline(true);
00252 list.append(error);
00253
00254 KConfigGroup config(KateHlManager::self()->self()->getKConfig(),
00255 "Default Item Styles - Schema " + schema);
00256
00257 for (uint z = 0; z < defaultStyles(); z++)
00258 {
00259 KTextEditor::Attribute::Ptr i = list.at(z);
00260 QStringList s = config.readEntry(defaultStyleName(z), QStringList());
00261 if (!s.isEmpty())
00262 {
00263 while( s.count()<8)
00264 s << "";
00265
00266 QString tmp;
00267 QRgb col;
00268
00269 tmp=s[0]; if (!tmp.isEmpty()) {
00270 col=tmp.toUInt(0,16); i->setForeground(QColor(col)); }
00271
00272 tmp=s[1]; if (!tmp.isEmpty()) {
00273 col=tmp.toUInt(0,16); i->setSelectedForeground(QColor(col)); }
00274
00275 tmp=s[2]; if (!tmp.isEmpty()) i->setFontBold(tmp!="0");
00276
00277 tmp=s[3]; if (!tmp.isEmpty()) i->setFontItalic(tmp!="0");
00278
00279 tmp=s[4]; if (!tmp.isEmpty()) i->setFontStrikeOut(tmp!="0");
00280
00281 tmp=s[5]; if (!tmp.isEmpty()) i->setFontUnderline(tmp!="0");
00282
00283 tmp=s[6]; if (!tmp.isEmpty()) {
00284 if ( tmp != "-" )
00285 {
00286 col=tmp.toUInt(0,16);
00287 i->setBackground(QColor(col));
00288 }
00289 else
00290 i->clearBackground();
00291 }
00292 tmp=s[7]; if (!tmp.isEmpty()) {
00293 if ( tmp != "-" )
00294 {
00295 col=tmp.toUInt(0,16);
00296 i->setSelectedBackground(QColor(col));
00297 }
00298 else
00299 i->clearProperty(KTextEditor::Attribute::SelectedBackground);
00300 }
00301 }
00302 }
00303 }
00304
00305 void KateHlManager::setDefaults(const QString &schema, KateAttributeList &list)
00306 {
00307 KConfigGroup config(KateHlManager::self()->self()->getKConfig(),
00308 "Default Item Styles - Schema " + schema);
00309
00310 for (uint z = 0; z < defaultStyles(); z++)
00311 {
00312 QStringList settings;
00313 KTextEditor::Attribute::Ptr p = list.at(z);
00314
00315 settings<<(p->hasProperty(QTextFormat::ForegroundBrush)?QString::number(p->foreground().color().rgb(),16):"");
00316 settings<<(p->hasProperty(KTextEditor::Attribute::SelectedForeground)?QString::number(p->selectedForeground().color().rgb(),16):"");
00317 settings<<(p->hasProperty(QTextFormat::FontWeight)?(p->fontBold()?"1":"0"):"");
00318 settings<<(p->hasProperty(QTextFormat::FontItalic)?(p->fontItalic()?"1":"0"):"");
00319 settings<<(p->hasProperty(QTextFormat::FontStrikeOut)?(p->fontStrikeOut()?"1":"0"):"");
00320 settings<<(p->hasProperty(QTextFormat::FontUnderline)?(p->fontUnderline()?"1":"0"):"");
00321 settings<<(p->hasProperty(QTextFormat::BackgroundBrush)?QString::number(p->background().color().rgb(),16):"");
00322 settings<<(p->hasProperty(KTextEditor::Attribute::SelectedBackground)?QString::number(p->selectedBackground().color().rgb(),16):"");
00323 settings<<"---";
00324
00325 config.writeEntry(defaultStyleName(z),settings);
00326 }
00327
00328 emit changed();
00329 }
00330
00331 int KateHlManager::highlights()
00332 {
00333 return (int) hlList.count();
00334 }
00335
00336 QString KateHlManager::hlName(int n)
00337 {
00338 return hlList.at(n)->name();
00339 }
00340
00341 QString KateHlManager::hlNameTranslated(int n)
00342 {
00343 return hlList.at(n)->nameTranslated();
00344 }
00345
00346 QString KateHlManager::hlSection(int n)
00347 {
00348 return hlList.at(n)->section();
00349 }
00350
00351 bool KateHlManager::hlHidden(int n)
00352 {
00353 return hlList.at(n)->hidden();
00354 }
00355
00356 QString KateHlManager::identifierForName(const QString& name)
00357 {
00358 KateHighlighting *hl = 0;
00359
00360 if ((hl = hlDict[name]))
00361 return hl->getIdentifier ();
00362
00363 return QString();
00364 }
00365
00366 bool KateHlManager::resetDynamicCtxs()
00367 {
00368 if (forceNoDCReset)
00369 return false;
00370
00371 if (lastCtxsReset.elapsed() < KATE_DYNAMIC_CONTEXTS_RESET_DELAY)
00372 return false;
00373
00374 foreach (KateHighlighting *hl, hlList)
00375 hl->dropDynamicContexts();
00376
00377 dynamicCtxsCount = 0;
00378 lastCtxsReset.start();
00379
00380 return true;
00381 }
00382
00383
00384