Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.
TypeScript claude-code-mod
Copy inline math as its LaTeX source
razorback1609fd1410cd52
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2247556..ed89c75 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,7 @@ All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Cod### New- **Links, file paths and commands in the pictures are live** (picture select, fullscreen mode). Move the pointer over one: its cells show on the selection colour. A click copies it: a web link its URL, a file path its full path, a command the full command (toasts "copied the link", "copied the path", "copied the command"). A command is any inline code (for example `` `git status` ``; `` `! cmd` `` copies only `cmd`); a code block is not a target. A drag that starts on one still selects text. Refer to [Click a link, a file or a command](docs/usage.md#click-a-link-a-file-or-a-command).+- **A copy includes inline math** as its LaTeX source, for example `$A = \pi r^2$`. The mod copies the full equation when the selection covers its middle. Display math is not copied.- `IUI_DEBUG=1` writes a `select: click` line with the target of each click, and a `select: hover` line when the target under the pointer changes.### Changeddiff --git a/docs/limitations.md b/docs/limitations.mdindex 750bd4c..bc117bc 100644--- a/docs/limitations.md+++ b/docs/limitations.md@@ -9,7 +9,7 @@- On Linux with the old `clipnotify` (1.0.2 or earlier), a mouse selection (the PRIMARY selection) also makes the mod read the clipboard once. The mod changes nothing in that case.- The mod finds the words of a page in the background when the page is on the screen. If they are not ready when you release the button (or when you run `/iui grab`), the copy takes approximately 0.05 s for plain text (refer to [The lean font set](how-it-works.md#the-lean-font-set)), 0.15–0.5 s for other text, and 2–3 s for a page with a large Typst drawing. Wait for the toast "copied N chars of text", then paste.- The mod copies whole words. It copies a word when the selection covers the middle of the word. In a code block, in a long word (30 characters or more, for example a path or a URL) and in CJK text, it copies each character whose middle is in the selection.- - The mod does not copy math, the text in an image, or the text in a Typst drawing.+ - The mod copies inline math as its LaTeX source in dollars (for example `$A = \pi r^2$`), as one word: it copies the full equation when the selection covers the middle of the equation. If an inline equation breaks across two lines, select its first line. The mod does not copy display math (`$$…$$`), the text in an image, or the text in a Typst drawing.- For picture cells on the clipboard, the cells do not identify the answer. The mod uses the answer of the selected row if it knows it, else the newest answer whose pages fit the selection. A selection in one page of a long answer fits each page that is tall enough, and the mod uses the first one. Thus the text can come from a different page or an older answer of the same width. `/iui dump` shows the page that the mod used.- The mod knows only the last 64 answers, plans or files that it drew in this session.- The mod never changes the clipboard for a copy of picture cells: two sessions can have the same picture cells, and the mod cannot know which session a copy is from. A toast tells you to use `/iui grab`. Run `/iui grab` in the session that has the picture.diff --git a/render/md.typ b/render/md.typindex 928143c..16c519b 100644--- a/render/md.typ+++ b/render/md.typ@@ -61,9 +61,9 @@#let only = int(inp.at("page", default: "0"))// The probe (probe=1) and its marks: see the probe section below.#let probe = inp.at("probe", default: "0") not in ("", "0", "false", "no", "off")-#let mark(kind, ..extra) = context {+#let mark(kind, dy: 0pt, ..extra) = context {let at = here().position()- [#metadata((k: kind, p: at.page + calc.max(0, only - 1), x: at.x / 1pt, y: at.y / 1pt, s: text.size.to-absolute() / 1pt, ..extra.named()))<mdpos>]+ [#metadata((k: kind, p: at.page + calc.max(0, only - 1), x: at.x / 1pt, y: (at.y + dy) / 1pt, s: text.size.to-absolute() / 1pt, ..extra.named()))<mdpos>]}// Block code sets this so the word rule leaves its text to the line marks;// math and live drawings set no-marks. The orphan cost changes nothing@@ -568,6 +568,8 @@// A word of inline code has `pad`, the theme's padding beside the// code, when it is not 0, and `code`, the whole text of that code.// A word in a link to a URL (a string) has `href`, the URL.+// Inline math is one word, its LaTeX source in dollars, boxed as a+// word of the text's line (`s` is the math's size); see md-math.// c: a block code line: x its start, y its baseline, a and d the code// font's ascender and descender, w its width, n its characters.// b: a block starts: paragraph, heading, code block, quote, table cell@@ -576,8 +578,8 @@// its `b`, before its lines), boxed; x, y the top-left of its box, w// and h its size, `code` the block's whole text, `lang` its language// (when the fence has one).-// Math and live ```typst drawings have no marks (a drawing is scaled, so-// its marks would not be where it is drawn).+// Display math, and the text in math and in live ```typst drawings, have no+// marks (a drawing is scaled, so its marks would not be where it is drawn).// With page=<n>, only that segment is laid out, as in the whole document// (each segment is a page of its own: its own margins and labels), so a// copy probes only the pages it reads; `p` is still the PNG's page.@@ -1082,9 +1084,20 @@(src, block: false) => if block { raw(src, block: true, lang: "latex") } else { raw(src) }} else {(src, block: false) => {- let typ = mitex-convert(latex-bars(if type(src) == str { src } else { src.text }))- set text(size: (if block { display-scale } else { inline-scale }) * 1em)- math.equation(block: block, eval("$" + typ + "$", scope: latex-scope))+ let src = if type(src) == str { src } else { src.text }+ let typ = mitex-convert(latex-bars(src))+ let body = eval("$" + typ + "$", scope: latex-scope)+ let scaled(it) = { set text(size: (if block { display-scale } else { inline-scale }) * 1em); it }+ // The probe: inline math is one word, its LaTeX source, boxed as the+ // text's line (see the probe). The mark is in the equation, so it is+ // where the equation starts, also at the start of a wrapped line.+ if probe and not block { context {+ let asc = measure(text(bottom-edge: "baseline", "x")).height+ let h = measure(text("x")).height+ let w = measure(scaled(math.equation(block: false, body))).width+ let word = mark("w", dy: -asc, t: "$" + src + "$", w: w / 1pt, h: h / 1pt)+ scaled(math.equation(block: false, [#word#body]))+ } } else { scaled(math.equation(block: block, body)) }}}diff --git a/tests/copy-fixture.ts b/tests/copy-fixture.tsindex 90a0c2a..c7063cf 100644--- a/tests/copy-fixture.ts+++ b/tests/copy-fixture.ts@@ -395,3 +395,42 @@ export const TARGETS_MARKS = [{"k": "c", "p": 1, "x": 12.571428571428573, "y": 131.2720396205357, "s": 11.0, "t": "echo hello", "w": 66.2255859375, "n": 10, "a": 8.357421875, "d": 2.642578125},{"k": "c", "p": 1, "x": 12.571428571428573, "y": 146.19903962053573, "s": 11.0, "t": "ls -la", "w": 39.7353515625, "n": 6, "a": 8.357421875, "d": 2.642578125},] as const++// The same probe (same inputs) for inline math: each equation is one word,+// its LaTeX source in dollars; display math has no marks. Its PNG is 1568 x+// 256 px, 8 rows.+export const MATH_MD = "Euler said $e^{i\\pi} + 1 = 0$ and the area is $A = \\pi r^2$ here. The quick brown fox jumps over the lazy dog, then keeps running across the wide green field $-x \\le \\frac{a}{b}$ ends.\n\n$$\\int_0^1 x\\,dx$$\n"++export const MATH_PAGE = { width: 1568, height: 256, columns: 98, rows: 8 }++export const MATH_MARKS = [+ {"k": "b", "p": 1, "x": 0.0, "y": 4.36, "s": 11.0, "kind": "par"},+ {"k": "w", "p": 1, "x": 0.0, "y": 4.36, "s": 11.0, "t": "Euler", "w": 26.22705078125, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 29.32080078125, "y": 4.36, "s": 11.0, "t": "said", "w": 21.3017578125, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 53.71630859375, "y": 4.36, "s": 12.1, "t": "$e^{i\\pi} + 1 = 0$", "w": 58.45752, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 115.26757859375, "y": 4.36, "s": 11.0, "t": "and", "w": 19.24462890625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 137.60595750000002, "y": 4.36, "s": 11.0, "t": "the", "w": 16.75244140625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 157.45214890625002, "y": 4.36, "s": 11.0, "t": "area", "w": 22.451171875, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 182.99707078125002, "y": 4.36, "s": 11.0, "t": "is", "w": 8.4755859375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 194.56640671875002, "y": 4.36, "s": 12.1, "t": "$A = \\pi r^2$", "w": 43.52195222222222, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 241.1821089409722, "y": 4.36, "s": 11.0, "t": "here.", "w": 26.33447265625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 270.6103315972222, "y": 4.36, "s": 11.0, "t": "The", "w": 19.72802734375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 293.4321089409722, "y": 4.36, "s": 11.0, "t": "quick", "w": 27.96728515625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 324.4931440972222, "y": 4.36, "s": 11.0, "t": "brown", "w": 32.00634765625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 359.5932417534722, "y": 4.36, "s": 11.0, "t": "fox", "w": 15.7373046875, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 378.4242964409722, "y": 4.36, "s": 11.0, "t": "jumps", "w": 31.173828125, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 412.6918745659722, "y": 4.36, "s": 11.0, "t": "over", "w": 22.5048828125, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 438.2905073784722, "y": 4.36, "s": 11.0, "t": "the", "w": 16.75244140625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 458.1366987847222, "y": 4.36, "s": 11.0, "t": "lazy", "w": 20.75390625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 481.9843550347222, "y": 4.36, "s": 11.0, "t": "dog,", "w": 23.224609375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 508.3027144097222, "y": 4.36, "s": 11.0, "t": "then", "w": 23.1708984375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 534.5673628472223, "y": 4.36, "s": 11.0, "t": "keeps", "w": 30.744140625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 0.0, "y": 21.666953125, "s": 11.0, "t": "running", "w": 39.2841796875, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 42.3779296875, "y": 21.666953125, "s": 11.0, "t": "across", "w": 34.16015625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 79.6318359375, "y": 21.666953125, "s": 11.0, "t": "the", "w": 16.75244140625, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 99.47802734375, "y": 21.666953125, "s": 11.0, "t": "wide", "w": 24.27734375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 126.84912109375, "y": 21.666953125, "s": 11.0, "t": "green", "w": 29.71826171875, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 159.6611328125, "y": 21.666953125, "s": 11.0, "t": "field", "w": 22.52099609375, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 185.27587890625, "y": 21.666953125, "s": 12.1, "t": "$-x \\le \\frac{a}{b}$", "w": 40.14242222222223, "h": 13.126953125},+ {"k": "w", "p": 1, "x": 228.51205112847222, "y": 21.666953125, "s": 11.0, "t": "ends.", "w": 28.48291015625, "h": 13.126953125},+] as constdiff --git a/tests/copy.test.ts b/tests/copy.test.tsindex 3eeb3af..052fad3 100644--- a/tests/copy.test.ts+++ b/tests/copy.test.ts@@ -16,7 +16,7 @@ import { clip, drawnAnswers, lastKey, results, type Job, type Result } from '../import { forgetBlits, over, rgbOf, WRITE_PL } from '../hooks/lib/blit'import { forgetRedraws } from '../hooks/lib/select'import { chunk, concat, decodePng, deflatePiece, fromBase64, headOf, tailOf, toBase64 } from '../hooks/lib/png'-import { LONG_MARKS, LONG_MD, LONG_PAGE, SMALL_MARKS, SMALL_MD, SMALL_PAGE, SMALL_PPI, TARGETS_MARKS, TARGETS_MD, TARGETS_PAGE } from './copy-fixture'+import { LONG_MARKS, LONG_MD, LONG_PAGE, MATH_MARKS, MATH_MD, MATH_PAGE, SMALL_MARKS, SMALL_MD, SMALL_PAGE, SMALL_PPI, TARGETS_MARKS, TARGETS_MD, TARGETS_PAGE } from './copy-fixture'// One screen row of a picture as Claude Code draws it: `lead` (the gutter),// then the cells of `row` from `from` to `to`.@@ -399,6 +399,17 @@ test('a real probe: a long path, a long URL that wraps and CJK text are on theirexpect(LONG.pages[0]!.every(i => i.x1 <= LONG_PAGE.width && i.y1 <= LONG_PAGE.height)).toBe(true)})+const MATH = positionsOf(MATH_MARKS as unknown as Mark[], SMALL_PPI)++test('a real probe: inline math copies as its LaTeX source; display math copies nothing', () => {+ // Rows: the paragraph's lines 0 and 2, the display math 4-6.+ const all = Array.from({ length: MATH_PAGE.rows }, (_, row) => ALL_COLUMNS(row))+ expect(textOfRun(all, MATH.pages[0]!, MATH.blocks, MATH_PAGE)).toBe(MATH_MD.split('\n\n')[0])+ // The first equation is columns 9-19, its centre in column 14.+ expect(textOfRun([{ row: 0, colStart: 14, colEnd: 30 }], MATH.pages[0]!, MATH.blocks, MATH_PAGE)).toBe('$e^{i\\pi} + 1 = 0$ and the area')+ expect(textOfRun([{ row: 0, colStart: 15, colEnd: 30 }], MATH.pages[0]!, MATH.blocks, MATH_PAGE)).toBe('and the area')+})+test('invisible marks and list markers of other themes are dropped', () => {const marks: Mark[] = [{ k: 'b', p: 1, x: 0, y: 0, kind: 'par' },