# Writing scenarios

## 12. `lim` commands

Locations are written the same way for all commands: `file` or `file#label`.
`lim help` and `lim command --help` show usage, and `lim version` (`--version`) shows the version (they work outside a project too).
Unknown commands and options stop with a similar name suggested (exit code 2).

Every command chooses the language of its usage, errors and progress messages the same way: `--lang en` (or `--lang ja`) if given, otherwise the
`LIMINALIA_LANG` environment variable (`en` or `ja`), then the terminal's language (the first of `LC_ALL`, `LC_MESSAGES` and `LANG`: Japanese if it starts with `ja`,
undecided for `C` and `POSIX`, English for anything else), and Japanese if none of these decides. Without `--lang`, `lim new` makes the project in this language
(the English sample or blank project in English). The `--lang` of `lim shot` is also the language to play in, and a language other than `en` and `ja` doesn't
choose the output language. The keys and values in `--json` output don't change with the language.

| Command | What it does |
|---|---|
| `lim new <folder>` | Creates a new project from the template (`templates/default`) (`--title` / `--id`) |
| `lim check` | Syntax check (chapter 9). Checks syntax, types, undefined speakers, labels, characters, and jump targets in other files. With suggested fixes (`--json`). Diagnostics are sorted by file, line and column. `--fix code,…` applies the suggested fixes for those codes across all files before checking (`--fix chara-solo` turns the one-at-a-time style into `solo=true`) |
| `lim explain [code]` | Shows the severity, contents, an example with its fixed form, and the section of the docs for a diagnostic code (from the tables in chapter 9; the list if omitted; `--lang en` for English; works outside a project too) |
| `lim test [file or folder…]` | Runs the automated script tests (`tests/*.limtest`) ("Script tests" below). The exit code is 1 if something differs from what's expected. `--github` for annotations, `--json`, `--lang en` |
| `lim reach <location>` | Searches for a way of choosing that reaches that location (`file#label`, `file:line`, `file`) from the start (`entry`), and shows the choices to make, the condition variables at the time it's reached, and how to write it as a test. If it can't be reached, shows the conditions for entering and the lines that set the condition's variables (`--timeout seconds`; default 30; `--json`). The search is the same as Studio's search for unreachable lines. `--ending kind or title` searches for a way to reach an ending (found the same way as `expect ending`) and writes the test with `expect ending`, and `--endings` lists whether each ending is reached |
| `lim trace <location> [--choose 1,north]` | Runs without a browser and prints pages, speakers, commands and choices. `--set game.x=expression` sets a value before starting (can be written any number of times; `system.` `temp.` too), and `--from-state file.json` continues from a save (the `save` of `--json` output, or save data itself). Commands are shown in .lim syntax (unnamed parameters, duration units), listing only the values changed by the parameters you wrote and the project defaults (`commandDefaults`) (`--json` gives all values). `--choose` takes numbers (from 0) or choice text (the same text, or a choice containing it). The exit code is 1 if there's a runtime error. A loop that keeps coming back to the same choice with no text in between is stopped and reported once every way of choosing has been tried (`choice-loop`). The last line, "End: reason (file:line)", shows the last line that ran (the `@title-screen` line if it ended there; `endedAt` in `--json`) |
| `lim coverage` | Runs every file, widening to choices it hasn't tried yet, and reports text coverage and runtime diagnostics (`--runs N` is the per-file limit, 8 by default; it stops early once every way of choosing has been tried; `--seeds N` runs with different random seeds; `--min-lines`, `--require-endings`, `--json` and `--github` for CI thresholds are in [ci.md](../../ci.md)) |
| `lim fmt` | Tidies command lines and expressions (below). `--check` doesn't rewrite anything and fails if any file isn't tidy (it shows each place to fix as `file:line` with the line before and after, up to 10 places per file). `--indent 2` (`tab`, `keep`) sets the block indentation |
| `lim migrate` | Moves the scripts to the current format (earlier command names to the current names, and `format` in liminalia.json to the current version; "Script format versions" in chapter 10). `--check` doesn't rewrite anything and fails if there's anything to move. `--keep-behavior [name,…]` also writes settings that keep the old behavior where defaults changed |
| `lim script-export` | Exports the script for proofreading (see "Proofreading" below). `--format text` (default) or `html`, `--out` for where to write (default `exports/script`), `--lines` for line numbers at the start of text lines |
| `lim prose extract` / `lim prose apply` | Extracts only the text with a key for each line (`--out`, `--ruby aozora\|lim`, `--lang en` for an English note at the top), and writes edited text back after confirming that nothing but the text changed (`--check` only checks; see "Proofreading" below) |
| `lim voice-lock` | Locks the numbers of numbered voices to each line of dialogue (chapter 10) |
| `lim rename <kind> <old name> <new name>` | Renames something and rewrites the places that refer to it too (the same rewriting as renaming in Studio; the earlier version of the script stays in the change history). The kinds are `label` (`file#label` to the new label name), `file` (a scenario name), `variable` (`game.x`), `character` (a character ID), `speaker` (a speaker name), `asset` (`kind/path`) and `macro` (a `@define` name). Script tests (`tests/*.limtest`), `files` in `chapters` and the keys of `scenes` in liminalia.json are rewritten too. For assets, commands inside text (`{@se …}`), `{img …}`, translations and `url()` in the project's CSS are rewritten as well. References that couldn't be rewritten (jump targets decided by expressions, chapter wildcards that won't match the new name, asset names left as text in data or templates and so on) are listed. If a script file can't be read as UTF-8, nothing is rewritten. `--lang en` for English |
| `lim shot <location>` / `lim shot --title` | Takes a screenshot of the given page or the title screen (`--video` records from there, advancing at real speed; [ci.md](../../ci.md)) |
| `lim dev` | The player's dev server |
| `lim profile [<location>]` | Opens the player and advances a set number of pages while measuring the frame rate, memory, asset loading and the number of DOM elements ([player.md](../../player.md)) |
| `lim studio` | Studio (runs on the same dev server as `lim dev`; [studio.md](../../studio.md)) |
| `lim import` | Imports from TyranoScript .ks. Compares the text to confirm it matches, and lists the parts that can't be converted, with reasons. Macros are gathered into `@define` in one file ([tyrano-import.md](../../tyrano-import.md)) |
| `lim import-ink` / `lim import-yarn` / `lim import-renpy` / `lim import-twine` | Converts and imports Ink, Yarn Spinner, Ren'Py and Twine (Twee 3) scenarios. Syntax it can't handle becomes comments and is listed in the report ([ink-yarn-import.md](../../ink-yarn-import.md)) |
| `lim backport --base <base>` | Writes changes made to the .lim of an imported project back into the .ks, then imports again to confirm ([tyrano-backport.md](../../tyrano-backport.md)) |
| `lim build <browser\|mac\|windows\|linux\|android\|ios\|all>` | Exports. Checks before exporting, and starts the result after exporting to confirm it (for the mobile versions, see exporting in the README; `ios --device` installs and starts it on a device) |
| `lim reference` | Generates the command reference from the definitions of the built-in commands and the plugin commands (`--json` for machine-readable output) |
| `lim patch --base <previous export>` | Makes a patch (pack) with only what changed, from the difference with the previous export (patches in [distribution.md](../../distribution.md)) |
| `lim psd <file> <character>` | Imports the layers of a PSD or PSB as a layered sprite (top-level groups are layers, the layers inside them are the pictures to choose from, and visible ones are the defaults; `--appearance` sets the sprite set and `--folder` the folder in fgimage for the pictures. Dropping a PSD on "Add a variant" for a character in Studio does the same) |
| `lim stats` | Counts the characters of dialogue and narration (by file, scene and speaker), the number of voiced lines, and spelling variant candidates ([text-stats.md](../../text-stats.md)) |
| `lim i18n <language>` | Updates the translation file for that language (`lang/<language>.json`) to match the current scenarios. Exports to and imports from tables and XLIFF (`--export` `--import`, `--untranslated` `--changed`), and gives line IDs with `--stamp-ids` (chapter 10) |

`lim fmt`, `lim stats`, `lim script-export`, `lim prose`, `lim coverage` and `lim reach` treat pack scenarios (`packs/<id>/scenario`) like the main ones,
and their output gives them as `packs/<id>/scenario/<scenario>` (read with the speakers in the pack's `cast.json` too). `lim coverage` runs each pack scenario with all packs laid over the main scripts and that pack last (some packs jump to scenarios in other packs; pack data in `packs/<id>/data` is laid over too, as in the game; counted in the coverage and `--min-lines`),
and also runs from "New Game" with all packs laid over the main scripts (as in an export), showing whether the endings in pack scenarios are reached (counted in `--require-endings` too).
Give `lim reach` a place in a pack scenario (such as `packs/<id>/scenario/<scenario>:<line>`) and it searches from "New Game" with the packs laid over the same way (`lim test` doesn't run packs, so no test is suggested).
`--ending` and `--endings` also search the endings in pack scenarios.

`lim new --ci` also adds a GitHub Actions check template (`.github/workflows/liminalia-check.yml`; [ci.md](../../ci.md)). As for script tests (`tests/start.limtest`), a project with the sample story gets the sample tests even without `--ci`, and a blank project (`--blank`) gets a test that only checks the game runs from start to end when `--ci` is given.

### Definitions and references (`lim query`)

`lim query [project] <label|var|asset> [name]` lists names and definition/reference counts, or the sites for a name (`start#intro`, `game.count`, `bgimage/room.png`). Sites are printed as `file:line[:column]  role  text`, definitions first. Configuration, script tests and declared project data are included. Nested variables normalize to their first property; compound assignments both read and write. Asset details include file existence, `guess` for textual matches (up to 20 per asset), and a separate `dynamic` list for expressions that could use the same asset folder.

`--json` returns `{kind, items:[{name, definitions, uses}]}` for inventories, or `{kind, name, found, candidates, definitions, uses, dynamic}` for details (`exists` for assets). Unknown names return up to three candidates and exit 1; successful queries exit 0, usage errors exit 2. Use `--lang en` for English output.

### Proofreading (`lim script-export`, `lim prose`)

**The script for proofreading.** `lim script-export` and "Script for proofreading" under Export in the Studio project settings write, for each script file, only the text
(dialogue, narration and choice text) and the scene headings. Dialogue gets the speaker name (the display name if there is `{name …}`), and ruby becomes `｜base《reading》` in text and `<ruby>` in HTML.
Commands and click waits are left out, `{br}` becomes a line break, and `{= expression}` stays as it is. Text inside `@if` branches is listed in the order written. HTML has line numbers, and `index.html` links to each file.

**Extracting and writing back the text.** `lim prose extract` extracts the text lines into one document. Each line starts with a key (`[file:line hash]`).

```
// lim prose: Edit only the text after the [ … ] at the start of each line. Don't add or delete lines.
// ruby: aozora

// file: start
[start:12 3f9a1c] Akari "We made it. Was this slope always this long?"
[start:20 0c1d2e] - Go north
```

After editing the text, `lim prose apply document` writes it back. It writes back only the lines whose original line hasn't changed since extraction (same hash)
and whose non-text parts (the kind of line, the speaker, commands, values and click waits in the text, and the jump target and parameters of a choice) haven't changed. Indentation and everything after `->` in a choice stay as they are.
Lines not written back are listed with the reason, and the exit code is 1. `--ruby aozora` (the default) turns `{r base|reading}` into `｜base《reading》` when extracting,
and turns `｜base《reading》` and `kanji《kana reading》` back into `{r …}` when writing back (`--ruby lim` keeps `{r …}`).

Text import in Studio also turns `｜base《reading》` and `kanji《kana reading》` into `{r base|reading}`.

### Script tests (`lim test`)

Put `.limtest` files in the project's `tests/`, and write the choices to make and what should happen then. `lim test` runs them from "New Game" without a screen and checks
(like Ren'Py's testcase and Yarn Spinner's test plans). One file can hold any number of tests, each starting with `# name`.

```
// Taking the north road raises Yui's trust and reaches the good ending
# North road
choose Look through the telescope
choose Take the north road
choose 2
expect game.trust == 2
expect reach ending#good
expect text "I can see the stars"
expect no-errors

# From partway, with values set
from chapter2#start
set game.trust = 5
choose Trust her
expect ending good
```

| Line | Meaning |
|---|---|
| `# name` | The start of a test. The name appears in the results |
| `from location` | Where to start (`file` or `file#label`). If omitted, `entry` in liminalia.json |
| `from-state file.json` | Start from a save (the `save` of `lim trace --json` output, or save data itself; a path relative to the test file) |
| `set variable = expression` | A value set before starting (`game.` `system.` `temp.`; takes priority over initial values) |
| `seed number` | The random seed (default 1) |
| `choose text` / `choose number` | What to choose at choices, used in the order written. Text matches the same text, or a choice containing it. Numbers count from 1 at the top (the same as `@choice default=`; `lim trace --choose` counts from 0) |
| `rest stop` / `rest first` / `rest rotate` | What happens after the `choose` lines run out. `stop` (default) stops at the next choice, `first` always takes the first, `rotate` takes the next one each time it comes back to the same choice |
| `limit number` | Stops after this many pages (default 5000. If the limit is reached without writing it, the test fails as one that doesn't end) |
| `expect expression` | The expression is true at the end (`expect game.trust >= 2`, `expect visits("start#north") == 1`) |
| `expect reach location` / `expect not reach location` | That location was passed (wasn't passed). The location is `file`, `file#label` or `file:line` (a test runs across files, so `#label` alone can't be written) |
| `expect text "text"` / `expect not text "text"` | The text (or name box) of some page contains that text (none does) |
| `expect ending kind` | It ended with an ending of that kind (`ending=` of a command that ends the playthrough, such as `bad`). With text that isn't a kind, it ended with an ending whose name (`title=`) contains that text |
| `expect end` | It ran to the end of the script or to a command that ends the playthrough (such as `@title-screen`), and didn't stop because the choices ran out or the page limit was reached |
| `expect no-errors` / `expect no-warnings` | No runtime errors (with `no-warnings`, no warnings either). Errors fail the test even if this isn't written |

- Indentation is only for looks. `//` lines are comments. Each test runs as a newly started playthrough (`system.` starts from its initial values too).
- A failure shows the line in the test file and what differed (the values of the variables in the expression, a choice that wasn't there, `choose` lines that weren't used, and so on).
- Ways of choosing can be found with `lim reach` (it shows the choices that reach a place, in a form you can write as a test as is).
- Studio doesn't have a way to run tests yet (use `lim test`).

| code | Severity | Contents |
|---|---|---|
| `bad-test` | Error | A line in a `.limtest` that can't be read (a line before `# name`, an unknown word, a malformed `set` `choose` `expect reach` and so on). A test with an unreadable line doesn't pass |

### What `lim fmt` tidies

Command lines and expressions are tidied. The content of text and dialogue lines isn't changed (trailing spaces are removed, and when indentation is set, only the indentation is aligned).
"Format" in the menu of the script heading and in ⌘K in Studio tidies the same way, as one editor change (Undo reverts it).

| Target | How it's tidied |
|---|---|
| Commands | One space between the name and parameters, and between parameters. Removes `"…"` that can be removed without changing the value. Orders named parameters as in the command definition (unnamed parameters first; if the same parameter appears twice, they aren't reordered) |
| `@set` `@let` | One space on each side of `=` (and `+=` and the like) |
| Expressions | Conditions of `@if` `@elif` `@while`, values of `@set` `@let`, `@return (expression)`, and `(expression)` parameters (including `if=(…)`). One space on each side of binary operators and `?` `:` of a conditional, one space after `,`, and no spaces inside brackets, after unary `-` `!`, or around `.` (`game.n+1>2` becomes `game.n + 1 > 2`). Strings and numbers are written as they were. `{= expression}` in text isn't changed |
| `if=(expression)` | On command lines, placed after the other named parameters (at the end of the line) |
| Choices | Tidies everything after `->` (the target and parameters) in the same way as commands. The text shown isn't changed |
| Indentation | Not changed by default. With a number of spaces or `"tab"` in `fmt.indent` in liminalia.json (or `--indent`), lines are aligned one step per block level (`@if` `@while` `@with` `@choice` `@define`), text lines included (`@elif` `@else` `@end` at the depth of the block's start; full-width spaces are text and aren't changed). Nothing inside `@script` or `@html` is changed |

```json
"fmt": { "indent": 2 }
```

A project that doesn't want expression spacing tidied writes `"fmt": { "expressions": false }` (the earlier `lim fmt` behavior: only the spaces around `=` are aligned, and expressions aren't changed).

`lim fmt` doesn't remove parameters equal to the default. Those equal to the project default (`commandDefaults`) are reported as info by the syntax check (`same-as-default`),
with a fix that removes them. Files with syntax errors aren't tidied. The result has the same syntax tree as before tidying (confirmed by tests).

<!-- For anyone changing the format: the implementation rules (the registry, where conversion tables live, how to raise the save version and so on) are in docs/design/format-rules.md. When you change it, update this document and the tests at the same time. -->
