KTextEditor

katetextbuffer.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Christoph Cullmann <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KATE_TEXTBUFFER_H
22 #define KATE_TEXTBUFFER_H
23 
24 #include <QObject>
25 #include <QSet>
26 #include <QString>
27 #include <QTextCodec>
28 #include <QVector>
29 
30 #include <ktexteditor/document.h>
31 
32 #include "katedocument.h"
33 #include "katetextblock.h"
34 #include "katetextcursor.h"
35 #include "katetexthistory.h"
36 #include "katetextrange.h"
37 #include <ktexteditor_export.h>
38 
39 // encoding prober
40 #include <KEncodingProber>
41 
42 class KCompressionDevice;
43 
44 namespace Kate
45 {
50 class KTEXTEDITOR_EXPORT TextBuffer : public QObject
51 {
52  friend class TextCursor;
53  friend class TextRange;
54  friend class TextBlock;
55 
56  Q_OBJECT
57 
58 public:
62  enum EndOfLineMode { eolUnknown = -1, eolUnix = 0, eolDos = 1, eolMac = 2 };
63 
71  explicit TextBuffer(KTextEditor::DocumentPrivate *parent, int blockSize = 64, bool alwaysUseKAuth = false);
72 
77  virtual ~TextBuffer();
78 
84  virtual void clear();
85 
90  void setEncodingProberType(KEncodingProber::ProberType proberType)
91  {
92  m_encodingProberType = proberType;
93  }
94 
99  KEncodingProber::ProberType encodingProberType() const
100  {
101  return m_encodingProberType;
102  }
103 
109  {
110  m_fallbackTextCodec = codec;
111  }
112 
118  {
119  return m_fallbackTextCodec;
120  }
121 
128  void setTextCodec(QTextCodec *codec);
129 
135  {
136  return m_textCodec;
137  }
138 
144  void setGenerateByteOrderMark(bool generateByteOrderMark)
145  {
146  m_generateByteOrderMark = generateByteOrderMark;
147  }
148 
154  {
155  return m_generateByteOrderMark;
156  }
157 
163  void setEndOfLineMode(EndOfLineMode endOfLineMode)
164  {
165  Q_ASSERT(endOfLineMode != eolUnknown);
166  m_endOfLineMode = endOfLineMode;
167  }
168 
174  {
175  return m_endOfLineMode;
176  }
177 
182  void setNewLineAtEof(bool newlineAtEof)
183  {
184  m_newLineAtEof = newlineAtEof;
185  }
186 
191  void setLineLengthLimit(int lineLengthLimit)
192  {
193  m_lineLengthLimit = lineLengthLimit;
194  }
195 
208  virtual bool load(const QString &filename, bool &encodingErrors, bool &tooLongLinesWrapped, int &longestLineLoaded, bool enforceTextCodec);
209 
217  virtual bool save(const QString &filename);
218 
223  int lines() const
224  {
225  Q_ASSERT(m_lines > 0);
226  return m_lines;
227  }
228 
234  qint64 revision() const
235  {
236  return m_revision;
237  }
238 
244  TextLine line(int line) const;
245 
250  QString text() const;
251 
259  virtual bool startEditing();
260 
266  virtual bool finishEditing();
267 
273  {
274  return m_editingTransactions;
275  }
276 
281  qint64 editingLastRevision() const
282  {
283  return m_editingLastRevision;
284  }
285 
290  int editingLastLines() const
291  {
292  return m_editingLastLines;
293  }
294 
300  bool editingChangedBuffer() const
301  {
302  return editingLastRevision() != revision();
303  }
304 
311  {
312  return editingLastLines() != lines();
313  }
314 
320  {
321  return m_editingMinimalLineChanged;
322  }
323 
329  {
330  return m_editingMaximalLineChanged;
331  }
332 
338  virtual void wrapLine(const KTextEditor::Cursor &position);
339 
345  virtual void unwrapLine(int line);
346 
353  virtual void insertText(const KTextEditor::Cursor &position, const QString &text);
354 
360  virtual void removeText(const KTextEditor::Range &range);
361 
367  {
368  return m_history;
369  }
370 
371 Q_SIGNALS:
376  void cleared();
377 
383  void loaded(const QString &filename, bool encodingErrors);
384 
389  void saved(const QString &filename);
390 
394  void editingStarted();
395 
399  void editingFinished();
400 
405  void lineWrapped(const KTextEditor::Cursor &position);
406 
411  void lineUnwrapped(int line);
412 
418  void textInserted(const KTextEditor::Cursor &position, const QString &text);
419 
425  void textRemoved(const KTextEditor::Range &range, const QString &text);
426 
427 private:
432  enum class SaveResult { Failed = 0, MissingPermissions, Success };
433 
439  int blockForLine(int line) const;
440 
445  void fixStartLines(int startBlock);
446 
451  void balanceBlock(int index);
452 
458  TextBlock *blockForIndex(int index)
459  {
460  return m_blocks[index];
461  }
462 
470  void notifyAboutRangeChange(KTextEditor::View *view, int startLine, int endLine, bool rangeWithAttribute);
471 
475  void markModifiedLinesAsSaved();
476 
483  bool saveBuffer(const QString &filename, KCompressionDevice &saveFile);
484 
489  SaveResult saveBufferUnprivileged(const QString &filename);
490 
495  bool saveBufferEscalated(const QString &filename);
496 
497 public:
502  KTextEditor::DocumentPrivate *document() const
503  {
504  return m_document;
505  }
506 
511  void debugPrint(const QString &title) const;
512 
520  QList<TextRange *> rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly) const;
521 
526  bool rangePointerValid(TextRange *range) const
527  {
528  return m_ranges.contains(range);
529  }
530 
534  void invalidateRanges();
535 
536  //
537  // checksum handling
538  //
539 public:
545  const QByteArray &digest() const;
546 
552  void setDigest(const QByteArray &checksum);
553 
554 private:
555  QByteArray m_digest;
556 
557 private:
561  KTextEditor::DocumentPrivate *m_document;
562 
566  TextHistory m_history;
567 
571  const int m_blockSize;
572 
576  QVector<TextBlock *> m_blocks;
577 
581  int m_lines;
582 
587  mutable int m_lastUsedBlock;
588 
592  qint64 m_revision;
593 
597  int m_editingTransactions;
598 
602  qint64 m_editingLastRevision;
603 
607  int m_editingLastLines;
608 
612  int m_editingMinimalLineChanged;
613 
617  int m_editingMaximalLineChanged;
618 
623  QSet<TextCursor *> m_invalidCursors;
624 
628  QSet<TextRange *> m_ranges;
629 
633  KEncodingProber::ProberType m_encodingProberType;
634 
638  QTextCodec *m_fallbackTextCodec;
639 
643  QTextCodec *m_textCodec;
644 
649  QString m_mimeTypeForFilterDev;
650 
654  bool m_generateByteOrderMark;
655 
659  EndOfLineMode m_endOfLineMode;
660 
664  bool m_newLineAtEof;
665 
669  int m_lineLengthLimit;
670 
674  bool m_alwaysUseKAuthForSave;
675 
679  static const qint64 bufferLength = 4096;
680 };
681 
682 }
683 
684 #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:85
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:155
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:46
qint64 editingLastRevision() const
Query the revision of this buffer before the ongoing editing transactions.
Class representing a text block.
Definition: katetextblock.h:44
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 Sat Jul 4 2020 22:58:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.