command

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package command implements vi/ex-style command parsing and execution.

Command format: :{range}{command}{flags} Supported commands:

:q         Quit
:q!        Force quit (no save)
:w         Save
:wq        Save and quit
:w {path}  Save as
:e {path}  Open file
:10,20d    Delete lines 10-20
:%s/old/new/g    Replace all
:3,5s/old/new    Replace in range
:set nu    Show line numbers (etc.)

Design: Uses a recursive descent parser that walks each token of the command string, builds a Command struct, then executes it via the Executor on the Document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdKind

type CmdKind int

CmdKind is the command type enumeration.

const (
	CmdNone       CmdKind = iota
	CmdDelete             // d - delete lines
	CmdSubstitute         // s - substitute
	CmdWrite              // w - write
	CmdQuit               // q - quit
	CmdWriteQuit          // wq - write and quit
	CmdForceQuit          // q! - force quit
	CmdEdit               // e - edit file
	CmdSet                // set - set option
	CmdGoto               // goto line (e.g. :42)
	CmdUninstall          // uninstall - remove editor and config
)

type Command

type Command struct {
	Kind   CmdKind    // Command type
	Range  Range      // Line range
	Args   []string   // Command arguments
	SubOld string     // s command match pattern
	SubNew string     // s command replacement text
	SubFlg string     // s command flags (g global, i ignore case)
	SetOpt *SetOption // set command option
	Force  bool       // Force flag (!)
}

Command represents a parsed command.

func Parse

func Parse(input string) (*Command, error)

Parse parses a command string (e.g. "10,20d", "%%s/old/new/g", "wq!"), returning a Command. Input should not include the leading colon.

func (*Command) CompileRegex

func (c *Command) CompileRegex() (*regexp.Regexp, error)

CompileRegex compiles the substitution pattern regex (handles ignoreCase flag).

func (*Command) MakeGlobal

func (c *Command) MakeGlobal() bool

MakeGlobal returns whether the substitution is global (g flag).

func (*Command) String

func (c *Command) String() string

String returns a human-readable representation of the command.

type Executor

type Executor struct {
	DeleteRange  func(startLine, endLine int) (string, error)
	GetLineText  func(line int) string
	SetOption    func(name, value string) error
	OpenFile     func(path string) error
	WriteFile    func(path string) error
	Quit         func(force bool) error
	ShowMsg      func(msg string)
	ReplaceText  func(oldPattern, newText string, startLine, endLine int, global, ignoreCase bool) (int, error)
	GetLineCount func() int
	GotoLine     func(line int) error
	Uninstall    func() error
}

Executor executes commands on the document. To avoid circular dependency (command depending on document), Executor uses callback functions for operations.

func (*Executor) Execute

func (ex *Executor) Execute(cmd *Command) (shouldQuit bool, err error)

Execute executes the parsed command, returning whether the editor should quit.

type ParseError

type ParseError struct {
	Msg string
}

ParseError represents a command parsing error.

func (*ParseError) Error

func (e *ParseError) Error() string

type Range

type Range struct {
	Start int  // Start line (0-indexed), -1 means unspecified
	End   int  // End line (0-indexed), -1 means unspecified
	All   bool // Full document (%)
}

Range represents a line addressing range.

func (Range) ExpandRange

func (r Range) ExpandRange(totalLines int) (int, int)

ExpandRange expands a range from 0-indexed lines to actual start/end positions. The totalLines parameter is the total number of lines in the document.

type SetOption

type SetOption struct {
	Name  string // Option name
	Value string // Option value (empty for boolean toggle)
}

SetOption stores a set command option.

Jump to

Keyboard shortcuts

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