# editor

A code editor as an element: what CodeMirror is to a React app. Written from
nothing (`runtime/page/editor.mote`, part `ED`); a page that does not use it does not
carry it.

    state src = "let n = 1\n"
    editor value=src lang="javascript" height=300

    editor value=src lang=lang lines=numbered wrap=wrapped readonly=locked tab=4 placeholder="Type some code…" change=saved

It is a `<div>`: size and dress it with styles on the element (`width`,
`height`, `flex`, `font-size`, `border`, `border-radius`, `background`). With
none it is 240px high, 13px monospace, with a 1px `line` border and 8px
corners. The line height is 1.5 times the font size, in whole pixels.
`aria-label` on the element names the text field. `focus("id")` with the
element's id puts the caret in it.

## Attributes

| attribute     | type   | default | what it does |
|---------------|--------|---------|--------------|
| `value`       | string | `""`    | The text. Written back on every edit when it is state (or reaches into it). When the page assigns something that differs from the text, the editor takes it, keeps its scroll position and selection, and raises no event. Line ends are `\n` (`\r\n` is taken as `\n`). |
| `lang`        | string | none    | Highlight as that language (the names the `syntax` battery knows: [text.md](../text.md#code)), and choose the comment style for Cmd/Ctrl+/. May change while the page runs. |
| `lines`       | bool   | `true`  | The line-number gutter. `lines=false` hides it. |
| `readonly`    | bool   | `false` | The same view; the text can be selected, copied and searched, not changed. Tab moves on. |
| `wrap`        | bool   | `false` | Soft wrap. Line numbers stay beside the first row of their line. Without it, long lines scroll sideways. |
| `tab`         | number | `2`     | Spaces in one level of indentation (and the width a tab character is shown at). |
| `placeholder` | string | none    | Shown, dim, while the text is empty. |

## Events

| event    | when | the handler is given |
|----------|------|----------------------|
| `input`  | every edit (typing, paste, undo, a replace…) | the text |
| `change` | the editor is left, if the text is not what it was when it was entered | the text |

    editor value=src input=typed change=save
    action save(text: string)

`key`, `focus`, `blur`, `scroll` and the pointer events are the element's own.
A value the page assigns raises neither event.

## Keys

`Mod` is Cmd on a Mac, Ctrl elsewhere.

| key | does |
|-----|------|
| Tab | With a selection over several lines: indents them. Otherwise inserts spaces to the next stop. |
| Shift+Tab | Outdents the line, or every line of the selection. |
| Mod+] / Mod+[ | Indent / outdent the same lines. |
| Enter | A new line with the indentation of this one; one level deeper after `(`, `[`, `{`, and after a line ending in `:` when `lang` is python or yaml. Between a pair (`{▮}`) it opens three lines with the caret on the middle one. |
| Backspace | In a line's indentation: back one level. Between an empty pair (`(▮)`, `"▮"`): removes both. Otherwise as usual. |
| `(` `[` `{` `"` `'` `` ` `` | Also writes the closer (before a space, a closer, `,` `;` `:` or the line's end; a quote not straight after a letter, so `don't` is safe). Typing the closer that is already there steps over it. Typed over a selection, they wrap it. |
| Mod+/ | Comments or uncomments the line or the selected lines, in the language's style: `//` (the default), `#` (python, ruby, shell, yaml, toml…), `--` (sql, lua, haskell), `;` (lisp, clojure, ini, asm), `%` (tex, erlang), `<!-- -->` (html, xml, markdown), `/* */` (css). |
| Mod+D, Shift+Alt+Down / Up | Duplicates the line or the selected lines. |
| Alt+Up / Alt+Down | Moves them up / down. |
| Mod+Z, Shift+Mod+Z / Ctrl+Y | Undo, redo: the browser's own history for the field, and every edit above is one step in it. |
| Mod+F | Opens find, with the selection as the query. |
| Mod+G / Shift+Mod+G | Next / previous match while find is open. |
| Escape, then Tab | **Leaves the editor.** Tab indents, so a keyboard user would otherwise be held: after Escape (a note "Tab moves focus" shows), the next Tab or Shift+Tab moves focus as it does anywhere else. The text field says so to screen readers (`aria-description`). In a readonly editor Tab always moves on. |

The caret, selection (drag, double and triple click, Shift+arrows), clipboard,
IME, and the on-screen keyboard are the platform's: the text is in a real
`<textarea>`. Spellcheck, autocorrect and autocapitalise are off. A press on a
line's number selects the line.

### Find

Query, a count ("3 of 17"), previous and next, a match-case toggle (`Aa`),
replace, close. Enter is next, Shift+Enter previous, Escape closes and leaves
the current match selected in the text. Matches are marked in the text and
the current one more strongly. The replace row (the bent arrow; not in a
readonly editor) has Replace (Enter in its field) and All; "All" is one undo
step. The search is plain text, at most 20,000 matches counted.

## What is shown

- The line the caret is on is banded and its number lit, while the editor
  has focus.
- The bracket beside the caret and its partner are outlined. (Brackets are
  counted as written: one inside a string or comment counts too.)
- With a word selected, the other places it appears in view are tinted.
- The colours are the page's roles (`fg bg dim line accent warn`), so it
  follows light and dark; a focus ring in `accent`; the selection in
  `accent` at 30%. The token colours are `Highlight`'s.

## How it holds a long file

The textarea holds the whole text; the highlighted layer and the gutter hold
only the lines in view and a dozen either side, redrawn at most once a
frame. A text the highlighter does in under 4 ms is highlighted whole on
every edit. A longer one has the changed lines in view highlighted on their
own at once (so a line inside a long comment or string may be coloured as
code for a moment) and the whole text again a fifth of a second after the
typing stops. With `wrap`, only lines too long to fit are measured.

## What it is not

Compared with CodeMirror: one caret (no multiple selections or column
selection), no folding, no autocompletion, no lint gutter, no minimap, no
regular-expression search, no key-binding or extension API. A value assigned
by the page replaces the text outright, and the undo history with it.
On an iPhone, a font-size under 16px makes Safari zoom in on focus: give the
element `font-size=16` there if that matters.
