# apps/lua

Our small apps written in Lua: Calculator, Clock, Editor, Merge, Mines, Notes, Reversi.
Depends on: c8-lua (`toolkit/lua`, built first by `build.sh` for `--apps lua`); nothing to compile.

A meson project of its own (`c8-lua-apps`) since 2026-12-04, so the runtime and the apps are apart; the Lua API is `toolkit/lua/README.md`, the runtime's notes `toolkit/lua/CLAUDE.md`.
Our own Lua apps, run by `c8-lua` (`toolkit/lua`), installed to `share/c8-lua/apps/NAME/`
with a desktop entry (`app.desktop.in`, `@bindir@`/`@appdir@`; the
installed name is its `StartupWMClass`) and a white icon each, all by
the `foreach` in meson.build: Calculator (see below), Clock (stopwatch with laps, timer), Notes (`~/Notes/*.md`,
saved 0.8 s after typing, `gio trash`), Mines (Hard turns on its side
in a tall window; hold to flag), Merge (2048-style, swipes or arrows,
game kept), Reversi (two-ply PC). Each builds its window once and
reflows: Calculator's history is `narrow_hidden` and its margins
`narrow_padding`; Notes shows its list or its note narrow (`on_resize`
hears it, panes shown and hidden, nothing built again). Tested headless (nested c8, scratch HOME,
`dbus-run-session`, wide and at 480): each draws; Calculator's keys and
history; Mines' flood fill, flag, the turned board's hit testing;
Merge's arrow keys and saved game; Notes' new, typing, files, narrow
back, delete to Trash; Clock's laps, tabs, pause, ring; Reversi's move
and the PC's answer. Untested: swipes, holds and touch, the tiles they
update, the Search charm in Notes, the real session.
Calculator (2026-12-04): a pivot of Standard (each operator works on
what's there so far, as Windows' did), Scientific and Converter, with
one History pane beside it for the first two (`on_key` goes to the tab
shown; the converter's entry takes its own keys, as `on_key` only hears
keys nothing took). Scientific keeps the expression as tokens and reads
them by precedence on every key (a live answer; brackets left open
close on =), with implicit multiplication (2π, 3(4+1)), Deg/Rad kept,
Inv turning six keys into their opposites, a function after a number
taking it (9 √ is √(9)), ± turning the last number or bracket round
(−(…)), and an operator after = going on from Ans (which holds its
value from when it went in: read live, = changed it under you).
Converter: twelve kinds, each unit as its size in the kind's unit of
1 (temperatures apart, through kelvin), the number in every unit
listed. `calculator.json` is now `{ history, tab, degrees, converter }`;
an old file (the history as a bare list) is read as the history.
Traps: `narrow_hidden` shows a box again as the window widens even if
it was hidden by hand, so History's `hidden` (for the Converter) is on
a box inside the `narrow_hidden` one; `gsub` returns two values, and as
`tonumber`'s argument the count became the base. Tested (2026-12-04):
the logic through a stub `c8` (`package.loaded.c8`, then `dofile` the
app; driving `on_key` and the buttons' `on_click`): 47 cases across
the three tabs; headless in a nested c8 (1280x800 and 480x900) each tab
drawn, typed sums in Standard and Scientific (Super twice in one
`wtype` call, the text in a second: in one call the keys came through
garbled), a click on the Converter header hiding History and keeping
the tab. Untested: touch, the History list's clicks, the real session.
Text editor (`editor/`, 2026-12-04): tabs of documents, plain or
rich. Rich text is c8ui's (`c8ui_textview_set_rich`, opt-in per view:
"not all apps need RTF", the user's words), RTF is the app's own
`rtf.lua` (the only module installed beside an app: meson.build's
`modules` dict). `rtf.read` takes groups, font and colour tables,
`\b \i \ul* \strike \fs \f \cf \plain`, `\u` with `\uc`
(surrogate pairs too), `\'hh` as cp1252, skips other destinations and
`\*` groups, keeps `\fldrslt`; paragraphs' formats, pictures and
tables are dropped (c8ui has no paragraph formats). `rtf.write` gives
cp1252 `\'hh` or `\uN?` and only the control words that change. A
file is RTF by its `{\rtf` start; plain text keeps a BOM and CRLF,
non-UTF-8 is read as cp1252 and saved as UTF-8, NUL refuses it. A
rich document saved as non-.rtf turns plain (asked first); a plain one
saved as .rtf turns rich. Unsaved work is asked about on closing a tab
and through `can_close` (Alt+F4). Fonts for the format bar come from
`fc-list : family`. Rich pages are white (`scheme = "light"`), plain
ones keep the theme in `monospace` (always: the user asked, 2026-12-04; only its size is a setting). Traps: the find and replace entries need
`enter_only = true` (Tab out of them fired `on_enter`: replace ran
twice); a found match showed nothing while the find box had focus,
hence c8ui's faint unfocused selection; Claude's file-writing tool
turns `\uXXXX` in content into the character, so RTF test data with
`\u` must be written another way. Tested: `rtf.lua` (92 cases, read,
write, round trips) and the format bindings (23 cases: runs, undo of
a format, typing format, replace all keeping formats) through
`c8-lua`, also under ASan; headless in a nested c8 (1280x800 and
480x900): rich drawing, Ctrl+Tab, Ctrl+B typing and Ctrl+I on a
selection, their undo and redo, Ctrl+S writing RTF, replace all, a
plain save kept UTF-8 with CRLF, Alt+F4's question (Escape keeps the
app, Don't save closes), the bars wrapping narrow. Untested: the file
pickers (no portal nested), so Save as's turning plain or rich; the
Search charm, the live tile, the font, size and colour pickers by
pointer, touch, the real session.
Markdown preview (2026-12-05): `md.lua` beside `rtf.lua` (meson.build's
`modules`), no new dependency. `md.parse` is CommonMark's blocks by
lines (headings ATX and setext, paragraphs with link definitions taken
off their start, fenced and indented code, quotes and lists with lazy
lines, tight or loose, task items) plus GitHub's tables, `~~strike~~`
and bare links; raw HTML keeps its text and drops its tags (`<br>`
breaks, an `<img src>` block is a picture). `md.markup` is inline to
Pango markup: code spans, escapes, links (accent, underlined; refs
resolved after the blocks are read, so a definition may come later),
autolinks, emphasis by CommonMark's delimiter runs (flanking, `_`
inside words, the rule of 3). `md.build` makes a widget per block in
the preview column: `c8.rich` (headings Selawik light/semibold by
level), code on a shade of fg over bg (`w:colors()`, so it suits dark
schemes), a canvas bar for quotes, rows of marker and column for
lists, a `c8.grid` of rich cells in a sideways scroll for tables,
`c8.separator` for rules, local pictures through a `c8.viewbox`
(relative to the file; nothing is fetched from the web: its words show
instead). Links are c8.rich's `<a href>` (2026-12-05) with `on_link`
only on rich texts that have one; a picture that is a link is a
`c8.item`. `follow` in main.lua: `#anchor` scrolls the preview to the
heading (`md.build` returns them by GitHub's slug, `-1`, `-2` for the
same words again; `w:rect()` against the preview box's), a link with a
scheme goes to `win:open_uri`, a path (relative to the file; an
untitled document says it can't) opens here in a tab when it is text
(`TEXT_FILES` or no ending) and in its app otherwise (folders too). In the app a plain document's page is a row of the editor,
a divider and the preview's scroll; `show_panes`: wide side by side,
narrow the preview alone while it's on (Ctrl+E or the app bar's
Preview, `toggle = false`, back to the editor; Find turns it off
narrow). The status bar starts with the kind as a link ("Plain text ▾",
"Markdown ▾", "Rich text (RTF) ▾"; a menu of the three), because the
user couldn't find how to switch (2026-12-05: the app bar is hidden
and the kinds were only New rich text and Ctrl+E). Rich to plain asks
first; a kind change marks the document unsaved (`doc.dirty`, as
c8.text's modified can't be set), and `save` goes to Save as, the
name's ending changed, when the file's ending doesn't fit (a .txt
turned rich isn't written as RTF over it, nor turned back). It is rebuilt 0.3 s after typing stops, the scroll kept. On
for `.md`/`.markdown`/`.mdown`/`.mkd` when opened (Settings charm:
"Preview Markdown files when they open"), never for rich documents.
Traps: `c8.picture(path, max)` scales small pictures up to `max`, so
load at natural size and only shrink big ones; a GitHub delimiter
cell with no colon is `false` in the align list, as a nil left a hole
and the table didn't match its header; rich text in a grid needed
c8ui's natural-width fix (`richtext.c`, WIDGETS.md). Tested: the
parser through `c8-lua` (74 cases, and 20,000 random inline strings
giving balanced markup, no errors; 165 KB parsed in about 95 ms);
headless in a nested c8 (1280x800 and 480x900): every block kind
drawn, typing redrawing it, Ctrl+E both ways wide and narrow, the app
bar's Preview by Menu and a click, a dark scheme (test app), the
strike; clicking an anchor (scrolled) and a link to another .md
(opened in a tab), the kind menu wide and at 480, plain to rich and
back through its question. Parser: 82 cases. Untested: web links
(clicking one nested would open the real browser), picture links, the
pointer's hand (not in screenshots), Save as after a kind change (no
portal nested), the theme changing while open, a real session, very
long documents' typing feel, News and Weather after the rich-text
width fix.
