From 09e2b217dba1741483e9e7ad72dd804d72773992 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Uwe=20St=C3=B6hr?= Date: Tue, 22 Jan 2013 01:07:52 +0100 Subject: [PATCH] tex2lyx: support for listings with options (bug 8066) --- src/tex2lyx/Parser.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ src/tex2lyx/Parser.h | 13 +++++++++++++ src/tex2lyx/text.cpp | 37 +++++++++++++++++++++++------------- status.20x | 1 + 4 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/tex2lyx/Parser.cpp b/src/tex2lyx/Parser.cpp index 3d09e01b00..0067f88721 100644 --- a/src/tex2lyx/Parser.cpp +++ b/src/tex2lyx/Parser.cpp @@ -492,6 +492,49 @@ string const Parser::verbatimEnvironment(string const & name) } +string const Parser::plainEnvironment(string const & name) +{ + if (!good()) + return string(); + + ostringstream os; + for (Token t = get_token(); good(); t = get_token()) { + if (t.asInput() == "\\end") { + string const end = getArg('{', '}'); + if (end == name) + return os.str(); + else + os << "\\end{" << end << '}'; + } else + os << t.asInput(); + } + cerr << "unexpected end of input" << endl; + return os.str(); +} + + +string const Parser::plainCommand(char left, char right, string const & name) +{ + if (!good()) + return string(); + // ceck if first token is really the start character + Token tok = get_token(); + if (tok.character() != left) { + cerr << "first character does not match start character of command \\" << name << endl; + return string(); + } + ostringstream os; + for (Token t = get_token(); good(); t = get_token()) { + if (t.character() == right) { + return os.str(); + } else + os << t.asInput(); + } + cerr << "unexpected end of input" << endl; + return os.str(); +} + + void Parser::tokenize_one() { catInit(); diff --git a/src/tex2lyx/Parser.h b/src/tex2lyx/Parser.h index 3ddb9df3e4..c82988bf71 100644 --- a/src/tex2lyx/Parser.h +++ b/src/tex2lyx/Parser.h @@ -202,6 +202,19 @@ public: * is parsed but not returned. */ std::string const verbatimEnvironment(std::string const & name); + /* + * The same as verbatimEnvironment(std::string const & name) but + * \begin and \end commands inside the name environment are not parsed. + * This function is designed to parse verbatim environments. + */ + std::string const plainEnvironment(std::string const & name); + /* + * Basically the same as plainEnvironment(std::string const & name) but + * instead of \begin and \end commands the parsing is started/stopped + * at given characters. + * This function is designed to parse verbatim commands. + */ + std::string const plainCommand(char left, char right, std::string const & name); /*! * Returns the character of the current token and increments * the token position. diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 3e38367c21..55b746befb 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -1110,7 +1110,7 @@ void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer, } -void parse_listings(Parser & p, ostream & os, Context & parent_context) +void parse_listings(Parser & p, ostream & os, Context & parent_context, bool in_line) { parent_context.check_layout(os); begin_inset(os, "listings\n"); @@ -1118,14 +1118,26 @@ void parse_listings(Parser & p, ostream & os, Context & parent_context) string arg = p.verbatimOption(); os << "lstparams " << '"' << arg << '"' << '\n'; if (arg.find("\\color") != string::npos) - preamble.registerAutomaticallyLoadedPackage("color"); + preamble.registerAutomaticallyLoadedPackage("color"); + } + if (p.hasOpt()) { + string arg = p.verbatimOption(); + os << "lstparams " << '"' << arg << '"' << '\n'; } - os << "inline false\n" - << "status collapsed\n"; + if (in_line) + os << "inline true\n"; + else + os << "inline false\n"; + os << "status collapsed\n"; Context context(true, parent_context.textclass); context.layout = &parent_context.textclass.plainLayout(); - context.check_layout(os); - string const s = p.verbatimEnvironment("lstlisting"); + string s; + if (in_line) { + s = p.plainCommand('!', '!', "lstinline"); + context.new_paragraph(os); + context.check_layout(os); + } else + s = p.plainEnvironment("lstlisting"); for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) { if (*it == '\\') os << "\n\\backslash\n"; @@ -1374,13 +1386,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, else if (name == "lstlisting") { eat_whitespace(p, os, parent_context, false); - // FIXME handle listings with parameters - if (p.hasOpt()) - parse_unknown_environment(p, name, os, FLAG_END, - outer, parent_context); - else - parse_listings(p, os, parent_context); - p.skip_spaces(); + parse_listings(p, os, parent_context, false); } else if (!parent_context.new_layout_allowed) @@ -2758,6 +2764,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, end_inset(os); } + else if (t.cs() == "lstinline") { + p.skip_spaces(); + parse_listings(p, os, context, true); + } + else if (t.cs() == "ensuremath") { p.skip_spaces(); context.check_layout(os); diff --git a/status.20x b/status.20x index d3bb1f4dd5..a76676f915 100644 --- a/status.20x +++ b/status.20x @@ -34,6 +34,7 @@ What's new * TEX2LYX IMPROVEMENTS +- support for listings with options (bug #8066). - add new option -m to select needed modules (bug #8393). -- 2.39.5