Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Direction and alignment of right-to-left text per paragraph

Status: accepted. Date: 2026-11-06. Supersedes: 0006.

ADR 0006 reordered right-to-left runs per row at a fixed left-to-right base. That shows every Arabic sentence with its final punctuation on the wrong side and short Arabic rows at the left (build/a1, build/a2 reports). There is still no settled standard for bidi in monospace terminals; the "BiDi in Terminal Emulators" recommendation ties alignment to the paragraph base and leaves autodetection to the terminal. Every mainstream terminal defaults to fixed LTR; the owner asked for a compromise that keeps tables, file names and prompts where they are while Arabic prose reads correctly.

Approved by the owner on 2026-11-06.

Decision

  • Unchanged from ADR 0006: the VT grid is logical; libghostty-vt is unchanged; right-to-left runs are reordered as whole cells with joining across cells; each row keeps a logical-to-visual map used by cursor, backgrounds, selection and hit testing; rows with no strong right-to-left character pay only a class scan; a config switch turns reordering off; the recommendation's escape sequences come later.
  • A paragraph is a row plus the rows that continue it by soft wrap (Ghostty's wrap flags). Its base direction is right-to-left only when it contains a strong right-to-left letter and no strong left-to-right letter; box-drawing and block characters (U+2500..U+259F) count as strong left-to-right so tables keep their columns. Everything else is left-to-right. The setting v7-bidi-base (auto, ltr, rtl) overrides the rule.
  • The alternate screen is always left-to-right per row: full-screen programs own their cell coordinates. Rows marked as a shell prompt by OSC 133 are left-to-right.
  • Rows of a right-to-left paragraph are resolved with their erased cells, so they align to the right edge and the cursor sits where the next letter appears. Left-to-right rows stay left.
  • Levels are resolved per row at the paragraph's base for the alpha; a row shaped across a soft wrap gets the neighbouring row's edge letter as joining context. Whole-paragraph level resolution is deferred until a case needs it.

Consequences

  • Arabic prose, headings and numbered lists read correctly and sit at the right. bat tables, File: مرحبا.txt, ls rows with any Latin, and prompts with any Latin render exactly as before (approved goldens unchanged).
  • Known wrong: Arabic prose containing a Latin word stays left-to-right; a prompt with no Latin jumps right when Arabic is typed; ASCII-drawn tables with only Arabic reverse; a paragraph whose head is above the viewport is classified from its visible rows.
  • Cost: one combined class scan per dirty row, a per-row base byte in the cache key, and relayout of a wrap chain when its base changes. Left-to-right rows in left-to-right paragraphs are untouched.
  • Tests: the approved goldens stay; bidi-arabic-rtl must equal bidi-arabic translated; a multi-row UDHR excerpt golden; table-driven tests of the base rule; property tests at both bases; GPU equals CPU on the VMs.

Implementation notes

The configuration file is selected with --config PATH; its keys are v7-bidi = implicit | off and v7-bidi-base = auto | ltr | rtl. CLI --v7-bidi, --v7-bidi-base override the file, and --no-bidi takes precedence. No general configuration loader existed before this change.

The forced RTL table assertion in proposal step 2 was incorrect: strong-L borders preserve columns under the auto-selected LTR base, but UAX #9 can still reorder runs under a forced RTL base. The class wrapper is used without exceptions.

Joining context is excluded from level resolution and maps. A shaping-only ZWJ at a lam/alef wrap boundary prevents cross-row ligation while retaining forms. Equal-position glyphs have deterministic paint order so the approved Arabic image is exactly translated by 38 cells (456 pixels), with no raster changes. CPU page surfaces permit bounded heights up to 8192 pixels for the full UDHR. Window padding remains four pixels; the proposal's accepted last-row clipping tradeoff remains (about two pixels for the deepest marks).

Paragraph-wide levels (proposal step 9) remain deferred.

Block selection (2026-11-30, ADR 0009) is of logical columns. On a row this policy reorders, the highlighted cells sit where those columns are drawn and the copy is exactly them, in logical order.