Skip to content

[6224] Fix headless -c file conversion - #4745

Merged
PinkMagicFly merged 2 commits into
MoganLab:mainfrom
guraltsev:pr/headless-convert
Oct 20, 2026
Merged

PinkMagicFly merged 2 commits into
MoganLab:mainfrom
guraltsev:pr/headless-convert

Conversation

@guraltsev

Copy link
Copy Markdown
Contributor

Fixes #2223

What happened

I wanted to convert documents from the command line, without opening a window, with
MoganSTEM -headless -c <in> <out>. Converting a TeXmacs document to LaTeX or to another
TeXmacs format wrote no output file at all, and nothing about the conversion was printed (only the
normal startup and shutdown lines).

Problem

For every conversion except .tex -> .tmu (which has its own branch) and PDF, the code
in TeXmacs_main generates
(load-buffer "<in>" :strict) (export-buffer "<out>").

On a clean build of main (c68a494fc, Windows), with an empty user directory and cache:

  • MoganSTEM -headless -c doc.tm doc.tex writes no file;
  • MoganSTEM -headless -c doc.tm doc.tmu writes no file;
  • MoganSTEM -headless -c blocks.tex blocks.tmu works (the special branch).

I did not trace why the generated sequence produces nothing; headless mode has no active
view, and export-buffer exports the current buffer.

Change (src/System/Boot/init_texmacs.cpp, default branch only)

The generated Scheme now:

  • calls (lazy-plugin-force) first (about 1 s);
  • takes the source format from (url-format source) (the input file exists) and the
    target format from the file suffix, (format-from-suffix (url-suffix target)). The
    output file normally does not exist yet, so url-format cannot be asked about it, and
    formats such as latex are only registered once their plugin has been initialised;
  • initialises the plugins of both formats, loads the buffer by URL
    (buffer-load source) and exports that buffer (buffer-export source target target-format), printing a message if either step fails.

The .tex -> .tmu branch is unchanged. The PDF branch (screens expanded into slides) is
unchanged except that it now carries its own load-buffer.

Verified

A local script runs conversions and checks the output text (31 checks, all pass on this
branch):

  • .tex -> .tmu: gathered/aligned/array/pmatrix rows and options (the unchanged
    branch, as a regression guard);
  • .tm -> .tex: a full document with \documentclass, \usepackage{amsmath},
    \begin{equation}, \begin{gather*}, aligned and plain text;
  • .tm -> .tmu, .tmu -> .tex, and a .tex -> .tmu -> .tex round trip.

On the clean main build, .tm -> .tex and .tm -> .tmu write nothing (above).

Not checked

  • With the new code, a failed load or export is only printed as a message; I did not
    check or change the process exit status.
  • The PDF branch and the .tex -> .tmu branch were not changed and were not re-tested
    beyond the regression guard above.
  • Linux and macOS were not tested.
  • The test script is a bash script outside the project's test layout. I am happy to port
    it to tests/ (C++) or a Scheme integration test if you tell me which you prefer.

How this fix was made

The fix was developed through human-aided debugging: I reported the symptoms, ran the
builds and checked the results by hand, and the cause was worked out interactively with
an AI assistant. The code itself was written using Claude Sonnet 5.5 (Anthropic). I
reviewed and tested it before submitting. The commit carries a Co-Authored-By trailer
to the same effect.

@PinkMagicFly PinkMagicFly changed the title Fix headless -c file conversion [6224] Fix headless -c file conversion Oct 19, 2026
guraltsev and others added 2 commits October 19, 2026 18:03
MoganSTEM -headless -c <in> <out> had two problems:

- In headless mode there is no active view, so (load-buffer ...) followed by
  (export-buffer ...) depends on current-buffer and can fail or export the
  wrong buffer.  Load and export the buffers addressed by the input and output
  URLs explicitly, initialising the plugins of both formats.  The pdf case keeps
  its dedicated branch (screens are expanded into slides).

- The format of the output file was looked up with url-format before any plugin
  was loaded.  The latex format is only registered once its plugin is
  initialised, and the output file normally does not exist yet, so the answer
  was "generic" and the export wrote the plain text only (no preamble, no
  equations).  Force the lazy plugins first and take the target format from the
  file suffix.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
- lazy-plugin-force 之后的两个 plugin-initialize 是 no-op,删除;
  source-format 绑定随之不再需要
- headless 下 scheme 输出被重定向进日志,错误信息改写
  current-error-port 才能到达终端 stderr
- 注释改为中文精简版,说明 lazy-plugin-force 必须先于格式解析

@MoonL79 MoonL79 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PinkMagicFly
PinkMagicFly merged commit 980bc1e into MoganLab:main Oct 20, 2026
6 checks passed
@guraltsev
guraltsev deleted the pr/headless-convert branch October 21, 2026 11:58
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

3 participants