Skip to content

Fix: Multiline input broken in Julia executable folds#15

Open
Parth-Garg2629 wants to merge 1 commit intomgubi:mainfrom
Parth-Garg2629:fix/multiline-input-broken
Open

Fix: Multiline input broken in Julia executable folds#15
Parth-Garg2629 wants to merge 1 commit intomgubi:mainfrom
Parth-Garg2629:fix/multiline-input-broken

Conversation

@Parth-Garg2629
Copy link

Fix: Multiline input broken in Julia executable folds

**Closes #14 **


🐛 Problem

Multiline Julia code inside executable folds (Insert → Fold → Executable) was silently broken — all lines were concatenated into a single line before being sent to the Julia process, causing multi-statement blocks to fail.

Example of broken behaviour:

A fold containing:

using Random
Random.seed!(42)
rand()

Was being serialised and sent to Julia as the single line:

using Random Random.seed!(42) rand()

which raises a parse error or produces wrong results.


🔍 Root Cause

The two lines at the bottom of progs/init-julia.scm:

(when (supports-julia?)
  (plugin-input-converters julia))

were the culprit. Even when called with no conversion rules, the macro plugin-input-converters registers the plugin name into the plugin-input-converters% logic group (in plugin-convert.scm):

(tm-define-macro (plugin-input-converters name2 . l)
  (let ((name (if (string? name2) name2 (symbol->string name2))))
    (lazy-input-converter-force name)
    (logic-group plugin-input-converters% ,name)   ;; ← always runs
    `(logic-rules ,@(plugin-input-converters-rules name l))))

The predicate plugin-supports-math-input-ref checks exactly this group:

(tm-define (plugin-supports-math-input-ref key)
  (lazy-input-converter-force key)
  (logic-in? key plugin-input-converters%))        ;; ← returns #t after the call above

Once plugin-supports-math-input-ref returns #t for "julia", plugin-preprocess (in plugin-eval.scm) routes the entire input tree through plugin-math-input:

(if (and (== (car opts) :math-input)
         (plugin-supports-math-input-ref name))
    (set! t (plugin-math-input (list 'tuple name t))))

plugin-math-input flattens the TeXmacs document tree into a single math expression, stripping every newline in the process. This is what destroyed the multiline structure of Julia code cells.


✅ Fix

Remove the (when (supports-julia?) (plugin-input-converters julia)) block entirely.

Julia already registers a custom serializer (julia-serialize) via (:serializer ,julia-serialize) in plugin-configure. That serializer calls texmacs->code with "SourceCode" style and appends the <EOF> sentinel correctly — it handles the full document tree, preserving newlines. Math-input pre-processing is therefore not needed and must be kept disabled.

A detailed explanatory comment has been added in place of the removed code to prevent future regressions.


📂 Files Changed

File Change
progs/init-julia.scm Removed (when (supports-julia?) (plugin-input-converters julia)); added a detailed explanatory comment

🧪 How to Test

  1. Install the plugin and open TeXmacs / Mogan.

  2. Go to Insert → Fold → Executable → Julia.

  3. Enter a multiline Julia block, e.g.:

    using Random
    Random.seed!(42)
    rand()
  4. Execute the fold.

Before this fix: All lines are collapsed into one; Julia raises a parse or runtime error.
After this fix: Each line is preserved; Julia returns a reproducible random float (e.g., 0.6293451231426089).


📎 References

  • Reported in: mgubi/tm-julia#12
  • Relevant TeXmacs source files:
    • TeXmacs/progs/utils/plugins/plugin-convert.scm — defines plugin-input-converters macro and plugin-supports-math-input-ref
    • TeXmacs/progs/utils/plugins/plugin-eval.scm — defines plugin-preprocess which reads plugin-supports-math-input-ref

…ble folds

Calling (plugin-input-converters julia) — even with no conversion rules —
registers 'julia' into the plugin-input-converters% logic group. The predicate
plugin-supports-math-input-ref checks exactly this group:

  (tm-define (plugin-supports-math-input-ref key)
    (lazy-input-converter-force key)
    (logic-in? key plugin-input-converters%))   ; defined in plugin-convert.scm

When this returns #t, plugin-preprocess (plugin-eval.scm) pipes the input tree
through plugin-math-input which flattens the document tree into a single math
expression, stripping all newline characters. This caused every line of a
multiline Julia cell inside an executable fold (Insert -> Fold -> Executable)
to be concatenated into one line, silently breaking multi-statement blocks.

Julia already has a custom serializer (julia-serialize) that calls
texmacs->code with SourceCode style and appends the <EOF> sentinel correctly.
Math-input pre-processing is therefore not needed and keeping it disabled is
the correct behaviour.

Fix: remove the bare (when (supports-julia?) (plugin-input-converters julia))
block entirely, with a detailed explanation comment in its place.

Fixes: mgubi#12
@Parth-Garg2629
Copy link
Author

@mgubi @hammerfunctor could u please review my PR

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.

Fix: Multiline input broken in Julia executable fold

1 participant