Skip to content

[209_21] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题#3276

Open
JackYansongLi wants to merge 45 commits into
mainfrom
jl/listing-algorithm
Open

[209_21] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题#3276
JackYansongLi wants to merge 45 commits into
mainfrom
jl/listing-algorithm

Conversation

@JackYansongLi
Copy link
Copy Markdown
Contributor

@JackYansongLi JackYansongLi commented May 7, 2026

如何测试

  1. 清除缓存并重启 Mogan
    • macOS: 删除 ~/Library/Caches/MoganLab,然后重启 Mogan
    • Windows: 删除 %appdata%/MoganLab%localappdata%/MoganLab,然后重启 Mogan
    • Linux: 删除 ~/.cache/MoganLab,然后重启 Mogan
  2. 打开 Mogan,新建文档,选择 generic 样式
  3. 插入 algorithm 环境(插入 -> 程序 -> Algorithm
  4. 在 algorithm 中插入 listing 环境(插入 -> 程序 -> Listing -> Verbatim
  5. 在 listing 中插入以下伪代码结构:
    • algo-ifalgo-whilealgo-foralgo-if-else-if
  6. 预期结果:所有行(包括头部、body、结束标记)都有连续行号
  7. 测试文件参考 TeXmacs/tests/tmu/209_21.tmu

What

修复在 algorithm 环境中使用 listing 时,algo-ifalgo-whilealgo-foralgo-if-else-if 等条件/循环结构的头部行及其内部语句没有行号的问题。

Why

listing 宏通过 ext-numbered-prog 为每行包裹 numbered-line 来生成行号。原始逻辑对普通代码块(如 python-code)是对的(递归进入多行节点),但对算法宏(如 algo-while)不对——算法宏需要被拆开成"头部 + body + 尾部",每部分都要独立编号。

How

TeXmacs/progs/utils/misc/markup-funcs.scm 中:

  1. 保留原有 ext-numbered-sub/ext-numbered-line:处理普通多行节点(如 python-code)的高亮和行号,保持结构不变
  2. 新增 is-algo-macro?:识别所有算法控制结构宏
  3. 新增 wrap-algo-macro:将算法宏展开为多个 numbered-line 行(头部、body、尾部),body 行通过 indent-lines 恢复缩进
  4. 新增 build-if-else-if/build-else-if:递归展开 algo-if-else-if 的多分支条件结构
  5. 修改 ext-numbered/ext-numbered-prog:遇到算法宏调用 wrap-algo-macro 拆开;其他多行节点沿用原来的 ext-numbered-sub

TeXmacs/progs/generic/generic-edit.scm 中:

  1. 新增 algo-macro-tagsfind-algo-macro-ancestorcursor-in-algo-macro-first-param?
  2. 新增 kbd-enter 多态定义:在 listing 环境内的 algo-ifalgo-while 等宏第一个参数中按 Enter,跳到第二个参数(模拟 End + Right Arrow)

TeXmacs/progs/prog/prog-menu.scm 中:

  1. 菜单项 "Algorithm"specified-algorithm 改为 algorithm

@JackYansongLi JackYansongLi force-pushed the jl/listing-algorithm branch from b244109 to 543ae6a Compare May 7, 2026 02:50
@JackYansongLi JackYansongLi changed the title [202_115] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题 [222_81] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题 May 7, 2026
@JackYansongLi JackYansongLi marked this pull request as draft May 7, 2026 02:52
@JackYansongLi JackYansongLi marked this pull request as ready for review May 8, 2026 07:13
@JackYansongLi JackYansongLi changed the title [222_81] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题 [209_21] 修复 algorithm 环境中 listing 对条件结构缺失行号的问题 May 8, 2026
Add wrap-algo-macro and wrap-algo-if-else-if in markup-funcs.scm to
expand algorithm macros (algo-if, algo-while, algo-for, etc.) into
numbered-line items, ensuring all lines (headers, body, footers) get
continuous line numbers when used inside listing environments.

Refs: #222_81
No changes needed to algorithm macro definitions; line numbering
fix is handled entirely in markup-funcs.scm.
- Add specified-algorithm test case to 222_81.tmu
- Simplify testing instructions in 222_81.md
GUI-inserted algorithm macros may omit optional arguments (e.g. algo-if
without cond). Add arity checks and fallback logic to prevent malformed
tree construction when arguments are missing or empty.
specified-algorithm has two args (intro + body) and places cursor
in the intro area by default. Users often type code in intro by
mistake, causing cursor positioning issues when inserting listing.
Change menu to create plain algorithm (single body arg) instead.
When algo-if-else-if is used as an environment (<\algo-if-else-if>...),
tm-children returns a single document node containing all arguments.
Add logic to unwrap the document and filter out empty paragraphs
before processing arguments.
- Add listing-active flag to env-program.ts
- Add build-if-else-if and build-else-if helpers in markup-funcs.scm
- Properly expand algo-if-else-if (both direct and environment forms)
  into individually numbered lines matching algorithm2e style
- Filter out next-line/new-line nodes when parsing environment form args
- Replace wrap-algo-body with ext-apply-on-paragraphs to apply numbered-line
  recursively to all paragraphs inside algorithm macro bodies
- This fixes missing line numbers for content inside algo-if, algo-while,
  algo-for, and algo-if-else-if when used in listing mode
- Add direct-form algo-if-else-if test case to 222_81.tmu
- Replace original ext-numbered-sub/line approach with wrap-listing-line
  that properly expands algorithm macros into individually numbered lines
- Add is-algo-macro?, wrap-algo-macro, wrap-algo-body, build-if-else-if,
  build-else-if helpers
- wrap-algo-body now flattens multi-line non-document nodes (like concat)
  to ensure nested body paragraphs get independent line numbers
- algo-if-else-if handles both direct form and environment form by
  extracting args from document children or direct children
- Fix crash caused by calling ext-apply-on-paragraphs with string
…e nodes

- Keep original ext-numbered-sub/ext-numbered-line for all non-algorithm macros
- Add is-algo-macro? predicate and wrap-algo-macro to specially expand
  algo-if/algo-while/algo-for/algo-if-else-if etc. into flat numbered-line list
- wrap-algo-body uses ext-numbered-sub for non-algo multi-line nodes,
  preserving python-code/shell-code highlighting and structure
- Revert env-program.ts to main (removed unused listing-active flag)
@JackYansongLi JackYansongLi force-pushed the jl/listing-algorithm branch from 1639483 to f983f1a Compare May 8, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant