00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _KATE_DOCUMENT_H_
00022 #define _KATE_DOCUMENT_H_
00023
00024 #include "katesupercursor.h"
00025 #include "katetextline.h"
00026 #include "kateundo.h"
00027 #include "katebuffer.h"
00028 #include "katecodefoldinghelpers.h"
00029
00030 #include "../interfaces/document.h"
00031
00032 #include <ktexteditor/configinterfaceextension.h>
00033 #include <ktexteditor/encodinginterface.h>
00034 #include <ktexteditor/sessionconfiginterface.h>
00035 #include <ktexteditor/editinterfaceext.h>
00036 #include <ktexteditor/templateinterface.h>
00037
00038 #include <dcopobject.h>
00039
00040 #include <kmimetype.h>
00041 #include <klocale.h>
00042
00043 #include <qintdict.h>
00044 #include <qmap.h>
00045 #include <qdatetime.h>
00046
00047 namespace KTextEditor { class Plugin; }
00048
00049 namespace KIO { class TransferJob; }
00050
00051 class KateUndoGroup;
00052 class KateCmd;
00053 class KateAttribute;
00054 class KateAutoIndent;
00055 class KateCodeFoldingTree;
00056 class KateBuffer;
00057 class KateView;
00058 class KateViewInternal;
00059 class KateArbitraryHighlight;
00060 class KateSuperRange;
00061 class KateLineInfo;
00062 class KateBrowserExtension;
00063 class KateDocumentConfig;
00064 class KateHighlighting;
00065 class KatePartPluginItem;
00066 class KatePartPluginInfo;
00067
00068 class KTempFile;
00069
00070 class QTimer;
00071
00072 class KateKeyInterceptorFunctor;
00073
00074
00075
00076
00077 class KateDocument : public Kate::Document,
00078 public Kate::DocumentExt,
00079 public KTextEditor::ConfigInterfaceExtension,
00080 public KTextEditor::EncodingInterface,
00081 public KTextEditor::SessionConfigInterface,
00082 public KTextEditor::EditInterfaceExt,
00083 public KTextEditor::TemplateInterface,
00084 public DCOPObject
00085 {
00086 K_DCOP
00087 Q_OBJECT
00088
00089 friend class KateViewInternal;
00090 friend class KateRenderer;
00091
00092 public:
00093 KateDocument (bool bSingleViewMode=false, bool bBrowserView=false, bool bReadOnly=false,
00094 QWidget *parentWidget = 0, const char *widgetName = 0, QObject * = 0, const char * = 0);
00095 ~KateDocument ();
00096
00097 bool closeURL();
00098
00099
00100
00101
00102 public:
00103 void unloadAllPlugins ();
00104
00105 void enableAllPluginsGUI (KateView *view);
00106 void disableAllPluginsGUI (KateView *view);
00107
00108 void loadPlugin (uint pluginIndex);
00109 void unloadPlugin (uint pluginIndex);
00110
00111 void enablePluginGUI (KTextEditor::Plugin *plugin, KateView *view);
00112 void enablePluginGUI (KTextEditor::Plugin *plugin);
00113
00114 void disablePluginGUI (KTextEditor::Plugin *plugin, KateView *view);
00115 void disablePluginGUI (KTextEditor::Plugin *plugin);
00116
00117 private:
00118 QMemArray<KTextEditor::Plugin *> m_plugins;
00119
00120 public:
00121 bool readOnly () const { return m_bReadOnly; }
00122 bool browserView () const { return m_bBrowserView; }
00123 bool singleViewMode () const { return m_bSingleViewMode; }
00124 KateBrowserExtension *browserExtension () { return m_extension; }
00125
00126 private:
00127
00128 bool m_bSingleViewMode;
00129 bool m_bBrowserView;
00130 bool m_bReadOnly;
00131 KateBrowserExtension *m_extension;
00132
00133
00134
00135
00136 public:
00137 KTextEditor::View *createView( QWidget *parent, const char *name );
00138 QPtrList<KTextEditor::View> views () const;
00139
00140 inline KateView *activeView () const { return m_activeView; }
00141
00142 private:
00143 QPtrList<KateView> m_views;
00144 QPtrList<KTextEditor::View> m_textEditViews;
00145 KateView *m_activeView;
00146
00156 void setActiveView( KateView *view );
00157
00158
00159
00160
00161 public slots:
00162 uint configPages () const;
00163 KTextEditor::ConfigPage *configPage (uint number = 0, QWidget *parent = 0, const char *name=0 );
00164 QString configPageName (uint number = 0) const;
00165 QString configPageFullName (uint number = 0) const;
00166 QPixmap configPagePixmap (uint number = 0, int size = KIcon::SizeSmall) const;
00167
00168
00169
00170
00171 public slots:
00172 QString text() const;
00173
00174 QString text ( uint startLine, uint startCol, uint endLine, uint endCol ) const;
00175 QString text ( uint startLine, uint startCol, uint endLine, uint endCol, bool blockwise ) const;
00176
00177 QString textLine ( uint line ) const;
00178
00179 bool setText(const QString &);
00180 bool clear ();
00181
00182 bool insertText ( uint line, uint col, const QString &s );
00183 bool insertText ( uint line, uint col, const QString &s, bool blockwise );
00184
00185 bool removeText ( uint startLine, uint startCol, uint endLine, uint endCol );
00186 bool removeText ( uint startLine, uint startCol, uint endLine, uint endCol, bool blockwise );
00187
00188 bool insertLine ( uint line, const QString &s );
00189 bool removeLine ( uint line );
00190
00191 uint numLines() const;
00192 uint numVisLines() const;
00193 uint length () const;
00194 int lineLength ( uint line ) const;
00195
00196 signals:
00197 void textChanged ();
00198 void charactersInteractivelyInserted(int ,int ,const QString&);
00199 void charactersSemiInteractivelyInserted(int ,int ,const QString&);
00200 void backspacePressed();
00201
00202 public:
00203
00209 void editStart (bool withUndo = true);
00211 void editBegin () { editStart(); }
00216 void editEnd ();
00217 private:
00218 bool m_isInUndo;
00219
00220
00221
00222
00223 public:
00231 bool editInsertText ( uint line, uint col, const QString &s );
00239 bool editRemoveText ( uint line, uint col, uint len );
00240
00249 bool editMarkLineAutoWrapped ( uint line, bool autowrapped );
00250
00261 bool editWrapLine ( uint line, uint col, bool newLine = true, bool *newLineAdded = 0 );
00269 bool editUnWrapLine ( uint line, bool removeLine = true, uint length = 0 );
00270
00277 bool editInsertLine ( uint line, const QString &s );
00283 bool editRemoveLine ( uint line );
00284
00291 bool wrapText (uint startLine, uint endLine);
00292
00293
00294 signals:
00299 void editTextInserted ( uint line, uint col, uint len);
00300
00304 void editTextRemoved ( uint line, uint col, uint len);
00305
00309 void editLineWrapped ( uint line, uint col, uint len );
00310
00314 void editLineUnWrapped ( uint line, uint col );
00315
00319 void editLineInserted ( uint line );
00320
00324 void editLineRemoved ( uint line );
00325
00326 private:
00327 void undoStart();
00328 void undoEnd();
00329 void undoSafePoint();
00330
00331 private slots:
00332 void undoCancel();
00333
00334 private:
00335 void editAddUndo (KateUndoGroup::UndoType type, uint line, uint col, uint len, const QString &text);
00336
00337 uint editSessionNumber;
00338 bool editIsRunning;
00339 bool editWithUndo;
00340 bool m_undoComplexMerge;
00341 KateUndoGroup* m_editCurrentUndo;
00342
00343
00344
00345
00346 public slots:
00347 void undo ();
00348 void redo ();
00349 void clearUndo ();
00350 void clearRedo ();
00351
00352 uint undoCount () const;
00353 uint redoCount () const;
00354
00355 uint undoSteps () const;
00356 void setUndoSteps ( uint steps );
00357
00358 private:
00359 friend class KateTemplateHandler;
00360
00361 private:
00362 QPtrList<KateSuperCursor> m_superCursors;
00363
00364
00365
00366
00367 QPtrList<KateUndoGroup> undoItems;
00368 QPtrList<KateUndoGroup> redoItems;
00369 bool m_undoDontMerge;
00370 bool m_undoIgnoreCancel;
00371 QTimer* m_undoMergeTimer;
00372
00373
00374 KateUndoGroup* lastUndoGroupWhenSaved;
00375 bool docWasSavedWhenUndoWasEmpty;
00376
00377
00378 void updateModified();
00379
00380 signals:
00381 void undoChanged ();
00382 void textInserted(int line,int column);
00383
00384
00385
00386
00387 public slots:
00388 KTextEditor::Cursor *createCursor ();
00389 QPtrList<KTextEditor::Cursor> cursors () const;
00390
00391 private:
00392 QPtrList<KTextEditor::Cursor> myCursors;
00393
00394
00395
00396
00397 public slots:
00398 bool searchText (unsigned int startLine, unsigned int startCol,
00399 const QString &text, unsigned int *foundAtLine, unsigned int *foundAtCol,
00400 unsigned int *matchLen, bool casesensitive = true, bool backwards = false);
00401 bool searchText (unsigned int startLine, unsigned int startCol,
00402 const QRegExp ®exp, unsigned int *foundAtLine, unsigned int *foundAtCol,
00403 unsigned int *matchLen, bool backwards = false);
00404
00405
00406
00407
00408 public slots:
00409 uint hlMode ();
00410 bool setHlMode (uint mode);
00411 uint hlModeCount ();
00412 QString hlModeName (uint mode);
00413 QString hlModeSectionName (uint mode);
00414
00415 public:
00416 void bufferHlChanged ();
00417
00418 private:
00419 void setDontChangeHlOnSave();
00420
00421 signals:
00422 void hlChanged ();
00423
00424
00425
00426
00427 public:
00428 KateArbitraryHighlight* arbitraryHL() const { return m_arbitraryHL; };
00429
00430 private slots:
00431 void tagArbitraryLines(KateView* view, KateSuperRange* range);
00432
00433
00434
00435
00436 public slots:
00437 void readConfig ();
00438 void writeConfig ();
00439 void readConfig (KConfig *);
00440 void writeConfig (KConfig *);
00441 void readSessionConfig (KConfig *);
00442 void writeSessionConfig (KConfig *);
00443 void configDialog ();
00444
00445
00446
00447
00448 public slots:
00449 uint mark( uint line );
00450
00451 void setMark( uint line, uint markType );
00452 void clearMark( uint line );
00453
00454 void addMark( uint line, uint markType );
00455 void removeMark( uint line, uint markType );
00456
00457 QPtrList<KTextEditor::Mark> marks();
00458 void clearMarks();
00459
00460 void setPixmap( MarkInterface::MarkTypes, const QPixmap& );
00461 void setDescription( MarkInterface::MarkTypes, const QString& );
00462 QString markDescription( MarkInterface::MarkTypes );
00463 QPixmap *markPixmap( MarkInterface::MarkTypes );
00464 QColor markColor( MarkInterface::MarkTypes );
00465
00466 void setMarksUserChangable( uint markMask );
00467 uint editableMarks();
00468
00469 signals:
00470 void marksChanged();
00471 void markChanged( KTextEditor::Mark, KTextEditor::MarkInterfaceExtension::MarkChangeAction );
00472
00473 private:
00474 QIntDict<KTextEditor::Mark> m_marks;
00475 QIntDict<QPixmap> m_markPixmaps;
00476 QIntDict<QString> m_markDescriptions;
00477 uint m_editableMarks;
00478
00479
00480
00481
00482 public slots:
00483 bool printDialog ();
00484 bool print ();
00485
00486
00487
00488
00489 public:
00499 QString mimeType();
00500
00508 long fileSize();
00509
00517 QString niceFileSize();
00518
00527 KMimeType::Ptr mimeTypeForContent();
00528
00529
00530
00531
00532 public:
00533 QString variable( const QString &name ) const;
00534
00535 signals:
00536 void variableChanged( const QString &, const QString & );
00537
00538 private:
00539 QMap<QString, QString> m_storedVariables;
00540
00541
00542
00543
00544 public:
00545 bool openURL( const KURL &url );
00546
00547
00548
00549
00550 bool save();
00551
00552
00553 bool saveAs( const KURL &url );
00554
00555 bool openFile (KIO::Job * job);
00556 bool openFile ();
00557
00558 bool saveFile ();
00559
00560 void setReadWrite ( bool rw = true );
00561
00562 void setModified( bool m );
00563
00564 private slots:
00565 void slotDataKate ( KIO::Job* kio_job, const QByteArray &data );
00566 void slotFinishedKate ( KIO::Job * job );
00567
00568 private:
00569 void abortLoadKate();
00570
00571 void activateDirWatch ();
00572 void deactivateDirWatch ();
00573
00574 QString m_dirWatchFile;
00575
00576
00577
00578
00579 public:
00580 Kate::ConfigPage *colorConfigPage (QWidget *) { return 0; }
00581 Kate::ConfigPage *fontConfigPage (QWidget *) { return 0; }
00582 Kate::ConfigPage *indentConfigPage (QWidget *) { return 0; }
00583 Kate::ConfigPage *selectConfigPage (QWidget *) { return 0; }
00584 Kate::ConfigPage *editConfigPage (QWidget *) { return 0; }
00585 Kate::ConfigPage *keysConfigPage (QWidget *) { return 0; }
00586 Kate::ConfigPage *hlConfigPage (QWidget *) { return 0; }
00587 Kate::ConfigPage *viewDefaultsConfigPage (QWidget *) { return 0; }
00588 Kate::ConfigPage *saveConfigPage( QWidget * ) { return 0; }
00589
00590 Kate::ActionMenu *hlActionMenu (const QString& , QObject* = 0, const char* = 0) { return 0; }
00591 Kate::ActionMenu *exportActionMenu (const QString& , QObject* = 0, const char* = 0) { return 0; }
00592
00593 public:
00597 bool typeChars ( KateView *type, const QString &chars );
00598
00602 inline uint lastLine() const { return numLines()-1; }
00603
00604 uint configFlags ();
00605 void setConfigFlags (uint flags);
00606
00607
00608 void repaintViews(bool paintOnlyDirty = true);
00609
00610 inline KateHighlighting *highlight () { return m_buffer->highlight(); }
00611
00612 inline KateHighlighting *highlight () const { return m_buffer->highlight(); }
00613
00614 public slots:
00615 void tagLines(int start, int end);
00616 void tagLines(KateTextCursor start, KateTextCursor end);
00617
00618
00619 public slots:
00620 void exportAs(const QString&) { };
00621
00622 signals:
00623 void modifiedChanged ();
00624 void preHighlightChanged(uint);
00625
00626 private slots:
00627 void internalHlChanged();
00628
00629 public:
00630 void addView(KTextEditor::View *);
00631 void removeView(KTextEditor::View *);
00632
00633 void addSuperCursor(class KateSuperCursor *, bool privateC);
00634 void removeSuperCursor(class KateSuperCursor *, bool privateC);
00635
00636 bool ownedView(KateView *);
00637 bool isLastView(int numViews);
00638
00639 uint currentColumn( const KateTextCursor& );
00640 void newLine( KateTextCursor&, KateViewInternal * );
00641 void backspace( KateView *view, const KateTextCursor& );
00642 void del( KateView *view, const KateTextCursor& );
00643 void transpose( const KateTextCursor& );
00644
00645 void paste ( KateView* view );
00646
00647 public:
00648 void insertIndentChars ( KateView *view );
00649
00650 void indent ( KateView *view, uint line, int change );
00651 void comment ( KateView *view, uint line, uint column, int change );
00652 void align ( KateView *view, uint line );
00653
00654 enum TextTransform { Uppercase, Lowercase, Capitalize };
00655
00663 void transform ( KateView *view, const KateTextCursor &, TextTransform );
00667 void joinLines( uint first, uint last );
00668
00669 private:
00670 void optimizeLeadingSpace( uint line, int flags, int change );
00671 void replaceWithOptimizedSpace( uint line, uint upto_column, uint space, int flags );
00672
00673 bool removeStringFromBegining(int line, QString &str);
00674 bool removeStringFromEnd(int line, QString &str);
00675
00685 bool nextNonSpaceCharPos(int &line, int &col);
00686
00694 bool previousNonSpaceCharPos(int &line, int &col);
00695
00700 void addStartLineCommentToSingleLine(int line, int attrib=0);
00705 bool removeStartLineCommentFromSingleLine(int line, int attrib=0);
00706
00710 void addStartStopCommentToSingleLine(int line, int attrib=0);
00714 bool removeStartStopCommentFromSingleLine(int line, int attrib=0);
00718 bool removeStartStopCommentFromRegion(const KateTextCursor &start, const KateTextCursor &end, int attrib=0);
00719
00724 void addStartStopCommentToSelection( KateView *view, int attrib=0 );
00728 void addStartLineCommentToSelection( KateView *view, int attrib=0 );
00729
00736 bool removeStartStopCommentFromSelection( KateView *view, int attrib=0 );
00740 bool removeStartLineCommentFromSelection( KateView *view, int attrib=0 );
00741
00742 public:
00743 QString getWord( const KateTextCursor& cursor );
00744
00745 public:
00746 void tagAll();
00747
00748 void newBracketMark( const KateTextCursor& start, KateBracketRange& bm, int maxLines = -1 );
00749 bool findMatchingBracket( KateTextCursor& start, KateTextCursor& end, int maxLines = -1 );
00750
00751 private:
00752 void guiActivateEvent( KParts::GUIActivateEvent *ev );
00753
00754 public:
00755
00756 QString docName () {return m_docName;};
00757
00758 void setDocName (QString docName);
00759
00760 void lineInfo (KateLineInfo *info, unsigned int line);
00761
00762 KateCodeFoldingTree *foldingTree ();
00763
00764 public:
00770 bool isModifiedOnDisc() { return m_modOnHd; };
00771
00773 void isModOnHD( bool =false ) {};
00774
00775 void setModifiedOnDisk( int reason );
00776
00777 public slots:
00784 void slotModifiedOnDisk( Kate::View *v=0 );
00785
00789 void reloadFile();
00790
00791 private:
00792 int m_isasking;
00793
00794
00795 public slots:
00796 void setEncoding (const QString &e);
00797 QString encoding() const;
00798
00799 public slots:
00800 void setWordWrap (bool on);
00801 bool wordWrap ();
00802
00803 void setWordWrapAt (uint col);
00804 uint wordWrapAt ();
00805
00806 public slots:
00807 void setPageUpDownMovesCursor(bool on);
00808 bool pageUpDownMovesCursor();
00809
00810 signals:
00811 void modStateChanged (Kate::Document *doc);
00812 void nameChanged (Kate::Document *doc);
00813
00814 public slots:
00815
00816 void flush ();
00817
00818 signals:
00823 void fileNameChanged ();
00824
00825 public slots:
00826 void applyWordWrap ();
00827
00828
00829 public:
00830 inline uint getRealLine(unsigned int virtualLine)
00831 {
00832 return m_buffer->lineNumber (virtualLine);
00833 }
00834
00835 inline uint getVirtualLine(unsigned int realLine)
00836 {
00837 return m_buffer->lineVisibleNumber (realLine);
00838 }
00839
00840 inline uint visibleLines ()
00841 {
00842 return m_buffer->countVisible ();
00843 }
00844
00845 inline KateTextLine::Ptr kateTextLine(uint i)
00846 {
00847 return m_buffer->line (i);
00848 }
00849
00850 inline KateTextLine::Ptr plainKateTextLine(uint i)
00851 {
00852 return m_buffer->plainLine (i);
00853 }
00854
00855 signals:
00856 void codeFoldingUpdated();
00857 void aboutToRemoveText(const KateTextRange&);
00858 void textRemoved();
00859
00860 private slots:
00861 void slotModOnHdDirty (const QString &path);
00862 void slotModOnHdCreated (const QString &path);
00863 void slotModOnHdDeleted (const QString &path);
00864
00865 private:
00875 bool createDigest ( QCString &result );
00876
00882 QString reasonedMOHString() const;
00883
00892 void removeTrailingSpace( uint line );
00893
00894 public:
00895 void updateFileType (int newType, bool user = false);
00896
00897 int fileType () const { return m_fileType; };
00898
00899
00900
00901
00902 private:
00903
00904 KateBuffer *m_buffer;
00905
00906 KateArbitraryHighlight* m_arbitraryHL;
00907
00908 KateAutoIndent *m_indenter;
00909
00910 bool hlSetByUser;
00911
00912 bool m_modOnHd;
00913 unsigned char m_modOnHdReason;
00914 QCString m_digest;
00915
00916 QString m_docName;
00917 int m_docNameNumber;
00918
00919
00920 int m_fileType;
00921 bool m_fileTypeSetByUser;
00922
00926 bool m_reloading;
00927 bool m_loading;
00928 bool m_encodingSticky;
00929
00930 public slots:
00931 void slotQueryClose_save(bool *handled, bool* abortClosing);
00932
00933 public:
00934 void makeAttribs (bool needInvalidate = true);
00935
00936 static bool checkOverwrite( KURL u );
00937
00938 static void setDefaultEncoding (const QString &encoding);
00939
00940 void setEncodingSticky( bool e ) { m_encodingSticky = e; }
00941
00945 public:
00946 inline KateDocumentConfig *config () { return m_config; };
00947
00948 void updateConfig ();
00949
00950 private:
00951 KateDocumentConfig *m_config;
00952
00957 private:
00961 void readDirConfig ();
00962
00967 void readVariables(bool onlyViewAndRenderer = false);
00968
00973 void readVariableLine( QString t, bool onlyViewAndRenderer = false );
00977 void setViewVariable( QString var, QString val );
00983 static bool checkBoolValue( QString value, bool *result );
00989 static bool checkIntValue( QString value, int *result );
00994 static bool checkColorValue( QString value, QColor &col );
00995
00996 static QRegExp kvLine;
00997 static QRegExp kvVar;
00998
00999 KIO::TransferJob *m_job;
01000 KTempFile *m_tempFile;
01001
01002
01003 public:
01004 bool setTabInterceptor(KateKeyInterceptorFunctor *interceptor);
01005 bool removeTabInterceptor(KateKeyInterceptorFunctor *interceptor);
01006 bool invokeTabInterceptor(KKey);
01007
01008 protected:
01009 virtual bool insertTemplateTextImplementation ( uint line, uint column, const QString &templateString, const QMap<QString,QString> &initialValues, QWidget *parentWindow=0 );
01010 KateKeyInterceptorFunctor *m_tabInterceptor;
01011
01012 protected slots:
01013 void testTemplateCode();
01014 void dumpRegionTree();
01015
01016
01017
01018
01019
01020
01021 public slots:
01022 bool setSelection ( uint startLine, uint startCol, uint endLine, uint endCol );
01023 bool clearSelection ();
01024 bool hasSelection () const;
01025 QString selection () const;
01026 bool removeSelectedText ();
01027 bool selectAll();
01028
01029
01030
01031
01032 int selStartLine();
01033 int selStartCol();
01034 int selEndLine();
01035 int selEndCol();
01036
01037
01038
01039 #undef signals
01040 #define signals public
01041 signals:
01042 #undef signals
01043 #define signals protected
01044 void selectionChanged ();
01045
01046
01047
01048
01049
01050 public slots:
01051 bool blockSelectionMode ();
01052 bool setBlockSelectionMode (bool on);
01053 bool toggleBlockSelectionMode ();
01054
01055 private:
01056
01057
01058 k_dcop:
01059 uint documentNumber () const;
01060 };
01061
01062 #endif
01063
01064
01065