Warning, file /frameworks/ktexteditor/src/include/ktexteditor/document.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2001-2014 Christoph Cullmann <cullmann@kde.org>
0003     SPDX-FileCopyrightText: 2005-2014 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KTEXTEDITOR_DOCUMENT_H
0009 #define KTEXTEDITOR_DOCUMENT_H
0010 
0011 #include <ktexteditor_export.h>
0012 
0013 #include <ktexteditor/attribute.h>
0014 #include <ktexteditor/cursor.h>
0015 #include <ktexteditor/range.h>
0016 
0017 // our main baseclass of the KTextEditor::Document
0018 #include <KParts/ReadWritePart>
0019 
0020 // the list of views
0021 #include <QList>
0022 #include <QMetaType>
0023 
0024 class KConfigGroup;
0025 
0026 namespace KTextEditor
0027 {
0028 class DocumentPrivate;
0029 class EditingTransactionPrivate;
0030 class MainWindow;
0031 class Message;
0032 class View;
0033 
0034 /**
0035  * \brief Search flags for use with searchText.
0036  *
0037  * Modifies the behavior of searchText.
0038  * By default it is searched for a case-sensitive plaintext pattern,
0039  * without processing of escape sequences, with "whole words" off,
0040  * in forward direction, within a non-block-mode text range.
0041  *
0042  * \see SearchOptions
0043  * \author Sebastian Pipping \<webmaster@hartwork.org\>
0044  */
0045 enum SearchOption {
0046     Default = 0, ///< Default settings
0047 
0048     // modes
0049     Regex = 1 << 1, ///< Treats the pattern as a regular expression
0050 
0051     // options for all modes
0052     CaseInsensitive = 1 << 4, ///< Ignores cases, e.g. "a" matches "A"
0053     Backwards = 1 << 5, ///< Searches in backward direction
0054 
0055     // options for plaintext
0056     EscapeSequences = 1 << 10, ///< Plaintext mode: Processes escape sequences
0057     WholeWords = 1 << 11, ///< Plaintext mode: Whole words only, e.g. @em not &quot;amp&quot; in &quot;example&quot;
0058 
0059     MaxSearchOption = 1 << 31 ///< Placeholder for binary compatibility
0060 };
0061 
0062 /// Stores a combination of #SearchOption values.
0063 Q_DECLARE_FLAGS(SearchOptions, SearchOption)
0064 Q_DECLARE_OPERATORS_FOR_FLAGS(SearchOptions)
0065 
0066 /**
0067  * \class Document document.h <KTextEditor/Document>
0068  *
0069  * \brief A KParts derived class representing a text document.
0070  *
0071  * Topics:
0072  *  - \ref doc_intro
0073  *  - \ref doc_manipulation
0074  *  - \ref doc_views
0075  *  - \ref doc_readwrite
0076  *  - \ref doc_notifications
0077  *  - \ref doc_recovery
0078  *  - \ref doc_extensions
0079  *
0080  * \section doc_intro Introduction
0081  *
0082  * The Document class represents a pure text document providing methods to
0083  * modify the content and create views. A document can have any number
0084  * of views, each view representing the same content, i.e. all views are
0085  * synchronized. Support for text selection is handled by a View and text
0086  * format attributes by the Attribute class.
0087  *
0088  * To load a document call KParts::ReadOnlyPart::openUrl().
0089  * To reload a document from a file call documentReload(), to save the
0090  * document call documentSave() or documentSaveAs(). Whenever the modified
0091  * state of the document changes the signal modifiedChanged() is emitted.
0092  * Check the modified state with KParts::ReadWritePart::isModified().
0093  * Further signals are documentUrlChanged(). The encoding can be specified
0094  * with setEncoding(), however this will only take effect on file reload and
0095  * file save.
0096  *
0097  * \section doc_manipulation Text Manipulation
0098  *
0099  * Get the whole content with text() and set new content with setText().
0100  * Call insertText() or insertLine() to insert new text or removeText()
0101  * and removeLine() to remove content. Whenever the document's content
0102  * changed the signal textChanged() is emitted. Additional signals are
0103  * textInserted() and textRemoved(). Note, that the first line in the
0104  * document is line 0.
0105  *
0106  * A Document provides full undo/redo history.
0107  * Text manipulation actions can be grouped together to one undo/redo action by
0108  * using an the class EditingTransaction. You can stack multiple EditingTransaction%s.
0109  * Internally, the Document has a reference counter. If this reference counter
0110  * is increased the first time (by creating an instance of EditingTransaction),
0111  * the signal editingStarted() is emitted. Only when the internal reference counter
0112  * reaches zero again, the signal editingFinished() and optionally the signal
0113  * textChanged() are emitted. Whether an editing transaction is currently active
0114  * can be checked by calling isEditingTransactionRunning().
0115  *
0116  * @note The signal editingFinished() is always emitted when the last instance
0117  *       of EditingTransaction is destroyed. Contrary, the signal textChanged()
0118  *       is emitted only if text changed. Hence, textChanged() is more accurate
0119  *       with respect to changes in the Document.
0120  *
0121  * Every text editing transaction is also available through the signals
0122  * lineWrapped(), lineUnwrapped(), textInserted() and textRemoved().
0123  * However, these signals should be used with care. Please be aware of the
0124  * following warning:
0125  *
0126  * @warning Never change the Document's contents when edit actions are active,
0127  *          i.e. in between of (foreign) editing transactions. In case you
0128  *          violate this, the currently active edit action may perform edits
0129  *          that lead to undefined behavior.
0130  *
0131  * \section doc_views Document Views
0132  *
0133  * A View displays the document's content. As already mentioned, a document
0134  * can have any number of views, all synchronized. Get a list of all views
0135  * with views(). Create a new view with createView(). Every time a new view
0136  * is created the signal viewCreated() is emitted.
0137  *
0138  * \section doc_readwrite Read-Only Mode
0139  *
0140  * A Document may be in read-only mode, for instance due to missing file
0141  * permissions. The read-only mode can be checked with isReadWrite(). Further,
0142  * the signal readWriteChanged() is emitted whenever the state changes either
0143  * to read-only mode or to read/write mode. The read-only mode can be controlled
0144  * with setReadWrite().
0145  *
0146  * \section doc_notifications Notifications in Documents and Views
0147  *
0148  * A Document has the ability to show a Message to the user in a View.
0149  * The Message then is shown either the specified View if Message::setView()
0150  * was called, or in all View%s of the Document.
0151  *
0152  * To post a message just create a new Message and send it with postMessage().
0153  * Further information is available in the API documentation of Message.
0154  *
0155  * @see Message
0156  *
0157  * \section doc_recovery Crash Recovery for Documents
0158  *
0159  * When the system or the application using the editor component crashed
0160  * with unsaved changes in the Document, the View notifies the user about
0161  * the lost data and asks, whether the data should be recovered.
0162  *
0163  * This Document gives you control over the data recovery process. Use
0164  * isDataRecoveryAvailable() to check for lost data. If you do not want the
0165  * editor component to handle the data recovery process automatically, you can
0166  * either trigger the data recovery by calling recoverData() or discard it
0167  * through discardDataRecovery().
0168  *
0169  * \section doc_extensions Document Extension Interfaces
0170  *
0171  * A simple document represents text and provides text manipulation methods.
0172  * However, a real text editor should support advanced concepts like session
0173  * support, textsearch support, bookmark/general mark support etc. That is why
0174  * the KTextEditor library provides several additional interfaces to extend
0175  * a document's capabilities via multiple inheritance.
0176  *
0177  * More information about interfaces for the document can be found in
0178  * \ref kte_group_doc_extensions.
0179  *
0180  * \see KParts::ReadWritePart, KTextEditor::Editor, KTextEditor::View,
0181  *      KTextEditor::MarkInterface, KTextEditor::ModificationInterface,
0182  *      KTextEditor::MovingInterface
0183  * \author Christoph Cullmann \<cullmann@kde.org\>
0184  */
0185 class KTEXTEDITOR_EXPORT Document : public KParts::ReadWritePart
0186 {
0187     Q_OBJECT
0188 
0189 protected:
0190     /**
0191      * Constructor.
0192      *
0193      * Create a new document with \p parent.
0194      *
0195      * Pass it the internal implementation to store a d-pointer.
0196      *
0197      * \param impl d-pointer to use
0198      * \param parent parent object
0199      * \see Editor::createDocument()
0200      */
0201     Document(DocumentPrivate *impl, QObject *parent);
0202 
0203 public:
0204     /**
0205      * Virtual destructor.
0206      */
0207     ~Document() override;
0208 
0209     /**
0210      * \name Manage View%s of this Document
0211      *
0212      * \{
0213      */
0214 public:
0215     /**
0216      * Create a new view attached to @p parent.
0217      * @param parent parent widget
0218      * @param mainWindow the main window responsible for this view, if any
0219      * @return the new view
0220      */
0221     virtual View *createView(QWidget *parent, KTextEditor::MainWindow *mainWindow = nullptr) = 0;
0222 
0223     /**
0224      * Returns the views pre-casted to KTextEditor::View%s
0225      */
0226     virtual QList<View *> views() const = 0;
0227 
0228 Q_SIGNALS:
0229     /**
0230      * This signal is emitted whenever the \p document creates a new \p view.
0231      * It should be called for every view to help applications / plugins to
0232      * attach to the \p view.
0233      * \attention This signal should be emitted after the view constructor is
0234      *            completed, e.g. in the createView() method.
0235      * \param document the document for which a new view is created
0236      * \param view the new view
0237      * \see createView()
0238      */
0239     void viewCreated(KTextEditor::Document *document, KTextEditor::View *view);
0240 
0241     //!\}
0242 
0243     /**
0244      * \name General Information about this Document
0245      *
0246      * \{
0247      */
0248 public:
0249     /**
0250      * Get this document's name.
0251      * The editor part should provide some meaningful name, like some unique
0252      * "Untitled XYZ" for the document - \e without URL or basename for
0253      * documents with url.
0254      * \return readable document name
0255      */
0256     virtual QString documentName() const = 0;
0257 
0258     /**
0259      * Get this document's mimetype.
0260      * \return mimetype
0261      */
0262     virtual QString mimeType() = 0;
0263 
0264     /**
0265      * Get the git hash of the Document's contents on disk.
0266      * The returned hash equals the git hash of the file written to disk.
0267      * If the document is a remote document, the checksum may not be
0268      * available. In this case, QByteArray::isNull() returns \e true.
0269      *
0270      * git hash is defined as:
0271      *
0272      * sha1("blob " + filesize + "\0" + filecontent)
0273      *
0274      * \return the git hash of the document
0275      */
0276     virtual QByteArray checksum() const = 0;
0277 
0278     /*
0279      * SIGNALS
0280      * following signals should be emitted by the editor document.
0281      */
0282 Q_SIGNALS:
0283     /**
0284      * This signal is emitted whenever the \p document name changes.
0285      * \param document document which changed its name
0286      * \see documentName()
0287      */
0288     void documentNameChanged(KTextEditor::Document *document);
0289 
0290     /**
0291      * This signal is emitted whenever the \p document URL changes.
0292      * \param document document which changed its URL
0293      * \see KParts::ReadOnlyPart::url()
0294      */
0295     void documentUrlChanged(KTextEditor::Document *document);
0296 
0297     /**
0298      * This signal is emitted whenever the \p document's buffer changed from
0299      * either state \e unmodified to \e modified or vice versa.
0300      *
0301      * \param document document which changed its modified state
0302      * \see KParts::ReadWritePart::isModified().
0303      * \see KParts::ReadWritePart::setModified()
0304      */
0305     void modifiedChanged(KTextEditor::Document *document);
0306 
0307     /**
0308      * This signal is emitted whenever the readWrite state of a document
0309      * changes.
0310      * \param document the document whose read/write property changed
0311      * \see KParts::ReadWritePart::setReadWrite()
0312      */
0313     void readWriteChanged(KTextEditor::Document *document);
0314 
0315     /*
0316      * VERY IMPORTANT: Methods to set and query the current encoding of the
0317      * document
0318      */
0319 public:
0320     /**
0321      * Set the encoding for this document. This encoding will be used
0322      * while loading and saving files, it will \e not affect the already
0323      * existing content of the document, e.g. if the file has already been
0324      * opened without the correct encoding, this will \e not fix it, you
0325      * would for example need to trigger a reload for this.
0326      * \param encoding new encoding for the document, the name must be
0327      *        accepted by QTextCodec, if an empty encoding name is given, the
0328      *        part should fallback to its own default encoding, e.g. the
0329      *        system encoding or the global user settings
0330      * \return \e true on success, or \e false, if the encoding could not be set.
0331      * \see encoding()
0332      */
0333     virtual bool setEncoding(const QString &encoding) = 0;
0334 
0335     /**
0336      * Get the current chosen encoding. The return value is an empty string,
0337      * if the document uses the default encoding of the editor and no own
0338      * special encoding.
0339      * \return current encoding of the document
0340      * \see setEncoding()
0341      */
0342     virtual QString encoding() const = 0;
0343 
0344     //!\}
0345 
0346     /**
0347      * \name File Loading and Saving
0348      *
0349      * All this actions cause user interaction in some cases.
0350      * \{
0351      */
0352 public:
0353     /**
0354      * Reload the current file.
0355      * The user will be prompted by the part on changes and more and can
0356      * cancel this action if it can harm.
0357      * \return \e true if the reload has been done, otherwise \e false. If
0358      *         the document has no url set, it will just return \e false.
0359      */
0360     virtual bool documentReload() = 0;
0361 
0362     /**
0363      * Save the current file.
0364      * The user will be asked for a filename if needed and more.
0365      * \return \e true on success, i.e. the save has been done, otherwise
0366      *         \e false
0367      */
0368     virtual bool documentSave() = 0;
0369 
0370     /**
0371      * Save the current file to another location.
0372      * The user will be asked for a filename and more.
0373      * \return \e true on success, i.e. the save has been done, otherwise
0374      *         \e false
0375      */
0376     virtual bool documentSaveAs() = 0;
0377 
0378     /**
0379      * True, eg if the file for opening could not be read
0380      * This doesn't have to handle the KPart job canceled cases.
0381      * @return was there some problem loading the file?
0382      */
0383     bool openingError() const;
0384 
0385     /**
0386      * Error message if any problem occurred on last load.
0387      * @return error message what went wrong on loading
0388      */
0389     // TODO KF6: Not needed anymore since we show load trouble as KTextEditor::Message.
0390     //      Remove all code which set m_openingErrorMessage
0391     QString openingErrorMessage() const;
0392 
0393     /*
0394      * SIGNALS
0395      * Following signals should be emitted by the document if the text content
0396      * is changed.
0397      */
0398 Q_SIGNALS:
0399     /**
0400      * This signal should be emitted after a document has been saved to disk or for remote files uploaded.
0401      * saveAs should be set to true, if the operation is a save as operation
0402      */
0403     void documentSavedOrUploaded(KTextEditor::Document *document, bool saveAs);
0404 
0405     /**
0406      * Warn anyone listening that the current document is about to close.
0407      * At this point all of the information is still accessible, such as the text,
0408      * cursors and ranges.
0409      *
0410      * Any modifications made to the document at this point will be lost.
0411      *
0412      * \param document the document being closed
0413      */
0414     void aboutToClose(KTextEditor::Document *document);
0415 
0416     /**
0417      * Warn anyone listening that the current document is about to reload.
0418      * At this point all of the information is still accessible, such as the text,
0419      * cursors and ranges.
0420      *
0421      * Any modifications made to the document at this point will be lost.
0422      *
0423      * \param document the document being reloaded
0424      */
0425     void aboutToReload(KTextEditor::Document *document);
0426 
0427     /**
0428      * Emitted after the current document was reloaded.
0429      * At this point, some information might have been invalidated, like
0430      * for example the editing history.
0431      *
0432      * \param document the document that was reloaded.
0433      *
0434      * @since 4.6
0435      */
0436     void reloaded(KTextEditor::Document *document);
0437 
0438     /**
0439      * Emitted just before the document will be saved
0440      * Any modifications made to the document at this point
0441      * will get stored on disk.
0442      *
0443      * \param document the document that was reloaded.
0444      *
0445      * @since 5.91
0446      */
0447     void aboutToSave(KTextEditor::Document *document);
0448 
0449     //!\}
0450 
0451     /**
0452      * \name Text Manipulation
0453      *
0454      * \{
0455      */
0456 public:
0457     /**
0458      * Editing transaction support.
0459      *
0460      * Edit commands during this sequence will be bunched together so that
0461      * they represent a single undo command in the editor, and so that
0462      * repaint events do not occur in between.
0463      *
0464      * Your application should \e not return control to the event loop while
0465      * it has an unterminated (i.e. this object is not destructed) editing
0466      * sequence (result undefined) - so do all of your work in one go!
0467      *
0468      * Using this class typically looks as follows:
0469      * @code
0470      * void foo() {
0471      *     KTextEditor::Document::EditingTransaction transaction(document);
0472      *     // now call editing functions
0473      *     document->removeText(...)
0474      *     document->insertText(...)
0475      * }
0476      * @endcode
0477      *
0478      * Although usually not required, the EditingTransaction additionally
0479      * allows to manually call finish() and start() in between.
0480      *
0481      * @see editingStarted(), editingFinished()
0482      */
0483     class KTEXTEDITOR_EXPORT EditingTransaction
0484     {
0485     public:
0486         /**
0487          * Constructs the object and starts an editing transaction by
0488          * calling start().
0489          *
0490          * @param document document for the transaction
0491          * @see start()
0492          */
0493         explicit EditingTransaction(Document *document);
0494 
0495         /**
0496          * Destructs the object and, if needed, finishes a running editing
0497          * transaction by calling finish().
0498          *
0499          * @see finish()
0500          */
0501         ~EditingTransaction();
0502 
0503         /**
0504          * By calling start(), the editing transaction can be started again.
0505          * This function only is of use in combination with finish().
0506          *
0507          * @see finish()
0508          */
0509         void start();
0510 
0511         /**
0512          * By calling finish(), the editing transaction can be finished
0513          * already before destruction of this instance.
0514          *
0515          * @see start()
0516          */
0517         void finish();
0518 
0519     private:
0520         /**
0521          * no copying allowed
0522          */
0523         Q_DISABLE_COPY(EditingTransaction)
0524 
0525         /**
0526          * private d-pointer
0527          */
0528         EditingTransactionPrivate *const d;
0529     };
0530 
0531     /**
0532      * Check whether an editing transaction is currently running.
0533      *
0534      * @see EditingTransaction
0535      */
0536     virtual bool isEditingTransactionRunning() const = 0;
0537 
0538     /*
0539      * General access to the document's text content.
0540      */
0541 public:
0542     /**
0543      * Get the document content.
0544      * \return the complete document content
0545      * \see setText()
0546      */
0547     virtual QString text() const = 0;
0548 
0549     /**
0550      * Get the document content within the given \p range.
0551      * \param range the range of text to retrieve
0552      * \param block Set this to \e true to receive text in a visual block,
0553      *        rather than everything inside \p range.
0554      * \return the requested text part, or QString() for invalid ranges.
0555      * \see setText()
0556      */
0557     virtual QString text(const Range &range, bool block = false) const = 0;
0558 
0559     /**
0560      * Get the character at text position \p cursor.
0561      * \param position the location of the character to retrieve
0562      * \return the requested character, or QChar() for invalid cursors.
0563      * \see setText()
0564      */
0565     virtual QChar characterAt(const Cursor &position) const = 0;
0566 
0567     /**
0568      * Get the word at the text position \p cursor.
0569      * The returned word is defined by the word boundaries to the left and
0570      * right starting at \p cursor. The algorithm takes highlighting information
0571      * into account, e.g. a dash ('-') in C++ is interpreted as word boundary,
0572      * whereas e.g. CSS allows identifiers with dash ('-').
0573      *
0574      * If \p cursor is not a valid text position or if there is no word
0575      * under the requested position \p cursor, an empty string is returned.
0576      *
0577      * \param cursor requested cursor position for the word
0578      * \return the word under the cursor or an empty string if there is no word.
0579      *
0580      * \see wordRangeAt(), characterAt()
0581      */
0582     virtual QString wordAt(const KTextEditor::Cursor &cursor) const = 0;
0583 
0584     /**
0585      * Get the text range for the word located under the text position \p cursor.
0586      * The returned word is defined by the word boundaries to the left and
0587      * right starting at \p cursor. The algorithm takes highlighting information
0588      * into account, e.g. a dash ('-') in C++ is interpreted as word boundary,
0589      * whereas e.g. CSS allows identifiers with dash ('-').
0590      *
0591      * If \p cursor is not a valid text position or if there is no word
0592      * under the requested position \p cursor, an invalid text range is returned.
0593      * If the text range is valid, it is \e always on a single line.
0594      *
0595      * \param cursor requested cursor position for the word
0596      * \return the Range spanning the word under the cursor or an invalid range if there is no word.
0597      *
0598      * \see wordAt(), characterAt(), KTextEditor::Range::isValid()
0599      */
0600     virtual KTextEditor::Range wordRangeAt(const KTextEditor::Cursor &cursor) const = 0;
0601 
0602     /**
0603      * Get whether \p cursor is a valid text position.
0604      * A cursor position at (line, column) is valid, if
0605      * - line >= 0 and line < lines() holds, and
0606      * - column >= 0 and column <= lineLength(column).
0607      *
0608      * The text position \p cursor is also invalid if it is inside a Unicode surrogate.
0609      * Therefore, use this function when iterating over the characters of a line.
0610      *
0611      * \param cursor cursor position to check for validity
0612      * \return true, if \p cursor is a valid text position, otherwise \p false
0613      *
0614      * \since 5.0
0615      */
0616     virtual bool isValidTextPosition(const KTextEditor::Cursor &cursor) const = 0;
0617 
0618     /**
0619      * Get the document content within the given \p range.
0620      * \param range the range of text to retrieve
0621      * \param block Set this to \e true to receive text in a visual block,
0622      *        rather than everything inside \p range.
0623      * \return the requested text lines, or QStringList() for invalid ranges.
0624      *         no end of line termination is included.
0625      * \see setText()
0626      */
0627     virtual QStringList textLines(const Range &range, bool block = false) const = 0;
0628 
0629     /**
0630      * Get a single text line.
0631      * \param line the wanted line
0632      * \return the requested line, or "" for invalid line numbers
0633      * \see text(), lineLength()
0634      */
0635     virtual QString line(int line) const = 0;
0636 
0637     /**
0638      * Get the count of lines of the document.
0639      * \return the current number of lines in the document
0640      * \see length()
0641      */
0642     virtual int lines() const = 0;
0643 
0644     /**
0645      * Check whether \p line currently contains unsaved data.
0646      * If \p line contains unsaved data, \e true is returned, otherwise \e false.
0647      * When the user saves the file, a modified line turns into a \e saved line.
0648      * In this case isLineModified() returns \e false and in its stead isLineSaved()
0649      * returns \e true.
0650      * \param line line to query
0651      * \see isLineSaved(), isLineTouched()
0652      * \since 5.0
0653      */
0654     virtual bool isLineModified(int line) const = 0;
0655 
0656     /**
0657      * Check whether \p line currently contains only saved text.
0658      * Saved text in this case implies that a line was touched at some point
0659      * by the user and then then changes were either undone or the user saved
0660      * the file.
0661      *
0662      * In case \p line was touched and currently contains only saved data,
0663      * \e true is returned, otherwise \e false.
0664      * \param line line to query
0665      * \see isLineModified(), isLineTouched()
0666      * \since 5.0
0667      */
0668     virtual bool isLineSaved(int line) const = 0;
0669 
0670     /**
0671      * Check whether \p line was touched since the file was opened.
0672      * This equals the statement isLineModified() || isLineSaved().
0673      * \param line line to query
0674      * \see isLineModified(), isLineSaved()
0675      * \since 5.0
0676      */
0677     virtual bool isLineTouched(int line) const = 0;
0678 
0679     /**
0680      * End position of the document.
0681      * \return The last column on the last line of the document
0682      * \see all()
0683      */
0684     virtual Cursor documentEnd() const = 0;
0685 
0686     /**
0687      * A Range which encompasses the whole document.
0688      * \return A range from the start to the end of the document
0689      */
0690     inline Range documentRange() const
0691     {
0692         return Range(Cursor::start(), documentEnd());
0693     }
0694 
0695     /**
0696      * Get the count of characters in the document. A TAB character counts as
0697      * only one character.
0698      * \return the number of characters in the document
0699      * \see lines()
0700      */
0701     virtual int totalCharacters() const = 0;
0702 
0703     /**
0704      * Returns if the document is empty.
0705      */
0706     virtual bool isEmpty() const;
0707 
0708     /**
0709      * Get the length of a given line in characters.
0710      * \param line line to get length from
0711      * \return the number of characters in the line or -1 if the line was
0712      *         invalid
0713      * \see line()
0714      */
0715     virtual int lineLength(int line) const = 0;
0716 
0717     /**
0718      * Get the end cursor position of line \p line.
0719      * \param line line
0720      * \see lineLength(), line()
0721      */
0722     inline Cursor endOfLine(int line) const
0723     {
0724         return Cursor(line, lineLength(line));
0725     }
0726 
0727     /**
0728      * Set the given text as new document content.
0729      * \param text new content for the document
0730      * \return \e true on success, otherwise \e false
0731      * \see text()
0732      */
0733     virtual bool setText(const QString &text) = 0;
0734 
0735     /**
0736      * Set the given text as new document content.
0737      * \param text new content for the document
0738      * \return \e true on success, otherwise \e false
0739      * \see text()
0740      */
0741     virtual bool setText(const QStringList &text) = 0;
0742 
0743     /**
0744      * Remove the whole content of the document.
0745      * \return \e true on success, otherwise \e false
0746      * \see removeText(), removeLine()
0747      */
0748     virtual bool clear() = 0;
0749 
0750     /**
0751      * Insert \p text at \p position.
0752      * \param position position to insert the text
0753      * \param text text to insert
0754      * \param block insert this text as a visual block of text rather than a linear sequence
0755      * \return \e true on success, otherwise \e false
0756      * \see setText(), removeText()
0757      */
0758     virtual bool insertText(const Cursor &position, const QString &text, bool block = false) = 0;
0759 
0760     /**
0761      * Insert \p text at \p position.
0762      * \param position position to insert the text
0763      * \param text text to insert
0764      * \param block insert this text as a visual block of text rather than a linear sequence
0765      * \return \e true on success, otherwise \e false
0766      * \see setText(), removeText()
0767      */
0768     virtual bool insertText(const Cursor &position, const QStringList &text, bool block = false) = 0;
0769 
0770     /**
0771      * Replace text from \p range with specified \p text.
0772      * \param range range of text to replace
0773      * \param text text to replace with
0774      * \param block replace text as a visual block of text rather than a linear sequence
0775      * \return \e true on success, otherwise \e false
0776      * \see setText(), removeText(), insertText()
0777      */
0778     virtual bool replaceText(const Range &range, const QString &text, bool block = false);
0779 
0780     /**
0781      * Replace text from \p range with specified \p text.
0782      * \param range range of text to replace
0783      * \param text text to replace with
0784      * \param block replace text as a visual block of text rather than a linear sequence
0785      * \return \e true on success, otherwise \e false
0786      * \see setText(), removeText(), insertText()
0787      */
0788     virtual bool replaceText(const Range &range, const QStringList &text, bool block = false);
0789 
0790     /**
0791      * Remove the text specified in \p range.
0792      * \param range range of text to remove
0793      * \param block set this to true to remove a text block on the basis of columns, rather than everything inside \p range
0794      * \return \e true on success, otherwise \e false
0795      * \see setText(), insertText()
0796      */
0797     virtual bool removeText(const Range &range, bool block = false) = 0;
0798 
0799     /**
0800      * Insert line(s) at the given line number. The newline character '\\n'
0801      * is treated as line delimiter, so it is possible to insert multiple
0802      * lines. To append lines at the end of the document, use
0803      * \code
0804      * insertLine( lines(), text )
0805      * \endcode
0806      * \param line line where to insert the text
0807      * \param text text which should be inserted
0808      * \return \e true on success, otherwise \e false
0809      * \see insertText()
0810      */
0811     virtual bool insertLine(int line, const QString &text) = 0;
0812 
0813     /**
0814      * Insert line(s) at the given line number. The newline character '\\n'
0815      * is treated as line delimiter, so it is possible to insert multiple
0816      * lines. To append lines at the end of the document, use
0817      * \code
0818      * insertLine( lines(), text )
0819      * \endcode
0820      * \param line line where to insert the text
0821      * \param text text which should be inserted
0822      * \return \e true on success, otherwise \e false
0823      * \see insertText()
0824      */
0825     virtual bool insertLines(int line, const QStringList &text) = 0;
0826 
0827     /**
0828      * Remove \p line from the document.
0829      * \param line line to remove
0830      * \return \e true on success, otherwise \e false
0831      * \see removeText(), clear()
0832      */
0833     virtual bool removeLine(int line) = 0;
0834 
0835     /**
0836      * \brief Searches the given input range for a text pattern.
0837      *
0838      * Searches for a text pattern within the given input range.
0839      * The kind of search performed depends on the \p options
0840      * used. Use this function for plaintext searches as well as
0841      * regular expression searches. If no match is found the first
0842      * (and only) element in the vector return is the invalid range.
0843      * When searching for regular expressions, the first element holds
0844      * the range of the full match, the subsequent elements hold
0845      * the ranges of the capturing parentheses.
0846      *
0847      * \param range    Input range to search in
0848      * \param pattern  Text pattern to search for
0849      * \param options  Combination of search flags
0850      * \return         List of ranges (length >=1)
0851      *
0852      * \author Sebastian Pipping \<webmaster@hartwork.org\>
0853      *
0854      * \since 5.11
0855      */
0856     QVector<KTextEditor::Range> searchText(const KTextEditor::Range &range, const QString &pattern, const SearchOptions options = Default) const;
0857 
0858     /*
0859      * SIGNALS
0860      * Following signals should be emitted by the document if the text content
0861      * is changed.
0862      */
0863 Q_SIGNALS:
0864     /**
0865      * Editing transaction has started.
0866      * \param document document which emitted this signal
0867      */
0868     void editingStarted(KTextEditor::Document *document);
0869 
0870     /**
0871      * Editing transaction has finished.
0872      *
0873      * @note This signal is emitted also for editing actions that maybe do not
0874      *       modify the @p document contents (think of having an empty
0875      *       EditingTransaction). If you want to get notified only
0876      *       after text really changed, connect to the signal textChanged().
0877      *
0878      * \param document document which emitted this signal
0879      * @see textChanged()
0880      */
0881     void editingFinished(KTextEditor::Document *document);
0882 
0883     /**
0884      * A line got wrapped.
0885      * \param document document which emitted this signal
0886      * @param position position where the wrap occurred
0887      */
0888     void lineWrapped(KTextEditor::Document *document, const KTextEditor::Cursor &position);
0889 
0890     /**
0891      * A line got unwrapped.
0892      * \param document document which emitted this signal
0893      * @param line line where the unwrap occurred
0894      */
0895     void lineUnwrapped(KTextEditor::Document *document, int line);
0896 
0897     /**
0898      * Text got inserted.
0899      * \param document document which emitted this signal
0900      * @param position position where the insertion occurred
0901      * @param text inserted text
0902      */
0903     void textInserted(KTextEditor::Document *document, const KTextEditor::Cursor &position, const QString &text);
0904 
0905     /**
0906      * Text got removed.
0907      * \param document document which emitted this signal
0908      * @param range range where the removal occurred
0909      * @param text removed text
0910      */
0911     void textRemoved(KTextEditor::Document *document, const KTextEditor::Range &range, const QString &text);
0912 
0913     /**
0914      * The \p document emits this signal whenever its text changes.
0915      * \param document document which emitted this signal
0916      * \see text(), textLine()
0917      */
0918     void textChanged(KTextEditor::Document *document);
0919 
0920     //!\}
0921 
0922     /**
0923      * \name Highlighting and Related Information
0924      *
0925      * \{
0926      */
0927 public:
0928     /**
0929      * Get the default style of the character located at @p position.
0930      * If @p position is not a valid text position, the default style
0931      * DefaultStyle::dsNormal is returned.
0932      *
0933      * @note Further information about the colors of default styles depend on
0934      *       the currently chosen schema. Since each View may have a different
0935      *       color schema, the color information can be obtained through
0936      *       View::defaultStyleAttribute() and View::lineAttributes().
0937      *
0938      * @param position text position
0939      * @return default style, see enum KTextEditor::DefaultStyle
0940      * @see View::defaultStyleAttribute(), View::lineAttributes()
0941      */
0942     virtual DefaultStyle defaultStyleAt(const KTextEditor::Cursor &position) const = 0;
0943 
0944     /**
0945      * Return the name of the currently used mode
0946      * \return name of the used mode
0947      * \see modes(), setMode()
0948      */
0949     virtual QString mode() const = 0;
0950 
0951     /**
0952      * Return the name of the currently used mode
0953      * \return name of the used mode
0954      * \see highlightingModes(), setHighlightingMode()
0955      */
0956     virtual QString highlightingMode() const = 0;
0957 
0958     /**
0959      * \brief Get all available highlighting modes for the current document.
0960      *
0961      * Each document can be highlighted using an arbitrary number of highlighting
0962      * contexts. This method will return the names for each of the used modes.
0963      *
0964      * Example: The "PHP (HTML)" mode includes the highlighting for PHP, HTML, CSS and JavaScript.
0965      *
0966      * \return Returns a list of embedded highlighting modes for the current Document.
0967      *
0968      * \see KTextEditor::Document::highlightingMode()
0969      */
0970     virtual QStringList embeddedHighlightingModes() const = 0;
0971 
0972     /**
0973      * \brief Get the highlight mode used at a given position in the document.
0974      *
0975      * Retrieve the name of the applied highlight mode at a given \p position
0976      * in the current document.
0977      *
0978      * Calling this might trigger re-highlighting up to the given line.
0979      * Therefore this is not const.
0980      *
0981      * \see highlightingModes()
0982      */
0983     virtual QString highlightingModeAt(const Cursor &position) = 0;
0984 
0985     /**
0986      * Return a list of the names of all possible modes
0987      * \return list of mode names
0988      * \see mode(), setMode()
0989      */
0990     virtual QStringList modes() const = 0;
0991 
0992     /**
0993      * Return a list of the names of all possible modes
0994      * \return list of mode names
0995      * \see highlightingMode(), setHighlightingMode()
0996      */
0997     virtual QStringList highlightingModes() const = 0;
0998 
0999     /**
1000      * Set the current mode of the document by giving its name
1001      * \param name name of the mode to use for this document
1002      * \return \e true on success, otherwise \e false
1003      * \see mode(), modes(), modeChanged()
1004      */
1005     virtual bool setMode(const QString &name) = 0;
1006 
1007     /**
1008      * Set the current mode of the document by giving its name
1009      * \param name name of the mode to use for this document
1010      * \return \e true on success, otherwise \e false
1011      * \see highlightingMode(), highlightingModes(), highlightingModeChanged()
1012      */
1013     virtual bool setHighlightingMode(const QString &name) = 0;
1014 
1015     /**
1016      * Returns the name of the section for a highlight given its index in the highlight
1017      * list (as returned by highlightModes()).
1018      *
1019      * You can use this function to build a tree of the highlight names, organized in sections.
1020      *
1021      * \param index the index of the highlight in the list returned by modes()
1022      */
1023     virtual QString highlightingModeSection(int index) const = 0;
1024 
1025     /**
1026      * Returns the name of the section for a mode given its index in the highlight
1027      * list (as returned by modes()).
1028      *
1029      * You can use this function to build a tree of the mode names, organized in sections.
1030      *
1031      * \param index the index of the highlight in the list returned by modes()
1032      */
1033     virtual QString modeSection(int index) const = 0;
1034 
1035     /*
1036      * SIGNALS
1037      * Following signals should be emitted by the document if the mode
1038      * of the document changes
1039      */
1040 Q_SIGNALS:
1041     /**
1042      * Warn anyone listening that the current document's mode has
1043      * changed.
1044      *
1045      * \param document the document whose mode has changed
1046      * \see setMode()
1047      */
1048     void modeChanged(KTextEditor::Document *document);
1049 
1050     /**
1051      * Warn anyone listening that the current document's highlighting mode has
1052      * changed.
1053      *
1054      * \param document the document which's mode has changed
1055      * \see setHighlightingMode()
1056      */
1057     void highlightingModeChanged(KTextEditor::Document *document);
1058 
1059     //!\}
1060 
1061     /**
1062      * \name Printing
1063      *
1064      * \{
1065      */
1066 public:
1067     /**
1068      * Print the document. This should result in showing the print dialog.
1069      *
1070      * @returns true if document was printed
1071      */
1072     virtual bool print() = 0;
1073 
1074     /**
1075      * Shows the print preview dialog/
1076      */
1077     virtual void printPreview() = 0;
1078 
1079     //!\}
1080 
1081     /**
1082      * \name Showing Interactive Notifications
1083      *
1084      * \{
1085      */
1086 public:
1087     /**
1088      * Post @p message to the Document and its View%s.
1089      * If multiple Message%s are posted, the one with the highest priority
1090      * is shown first.
1091      *
1092      * Usually, you can simply forget the pointer, as the Message is deleted
1093      * automatically, once it is processed or the document gets closed.
1094      *
1095      * If the Document does not have a View yet, the Message is queued and
1096      * shown, once a View for the Document is created.
1097      *
1098      * @param message the message to show
1099      * @return @e true, if @p message was posted. @e false, if message == 0.
1100      */
1101     virtual bool postMessage(Message *message) = 0;
1102 
1103     //!\}
1104 
1105     /**
1106      * \name Session Configuration
1107      *
1108      * \{
1109      */
1110 public:
1111     /**
1112      * Read session settings from the given \p config.
1113      *
1114      * Known flags:
1115      * - \p SkipUrl => do not save/restore the file
1116      * - \p SkipMode => do not save/restore the mode
1117      * - \p SkipHighlighting => do not save/restore the highlighting
1118      * - \p SkipEncoding => do not save/restore the encoding
1119      *
1120      * \param config read the session settings from this KConfigGroup
1121      * \param flags additional flags
1122      * \see writeSessionConfig()
1123      */
1124     virtual void readSessionConfig(const KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
1125 
1126     /**
1127      * Write session settings to the \p config.
1128      * See readSessionConfig() for more details about available \p flags.
1129      *
1130      * \param config write the session settings to this KConfigGroup
1131      * \param flags additional flags
1132      * \see readSessionConfig()
1133      */
1134     virtual void writeSessionConfig(KConfigGroup &config, const QSet<QString> &flags = QSet<QString>()) = 0;
1135 
1136     //!\}
1137 
1138     /**
1139      * \name Crash Recovery
1140      *
1141      * \{
1142      */
1143 public:
1144     /**
1145      * Returns whether a recovery is available for the current document.
1146      *
1147      * \see recoverData(), discardDataRecovery()
1148      */
1149     virtual bool isDataRecoveryAvailable() const = 0;
1150 
1151     /**
1152      * If recover data is available, calling recoverData() will trigger the
1153      * recovery of the data. If isDataRecoveryAvailable() returns \e false,
1154      * calling this function does nothing.
1155      *
1156      * \see isDataRecoveryAvailable(), discardDataRecovery()
1157      */
1158     virtual void recoverData() = 0;
1159 
1160     /**
1161      * If recover data is available, calling discardDataRecovery() will discard
1162      * the recover data and the recover data is lost.
1163      * If isDataRecoveryAvailable() returns \e false, calling this function
1164      * does nothing.
1165      *
1166      * \see isDataRecoveryAvailable(), recoverData()
1167      */
1168     virtual void discardDataRecovery() = 0;
1169 
1170     //!\}
1171 
1172 Q_SIGNALS:
1173     /**
1174      * This signal is emitted whenever the current document configuration is changed.
1175      *
1176      * \param document the document which's config has changed
1177      *
1178      * \since 5.79
1179      */
1180     void configChanged(KTextEditor::Document *document);
1181 
1182 private:
1183     /**
1184      * private d-pointer, pointing to the internal implementation
1185      */
1186     DocumentPrivate *const d;
1187 };
1188 
1189 }
1190 
1191 Q_DECLARE_METATYPE(KTextEditor::Document *)
1192 
1193 #endif