Repository navigation
Code 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 MBparser.cpassed 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 tosrc/docs/code-features.md(Code features).
- 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.
- 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.
- Files saved as they were loaded: tabs, control characters and line ends kept byte for byte.
- Paper mode and printing still work: the page breaker gets the same penalties as for an ordinary document of lines.
(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 preferencecode linesisoff(TEXMACS_CODE_LINES=0or1overrides 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.
| 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 through typeset_layered_string, 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 |
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).
-
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-linesaround the document, which brought its own costs (see Audits below). -
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 (subnris 0). - 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.
- 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.
-
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 suitecode-linesruns both ways.
- 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 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.
-
The cursor in documents taller than 2^31 units (about 1300 A4 pages):
box_rep::distancecut distances to 32 bits, andgo_up/go_downtook the ends of the document at 2^30 units. -
Lists without deep recursion: the functions of
list.cppare loops (a list of 10^5 cells overflowed the stack).
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.
-
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.
- 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 nestedwiths 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.