diff --git a/TeXmacs/plugins/gnuplot/goldfish/tm-gnuplot.scm b/TeXmacs/plugins/gnuplot/goldfish/tm-gnuplot.scm index 9fe073aead..835b73c9df 100644 --- a/TeXmacs/plugins/gnuplot/goldfish/tm-gnuplot.scm +++ b/TeXmacs/plugins/gnuplot/goldfish/tm-gnuplot.scm @@ -40,14 +40,11 @@ ) ;let ) ;define -(define (gnuplot-read-code) - (define (read-code code) - (let ((line (read-line))) - (if (string=? line "\n") code (read-code (string-append code line))) - ) ;let - ) ;define +;; 协议读取统一由 (texmacs protocol) 承担:兼容 R7RS read-line(不保留行尾 +;; 换行),实现见 read-paragraph-by-visible-eof - (read-code "") +(define (gnuplot-read-code) + (read-paragraph-by-visible-eof) ) ;define (define (gen-temp-path) diff --git a/TeXmacs/plugins/goldfish/goldfish/texmacs/protocol.scm b/TeXmacs/plugins/goldfish/goldfish/texmacs/protocol.scm index 6fc568b84a..38d1b1dd3b 100644 --- a/TeXmacs/plugins/goldfish/goldfish/texmacs/protocol.scm +++ b/TeXmacs/plugins/goldfish/goldfish/texmacs/protocol.scm @@ -64,10 +64,36 @@ (flush-any (string-append "latex:" msg)) ) ;define + ;; 行尾归一:R7RS 的 read-line 不保留行尾换行(goldfish >= 18.11.40 的 + ;; stdin 实现);18.11.39 及更早的 stdin 实现用 fgets,保留换行。插件 + ;; 协议以逐行拼接还原段落,故新语义下须补回换行。 + (define (read-paragraph-line-with-eol line) + (let ((n (string-length line))) + (if + (and (> n 0) (char=? (string-ref line (- n 1)) #\newline)) + line + (string-append line "\n") + ) ;if + ) ;let + ) ;define + + ;; 段末标记:同时接受带/不带行尾换行(含 CRLF)的 ""。旧 goldfish + ;; 送来的行自带 "\n",新 goldfish 不带;两种都认,升级前后行为一致。 + (define (read-paragraph-eof-marker? line) + (or (string=? line "") + (string=? line "\n") + (string=? line "\r") + (string=? line "\r\n") + ) ;or + ) ;define + (define (read-paragraph-by-visible-eof) (define (read-code code) (let ((line (read-line))) - (if (string=? line "\n") code (read-code (string-append code line))) + (if (read-paragraph-eof-marker? line) + code + (read-code (string-append code (read-paragraph-line-with-eol line))) + ) ;if ) ;let ) ;define diff --git a/TeXmacs/plugins/llm/progs/llm/tests/chat-protocol-test.scm b/TeXmacs/plugins/llm/progs/llm/tests/chat-protocol-test.scm index 195eec8d88..7c30293d68 100644 --- a/TeXmacs/plugins/llm/progs/llm/tests/chat-protocol-test.scm +++ b/TeXmacs/plugins/llm/progs/llm/tests/chat-protocol-test.scm @@ -16,6 +16,7 @@ (import (liii json)) (import (liii os)) (import (liii path)) +(import (texmacs protocol)) (check-set-mode! 'report-failed) @@ -467,6 +468,24 @@ ) ;let* ) ;define +;;; ---------- read-paragraph-by-visible-eof:R7RS read-line 兼容 ---------- + +(define (test-read-paragraph-eof-marker) + ;; 18.11.40:read-line 不带行尾换行,helper 需自行补回; + ;; 18.11.39:行内已带换行,helper 原样拼接。两种语义期望结果相同。 + (with-input-from-string "line1\nline2\n\n" + (lambda () (check (read-paragraph-by-visible-eof) => "line1\nline2\n")) + ) ;with-input-from-string + ;; 空段落 + (with-input-from-string "\n" + (lambda () (check (read-paragraph-by-visible-eof) => "")) + ) ;with-input-from-string + ;; 空行也保留 + (with-input-from-string "a\n\nb\n\n" + (lambda () (check (read-paragraph-by-visible-eof) => "a\n\nb\n")) + ) ;with-input-from-string +) ;define + ;;; ---------- 测试套件入口 ---------- (tm-define (regtest-chat-protocol) @@ -484,5 +503,6 @@ (test-fake-llm-attachment-injection) (test-fake-llm-no-attachments) (test-chat-wire-encoding) + (test-read-paragraph-eof-marker) (check-report) ) ;tm-define diff --git a/TeXmacs/plugins/quiver/goldfish/tm-quiver.scm b/TeXmacs/plugins/quiver/goldfish/tm-quiver.scm index 632672e4e0..337442793b 100644 --- a/TeXmacs/plugins/quiver/goldfish/tm-quiver.scm +++ b/TeXmacs/plugins/quiver/goldfish/tm-quiver.scm @@ -43,16 +43,11 @@ (flush-verbatim "Liii STEM interface to Quiver") ) ;define +;; 协议读取统一由 (texmacs protocol) 承担:兼容 R7RS read-line(不保留行尾 +;; 换行),实现见 read-paragraph-by-visible-eof + (define (quiver-read-code) - (define (read-code code) - (let ((line (read-line))) - (if (or (eof-object? line) (string=? line "\n")) - code - (read-code (string-append code line)) - ) ;if - ) ;let - ) ;define - (read-code "") + (read-paragraph-by-visible-eof) ) ;define (define (gen-temp-path) diff --git a/TeXmacs/plugins/tikz/goldfish/tm-tikz.scm b/TeXmacs/plugins/tikz/goldfish/tm-tikz.scm index 7f6d7c6f15..c6395288bf 100644 --- a/TeXmacs/plugins/tikz/goldfish/tm-tikz.scm +++ b/TeXmacs/plugins/tikz/goldfish/tm-tikz.scm @@ -32,16 +32,11 @@ (flush-verbatim "Liii STEM interface to TikZ") ) ;define +;; 协议读取统一由 (texmacs protocol) 承担:兼容 R7RS read-line(不保留行尾 +;; 换行),实现见 read-paragraph-by-visible-eof + (define (tikz-read-code) - (define (read-code code) - (let ((line (read-line))) - (if (or (eof-object? line) (string=? line "\n")) - code - (read-code (string-append code line)) - ) ;if - ) ;let - ) ;define - (read-code "") + (read-paragraph-by-visible-eof) ) ;define (define (decode-texmacs-markup code)