Author: uwestoehr
Date: Mon Mar  5 23:04:22 2012
New Revision: 40860
URL: http://www.lyx.org/trac/changeset/40860

Log:
tex2lyx: support for inline listings (fixes last part of #8066)
OK also for branch?

Modified:
   lyx-devel/trunk/src/tex2lyx/Parser.cpp
   lyx-devel/trunk/src/tex2lyx/Parser.h
   lyx-devel/trunk/src/tex2lyx/text.cpp

Modified: lyx-devel/trunk/src/tex2lyx/Parser.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/Parser.cpp      Mon Mar  5 22:04:21 2012        
(r40859)
+++ lyx-devel/trunk/src/tex2lyx/Parser.cpp      Mon Mar  5 23:04:22 2012        
(r40860)
@@ -502,6 +502,28 @@
 }
 
 
+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();

Modified: lyx-devel/trunk/src/tex2lyx/Parser.h
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/Parser.h        Mon Mar  5 22:04:21 2012        
(r40859)
+++ lyx-devel/trunk/src/tex2lyx/Parser.h        Mon Mar  5 23:04:22 2012        
(r40860)
@@ -202,6 +202,13 @@
        * 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.

Modified: lyx-devel/trunk/src/tex2lyx/text.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/text.cpp        Mon Mar  5 22:04:21 2012        
(r40859)
+++ lyx-devel/trunk/src/tex2lyx/text.cpp        Mon Mar  5 23:04:22 2012        
(r40860)
@@ -1118,7 +1118,7 @@
 }
 
 
-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");
@@ -1133,11 +1133,20 @@
                }
                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();
-       string const s = p.plainEnvironment("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";
@@ -1399,7 +1408,7 @@
                eat_whitespace(p, os, parent_context, false);
                // FIXME handle the automatic color package loading
                // uwestoehr asks: In what case color is loaded?
-               parse_listings(p, os, parent_context);
+               parse_listings(p, os, parent_context, false);
                p.skip_spaces();
        }
 
@@ -2787,6 +2796,11 @@
                        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);

Reply via email to