Minga.API (Minga v0.1.0)

Copy Markdown View Source

Public API for interacting with the editor from eval (M-:).

Provides user-friendly functions for common editor operations. All functions default to the running MingaEditor GenServer and the active buffer. Pass an explicit editor PID to target a different instance.

Usage from eval

# M-: Minga.API.insert("hello")
# M-: Minga.API.save()
# M-: Minga.API.cursor()
# M-: Minga.API.open("lib/minga.ex")

Error handling

Functions return {:ok, result} or {:error, reason} where possible. They never raise on bad input.

Summary

Types

Editor GenServer reference.

Functions

Returns the full content of the active buffer.

Returns the current cursor position as {line, col} (0-indexed).

Executes an editor command by name.

Folds all available ranges in the active window.

Toggles the fold at the cursor line in the active window.

Inserts text at the current cursor position in the active buffer.

Returns the number of lines in the active buffer.

Logs a message to the *Messages* buffer.

Returns the current editor mode (e.g. :normal, :insert, :visual).

Moves the cursor to the given {line, col} position (0-indexed).

Opens a file in the editor. If the file is already open, switches to it.

Saves the active buffer to disk.

Sets the available fold ranges for the active window.

Unfolds all folds in the active window.

Types

editor()

@type editor() :: GenServer.server()

Editor GenServer reference.

Functions

content(editor \\ MingaEditor)

@spec content(editor()) :: {:ok, String.t()} | {:error, :no_buffer}

Returns the full content of the active buffer.

Examples

{:ok, text} = Minga.API.content()

cursor(editor \\ MingaEditor)

@spec cursor(editor()) ::
  {:ok, {non_neg_integer(), non_neg_integer()}} | {:error, :no_buffer}

Returns the current cursor position as {line, col} (0-indexed).

Examples

{:ok, {line, col}} = Minga.API.cursor()

execute(command, editor \\ MingaEditor)

@spec execute(Minga.Mode.command(), editor()) :: :ok

Executes an editor command by name.

Commands are the same atoms used internally (e.g. :save, :undo, :move_down, :buffer_next). See Minga.Command.Registry for the full list.

Examples

Minga.API.execute(:undo)
Minga.API.execute(:buffer_next)
Minga.API.execute({:goto_line, 42})

fold_all(editor \\ MingaEditor)

@spec fold_all(editor()) :: :ok

Folds all available ranges in the active window.

fold_toggle(editor \\ MingaEditor)

@spec fold_toggle(editor()) :: :ok

Toggles the fold at the cursor line in the active window.

insert(text, editor \\ MingaEditor)

@spec insert(String.t(), editor()) :: :ok | {:error, :no_buffer}

Inserts text at the current cursor position in the active buffer.

Examples

Minga.API.insert("hello world")
Minga.API.insert("\n")  # insert a newline

line_count(editor \\ MingaEditor)

@spec line_count(editor()) :: {:ok, non_neg_integer()} | {:error, :no_buffer}

Returns the number of lines in the active buffer.

Examples

{:ok, count} = Minga.API.line_count()

message(text, editor \\ MingaEditor)

@spec message(String.t(), editor()) :: :ok

Logs a message to the *Messages* buffer.

Examples

Minga.API.message("Build completed!")

mode(editor \\ MingaEditor)

@spec mode(editor()) :: Minga.Mode.mode()

Returns the current editor mode (e.g. :normal, :insert, :visual).

Examples

:normal = Minga.API.mode()

move_to(line, col, editor \\ MingaEditor)

@spec move_to(non_neg_integer(), non_neg_integer(), editor()) ::
  :ok | {:error, :no_buffer}

Moves the cursor to the given {line, col} position (0-indexed).

Examples

Minga.API.move_to(0, 0)   # go to start of file
Minga.API.move_to(10, 5)  # line 11, column 6

open(file_path, editor \\ MingaEditor)

@spec open(String.t(), editor()) :: :ok | {:error, term()}

Opens a file in the editor. If the file is already open, switches to it.

Examples

:ok = Minga.API.open("lib/minga.ex")

save(editor \\ MingaEditor)

@spec save(editor()) :: :ok | {:error, term()}

Saves the active buffer to disk.

Examples

:ok = Minga.API.save()

set_fold_ranges(ranges, editor \\ MingaEditor)

@spec set_fold_ranges([Minga.Editing.Fold.Range.t()], editor()) :: :ok

Sets the available fold ranges for the active window.

Extensions call this to provide fold ranges computed from their own logic (e.g., org-mode heading ranges). The editor will preserve any existing folds that match the new ranges.

unfold_all(editor \\ MingaEditor)

@spec unfold_all(editor()) :: :ok

Unfolds all folds in the active window.