RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Text editor
- Never give Pango more than a few lines. One layout of 1 MiB took 46 s
(cost grows faster than the line count).
view.ziglays out one segment at a time (lines.zig: a logical line, or a piece of one folded atfold= 2 KiB, cut after a nearby space) in a content-keyed cache (Layouts), so edits need no invalidation. A scrollbar drag lays out a new piece per motion: 16 KiB pieces stuttered at 16–38 ms, 2 KiB take about 5 ms. Pieces are a function of the line start alone, so walking up or down finds the same ones; keeplines.zig's property test green when touching it. - Scroll is
Document.top(a byte offset) plusTab.dypixels into that line, never a pixel count from the file start (that needs every line's height).Document.replaceshiftstopand the line waypoints (retarget) so edits above the view don't move it.Port.clampafter any change;locatereturns null for a position more than three viewports away, so jump (jumpTo) instead of walking. - Scrollbar units are pixels (exact) up to
exact_limitand bytes above it, because a drag in progress must not see its units shift as the height estimate improves. Line numbers come from sparse waypoints (linesBefore). - Large saves are written in place (
Operation.startSaveborrow, abovecopy_limit) andDocument.lockedrefuses edits meanwhile; smaller ones are copied. Anything that frees or reallocates the text during a save is a use-after-free. Loads read straight into one buffer sized fromfstat, sniff the first 64 KiB for binary data and refuse files that won't fit inMemAvailable(ensureMemory). - Benchmark in ReleaseSafe. Debug makes the vector scans ~50x slower and
fakes a regression. Editing near the start of a huge file memmoves the tail
(about 100 ms per keystroke at 2 GB); a gap buffer would fix it, but every
text.itemsreader (segmenter, find, IME, clipboard) would need to learn it. - A selected line break is not in the segment layout, so
paintLinedraws the highlight out to the margin itself (wrapped mode only, as Pango did).
Line glide
- Edits that move lines glide instead of snapping (
reflow.zig): Enter pushes the lines below down and carries the rest of its line onto a new one, Backspace/Delete/undo joining or removing lines pulls them up, and a word that now wraps shifts what follows. Layout, hit tests and the scrollbar never see it: rows stay at rest andpaintLineonly gets anOffset(dx,dy,alpha) for the row that starts at one byte offset. Plain typing and cursor movement produce no offsets, so they cost one loop over the visible rows. - The edit span, not a diff, finds the same line again.
Document.replacemerges every edit sincetakeChangeintoChange(lo,suffix, lengths): a row starting beforelokeeps its start, a row at or afterhi()startednew_len - old_lenbytes earlier.stepReflowruns beforestepCaretin each loop pass, takes the change and plans;renderends withremember, which stores the visible rows at rest. Where a row was drawn is its rest place plus the move still in flight atnow, so an edit during a glide continues from the screen, not from the old target. - All or nothing. A line the edit made that has text (paste), a vertical
move over
max_glide(120 px, the caret's jump rule), another tab, rebuilt layouts (resize, font, wrap), a change the remembered rows don't describe, orcaret_motion_ms = 0/reduced motion snap every row. Lines that are empty (Enter at a line's end or start) are fine. The rest of a split line starts where the text before the break ends and fades in, so it doesn't overprint the line above while it crosses it. - The caret rides its line (
Caret.Shift): its box is the rest position plus the offset of the row holding the cursor, solid while any line moves, so the two never drift apart; a join leaves the cursor's row at rest and the caret snaps as before. - Frames are paced by the display. The duration is 1.5 x
caret_motion_ms(one setting for both).reflow_dirtyasks for a full repaint (damage is not just the caret) and, unlikecaret_dirty, waits for the frame callback; one last frame is drawn at rest when it ends. Nothing runs when idle.tests/editor_visuals.pysamples a frame mid-glide (screenshots show the frame at request time, so sample ~100 ms after the key); unit tests are inreflow.zig.
Drafts
- Unsaved text survives closing, crashes and reboots.
drafts.zigowns the files ($XDG_STATE_HOME/rediwm/editor-drafts/:session.json, one<name>.txtper modified tab,lock; all 0600 in a 0700 directory, each replaced through a synced temporary name, strays swept after every write).app_drafts.zigdecides what and when. A tab is kept when it is modified and not an empty untitled one; clean tabs are not restored. A restored tab hasdoc.saved = nulland the file'sstampfrom before, so saving it still tripsFileChangedif the file moved on. - Writes follow
app_drafts.signature(tabs, revisions, modified, paths, selected tab; not the cursor), debounced 1 s after the last change and at most 5 s into continuous typing, on a thread over copied text. No timer runs unless a change is unwritten: a tab switch with nothing to keep arms none. State that should trigger a write belongs in the signature. A failed write is retried after the usual delay, never in a loop. - Drafts are for a session that ends some other way. Closing the window
or a tab asks to save as usual (Discard deletes the draft). A SIGTERM/SIGHUP/
SIGINT (they write through the eventfd the writer uses to wake the loop), a
dying compositor or a crash leaves the drafts, and
deinitwrites last. Tabs overtext_limit(8 MiB; 64 MiB together) are not kept. - Tests that start the editor need a scratch
XDG_STATE_HOME, or they restore and overwrite the real drafts.
Tab strip
- Tabs share the strip left of the new-tab button,
tab_max(176) down totab_min(112) wide; beyond that it scrolls by pixels (tab_scroll), by wheel over the strip.revealTabbrings the selected tab into view only when the selection, the tab count or the width changed, so typing never snaps a strip the user scrolled away. Go throughtabX/tabAt/tabCloseBox; a tab at the edge is partly drawn (the strip is clipped), so hit tests must stay insidetabRight(). Names are ellipsized; the unsaved dot never is.
Open Recent
- The history is Files' (
files/recent.zig,recently-used.xbel), not an editor file.app_recent.zigrereads it each time the menu opens (text files that still exist, newest 10) andrecordRecentadds to it after a load or save, on a short-lived thread (it rewrites the whole file, which GLib loads and saves). GLib drops a bookmark with no application entry, so tests that seed the file need one. The editor links glib for this. - The submenu is drawn by the editor beside the menu (
menu_selectedstays on the Open Recent row,sub_selectedis its own highlight; keyboard focus is in the submenu wheneversub_selectedis set). It overlaps the menu's edge so the pointer never crosses a gap. Menu rows are indexed byAction: the test helpermenu_actionintests/editor.pymirrors the row and separator order.