Skip to main content

Overview

The ZepBuffer class represents a text document in Zep. It uses a gap buffer data structure for efficient text insertion and deletion operations. Each buffer can have syntax highlighting, themes, selections, range markers, and an associated editing mode.

Constructor

ZepBuffer

ZepBuffer(ZepEditor& editor, const std::string& strName);
ZepBuffer(ZepEditor& editor, const fs::path& path);
editor
ZepEditor&
required
Reference to the parent editor
strName
const std::string&
Name for a new empty buffer
path
const fs::path&
File path to load into the buffer
Create a buffer either with a name for empty content or from a file path.

File Operations

Load

void Load(const fs::path& path);
path
const fs::path&
required
Path to file to load
Load file content into the buffer.

Save

bool Save(int64_t& size);
size
int64_t&
required
Output parameter receiving the number of bytes saved
Save buffer content to its file path. Returns true on success.

GetFilePath / SetFilePath

fs::path GetFilePath() const;
void SetFilePath(const fs::path& path);
path
const fs::path&
required
File path to set
Get or set the file path associated with this buffer.

GetFileExtension

std::string GetFileExtension() const;
Returns the file extension (e.g., “cpp”, “h”, “txt”).

Text Content

SetText

void SetText(const std::string& strText, bool initFromFile = false);
strText
const std::string&
required
Text content to set
initFromFile
bool
True if this text was loaded from a file (default: false)
Replace the entire buffer content with new text.

GetBufferText

std::string GetBufferText(const GlyphIterator& start, const GlyphIterator& end) const;
start
const GlyphIterator&
required
Start position
end
const GlyphIterator&
required
End position
Extract text between two buffer positions.

Clear

void Clear();
Clear all buffer content.

GetWorkingBuffer

const GapBuffer<uint8_t>& GetWorkingBuffer() const;
GapBuffer<uint8_t>& GetMutableWorkingBuffer();
Access the underlying gap buffer data structure.

Text Modification

Insert

bool Insert(const GlyphIterator& startOffset, const std::string& str, ChangeRecord& changeRecord);
startOffset
const GlyphIterator&
required
Position to insert at
str
const std::string&
required
Text to insert
changeRecord
ChangeRecord&
required
Record of the change for undo/redo
Insert text at the specified position. Returns true on success.

Delete

bool Delete(const GlyphIterator& startOffset, const GlyphIterator& endOffset, ChangeRecord& changeRecord);
startOffset
const GlyphIterator&
required
Start of range to delete
endOffset
const GlyphIterator&
required
End of range to delete
changeRecord
ChangeRecord&
required
Record of the change for undo/redo
Delete text in the specified range. Returns true on success.

Replace

bool Replace(const GlyphIterator& startOffset, const GlyphIterator& endOffset, 
             std::string str, ReplaceRangeMode mode, ChangeRecord& changeRecord);
startOffset
const GlyphIterator&
required
Start of range to replace
endOffset
const GlyphIterator&
required
End of range to replace
str
std::string
required
Replacement text
mode
ReplaceRangeMode
required
Replace mode: Fill or Replace
changeRecord
ChangeRecord&
required
Record of the change for undo/redo
Replace text in a range. Returns true on success.

Line Operations

GetLineCount

long GetLineCount() const;
Returns the total number of lines in the buffer.

GetBufferLine

long GetBufferLine(GlyphIterator offset) const;
offset
GlyphIterator
required
Buffer position
Get the line number for a buffer position.

GetLineOffsets

bool GetLineOffsets(const long line, ByteRange& range) const;
line
long
required
Line number
range
ByteRange&
required
Output parameter receiving start and end byte offsets
Get the byte range for a line. Returns true if line exists.

GetLinePos

GlyphIterator GetLinePos(GlyphIterator bufferLocation, LineLocation lineLocation) const;
bufferLocation
GlyphIterator
required
Position in buffer
lineLocation
LineLocation
required
Desired location on line (LineBegin, LineEnd, etc.)
Get position on a line relative to buffer location.

GetBufferColumn

long GetBufferColumn(GlyphIterator location) const;
location
GlyphIterator
required
Buffer position
Get the column number for a buffer position.

GetLineEnds

const std::vector<ByteIndex> GetLineEnds() const;
Get all line ending positions in the buffer.

Begin / End

GlyphIterator Begin() const;
GlyphIterator End() const;
Get iterators to the beginning or end of the buffer.

Move

bool Move(GlyphIterator& loc, Direction dir) const;
loc
GlyphIterator&
required
Position to move from (updated with new position)
dir
Direction
required
Forward or Backward
Move one character in the specified direction. Returns false if at boundary.

Find

GlyphIterator Find(GlyphIterator start, const uint8_t* pBegin, const uint8_t* pEnd) const;
start
GlyphIterator
required
Position to start search
pBegin
const uint8_t*
required
Pointer to start of search string
pEnd
const uint8_t*
required
Pointer to end of search string
Find text in buffer starting from position. Returns position or invalid iterator.

WordMotion

GlyphIterator WordMotion(GlyphIterator start, uint32_t searchType, Direction dir) const;
start
GlyphIterator
required
Starting position
searchType
uint32_t
required
Search flags from SearchType enum (WORD, Begin, End, Word, SingleLine)
dir
Direction
required
Forward or Backward
Move to the start of the next/previous word.

EndWordMotion

GlyphIterator EndWordMotion(GlyphIterator start, uint32_t searchType, Direction dir) const;
start
GlyphIterator
required
Starting position
searchType
uint32_t
required
Search type flags
dir
Direction
required
Direction to search
Move to the end of the next/previous word.

FindMatchingParen

GlyphIterator FindMatchingParen(GlyphIterator bufferCursor);
bufferCursor
GlyphIterator
required
Position of opening/closing parenthesis
Find the matching parenthesis, bracket, or brace.

Selections

SetSelection

void SetSelection(const GlyphRange& sel);
sel
const GlyphRange&
required
Range to select
Set the current selection range.

GetInclusiveSelection

GlyphRange GetInclusiveSelection() const;
Get the current selection as an inclusive range.

HasSelection

bool HasSelection() const;
Check if there is an active selection.

ClearSelection

void ClearSelection();
Clear the current selection.

Range Markers

AddRangeMarker

void AddRangeMarker(std::shared_ptr<RangeMarker> spMarker);
spMarker
std::shared_ptr<RangeMarker>
required
Marker to add
Add a range marker (for errors, warnings, search results, etc.).

ClearRangeMarker

void ClearRangeMarker(std::shared_ptr<RangeMarker> spMarker);
void ClearRangeMarkers(const std::set<std::shared_ptr<RangeMarker>>& markers);
void ClearRangeMarkers(uint32_t types);
spMarker
std::shared_ptr<RangeMarker>
Specific marker to remove
markers
const std::set<std::shared_ptr<RangeMarker>>&
Set of markers to remove
types
uint32_t
Marker type flags to remove
Remove range markers from the buffer.

GetRangeMarkers

tRangeMarkers GetRangeMarkers(uint32_t types) const;
tRangeMarkers GetRangeMarkersOnLine(uint32_t types, long line) const;
types
uint32_t
required
Marker type flags to retrieve
line
long
Specific line number
Get markers of specified types, optionally filtered by line.

ShowMarkers / HideMarkers

void ShowMarkers(uint32_t markerType, uint32_t displayType);
void HideMarkers(uint32_t markerType);
markerType
uint32_t
required
Type of markers to show/hide
displayType
uint32_t
How to display the markers
Show or hide markers of a specific type.

Syntax and Theme

SetSyntax / GetSyntax

void SetSyntax(std::shared_ptr<ZepSyntax> syntax);
ZepSyntax* GetSyntax() const;
syntax
std::shared_ptr<ZepSyntax>
required
Syntax highlighter to use
Set or get the syntax highlighter for this buffer.

SetSyntaxProvider

void SetSyntaxProvider(SyntaxProvider provider);
provider
SyntaxProvider
required
Syntax provider with factory function
Set the syntax provider which creates the appropriate syntax highlighter.

SetTheme / GetTheme

void SetTheme(std::shared_ptr<ZepTheme> spTheme);
ZepTheme& GetTheme() const;
spTheme
std::shared_ptr<ZepTheme>
required
Theme to apply
Set or get the theme for this buffer.

SetToneColor / GetToneColor

void SetToneColor(const NVec4f& toneColor);
const NVec4f& GetToneColor() const;
toneColor
const NVec4f&
required
RGBA tone color
Set or get an overlay tone color for the buffer.

Mode Management

SetMode / GetMode

void SetMode(std::shared_ptr<ZepMode> spMode);
ZepMode* GetMode() const;
spMode
std::shared_ptr<ZepMode>
required
Editor mode (Vim, Standard, etc.)
Set or get the editing mode for this buffer.

Buffer Properties

GetName

const std::string& GetName() const;
Get the buffer name.

GetDisplayName

std::string GetDisplayName() const;
Get the display name (may be shortened or formatted).

SetBufferType / GetBufferType

void SetBufferType(BufferType type);
BufferType GetBufferType() const;
type
BufferType
required
Buffer type: Normal, Search, Repl, DataGrid, or Tree
Set or get the buffer type.

HasFileFlags / SetFileFlags / ClearFileFlags

bool HasFileFlags(uint32_t flags) const;
void SetFileFlags(uint32_t flags, bool set = true);
void ClearFileFlags(uint32_t flags);
void ToggleFileFlag(uint32_t flags);
flags
uint32_t
required
Flags from FileFlags enum:
  • StrippedCR: Carriage returns were removed
  • ReadOnly: Buffer is read-only
  • Locked: File path cannot be written to
  • Dirty: Buffer has unsaved changes
  • HasWarnings: Buffer has warnings
  • HasErrors: Buffer has errors
  • HasTabs: Buffer contains tab characters
  • InsertTabs: Insert tabs instead of spaces
Manage file flags for the buffer.

IsHidden

bool IsHidden() const;
Check if the buffer is hidden.

Undo/Redo

GetUndoStack / GetRedoStack

std::stack<std::shared_ptr<ZepCommand>>& GetUndoStack();
std::stack<std::shared_ptr<ZepCommand>>& GetRedoStack();
Access the undo and redo command stacks.

Update Tracking

GetLastUpdateTime

uint64_t GetLastUpdateTime() const;
Get timestamp of last buffer modification.

GetUpdateCount

uint64_t GetUpdateCount() const;
Get the number of times the buffer has been updated.

Signals

sigPreInsert

Zep::signal<void(ZepBuffer& buffer, const GlyphIterator&, const std::string&)> sigPreInsert;
Signal emitted before text insertion.

sigPreDelete

Zep::signal<void(ZepBuffer& buffer, const GlyphIterator&, const GlyphIterator&)> sigPreDelete;
Signal emitted before text deletion.

Usage Example

#include <zep/buffer.h>
#include <zep/editor.h>

// Create buffer from file
ZepBuffer buffer(editor, "/path/to/file.cpp");

// Set syntax highlighting
auto spSyntax = std::make_shared<ZepSyntax_CPP>(buffer);
buffer.SetSyntax(spSyntax);

// Insert text
ChangeRecord change;
GlyphIterator pos = buffer.Begin();
buffer.Insert(pos, "// New comment\n", change);

// Create a selection
GlyphIterator start = buffer.Begin();
GlyphIterator end = buffer.End();
buffer.SetSelection(GlyphRange(start, end));

// Add error marker
auto marker = std::make_shared<RangeMarker>();
marker->range = GlyphRange(errorStart, errorEnd);
marker->markerType = RangeMarkerType::Error;
buffer.AddRangeMarker(marker);

// Save buffer
int64_t size;
buffer.Save(size);

Build docs developers (and LLMs) love