KTextEditor

katetextbuffer.h
1 /*
2  SPDX-FileCopyrightText: 2010 Christoph Cullmann <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef KATE_TEXTBUFFER_H
8 #define KATE_TEXTBUFFER_H
9 
10 #include <QObject>
11 #include <QSet>
12 #include <QString>
13 #include <QTextCodec>
14 #include <QVector>
15 
16 #include <ktexteditor/document.h>
17 
18 #include "katedocument.h"
19 #include "katetextblock.h"
20 #include "katetextcursor.h"
21 #include "katetexthistory.h"
22 #include "katetextrange.h"
23 #include <ktexteditor_export.h>
24 
25 // encoding prober
26 #include <KEncodingProber>
27 
28 class KCompressionDevice;
29 
30 namespace Kate
31 {
36 class KTEXTEDITOR_EXPORT TextBuffer : public QObject
37 {
38  friend class TextCursor;
39  friend class TextRange;
40  friend class TextBlock;
41 
42  Q_OBJECT
43 
44 public:
48  enum EndOfLineMode { eolUnknown = -1, eolUnix = 0, eolDos = 1, eolMac = 2 };
49 
57  explicit TextBuffer(KTextEditor::DocumentPrivate *parent, int blockSize = 64, bool alwaysUseKAuth = false);
58 
63  virtual ~TextBuffer();
64 
70  virtual void clear();
71 
76  void setEncodingProberType(KEncodingProber::ProberType proberType)
77  {
78  m_encodingProberType = proberType;
79  }
80 
85  KEncodingProber::ProberType encodingProberType() const
86  {
87  return m_encodingProberType;
88  }
89 
95  {
96  m_fallbackTextCodec = codec;
97  }
98 
104  {
105  return m_fallbackTextCodec;
106  }
107 
114  void setTextCodec(QTextCodec *codec);
115 
121  {
122  return m_textCodec;
123  }
124 
130  void setGenerateByteOrderMark(bool generateByteOrderMark)
131  {
132  m_generateByteOrderMark = generateByteOrderMark;
133  }
134 
140  {
141  return m_generateByteOrderMark;
142  }
143 
149  void setEndOfLineMode(EndOfLineMode endOfLineMode)
150  {
151  Q_ASSERT(endOfLineMode != eolUnknown);
152  m_endOfLineMode = endOfLineMode;
153  }
154 
160  {
161  return m_endOfLineMode;
162  }
163 
168  void setNewLineAtEof(bool newlineAtEof)
169  {
170  m_newLineAtEof = newlineAtEof;
171  }
172 
177  void setLineLengthLimit(int lineLengthLimit)
178  {
179  m_lineLengthLimit = lineLengthLimit;
180  }
181 
194  virtual bool load(const QString &filename, bool &encodingErrors, bool &tooLongLinesWrapped, int &longestLineLoaded, bool enforceTextCodec);
195 
203  virtual bool save(const QString &filename);
204 
209  int lines() const
210  {
211  Q_ASSERT(m_lines > 0);
212  return m_lines;
213  }
214 
220  qint64 revision() const
221  {
222  return m_revision;
223  }
224 
230  TextLine line(int line) const;
231 
236  QString text() const;
237 
245  virtual bool startEditing();
246 
252  virtual bool finishEditing();
253 
259  {
260  return m_editingTransactions;
261  }
262 
267  qint64 editingLastRevision() const
268  {
269  return m_editingLastRevision;
270  }
271 
276  int editingLastLines() const
277  {
278  return m_editingLastLines;
279  }
280 
286  bool editingChangedBuffer() const
287  {
288  return editingLastRevision() != revision();
289  }
290 
297  {
298  return editingLastLines() != lines();
299  }
300 
306  {
307  return m_editingMinimalLineChanged;
308  }
309 
315  {
316  return m_editingMaximalLineChanged;
317  }
318 
324  virtual void wrapLine(const KTextEditor::Cursor &position);
325 
331  virtual void unwrapLine(int line);
332 
339  virtual void insertText(const KTextEditor::Cursor &position, const QString &text);
340 
346  virtual void removeText(const KTextEditor::Range &range);
347 
353  {
354  return m_history;
355  }
356 
357 Q_SIGNALS:
362  void cleared();
363 
369  void loaded(const QString &filename, bool encodingErrors);
370 
375  void saved(const QString &filename);
376 
380  void editingStarted();
381 
385  void editingFinished();
386 
391  void lineWrapped(const KTextEditor::Cursor &position);
392 
397  void lineUnwrapped(int line);
398 
404  void textInserted(const KTextEditor::Cursor &position, const QString &text);
405 
411  void textRemoved(const KTextEditor::Range &range, const QString &text);
412 
413 private:
418  enum class SaveResult { Failed = 0, MissingPermissions, Success };
419 
425  int blockForLine(int line) const;
426 
431  void fixStartLines(int startBlock);
432 
437  void balanceBlock(int index);
438 
444  TextBlock *blockForIndex(int index)
445  {
446  return m_blocks[index];
447  }
448 
456  void notifyAboutRangeChange(KTextEditor::View *view, int startLine, int endLine, bool rangeWithAttribute);
457 
461  void markModifiedLinesAsSaved();
462 
469  bool saveBuffer(const QString &filename, KCompressionDevice &saveFile);
470 
475  SaveResult saveBufferUnprivileged(const QString &filename);
476 
481  bool saveBufferEscalated(const QString &filename);
482 
483 public:
488  KTextEditor::DocumentPrivate *document() const
489  {
490  return m_document;
491  }
492 
497  void debugPrint(const QString &title) const;
498 
506  QList<TextRange *> rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly) const;
507 
512  bool rangePointerValid(TextRange *range) const
513  {
514  return m_ranges.contains(range);
515  }
516 
520  void invalidateRanges();
521 
522  //
523  // checksum handling
524  //
525 public:
531  const QByteArray &digest() const;
532 
538  void setDigest(const QByteArray &checksum);
539 
540 private:
541  QByteArray m_digest;
542 
543 private:
547  KTextEditor::DocumentPrivate *m_document;
548 
552  TextHistory m_history;
553 
557  const int m_blockSize;
558 
562  QVector<TextBlock *> m_blocks;
563 
567  int m_lines;
568 
573  mutable int m_lastUsedBlock;
574 
578  qint64 m_revision;
579 
583  int m_editingTransactions;
584 
588  qint64 m_editingLastRevision;
589 
593  int m_editingLastLines;
594 
598  int m_editingMinimalLineChanged;
599 
603  int m_editingMaximalLineChanged;
604 
609  QSet<TextCursor *> m_invalidCursors;
610 
614  QSet<TextRange *> m_ranges;
615 
619  KEncodingProber::ProberType m_encodingProberType;
620 
624  QTextCodec *m_fallbackTextCodec;
625 
629  QTextCodec *m_textCodec;
630 
635  QString m_mimeTypeForFilterDev;
636 
640  bool m_generateByteOrderMark;
641 
645  EndOfLineMode m_endOfLineMode;
646 
650  bool m_newLineAtEof;
651 
655  int m_lineLengthLimit;
656 
660  bool m_alwaysUseKAuthForSave;
661 
665  static const qint64 bufferLength = 4096;
666 };
667 
668 }
669 
670 #endif
void setGenerateByteOrderMark(bool generateByteOrderMark)
Generate byte order mark on save.
bool editingChangedBuffer() const
Query information from the last editing transaction: was the content of the buffer changed...
EndOfLineMode endOfLineMode() const
Get end of line mode.
QTextCodec * textCodec() const
Get codec for this buffer.
KEncodingProber::ProberType encodingProberType() const
Get encoding prober type for this buffer.
bool rangePointerValid(TextRange *range) const
Check if the given range pointer is still valid.
void setFallbackTextCodec(QTextCodec *codec)
Set fallback codec for this buffer to use for load.
EndOfLineMode
End of line mode.
The Cursor represents a position in a Document.
Definition: cursor.h:71
int editingMaximalLineChanged() const
Get maximal line number changed by last editing transaction.
bool editingChangedNumberOfLines() const
Query information from the last editing transaction: was the number of lines of the buffer changed...
void setNewLineAtEof(bool newlineAtEof)
Set whether to insert a newline character on save at the end of the file.
QTextCodec * fallbackTextCodec() const
Get fallback codec for this buffer.
An object representing a section of text, from one Cursor to another.
void setEncodingProberType(KEncodingProber::ProberType proberType)
Set encoding prober type for this buffer to use for load.
int lines() const
Lines currently stored in this buffer.
void setEndOfLineMode(EndOfLineMode endOfLineMode)
Set end of line mode for this buffer, not allowed to be set to unknown.
Class representing a &#39;clever&#39; text cursor.
Class representing the editing history of a TextBuffer.
void setLineLengthLimit(int lineLengthLimit)
Set line length limit.
bool generateByteOrderMark() const
Generate byte order mark on save?
qint64 revision() const
Revision of this buffer.
int editingMinimalLineChanged() const
Get minimal line number changed by last editing transaction.
int editingTransactions() const
Query the number of editing transactions running atm.
Class representing a text buffer.
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:143
KTextEditor::DocumentPrivate * document() const
Gets the document to which this buffer is bound.
TextHistory & history()
TextHistory of this buffer.
Class representing a &#39;clever&#39; text range.
Definition: katetextrange.h:32
qint64 editingLastRevision() const
Query the revision of this buffer before the ongoing editing transactions.
Class representing a text block.
Definition: katetextblock.h:30
int editingLastLines() const
Query the number of lines of this buffer before the ongoing editing transactions. ...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:58:29 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.