From 972ae99477142f77743a90d2a8ebf6ad1997edf3 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 17:52:16 +0800 Subject: [PATCH 1/6] wip --- TeXmacs/packages/environment/env-program.ts | 2 + src/Data/Convert/Mogan/to_tmu.cpp | 44 ++++++++++++++++++++- src/Typeset/Env/env_exec.cpp | 3 ++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/TeXmacs/packages/environment/env-program.ts b/TeXmacs/packages/environment/env-program.ts index 9234b7ef16..b418d35472 100644 --- a/TeXmacs/packages/environment/env-program.ts +++ b/TeXmacs/packages/environment/env-program.ts @@ -214,6 +214,8 @@ > > + + <\active*> <\src-comment> diff --git a/src/Data/Convert/Mogan/to_tmu.cpp b/src/Data/Convert/Mogan/to_tmu.cpp index ae5adc6969..fb4ed20327 100644 --- a/src/Data/Convert/Mogan/to_tmu.cpp +++ b/src/Data/Convert/Mogan/to_tmu.cpp @@ -19,6 +19,7 @@ using namespace moebius; using lolly::data::binary_to_hexadecimal; using moebius::drd::std_contains; +using moebius::drd::the_drd; const string TMU_VERSION= "1.0.2"; @@ -47,6 +48,7 @@ struct tmu_writer { void br (int indent= 0); void tag (string before, string s, string after); void apply (string func, array args); + void write_prog (tree t); void write (tree t); }; @@ -208,6 +210,42 @@ tmu_writer::apply (string func, array args) { } } +static bool is_tree_in_prog (tree t) { + return N (t) == 1 && L(t[0]) == DOCUMENT && the_drd->get_attribute(L(t), "prog") == "true"; +} + +void +tmu_writer::write_prog (tree t) { + string func= as_string (L (t)); + + // <\python-code> + write ("<\\", false); + write (func, true, true); + write (">", false); + write ("\n", false); + + tree doc= t[0]; + int doc_N= N(doc); + for (int i=0; ilabel), false); + } else { + write (doc[i]); + } + write ("\n", false); + } + + // __ + // _ means spaces, the number is controlled by tab + for (int i=0;i", false); +} + + void tmu_writer::write (tree t) { if (is_atomic (t)) { @@ -265,7 +303,11 @@ tmu_writer::write (tree t) { tag (""); break; default: - apply (as_string (L (t)), A (t)); + if (is_tree_in_prog (t)) { + write_prog (t); + } else { + apply (as_string (L (t)), A (t)); + } break; } } diff --git a/src/Typeset/Env/env_exec.cpp b/src/Typeset/Env/env_exec.cpp index a16d9bdd5a..7f54c995b1 100644 --- a/src/Typeset/Env/env_exec.cpp +++ b/src/Typeset/Env/env_exec.cpp @@ -742,6 +742,9 @@ edit_env_rep::exec_drd_props (tree t) { else if (prop == "name") { if (is_atomic (val)) drd->set_attribute (l, prop, val->label); } + else if (prop == "prog") { + if (is_atomic (val)) drd->set_attribute (l, prop, val->label); + } else if (prop == "syntax") drd->set_syntax (l, val); else if (prop == "border") { if (val == "yes") drd->set_border (l, BORDER_YES); From 1c46cfd91b32bea9dfd8c1de541b33b45124d5cf Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 17:54:01 +0800 Subject: [PATCH 2/6] wip --- src/Data/Convert/Mogan/to_tmu.cpp | 19 ++++++++----------- src/Data/Convert/convert.hpp | 6 ++++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Data/Convert/Mogan/to_tmu.cpp b/src/Data/Convert/Mogan/to_tmu.cpp index fb4ed20327..34b3ca5a55 100644 --- a/src/Data/Convert/Mogan/to_tmu.cpp +++ b/src/Data/Convert/Mogan/to_tmu.cpp @@ -210,10 +210,6 @@ tmu_writer::apply (string func, array args) { } } -static bool is_tree_in_prog (tree t) { - return N (t) == 1 && L(t[0]) == DOCUMENT && the_drd->get_attribute(L(t), "prog") == "true"; -} - void tmu_writer::write_prog (tree t) { string func= as_string (L (t)); @@ -224,12 +220,13 @@ tmu_writer::write_prog (tree t) { write (">", false); write ("\n", false); - tree doc= t[0]; - int doc_N= N(doc); - for (int i=0; ilabel), false); - } else { + } + else { write (doc[i]); } write ("\n", false); @@ -237,7 +234,7 @@ tmu_writer::write_prog (tree t) { // __ // _ means spaces, the number is controlled by tab - for (int i=0;i", false); } - void tmu_writer::write (tree t) { if (is_atomic (t)) { @@ -305,7 +301,8 @@ tmu_writer::write (tree t) { default: if (is_tree_in_prog (t)) { write_prog (t); - } else { + } + else { apply (as_string (L (t)), A (t)); } break; diff --git a/src/Data/Convert/convert.hpp b/src/Data/Convert/convert.hpp index 5e3db2c3b9..cf0fe933fd 100644 --- a/src/Data/Convert/convert.hpp +++ b/src/Data/Convert/convert.hpp @@ -54,6 +54,12 @@ tree eqnumber_to_nonumber (tree t); string search_metadata (tree doc, string kind); /*** TMU ***/ +inline bool +is_tree_in_prog (tree t) { + return N (t) == 1 && L (t[0]) == DOCUMENT && + the_drd->get_attribute (L (t), "prog") == "true"; +} + tree tmu_to_tree (string s); tree tmu_document_to_tree (string s); string tree_to_tmu (tree t); From 4fbb3576d414178f939e97a542d13ee361a7f327 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 17:54:54 +0800 Subject: [PATCH 3/6] test doc --- TeXmacs/tests/tmu/66_8_prog.tmu | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 TeXmacs/tests/tmu/66_8_prog.tmu diff --git a/TeXmacs/tests/tmu/66_8_prog.tmu b/TeXmacs/tests/tmu/66_8_prog.tmu new file mode 100644 index 0000000000..617a73014a --- /dev/null +++ b/TeXmacs/tests/tmu/66_8_prog.tmu @@ -0,0 +1,28 @@ +> + +> + +<\body> + 开始 + + <\python-code> +import os +def less(a, b): + return a <= b + + + 结束 + + +<\initial> + <\collection> + + + + + +<\references> + <\collection> + > + + \ No newline at end of file From f2552a25752313991b5f8d4c1ffe1ab4ecdd4877 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 18:02:00 +0800 Subject: [PATCH 4/6] wip --- src/Data/Convert/Mogan/to_tmu.cpp | 1 - src/Data/Convert/convert.hpp | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Data/Convert/Mogan/to_tmu.cpp b/src/Data/Convert/Mogan/to_tmu.cpp index 34b3ca5a55..c26e8ca0b3 100644 --- a/src/Data/Convert/Mogan/to_tmu.cpp +++ b/src/Data/Convert/Mogan/to_tmu.cpp @@ -19,7 +19,6 @@ using namespace moebius; using lolly::data::binary_to_hexadecimal; using moebius::drd::std_contains; -using moebius::drd::the_drd; const string TMU_VERSION= "1.0.2"; diff --git a/src/Data/Convert/convert.hpp b/src/Data/Convert/convert.hpp index cf0fe933fd..ec3f9fa846 100644 --- a/src/Data/Convert/convert.hpp +++ b/src/Data/Convert/convert.hpp @@ -15,6 +15,8 @@ #include "hashmap.hpp" #include "tree.hpp" #include "url.hpp" +#include "tree_helper.hpp" +#include class object; @@ -56,8 +58,8 @@ string search_metadata (tree doc, string kind); /*** TMU ***/ inline bool is_tree_in_prog (tree t) { - return N (t) == 1 && L (t[0]) == DOCUMENT && - the_drd->get_attribute (L (t), "prog") == "true"; + return N (t) == 1 && L (t[0]) == moebius::DOCUMENT && + moebius::drd::the_drd->get_attribute (L (t), "prog") == "true"; } tree tmu_to_tree (string s); From 0a9424d74a79e577cbe46475bd73792a191ecc5f Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 18:20:54 +0800 Subject: [PATCH 5/6] wip --- src/Data/Convert/Mogan/to_tmu.cpp | 2 +- src/Data/Convert/convert.hpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Data/Convert/Mogan/to_tmu.cpp b/src/Data/Convert/Mogan/to_tmu.cpp index c26e8ca0b3..4b3609408c 100644 --- a/src/Data/Convert/Mogan/to_tmu.cpp +++ b/src/Data/Convert/Mogan/to_tmu.cpp @@ -298,7 +298,7 @@ tmu_writer::write (tree t) { tag (""); break; default: - if (is_tree_in_prog (t)) { + if (is_tree_in_prog (t) && N (t) == 1 && L (t[0]) == moebius::DOCUMENT) { write_prog (t); } else { diff --git a/src/Data/Convert/convert.hpp b/src/Data/Convert/convert.hpp index ec3f9fa846..3b85bceebf 100644 --- a/src/Data/Convert/convert.hpp +++ b/src/Data/Convert/convert.hpp @@ -14,8 +14,8 @@ #include "analyze.hpp" #include "hashmap.hpp" #include "tree.hpp" -#include "url.hpp" #include "tree_helper.hpp" +#include "url.hpp" #include class object; @@ -58,8 +58,7 @@ string search_metadata (tree doc, string kind); /*** TMU ***/ inline bool is_tree_in_prog (tree t) { - return N (t) == 1 && L (t[0]) == moebius::DOCUMENT && - moebius::drd::the_drd->get_attribute (L (t), "prog") == "true"; + return moebius::drd::the_drd->get_attribute (L (t), "prog") == "true"; } tree tmu_to_tree (string s); From 906a1199cedf3c6487945329c01d58903426fd0d Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 15 Jul 2024 19:05:38 +0800 Subject: [PATCH 6/6] wip --- src/Data/Convert/Mogan/from_tmu.cpp | 51 ++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Data/Convert/Mogan/from_tmu.cpp b/src/Data/Convert/Mogan/from_tmu.cpp index 530a85bf34..6d1e208314 100644 --- a/src/Data/Convert/Mogan/from_tmu.cpp +++ b/src/Data/Convert/Mogan/from_tmu.cpp @@ -50,6 +50,7 @@ struct tmu_reader { string read_char (); string read_next (); string read_function_name (); + string read_prog_line (); tree read_apply (string s, bool skip_flag); tree read (bool skip_flag); }; @@ -176,6 +177,16 @@ tmu_reader::read_function_name () { return name; } +string +tmu_reader::read_prog_line () { + int old_pos= pos; + int buf_N = N (buf); + while (((pos + 1) < buf_N) && (buf[pos] != '\n')) { + pos++; + } + return buf (old_pos, pos); +} + static void get_collection (tree& u, tree t) { if (is_func (t, COLLECTION) || is_func (t, DOCUMENT) || is_func (t, CONCAT)) { @@ -196,18 +207,36 @@ tmu_reader::read_apply (string name, bool skip_flag) { t= tree ((tree_label) codes[name]); } - bool closed= !skip_flag; - int buf_N = N (buf); - while (pos < buf_N) { - // cout << "last= " << last << LF; - bool sub_flag= (skip_flag) && ((last == "") || (last[N (last) - 1] != '|')); - if (sub_flag) (void) skip_blank (); - t << read (sub_flag); - if ((last == "/>") || (last == "/|")) closed= true; - if (closed && ((last == ">") || (last == "/>"))) break; + int buf_N= N (buf); + if (is_tree_in_prog (t)) { + tree D (DOCUMENT); + while (pos < buf_N) { + string line= read_prog_line (); + if (is_empty (line)) { + pos++; + continue; + } + if (ends (line, "")) { + break; + } + D << line; + } + t << D; + } + else { + bool closed= !skip_flag; + while (pos < buf_N) { + // cout << "last= " << last << LF; + bool sub_flag= + (skip_flag) && ((last == "") || (last[N (last) - 1] != '|')); + if (sub_flag) (void) skip_blank (); + t << read (sub_flag); + if ((last == "/>") || (last == "/|")) closed= true; + if (closed && ((last == ">") || (last == "/>"))) break; + } + // cout << "last= " << last << UNINDENT << LF; + // cout << "Done" << LF; } - // cout << "last= " << last << UNINDENT << LF; - // cout << "Done" << LF; if (is_func (t, COLLECTION)) { tree u (COLLECTION);