buffer

package
v0.0.0-...-2e0bb3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package buffer holds the editable text and cursor for a single file.

The buffer is intentionally simple (a slice of rune-lines) so the interesting parts — command dispatch, keymaps and the out-of-process plugin protocol — stay readable. Every mutating operation bumps Version; plugin edits carry the version they were computed against so the core can reject stale writes (the same document-versioning idea LSP uses).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is one open file's text plus a cursor.

func New

func New(name string) *Buffer

New returns an empty, unnamed buffer.

func Open

func Open(path string) (*Buffer, error)

Open reads path into a new buffer. A missing file yields an empty buffer.

func (*Buffer) AppendLine

func (b *Buffer) AppendLine(s string)

AppendLine appends s (which may contain newlines) as new line(s) at the end of the buffer. Used for the *messages* log.

func (*Buffer) BeginningOfBuffer

func (b *Buffer) BeginningOfBuffer()

BeginningOfBuffer moves point to the very start.

func (*Buffer) BeginningOfLine

func (b *Buffer) BeginningOfLine()

BeginningOfLine moves the cursor to column 0 (C-a).

func (*Buffer) ClearMark

func (b *Buffer) ClearMark()

ClearMark deactivates the region.

func (*Buffer) DeleteBackward

func (b *Buffer) DeleteBackward()

DeleteBackward removes the rune before the cursor (backspace).

func (*Buffer) DeleteForward

func (b *Buffer) DeleteForward()

DeleteForward removes the rune under the cursor (C-d).

func (*Buffer) DeleteRange

func (b *Buffer) DeleteRange(from, to Point) string

DeleteRange removes the text between two (unordered) points, leaves point at the start, and returns what was removed.

func (*Buffer) Dirty

func (b *Buffer) Dirty() bool

Dirty reports whether the buffer has unsaved changes.

func (*Buffer) EOL

func (b *Buffer) EOL() string

EOL returns the buffer's line ending ("\n" or "\r\n").

func (*Buffer) EndOfBuffer

func (b *Buffer) EndOfBuffer()

EndOfBuffer moves point to the very end.

func (*Buffer) EndOfLine

func (b *Buffer) EndOfLine()

EndOfLine moves the cursor to end of the current line (C-e).

func (*Buffer) ExchangePointAndMark

func (b *Buffer) ExchangePointAndMark()

ExchangePointAndMark swaps point and mark (Emacs C-x C-x).

func (*Buffer) Insert

func (b *Buffer) Insert(s string)

Insert writes s at the cursor and advances the cursor past it.

func (*Buffer) KillLine

func (b *Buffer) KillLine() string

KillLine deletes from the cursor to end of line (or joins the next line if already at end), returning the killed text (C-k).

func (*Buffer) Line

func (b *Buffer) Line(i int) []rune

Line returns a copy of line i, or nil if out of range.

func (*Buffer) LineCount

func (b *Buffer) LineCount() int

LineCount returns the number of lines.

func (*Buffer) MarkActive

func (b *Buffer) MarkActive() bool

MarkActive reports whether a region is active.

func (*Buffer) MovePoint

func (b *Buffer) MovePoint(dl, dc int)

MovePoint shifts the cursor by dl lines and dc columns, clamping to bounds.

func (*Buffer) Name

func (b *Buffer) Name() string

Name returns the buffer's display name.

func (*Buffer) OffsetToPoint

func (b *Buffer) OffsetToPoint(off int) Point

OffsetToPoint converts a whole-buffer rune offset back to a point.

func (*Buffer) Path

func (b *Buffer) Path() string

Path returns the file path backing the buffer (may be empty).

func (*Buffer) Point

func (b *Buffer) Point() Point

Point returns the current cursor position.

func (*Buffer) PointToOffset

func (b *Buffer) PointToOffset(p Point) int

PointToOffset returns the rune offset of a point into the whole buffer text.

func (*Buffer) Region

func (b *Buffer) Region() (from, to Point, ok bool)

Region returns the active region as an ordered [from,to] pair; ok is false when no mark is set.

func (*Buffer) Replace

func (b *Buffer) Replace(from, to Point, s string)

Replace replaces the text between two (unordered) points with s, leaving point at the start. It is used to apply LSP text edits.

func (*Buffer) ReplaceRange

func (b *Buffer) ReplaceRange(base int, from, to Point, s string) bool

ReplaceRange is used by plugins: it replaces the text between two points with s, but only if base matches the current version. It returns false on a stale edit so the plugin can re-read and retry.

func (*Buffer) Restore

func (b *Buffer) Restore(s Snapshot)

Restore reinstates a snapshot, bumping the version like any edit.

func (*Buffer) Revert

func (b *Buffer) Revert(path string) error

Revert reloads the buffer from path, keeping point (clamped) and marking the buffer unmodified. Used for auto-revert when the file changed on disk.

func (*Buffer) Save

func (b *Buffer) Save() error

Save writes the buffer to its path and clears the dirty flag.

func (*Buffer) SetEOL

func (b *Buffer) SetEOL(eol string)

SetEOL sets the line ending used on save and marks the buffer modified so the change is written out. The in-memory text stays LF-normalized.

func (*Buffer) SetLine

func (b *Buffer) SetLine(i int, s string)

SetLine replaces the content of line i (s must not contain newlines).

func (*Buffer) SetMark

func (b *Buffer) SetMark()

SetMark plants the mark at point and activates the region.

func (*Buffer) SetPath

func (b *Buffer) SetPath(path string)

SetPath changes the file path (and display name) the buffer saves to.

func (*Buffer) SetPoint

func (b *Buffer) SetPoint(p Point)

SetPoint moves the cursor to an absolute, clamped position.

func (*Buffer) SetText

func (b *Buffer) SetText(s string)

SetText replaces the whole buffer content and moves point to the start. Used for generated/results buffers.

func (*Buffer) Snapshot

func (b *Buffer) Snapshot() Snapshot

Snapshot captures the current text and cursor for later Restore (undo).

func (*Buffer) Text

func (b *Buffer) Text() string

Text returns the whole buffer as a string.

func (*Buffer) TextIfWithin

func (b *Buffer) TextIfWithin(limit int) (string, bool)

TextIfWithin returns the whole buffer text and true when it holds at most limit runes. For a larger buffer it returns ("", false) after a bounded scan (it never builds the string), so callers such as syntax highlighting can skip expensive whole-buffer work on very large files cheaply.

func (*Buffer) TextInRange

func (b *Buffer) TextInRange(from, to Point) string

TextInRange returns the text between two (unordered) points.

func (*Buffer) TrimTrailingWhitespace

func (b *Buffer) TrimTrailingWhitespace(from, to int) int

TrimTrailingWhitespace removes trailing spaces/tabs from each line in the inclusive range [from,to] and returns how many lines changed.

func (*Buffer) Version

func (b *Buffer) Version() int

Version returns the current edit version; it increases on every mutation.

type Point

type Point struct {
	Line int
	Col  int
}

Point is a cursor position: zero-based line and rune-column.

type Snapshot

type Snapshot struct {
	// contains filtered or unexported fields
}

Snapshot is an immutable copy of a buffer's text and cursor, used for undo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL