• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • syntax
katesyntaxmanager.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
3  Copyright (C) 2003, 2004 Anders Lund <anders@alweb.dk>
4  Copyright (C) 2003 Hamish Rodda <rodda@kde.org>
5  Copyright (C) 2001,2002 Joseph Wenninger <jowenn@kde.org>
6  Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
7  Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License version 2 as published by the Free Software Foundation.
12 
13  This library 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. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 //BEGIN INCLUDES
25 #include "katesyntaxmanager.h"
26 #include "katesyntaxmanager.moc"
27 
28 #include "katetextline.h"
29 #include "katedocument.h"
30 #include "katesyntaxdocument.h"
31 #include "katerenderer.h"
32 #include "kateglobal.h"
33 #include "kateschema.h"
34 #include "kateconfig.h"
35 #include "kateextendedattribute.h"
36 #include "katehighlight.h"
37 
38 #include <kconfig.h>
39 #include <kconfiggroup.h>
40 #include <kglobal.h>
41 #include <kcomponentdata.h>
42 #include <kmimetype.h>
43 #include <klocale.h>
44 #include <kmenu.h>
45 #include <kcolorscheme.h>
46 #include <kcolorutils.h>
47 #include <kdebug.h>
48 #include <kstandarddirs.h>
49 #include <kmessagebox.h>
50 #include <kapplication.h>
51 
52 #include <QtCore/QSet>
53 #include <QtGui/QAction>
54 #include <QtCore/QStringList>
55 #include <QtCore/QTextStream>
56 //END
57 
58 using namespace KTextEditor;
59 
60 bool compareKateHighlighting(const KateHighlighting* const left, const KateHighlighting* const right)
61 {
62  int comparison = left->section().compare(right->section(), Qt::CaseInsensitive);
63  if (comparison == 0) {
64  comparison = left->nameTranslated().compare(right->nameTranslated(), Qt::CaseInsensitive);
65  }
66  return comparison < 0;
67 }
68 
69 //BEGIN KateHlManager
70 KateHlManager::KateHlManager()
71  : QObject()
72  , m_config ("katesyntaxhighlightingrc", KConfig::NoGlobals)
73  , commonSuffixes (QString(".orig;.new;~;.bak;.BAK").split(';'))
74  , syntax (new KateSyntaxDocument(&m_config))
75  , dynamicCtxsCount(0)
76  , forceNoDCReset(false)
77 {
78  KateSyntaxModeList modeList = syntax->modeList();
79  hlList.reserve(modeList.size() + 1);
80  hlDict.reserve(modeList.size() + 1);
81  for (int i=0; i < modeList.count(); i++)
82  {
83  KateHighlighting *hl = new KateHighlighting(modeList[i]);
84 
85  hlList.insert (qLowerBound(hlList.begin(), hlList.end(), hl, compareKateHighlighting), hl);
86  hlDict.insert (hl->name(), hl);
87  }
88 
89  // Normal HL
90  KateHighlighting *hl = new KateHighlighting(0);
91  hlList.prepend (hl);
92  hlDict.insert (hl->name(), hl);
93 
94  lastCtxsReset.start();
95 }
96 
97 KateHlManager::~KateHlManager()
98 {
99  delete syntax;
100  qDeleteAll(hlList);
101 }
102 
103 KateHlManager *KateHlManager::self()
104 {
105  return KateGlobal::self ()->hlManager ();
106 }
107 
108 KateHighlighting *KateHlManager::getHl(int n)
109 {
110  if (n < 0 || n >= hlList.count())
111  n = 0;
112 
113  return hlList.at(n);
114 }
115 
116 int KateHlManager::nameFind(const QString &name)
117 {
118  for(int i = 0; i < hlList.count(); ++i) {
119  if (hlList.at(i)->name().compare(name, Qt::CaseInsensitive) == 0) {
120  return i;
121  }
122  }
123 
124  return -1;
125 }
126 
127 uint KateHlManager::defaultStyles()
128 {
129  return 14;
130 }
131 
132 QString KateHlManager::defaultStyleName(int n, bool translateNames)
133 {
134  static QStringList names;
135  static QStringList translatedNames;
136 
137  if (names.isEmpty())
138  {
139  names << "Normal";
140  names << "Keyword";
141  names << "Data Type";
142  names << "Decimal/Value";
143  names << "Base-N Integer";
144  names << "Floating Point";
145  names << "Character";
146  names << "String";
147  names << "Comment";
148  names << "Others";
149  names << "Alert";
150  names << "Function";
151  // this next one is for denoting the beginning/end of a user defined folding region
152  names << "Region Marker";
153  // this one is for marking invalid input
154  names << "Error";
155 
156  translatedNames << i18nc("@item:intable Text context", "Normal");
157  translatedNames << i18nc("@item:intable Text context", "Keyword");
158  translatedNames << i18nc("@item:intable Text context", "Data Type");
159  translatedNames << i18nc("@item:intable Text context", "Decimal/Value");
160  translatedNames << i18nc("@item:intable Text context", "Base-N Integer");
161  translatedNames << i18nc("@item:intable Text context", "Floating Point");
162  translatedNames << i18nc("@item:intable Text context", "Character");
163  translatedNames << i18nc("@item:intable Text context", "String");
164  translatedNames << i18nc("@item:intable Text context", "Comment");
165  translatedNames << i18nc("@item:intable Text context", "Others");
166  translatedNames << i18nc("@item:intable Text context", "Alert");
167  translatedNames << i18nc("@item:intable Text context", "Function");
168  // this next one is for denoting the beginning/end of a user defined folding region
169  translatedNames << i18nc("@item:intable Text context", "Region Marker");
170  // this one is for marking invalid input
171  translatedNames << i18nc("@item:intable Text context", "Error");
172  }
173 
174  return translateNames ? translatedNames[n] : names[n];
175 }
176 
177 void KateHlManager::getDefaults(const QString &schema, KateAttributeList &list, KConfig *cfg)
178 {
179  KColorScheme scheme(QPalette::Active, KColorScheme::View);
180  KColorScheme schemeSelected(QPalette::Active, KColorScheme::Selection);
181 
184 
185  { // dsNormal
186  Attribute::Ptr attrib(new KTextEditor::Attribute());
187  attrib->setForeground( scheme.foreground().color() );
188  attrib->setSelectedForeground( schemeSelected.foreground().color() );
189  list.append(attrib);
190  }
191  { // dsKeyword
192  Attribute::Ptr attrib(new KTextEditor::Attribute());
193  attrib->setForeground( scheme.foreground().color() );
194  attrib->setSelectedForeground( schemeSelected.foreground().color() );
195  attrib->setFontBold(true);
196  list.append(attrib);
197  }
198  { // dsDataType
199  Attribute::Ptr attrib(new KTextEditor::Attribute());
200  attrib->setForeground( scheme.foreground(KColorScheme::LinkText).color() );
201  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() );
202  list.append(attrib);
203  }
204  { // dsDecVal
205  Attribute::Ptr attrib(new KTextEditor::Attribute());
206  attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
207  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
208  list.append(attrib);
209  }
210  { // dsBaseN
211  Attribute::Ptr attrib(new KTextEditor::Attribute());
212  attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
213  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
214  list.append(attrib);
215  }
216  { // dsFloat
217  Attribute::Ptr attrib(new KTextEditor::Attribute());
218  attrib->setForeground( scheme.foreground(KColorScheme::NeutralText).color() );
219  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NeutralText).color() );
220  list.append(attrib);
221  }
222  { // dsChar
223  Attribute::Ptr attrib(new KTextEditor::Attribute());
224  attrib->setForeground( scheme.foreground(KColorScheme::ActiveText).color() );
225  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::ActiveText).color() );
226  list.append(attrib);
227  }
228  { // dsString
229  Attribute::Ptr attrib(new KTextEditor::Attribute());
230  attrib->setForeground( scheme.foreground(KColorScheme::NegativeText).color() );
231  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
232  list.append(attrib);
233  }
234  { // dsComment
235  Attribute::Ptr attrib(new KTextEditor::Attribute());
236  attrib->setForeground( scheme.foreground(KColorScheme::InactiveText).color() );
237  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::InactiveText).color() );
238  list.append(attrib);
239  }
240  { // dsOthers
241  Attribute::Ptr attrib(new KTextEditor::Attribute());
242  attrib->setForeground( scheme.foreground(KColorScheme::PositiveText).color() );
243  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::PositiveText).color() );
244  list.append(attrib);
245  }
246  { // dsAlert
247  Attribute::Ptr attrib(new KTextEditor::Attribute());
248  attrib->setForeground( scheme.foreground(KColorScheme::NegativeText).color() );
249  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
250  attrib->setFontBold(true);
251  attrib->setBackground( scheme.background(KColorScheme::NegativeBackground).color() );
252  list.append(attrib);
253  }
254  { // dsFunction
255  Attribute::Ptr attrib(new KTextEditor::Attribute());
256  attrib->setForeground( scheme.foreground(KColorScheme::VisitedText).color() );
257  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::VisitedText).color() );
258  list.append(attrib);
259  }
260  { // dsRegionMarker
261  Attribute::Ptr attrib(new KTextEditor::Attribute());
262  attrib->setForeground( scheme.foreground(KColorScheme::LinkText).color() );
263  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::LinkText).color() );
264  attrib->setBackground( scheme.background(KColorScheme::LinkBackground).color() );
265  list.append(attrib);
266  }
267  { // dsError
268  Attribute::Ptr attrib(new KTextEditor::Attribute());
269  attrib->setForeground( scheme.foreground(KColorScheme::NegativeText) );
270  attrib->setSelectedForeground( schemeSelected.foreground(KColorScheme::NegativeText).color() );
271  attrib->setFontUnderline(true);
272  list.append(attrib);
273  }
274 
275  KConfigGroup config(cfg?cfg:KateHlManager::self()->self()->getKConfig(),
276  "Default Item Styles - Schema " + schema);
277 
278  for (uint z = 0; z < defaultStyles(); z++)
279  {
280  KTextEditor::Attribute::Ptr i = list.at(z);
281  QStringList s = config.readEntry(defaultStyleName(z), QStringList());
282  if (!s.isEmpty())
283  {
284  while( s.count()<9)
285  s << "";
286 
287  QString tmp;
288  QRgb col;
289 
290  tmp=s[0]; if (!tmp.isEmpty()) {
291  col=tmp.toUInt(0,16); i->setForeground(QColor(col)); }
292 
293  tmp=s[1]; if (!tmp.isEmpty()) {
294  col=tmp.toUInt(0,16); i->setSelectedForeground(QColor(col)); }
295 
296  tmp=s[2]; if (!tmp.isEmpty()) i->setFontBold(tmp!="0");
297 
298  tmp=s[3]; if (!tmp.isEmpty()) i->setFontItalic(tmp!="0");
299 
300  tmp=s[4]; if (!tmp.isEmpty()) i->setFontStrikeOut(tmp!="0");
301 
302  tmp=s[5]; if (!tmp.isEmpty()) i->setFontUnderline(tmp!="0");
303 
304  tmp=s[6]; if (!tmp.isEmpty()) {
305  if ( tmp != "-" )
306  {
307  col=tmp.toUInt(0,16);
308  i->setBackground(QColor(col));
309  }
310  else
311  i->clearBackground();
312  }
313  tmp=s[7]; if (!tmp.isEmpty()) {
314  if ( tmp != "-" )
315  {
316  col=tmp.toUInt(0,16);
317  i->setSelectedBackground(QColor(col));
318  }
319  else
320  i->clearProperty(KTextEditor::Attribute::SelectedBackground);
321  }
322  tmp=s[8]; if (!tmp.isEmpty() && tmp!=QLatin1String("---")) i->setFontFamily(tmp);
323  }
324  }
325 }
326 
327 void KateHlManager::setDefaults(const QString &schema, KateAttributeList &list,KConfig *cfg)
328 {
329  cfg=cfg?cfg:KateHlManager::self()->self()->getKConfig();
330  KConfigGroup config(cfg,
331  "Default Item Styles - Schema " + schema);
332 
333  for (uint z = 0; z < defaultStyles(); z++)
334  {
335  QStringList settings;
336  KTextEditor::Attribute::Ptr p = list.at(z);
337 
338  settings<<(p->hasProperty(QTextFormat::ForegroundBrush)?QString::number(p->foreground().color().rgb(),16):"");
339  settings<<(p->hasProperty(KTextEditor::Attribute::SelectedForeground)?QString::number(p->selectedForeground().color().rgb(),16):"");
340  settings<<(p->hasProperty(QTextFormat::FontWeight)?(p->fontBold()?"1":"0"):"");
341  settings<<(p->hasProperty(QTextFormat::FontItalic)?(p->fontItalic()?"1":"0"):"");
342  settings<<(p->hasProperty(QTextFormat::FontStrikeOut)?(p->fontStrikeOut()?"1":"0"):"");
343  settings<<(p->hasProperty(QTextFormat::FontUnderline)?(p->fontUnderline()?"1":"0"):"");
344  settings<<(p->hasProperty(QTextFormat::BackgroundBrush)?QString::number(p->background().color().rgb(),16):"-");
345  settings<<(p->hasProperty(KTextEditor::Attribute::SelectedBackground)?QString::number(p->selectedBackground().color().rgb(),16):"-");
346  settings<<(p->hasProperty(QTextFormat::FontFamily)?(p->fontFamily()):QString());
347  settings<<"---";
348 
349  config.writeEntry(defaultStyleName(z),settings);
350  }
351 
352  emit changed();
353 }
354 
355 int KateHlManager::highlights()
356 {
357  return (int) hlList.count();
358 }
359 
360 QString KateHlManager::hlName(int n)
361 {
362  return hlList.at(n)->name();
363 }
364 
365 QString KateHlManager::hlNameTranslated(int n)
366 {
367  return hlList.at(n)->nameTranslated();
368 }
369 
370 QString KateHlManager::hlSection(int n)
371 {
372  return hlList.at(n)->section();
373 }
374 
375 bool KateHlManager::hlHidden(int n)
376 {
377  return hlList.at(n)->hidden();
378 }
379 
380 QString KateHlManager::identifierForName(const QString& name)
381 {
382  if (hlDict.contains(name))
383  return hlDict[name]->getIdentifier();
384 
385  return QString();
386 }
387 
388 QString KateHlManager::nameForIdentifier(const QString& identifier)
389 {
390  for ( QHash<QString, KateHighlighting*>::iterator it = hlDict.begin();
391  it != hlDict.end(); ++it )
392  {
393  if ( (*it)->getIdentifier() == identifier ) {
394  return it.key();
395  }
396  }
397 
398  return QString();
399 }
400 
401 bool KateHlManager::resetDynamicCtxs()
402 {
403  if (forceNoDCReset)
404  return false;
405 
406  if (lastCtxsReset.elapsed() < KATE_DYNAMIC_CONTEXTS_RESET_DELAY)
407  return false;
408 
409  foreach (KateHighlighting *hl, hlList)
410  hl->dropDynamicContexts();
411 
412  dynamicCtxsCount = 0;
413  lastCtxsReset.start();
414 
415  return true;
416 }
417 //END
418 
419 // kate: space-indent on; indent-width 2; replace-tabs on;
KateHlManager::hlName
QString hlName(int n)
Definition: katesyntaxmanager.cpp:360
katehighlight.h
katetextline.h
QHash::insert
iterator insert(const Key &key, const T &value)
compareKateHighlighting
bool compareKateHighlighting(const KateHighlighting *const left, const KateHighlighting *const right)
Definition: katesyntaxmanager.cpp:60
katerenderer.h
KateHlManager
Definition: katesyntaxmanager.h:48
KateHlManager::KateHlManager
KateHlManager()
Definition: katesyntaxmanager.cpp:70
Kate::Script::i18nc
QScriptValue i18nc(QScriptContext *context, QScriptEngine *engine)
i18nc("context", "text", arguments [optional])
Definition: katescripthelpers.cpp:210
KateHlManager::KateHighlighting
friend class KateHighlighting
Definition: katesyntaxmanager.h:96
QList::reserve
void reserve(int alloc)
KateHlManager::nameForIdentifier
QString nameForIdentifier(const QString &)
Returns the mode name for a given identifier, as e.g.
Definition: katesyntaxmanager.cpp:388
QList::at
const T & at(int i) const
katedocument.h
QHash::reserve
void reserve(int size)
kateextendedattribute.h
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KateHlManager::changed
void changed()
KateHlManager::hlNameTranslated
QString hlNameTranslated(int n)
Definition: katesyntaxmanager.cpp:365
QList::size
int size() const
KateHighlighting::nameTranslated
const QString & nameTranslated() const
Definition: katehighlight.h:162
KateHlManager::defaultStyleName
static QString defaultStyleName(int n, bool translateNames=false)
Definition: katesyntaxmanager.cpp:132
QTime::elapsed
int elapsed() const
QString::number
QString number(int n, int base)
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
katesyntaxmanager.h
KateHighlighting::section
const QString & section() const
Definition: katehighlight.h:163
kateschema.h
QHash
QObject
kateglobal.h
KateHlManager::defaultStyles
static uint defaultStyles()
Definition: katesyntaxmanager.cpp:127
KateSyntaxDocument::modeList
const KateSyntaxModeList & modeList()
Get the mode list.
Definition: katesyntaxdocument.h:100
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KateHlManager::setDefaults
void setDefaults(const QString &schema, KateAttributeList &, KConfig *cfg=0)
Definition: katesyntaxmanager.cpp:327
katesyntaxdocument.h
QHash::begin
iterator begin()
KateSyntaxDocument
Store and manage the information about Syntax Highlighting.
Definition: katesyntaxdocument.h:73
KateHlManager::getKConfig
KConfig * getKConfig()
Definition: katesyntaxmanager.h:60
QString
QList< KateSyntaxModeListItem * >
QColor
QStringList
KATE_DYNAMIC_CONTEXTS_RESET_DELAY
#define KATE_DYNAMIC_CONTEXTS_RESET_DELAY
Definition: katehighlight.h:55
QList::end
iterator end()
KateGlobal::hlManager
KateHlManager * hlManager()
hl manager
Definition: kateglobal.h:327
KateHlManager::~KateHlManager
~KateHlManager()
Definition: katesyntaxmanager.cpp:97
KateHlManager::resetDynamicCtxs
bool resetDynamicCtxs()
Definition: katesyntaxmanager.cpp:401
KateHlManager::hlSection
QString hlSection(int n)
Definition: katesyntaxmanager.cpp:370
KateHlManager::getHl
KateHighlighting * getHl(int n)
Definition: katesyntaxmanager.cpp:108
QLatin1String
QList::insert
void insert(int i, const T &value)
KateHighlighting
Definition: katehighlight.h:119
KateHlManager::nameFind
int nameFind(const QString &name)
Definition: katesyntaxmanager.cpp:116
KateHlManager::getDefaults
void getDefaults(const QString &schema, KateAttributeList &, KConfig *cfg=0)
Definition: katesyntaxmanager.cpp:177
QTime::start
void start()
QList::prepend
void prepend(const T &value)
QHash::contains
bool contains(const Key &key) const
QHash::end
iterator end()
KateHighlighting::name
const QString & name() const
Definition: katehighlight.h:161
KateHighlighting::dropDynamicContexts
void dropDynamicContexts()
Drop all dynamic contexts.
Definition: katehighlight.cpp:264
KateHlManager::identifierForName
QString identifierForName(const QString &)
Definition: katesyntaxmanager.cpp:380
QString::compare
int compare(const QString &other) const
kateconfig.h
KateHlManager::hlHidden
bool hlHidden(int n)
Definition: katesyntaxmanager.cpp:375
QList::begin
iterator begin()
KateHlManager::self
static KateHlManager * self()
Definition: katesyntaxmanager.cpp:103
QString::toUInt
uint toUInt(bool *ok, int base) const
KateHlManager::highlights
int highlights()
Definition: katesyntaxmanager.cpp:355
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal