navkit-bidi
Right-to-left text and the mirrored screen -- navkit/bidi.py (layout, order_cells for the viewer's and editor's rows, cells with logical indices, python-bidi's pure-Python UBA run step by step, arabic
SKILL.md.claude/skills/navkit-bidi/SKILL.md
GitHubRight-to-left text and the mirrored screen
Navigator lays right-to-left text out itself; no terminal is trusted to. Measured (Oct 2026): VTE reorders a whole screen row as one paragraph (columns, frames and overlapping windows trade places), kitty reverses glyphs of some runs whatever order they arrive in, ghostty reorders nothing and joins no Arabic letter.
Mirroring is a departure from DOS Navigator, which never mirrored anything; asked for by the user (a right-to-left language reads its screen right to left). It is the second standing exception beside the icon gutter.
Text: navkit/bidi.py
layout(text, rtl) -> ((logical_index, cell), ...), visual order left to right, cached. Pure-LTR text short-cuts. Paragraph direction is the first strong letter's, else rtl (the surface's) -- so( )on a mirrored screen is laid out as inside Hebrew. Explicit format characters take part and get no cell.- Arabic is shaped by
arabic-reshaperwith ligatures off (one letter in, one form out, indices kept); lam+alef is folded into its ligature by_shape, keeping the lam's index. Marks are folded into the letter before them before ordering, so a reversed run cannot put niqqud/harakat first. Brackets are mirrored at odd levels (L4). - python-bidi is held below 0.5: 0.5+ is a Rust extension returning only the display string; Navigator needs each
character's place (style, caret) and the Linux packages are
arch: all. Itsbidi.algorithmsteps are run one by one with anatindex stamped on each char dict. - 0.4 predates Unicode 6.3's isolates and knows no class
"": an LRI/RLI/FSI/PDI, an unassigned code point or a noncharacter beside an RTL letter failedresolve_implicit_levels' assert inside a paint._classifiable()hands the algorithm stand-ins of the same length (isolates as LRE/RLE/PDF, FSI by the next strong letter, the classless as U+FFFD's ON); the cells still show the real characters, and the isolates, like embeddings, get none. caret_column(text, index, rtl)/index_at(text, column, rtl): columns asdraw_runspaints them (reversed on a mirrored surface). The caret sits on the character it is before; at the end, past the end the paragraph grows from.painted_cells(text, rtl)is(index, column, width)per painted cell -- howlink_urls(StaticText) marks an address where it was painted.display_width(text): the cellsdraw_runspaints (marks on their letter, format characters none, lam-alef one). Centring and right alignment measure with it, neverlen()(titles, captions, labels, headers, panel names).order_cells(chars, rtl, detect=True): detect false fixes the paragraph direction to rtl -- a console row (VTE's left to right), a wrapped viewer row (its whole line's direction, UBA's L1: break first, order each row).char_width: marks and everyCf(marks of direction, joiners, soft hyphen) are 0 wide.reveal(text, mark="⁇", keep=""): embeddings/overrides (U+202A..202E), isolates (U+2066..2069) and C0/C1 controls become mark, one character for one (alenstill holds), sophoto+ U+202E +gpj.execannot paint asphotoexe.jpg. LRM/RLM/ALM stay hidden (they cannot turn letters round). Applied to what is shown only --Panel.shown_name(rows, list widths, scroll, tooltips, info line; the mark reversed out of the row viaREVEALED/reveal_runs),escape_caption(every dialog caption naming a file; keeps\n/\t), Ctrl+L, the quick view title,TreeView-- never to a name that is used (input values, the command line, file operations).glyphs.hidden_mark(tier):⁇,?in ASCII.
Painting: Surface.rtl, draw_runs, the mirror
Surface.draw_runs(x, y, [(text, style), ...], max_width)is the one text primitive;draw_textis one run (ASCII on an LTR surface keeps a fast path). A caption with a highlighted letter is onedraw_runscall (draw_caption,shortcut_runsinnavml/widgets/dialog/control), or the letter lands in the wrong cell.- The application composes the frame left to right with
_back.rtlset, thenScreenBuffer.mirrored()flips it on the way out: rows reversed, wide characters kept owner-first, glyphs throughFLIP(corners, tees, half blocks, pointers, arrows, brackets).draw_runson an rtl surface writes reversed and FLIP-swapped, so the mirror puts text back the right way and against the right-hand edge of its span. A glyph that points the layout's way is aset_cell, not text (the submenu arrow), so it flips. - Text painted cell by cell with
set_cellcomes out reversed on a mirrored screen. Usedraw_runs, or keep the widget left to right. Widget.mirrors = Falsekeeps a widget's content left to right inside the mirrored frame:render_treegives it a view withrtl=Falseandmirror()s its rectangle afterwards, which the screen's mirror cancels. Set onScreenView(console),CommandLine,FileViewer,FileEditor,DiffView,Clock(a blinked colon makes the time two numbers, which an RTL line swaps).- The console (
navkit/console.pyConsoleScreen) lays each row out as VTE does a line:sync()runsorder_cells(row, detect=False)(paragraph left to right whatever the row starts with, so its blanks stay at the end) on the dirty rows only, keeping the written cells (written, forprompt_cells) and the laid ones (surface,blit_into), plus each row's order.visual_x/logical_xmap columns:ScreenView.cursor_positionpaints the child's cursor where its column is shown, a click passed to a mouse-tracking program goes to the written column, andtext()(selection, double-click, copy) takes the painted cells and copies what they cover in written order. A child sendingCSI 8 l(BDSM explicit,_Screen.implicit_bidi; seeded on in the constructor andreset(), since pyte's constructor resets its modes afterreset()) gets its rows unordered. - The diff view (
DiffView._paint_line) orders each side's line withorder_cellsas the viewer does; a line has one look, so only the characters move. - A line cut at the screen's edge is decoded whole before it is reordered when it holds right-to-left text
(
FileViewer._whole,DiffView, up toviewer.WHOLE_LINEcolumns): its far end can be what is painted at the left. The viewer's search hit is scrolled to the column it is painted in (show_hit). Wrapped rows take their line's direction (FileViewer._ordered/_line_rtl). - The rendered Markdown/reST/HTML view: a paragraph or heading whose first strong letter is right to left gets
Row.rtlandRow.lead(its gutter/bullet cells) from the builder;file_viewer._laid_roworders what follows the lead right to left and sets it against the right edge (padding columns map to None for the marks). - File contents (viewer, editor) are left to right widgets but their lines are still laid out: each painted row's
cells go through
bidi.order_cells(chars)-- the row as columns (""second halves, tabs already spread), returned as(column, shown)in visual order, or None when nothing changes (cached). Every per-cell decision keeps using the original column/offset: the viewer's search hit and tokens (looked up per cell once reordered, the one-pointer walk assumes growing offsets), the rendered view's marks, the editor's block/match (lit per cell by original column; the fill covers only what is past the text) and tokens. The viewer keeps lines left-aligned. - The editor right-aligns a right-to-left line (its first strong letter Hebrew or Arabic; a departure, asked
for):
_laid_linegives a line's cells, painted order andshift=max(width, painted length + 1) - painted length, so the line ends against the window's right edge and the cell left of it -- where typing at its end appears, and where its end-of-line caret sits -- always exists; columns past the end run leftwards, clamped at 0. A line holding Hebrew or Arabic is laid out whole, never cut at the screen edge. Latin-first lines stay left. Every mapping goes throughFileEditor._laid:_visual_column/_logical_column(cursor, click; a click right of an RTL line is its start),_painted_here,_rect_spans,_text_column, render (line-break cell, block fill, line-ending mark),_follow(scrolls by painted column), the current-column highlight. The editor's columns stay logical. Left/Right move to the neighbouring painted cell (_step_visually): on a left-to-right line off the left end it stays and off the right end goes past the text and on to the next line, as Right always did; on an RTL line mirrored -- Right at its right end (its start) stays, Left past its left end goes to the end and then on. Ctrl+Left/Right go to the nearest word start that way on screen (_word_visually), else on in reading order. _orderedkeeps every column of a line: a lam-alef ligature's spare alef column becomes a""cell beside the next blank after it in the text (one blank wider, never a gap inside the word), or at the line's end, so tab stops after it hold.- A block the cursor marked is what is painted between its ends (Word's visual selection):
FileEditor._painted_blockis the veryblocktuple_set_block/_set_orderedmade (kept through_follow_edit, and through a recase that changes lengths, its ends moved through the recased text); any other block -- ^K C's copy, ^K R's read -- is the stretch it is. On an RTL line a caret is its cell's right-hand edge and the line's end the cell left of the text (_painted_spans)._block_runs()turns it into the text stretches it holds (_painted_spansper reordered line: between the ends on one line; from the start's cell towards the end the line reads to on the first; from the line's beginning to the end's cell on the last; whole lines between). Every stream-block command goes through the runs --block_text,_take_block, ^K C/V,_recase,block_lines,_cursor_in_block, the right-click test; render lights a reordered line by_block_picked. A new stream-block command must use_block_runs(), neverdocument.text(*self.block). Line-based ones (indent, format) are unchanged. - A column block's columns are painted columns (rectangle straight on screen): its corners are made painted --
_here(Shift+movement,_visual_column),_mark_span/switch block (_painted_column), ^K C/V;_rect_spansgives what a line paints in them (scattered text stretches on a reordered line,columns.spanelsewhere), used by pieces, delete, recase, sort keys, calc,block_lines, the cursor-in-block test; render compares the painted column for a rectangle (a match stays lit by the text's columns)._line_spans(number)is any block's stretches on a line; Selected text search covers first..last stretch andfindpasses over a match not wholly inside one. ^K I/U at a column block's left edge work on what is painted there:_painted_inserttries the blanks before and after the character painted at the edge, the preferred side first, lays each out, and keeps the one painting the blanks nearest the edge; on a right-aligned line they go just left of the edge (what is painted from the edge on stays put, the rest grows leftwards), and_text_columnmakes ^K U take the cell left of it, undoing ^K I. A stream block and the lone-line unindent still work at the line's start in the text (a Hebrew line's right-hand end). Combining marks join their letter's cell in both rows (viewer.decode_cells,editor.columns.cells). Application._unmirrorruns on every event as it comes off the queue (once, so derived double-clicks are already converted; the original event's id is what_postedtracks): a mouse x is flipped, and flipped again inside an unmirrored widget (Widget.unmirrored()); Left/Right swap when the focused widgetarrows_mirrored()(InputLine too: it moves by painted place, so the swap is always right on a mirrored screen)._cursormaps the caret through_mirror_xthe same way.- InputLine (so CommandLine and every field) moves by painted place once the value holds right-to-left text or
sits on a mirrored surface (
_bidi()): Left/Right to the neighbouring caret column (_step), Ctrl to the nearest word start that way (_word_step), off the painted text on to the end lying that way (the line scrolls). A selection is the cells painted between the anchor's caret and the cursor's (picked(), the editor's rule): copied in text order, deleted wholly, replaced at its first index;selectionis its first..last for callers that want a range. Plain text keeps the index arithmetic. - Direction:
Application.direction(auto/ltr/rtl);autoisi18n.is_rtl(), a catalogue's[meta] direction = "rtl"(he, ar). Set from[interface] direction(Options > Settings > Interface, Text direction, applied at once byShell.settings), which--directionoverrides for a session. A change -- of it or of the language -- repaints everything (no underlay reuse), mirrored or not on the next frame (tested both ways, and dragging and sizing a window on a mirrored screen,tests/test_windows.py). - Columns in a translated table are laid out a column at a time, never a translated string spaced to fit (a
Hebrew one turns round as a whole and sits against the far edge): the attributes grid's headings are a Label per
column at
Cluster.column_x(i)(public, measured withdisplay_width), the Top Ten's headings and lines a Label per column attetris.SCORE_COLUMNS; the dBase structure box, whose field names are ASCII by the format, holds its rows left to right with an LRM opening every cell (db_window._table_row) -- one LRM at the line's start is not enough, two Hebrew cells with blanks between run together into one reversed stretch.
Terminals: TerminalInfo.bidi
"app"(default): cells are sent as laid out;TerminalsendsCSI 8 l(ECMA-48 BDSM reset, explicit) before the screen is cleared -- VTE keeps the mode per line from when the line is made -- andCSI 8 hwhen it gives the screen back, so the console's programs get VTE's own bidi. Ignored (logged) by kitty/ghostty.terminal.bidi_switch(info)builds both strings:TerminalInfo.reordersnames a terminal that reorders and ignores BDSM but has its own switch, sent beside it (BIDI_SWITCHES): Konsole (KONSOLE_VERSION)OSC 50;BidiRenderingEnabled=false/true, mlterm (MLTERMorTERM=mlterm*)OSC 5379;use_ctl=false/true(use_bididoes nothing). Neither can be asked what it was, so leaving turns it back on, both defaults.TerminalInfo.multiplexer: inside screen (STY) each sequence goes out wrapped inESC P … ESC \, which screen passes on (works for VTE's BDSM and Konsole's OSC 50). Inside tmux (TMUX) nothing gets through -- VTE's BDSM, Konsole's and mlterm's switches all failed withallow-passthrough ontoo, with or without arefresh-clientafter -- so the terminal's own bidi has to be switched off by hand there.- Probed (Oct 2026, the scratch probe paints
abc לאאך 123 def: a reordering terminal puts123left of the Hebrew): VTE (xfce4-terminal) reorders, BDSM stops it; Konsole 23.08 and mlterm 3.9.3 reorder, only their own switch stops them (mlterm right-aligns an RTL line too); alacritty and ghostty reorder nothing (ghostty 1.4 also dropped two probe rows, a frame and Arabic, unexplained); kitty as below. Untested: WezTerm, foot (Wayland only), Windows Terminal.
--bidi {app,words}overridesNAVKIT_BIDI, which overrides the detection.- No tier hands bidi back to the terminal, deliberately: every terminal probed that does bidi (VTE, Konsole, mlterm) reorders a whole screen row as one paragraph, frames and side-by-side panels included, so text must reach it laid out with its own bidi off.
"words"(kitty:TERM=xterm-kittyorKITTY_WINDOW_ID;NAVKIT_BIDIor--bidioverrides):screen.for_wordssends kitty glyphs pre-reversed for it to put back, styles left in place (kitty moves glyphs, not colours). A run is broken by a space, a box/block character or a bold/italic change, not by colour; a run starting with Arabic is reversed to its end (Latin after it included); Hebrew is reversed a stretch of Hebrew letters at a time. Found by probing kitty, undocumented: re-probe (lamed/final kaf, Latin markers) if kitty changes. Re-probed on kitty 0.32.2 (Oct 2026) against VTE, Navigator's viewer onabc سلام xyz,abسلام xyz(Arabic straight after Latin in one run), lam-alef, Hebrew withך,a1b שלום2 c: all identical but niqqud, whose marks kitty places imperfectly.
Checking it
tests/test_bidi.py. By eye: nav --language he / --language ar (or --direction rtl) in a VTE terminal, kitty
and ghostty, Konsole, mlterm, alacritty; xdotool + import can capture windows (Konsole and mlterm: find the window
by --class, Navigator retitles it) (kitty: capture after windowactivate --sync, or it comes out
black; find a window by its process, not its title, or the keys go to another Navigator). Reading Hebrew order off a
screenshot is unreliable; use letters with ascenders/descenders (ל, ך) or Latin next to Arabic.
tools/screenshot.py --language he paints a mirrored scene to docs/screenshot-he.svg;
NAVIGATOR_PTY_TESTS=1 ./venv/bin/python -m pytest tests/test_pty_end_to_end.py runs --language he on a real pty
(mirrored menu bar, CSI 8 l/8 h, quitting).
Cost (Oct 2026, 300x80, bench in the session's scratch): the mirror 1.4 ms a frame, kitty's pass 11.6 ms. A
screen of rows not seen before costs python-bidi's layout: the editor 57 ms (8 ms in Latin), the viewer 27 ms (14);
the same screen again is the Latin cost (10 ms, 15 ms), layout/_ordered_cells being cached.
What is left
reference/backlog.md lists what is still to do for right-to-left text, by priority. Update it as items are done.