Skip to content
mgubi edited this page Nov 5, 2026 · 16 revisions

Code lines: large source files as blocks of lines

How TeXmacs Upsilon opens and edits source files of hundreds of thousands of lines: a document of lines typeset as a block of lines, with its own bridge and box, when the environment variable code-lines is true. This page sums up the goals, what the block does, the timings, the design decisions and what could come next.

  • Pull request: #798 (to upsilon_texmacs). Tracking issue: #797. Origin: #710 (a 19.8 MB parser.c passed 3 GB of memory while it was typeset).
  • Notes of the developers, on the branch of the PR and then on upsilon_texmacs: src/docs/code-lines.md, next to src/docs/code-features.md (Code features).

Goals

  1. TeXmacs as a real editor of source files: a large file opens at once and a key costs the same at line 10 and at line 150000.
  2. No loss of the features of programs: tree-sitter colours, the language servers (diagnostics, semantic colours, inlay hints, folds, edits), markup in a line, folds; the block is one more client of the line state of the core, not a third mechanism.
  3. Files saved as they were loaded: tabs, control characters and line ends kept byte for byte.
  4. Paper mode and printing still work: the page breaker gets the same penalties as for an ordinary document of lines.

What the block is

(document "line 0" "line 1" ...)      with code-lines = true

There is no tag: the lines are an ordinary document, and the environment variable code-lines (default false) says that it is typeset as lines of code.

  • Source and text files: a source file (source_document_to_tree) or a text file (verbatim_document_to_tree) gets the variable in its initial environment unless the preference code lines is off (TEXMACS_CODE_LINES=0 or 1 overrides it, for tests).
  • Inside another document: a block is <with|code-lines|true|<document|...>>.
  • Switching: Format › Source code › Lines of code switches a document between lines and paragraphs. It is offered for programs and texts, and doesn't mark a source file or a text as modified.
  • Preference: Preferences › Convert › Verbatim › "Open source files and texts as lines of code" (on by default).

The bridge of a document reads the variable when it is typeset. It then either makes its paragraph bridges or hands the document to bridge_code; a long file never gets a bridge per line. A plain line is a row of a monospaced font on a grid of cells: it has no line item, and outside paper mode no paragraph of its own.

Since the tree is the same, the lines of a source file are its body, as without the block. Editing, undo, search, the cursor, the converters, the language servers, tree-sitter and go-to-line see a document of lines and needed no change. An older TeXmacs ignores the variable and shows the lines as paragraphs.

Features

Papyrus mode the block is one box (code_box), which draws only the visible lines from the live document; a sparse list holds the special lines (wrapped, with markup, folded); a click or a redraw finds its line by a binary search
Paper mode a line is a paragraph of rows made from templates of paragraphs of 1 to 5 rows, so that pages break as in a document; rows are made again only for the lines which changed
Records of the lines kept in place by the notifications of the editor (insert, remove, split, join, assign); TEXMACS_TYPESET_CACHES=check makes them all again and counts the differences (suite code-lines-check)
Colours languages with states at the starts of lines (prog languages, C++ without tree-sitter) use a lexer of the block which keeps those states; the others, tree-sitter included, are asked for the colours of the line when it is drawn
Soft line breaks long lines wrap at the width of the page, after a space when there is one
Markup in a line strong, em, colours, math, images...: the line is typeset by the concater and put on the grid; option code-tall-objects = shrink scales a tall object to the height of a row (clicks, cursor and selections scaled too)
Language servers semantic colours and inlay hints drawn by the box itself, as the syntax colours, only for the visible lines: a key costs the same with or without a server; folds through code_line_fold_state (hidden lines take no room, the line before ends with "..."; not when printing)
Tabs and control characters in source files, kept as <#9> and <#x> and saved byte for byte; tabs stop every prog-tab-width characters and show as a small arrow; Format > Source code > Show invisible characters also shows spaces, line ends and control characters (texts are imported as before, their tabs as spaces)
Export unchanged: the converters see the document of lines, as without the block

Timings

A C file of 200000 lines (40000 functions), the cursor at line 150000, s7 build, macOS, measured on 4 November 2026:

with the block without
load and typeset (papyrus) 2.4 s 9.5 s
a key, papyrus 14 ms 50 ms
Return, papyrus 16 ms 152 ms
switch to paper 1.1 s 5.6 s
a key, paper 270 ms 360 ms
Return, paper 530 ms 900 ms

The first prototype, on Linux (Qt 5 offscreen, Guile 1.8), with a C file of 680001 lines (26.8 MB) opened as text, in papyrus mode:

without the block with the block
first typesetting 57 s 9 ms
peak memory 3.2 GB 441 MB (the tree itself)
a cursor movement 130 ms (+2.2 GB) 3 ms
Return 1.5 s 413 ms

What is left: in paper mode, mostly the linear passes of the page breaker when the number of lines changes. A file that tree-sitter can't cut into parts is the exception: for example, statements at the top level of a C file, which are syntax errors. There a key costs about 1 s in both modes. Most of that is a useless attempt by tree-sitter to cut the file again at each key, which a separate tree-sitter fix removes, #811 (0.4 s left).

Design decisions

  1. A typesetting mode, not a tag. The lines stay a document, and only its typesetting changes, through an environment variable read by the bridge of the document. Every tool which works on a document of lines works unchanged, and older versions open such files as paragraphs. The first version used a tag code-lines around the document, which brought its own costs (see Audits below).
  2. Nothing per line unless needed. A short plain line has no record and no box; a box is made for a line only when the cursor, a selection or a path goes into it (subbox). The generic code that walks all the children of a box does not visit the lines (subnr is 0).
  3. One mechanism with the features of programs. The block uses the line state of the core (layers, folds), as described in Code features; tree-sitter and the language servers see the same document of lines as without it.
  4. Paper mode reuses the stacker's rules. Rows are made as those of real paragraphs, so that page breaking, penalties and printing are those of a document of lines.
  5. Checked caches. Every kept record, row and colour state can be compared with what is made again from scratch (TEXMACS_TYPESET_CACHES=check), and the suite code-lines runs both ways.

Known differences

  • The paragraph after a block inside a document is about a pixel higher in papyrus mode. The block is one tall box for the page layout, and fixing this would add a level to all its box paths.
  • A block is not aligned by the paragraph mode (centred or right): its lines start at the left margin, as in an editor of code. In paper mode, the cursor after such a block is then at the right margin.
  • A block nested in markup (a with, a surround, a proof), in papyrus mode, is one box in the line of its paragraph, with the places before and after it at the two ends of that line: the cursor after the block is on its last line but at the right margin, Up and Down from the paragraphs around skip it, a click at the left end of a line goes before it, and Right from the end of the last line may go past some places after it. Moving those places into the block's box was tried and dropped: it needs the exact rules a line of text uses to tell apart places at the same spot, and its approximations left the cursor stuck or skipping places.
  • A line doesn't change the environment of the lines after it.
  • A document whose lines hold paragraphs (a document nested in a line) is typeset as paragraphs: a line of code is one row, and its paragraphs couldn't wrap.

General fixes found on the way

  • The cursor in documents taller than 2^31 units (about 1300 A4 pages): box_rep::distance cut distances to 32 bits, and go_up/go_down took the ends of the document at 2^30 units.
  • Lists without deep recursion: the functions of list.cpp are loops (a list of 10^5 cells overflowed the stack).

Audits

The PR is audited by fresh agents until an audit finds nothing. Each defect found was fixed with a check that fails without the fix:

  • The box and paper mode:

    • colour states after lines inserted or removed by a language server;
    • a block of one line with markup that was not typeset;
    • a line with markup typeset twice;
    • lines left blank by an interrupted repaint;
    • clicks beyond a wrapped row.
    • inlay hints: room put after their character instead of before, clicks next to a hint, and the order of the text of a printed file.
    • around a block nested in markup (papyrus): Up and Down skipped it, a click left of a line went before it, and the cursor after it was at the right margin; the places before and after it are now inside the block's box. Later audits found selections across an end of the block, a QED after it, an empty last line, and places at the same spot (the end of the last line and after the block), which the box told apart only approximately; the box was then removed, and these places are back in the line of the paragraph.
  • The tag: in the first version, most of the defects came from the tag itself:

    • the cursor started before the block, so what was typed went outside it;
    • the Focus menu of source files;
    • empty HTML and LaTeX exports;
    • a block inserted into a line;
    • a case was needed wherever TeXmacs lists its primitives one by one.

    Rather than fix them one by one, the tag was replaced by the environment variable, which removes that whole class of defects.

  • Texts and source files (after the change to the variable): tabs dropped in LaTeX export, no way to switch a text back to paragraphs from the interface, and files of one line left in paragraphs.

Possible developments

  • Paper mode in proportion to the change: a page breaker that redoes only the pages around an edit, so that Return in a large file in paper mode costs as much as in papyrus.
  • Tree-sitter's region in proportion to the change (syntax_region.cpp): the main cost of a key in papyrus mode.
  • Memory: the tree of the lines is now most of what a large file costs; a more compact representation of plain lines (one string per block, lines as offsets) would cut it further.
  • Lines with markup as children of the box: animations and other boxes that the generic traversals visit (relocate, the repaint log) would then work inside a line.
  • Tree-sitter in nested withs: a document inside two nested withs was not coloured by tree-sitter, in paragraph mode too. This was seen on an older upsilon build and has not been checked on the current one.
  • A colour-state test with C++: where tree-sitter is built, C++ colours come from tree-sitter, so the suite checks the lexer of the block with Scala.
  • Very long lines: the positions of the characters are computed for the whole line for each row; wrapped minified files would gain from computing them per row.

Clone this wiki locally