Documentation
¶
Overview ¶
Package vim is a Vim editing engine over a plain text buffer: modes, counts, registers, motions, operators, text objects, visual selections, undo, dot-repeat, macros, marks, search and substitute, an ex command line, and heading folds. It knows nothing about terminals; the terminal UI feeds it keys and draws what it exposes.
Index ¶
- Variables
- func KeysString(keys []Key) string
- type Buffer
- func (b *Buffer) DeleteLines(from, to int) []string
- func (b *Buffer) DeleteRange(start, end Pos) string
- func (b *Buffer) InsertLines(at int, lines []string)
- func (b *Buffer) InsertText(p Pos, text string) Pos
- func (b *Buffer) Line(i int) []rune
- func (b *Buffer) LineCount() int
- func (b *Buffer) LineLen(i int) int
- func (b *Buffer) LineString(i int) string
- func (b *Buffer) Lines() []string
- func (b *Buffer) Range(start, end Pos) string
- func (b *Buffer) ReplaceLine(i int, text []rune)
- func (b *Buffer) ReplaceLines(from, to int, lines []string)
- func (b *Buffer) Restore(lines [][]rune)
- func (b *Buffer) SetText(text string)
- func (b *Buffer) Snapshot() [][]rune
- func (b *Buffer) Text() string
- type Editor
- func (e *Editor) BlockColumns() (left, right int, toEnd bool)
- func (e *Editor) ClearMessage()
- func (e *Editor) Cmdline() (kind rune, text string, cursor int, active bool)
- func (e *Editor) Completions() ([]string, int)
- func (e *Editor) Cursor() Pos
- func (e *Editor) Dirty() bool
- func (e *Editor) EnsureCursorVisible()
- func (e *Editor) ExecuteEx(line string)
- func (e *Editor) Feed(notation string)
- func (e *Editor) Folds() map[int]int
- func (e *Editor) GotoLine(line int)
- func (e *Editor) HandleKey(k Key) bool
- func (e *Editor) Hidden(line int) bool
- func (e *Editor) HighlightOn() bool
- func (e *Editor) IncrementalHighlights(line int) [][2]int
- func (e *Editor) InsertAtCursor(text string)
- func (e *Editor) IsFoldStart(line int) (int, bool)
- func (e *Editor) LastSearch() string
- func (e *Editor) Line(i int) string
- func (e *Editor) LineCount() int
- func (e *Editor) LineRunes(i int) []rune
- func (e *Editor) Lines() []string
- func (e *Editor) MarkSaved()
- func (e *Editor) Marks() map[rune]Pos
- func (e *Editor) Message() (string, bool)
- func (e *Editor) Mode() Mode
- func (e *Editor) OpenCmdline(kind rune, initial string)
- func (e *Editor) Options() Options
- func (e *Editor) Pending() string
- func (e *Editor) PendingCount() (int, bool)
- func (e *Editor) Recording() rune
- func (e *Editor) Redo()
- func (e *Editor) ReflowParagraph()
- func (e *Editor) RegisterText(r rune) (Register, bool)
- func (e *Editor) ReplaceAtCursor(before, after int, text string)
- func (e *Editor) ReplaceLineText(line int, text string)
- func (e *Editor) ReplaceLines(from, to int, lines []string)
- func (e *Editor) ReplaceText(text string)
- func (e *Editor) ResetPending()
- func (e *Editor) Rows() int
- func (e *Editor) ScrollTop() int
- func (e *Editor) SearchHighlights(line int) [][2]int
- func (e *Editor) SetCursor(p Pos)
- func (e *Editor) SetHooks(h Hooks)
- func (e *Editor) SetOptions(opts Options)
- func (e *Editor) SetScrollTop(line int)
- func (e *Editor) SetText(text string)
- func (e *Editor) SetViewport(rows int)
- func (e *Editor) StatusSuffix() string
- func (e *Editor) Text() string
- func (e *Editor) ToggleCheckboxAtCursor()
- func (e *Editor) Undo()
- func (e *Editor) Version() int
- func (e *Editor) VisualRange() (start, end Pos, mode Mode, ok bool)
- func (e *Editor) WordUnderCursor() string
- type ExCommand
- type Hooks
- type Key
- type Mode
- type Options
- type Pos
- type Range
- type Register
Constants ¶
This section is empty.
Variables ¶
var ( KeyEsc = Special("esc") KeyEnter = Special("enter") KeyTab = Special("tab") KeyShiftTab = Key{Name: "tab", Shift: true} KeyBackspace = Special("backspace") KeyDelete = Special("delete") KeyUp = Special("up") KeyDown = Special("down") KeyLeft = Special("left") KeyRight = Special("right") KeyHome = Special("home") KeyEnd = Special("end") KeyPgUp = Special("pgup") KeyPgDn = Special("pgdn") )
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is the text: a slice of lines without their newlines. Line slices are treated as immutable, so an undo snapshot is just a copy of the outer slice.
func (*Buffer) DeleteLines ¶
DeleteLines removes lines from..to inclusive and returns them. The buffer always keeps one line.
func (*Buffer) DeleteRange ¶
DeleteRange removes the text between start (inclusive) and end (exclusive) and returns it.
func (*Buffer) InsertLines ¶
InsertLines inserts lines before index at (at may equal LineCount).
func (*Buffer) InsertText ¶
InsertText inserts text (which may contain newlines) at p and returns the position just after it.
func (*Buffer) LineString ¶
LineString is a line as a string.
func (*Buffer) ReplaceLine ¶
ReplaceLine swaps a line's content.
func (*Buffer) ReplaceLines ¶
ReplaceLines swaps lines from..to (inclusive) for new ones. Replacing every line replaces the buffer's mandatory single line too, so no empty line lingers.
type Editor ¶
type Editor struct {
// contains filtered or unexported fields
}
Editor is one buffer with its full Vim state.
func (*Editor) BlockColumns ¶
BlockColumns is the column span of a block selection, plus whether `$` extends every line to its end.
func (*Editor) ClearMessage ¶
func (e *Editor) ClearMessage()
ClearMessage drops the status message.
func (*Editor) Completions ¶
Completions lists the current completion candidates (for a wildmenu).
func (*Editor) EnsureCursorVisible ¶
func (e *Editor) EnsureCursorVisible()
EnsureCursorVisible scrolls so the cursor is on screen, honoring scrolloff.
func (*Editor) HandleKey ¶
HandleKey processes one keystroke. It reports false only in plain (non Vim) mode for keys the editor has no use for, so the host may act.
func (*Editor) HighlightOn ¶
HighlightOn reports whether search matches are shown.
func (*Editor) IncrementalHighlights ¶
IncrementalHighlights lists spans of the pattern being typed on a line.
func (*Editor) InsertAtCursor ¶
InsertAtCursor inserts text as one undoable change and leaves the cursor after it.
func (*Editor) IsFoldStart ¶
IsFoldStart reports whether line starts a closed fold, and where it ends.
func (*Editor) MarkSaved ¶
func (e *Editor) MarkSaved()
MarkSaved records the current version as the on-disk one.
func (*Editor) OpenCmdline ¶
OpenCmdline starts an ex or search line from the host.
func (*Editor) PendingCount ¶
PendingCount is the count typed so far when the pending keys are only digits: the host reads it before taking over a sequence like `3gt`.
func (*Editor) ReflowParagraph ¶
func (e *Editor) ReflowParagraph()
ReflowParagraph joins the paragraph under the cursor (Alt+Q).
func (*Editor) RegisterText ¶
RegisterText reads a register.
func (*Editor) ReplaceAtCursor ¶ added in v0.3.0
ReplaceAtCursor swaps the before runes left of the cursor and the after runes right of it for text and leaves the cursor behind the text: what accepting a completion does. In insert mode it joins the insert in progress, so one undo takes the typing and the completion together.
func (*Editor) ReplaceLineText ¶
ReplaceLineText swaps one line as a change.
func (*Editor) ReplaceLines ¶
ReplaceLines swaps a block of lines as a change.
func (*Editor) ReplaceText ¶
ReplaceText swaps the text in as a change (undoable) keeping the cursor where it was where possible, for a note reloaded from disk.
func (*Editor) ResetPending ¶
func (e *Editor) ResetPending()
ResetPending drops an unfinished normal-mode command, for a host that takes a key sequence over from the engine.
func (*Editor) SearchHighlights ¶
SearchHighlights lists highlighted spans on a line while search highlighting is on.
func (*Editor) SetOptions ¶
SetOptions replaces the options live.
func (*Editor) SetScrollTop ¶
SetScrollTop moves the viewport.
func (*Editor) SetViewport ¶
SetViewport tells the editor how many screen rows it has.
func (*Editor) StatusSuffix ¶
StatusSuffix is the text a status line shows after the mode: pending keys and the macro being recorded.
func (*Editor) ToggleCheckboxAtCursor ¶
func (e *Editor) ToggleCheckboxAtCursor()
ToggleCheckboxAtCursor turns the current line into a checkbox and toggles it on repeat: plain text becomes `- [ ] text`, a bullet keeps its marker, `[ ]` flips to `[x]` and back, `[/]` checks off, `[>]` and `[-]` stay.
func (*Editor) VisualRange ¶
VisualRange is the normalized selection: start..end inclusive, the mode, and ok when a selection is active. For block mode the columns come from BlockColumns.
func (*Editor) WordUnderCursor ¶
WordUnderCursor is the keyword at or after the cursor on its line.
type Hooks ¶
type Hooks struct {
// ExCommand runs a command the editor does not own; handled false means
// unknown command.
ExCommand func(cmd ExCommand) (handled bool, err error)
// ExComplete proposes completions for the ex line. It receives the text
// before the cursor and returns the part to keep plus the candidates
// that replace the rest, so a host can complete note titles with spaces
// as one argument. Nil candidates fall back to the built-in command
// names.
ExComplete func(text string) (keep string, candidates []string)
ReadClipboard func() (string, bool)
WriteClipboard func(text string) bool
// OnJump is told before a jump motion moves the cursor.
OnJump func(from Pos)
// FollowLink is `gd`, CopyLink is `gy`, OpenURL is `gx`.
FollowLink func()
CopyLink func()
OpenURL func()
// LineRows is how many screen rows a line takes when soft-wrapped.
LineRows func(line int) int
// Changed is called after every committed change.
Changed func()
}
Hooks are the host's side of the engine. Every field is optional.
type Key ¶
Key is one keystroke, normalized. Printable keys carry a Rune; special keys carry a Name (esc, enter, tab, backspace, delete, up, down, left, right, home, end, pgup, pgdn, insert).
func ParseKeys ¶
ParseKeys turns Vim notation into keys: `dw<Esc>ihello<CR>`, `<C-d>`. An unterminated `<` is a literal less-than.
func (Key) Is ¶
Is is true for a special key by name (shift-insensitive unless the name is "shift+tab").
type Options ¶
type Options struct {
TabSize int
ScrollOff int
IgnoreCase bool
SmartCase bool
WrapScan bool
InsertEscape string
AutoPairs bool
AutoPairQuotes bool
TextReplacements map[string]string
TextReplacementsEnabled bool
MarkdownLists bool
YankToClipboard bool
// VimEnabled false runs the buffer as a plain editor: always inserting,
// arrows and the usual editing keys only.
VimEnabled bool
}
Options tune the engine. Zero values are filled by New.
type Pos ¶
type Pos struct {
Line, Col int
}
Pos is a position in the buffer: a zero-based line and a rune column.