diff --git a/TeXmacs/plugins/s7/doc/s7.en.tm b/TeXmacs/plugins/s7/doc/s7.en.tm index 9dbcd620cd..b83d0bf074 100644 --- a/TeXmacs/plugins/s7/doc/s7.en.tm +++ b/TeXmacs/plugins/s7/doc/s7.en.tm @@ -264,7 +264,7 @@ <|unfolded-io> (random 1.0) <|unfolded-io> - 0.5606284330786007 + 0.848072511242711 <\unfolded-io> @@ -272,7 +272,7 @@ <|unfolded-io> (random 3/4) <|unfolded-io> - 120413/340517 + 477221/1610063 <\input> @@ -344,7 +344,7 @@ <|unfolded-io> (define test-tb (hash-table 'a 1 'b 2)) <|unfolded-io> - (hash-table 'b 2 'a 1) + (hash-table 'a 1 'b 2) <\unfolded-io> diff --git a/TeXmacs/plugins/s7/src/tm_s7.cpp b/TeXmacs/plugins/s7/src/tm_s7.cpp index 006ba5c7c6..bc2d943765 100644 --- a/TeXmacs/plugins/s7/src/tm_s7.cpp +++ b/TeXmacs/plugins/s7/src/tm_s7.cpp @@ -4,6 +4,7 @@ #include #include +#include #include using std::cout; @@ -12,9 +13,9 @@ using std::string; #include "s7.h" -#define DATA_BEGIN ((char) 2) -#define DATA_END ((char) 5) -#define DATA_ESCAPE ((char) 27) +#define DATA_BEGIN ((char) 2) +#define DATA_END ((char) 5) +#define DATA_ESCAPE ((char) 27) void data_begin () { @@ -26,6 +27,13 @@ data_end () { cout << DATA_END << flush; } +void +flush_scheme (string msg) { + data_begin (); + cout << "scheme:" << msg; + data_end (); +} + void flush_verbatim (string msg) { data_begin (); @@ -40,11 +48,42 @@ flush_prompt (string prompt) { data_end (); } +string +getBeforeSpace (const string& str) { + size_t pos= str.find (' '); + if (pos == string::npos) { + return str; + } + return str.substr (0, pos); +} + +std::string::size_type +findBegin (const std::string& s) { + std::string::size_type pos= s.find_first_not_of (" \t\n\r\f\v"); + return (pos == std::string::npos) ? s.length () : pos; +} + +std::string::size_type +findEnd (const std::string& s) { + std::string::size_type pos= s.find_last_not_of (" \t\n\r\f\v"); + return (pos == std::string::npos) ? 0 : pos + 1; +} + +std::string +trim (const std::string& s) { + std::string::size_type left = findBegin (s); + std::string::size_type right= findEnd (s); + return s.substr (left, right - left); +} + int main (int argc, char** argv) { + std::unordered_set scheme_headers= { + "(document", "(math", "(equation*", "(align", "(with", "(graphics"}; + std::stringstream welcome; welcome << "S7 Scheme " << S7_VERSION << " (" << S7_DATE << ")\n"; - flush_verbatim (welcome.str()); + flush_verbatim (welcome.str ()); flush_prompt ("> "); s7_scheme* sc; @@ -59,15 +98,28 @@ main (int argc, char** argv) { std::vector lines; lines.push_back (first_line); std::stringstream ss; - string line= first_line; + string line= first_line; while (line != "") { ss << line << "\n"; line= ""; std::getline (std::cin, line); } - s7_pointer x= s7_eval_c_string (sc, ss.str().c_str ()); - - flush_verbatim (s7_object_to_c_string (sc, x)); + s7_pointer x = s7_eval_c_string (sc, ss.str ().c_str ()); + string result= s7_object_to_c_string (sc, x); + if (result.size () == 0) { + flush_verbatim (""); + } + else { + string trimmed= trim (result); + string head = getBeforeSpace (trimmed); + if (trimmed[trimmed.size () - 1] == ')' && + scheme_headers.find (head) != scheme_headers.end ()) { + flush_scheme (trimmed); + } + else { + flush_verbatim (result); + } + } } return 0; diff --git a/bin/format b/bin/format index 4986388d2d..716c6b4e41 100755 --- a/bin/format +++ b/bin/format @@ -22,4 +22,5 @@ clang-format -i tests/**/*.cpp clang-format -i tests/**/*.hpp clang-format -i src/**/*.cpp clang-format -i src/**/*.hpp +clang-format -i TeXmacs/plugins/**/*.cpp