ral — plugins

ral plugins

A plugin extends an interactive ral session. It is an ordinary ral file ending in a written manifest: a record naming the hooks, keybindings, and handlers it contributes, or a written factory that ends in that record. The rc's plugins: map loads them at startup; they remain installed for the session. A new plugin requires a new session.

This document is the design narrative. The SPEC is the reference: §15.5 (manifest, loading), §15.6 (hooks, key notation, dispatch), §15.7 (the _ed-* builtins), §12.8 (the editor and shell grants). Where a rule is exact, this document cites it rather than restating it. Section numbers below without the word SPEC are this document's own.

1 What a plugin is

A plugin is data, not a program that registers things. The file writes out its manifest's keys and computes their values; the host reads the manifest and does the registering. Four consequences follow.

  • No plugin language. There is no register-hook call, no plugin form, no magic config binding. A plugin is written in ral and checked as ral is checked. Its written manifest is checked key by key during generation (SPEC §15.5, under the host-file rule in §15.3), so a misspelled key is a compile error and the file never executes. Quoted constant keys are names too: ["name": 'example'] is a record checked against the manifest table, not a map. [:, name: 'example'] is a map and cannot stand for a manifest.
  • Load is atomic. The manifest is validated whole, and registration is all or nothing: a rejected load leaves no hooks, keybindings, or handlers behind.
  • Registration is static. A plugin cannot add a hook at run time. What it contributes is what its manifest says at startup.
  • No native ABI. A plugin is ral source and nothing else: no shared object, no bytecode module, no guest interface. The interface it programs against is the set of hook events and _ed-* builtins below, all of them ral values and ral builtins.

The file ends in a manifest record written out, or a written factory: a block of exactly one parameter, the options map, whose body ends in that record. Top-level bindings may supply helpers and field values, but return $manifest, spread manifests, and computed factories are refused (SPEC §15.3).

{ |options| [
    name: fzf-files,
    keybindings: [[key: !{get $options key ctrl-t}, handler: { |_| … }]],
] }

A plugin with nothing to configure returns the manifest directly. A factory chooses its own defaults by reading fields by name; there is no options schema, and options may hold blocks (SPEC §15.5).

Field Meaning
name required; unique among loaded plugins
hooks [event: handler, …] (section 4)
keybindings [[key: Str, handler: block, guard?: Str], …] (section 5)
handlers [:, name: handler, …], or [:] for none (SPEC §9.4)

The keyset is closed. In particular capabilities: is not an unknown key but a refused one, with its own message, for the reason in section 2.

Manifest fields are ordinary uses during inference. For example, name: can fix a decoded value's type to text, so incompatible data is refused at its decode, naming the field use.

handlers: is a map, as in the rc. A written map is checked arm by arm, each in its own region, with duplicate names refused. A bound or computed map is checked against Map(U([String] → Command)). Its keys are data, not table keys. A record such as [hi: …] is refused; write [:, hi: { |args| … }], or [:] for none. Checks reach sites inside locally checked handlers. A module's exports have decided schemes, so an imported handler is held to the same command type statically. Persistent handlers are also vetted by actual name at installation. Hooks and keybindings retain host validation; their results do not enter typed code unchecked.

2 Authority

A plugin has no authority of its own to declare. A manifest field naming capabilities would be decoration: nothing could enforce it against the plugin's own code, and a reader would take it for confinement. So the key is refused before the file runs, and the advice is the message: wrap the invocation in grant.

What a plugin may do is what the code that runs it may do.

  • Hooks and keybinding handlers run in the session, under the session's grant stack: the root layer, narrowed by any --capabilities profile the session started under (SPEC §15.3). They fire from the read-eval loop, not from a user line, so no lexical grant encloses them. A plain ral withholds nothing; a session under a profile narrows every handler exactly as it narrows everything else. A handler may itself wrap risky work in grant.
  • Command handlers run as commands in a user's line and push no grant of their own, so they run under whatever grant encloses the line that names them. A handler that calls cd inside grant [shell: [detach]] { … }is refused like any othercd`.
  • Loading reads the one named plugin file through the current grant. A denied read refuses that file; no alternative is tried. Plugins named in the rc are loaded during bootstrap, before the command-line ceiling narrows the session (SPEC §15.3): the rc is trusted.

Two grant fields exist for plugin machinery (SPEC §12.8). editor lists verbs gating the _ed-* builtins, shell lists verbs gating cd and detach:

Verb Gates
editor: `read _ed-get, _ed-parse, _ed-history, and the read half of _ed-state
editor: `write _ed-set, _ed-set-lbuffer, _ed-insert, _ed-push, _ed-accept, _ed-ghost, _ed-highlight, _ed-clipboard, and the write half of _ed-state
editor: `tui _ed-tui
shell: `chdir cd

A builtin that needs two verbs checks both before its first effect. _ed-state checks write before its updater runs, so a refused call has done nothing. _ed-hyperlink is a pure formatter and needs no verb.

A plugin that needs cd from a handler cannot grant it to itself. It documents that the session needs shell: [`chdir], and lives under whatever the operator allows.

3 Architecture: split at the protocol

The ral executable is a host over an engine, and a plugin straddles the two. The split is chosen so that the engine keeps no editor state and the host keeps no ral values.

  • Engine side. Hooks, keybinding handlers, and command handlers are ral values and live in the shell: the hook table and persistent handler frames, under a per-plugin namespace so that a failed load can remove exactly what was added. Handlers never cross to the host.
  • Host side. The editor state, the keybinding router, the per-plugin state cells, and the first-order part of each manifest (name, which events, the key notations and guards) live in the REPL's plugin runtime. Only data crosses.
  • Between them. The host drives a handler by dispatching a hook run to the engine. A handler reaches the editor by calling an _ed-* builtin, which puts the request to the host as an enquiry.

The consequence for plugin authors is a single rule: the _ed-* builtins work only inside a handler that carries an editor context. That is a buffer-change hook, a complete hook, or a keybinding handler. Lifecycle hooks (pre-exec, post-exec, chpwd) and prompt carry none, and an _ed-* call there, or at an ordinary prompt, or in a batch host, is refused with a message saying so.

4 Loading

The host loads plugins from the rc's plugins: map during startup. Each key names exactly one file (SPEC §15.5):

  • a key without a path separator, NAME, names <config>/ral/plugins/NAME.ral, <config> being the user configuration directory;
  • any other key is a written path, resolved exactly as use resolves one (SPEC §10.3), including ~ and xdg: heads.

There is no search list, literal-path fallback, or implicit .ral added to written paths.

The file is checked against Σ (natives and the prelude) and evaluated under Σ, like a module, with its written manifest checked against the declared table. It sees neither rc helpers nor session bindings, and its own helper lets never reach the session. A helper shared with the rc belongs in a module; either file may use it. The plugin's value is its tail. A factory is applied to its options, and the result is parsed: handler arity, hook names, key notation, guard regexes, and handler collisions are all checked before anything is installed. Persistent command handlers already checked statically are vetted again against their actual names at installation, yielding only a verdict, no stored scheme. Hooks and handlers are then committed and the host told; if the host refuses the manifest, the whole namespace is rolled back. Errors are reported as plugin 'NAME': …, with source spans in the plugin file.

The rc file's plugins: map supplies options (section 8); [:] supplies only the plugin's defaults. Handing a non-empty options map to a plugin that returns a manifest directly, as from an rc entry giving options to one, is an error, since they would be silently dropped.

A duplicate plugin name is an error. There is no run-time loading or unloading command; changing the plugin set requires a new session.

5 Hooks

A hook is a handler for one of six events. Hooks are registered statically from the manifest and are never added at run time. The events, their records, and their exact semantics are SPEC §15.6; the design points are these.

One argument. Every handler takes exactly one: an event record, or for prompt the prompt string. Registration checks the arity, so a handler of any other shape is a load error, not a run-time surprise. A record carries only what the event alone knows (buffer-change gives the old buffer, not the new one); the editor is read through _ed-*.

Event Role
buffer-change observe an edit; set ghost text, highlights
complete claim the word under the cursor at Tab
pre-exec rewrite the line about to run
post-exec observe the line that ran and its status
chpwd observe a change of the session's directory
prompt transform the prompt string

Three shapes of composition. Observers (buffer-change, post-exec, chpwd) run for every plugin, in load order, and their values are ignored. Transformers (pre-exec, prompt) fold: each handler receives what the previous one returned. Claimants (complete) are tried in order and the first non-empty answer wins, passing to ral's own completion when none does.

No handler can wedge the shell. A failing handler is reported with the label plugin 'NAME' hook 'EVENT': and the others still run. A failing pre-exec leaves the line as it was, so no hook can stop every line. A line is refused by rewriting it to one that fails, and its status reaches post-exec through the ordinary run:

hooks: [
    pre-exec: { |ev|
        if !{re-match '^rm -rf /' $ev[src]} {
            return "fail 'refused'"
        } else { return $ev[src] }
    },
]

buffer-change and complete fire on every keystroke and every Tab, so they are held to a different standard. They run aside, so nothing they do flows back into the session. A buffer-change hook's ghost text and highlights are its output and are kept; a complete hook's writes to the buffer, ghost text, and highlights are discarded. They are also braked per plugin and event. A run that overruns its budget (100ms, 500ms), or three failures in a row, disables that hook for the rest of the session with one notice. A slow handler thus costs the user at most one stutter.

prompt is the one event that is a transformer of a string rather than an event record, and it is read like the session prompt: a returned value is the prompt, and otherwise captured output is.

hooks: [
    prompt: { |base|
        let b = try { return !{git branch --show-current | from-line} } { |_| return '' }
        if $[$b == ''] { return $base } else { return "$base [$b] " }
    },
]

complete receives what _ed-parse returns, so a plugin completes the word the way the editor sees it. It claims a word only where it has something to say, and returns [] elsewhere:

hooks: [complete: { |[words: ws, current: i]|
    let head = intercalate ' ' !{take 2 $ws}
    if $[$i == 2 && !{elem $head ['git checkout', 'git switch']}] {
        let w = last !{take 3 $ws}
        let branches = try { return !{lines !{git branch '--format=%(refname:short)'}} } { |_| return [] }
        return !{filter { |b| $[!{slice $b 0 !{length !{chars $w}}} == $w] } $branches}
    } else { return [] }
}]

The minimal frontend, used on dumb terminals, dispatches no buffer-change or complete hooks and no editor keybindings, and shows no ghost text or highlights. Command handlers and the other four events do not depend on raw-mode editing and work there (SPEC §15.6).

6 Keybindings

A keybinding is [key: Str, handler: block, guard?: Str]. The design is one ordered dispatch table shared by every frontend, so that precedence is decided once and the frontends cannot disagree: plugins in load order, manifest order within a plugin, and the editor's built-in action as the tail.

A guard decides, not the handler. A press runs the first entry whose keys match and whose guard, a regex tested against the text left of the cursor, allows. Whether a binding claims a press is settled before its handler runs, and the handler's return value carries nothing. There is no fallthrough protocol for a handler to forget or get wrong. Several guarded bindings on one key compose as an ordered pattern match, and an unmatched press does the built-in thing: a guarded tab cooperates with ral's own completion.

Key notation has one spelling per key (SPEC §15.6): chords are modifier-…-key, a sequence is chords separated by spaces. Where a terminal cannot distinguish two spellings (ctrl-h from backspace, ctrl-C from ctrl-c, shift-a from A), the unreachable one is refused, and the refusal names the spelling to use. A key that could be written two ways would be bound twice and fire once.

Load-time rules keep the table honest:

  • ctrl-c and ctrl-d, alone or in a sequence, are the session's escape hatches and cannot be bound. A wedged handler on either would leave no way to stop it or to leave the shell.
  • An unmodified key other than f1 to f12 carries a built-in editing action, so binding it requires a guard; otherwise the binding would shadow typing on every press. Modified chords and function keys may be unguarded, since replacing the editor default (ctrl-t, alt-c, ctrl-r) is the point.
  • A sequence begins with ctrl-x, the prefix the line editor leaves free.
  • A binding that can never fire loads with a warning naming what claims its keys first: an earlier unguarded binding on the same keys, or an unguarded binding on a proper prefix of the sequence, wherever loaded.

A handler receives [key: Str, mode: Str]: the binding's notation and the editor's input mode at the press (insert, command, replace; always insert in emacs). It reads the buffer with _ed-get and acts through the other _ed-* builtins. When it returns without _ed-accept, the editor re-enters with the handler's final buffer and cursor.

A keybinding handler is the one run that holds the terminal, which is what _ed-tui needs (section 7).

7 The editor interface

The _ed-* builtins are the whole of a plugin's access to the editor. They are installed only by the interactive host and hidden from help. Each is its own builtin, not one _ed command with an op argument, so that the checker sees each op's real return type and arity is fixed per op. Signatures and contracts are SPEC §15.7. What a plugin author needs to know that the table does not say:

  • Offsets are characters, as chars splits text and slice counts it, never bytes. The cursor is clamped into the text after every write.
  • State is read whole. _ed-get answers text, cursor, and the configured keymap (emacs or vi); the live vi mode arrives instead with a keybinding's mode.
  • _ed-push and _ed-accept are zsh's push-line and accept-line. The pushed buffer returns at the next prompt, so a handler can run a generated command and give the user back the line they were typing.
  • _ed-ghost and _ed-highlight set display state, not input. Among plugins the last in load order with non-empty ghost text wins. Each highlight call replaces the spans that run had set, spans from several plugins are composited with the later plugin on top, and out-of-range offsets are clamped. The style names are a fixed vocabulary (command, builtin, prelude, argument, option, path-exists, path-missing, string, number, comment, error, match, bracket-1, bracket-2, bracket-3); an unknown one is an error.
  • _ed-parse is the lexer's simple command at the cursor: its tokens split at |, ?, ;, newlines, and braces, with an open { or [ read as closed, because a buffer is routinely mid-block. A cursor in whitespace stands on a fresh '' at its own offset, so words[current] is always the word being typed. words is empty exactly when the buffer does not lex.
  • _ed-history returns entries most recent first, deduplicated; limit 0 means all.
  • _ed-state is the plugin's one persistent cell, held by the host for the session. The first call runs the updater on the default, later calls on the stored value; to read without changing, return the argument. The cell is a boundary: what it holds was written by an earlier run, so it is admitted against the type this call uses it at, before the updater runs, and refused with the field named when it does not fit. The updater's result must be first-order data.
  • _ed-clipboard copies natively when ral runs locally and by OSC 52 otherwise, and answers whether all of the text landed. It needs editor:write`.

_ed-tui runs a body with the editor suspended, stdout captured, and the terminal lent to it, so that a fuzzy finder can draw while its pick comes back as a string. The body is a command, its value its output; the captured bytes are decoded lossily after stripping exactly one trailing LF or CRLF. The call never raises. On success status is 0; when the body fails, status is its exit code and output its error message, so a plugin tells cancellation (fzf's 1 and 130) from a real error without wrapping in try. The terminal is lent only to a keybinding handler. From any other hook, or nested inside another _ed-tui, the call reports status 1 and says why instead of raising, since those runs have no terminal to lend.

8 Configuration

Plugins are configured, not scripted into place, by the rc file's plugins: map (SPEC §15.3): from plugin name or path to that plugin's options map, forwarded to the plugin's factory as its single argument. Every rc key is required; the field below belongs in a complete rc, such as rc.example, not a file on its own.

plugins: [
    autosuggestion: [:],
    fzf-files:      [key: 'ctrl-t'],
    zoxide:         [key: 'alt-z'],
],

[:] is the empty map, for a plugin taking no options. A value that is not a map is reported by name and that plugin skipped; the rest load. A plugin that fails to load is reported and skipped, never fatal to the session. Plugins from plugins: load after the rest of the rc has taken effect, in alphabetical order of name (ral maps are ordered by key), so load order, and with it hook and keybinding precedence, does not depend on how the file was written. To load conditionally, compute the map:

let plugins = if !{succeeds { which fzf > /dev/null }} {
    return [autosuggestion: [:], fzf-files: [:], fzf-history: [:]]
} else {
    return [autosuggestion: [:]]
}

Then write plugins: $plugins in the rc's final record, alongside all its other required keys. Collection entries may be computed; the rc's own keys must remain written.

9 Examples

The plugins shipped in plugins/ are the working examples, and each is loaded in CI (just test).

  • autosuggestion.ral: fish-style ghost text from history, one buffer-change hook over _ed-get, _ed-history, and _ed-ghost.
  • zoxide.ral: handlers (z, zi), a chpwd hook, and a keybinding with _ed-tui, with a configurable key.
  • fzf-files.ral, fzf-cd.ral, fzf-history.ral: fzf's CTRL-T, ALT-C, and CTRL-R widgets. They port fzf's own key-bindings.zsh, and what those scripts share (the option stack, the fzf-tmux switch, the exit statuses) is one module, plugins/lib/fzf.ral, bound with use at each plugin's top level, where the path resolves beside the plugin file.
  • fzf-completion.ral: **-trigger completion on a guarded tab, so a plain Tab still reaches ral's completer.

fzf-cd shows the whole shape: a factory, one keybinding, the shared picker with a continuation that runs only on a selection, and the push, set, accept triple.

let [pick: pick, var: var] = use 'lib/fzf.ral'

{ |options| [
    name: fzf-cd,
    keybindings: [[key: !{get $options key alt-c}, handler: { |_|
        pick '--reverse --walker=dir,follow,hidden --scheme=path' "!{var FZF_ALT_C_OPTS ''} +m" `walk !{var FZF_ALT_C_COMMAND ''} [] { |[d]|
            _ed-push
            _ed-set [text: `set "cd !{ral-quote !{absolute-path $d}}", cursor: `keep]
            _ed-accept
        }
    }]],
] }

A small buffer-change sketch, dimming everything after a #:

let dim-comment = { |_|
    let text = !{_ed-get}[text]
    let [code, ...rest] = re-split '#' $text
    if !{is-empty $rest} { _ed-highlight [] } else {
        _ed-highlight [[start: !{length !{chars $code}}, end: !{length !{chars $text}}, style: 'comment']]
    }
}
return [name: 'dim-comment', hooks: [buffer-change: $dim-comment]]

10 Deliberately not supported

Each of these is a choice, not a gap.

  • A capability manifest. See section 2. Confinement is grant and the session ceiling.
  • Run-time registration. Hooks, keybindings, and command handlers come only from manifests loaded at startup.
  • Return-value fallthrough in keybindings. A guard claims a press before the handler runs.
  • Plugins outside the interactive ral. Batch hosts, exarch, and synod load no REPL plugins and install no _ed-*; portable code must not assume them (SPEC §15, §15.8).
  • Key sequences not under ctrl-x, and a bindable ctrl-c or ctrl-d. See section 6.

Wanted, not built:

  • Left and right prompt hooks. prompt transforms the whole prompt string, which suffices for decorating the left and leaves no clean way to contribute to a right-hand prompt.
  • Asynchronous prompt segments. A prompt handler runs to completion before the prompt draws; slow work such as VCS queries wants a cached result.
  • Highlight-style overrides. The style vocabulary is fixed and the rc keyset has no key to remap it.
  • _ed-parse over the parser. It splits the lexer's tokens, so a cursor inside $[…], a list literal, or a redirect target is not seen for what it is.