katehighlight.h

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2001,2002 Joseph Wenninger <jowenn@kde.org>
00003    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00004    Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #ifndef __KATE_HIGHLIGHT_H__
00022 #define __KATE_HIGHLIGHT_H__
00023 
00024 #include "katetextline.h"
00025 #include "kateattribute.h"
00026 
00027 #include "../interfaces/document.h"
00028 
00029 #include <kconfig.h>
00030 
00031 #include <qptrlist.h>
00032 #include <qvaluelist.h>
00033 #include <qvaluevector.h>
00034 #include <qregexp.h>
00035 #include <qdict.h>
00036 #include <qintdict.h>
00037 #include <qmap.h>
00038 #include <qobject.h>
00039 #include <qstringlist.h>
00040 #include <qguardedptr.h>
00041 #include <qdatetime.h>
00042 
00043 class KateHlContext;
00044 class KateHlItem;
00045 class KateHlItemData;
00046 class KateHlData;
00047 class KateEmbeddedHlInfo;
00048 class KateHlIncludeRule;
00049 class KateSyntaxDocument;
00050 class KateTextLine;
00051 class KateSyntaxModeListItem;
00052 class KateSyntaxContextData;
00053 
00054 class QPopupMenu;
00055 
00056 // some typedefs
00057 typedef QPtrList<KateAttribute> KateAttributeList;
00058 typedef QValueList<KateHlIncludeRule*> KateHlIncludeRules;
00059 typedef QPtrList<KateHlItemData> KateHlItemDataList;
00060 typedef QPtrList<KateHlData> KateHlDataList;
00061 typedef QMap<QString,KateEmbeddedHlInfo> KateEmbeddedHlInfos;
00062 typedef QMap<int*,QString> KateHlUnresolvedCtxRefs;
00063 typedef QValueList<int> IntList;
00064 
00065 //Item Properties: name, Item Style, Item Font
00066 class KateHlItemData : public KateAttribute
00067 {
00068   public:
00069     KateHlItemData(const QString  name, int defStyleNum);
00070 
00071     enum ItemStyles {
00072       dsNormal,
00073       dsKeyword,
00074       dsDataType,
00075       dsDecVal,
00076       dsBaseN,
00077       dsFloat,
00078       dsChar,
00079       dsString,
00080       dsComment,
00081       dsOthers,
00082       dsAlert,
00083       dsFunction,
00084       dsRegionMarker,
00085       dsError };
00086 
00087   public:
00088     const QString name;
00089     int defStyleNum;
00090 };
00091 
00092 class KateHlData
00093 {
00094   public:
00095     KateHlData(const QString &wildcards, const QString &mimetypes,const QString &identifier, int priority);
00096 
00097   public:
00098     QString wildcards;
00099     QString mimetypes;
00100     QString identifier;
00101     int priority;
00102 };
00103 
00104 class KateHighlighting
00105 {
00106   public:
00107     KateHighlighting(const KateSyntaxModeListItem *def);
00108     ~KateHighlighting();
00109 
00110   public:
00111     void doHighlight ( KateTextLine *prevLine,
00112                        KateTextLine *textLine,
00113                        QMemArray<uint> *foldingList,
00114                        bool *ctxChanged );
00115 
00116     void loadWildcards();
00117     QValueList<QRegExp>& getRegexpExtensions();
00118     QStringList& getPlainExtensions();
00119 
00120     QString getMimetypes();
00121 
00122     // this pointer needs to be deleted !!!!!!!!!!
00123     KateHlData *getData();
00124     void setData(KateHlData *);
00125 
00126     void setKateHlItemDataList(uint schema, KateHlItemDataList &);
00127 
00128     // both methodes return hard copies of the internal lists
00129     // the lists are cleared first + autodelete is set !
00130     // keep track that you delete them, or mem will be lost
00131     void getKateHlItemDataListCopy (uint schema, KateHlItemDataList &);
00132 
00133     const QString &name() const {return iName;}
00134     const QString &nameTranslated() const {return iNameTranslated;}
00135     const QString &section() const {return iSection;}
00136     bool hidden() const {return iHidden;}
00137     const QString &version() const {return iVersion;}
00138     const QString &author () const { return iAuthor; }
00139     const QString &license () const { return iLicense; }
00140     int priority();
00141     const QString &getIdentifier() const {return identifier;}
00142     void use();
00143     void release();
00144 
00149     bool isInWord( QChar c, int attrib=0 ) const;
00150 
00155     bool canBreakAt( QChar c, int attrib=0 ) const;
00156 
00161     bool canComment( int startAttr, int endAttr ) const;
00162 
00167     signed char commentRegion(int attr) const;
00168 
00173     QString getCommentStart( int attrib=0 ) const;
00174 
00179     QString getCommentEnd( int attrib=0 ) const;
00180 
00185     QString getCommentSingleLineStart( int attrib=0 ) const;
00186 
00187 
00191     enum CSLPos { CSLPosColumn0=0,CSLPosAfterWhitespace=1};
00192 
00197     CSLPos getCommentSingleLinePosition( int attrib=0 ) const;
00198 
00202     int attribute( int context ) const;
00203 
00208     QString hlKeyForAttrib( int attrib ) const;
00209 
00210 
00211     void clearAttributeArrays ();
00212 
00213     QMemArray<KateAttribute> *attributes (uint schema);
00214 
00215     inline bool noHighlighting () const { return noHl; };
00216 
00217     // be carefull: all documents hl should be invalidated after calling this method!
00218     void dropDynamicContexts();
00219 
00220     QString indentation () { return m_indentation; }
00221 
00222   private:
00223     // make this private, nobody should play with the internal data pointers
00224     void getKateHlItemDataList(uint schema, KateHlItemDataList &);
00225 
00226     void init();
00227     void done();
00228     void makeContextList ();
00229     int makeDynamicContext(KateHlContext *model, const QStringList *args);
00230     void handleKateHlIncludeRules ();
00231     void handleKateHlIncludeRulesRecursive(KateHlIncludeRules::iterator it, KateHlIncludeRules *list);
00232     int addToContextList(const QString &ident, int ctx0);
00233     void addToKateHlItemDataList();
00234     void createKateHlItemData (KateHlItemDataList &list);
00235     void readGlobalKeywordConfig();
00236     void readWordWrapConfig();
00237     void readCommentConfig();
00238     void readIndentationConfig ();
00239     void readFoldingConfig ();
00240 
00241     // manipulates the ctxs array directly ;)
00242     void generateContextStack(int *ctxNum, int ctx, QMemArray<short> *ctxs, int *posPrevLine);
00243 
00244     KateHlItem *createKateHlItem(KateSyntaxContextData *data, KateHlItemDataList &iDl, QStringList *RegionList, QStringList *ContextList);
00245     int lookupAttrName(const QString& name, KateHlItemDataList &iDl);
00246 
00247     void createContextNameList(QStringList *ContextNameList, int ctx0);
00248     int getIdFromString(QStringList *ContextNameList, QString tmpLineEndContext,/*NO CONST*/ QString &unres);
00249 
00250     KateHlItemDataList internalIDList;
00251 
00252     QValueVector<KateHlContext*> m_contexts;
00253     inline KateHlContext *contextNum (uint n) { if (n < m_contexts.size()) return m_contexts[n]; return 0; }
00254 
00255     QMap< QPair<KateHlContext *, QString>, short> dynamicCtxs;
00256 
00257     // make them pointers perhaps
00258     KateEmbeddedHlInfos embeddedHls;
00259     KateHlUnresolvedCtxRefs unresolvedContextReferences;
00260     QStringList RegionList;
00261     QStringList ContextNameList;
00262 
00263     bool noHl;
00264     bool folding;
00265     bool casesensitive;
00266     QString weakDeliminator;
00267     QString deliminator;
00268 
00269     QString iName;
00270     QString iNameTranslated;
00271     QString iSection;
00272     bool iHidden;
00273     QString iWildcards;
00274     QString iMimetypes;
00275     QString identifier;
00276     QString iVersion;
00277     QString iAuthor;
00278     QString iLicense;
00279     QString m_indentation;
00280     int m_priority;
00281     int refCount;
00282     int startctx, base_startctx;
00283 
00284     QString errorsAndWarnings;
00285     QString buildIdentifier;
00286     QString buildPrefix;
00287     bool building;
00288     uint itemData0;
00289     uint buildContext0Offset;
00290     KateHlIncludeRules includeRules;
00291     bool m_foldingIndentationSensitive;
00292 
00293     QIntDict< QMemArray<KateAttribute> > m_attributeArrays;
00294 
00295 
00308     class HighlightPropertyBag {
00309       public:
00310         QString singleLineCommentMarker;
00311         QString multiLineCommentStart;
00312         QString multiLineCommentEnd;
00313         QString multiLineRegion;
00314         CSLPos  singleLineCommentPosition;
00315         QString deliminator;
00316         QString wordWrapDeliminator;
00317     };
00318 
00323     QDict<HighlightPropertyBag> m_additionalData;
00324 
00330     QMap<int, QString> m_hlIndex;
00331 
00332 
00333     QString extensionSource;
00334     QValueList<QRegExp> regexpExtensions;
00335     QStringList plainExtensions;
00336 
00337   public:
00338     inline bool foldingIndentationSensitive () { return m_foldingIndentationSensitive; }
00339     inline bool allowsFolding(){return folding;}
00340 };
00341 
00342 class KateHlManager : public QObject
00343 {
00344   Q_OBJECT
00345 
00346   private:
00347     KateHlManager();
00348 
00349   public:
00350     ~KateHlManager();
00351 
00352     static KateHlManager *self();
00353 
00354     inline KConfig *getKConfig() { return &m_config; };
00355 
00356     KateHighlighting *getHl(int n);
00357     int nameFind(const QString &name);
00358 
00359     int detectHighlighting (class KateDocument *doc);
00360 
00361     int findHl(KateHighlighting *h) {return hlList.find(h);}
00362     QString identifierForName(const QString&);
00363 
00364     // methodes to get the default style count + names
00365     static uint defaultStyles();
00366     static QString defaultStyleName(int n, bool translateNames = false);
00367 
00368     void getDefaults(uint schema, KateAttributeList &);
00369     void setDefaults(uint schema, KateAttributeList &);
00370 
00371     int highlights();
00372     QString hlName(int n);
00373     QString hlNameTranslated (int n);
00374     QString hlSection(int n);
00375     bool hlHidden(int n);
00376 
00377     void incDynamicCtxs() { ++dynamicCtxsCount; };
00378     uint countDynamicCtxs() { return dynamicCtxsCount; };
00379     void setForceNoDCReset(bool b) { forceNoDCReset = b; };
00380 
00381     // be carefull: all documents hl should be invalidated after having successfully called this method!
00382     bool resetDynamicCtxs();
00383 
00384   signals:
00385     void changed();
00386 
00387   private:
00388     int wildcardFind(const QString &fileName);
00389     int mimeFind(KateDocument *);
00390     int realWildcardFind(const QString &fileName);
00391 
00392   private:
00393     friend class KateHighlighting;
00394 
00395     QPtrList<KateHighlighting> hlList;
00396     QDict<KateHighlighting> hlDict;
00397 
00398     static KateHlManager *s_self;
00399 
00400     KConfig m_config;
00401     QStringList commonSuffixes;
00402 
00403     KateSyntaxDocument *syntax;
00404 
00405     uint dynamicCtxsCount;
00406     QTime lastCtxsReset;
00407     bool forceNoDCReset;
00408 };
00409 
00410 class KateViewHighlightAction: public Kate::ActionMenu
00411 {
00412   Q_OBJECT
00413 
00414   public:
00415     KateViewHighlightAction(const QString& text, QObject* parent = 0, const char* name = 0)
00416        : Kate::ActionMenu(text, parent, name) { init(); };
00417 
00418     ~KateViewHighlightAction(){;};
00419 
00420     void updateMenu (Kate::Document *doc);
00421 
00422   private:
00423     void init();
00424 
00425     QGuardedPtr<Kate::Document> m_doc;
00426     QStringList subMenusName;
00427     QStringList names;
00428     QPtrList<QPopupMenu> subMenus;
00429 
00430   public  slots:
00431     void slotAboutToShow();
00432 
00433   private slots:
00434     void setHl (int mode);
00435 };
00436 
00437 #endif
00438 
00439 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Home | KDE Accessibility Home | Description of Access Keys