# hotty SDKs

    Version:  0.10, with SPEC.md 0.10
    Status:   Draft. It changes with SPEC.md (§15 there).
    Licence:  CC BY 4.0

## Abstract

An **SDK** is a library through which a program speaks hotty in one
language. This document says what every SDK provides, whatever its
language: its components, what each one does, the options each takes and
their defaults, and the names they go by. A program written against one SDK
reads the same against another, once the language's idioms are applied.

An SDK has two layers. The **wire layer** encodes and decodes, and does no
I/O; the conformance vectors check it. The **SDK layer** does the I/O for a
kind of program: one that prints and exits, one built on a UI framework, a
test. It is described here so that SDKs agree where a program would notice,
and follow their language everywhere else.

The names this document gives are the canonical ones (§2.2, Appendix A).
The SDKs that follow it are listed in the documentation, not here
([Choose an SDK](https://hotty.neuroplast.io/docs/sdks)).

## Contents

1. Introduction
2. Principles
3. The wire layer
4. The SDK layer
5. Conformance

Appendices: A. Names · B. Rationale · C–I. Moving from 0.2 to 0.9

---

## 1. Introduction

[SPEC.md](https://hotty.neuroplast.io/docs/protocol) defines the wire: what a program and a host send each
other. This document defines how a library presents the wire to a program.
Where the two disagree, SPEC.md is right and this document is wrong.

The key words **MUST**, **MUST NOT**, **SHOULD**, **SHOULD NOT** and **MAY**
are to be interpreted as in SPEC.md §2.

- **Program**, **host**, **surface**, **placement**, **reply**, **event**:
  as in SPEC.md.
- **SDK**: a library that speaks hotty for programs in one language.
- **Wire layer**: the part of an SDK that builds commands and reads what the
  host sends. It does no I/O (§2.1).
- **SDK layer**: the part that reads and writes a terminal, or works through
  whatever stands for one (§4).
- **Canonical name**: the name this document gives a component, an
  operation, an option or a field. An SDK spells it in its language's case
  (Appendix A).
- **Environment**: what the program runs in: a process with a terminal, a
  UI framework that owns the screen, an editor, a page.

## 2. Principles

### 2.1 Two layers

- **The wire layer is pure.** It **MUST NOT** do I/O, start threads, or read
  a clock: where it needs the time (the Detector, §3.8), the caller passes
  it in. Given the same input, it returns the same output.
- **It depends on the language's standard library only**, and on base64,
  zlib and msgpack where that library lacks them. msgpack writes a
  program's deltas and menus and reads what a host sends (SPEC.md §3.3).
  zlib is needed only to compress what a program sends, which is optional
  (§3.3); a host never compresses what it sends (SPEC.md §3.3).
- **The SDK layer is built on the wire layer's public interface**, so a
  program that needs something the SDK layer does not do can do it from the
  same parts.

### 2.2 Canonical concepts, idiomatic surface

- Every component, operation, option and field in §3 has a canonical name.
  An SDK **MUST** provide each under a name a reader maps to it at a glance:
  the canonical name in the language's case (`SetText`, `set_text`,
  `setText`). Where the name is a keyword of the language, the SDK picks the
  nearest word (`del` is `delete` in Python) and says so in its
  documentation.
- **Options take the language's form**: functional options in Go, keyword
  arguments in Python, a table in Lua, a struct or a builder in Rust, an
  object in TypeScript. What an option is called, and its default, are
  canonical.
- **Errors take the language's form**: an error value, an exception, a
  result type. What they carry (a code and a detail, §3.9) is canonical.
- **Absence takes the language's form**: Go's zero value and `ok`, Python's
  `None`, Lua's `nil`, Rust's `Option`. The vectors write it `null`.
- **Data takes the language's form**: what a message or a control holds
  (§3.2, §3.9) is a field or an accessor, and a question about it
  (`Has`) a method or a function.

### 2.3 Shared defaults

The defaults in this document (quiet levels, timeouts, limits, the keymap
of §3.10) are the same in every SDK, so a program behaves the same in every language. An SDK
**MUST NOT** choose other defaults; it **MAY** let the program change them.

### 2.4 Protocol only

An SDK's contract is SPEC.md. Everything a program does with the protocol
that the protocol does not define, such as rendering Markdown, highlighting
code, drawing charts, or building forms from a schema, is presentation, and
outside this document. An SDK **MAY** ship such helpers beside it. They
depend on the SDK; the SDK never depends on them, and nothing in §3 or §4
needs them.

What a text field does with a key is the protocol's (SPEC.md §10.2), so
keymaps (§3.10) and editing a field in cells as a host edits it (§4.6) are
in this document.

### 2.5 Three renditions

Every program has three renditions (SPEC.md §14): surfaces on a host, cells
on a terminal that is not one, and plain data when there is no terminal. An
SDK makes the three branch points plain:
- **No terminal:** opening the terminal fails (§4.1). The program writes
  plain data.
- **Not a host:** detection says no (§3.8). The program draws in cells, and
  sends nothing of hotty but the query.
- **A host:** surfaces.

### 2.6 Leave the terminal clean

Replies and events arrive on the program's input, and whatever reads the
input after the program exits, a shell for instance, reads them as typing.
An SDK makes it easy, and its documentation says how, for a program to:
- read every reply its commands caused before it exits (a fence, §4.1);
- leave no surface that still reports events: create the ones it leaves on
  the screen detached, or detach them (SPEC.md §5.5).

### 2.7 Forward compatibility

As SPEC.md §15 has programs do:
- control keys the SDK does not know are kept in the message, and change
  nothing;
- an event of a kind it does not know is an event all the same: its kind is
  reported as it came;
- fields it does not know, in the capabilities or a detail, are ignored.
  Reading a message **MUST NOT** fail because of its body: a body that
  does not decode leaves absent what it would have filled (§3.9).

### 2.8 Environments

Some environments already do part of what the SDK layer does. A UI
framework's input parser may hand over complete OSC sequences; an editor may
own the terminal's output and hand its input over as events (§4.2). There,
the SDK layer uses what the environment provides: it feeds complete
sequences to the Decoder (§3.6) and writes through the environment's
output. The wire layer is the same everywhere.

### 2.9 Updates cost what they change

A program that redraws a surface's tree, itself or through a framework,
diffs the new tree against the last one it sent and sends what changed as
the ops of one delta (SPEC.md §6.1): a `text` op where a cell's text
changed, 56 bytes, where morphing the whole tree of a small table sends 930.
A whole-tree `morph` without a target stays the fallback, and keeps what the
user was doing. A stylesheet that several documents share goes once, as a
resource (SPEC.md §7.1), not in each document. An SDK's helpers that build
documents (§2.4) do both.

## 3. The wire layer

An SDK **MUST** provide every component of this section.

| component | does | names | vectors |
| --- | --- | --- | --- |
| constants (§3.1) | the protocol's numbers and named values | `Number`, `Chunk`, … | — |
| Control (§3.2) | an ordered list of keys and values | `Control` | — |
| Encode (§3.3) | one command as OSC sequences | `Encode` | `encode` |
| commands (§3.4) | a builder for each action | `Doc`, `Place`, … | `build` |
| names (§3.5) | surface names | `ValidName`, `SurfaceName` | — |
| Decoder (§3.6) | OSC sequences into messages | `Decoder` | `wire`, `decode` |
| Scanner (§3.7) | hotty sequences out of a byte stream | `Scanner` | `scan` |
| Detector (§3.8) | whether the terminal is a host | `Detector` | `detect` |
| messages (§3.9) | replies, events, capabilities, errors | `Reply`, `Event`, `Caps`, `Error` | `decode` |
| keys (§3.10) | key names, keymaps, the SDK's keymap | `ParseKey`, `DecodeKeys`, `KeyText`, `Keymap`, `TerminalKeys` | `keys`, `keymap` |

### 3.1 Constants and named sets

| name | value |
| --- | --- |
| `Number` | `"7279"`, the OSC number |
| `Chunk` | 4096, the most base64 bytes in one sequence |
| `MaxSize` | 1000, the most columns or rows of a surface |
| `MaxName` | 64, the longest surface name |
| `Version` | `"0.10"`, the newest protocol version the SDK speaks (SPEC.md §4) |
| `Versions` | `["0.9", "0.10"]`, every version the SDK speaks, which its query lists (§3.8) |
| `DefaultMax` | 16777216 (16 MiB), the most bytes a Decoder decodes one message to unless its `Max` says otherwise (§3.6) |

And these sets, each value under its canonical name:
- **event kinds**: `click`, `change`, `input`, `caret`, `submit`, `press`,
  `contextmenu` (`EventContextMenu`), `auxclick`, `focus`, `blur`, `fit`,
  `hover`, `view`, `dragstart`, `drag`, `dragend`, `gesturestart`
  (`EventGestureStart`), `gesture`, `gestureend` (`EventGestureEnd`),
  `wheel`, `command`, `caps`, `pointer` (SPEC.md §9, §9.8, §9.9);
- **kinds of pointer**, as Pointer Events name them: `PointerMouse`
  `"mouse"`, `PointerPen` `"pen"`, `PointerTouch` `"touch"` (SPEC.md §9;
  a later kind has no name, and a program takes it as `PointerMouse`);
- **features**: `passthrough`, `hover`, `count`, `menu`, `backdrop`
  (SPEC.md §4);
- **buttons**, as the DOM numbers them: `ButtonPrimary` 0, `ButtonMiddle` 1,
  `ButtonSecondary` 2, `ButtonBack` 3, `ButtonForward` 4 (SPEC.md §9; a
  later button is 6 or more, and has no name);
- **modifier bits**: `ModShift` 1, `ModAlt` 2, `ModCtrl` 4, `ModSuper` 8
  (SPEC.md §9);
- **error codes**: `EINVAL`, `ENOENT`, `ENOTARGET`, `EDETACHED`, `EQUOTA`,
  `EBUDGET`, `ENOTERM`, `ESTALE`, `EVERSION` (SPEC.md §3.6);
- **delta ops**: `morph`, `inner`, `replace`, `append`, `prepend`, `before`,
  `after`, `remove`, `attr`, `unattr`, `text`, `var`, `reveal`, `scroll`
  (SPEC.md §6.1);
- **quiet levels**: `ReplyAlways` 0, `ReplyOnError` 1, `NoReply` 2
  (SPEC.md §3.5).

### 3.2 Control

A command's control is an **ordered** list of keys and values: keys go out
in the order they were added. It reads a key's value and whether it is
present (`get`), and sets one, in place if present, at the end if not
(`with`).

### 3.3 Encoding

`Encode(control, payload)` returns one command as one or more OSC
sequences. Its output is fixed except where the protocol leaves a choice to
the program (compression), so the `encode` vectors compare bytes.

- **Values are cleaned.** Each character a value may not hold becomes one
  `_`: `:`, `;`, `=`, a control character, and any character outside ASCII,
  one `_` for each Unicode code point, and one for each byte that is not
  UTF-8 (SPEC.md §3.2). Keys are the SDK's own and are not cleaned.
- **Keys go out in order**, as `key=value` joined by `:`.
- **The payload is base64**, the standard alphabet, padded. An empty payload
  is sent as nothing, with no `;` before it.
- **Compression is the program's choice.** An SDK **MAY** compress a payload
  with zlib and mark it `o=z`. It **SHOULD** compress only a payload longer
  than one chunk holds plain, 3072 bytes, whose base64 is `Chunk`: below
  that, compressing saves no sequence, and zlib's fixed cost per message is
  several times the base64's. It compresses only when that makes the
  payload smaller, and at zlib's fastest level, 1: on a recorded session of
  plx, level 1 took 58% less time than the default level, 6, for 37% more
  bytes, still a quarter of plain base64's. `o=z` follows the other keys.
  - **`Plain` turns it off for one document** (§3.4.2): a document that
    never leaves the program's process, such as one a renderer inside the
    program draws into cells, is sent uncompressed, so nothing is
    compressed only to be inflated again.
- **`o` and `m` are Encode's.** A control given with either goes out
  without it, and Encode sets them as this section says.
- **Chunks.** A payload whose base64 is longer than 4096 bytes is split
  into chunks of exactly 4096, the last one excepted. Nothing of 4096 or
  less is chunked.
  - The first chunk carries the whole control, then `m=1`.
  - Every further chunk carries only `m`: `1`, or `0` on the last.
- **Every sequence ends with ST** (`ESC \`).

### 3.4 Commands

A builder for each action returns the command's sequences, ready to write.
Each takes the reply options of §3.4.2 and has the default quiet level shown,
which SPEC.md leaves to the program; these are the levels every SDK uses.

| builder | sends | parameters | default `q` |
| --- | --- | --- | --- |
| `Query` | `a=q:n=<n>:v=<Versions>`, joined by `,`; `late=1` with the `Late` option, then DA1 (`ESC [ c`) | n | 0 |
| `Doc` | `a=doc:s` and the HTML; `d=1` with the `Detached` option, `scroll` with `Scroll`, never `o=z` with `Plain` | surface, html | 1 |
| `Place` | `a=place` (§3.4.1) | surface, placement | 1 |
| `PlaceAt` | `ESC 7`, `ESC [ <y+1> ; <x+1> H`, `Place` with `C=1`, `ESC 8` | surface, x, y, placement | 1 |
| `Hide` | `a=hide:s` | surface | 2 |
| `Delta` | `a=delta:s` and its ops as msgpack (§3.4.3) | surface, ops | 2 |
| `SetText` | `Delta` with one op, `Text` | surface, target, text | 2 |
| `SetVar` | `Delta` with one op, `Var` | surface, target, name, value | 2 |
| `SetAttr` | `Delta` with one op, `Attr` | surface, target, name, value | 2 |
| `RemoveAttr` | `Delta` with one op, `Unattr` | surface, target, name | 2 |
| `MorphTo` | `Delta` with one op, `Morph` | surface, target, html | 2 |
| `Reveal` | `Delta` with one op, `RevealOp`: `where` is `start`, `end`, or empty for the nearest (SPEC.md §6.1) | surface, target, where | 2 |
| `Scroll` | `Delta` with one op, `ScrollOp`: `action` is a scroll action (SPEC.md §6.1, §10.2) | surface, target, action | 2 |
| `SetField` | `a=field:s:t` and the change as msgpack (below; SPEC.md §6.4) | surface, target, change | 1 |
| `Res` | `a=res:id:type` and the data | id, mime, data | 2 |
| `DelRes` | `a=del:id` | id | 2 |
| `Del` | `a=del:s` | surface | 2 |
| `DelAll` | `a=del` | — | 2 |
| `Detach` | `a=detach:s` | surface | 2 |
| `Focus` | `a=focus:s[:t]`; `t` only when given | surface, target | 2 |
| `Blur` | `a=blur:s` | surface | 2 |
| `Sync` | `CSI ? 2026 h`, the commands, `CSI ? 2026 l` (SPEC.md §6.3) | commands | — |
| `WithdrawLate` | `a=q:q=2`: withdraws a query that asked for a late answer (SPEC.md §4) | — | — |
| `Edit` | `a=edit:take=<names>`, the names joined by `,`, in the order given; `take=` empty takes none (SPEC.md §10.6) | names | 2 |
| `Menu` | `a=menu` and the menu as msgpack (below); with no menu, no payload, which leaves the program none (SPEC.md §10.7) | menu | 2 |
| `MenuShow` | `a=menu:show=1` and `{"seq": [str]}`, the keys typed; with none, no payload, which shows the top level (SPEC.md §10.7) | keys | 2 |
| `MenuHide` | `a=menu:show=0` | — | 2 |
| `Touch` | `a=touch` and `{"areas": [area]}` as msgpack (below); with no areas, no payload, which leaves none (SPEC.md §9.6) | areas | 2 |
| `Hit` | `a=hit:s:c:r`: which element is at the surface's cell (c, r), counted from its top left cell, negative above and to its left (SPEC.md §9.7) | surface, c, r | 0 |

- **`Doc`, `Place` and `SetField` answer errors**, since a refused document
  or placement (`EQUOTA`, `ENOENT`), and a field the user changed first
  (`ESTALE`), are something a program must hear. Everything else
  answers nothing unless asked: a program that sends deltas every frame would
  otherwise fill its input with replies.
- **`PlaceAt`** places a surface at screen cell (x, y), counted from 0, and
  leaves the cursor where it was: for a full-screen program, which places
  surfaces where it draws them. The coordinates have no leading zeros.
- **`Sync`** takes commands already built, and brackets them.
- **`Menu`** takes a `MenuTree`: `Label` and `Items`, each item a
  `MenuItem` with `Key`, `Label`, `Name`, `Enabled` and `Items`, its own.
  It writes one msgpack map, each value in the smallest of its type's
  forms: `label` unless it is empty, `items`; each item `key`, `label` and
  `name` unless they are empty, `enabled`, and `items` unless there are
  none. The program keeps it within eight levels (SPEC.md §10.7).
- **`SetField`** takes a `FieldChange`: `Rev`, and `Value`, `From`, `To`,
  `Text`, `Sel` (two positions, the anchor and the caret) and `Checked`,
  each absent unless given, so that an empty `Value` or `Text` and a
  position 0 are sent. It writes one msgpack map of `rev` and the fields
  given, in that order, each value in the smallest of its type's forms.
  It checks none of them: the host does (SPEC.md §6.4).
- **`Touch`** takes a list of `TouchArea`, each with `C`, `R`, `W`, `H`
  and `Drag`: the screen's cells it covers, counted from 0, and `"x"`,
  `"y"` or `"xy"`. It writes each area as a map of all five, in that
  order. The program keeps to 256 areas, `W` and `H` at least 1, and
  sends them again when what it drew moves, or the screen changes
  (SPEC.md §9.6).

#### 3.4.1 Placement

A `Placement` says where and how a surface is shown (SPEC.md §5.2):

| field | key | default | sent |
| --- | --- | --- | --- |
| `Cols` | `c` | — | always |
| `Rows` | `r` | 0, which is `auto` | always: the number, or `auto` |
| `Window` | `x`, `y`, `w`, `h` | none | all four, or none: none when absent, when all are 0, or when it shows the whole surface (rows given, and the window is 0, 0, cols, rows) |
| `Z` | `z` | 0 | when not 0 |
| `Backdrop` | `b=1` | false | when true |
| `Press` | `p` | absent | `p=1` when on, `p=0` when off, nothing when absent |
| `Fit` | `f` | absent | as `Press` |
| `Hover` | `v` | absent | as `Press` |
| `KeepCursor` | `C=1` | false | when true |

`Press`, `Fit` and `Hover` belong to the surface (SPEC.md §5.2), so each is
absent, on or off: a placement that leaves them absent leaves them as they
were (Appendix A gives each language's three).

`Backdrop`, like `Z`, belongs to the placement: a placement without it is
above the cells, so a program that places a backdrop every frame sets it
every time. It goes out whatever the host: a program sets it only where
`Has(backdrop)` (§3.9; SPEC.md §5.2), since a host without the feature
ignores `b` and draws the surface over the text.

`Backdrop` is the last of the fields (Appendix A), though `b` goes out
after `z`. A language that takes the fields by position, as Python's
constructor does, then gives a caller's arguments the meaning they had
before 0.6.

#### 3.4.2 Reply options

Every builder but `Query`, `Sync` and `WithdrawLate` takes two options:
- **`N(n)`** numbers the command and asks for its reply: `n=<n>`, and
  quiet level 0 unless the program gives `Q`.
- **`Q(q)`** sets the quiet level. A `Q` given wins over the level `N`
  implies, whatever the order in which the program gives them.

**`q` goes out only when the wire does not imply it** (SPEC.md §3.5): it is
left out where it would be 0 with `n`, or 1 without, and for the query.
`Hit`, a query too, implies 0 with `n` or without, so it carries `q` only
when `Q` gives another level. So `Doc` and `Place` carry no `q`, a delta
carries `q=2`, and `Doc` with `N(4)` carries `n=4` alone.

`Query` takes one option of its own, **`Late`** (`late=1`): the program
asks for a late answer (SPEC.md §4).

Each key goes out once. `Doc` also takes **`Detached`** (`d=1`) and
**`Scroll(axes)`** (`scroll=<axes>`, SPEC.md §5.1): a bitmask of
`ScrollVertical` (1) and `ScrollHorizontal` (2). `0`, the default, sends
no key; any other value goes out as given, for the host to judge. **`Plain`** sends the HTML uncompressed and adds no key
(§3.3).

#### 3.4.3 Ops

`Delta(surface, ops…)` sends one delta of the ops given, in order (SPEC.md
§6.1). An **`Op`** has `Op`, `Target`, `Key` and `Value`, and a builder for
each op makes one:

| builder | op | parameters |
| --- | --- | --- |
| `Morph` | `morph` | target (empty: the whole tree), html |
| `Inner`, `Replace`, `Append`, `Prepend`, `Before`, `After` | the same, in lower case | target, html |
| `Remove` | `remove` | target |
| `Attr` | `attr` | target, name, value |
| `Unattr` | `unattr` | target, name |
| `Text` | `text` | target, text |
| `Var` | `var` | target, name, value |
| `RevealOp` | `reveal` | target, where |
| `ScrollOp` | `scroll` | target (empty: the root), action |

The body is one msgpack map, `{"ops": [op, …]}`, each value in the smallest
of its type's forms, and each op a map with its keys in this order:
- `op`;
- `t`, unless the target is empty;
- `k`, for `attr`, `unattr` and `var`;
- `v`: for an op that takes one (SPEC.md §6.1), always, even empty, as
  `scroll`'s action; for `reveal`, only when `where` is `start` or `end`.

Targets and values are not cleaned (§3.3): they travel in the body, as the
document has them. A `Delta` of no ops is still sent, and the host answers
it `EINVAL` (SPEC.md §6.1).

### 3.5 Names

- **`ValidName(s)`**: whether `s` is a surface name: 1 to 64 of `A`–`Z`,
  `a`–`z`, `0`–`9`, `_` and `-`.
- **`SurfaceName(s)`**: `s` made one: each other character becomes `_`, it
  is cut to 64, and an empty name becomes `_`.

### 3.6 Decoder

A Decoder turns OSC sequences into messages, one at a time, joining chunked
ones. A new Decoder is ready to use.

- **`Feed(seq)`** takes one OSC sequence, `ESC ] …`, **with or without its
  terminator** (an environment may strip it, §4.2), and returns a result and,
  when the result is `complete`, the message:

  | result | when |
  | --- | --- |
  | `not_hotty` | the sequence is another OSC, or another number (`ESC ] 72790`). The caller handles it; a chunked message in progress is not affected |
  | `partial` | a chunk of a message that is not complete yet |
  | `complete` | a whole message |
  | `invalid` | a malformed message (SPEC.md §3.7), dropped |

- **The control is strict.** It is SPEC.md §3.2's grammar: a key that does
  not parse, a value outside printable ASCII, a key that appears twice, or
  an empty control is malformed.
- **Chunks** (SPEC.md §3.4):
  - A control with only `m` continues the message in progress; with none
    in progress, it is malformed.
  - Any other message arriving before the last chunk aborts the one in
    progress, which counts as malformed, and is then decoded as usual. If it
    is malformed itself, that makes two (SPEC.md §3.7).
  - The pieces of base64 are joined before they are decoded, so a chunk may
    end anywhere in a base64 quantum.
- **The payload** is base64, with or without padding, whitespace ignored.
  Any other byte makes the message malformed. A payload marked `o=z` is
  inflated if the SDK has zlib; one that does not reports it `invalid`.
  Hosts never compress (SPEC.md §3.3), so only a relay meets one.
- **The message** has the control without `m` and `o`, and the payload as
  bytes.
- **`Invalid`** counts the malformed messages dropped so far.
- **`Max`** is the most bytes one message decodes to, after base64 and
  zlib; 0 is `DefaultMax`. Past it the message is `invalid`, counted once
  (SPEC.md §3.4, §3.7): a chunked one holds no more base64 than `Max` bytes
  could take, zlib's overhead included, and its further chunks are dropped
  up to its last, which reports `invalid` without counting again. Zlib
  stops inflating past `Max`. A host that decodes with a Decoder reports
  its `Max` as `limits.message` (SPEC.md §13).

### 3.7 Scanner

A Scanner cuts hotty sequences out of a byte stream, however the reads split
it. An SDK that reads a raw terminal uses one; so does a relay, on a
program's output. An environment that hands over complete sequences needs
none (§2.8), but the wire layer has one all the same.

- **`Feed(bytes)`** returns segments, in order:
  - `pass`: bytes that are not hotty's, unchanged: keys, mouse reports,
    other OSCs, other answers;
  - `osc`: one complete hotty sequence, from `ESC ] 7279 ;` to its
    terminator (ST or BEL), ready for the Decoder;
  - `da1`: optionally, an answer to Primary Device Attributes,
    `CSI ? <digits and ;> c`, for an SDK that reads DA1 itself (§4.1). A
    Scanner that does not look for them passes them.
- **It holds only what may still become a segment of its own**: bytes that
  are the start of `ESC ] 7279 ;` (or of a DA1 answer, when it looks for
  them), and a hotty sequence in progress. Everything else goes out in the
  same `Feed` that brought it. So the segments do not depend on how the
  stream was split, and a key is never kept waiting for the next read.
- **`Holding()`** returns what it holds, and **`InSequence()`** whether
  that is a hotty sequence in progress (or one too long, being dropped),
  which the next `Feed` goes on with, rather than the start of a segment,
  which a program reading keys may flush after a moment as typing.
- **An ESC inside a hotty sequence** that does not begin its ST ends the
  sequence unfinished: the sequence is dropped as malformed, and the ESC
  begins what comes next.
- **A hotty sequence longer than 64 KiB** (65536 bytes, its terminator not
  counted) is dropped as malformed, up to its terminator. No host sends one:
  chunks are at most 4096 bytes of base64.
- **`Flush()`** ends the stream: what is held goes out as `pass`, except a
  hotty sequence in progress, which is dropped as malformed. A program
  reading keys flushes a lone ESC held at the end of a read, as the Escape
  key; that is the SDK layer's choice (§4.1).
- **`Invalid`** counts the sequences it dropped.

### 3.8 Detector

A Detector decides whether the terminal is a host (SPEC.md §4). It is a
state machine with the time passed in, in milliseconds, so it behaves the
same in every SDK and environment, and the `detect` vectors check it step
by step.

- **`Start(now)`** returns the query to send: `Query(n)`, `n` 1 unless the
  program chooses another, with `Late` when the program asks for a late
  answer (SPEC.md §4).
- **`DA1(now)`**: a DA1 answer arrived. It returns whether the answer was
  detection's. **`Reply(r, now)`**: a reply arrived; it returns whether it
  answers the query. **`Tick(now)`**: the time is now. **`End(now)`**: the
  input ended, or the caller gave up.
- **It exposes** `State` (`detecting`, `native` or `text`), `Caps` (the
  host's, when native), `Decided`, `Done`, and `Deadline`: when to call
  `Tick` next, or none once done.
- **Every input first lets the time pass**: whatever was due at or before
  `now` happens before the input is taken.
- **It speaks both 0.9 and 0.10.** An SDK of 0.10 lists `0.9,0.10` in its
  query (`Versions`), and takes the host that answers in either (SPEC.md
  §4, §15). Versions compare as numbers, the minor too: 0.10 is after 0.9,
  where a comparison of strings would put it before.
  - **A 0.9 host** sends no `pointer` event, and no `Pointer`, `Finger`
    or `Cut` in any event (§3.9). It reads no `gesture` and no `wheel` in
    `data-on`, so it sends no `gesturestart`, `gesture`, `gestureend` or
    `wheel`: a pinch over such an element is its own, and a wheel scrolls
    or goes to the terminal as over any other. A second finger ends a
    touch's drag, and the surface hears nothing more until every finger
    lifts. Its media features may be its engine's defaults rather than the
    device's (SPEC.md §8).
  - **A program that may meet one** offers a canvas another way to zoom
    and pan, keys or controls, where it hears no gesture and no wheel; it
    takes an event with no `Pointer` as the mouse's or the keyboard's, as
    SPEC.md §9 has it take a kind it does not know. Or it writes its
    documents by `Caps.V`.
  - **Which version it met** is the capabilities' `V`, in `Caps`, which the
    program reads when it must tell them apart. The host speaks that
    version from then on.
  - A 0.10 host that speaks 0.9 too answers `v=0.9,0.10` in 0.10, and a
    0.9 host answers it in 0.9. A host that speaks only 0.10 answers
    `v=0.9` with `EVERSION`, and an 0.8 host answers `v=0.9,0.10` so: the
    terminal is `text` (§3.8's second rule).
  - **A 0.9 program on a 0.10 host** that speaks 0.9 too hears 0.9's
    events, with 0.9's fields, and its `data-on` is read as 0.9 read it
    (HOST.md §5). Its surfaces' media features follow the device, as in
    every session.

The rules, with their times:

| rule | |
| --- | --- |
| a reply that answers the query (`re=q`, the query's `n`): `a=ok`, its capabilities decoding (§3.9), their `V` one of the SDK's `Versions` | the terminal is a host: `native`, decided, with its capabilities. Done when the DA1 behind it arrives, or 300 ms after the reply, or at 1500 ms, whichever comes first |
| any other reply that answers the query: an error (`EVERSION`, or another), or capabilities that do not decode or name a version the SDK does not speak | the terminal is no host the program can use (SPEC.md §4): `text`, decided. Done as for a host's reply |
| a DA1 before any reply | it may answer a question asked before the query (SPEC.md §4). It opens a grace of 150 ms: a reply within it still answers the query |
| the grace ends with no reply | `text`: not a host. Decided and done |
| 1500 ms with no answer at all | `text`. Decided and done |
| the input ends | `text` if nothing was decided. Done |
| with `Late`, a reply that answers the query once the state is `text`, and makes the terminal a host (the first rule) | a late answer: `native`, with its capabilities. Still decided and done |

- **Which inputs are detection's.** Every DA1 from the start until done is
  detection's, and the SDK layer swallows it: it answers the query's fence or
  an earlier question, and is no key. After done, a DA1 is not
  detection's. A reply that answers the query, `a=ok` or `a=err`, is
  detection's whenever it arrives, even after the state is `text`; a late
  one changes nothing, unless the query asked for it (`Late`), and then
  only the first that makes the terminal a host. Replies with another `n`,
  and everything else, are not.
- **Decided and done** differ only when the terminal answers the query:
  whether it is a host the program can use is known when its reply
  arrives, and the detection is over when nothing more of it will arrive. A program that must not wait, such as a framework's adapter
  (§4.3), acts when decided; one that hands the terminal on, when done.

### 3.9 Messages

A decoded **`Message`** has its control (a map of keys to values) and its
payload (bytes). It reads as a reply or as an event.

**The body** (SPEC.md §3.3) is decoded when the reply or event is read, each
field as its type.
- Fields the SDK does not know are ignored (§2.7), whatever their type.
- A body nested more than 32 levels deep does not decode. An SDK stops
  reading there, so that no body can exhaust its stack: TinyGo's is 64 KB,
  and a few hundred levels crash it.
- Nor does a body that holds, anywhere in it, a field the SDK knows or
  not, what SPEC.md §3.3 has no host send: a nil, a map key that is not a
  string, a key given twice in one map (keys compared as the strings they
  are, whatever their form), a string that is not UTF-8, an int further than 2^53 − 1 from
  zero, or a timestamp of another size, with a second's nanoseconds or
  more, or with seconds further than 2^53 − 1 from 1970. Each is checked
  as the body is read, in every SDK alike, so a body decodes in all of
  them or in none: an int past 2^53 − 1 is not rounded where numbers are
  doubles, nor kept where they are not.
- A body that does not decode leaves absent everything it would have
  filled, and the rest of the message stands. That is a body that is not
  one msgpack map, one that breaks a rule above, or one with a field the
  SDK knows holding another type. What it fills is the capabilities, an
  error's code and detail, or what an event's detail carries.

**`Reply`** (SPEC.md §3.6), when the action is `ok` or `err`:

| name | from |
| --- | --- |
| `OK` | `a=ok` |
| `Re` | `re`: the action answered |
| `N` | `n`, as a number; absent when the command had none |
| `Surface` | `s`; absent when the command named none |
| `Cols`, `Rows` | `c` and `r`, as numbers, in a reply to `place` |
| `Code`, `Detail` | the error's body, when not OK |
| `Op` | `i`, as a number, in an error answering a delta: the op at fault (SPEC.md §6.1); absent otherwise |
| `Caps` | the capabilities, the body of an ok reply to `q` |
| `Hit` | what the cell holds, the body of an ok reply to `hit` (SPEC.md §9.7): `T`, the element, empty when absent, and `TX` and `TY`, each absent when the body has none; absent for any other reply |
| `Err` | the reply as an error, carrying `Code` and `Detail`; none when OK |

**`Event`** (SPEC.md §9), when the action is `ev`: `Surface` (absent for
`command` and `caps`, which belong to no surface), `Kind` (as it came,
known or not), `Target` (`t`, empty when absent), and what the detail
carries, each absent when the event does not carry it:

| name | holds | for |
| --- | --- | --- |
| `Value` | `value`, a string | `click`, `auxclick`, `change`, `input` |
| `Checked` | `checked`, a boolean | `change` |
| `Rev` | `rev`: the control's revision (SPEC.md §6.4), which `SetField` sends back | `change`, `input`, `caret` |
| `Sel` | `sel`: the selection, its anchor and its caret, in bytes of the value's UTF-8 (SPEC.md §6.4) | `input`, `caret` |
| `Fields` | `fields`: the pairs of a name and a value, in order (SPEC.md §9) | `submit` |
| `Mods` | `mods`: the modifier bits (§3.1), 0 when absent | `click`, `press`, `contextmenu`, `auxclick`, `dragstart`, `drag`, `dragend`, `gesturestart`, `wheel` |
| `Pointer` | `pointer`: the kind of pointer that made the event (§3.1, SPEC.md §9), as it came, a later kind included; empty when absent: from the keyboard, and in a 0.9 session (§3.8) | `click`, `press`, `contextmenu`, `dragstart`, `gesturestart`, `wheel`, `pointer` |
| `Finger` | `finger`: a touch's drag's finger, the lowest number no finger down held when it touched (SPEC.md §9.1); none for a mouse's or a pen's drag (−1 where the language has no absent int, since 0 is the first finger) | `dragstart`, `drag`, `dragend` |
| `Cut` | `cut`: true when something other than the release or the lift ended the drag, a gesture among them (SPEC.md §9.1); false when absent | `dragend` |
| `Gesture` | `C` and `R`, the centre's cell; `X` and `Y`, its steps, each absent when the detail has none; `Scale` and `Angle`, 1 and 0 when absent, as at the start; `Fingers`, how many are down, 0 when absent (SPEC.md §9.8). Absent when the detail has no `c` or `r` | `gesturestart`, `gesture`, `gestureend` |
| `Wheel` | `DX` and `DY`, the CSS pixels, 0 when absent; `C` and `R`, the pointer's cell; `X` and `Y`, its steps, each absent when the detail has none; `Precise`, false when absent (SPEC.md §9.9). Absent when the detail has no `c` or `r` | `wheel` |
| `Link` | `href`, and `url` if present | `click` or `auxclick` on a link |
| `FitRows` | `r` | `fit` |
| `Drag` | `c` and `r`; the steps `x` and `y` in the element dragged, and `tx` and `ty` in a `t` with `drop` (as `TX` and `TY` where the language capitalises), each absent when the detail has none (SPEC.md §9.1) | `dragstart`, `drag`, `dragend` |
| `Hover` | `c` and `r`, or `out` | `hover` |
| `Area` | `area`: `c`, `r`, `w` and `h`, the element's cells | `click`, `press`, `contextmenu`, `auxclick` |
| `Button` | `button`: the DOM's number for the button (SPEC.md §9), 1, 2, 3, 4, or 6 and up; on an `auxclick`, none when absent (0 where the language has no absent int), and on a `dragstart`, 0 when absent, the primary button's or a touch's. On a `press`, read where present, as an 0.8 host sent it for the secondary button (2), and none when absent | `auxclick`, `dragstart`, `press` |
| `C`, `R` | `c` and `r`: the pointer's cell on a `contextmenu` and at an `auxclick`'s release, each absent when the detail has none, as on a `contextmenu` from the menu key (SPEC.md §9); on a `press`, read where present, as `Button` is | `contextmenu`, `auxclick`, `press` |
| `Count` | `count`: the click's place in its run of clicks (SPEC.md §9), 2 for the second click of a double click; 1 for a click whose detail has none | `click` |
| `View` | `Areas`, from `in`: each id in view to its cells as `Area` holds them, leaving out one whose area lacks a field, and none when `in` is absent; and `AtEnd`, from `end`, false when absent (SPEC.md §9.5) | `view` |
| `Command` | `Name`, `Key` and `Text`, from `name`, `key` and `text`, each empty when absent, and `HasText`, whether `text` is there (SPEC.md §10.6); absent when the detail has no `name` | `command` |
| `Changed` | the capabilities that changed, a `Caps` with only the fields the detail has (SPEC.md §4) | `caps` |

A handler of `press` hears the primary button only (SPEC.md §9), as
before: a context menu's gesture is a `contextmenu`, and another button's
an `auxclick` at the release where the press began, each from an element
that asked, and `T` names that element. The secondary button,
`ButtonSecondary`, is heard the same way where an element takes it with
`rightbutton`: an `auxclick` or a drag, and no `contextmenu` from it even
where the element has `contextmenu` too, which then comes only from the
other gestures (a long press, a pen's barrel, Control-click, the menu
key). A handler of `dragstart` that means the primary button checks
`Button`: an element with `drag` and `aux` is dragged with the other
buttons too, and one with `drag` and `rightbutton` with the secondary,
and its `drag` and `dragend`
carry no `Button`, so the program keeps the one its `dragstart` gave until
the `dragend`. It ignores a `Button` it does not use, and the drag a
`dragstart` with one begins: a later version may report more. An
`auxclick` and a `dragstart` the SDK decodes give it any `Button` they
carry, a later one included.

A drag's `drag` and `dragend` carry no `Pointer`: they belong to their
`dragstart`, as they carry no `Button`, and a program pairs them by
`Surface` and `Finger` (SPEC.md §9.1), which is none for every drag that is
not a touch's. A `gesture` and a `gestureend` belong to their
`gesturestart` in the same way, by `Surface` and `Target`. `Scale` and
`Angle` are floats, and a detail where either is an int does not decode
(SPEC.md §3.3), as `cellcss` does not; so are `DX` and `DY`.

The detail is read by the event's kind, so a form field named `value` is in
`Fields` and nowhere else. An event of a kind the SDK does not know carries
none of these, and its payload is in its message.

**`Caps`** (SPEC.md §4) has every field SPEC.md lists: `V`, `CellCSS`
(`w`, `h`, from `cellcss`), `Scale`, `Scheme`, `Mod`, `Features`,
`Limits`, `Net`, `Edit`, `Host`, `Version`. It also answers:
- `Has(feature)`: whether `feature` is in `features` (SPEC.md §4);
- `Light`: the scheme is `light`;
- `Merge(changed)`: the capabilities with the fields of a `caps` event's
  `Changed` in place of their own, and without those it empties (`{}`,
  `[]` or `""`: absent now, SPEC.md §9).

**An error** carries `Code` and `Detail`, and its message names both.

### 3.10 Keys

What a text field does with a key (SPEC.md §10.2, §10.4), as data: so that a
program that draws its fields in cells edits them as a host edits them on a
surface, and the keymap it gives a surface is the one it uses in cells. And
which keys any other element gives the program, or scrolls with, for a
host.

- **`ParseKey(name)`** reads a key's name as SPEC.md §10.4 has hosts read
  one, with myelin key names' default reading (`Shift+Ctrl+A`, `PageDown`,
  `esc`), and returns the key, its canonical name (`ctrl+shift+a`,
  `page_down`, `escape`). A name the reading refuses (empty, `meta+a`,
  `ctrl+`, a modifier given twice) is absent, and so is one with `mod`,
  which only a Platform reads (`ReadKey`). Its vectors are that
  document's: the keys section carries its `read` vectors.
- **`Platform`** is how a host reads bindings: `Mod`, `ctrl` or `super`,
  what `mod` stands for (SPEC.md §10.4), and `Edit`, its edit shortcuts,
  from each action to its keys (SPEC.md §10.2). The zero Platform is
  `ctrl` with the default edit shortcuts. `PlatformOf(caps)` takes both
  from the capabilities, each the zero Platform's when absent.
  `DefaultEdit(mod)` returns the default edit shortcuts for a `mod`, in
  SPEC.md §10.2's order, each key read: `ctrl+shift+z` and `ctrl+y` for
  `redo` where it is `ctrl`.
- **`ReadKey(platform, name)`** reads a key's name as `ParseKey` does,
  with `mod` standing for the Platform's (myelin key names §4.4): on
  `ctrl`, `mod+s` is `ctrl+s`, and `mod+ctrl+s` is absent. The keys
  section carries that document's `mod` vectors for it.
- **`DecodeKeys(input)`** reads input from the terminal, the bytes a
  program reads, as SPEC.md §10.4 names it: a list with one entry for each
  key, its name, or absent for input that is no key it names (a mouse
  report, a sequence it does not know). It is pure: an `ESC` at the end of
  the input is `escape`, and the caller passes each read whole (a
  framework's parser has already cut the input into keys).
- **`KeyText(key)`** is the text a key types in a field (SPEC.md §10.2): a
  character's, a letter with `shift` as its capital, a space for `space`,
  and none for any other key, or a key with `ctrl`, `alt` or `super`.
- **`Keymap`** is a set of bindings, key to action, read on a Platform.
  `ParseKeymap(platform, value)` reads a `data-keys` or a `data-keys-inner`
  value as SPEC.md §10.2 has hosts read it, dropping the bindings a host
  ignores (it keeps `tab=program`, which a field honours, and
  `tab=default`), each key with `ReadKey`: on `ctrl`, `mod+a` and `ctrl+a`
  are one key, the later binding's. `Format()` writes one back: each key
  once, where it was first bound, as its last binding wrote it, with its
  last action, as `key=action`, separated by a space. A key is written
  canonical, and one written with `mod` keeps it, first (`mod+shift+z`).
- **`Resolve(platform, reader, own, inner…)`** builds the keymap a reader
  reads (SPEC.md §10.2, *Keymaps*). `own` is the focused element's
  `data-keys` value, and `inner` the `data-keys-inner` values of the
  elements from the document's root down to its parent, not its own. The
  readers:
  - **`input`** and **`multiline`**, a text field: SPEC.md's default
    keymap for the Platform's `mod`, with its `Edit`, for an `input` or for
    a multi-line field, then each `inner` value in turn, then `own`;
  - **`button`**, **`select`**, **`date`** and **`element`**, any other
    focused element, by its row of SPEC.md §10.2's first table: a button,
    an `input` of type `button`, `submit` or `reset`, a link, a summary, a
    checkbox or a radio button; a select; an `input` of a date or time
    type; and any other, such as one with a `tabindex`. No default keymap,
    then each `inner` value, then `own`. With nothing focused the reader
    is `element`, `own` is the body's `data-keys`, and `inner` the root's
    and the body's `data-keys-inner`, since the body then stands for the
    whole document;
  - **`selection`**, a selection in the document (SPEC.md §11): the
    default keymap for an `input`, then `inner` and `own` as with nothing
    focused.

  A binding its reader does not take is left out where it stands: it
  neither acts nor overrides an earlier binding of its key. A field takes
  the editing actions, `program` and `prefix`, and no scroll action. Any
  other element takes `program`, `prefix` and the scroll actions, and a
  selection `copy` from `own` only, and `program` and `prefix`. No reader
  takes an `inner` binding to `program` or `prefix` of a key it uses: the
  keys a field types (`KeyText` has text for them), and an element's row
  of the table, unmodified or with `shift` only (a button's `space` and
  `enter`; a select's characters, `space`, `enter` and its moves; a date
  input's characters and keys). Each takes `default` from `own` only. A
  binding to `default` gives the key back: `Lookup` then answers as with
  no keymap of the program's, from the default keymap or `insert`.
- **`Binds(key)`** says whether a keymap binds the key, or, for a key with
  `shift` that is not a character and that it does not bind, the key
  without `shift` (SPEC.md §10.2). A host that has two names for a press
  (SPEC.md §10.4) asks what follows of the physical one where the keymap
  binds it, and of the other one otherwise.
- **`Lookup(key)`** on a resolved keymap returns what the field does with
  the key: an action; `insert` for a key it types (`KeyText`); or absent
  when the key is not the field's (it reaches the program, or `tab` moves
  focus). On a selection's keymap it returns `copy` for a key that copies
  the selection, and is absent for every other. `tab`, `shift+tab` and `escape` are never the field's, nor a key
  bound to `program` or `prefix`, or to an action the field does not
  have. Whether `tab` reaches the program or moves focus, `Program` says.
- **`Selects(key)`** says whether the field selects with the key (SPEC.md
  §10.2, Shift selects): `Lookup` returns a move for it, and its name has
  `shift` (`shift+arrow_left`, `ctrl+shift+end`, not `alt+<`). A field
  then does the move with `Extend` (§4.6).
- **`Program(key)`** says whether a keymap gives the key to the program: it
  binds the key to `program` or `prefix`, with the fallback of `Binds`.
  `tab` it gives only on a field's keymap, from `Resolve`, where it is bound
  to `program` (SPEC.md §10.2, a field may give the program `tab`); on any
  other keymap `Program("tab")` is false, and `tab` moves focus. On
  an element that is not a text field, a host asks it of
  `Resolve(platform, reader, own, inner…)` with the element's reader, and
  with nothing focused of `element` for the body; a key it gives reaches
  the program before the element or a scroll uses it (SPEC.md §10.2, keys
  for the program).
- **`Prefix(key)`** says whether a keymap binds the key to `prefix`, with
  the same fallback without Shift. A host asks it of a key `Program` gives
  the program, and from then on gives the program every key (SPEC.md
  §10.5).
- **`Scroll(key)`** returns the scroll action a keymap binds the key to,
  with the same fallback, or absent. A key bound to `default` is none of
  `Program`'s, `Prefix`'s or `Scroll`'s: it is the element's, as with no
  keymap (SPEC.md §10.2, a key given back). A host asks it of the
  keymap it asks `Program` of, for a key the element does not use (SPEC.md
  §10.2, scrolling keys).
- **`TerminalKeys`** is the SDK's keymap, the same in every SDK (§2.3): the
  keys of Bubble Tea's text input and text area (bubbles), which a program
  that edits fields in cells is likely to share, but for `mod+a`, which
  selects all, as in a GUI field and SPEC.md's default keymap: `ctrl+a`
  on Linux, while on macOS `ctrl+a` stays the line's start. A program puts it in the
  `data-keys-inner` of an element that holds its fields, and gives its
  cells rendition the same keymap, `Resolve(platform, input, "",
  TerminalKeys)`, or `multiline` for a multi-line field.

  | action | keys |
  | --- | --- |
  | `char-backward`, `char-forward` | `arrow_left`, `ctrl+b`; `arrow_right`, `ctrl+f` |
  | `word-backward` | `alt+arrow_left`, `ctrl+arrow_left`, `alt+b` |
  | `word-forward` | `alt+arrow_right`, `ctrl+arrow_right`, `alt+f` |
  | `line-start`, `line-end` | `home`; `end`, `ctrl+e` |
  | `delete-char-backward` | `backspace`, `ctrl+h` |
  | `delete-char-forward` | `delete`, `ctrl+d` |
  | `delete-word-backward` | `alt+backspace`, `ctrl+w`, `ctrl+backspace` |
  | `delete-word-forward` | `alt+delete`, `alt+d`, `ctrl+delete` |
  | `delete-to-line-start`, `delete-to-line-end` | `ctrl+u`; `ctrl+k` |
  | `line-previous`, `line-next` | `arrow_up`, `ctrl+p`; `arrow_down`, `ctrl+n` |
  | `page-up`, `page-down` | `page_up`; `page_down` |
  | `input-start`, `input-end` | `alt+<`, `ctrl+home`; `alt+>`, `ctrl+end` |
  | `select-all` | `mod+a` |
  | `newline` | `ctrl+m` |

  It leaves `enter` to SPEC.md's default (`submit` in an `input`, `newline`
  in a multi-line field). In the order of this table, it is the value
  vectors call `terminal_keys`.

## 4. The SDK layer

What a program does with a terminal depends on the kind of program, and on
what it runs in. This section describes the components an SDK **SHOULD**
provide for the kinds of program its language is used for, by what they
do. An SDK follows its language and its frameworks in everything else.

### 4.1 Terminal

For a program that is not a full-screen framework's: a command that prints
documents and exits, one that asks a question, a chart that streams.

- **Opening.** `Open(name)` uses the process's terminal, whatever its
  standard output is (a pipe, say): `/dev/tty`, or the platform's console.
  With no terminal, it fails, and the program writes plain data.
  `New(in, out, name, size, known)` makes one from streams, for a test or an
  environment with no file (a terminal in a page). `known` tells it the
  terminal is a host, and with what capabilities, without asking.
- **One reader.** Everything the terminal sends is read once, by the
  terminal: keys, the mouse, pastes, replies and events. What a call waits
  for goes to that call (detection's answers, a fence's DA1, a numbered
  reply); everything else goes to one event stream, in the order it
  arrived. Calls that wait and a reader of events may run at once.
- **Detection.** `Detect()` runs the Detector (§3.8), once: later calls
  return the first answer. It returns when the Detector is done, so that
  nothing of the detection is left for whatever reads the terminal next. A
  terminal made with `known` answers at once.
- **A late answer.** A terminal opened with `Late` asks for one (SPEC.md
  §4). If it comes, it goes to the event stream as the host's capabilities,
  and `Detect()` answers with it from then on. Until it comes, the
  terminal stays in raw mode, or the answer would be echoed, and `Close()`
  withdraws the query (`WithdrawLate`), so that no answer reaches whatever
  reads the terminal next.
- **Requests.** `Request(build)` sends one command numbered with `N` and
  waits for its reply, 3 s at most. The number is never 1, which is the
  query's. An error reply is a reply, not an error; no reply in time is an
  error.
- **The fence.** `Fence()` waits until the terminal has taken everything
  written before it (it sends DA1, which terminals answer in order), 1 s at
  most, and returns the replies nothing read: the errors of commands sent
  with `q=1`, mostly. A program calls it before it exits.
- **The line.** `LineStart()` moves the cursor to the start of a line, where
  a surface placed at the cursor belongs: it asks where the cursor is (CPR),
  and writes CR LF if it is not at column 0. A terminal that does not
  answer within 1 s is taken to be at one.
- **Printing.** `Print(name, html, placement)` sends a document detached and
  places it at the cursor, in one write, and returns the surface's full
  name: for a document that stays in the scrollback among the program's
  output.
- **Writing.** `Send(commands…)` writes commands in one write, so that
  nothing the program prints lands between them.
- **Names.** `Surface(name)` makes a surface name of the program's own: a
  prefix, a dash and the name, made valid. The prefix is the program's name
  and a number unique on the terminal (its process id), or what the
  environment provides, so that two runs never share a surface. Every name
  is remembered (`Surfaces()`), and `DetachAll()` detaches them all.
- **Closing.** `Close()` gives the terminal back as it was (its mode, its
  file). It sends nothing but a withdrawal of a late query (above): what is
  left on the screen, and the replies not read, are the program's to
  settle first (§2.6).
- **Raw mode.** Calls that wait for the terminal put it in raw mode for
  themselves. A program that reads keys asks for it once.
- **The polyfill.** `KittyGraphics()` **MAY** be provided: whether a
  terminal that is not a host shows kitty graphics, so that a program can
  suggest a polyfill (SPEC.md §14).

### 4.2 Push environments

In some environments the SDK does not read the terminal: the environment
hands it what arrives, and writes for it. Neovim is one: its TUI parses the
input and raises `TermResponse` with each OSC, DCS and APC sequence, without
its terminator; a plugin writes to the terminal with `nvim_ui_send`. There:

- **Input is pushed.** The SDK layer receives sequences in callbacks, and
  feeds the Decoder and the Detector from them. It needs no Scanner.
- **Waiting is asynchronous.** Detection, requests and fences complete
  through callbacks or coroutines, with timers the environment provides.
- **What is detection's is advisory**: the environment may hand the same
  answer to other listeners. Others ask DA1 too, so a stale DA1 is routine,
  and the Detector's grace (§3.8) matters.
- **The terminal can change.** An editor can attach another UI, or come
  back from suspension; the SDK layer detects again, and sends documents
  again, when the environment says so.
- **A late answer is the environment's**, since detection is: one query
  asks for it for every session, and it is withdrawn while a shell has the
  terminal (on exit and suspension).
- **What the environment lacks, the SDK lacks**: CPR, `LineStart`, `Print`
  and raw mode may not exist. Surface names take a prefix the program
  supplies.
- **Batches do not nest.** Where the environment brackets its own frames in
  synchronized output, the SDK sends its commands within them, and does not
  add brackets of its own.

### 4.3 Session: a framework's adapter

For a full-screen program built on a UI framework, which redraws its frame
as its state changes. Each framework gets an adapter of its own, with the
same responsibilities.

- **Modes.** `Detecting` until the Detector decides, then `Native` (a host)
  or `Text` (not one). In `Text` the Session sends nothing, and the program
  draws everything in cells.
- **A late answer.** A Session made with `Late` asks for one (SPEC.md §4),
  through its terminal when the terminal detects. If it comes while the
  mode is `Text`, the mode becomes `Native`, ready is sent again, and then
  the program is asked for a layout: a program that asks for a late answer
  draws either rendition whenever the mode says. Until it comes, `Close()`
  withdraws the query (`WithdrawLate`).
- **Layout.** With each frame, the program says which surfaces it wants
  where: name, rectangle on the screen, an optional clip (the part of the
  screen the surface shows in, such as a scrolling region), `Keep`, `Z`,
  `Press`, `Fit`, `Hover`, `Scroll` (the axes the host scrolls the document
  along, §3.4.2), and a function that returns the document. The Session
  sends only what changed:
  - a document once, attached, with its `Scroll`, and again only when the
    host has lost it or its `Scroll` changed;
  - a placement (`PlaceAt`) when its rectangle, window or options changed,
    with `Press`, `Fit` and `Hover` only when they differ from what the
    surface has (SPEC.md §5.2), on or off; a surface clipped to nothing is
    as good as not wanted;
  - for a surface no longer wanted, `Hide` if `Keep`, else `Del`.
- **Order.** Its commands go out with the framework's frames, in the same
  stream, so that a placement lands with the cells around it.
- **Relayout.** A framework's renderer erases, scrolls and switches screens
  on its own, and a host drops placements with them (SPEC.md §5.4). The
  Session watches the output on its way out, and asks the program for a new
  layout when it sees: erase in display (2 or 3), scroll up or down, insert
  or delete lines, a new scrolling region, reverse index, leaving the
  alternate screen, or a full reset. An environment that knows its own
  redraws (an editor) uses its events instead.
- **Lost documents.** `ENOENT` in answer to a placement means the host lost
  the document (a reset, the alternate screen, a host restarting behind a
  relay). A host loses its documents together, so the Session takes them
  all as lost and sends every one again with the next layout; when only
  some went, the rest cost a few bytes. It may have lost the resources the
  documents refer to as well (a full reset ends the session, SPEC.md §2),
  and only the program knows them: the relayout it asks for says the host
  lost documents, so that the program sends its resources again before the
  documents go. One loss is reported once, however many documents it took.
  The Session numbers the first document it sends again, above any number a
  program picks, so that the host answers it (SPEC.md §3.5); the host
  answers in order, so an `ENOENT` read before that answer is for a
  placement sent before the documents went again: the same loss's, which
  gives nothing. The answer is the Session's and doesn't reach the program,
  even late. When it doesn't come within a few seconds (it went with the
  host), an `ENOENT` is a loss again. A full reset the Session reads in the
  output is a loss too once it has sent documents, though no `ENOENT`
  follows it.
- **Limits.** At most 48 surfaces by default, fewer when the host's
  `limits.surfaces` says so, or when an `EQUOTA` says so. To make room, it
  deletes the surfaces not wanted, the longest unseen first; under a limit
  the host set, a new surface waits for room.
- **Messages.** What the host sends comes back to the program as the
  framework's messages: ready (the mode is known), an event, an error (a
  command refused), an ack (the ok of a numbered command), relayout, and
  pong (a round trip timed). Everything else passes as it was.
- **Closing.** `Close()` deletes the surfaces the Session sent;
  `DetachAll()` detaches them instead, for a program that leaves them on
  the screen.

### 4.4 Test host

A host that runs inside a test, so that a program's tests need no terminal.

- **The terminal around it.** It answers what terminals answer: DA1, the
  cursor's position, the background colour, and, when asked to, the kitty
  graphics query. It tracks the alternate screen and full resets as a host
  does (SPEC.md §5.4), a full reset dropping the resources too, and keeps
  the cells the program printed. The test can make it lose every surface
  and resource without a word, as a host restarting behind a relay does. Made as a
  terminal that is not a host, it answers no hotty command, until the test
  makes it a host, as a multiplexer's terminal attaching does: then it
  answers a query that asked for a late answer and was not answered
  (SPEC.md §4), and every command after.
- **The host.** It keeps every surface's document with the delta operations,
  the morph and `a=field` of SPEC.md §6, and each control's `rev` and
  selection (SPEC.md §6.4), answers as SPEC.md §3.6 has hosts do, and
  passes the host vectors' `send`, `inspect` and `key` steps. It speaks
  the SDK's `Version` only: it answers a query that lists it with its
  capabilities, in it, and any other with `EVERSION` (SPEC.md §4). The
  vectors that need a host that speaks 0.9 as well (`speaks-0.9`,
  conformance/README.md) are skipped by it, unless it does. It lays
  nothing out: `r=auto` gets an estimate the test may replace, nothing in
  it scrolls, so a key bound to a scroll action goes on as if its keymap
  did not bind it (SPEC.md §10.2), and it sends no `view`. Nor can it
  measure where in an element the pointer is: a drag of an element with
  `data-steps` carries the steps the test gives, in it and in `t` (SPEC.md
  §9.1). A drag goes to the element the test names, which may be one with
  `drop` in its `data-on`. It keeps the focused element, and drops focus
  as SPEC.md §10.1 has a host do when the element goes away, sending only
  the `change` of a field the user edited that is still in the document:
  when a delta or a document removes it; makes it disabled, by its own
  `disabled` or a disabled fieldset's, outside that fieldset's first
  `legend`; makes it or an ancestor `inert` or `hidden`; or removes what
  made it focusable, a `tabindex`, an `href` or `contenteditable`. What
  CSS hides it cannot see.
  So it skips the vectors marked `scroll`, `steps`, `drop`, `view` and
  `layout` (conformance/README.md), which every other host passes.
- **Strict by default.** A malformed message, an `EINVAL`, or a hotty
  command other than the query sent to a terminal that is not a host fails
  the test. A lenient host records them instead.
- **The user.** It plays the user: typing, pressing a key as SPEC.md §10.2
  has a host take it, clicking, filling a field, checking a box, choosing
  an option, submitting, pressing, opening a context menu
  (`contextmenu`), pressing and releasing another button (an `auxclick`),
  dragging with any button, dragging with several fingers, each with its
  `finger`, a gesture, a wheel over an element with `wheel`, a change of
  the pointer's kind, and any event the test makes up, each with the
  `pointer` the test gives (a wheel's always `"mouse"`). As with a drag,
  it measures nothing: the test gives a wheel's or a gesture's cell and
  its step, and the host clamps the step to the element's `data-steps`.
  A click a drag makes by ending on its own element carries the drag's
  kind.
- **Inspection.** The test reads each surface: its document, an element's
  attributes and text, the focused element, whether it is detached, and
  where it is placed. It also reads the touch areas the program sent for
  each screen, which it checks as a host does and keeps per screen,
  though no finger reaches them (SPEC.md §9.6).

An SDK that does not build one **MAY** test its SDK layer against another
implementation's host, over a pipe.

### 4.5 Relay

A relay stands between programs and a host, as a multiplexer does (SPEC.md
§2): it answers the query with the host's capabilities, listing only the
features it knows and carries whole and leaving out the rest, so a
feature the host adds later never reaches a program it can't serve, gives
each
program's surfaces names of their own on the host, places them where the
program's pane is, and passes events back to the program whose surface they
came from. This section is reserved: what a relay does will be specified
here, with its own vectors.

Until then, what the protocol already asks of one:
- **Its features** are what it carries (SPEC.md §2): an allowlist, not a
  denylist of what it strips. A relay that places a program's backdrops
  (SPEC.md §5.2) without their `b` must not list `backdrop`.
- **Its own surfaces under a program's:** a relay that places surfaces
  of its own beside its programs', such as a canvas behind their panes,
  keeps its own below, by `z`, and replaces a document rather than
  creating the surface again, which keeps its place (SPEC.md §5.1, §5.2).
- **A wheel at a scrolling surface's edge** (SPEC.md §5.3): a wheel the host
  passes on over the window of a relayed surface, along an axis its document
  scrolls, the relay drops where it would turn it into keys for that program
  (its alternate screen, with no mouse tracking on); otherwise it passes it
  on as before. Over such a window the host passes on only wheels past the
  edge, and those along an axis its document does not scroll, so the relay
  needs no word of which: the placement and the document's `scroll`, which
  it relays already, are enough.
- **A `hit`** (SPEC.md §9.7) it passes on with the host's name for the
  surface, and the cell as the program counts it, since the surface's top
  left cell is the same for both. It answers `ENOENT` for a surface it
  does not hold, and passes the host's answer back to the program, its
  `n` the program's.

### 4.6 Field

A text field's value, caret and selection, edited as SPEC.md §10.2's actions
edit them, for a program that draws its fields in cells (the second
rendition, §2.5).

- **The field.** `Value`, `Caret` (a count of characters, grapheme
  clusters, from the start), `Anchor`, `Multiline`, `Password`, and
  `Rows`, the rows it shows (1 when absent), for `page-up` and
  `page-down`.
- **The selection** runs from `Anchor`, the end where it began, to the
  caret (SPEC.md §10.2). Nothing is selected when `Anchor` is absent or at
  the caret. `Selection()` returns its start and end, equal when nothing
  is selected, for the rendition to draw. `Select(anchor, caret)` sets
  both, for a selection the user makes with the pointer, and
  `Select(p, p)` puts the caret at `p` with nothing selected. In a
  language whose absence is a zero value, the anchor is kept behind
  `Anchor()` and `Select`, since a field's zero anchor would select.
- **Rows are lines.** A field in cells does not wrap: each line is a row,
  and the place along it is a count of characters. A rendition that wraps
  moves the caret between its rows itself.
- **`Do(action)`** does an action, and reports whether the value changed,
  so the program knows when to report an `input`. A selection comes first,
  as SPEC.md §10.2 says: a delete deletes it, a move starts from its start
  or its end and ends it, and `select-all` selects the whole value.
  `submit` and `program` are the program's, and change nothing, as are
  `copy`, `cut`, `paste`, `undo` and `redo`: a Field holds no clipboard
  and no history, which a program that keeps them adds around it.
- **`Extend(action)`** does a move as Shift does it (SPEC.md §10.2): the
  anchor stays, or is set where the caret is, and the caret moves from
  where it is. An action that is not a move is `Do`'s.
- **A run of row moves,** `line-previous`, `line-next`, `page-up` and
  `page-down`, with `Do` or `Extend`, keeps the place along the row it
  started from; any other action, typing, or `Select` ends it.
- **`Type(text)`** types text at the caret, in place of the selection.
- **`Key(keymap, key)`**, where an SDK has it, does what the keymap says
  of the key: types its `KeyText`, does its action, or, when
  `Selects(key)`, extends with it.

An SDK whose language has no grapheme segmentation **MAY** count code
points instead, CR LF still one, and says so (§5.3); it then skips the
vectors that require `graphemes`. So does its `DecodeKeys`, for a character
of more than one code point.

### 4.7 Context menus

An element's own menu, opened by a `contextmenu` (SPEC.md §9) or by a key,
for a program that draws it as a surface of its own. The host draws no
menu: SPEC.md Appendix B says why.

- **The element opts in** with `contextmenu` in its `data-on`; one that
  also has `rightbutton` takes the secondary button itself, and opens its menu
  from an `auxclick` with `Button` 2 where it wants one. A
  `contextmenu` then names it in `T`, with `Area`, and the pointer's cell
  in `C` and `R`.
- **The keys that open a menu** are `shift+f10` and `context_menu`
  (`ParseKey`, §3.10). They reach the program as keys while a surface has
  the keyboard (SPEC.md §10.2), and the last `focus` names the element, so
  the SDK opens that element's menu on them, at its `Area`. A host may
  report them as a `contextmenu` without `C` and `R` instead, which the SDK
  opens at the `Area` too. The same keys open it in the cells, so a
  program opens menus from the keys everywhere, and from the gesture on a
  surface.
- **The menu is a surface** placed at a higher `z` (SPEC.md §5.2), at the
  press's cell or under the element's `Area`, as a select's list is drawn
  where a host shows none (SPEC.md §10.2). The SDK gives it the keyboard
  (`Focus`), so its arrows, Enter and Escape are the program's. A click
  elsewhere takes the keyboard from it, and the `blur` that follows closes
  it. Nothing in it is new on the wire.
- **What makes it feel native:**
  - **Open at once.** The program sends the menu's document once and
    leaves it unplaced, or hidden (`a=hide`, SPEC.md §5.4) between uses, so
    a press costs a placement. Not detached: a detached surface takes no
    input (§5.5).
  - **Fit.** It opens below and to the right of the pointer, and flips to
    the other side where the grid ends.
  - **Keys.** Type-ahead picks an item, the right arrow opens a submenu,
    and the Menu key or Escape closes it.
  - **One at a time.** One menu is open, and it closes on a pick, on a
    `blur`, on Escape, on a scroll and on a resize.
- **The click that closes a menu** does what it would have done, as the
  host acts on it (a focus, a checkbox). The SDK drops only what the
  program would have done with it.

### 4.8 Fingers, gestures and the wheel

For a program that adapts to how the user points, and for one that draws
a canvas: a whiteboard, a map, a chart that pinches, a mixer's faders.
None of it is a capability (SPEC.md Appendix B): an SDK reads what comes,
and the program never asks what the device is.

- **The look is CSS.** A document that wants thumb-sized targets on a
  phone writes `@media (pointer: coarse)`, and one that hides what only a
  hover shows writes `@media (hover: none)`. The host that draws the
  surface matches its own device, in every session (SPEC.md §8), so the
  SDK adds nothing and sends nothing.
- **The kind is the last `pointer` heard.** A program in the cells keeps
  the kind from the `pointer` events (SPEC.md §9), `PointerMouse` until
  the first, and draws its hints by it: "hold for options" for a touch,
  "right-click" for a mouse. A session (§4.3) that keeps the capabilities
  keeps the kind beside them. A kind it does not know is `PointerMouse`.
- **The words of `data-on`.** An element asks with the words, and an SDK
  that writes attributes for the program writes them together, space
  separated, in any order:
  - `drag`, with `touch-action: none` or a `pan-*` value that leaves the
    other way to the page (SPEC.md §9.1), for what a finger drags;
  - `gesture` for what two fingers pinch, turn or pan (SPEC.md §9.8), on
    the element itself or one around it;
  - `wheel` for what takes the wheel in pixels (SPEC.md §9.9);
  - and `click`, `aux` or `rightbutton` as before. A whiteboard is
    `data-on="drag gesture wheel aux"` with `touch-action: none`.
- **Drags by finger.** A program keys a drag by `Surface` and `Finger`,
  none for the mouse's and a pen's, from its `dragstart` to its
  `dragend`, and gives each finger its own stroke or fader. A `dragend`
  with `Cut` was cut short: a whiteboard drops the stroke, where a
  `dragend` without it is the user letting go.
- **Gestures by element.** A `gesturestart` names the element in
  `Target`; each `gesture` after it carries the whole transform since the
  start, never a step, so a program that misses one loses nothing: it
  applies `Scale` about the centre where the gesture began, `Angle`, and
  the centre's move, to the view it had at the start.
- **The wheel.** `DX` and `DY` are CSS pixels, summed over a frame. A
  canvas zooms on a `Wheel` that is not `Precise` and on one with
  `ModCtrl` (a browser's touchpad pinch), and pans on a `Precise` one, by
  its own sensitivity: a touchpad's two fingers are slow at 1:1.
- **A surface's size follows `caps`.** `cellcss` can change in a
  session (SPEC.md §4): with the terminal's font, which the user zooms,
  and on some hosts when the window moves to a display of another
  density. A program that sizes a surface, its steps or a canvas in pixels
  from `CellCSS` takes the new one from each `caps` (`Caps.Merge`) and
  draws again; one that sized them from the first capabilities draws off
  the pointer from then on.
- **A 0.9 host** sends none of this (§3.8). A canvas there offers keys or
  controls to zoom and pan, and a stroke ends at the second finger.

## 5. Conformance

### 5.1 Vector sections

`conformance/vectors.json` holds the vectors of hosts and of SDKs
([`conformance/README.md`](https://hotty.neuroplast.io/docs/conformance)):

| section | checks | runs it |
| --- | --- | --- |
| `vectors` | a host: replies, documents, events | hosts, test hosts (§4.4) |
| `wire` | decoding a stream of commands | hosts and SDKs |
| `build` | the builders, their defaults and options (§3.4) | SDKs |
| `encode` | `Encode`: cleaning, chunks, compression (§3.3) | SDKs |
| `decode` | the Decoder and the messages (§3.6, §3.9) | SDKs |
| `scan` | the Scanner (§3.7) | SDKs |
| `detect` | the Detector (§3.8) | SDKs |
| `keys` | key names and `DecodeKeys` (§3.10, SPEC.md §10.4) | hosts and SDKs |
| `keymap` | `ParseKeymap`, `Resolve`, `Lookup`, `Program`, `Scroll` and `TerminalKeys` (§3.10, SPEC.md §10.2) | hosts and SDKs |
| `edit` | the actions on a value: a Field (§4.6) | hosts, and SDKs with a Field |

An SDK conforms when it meets every **MUST** of this document and passes
every SDK section, `edit` where it provides a Field.

### 5.2 Runners

- **Names in the vectors are canonical**, in snake case: a runner maps
  `set_text`, `keep_cursor` and `fit_rows` to its own names. That mapping is
  what keeps SDKs aligned.
- **`requires`.** A vector that needs something an SDK may not have yet
  names it in `requires`. A runner declares what it implements, and skips
  the vectors whose `requires` it lacks (the host sections' `speaks-0.9`
  among them, for a test host that speaks 0.10 only), so the list of what an
  SDK lacks is its runner's, not a list kept by hand.
- **A copy, checked.** An SDK keeps a copy of `vectors.json` in its tests,
  a way to refresh it from a checkout of this repository, and a test that
  fails when it differs from the spec's, where a checkout is at hand. The
  runner checks that the vectors' version is the SDK's.

### 5.3 What an SDK lacks

An SDK says in its own documentation what of this document it does not
do: a component of §4 it does not provide, a Field and `DecodeKeys` that
count code points (§4.6), a name its language makes it spell otherwise
(§2.2). The vectors it skips are its runner's `requires` (§5.2), so the
list of what it lacks is kept by its tests, not by hand.

---

## Appendix A. Names

The canonical names, in each language's case.

| canonical | Go | Python | Lua | Rust | TypeScript |
| --- | --- | --- | --- | --- | --- |
| `Encode` | `hotty.Encode` | `hotty.encode` | `hotty.encode` | `hotty::encode` | `encode` |
| `Query` | `hotty.Query` | `query` | `query` | `query` | `query` |
| `Doc` | `Doc` | `doc` | `doc` | `doc` | `doc` |
| `Place`, `PlaceAt` | `Place`, `PlaceAt` | `place`, `place_at` | `place`, `place_at` | `place`, `place_at` | `place`, `placeAt` |
| `Hide`, `Delta` | `Hide`, `Delta` | `hide`, `delta` | `hide`, `delta` | `hide`, `delta` | `hide`, `delta` |
| `Op` and its fields | `hotty.Op{Op, Target, Key, Value}` | `Op(op, target, key, value)` | a table with those keys | `Op { op, target, key, value }` | `{ op, target, key, value }` |
| `Morph`, `Inner`, `Replace`, `Append`, `Prepend`, `Before`, `After`, `Remove`, `Attr`, `Unattr`, `Text`, `Var`, `RevealOp`, `ScrollOp` | `hotty.OpMorph`, `hotty.OpInner`, …, `hotty.OpText`, `hotty.OpVar`, `hotty.OpReveal`, `hotty.OpScroll` (`hotty.Text` is a detector's state) | `morph`, `inner`, …, `text`, `var`, `reveal_op`, `scroll_op` | as Python | as Python | `morph`, `inner`, …, `text`, `varOp`, `revealOp`, `scrollOp` |
| `SetText`, `SetVar`, `SetAttr`, `RemoveAttr`, `MorphTo`, `Reveal`, `Scroll` | the same, but `Scroll` is `hotty.ScrollBy` (`hotty.Scroll` is `Doc`'s option) | `set_text`, `set_var`, `set_attr`, `remove_attr`, `morph_to`, `reveal`, `scroll` | as Python | as Python | `setText`, `setVar`, `setAttr`, `removeAttr`, `morphTo`, `reveal`, `scroll` |
| `SetField`, `FieldChange` and its fields | `hotty.SetField`, `hotty.FieldChange{Rev, Value, From, To, Text, Sel, Checked}`, absent as nil pointers | `set_field`, `FieldChange(rev, value, from_, to, text, sel, checked)`, absent as `None` | `set_field`, a table with those keys | `set_field`, `FieldChange { rev, value, from, to, text, sel, checked }`, each but `rev` an `Option` | `setField`, `{ rev, value, from, to, text, sel, checked }`, absent as `undefined` |
| `Res`, `DelRes` | `Res`, `DelRes` | `res`, `del_res` | `res`, `del_res` | `res`, `del_res` | `res`, `delRes` |
| `Del`, `DelAll` | `Del`, `DelAll` | `delete`, `del_all` | `del`, `del_all` | `del`, `del_all` | `del`, `delAll` |
| `Detach`, `Focus`, `Blur`, `Sync` | the same | `detach`, `focus`, `blur`, `sync` | as Python | as Python | `detach`, `focus`, `blur`, `sync` |
| `WithdrawLate` | `hotty.WithdrawLate` | `withdraw_late` | `withdraw_late` | `withdraw_late` | `withdrawLate` |
| `Edit`, `Menu`, `MenuShow`, `MenuHide` | `hotty.Edit`, `hotty.Menu`, `hotty.MenuShow`, `hotty.MenuHide` | `edit`, `menu`, `menu_show`, `menu_hide` | as Python | as Python | `edit`, `menu`, `menuShow`, `menuHide` |
| `Hit`, and `Reply`'s `Hit` with its fields | `hotty.Hit`; `Reply.Hit`, a `*hotty.HitCell{T, TX, TY}`, `TX` and `TY` as `*int` | `hit`; `Reply.hit`, a `HitCell(t, tx, ty)`, absent as `None` | `hit`; `reply.hit`, a table with those keys | `hit`; `Reply.hit`, an `Option<HitCell { t, tx, ty }>`, each step an `Option` | `hit`; `reply.hit`, `{ t, tx?, ty? }` |
| `Touch`, `TouchArea` and its fields | `hotty.Touch`, `hotty.TouchArea{C, R, W, H, Drag}` | `touch`, `TouchArea(c, r, w, h, drag)` | `touch`, tables with those keys | `touch`, `TouchArea { c, r, w, h, drag }` | `touch`, `{ c, r, w, h, drag }` |
| `MenuTree`, `MenuItem` and their fields | `hotty.MenuTree{Label, Items}`, `hotty.MenuItem{Key, Label, Name, Enabled, Items}` | `MenuTree(label, items)`, `MenuItem(key, label, name, enabled, items)` | tables with those keys | `MenuTree { label, items }` | `{ label, items }` |
| `Placement` and its fields | `Placement{Cols, Rows, Window, Z, Press, Fit, Hover, KeepCursor, Backdrop}` | `Placement(cols, rows, window, z, press, fit, hover, keep_cursor, backdrop)` | a table with those keys | `Placement { cols, rows, … }` | `{ cols, rows, window, z, press, fit, hover, keepCursor, backdrop }` |
| a flag on, off or absent (`Press`, `Fit`, `Hover`) | `hotty.On`, `hotty.Off`, the zero value | `True`, `False`, `None` | `true`, `false`, `nil` | `Option<bool>` | `true`, `false`, `undefined` |
| `N`, `Q`, `Detached`, `Scroll`, `Plain`, `Late` | `hotty.N(n)`, `hotty.Q(q)`, `hotty.Detached()`, `hotty.Scroll(axes)`, `hotty.Plain()`, `hotty.Late()` | `n=`, `q=`, `detached=`, `scroll=`, `plain=`, `late=` | `{ n = …, q = …, detached = …, scroll = …, plain = …, late = … }` | builder methods | `{ n, q, detached, scroll, plain, late }` |
| `ScrollVertical`, `ScrollHorizontal` | `hotty.ScrollVertical`, `hotty.ScrollHorizontal` | `SCROLL_VERTICAL`, `SCROLL_HORIZONTAL` | `SCROLL_VERTICAL`, `SCROLL_HORIZONTAL` | `SCROLL_VERTICAL`, `SCROLL_HORIZONTAL` | `SCROLL_VERTICAL`, `SCROLL_HORIZONTAL` |
| `Decoder.Feed`, `Invalid`, `Max` | `(*Decoder).Feed`, `.Invalid`, `.Max` | `Decoder.feed`, `.invalid`, `.max` | `decoder:feed`, `.invalid`, `hotty.decoder({ max = … })` | `Decoder::feed`, `.invalid`, `.max` | `Decoder.feed`, `.invalid`, `.max` |
| `DefaultMax` | `hotty.DefaultMax` | `DEFAULT_MAX` | `DEFAULT_MAX` | `DEFAULT_MAX` | `DEFAULT_MAX` |
| `Scanner`, `Detector` | `Scanner`, `Detector` | `Scanner`, `Detector` | `scanner`, `detector` | `Scanner`, `Detector` | `Scanner`, `Detector` |
| `Holding`, `InSequence` | `(*Scanner).Holding`, `.InSequence` | `Scanner.holding`, `.in_sequence` | `scanner:holding`, `:in_sequence` | `Scanner::holding`, `::in_sequence` | `Scanner.holding`, `.inSequence` |
| `Reply`, `Event`, `Caps` | `Reply`, `Event`, `Caps` | the same | tables with the fields in snake case | the same | the same |
| `FitRows`, `Mods`, `CellCSS` | `FitRows`, `Mods`, `CellCSS` | `fit_rows`, `mods`, `cell_css` | as Python | as Python | `fitRows`, `mods`, `cellCss` |
| `Button`, `C`, `R` | `Button`, `At` (`*hotty.Cell{Col, Row}`, nil when absent) | `button`, `c`, `r` | as Python | as Python | `button`, `c`, `r` |
| `ModShift`, `ModAlt`, `ModCtrl`, `ModSuper` | `hotty.ModShift`, … | `MOD_SHIFT`, … | `MOD_SHIFT`, … | `MOD_SHIFT`, … | `MOD_SHIFT`, … |
| `ButtonPrimary`, `ButtonMiddle`, `ButtonSecondary`, `ButtonBack`, `ButtonForward` | `hotty.ButtonPrimary`, … | `BUTTON_PRIMARY`, … | `BUTTON_PRIMARY`, … | `BUTTON_PRIMARY`, … | `BUTTON_PRIMARY`, … |
| `PointerMouse`, `PointerPen`, `PointerTouch` | `hotty.PointerMouse`, … | `POINTER_MOUSE`, … | `POINTER_MOUSE`, … | `POINTER_MOUSE`, … | `POINTER_MOUSE`, … |
| `Pointer`, `Finger`, `Cut` | `Pointer`, `Finger` (`HasFinger` false when absent), `Cut` | `pointer`, `finger` (`None` when absent), `cut` | as Python | as Python, `finger` an `Option` | `pointer`, `finger` (`undefined` when absent), `cut` |
| `Gesture`, `Wheel` and their fields | `Event.Gesture`, a `*hotty.Gesture{Col, Row, X, Y, HasX, HasY, Scale, Angle, Fingers}`; `Event.Wheel`, a `*hotty.Wheel{DX, DY, Col, Row, X, Y, HasX, HasY, Precise}`, `HasX` and `HasY` false when absent, as `hotty.Drag`'s | `e.gesture()`, `Gesture(c, r, x, y, scale, angle, fingers)`; `e.wheel()`, `Wheel(dx, dy, c, r, x, y, precise)`, absent as `None` | tables with those keys | `Option<Gesture>`, `Option<Wheel>`, each step an `Option` | `{ c, r, x?, y?, scale, angle, fingers }`, `{ dx, dy, c, r, x?, y?, precise }` |
| a message as a `Reply` or an `Event`, `Err` | `hotty.ReplyOf(m)`, `hotty.EventOf(m)`, `hotty.Err(r)` | `m.reply()`, `m.event()`, `r.err()` | `m:reply()`, `m:event()`, `r:err()` | methods | `m.reply()`, `m.event()`, `r.err()` |
| `Has`, `Light`, `Merge` | `hotty.HasFeature(c, feature)` (`hotty.Has` asks a control), `hotty.Light(c)`, `hotty.Merge(c, changed)` | `c.has(feature)`, `c.light()`, `c.merge(changed)` | `c:has(feature)`, … | methods | `c.has(feature)`, … |
| `Command`, `Changed` | `Event.Command`, `Event.Changed` | `e.command`, `e.changed` | as Python | methods | `e.command`, `e.changed` |
| a control's `get` and `with` | `hotty.Get(ctl, k)`, `hotty.With(ctl, k, v)` | the SDK's own | the SDK's own | the SDK's own | the SDK's own |
| `ParseKey`, `DecodeKeys`, `KeyText` | `hotty.ParseKey`, `hotty.DecodeKeys`, `hotty.KeyText` | `parse_key`, `decode_keys`, `key_text` | as Python | as Python | `parseKey`, `decodeKeys`, `keyText` |
| `Keymap`, `ParseKeymap`, `Resolve`, `Binds`, `Lookup`, `Program`, `Prefix`, `Scroll`, `Format` | `hotty.Keymap`, `hotty.ParseKeymap`, `hotty.Resolve`, `(Keymap).Binds`, `.Lookup`, `.Program`, `.Prefix`, `.Scroll`, `.Format` | `Keymap`, `parse_keymap`, `resolve`, `Keymap.binds`, `.lookup`, `.program`, `.prefix`, `.scroll`, `.format` | `parse_keymap`, `resolve`, `keymap:binds`, `:lookup`, `:program`, `:prefix`, `:scroll`, `:format` | as Python | `Keymap`, `parseKeymap`, `resolve`, `.binds`, `.lookup`, `.program`, `.prefix`, `.scroll`, `.format` |
| `TerminalKeys` | `hotty.TerminalKeys` | `TERMINAL_KEYS` | `TERMINAL_KEYS` | `TERMINAL_KEYS` | `TERMINAL_KEYS` |
| `Platform`, `PlatformOf`, `DefaultEdit`, `ReadKey` | `hotty.Platform{Mod, Edit}`, `hotty.PlatformOf`, `hotty.DefaultEdit`, `(Platform).ReadKey` | `Platform(mod, edit)`, `platform_of`, `default_edit`, `read_key` | a table `{ mod = …, edit = … }`, `platform_of`, `default_edit`, `read_key` | as Python | `{ mod, edit }`, `platformOf`, `defaultEdit`, `readKey` |
| `Field`, `Do`, `Type` | `Field`, `(*Field).Do`, `.Type` | `Field`, `.do`, `.type` | `field`, `:do_action` (`do` is a keyword), `:type` | `Field`, `do_action`, `type_text` | `Field`, `.do`, `.type` |

In the vectors, every name is in snake case: `place_at`, `keep_cursor`,
`fit_rows`, `cell_css`.

## Appendix B. Rationale

- **Why two layers.** What goes on the wire is the same in every program;
  how a program reads its terminal is not. A pure wire layer can be checked
  as data, in every language, by the same vectors, and an environment that
  does its own I/O (an editor, a page) still uses all of it.
- **Why shared defaults.** A program ported from one language to another
  should not start filling its input with replies, or wait a different time
  for an answer. Defaults are behaviour, and behaviour is what the program's
  user sees.
- **Why the Scanner and the Detector are in the wire layer.** Both are
  pure: bytes in, segments out; inputs and times in, a decision out. Both
  are where SDKs went wrong first: a reply cut across two reads read as
  keys, a DA1 left for the shell, a terminal that is a host taken for one
  that is not. As state machines with vectors, they are right once, in every
  language.
- **Why `Doc` and `Place` answer errors and nothing else does.** A refused
  document or placement leaves a hole in the program's screen, which the
  program must hear about; a delta that names a missing element is the
  program's bug, which a test finds. Asking for every reply would fill the
  input with them.
- **Why a `Q` given wins over `N`.** Options in most languages have no
  order: keyword arguments, a table, a struct. The rule that needs no order
  is the one every SDK can follow.
- **Why the SDK has a keymap of its own.** SPEC.md's default keymap is
  small, so that a program that names no keys loses none. A program that
  edits fields in cells, though, has a keymap already, usually its
  framework's, and wants its surfaces' fields to edit the same. Bubble
  Tea's is a common one among programs that edit fields in cells; one
  keymap shared by every SDK keeps a program the same in every language
  (§2.3).
- **Why a body decodes whole or not at all.** A codec generated from the
  types (Go's msgp, Rust's serde) stops at the first field of the wrong
  type. A rule every SDK can keep is the one such a codec keeps, and a
  host that sends a wrong type fails the vectors instead.
- **Why presentation is out.** hotty carries any HTML. How one program
  presents its content is that program's choice, and an SDK that builds
  documents would make it for every program, in each language differently.

## Appendix C. Moving from 0.2

Informative. hotty 0.3 breaks 0.2 (SPEC.md §15), as a fold of what 0.2
grew. A program and its SDK move together, and its tests' fake hosts with
them. These are the places a port goes wrong:

- **A delta is a list of ops.** `Delta(surface, op, target, key,
  payload)` became `Delta(surface, ops…)`, the ops built by `Text`,
  `Attr` and the rest (§3.4.3). A frame's changes to one surface go in one
  delta, which the host applies as one; the one-op shorthands (`SetText`,
  …) are still there for a single change.
- **The flags stay on the surface.** `p`, `f` and `v` hold until the
  program turns them off (SPEC.md §5.2). A program that placed a surface
  without `Press` to stop its presses now sends `Press: Off`. A Session
  (§4.3) sends them when they change.
- **Ask by feature.** `Supports`, `Sends`, `Drags`, `Hovers`, `Views` and
  the capabilities' `Passthrough`, `Scroll`, `Steps`, `Drop`, `Count` and
  `Menu` are gone: everything a 0.3 host does, it does, and `HasFeature(c,
  feature)` answers for `passthrough`, `hover`, `count` and `menu`.
- **The cell is in CSS pixels.** `Cell`, in device pixels, is gone, and
  `CellCSS` is the host's own field. There is no `resize`: a new font is a
  `caps` event with the new `CellCSS`, which `Merge` folds in.
- **A submit's fields are pairs**, in order: two fields of one name are
  two pairs. Code that looked a name up in a map looks it up in the pairs.
- **Modifiers are bits.** A drag's `Keys` list is `Mods`, and clicks and
  presses carry it too: `Mods & ModShift` for Shift.
- **An empty target is absent.** `Target` reads the same, empty, but a
  host's `t=` with nothing after it is gone from the wire, and a fake host
  that sends one is out of date.
- **No key sequences in the host.** `data-key` and the capabilities'
  `Keys` are gone. A program binds its leader to `prefix` (SPEC.md §10.5),
  reads the keys after it, and sends `a=focus` when the sequence ends. Its
  menu goes to `Menu` and `MenuShow`, as in the cells.
- **A menu is a tree.** `MenuLevel` is `MenuTree`, with no `Seq`, and an
  item has a `Name` and its own `Items` in place of `Group`. A program
  sends its tree when what it can do changes, `MenuShow(seq)` for each key
  of a sequence, and `MenuHide()` when it ends, where 0.2 sent a level for
  each key and hid it with an empty `Menu`. A pick, from a
  multiplexer's palette or an agent, reaches it as `command` with the
  item's name.
- **Errors only, unless asked.** A builder sends no `q` where the wire
  implies it, and `N(n)` alone asks for the reply. A fake host must answer
  a command without `q` only with an error.

## Appendix D. Moving from 0.3

Informative. hotty 0.4 breaks 0.3 (SPEC.md §15): it names keys as *myelin
key names* 0.1 does (SPEC.md §10.4). A program and its SDK move together,
and its tests' fake hosts with them. These are the places a port goes
wrong:

- **Every key's name changes.** `Control+a` is `ctrl+a`, `Meta` is
  `super`, `ArrowLeft` is `arrow_left`, `PageDown` is `page_down`, `Enter`
  is `enter` and `Space` is `space`. A program's `data-keys`, its menu's
  keys and `seq`, and the keys its tests expect all move.
- **`mod`, not `Mod`.** A binding writes `mod+s`, and one with `mod` and
  `ctrl` or `super` as well is refused. The capabilities' `mod` is
  `"ctrl"` or `"super"`, and `edit` names its keys the new way. `ParseKey`
  refuses `mod`: a binding read on a host's Platform goes through
  `ReadKey`.
- **A capital is no Shift.** A host reads `G` as `g`, so a binding of
  Shift with a letter writes `shift+g`, and a 0.3 `Control+A` is
  `ctrl+shift+a`. The text a key types is its `KeyText`: `shift+a` types
  `A`.
- **Shift falls back only from a key that is not a character.** An
  unbound `shift+arrow_left` is looked up as `arrow_left`, but `shift+g`
  is a key of its own.
- **A press can have two names.** A host that sees the key itself names
  it by its place and by what it types (`ctrl+key_w`, `ctrl+w`), and
  looks the place up first (`Binds`). A host that reads bytes names what
  they carry.
- **The modifier bits keep their values,** under myelin's names:
  `ModControl` is `ModCtrl`, and `ModMeta` is `ModSuper`.

## Appendix E. Moving from 0.4

Informative. hotty 0.5 breaks 0.4 (SPEC.md §15): a program changes a
control the user may be changing with `a=field` (SPEC.md §6.4), and the
events of controls carry its revision. A program and its SDK move
together, and its tests' fake hosts with them. These are the places a port
goes wrong:

- **`input` and `change` carry `rev`.** A test that expects their detail
  exactly expects `rev` too: 1 after the first edit of a field, and one
  more for each edit after it. A select's pick sends one `rev` in its
  `input` and its `change`, and a radio group counts together.
- **Complete with `SetField`, not a new element.** A program that replaced
  a field with one of a new id, so that its value would reach it while
  focused, sends `SetField` with the `rev` it last heard. The caret goes
  after the text, the history stays, and the field keeps its id.
- **`ESTALE` is not a failure.** The user typed first. The program does
  nothing, and decides again from the `input` it hears next: it does not
  resend with the `rev` the detail names, which would overwrite what the
  user did.
- **A field's caret starts at its end,** when the field enters the
  document and when a morph's value reaches it, and `a=focus` leaves a
  field's selection where it was.
- **Positions are bytes of UTF-8,** in `SetField` and in `Sel`, not
  characters: `é` is 2. A language whose strings count UTF-16 converts.

## Appendix F. Moving from 0.5

Informative. hotty 0.6 breaks 0.5 (SPEC.md §15): a 0.6 host answers a 0.5
query with `EVERSION`. A program and its SDK move together, and its tests'
fake hosts with them. These are the places a port goes wrong:

- **`Version` is `"0.6"`,** and the runner's copy of the vectors moves with
  it (§5.2).
- **`Backdrop` is the placement's, not the surface's.** It goes out as
  `b=1` when true and not at all when false, as `KeepCursor` does, never as
  `b=0`. Unlike `Press`, `Fit` and `Hover`, it has no absent: each
  placement says it again.
- **Ask before placing a backdrop.** A host without the feature `backdrop`
  ignores `b` and draws the surface over the text (SPEC.md §5.2), so a
  program checks `Has(backdrop)` and draws in cells without it.
- **A backdrop never has the keyboard.** `Focus` on one is `EINVAL`, and a
  focused surface placed as one sends `blur`. Hidden, a surface is no
  backdrop, and its next placement decides (SPEC.md §5.2). A test host
  that lists the feature answers so.
- **Enter commits a text field, in a form or not** (SPEC.md §9). A test
  host's `submit` commits the field first, as a host's does: Enter in an
  `input` the user changed sends `change`, and in a form `change` then
  `submit`. A program that read a lone field only on `blur` hears it on
  Enter too.

## Appendix G. Moving from 0.7

Informative. hotty 0.8 breaks 0.7 (SPEC.md §15) only for a host or a
program that speaks one of them: the SDKs speak both (§3.8). These are the
places a port goes wrong:

- **`Version` is `"0.8"`, `Versions` `["0.7", "0.8"]`,** and the runner's
  copy of the vectors moves with them (§5.2).
- **`Scroll` and `ScrollOp`** take a scroll action as their value, which
  always goes out, even where `reveal`'s `where` would not (§3.4.3). An
  empty target sends no `t`: the root scrolls.
- **`Hit` is a query.** It implies quiet level 0 with `n` or without, so
  it carries `q` only when `Q` gives another level (§3.4.2), and its
  answer is `Reply.Hit`: `T` empty for no element, `TX` and `TY` absent
  where the body has none (§3.9).
- **`Program("tab")`** is true only on a field's keymap, from `Resolve`,
  bound to `program`; on every other keymap it is false (§3.10).
  `ParseKeymap` keeps `tab=program` and `tab=default`, so `Format` writes
  them.
- **`default`** is an action `ParseKeymap` keeps. In a resolved keymap,
  `Lookup` of a key bound to it answers from the default keymap, or
  `insert`; outside a field it is none of `Program`'s, `Prefix`'s or
  `Scroll`'s (§3.10).
- **A test host** that speaks 0.8 only skips the `speaks-0.7` vectors
  (§4.4).

## Appendix H. Moving from 0.8

Informative. hotty 0.9 breaks 0.8 (SPEC.md §15) only for a host or a
program that speaks one of them: the SDKs speak both (§3.8). These are the
places a port goes wrong:

- **`Version` is `"0.9"`, `Versions` `["0.8", "0.9"]`,** and the runner's
  copy of the vectors moves with them (§5.2). A 0.7 host is no longer
  one the SDK takes.
- **The keymap vectors' shape:** a `Resolve` vector names its `reader`,
  `own` and `inner` in place of `multiline` and `keys`
  (conformance/README.md, *Keymap*), so every runner's reading of them
  changes.
- **`Resolve`'s readers** are `input`, `multiline`, `button`, `select`,
  `date`, `element` and `selection`, each as its language names a closed
  set of values (§3.10). A reader outside them is an error, raised or
  returned as its language does, never read as one of them: in the
  languages without a type to check it, Lua and TypeScript among them, an
  0.8 call such as `resolve(platform, false, …)` fails loudly.
  `input` and `multiline` take the place of the `multiline` flag that
  `Resolve` took before 0.9.
- **`Resolve` takes its keymaps as `own` and `inner`** (§3.10), and
  `default` from `own` only. In an 0.8 session a host passes each
  `data-keys` as both (HOST.md of 0.9, §5.1).
- **The rest of §3.10:** `ParseKeymap` reads `data-keys-inner` values
  too. On a selection's keymap, `Lookup` returns `copy` for a key that
  copies, and none for every other. A host asks of an element that is
  not a text field through `Resolve` with its reader, and with nothing
  focused of `element` for the body, no longer of its joined `data-keys`.
- **`contextmenu` and `auxclick`** are event kinds (§3.1), with `Area`,
  `C`, `R` and `Button` (§3.9), and an `auxclick` carries `Value`, `Mods`
  and `Link` as a `click` does. The `Button` constants, from
  `ButtonPrimary` to `ButtonForward`, name the DOM's numbers (§3.1). A
  `press` still reads `Button`, `C` and `R` where present, which only an
  0.8 session's secondary press sends (HOST.md of 0.9, §5.2). A `dragstart`
  handler that means the primary button checks `Button` (§3.9).
- **A Field** (§4.6) sets its keymap in the `data-keys-inner` of the
  element that holds the fields, and its cells use `Resolve(platform,
  input, "", TerminalKeys)`, or `multiline` for a multi-line field.
- **A test host** (§4.4) drops focus when the focused element goes away,
  sending only the `change` of a field the user edited that is still in
  the document. It skips the `layout` vectors, and the `speaks-0.8` ones
  where it speaks 0.9 only. Its test events open a context menu
  (`contextmenu`), press and release another button (`auxclick`), and
  drag with any button.
- **A relay** (§4.5) drops a wheel over a relayed surface's window, along
  an axis its document scrolls, where it would become keys for the
  program behind it.
- **Context menus** (§4.7): the word an element opts in with is
  `contextmenu`, with `secondary` beside it for an 0.8 session, and the
  menu key may come as a `contextmenu` with no `C` and `R`.

## Appendix I. Moving from 0.9

Informative. hotty 0.10 breaks 0.9 (SPEC.md §15) only for a host or a
program that speaks one of them: the SDKs speak both (§3.8). These are the
places a port goes wrong:

- **`Version` is `"0.10"`, `Versions` `["0.9", "0.10"]`,** and the
  runner's copy of the vectors moves with them (§5.2). An 0.8 host is no
  longer one the SDK takes. The first minor of two digits: a version
  compared as a string, or as a float, puts 0.10 before 0.9 (SPEC.md §4).
- **A test host** that speaks 0.10 only skips the `speaks-0.9` vectors
  (§4.4).
- **The new kinds and fields** (§3.1, §3.9): `gesturestart`,
  `gesture`, `gestureend`, `wheel` and `pointer`, and `Pointer`,
  `Finger`, `Cut`, `Gesture` and `Wheel`, with the `PointerMouse`,
  `PointerPen` and `PointerTouch` names. `Finger` is absent for the mouse
  and 0 for the first finger, so a language without an absent int needs
  −1, not 0. `Scale`, `Angle`, `DX` and `DY` are floats: a body with an
  int there does not decode.
- **The vectors' new runner work** (conformance/README.md): a decode
  vector's `pointer`, `finger`, `cut`, `gesture` and `wheel`. A test host
  plays them as the user (§4.4), and skips the host vectors with touch,
  pinch and wheel steps, which need a layout.
- **A program's drags** are keyed by finger as well as surface, and a
  `dragend` with `Cut` drops what it ended (§4.8). A program that sized
  its surfaces from the first `caps` follows each `caps` now (§4.8).
