Skip to content

[2223] 修复 -c 命令行转换把 .tex 等插件格式当作 verbatim 导出 - #4706

Open
bnspopi wants to merge 1 commit into
MoganLab:mainfrom
bnspopi:bnspopi/200_27/fix_cli_convert_format
Open

bnspopi wants to merge 1 commit into
MoganLab:mainfrom
bnspopi:bnspopi/200_27/fix_cli_convert_format

Conversation

@bnspopi

@bnspopi bnspopi commented Oct 11, 2026

Copy link
Copy Markdown

问题

Fixes #2223。mogan -c HW.tm HW.tex 得到的 .tex 只有纯文本、没有任何 LaTeX 标记;GUI 的 文件→导出 正常。1.2.9 正常,1.2.9.7 起复现,至今未修。

根因

不是格式检测代码的问题,是执行时序:

  1. .tex 的格式映射由 latex 插件登记:plugins/latex/progs/init-latex.scm 里 (lazy-format (latex latex-format) latex)。
  2. 插件 init 脚本本身是延迟执行的:lazy-plugin-initialize 用 (delayed (:idle 10) (plugin-initialize name))。
  3. -c 的转换命令由 tm_server 在启动末尾 exec_delayed,先于上面的 idle 延迟触发。
  4. 于是 export-buffer 调 url-format → C++ file_format → suffix_to_format → Scheme format-from-suffix,后缀表里没有 tex,返回 "generic",export-buffer 把 "generic" 改成 "verbatim",纯文本落盘。

GUI 不受影响,因为开窗建菜单时 tm_window_rep::menu_main 已 (lazy-initialize-force) 强载全部插件。-c 现在隐含 -headless(research.cpp),不开窗,这条强载路径永远不会走到。init_texmacs.cpp 里 #ifndef QTTEXMACS 分支对 ImGui 后端手动 (plugin-initialize 'latex) 的注释说的正是同一件事,只是 Qt headless 没有对应处理。

format-from-suffix 自己调的 lazy-format-force 不够:它只强载已经 lazy-format 登记过的模块,而登记动作本身在还没跑的插件 init 里。

改动

TeXmacs/progs/texmacs/texmacs/tm-files.scm

export-buffer 的格式判定抽成 export-buffer-format:

(tm-define (export-buffer-format to)
  (with fm (url-format to)
    (when (== fm "generic")
      (lazy-plugin-force)
      (set! fm (url-format to)))
    (if (== fm "generic") "verbatim" fm)))

后缀未知时先 (lazy-plugin-force) 再查一次,仍未知才退化为 verbatim。lazy-plugin-force 幂等(plugin-initialize-done? 置位后直接返回),已识别的后缀不触发任何额外加载,GUI 路径行为不变。

没有放进 kernel 的 format-from-suffix:那会让任何未知后缀的文件操作都强载全部插件,副作用面太大;导出是唯一需要这个保证的地方。

TeXmacs/progs/texmacs/texmacs/tm-files-test.scm

新增 test-export-buffer-format-forces-plugins(xmake r tm-files-test,headless)。测试进程与 -c 同样在插件初始化前执行到断言,所以它真实复现了时序:修复前 .tex 返回 "verbatim",修复后返回 "latex";真正未知的后缀仍为 "verbatim"。

说明

  • 本机没有 xmake / gf 构建环境,未能本地运行 xmake r tm-files-test 与 gf fmt,烦请以 CI 为准。改动全部在 Scheme 层。
  • -c in.tex out.tmu 方向在 init_texmacs.cpp 里已有专门路径(直接调 cpp-latex-document->texmacs),不经过 export-buffer,不受本 PR 影响。

🤖 Generated with Claude Code

mogan -c in.tm out.tex 得到的 .tex 只有纯文本、没有 LaTeX 标记,GUI 菜单导出正常。

根因:latex / bibtex 等格式由插件 init 脚本里的 lazy-format 登记,而插件本身走
(delayed (:idle 10) (plugin-initialize ...))。-c 的转换命令由 tm_server 用
exec_delayed 在启动后立即执行,先于插件初始化;此时后缀表里没有 tex,
url-format 返回 "generic",export-buffer 再退化为 verbatim。GUI 不受影响,
因为开窗建菜单时已 (lazy-initialize-force) 强载插件;-c 现在隐含 -headless,
不开窗,这条路径不会触发。

修复:export-buffer 的格式判定抽成 export-buffer-format——后缀未知时先
(lazy-plugin-force) 再查一次,仍未知才退化为 verbatim。lazy-plugin-force 幂等,
已初始化时直接返回。

测试:tm-files-test.scm 新增 headless 用例——测试进程与 -c 同样在插件初始化
前执行,export-buffer-format 对 .tex 须返回 "latex",对真正未知的后缀返回
"verbatim"。

Fixes MoganLab#2223

Co-Authored-By: Claude Fable 5.1 <[email protected]>

This branch has not been deployed

No deployments
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.

Format detection for commandline conversion using -c broken

1 participant